forked from SiliconEngine/PerlToPHP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPpiStructure.php
344 lines (298 loc) · 10.8 KB
/
PpiStructure.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php
/**
* PPI structure nodes.
*
* @author Tim Behrendsen <tim@siliconengine.com>
* @created 2016-10-14
*/
class PpiStructureGiven extends PpiStructure { }
class PpiStructureWhen extends PpiStructure { }
class PpiStructureUnknown extends PpiStructure { }
/**
* Block - note that this isn't only code blocks, can be array/hash context.
*/
class PpiStructureBlock extends PpiStructure { }
class PpiStructure extends PpiNode
{
/**
* anaContext used with code parenthesis, like PpiStructureList
* and PpiStructureCondition.
*
* PpiStructureCondition can happen with:
* print if ($var) = func();
* I think this might actually be a bug in PPI, but doesn't matter.
*/
protected function AnaCodeContext()
{
// Methods we can use:
//
// 1) Context is set by sub-elements, which works most of the time.
// Doesn't work with $a = (1, 2, 3), which is technically a scalar
// expression, but comes out as array because of the comma. But
// it's a rare case.
// Works: @a = (1, 2, (3 + 4))
// Doesn't Work: @a = (1 + 2, 3)
//
// 2) The other way to do it is to use the context of the parent.
// Not sure if this has issues yet.
// Works: @a = (1 + 2, 3)
// Doesn't Work: @a = (1, 2, (3 + 4))
//
// Shaky: $a = [ (1, 1, (3 + 4)) ] -> $a = [ [1, 1, [3 + 4] ]
// This should prob become: $a = array_merge([1, 1, [3 + 4])
//$this->setContext($this->parent->context);
//return;
//
// 3) Hybrid: We use #2 and get the context of the expression. AND:
// a) We make it scalar if the parent is another PpiStructureList.
// The logic is that individual elements of a array list are
// usually scalars
// b) If we get a comma and the list is a scalar, we convert it
// it to an array (see comma logic)
// c) We can possibly go from this to using array_merge
// PROBLEM: Brackets become PpiStructureConstructor, need to
// become array context. Just look for PpiStucture?
// Has a generic PpiStatement in-between.
// PROBLEM: Thinking I may not need the comma forcing an array.
//
// *** None of the above worked well
// New method:
//
// Types of brackets:
// $a = [ ]
// Brackets -> array context to scalar reference
// @a = [ ]
// Scalar reference -> cast to array (array_merge)
// $a = ( )
// Scalar expression, keep as parenthesis
// @a = ( )
// Array context, convert to brackets.
// $scalar = [ (1, 2, 3) ]
// 1. Interior is array context because of brackets.
// $scalar = ( (1, 2, 3) )
// ...
// function(1, 2, 3)
// Array context, keep as parenthesis
//
// Rules:
// 1) Brackets are:
// a) Always array context that generate a scalar reference.
// 2) Parenthesis are:
// a) Scalar if left is scalar context
// b) Array if left is array context
// 3) Elements within list are scalar
// 4) Scan backward by sibling rather than up to parent for
// context.
// 5) Special: 'return' is array if has a comma (chk in 'return')
// 6) Special: foreach $a (@$b)
// 7) Special: Empty parenthesis () is an empty array.
//
// TESTS:
// @a = (1 + 2, 3)
// @a = (1, 2, (3 + 4))
// $a = [ (1, 2, (3 + 4)) ] -> array_merge( [1, 2, (3 + 4)] )
// $a = (1, 2, 3)
// $a = [1, 2, (3 + 4) ]
// $a = [@a, @b] -> $a = array_merge($a, $b)
// ($a, $b, $c) = (1, 2, 3) -> list($a, $b, $c) = [1, 2, 3]
// my ($a, $b, $c) = (1, 2, 3) -> list($a, $b, $c) = [1, 2, 3]
// If equal sign follows parentheses, then it's an array.
$node = $this->getNextSiblingUpTree();
if ($node->content == '=') {
$this->setContext('array');
return;
}
$node = $this;
$context = null;
for(;;) {
if ($node->prevSibling === null) {
$node = $node->parent;
} else {
$node = $node->getPrevSiblingNonWs();
}
// Special case of foreach, where the second paren argument is
// implicitly an array: foreach $a (@$b)
if ($node instanceof PpiTokenSymbol) {
$firstChild = $node->parent->children[0];
if ($firstChild instanceof PpiTokenWord
&& $firstChild->content == 'foreach') {
$context = 'array';
break;
}
}
if ($node === null) {
$context = 'scalar';
break;
}
// Skip these until we get something else
if ($node instanceof PpiStatement ||
$node instanceof PpiTokenWhitespace) {
continue;
}
if ($node->context != 'neutral') {
$context = $node->context;
break;
}
}
$this->setContext($context);
return;
}
protected function genStructureCode()
{
if (! $this->converted) {
$prev = $this->getPrevNonWs();
$nextSib = $this->getNextSiblingUpTree();
if ($nextSib !== null && $nextSib->content == '=') {
// list l-value
$this->startContent = 'list(';
return parent::genCode();
}
$peek = $this->peekBehind(2, [ 'skip_ws' => true ]);
if ($peek[0]->content == '->' &&
$peek[1] instanceof PpiTokenSymbol) {
// Looks like anonymous function call: "$var->( )"
return parent::genCode();
}
// If a regular function call, don't change (ex: word('a', 'b') ).
// Exception 1: return ('a', 'b') -> return ['a', 'b'];
// Exception 2: foreach my $a (...
if (($this->prev instanceof PpiTokenWord
|| $this->prev instanceof PpiTokenSymbol)
&& $this->prev->content != 'return'
&& $this->parent->children[0]->content != 'foreach') {
// In perl, you can have a comma at the end, but no in PHP.
// Kill any stray commas.
if ($this->nextSibling !== null) {
$obj = $this->nextSibling->prev;
while ($obj !== null && $obj->isWs()) {
$obj = $obj->prev;
}
if ($obj !== null && $obj->content == ',') {
$obj->cancel();
}
}
return parent::genCode();
}
// Empty parentheses should be converted to brackets
if (count($this->children) == 0) {
$this->startContent = '[';
$this->endContent = ']';
return parent::genCode();
}
$obj = $this->getNextNonWs();
$childHasComma = $obj->childHasComma();
$childList = $obj->children;
$firstChild = $childList[0]->skipWs();
// If a list is contained in a list, just kill the parentheses
// to combine the lists.
// Example: func(1, 2, (3, 4)) => func(1, 2, 3, 4);
if ($this->getPrevNonWs()->content == ','
&& $this->parent->parent instanceof PpiStructureList
&& $childHasComma) {
$this->startContent = '';
$this->endContent = '';
return parent::genCode();
}
// If list with arrays (but not arrayref), then convert to
// array_merge
if (in_array($firstChild->context, [ 'array', 'hash' ])
&& ! in_array($firstChild->startContent, [ '[', '{' ])
&& $childHasComma) {
$this->startContent = 'array_merge(';
return parent::genCode();
}
// If array context, convert to brackets
if (in_array($this->context, ['array', 'hash'])) {
$this->startContent = '[';
$this->endContent = ']';
}
}
return parent::genCode();
}
}
/**
* Condition structure
*/
class PpiStructureCondition extends PpiStructure
{
public function anaContext()
{
$this->anaCodeContext();
}
public function genCode()
{
return $this->genStructureCode();
}
}
class PpiStructureFor extends PpiStructure
{
public function anaContext()
{
// For has kind of a list
$this->setContext('array');
}
}
/**
* Parentheses: functions, lists, etc.
*/
class PpiStructureList extends PpiStructure
{
public function anaContext()
{
$this->anaCodeContext();
}
public function genCode()
{
return $this->genStructureCode();
}
}
class PpiStructureConstructor extends PpiStructure
{
public function anaContext()
{
$this->setContextChain('array');
}
public function genCode()
{
if (! $this->converted) {
$childList = $this->getNextNonWs()->children;
$firstChild = count($childList) ? $childList[0]->skipWs() : null;
// If list with arrays (but not arrayref), then convert to
// array_merge
if ($firstChild !== null
&& in_array($firstChild->context, [ 'array', 'hash' ])) {
$this->startContent = '/*check*/array_merge(';
$this->endContent = ')';
} else {
// Might be brackets or braces, make them always brackets
$this->startContent = '[';
$this->endContent = ']';
}
}
return parent::genCode();
}
}
/**
* Array subscript
*/
class PpiStructureSubscript extends PpiStructure
{
public function anaContext()
{
if ($this->prev instanceof PpiTokenSymbol
|| $this->prev->content == '->') {
// Hash subscript
$this->setContextChain('scalar');
} else {
$this->setContextChain('array');
}
}
public function genCode()
{
if (! $this->converted) {
$this->startContent = '[';
$this->endContent = ']';
}
return parent::genCode();
}
}