forked from SiliconEngine/PerlToPHP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPpiToken.php
1574 lines (1343 loc) · 47.7 KB
/
PpiToken.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* PPI token nodes.
*
* @author Tim Behrendsen <tim@siliconengine.com>
* @created 2016-10-14
*/
class PpiTokenWhitespace extends PpiToken { }
class PpiTokenPod extends PpiToken { }
class PpiTokenNumberBinary extends PpiTokenNumber { }
class PpiTokenNumberOctal extends PpiTokenNumber { }
class PpiTokenNumberHex extends PpiTokenNumber { }
class PpiTokenNumberFloat extends PpiTokenNumber { }
class PpiTokenNumberExp extends PpiTokenNumberFloat { }
class PpiTokenNumberVersion extends PpiTokenNumber { }
class PpiTokenDashedWord extends PpiToken { }
class PpiTokenQuoteSingle extends PpiTokenQuote { }
class PpiTokenQuoteLiteral extends PpiTokenQuote { }
class PpiTokenQuoteLike extends PpiToken { }
class PpiTokenQuoteLikeBacktick extends PpiTokenQuoteLike { }
class PpiTokenQuoteLikeCommand extends PpiTokenQuoteLike { }
class PpiTokenQuoteLikeRegexp extends PpiTokenQuoteLike { }
class PpiTokenQuoteLikeReadline extends PpiTokenQuoteLike { }
class PpiTokenRegexpMatch extends PpiTokenRegexp { }
class PpiTokenRegexpSubstitute extends PpiTokenRegexp { }
class PpiTokenRegexpTransliterate extends PpiTokenRegexp { }
class PpiTokenHereDoc extends PpiToken { }
class PpiTokenStructure extends PpiToken { }
class PpiTokenSeparator extends PpiToken { }
class PpiTokenData extends PpiToken { }
class PpiTokenEnd extends PpiToken { }
class PpiTokenPrototype extends PpiToken { }
class PpiTokenAttribute extends PpiToken { }
class PpiTokenUnknown extends PpiToken { }
// Added token not in Perl
class PpiTokenNewline extends PpiTokenWhitespace { }
class PpiToken extends PpiElement
{
public function anaContext()
{
// Default to prior token's context, unless newline
if ($this->prev->isNewline()) {
$this->setContext('scalar');
} else {
$this->setContext($this->getPrevSiblingUpTree()->context);
}
}
}
/**
* Usually a variable name
*/
class PpiTokenSymbol extends PpiToken
{
public function anaContext()
{
switch (substr($this->content, 0, 1)) {
default:
case '&':
case '$':
if ($this->prevSibling !== null &&
$this->prevSibling->content == '@') {
$this->setContextChain('array');
break;
}
// Scalar variable, may or may not be scalar expression
$this->setContextChain('scalar');
break;
case '@':
// Hard to create a general rule for array or scalar here.
// This is all precariously balanced.
if ($this->prevSibling !== null &&
($this->prevSibling->content == 'my' ||
$this->prevSibling->content == '\\')) {
// my @var or \@var
$this->setContextChain('array');
} elseif ($this->parent->context == 'array' ||
$this->prev->context == 'array') {
// If the parent is array context, or the left token is a
// array context, call it array context.
$this->setContextChain('array');
} elseif ($this->prevSibling !== null &&
$this->prevSibling->context == 'scalar') {
// If left sibling is scalar, then call it scalar.
$this->setContext('scalar');
} elseif ($this->parent instanceof PpiStatementExpression) {
// Use context of parent. Not sure if this is reliable.
$this->setContextChain($this->parent->context);
} elseif ($this->prevSibling !== null &&
$this->prevSibling->content == '=') {
// An '=' sign implies scalar
$this->setContextChain('scalar');
} else {
$this->setContextChain('array');
}
break;
case '%':
$this->setContextChain('hash');
break;
}
}
function genCode()
{
if (! $this->converted) {
$varName = $this->content;
switch (substr($varName, 0, 1)) {
case '$':
// Normal variable
break;
case '@':
if ($varName == '@ISA' || $varName == '@EXPORT') {
// Special case, just comment out
$varName = "//{$varName}";
} else {
// Array, change to normal variable if array context
// otherwise convert to count.
if ($this->context == 'scalar') {
$varName = 'count($' .
$this->cvtCamelCase(substr($varName, 1)) . ')';
// If there's a comma on either side, mark this
// as suspect.
if ($this->prev->content == ',' ||
$this->next->content == ',') {
$varName = "/*check*/$varName";
}
} else {
$varName = '$' . substr($varName, 1);
}
}
break;
case '%':
// Hash, change to normal variable
$varName = '$' . substr($varName, 1);
break;
case '&':
// Function, just strip off
$varName = substr($varName, 1);
break;
default:
// Other is most likely function
break;
}
$path = '';
if (strpos($varName, '::') !== false) {
// Word has a path, convert it
$save = '';
if (substr($varName, 0, 1) == '$') {
$varName = substr($varName, 1);
$save = '$';
}
if (preg_match('/(.*)::(.*)/', $varName, $matches)) {
$path = '\\' . $this->cvtPackageName($matches[1]) . '::';
$varName = $save . $matches[2];
}
}
// Convert variable names to camel case
if (substr($varName, 0, 1) == '$') {
$this->content = $path . '$' .
$this->cvtCamelCase(substr($varName, 1));
} else {
$this->content = $path . $this->cvtCamelCase($varName);
}
// Translate special object reference name, unless it's
// an assignment
if ($this->content == '$self' && $this->next->content != '=') {
$this->content = '$this';
}
}
return parent::genCode();
}
}
class PpiTokenQuoteDouble extends PpiTokenQuote
{
function genCode()
{
if (! $this->converted) {
// If not within a subroutine, probably within class. Check if there
// are replacements in string, which we can't do in an initializer.
if (! $this->isWithinSub()) {
$this->content = str_replace('$', '$/*check*/', $this->content);
}
}
return parent::genCode();
}
}
/**
* Special count syntax like "$#var"
*/
class PpiTokenArrayIndex extends PpiTokenSymbol
{
function genCode()
{
if (! $this->converted) {
if (substr($this->content, 0, 2) != '$#') {
error_log("WARNING: Unknown array index syntax: {$this->content}");
return;
}
$var = '$' . substr($this->content, 2);
$this->content = "(count($var)-1)";
}
return parent::genCode();
}
}
/**
* "Magic" token (special variables)
*/
class PpiTokenMagic extends PpiTokenSymbol
{
function genCode()
{
if (! $this->converted) {
$name = substr($this->content, 1);
// Check for numbered variable (e.g., $1)
if (ctype_digit($name)) {
$this->content = "\$fake/*check:{$this->content}*/";
} else switch ($this->content) {
default:
$this->content = "\$fake/*check:{$this->content}*/";
break;
}
}
return parent::genCode();
}
}
/**
* Label
* Nothing to do except convert some unfortunate names that are reserved
* words in PHP.
*/
class PpiTokenLabel extends PpiToken
{
function genCode()
{
if (! $this->converted) {
if ($this->content == 'EXIT:') {
$this->content = 'EXIT_LABEL:';
}
}
return parent::genCode();
}
}
class PpiTokenQuote extends PpiToken
{
public function anaContext()
{
$this->setContext('string');
}
}
class PpiTokenNumber extends PpiToken
{
public function anaContext()
{
$this->setContext('scalar');
}
function genCode()
{
if (! $this->converted) {
// If we have the case of "1;" that needs to be at the end
// of perl modules, remove it.
if ($this->parent instanceof PpiStatement
&& $this->parent->parent instanceof PpiDocument
&& $this->prevSibling === null
&& $this->next->isSemicolon() == ';'
&& $this->next->next->isNewline()) {
$this->cancel();
$this->next->cancel();
$this->next->next->cancel();
}
}
return parent::genCode();
}
}
class PpiTokenRegexp extends PpiToken
{
function genCode()
{
if (! $this->converted) {
$regex = $this->content;
// Need to escape single quotes in regex expression
$regex = str_replace("'", "\\'", $regex);
$this->content = "'$regex'";
}
return parent::genCode();
}
}
class PpiTokenCast extends PpiToken
{
public function anaContext()
{
switch ($this->content) {
case '@':
// Might be scalar or array
$this->setContext($this->prev->context);
// $this->setContext($this->parent->context);
break;
case '%':
$this->setContextChain('hash');
break;
case '\\':
$this->setContextChain('neutral');
break;
case '$#':
$this->setContextChain('scalar');
break;
case '&':
$this->setContextChain('scalar');
break;
default:
// Default to scalar
$this->setContextChain('scalar');
break;
}
}
function genCode()
{
if (! $this->converted) {
switch ($this->content) {
case '$#': // count() - 1
case '@':
$needMinus = $this->content == '$#';
// Need to check context to see if this is a cast-to-array or
// it's a count of an array.
$next = $this->next;
if ($this->context == 'scalar') {
// Convert to count expression
$text = '';
if ($next instanceof PpiStructureBlock) {
// Something like @{expression}
$text = $this->next->getRecursiveContent();
$this->next->cancelAll();
$this->content = "count(" . substr($text, 1, -1) . ")";
$this->converted = true;
} elseif ($next instanceof PpiTokenSymbol) {
// Something like @$a
$this->content = "count({$next->genCode()})";
$next->cancel();
} else {
error_log("WARNING: Unknown cast following type: " .
get_class($next));
break;
}
if ($needMinus) {
$this->content = "({$this->genCode()}-1)";
}
} else {
// Array context, just remove the cast
$this->content = '';
if ($next instanceof PpiStructureBlock) {
// Change to parentheses, unless it's just a single
// word in the interior.
$text = $next->getRecursiveContent();
$next->converted = true;
if (preg_match('/\{\$\w+\}/', $text)) {
$next->cancel();
} else {
$next->startContent = '(';
$next->endContent = ')';
$next->converted = true;
}
}
}
break;
case '%':
// Comment out hash cast and remove braces if any
$this->content = "/*check:{$this->content}*/";
if ($this->next->startContent == '{') {
$this->next->startContent = '';
$this->next->endContent = '';
}
break;
case '&':
// Function cast, just comment out.
$this->content = '/*check:&*/';
// Change braces to parentheses
if ($this->next instanceof PpiStructureBlock) {
$this->next->startContent = '';
$this->next->endContent = '';
$this->next->converted = true;
}
break;
default:
$this->content = "/*check:{$this->content}*/";
break;
}
}
return parent::genCode();
}
}
class PpiTokenOperator extends PpiToken
{
public function anaContext()
{
switch ($this->content) {
case ',':
// Special logic, might be an array list or a scalar expression
// separator.
// See: PpiStructureList for notes.
//
// Always scalar, because it's marking off scalars even in lists
// $this->setContext('scalar');
$this->setContext($this->prev->context);
break;
case '.':
$this->setContextChain('string');
break;
case '=':
// lvalue generally determines context
$context = $this->getPrevSiblingUpTree()->context;
$this->setContextChain($this->getPrevSiblingUpTree()->context);
$this->parent->context = $context;
break;
case '..':
$this->setContext('array');
break;
default:
// Default to scalar
$this->setContextChain('scalar');
}
}
function genCode()
{
if (! $this->converted) {
switch ($this->content) {
case 'eq':
$this->content = '===';
break;
case 'ne':
$this->content = '!==';
break;
case 'lt':
$this->content = '<';
break;
case 'gt':
$this->content = '>';
break;
case 'not':
$this->content = '!';
break;
case 'and':
$this->content = '&&';
break;
case 'or':
$this->content = '||';
break;
case '->':
$next = $this->getNextNonWs();
if (! ($next instanceof PpiTokenWord)) {
$this->cancel();
}
break;
case '=~':
return $this->cvtRegEx();
case '!~':
return $this->cvtRegEx(true);
case 'x':
return $this->cvtStrRepeat();
case '-e':
return $this->cvtFileExists();
case '..':
return $this->cvtRange();
}
}
return parent::genCode();
}
/**
* Convert -e (file exists) operator.
*/
function cvtFileExists()
{
list($expr, $right) = $this->getRightArg();
$this->content = "file_exists($expr)";
}
function cvtRegEx($neg = false)
{
$left = $this->prevSibling;
$right = $this->next;
$right->preWs = '';
list($var, $left) = $this->getLeftArg();
// Get right side regex expression, which might come back with
// single quotes.
list($regex, $last) = $this->getRightArg();
if (substr($regex, 0, 2) == "'s") {
// Substitute, need to split into regex and substitute
// Strip the quotes and the 's' first
$regex = substr($regex, 2, -1);
$regex = preg_replace('/\\w*$/', '', $regex);
$delim = substr($regex, 0, 1);
if ($delim == '/') {
$delim = '\\' . $delim;
}
// Split into two strings. Have to check for escapes. Note the
// look-behind assertion for a backslash.
if (preg_match("/($delim.*(?<!\\\\)$delim)(.*)$delim/", $regex,
$matches)) {
$pattern = trim($matches[1]);
$replace = $matches[2];
$leftVar = $var = trim($var);
// Check for special case that looks like this:
// ($x = $y) =~ s/pattern/subst/;
if (preg_match('/\(\s*(\$\w+)\s*=/', $leftVar, $matches)) {
$leftVar = $matches[1];
}
$this->content = "$leftVar = preg_replace('$pattern', " .
"'$replace', $var)";
$this->preWs = $left->preWs;
} else {
// Can't convert
return parent::genCode();
}
} else {
// Match
$regex = trim($regex);
$var = trim($var);
$this->preWs = $left->preWs;
$this->content = "preg_match($regex, $var)";
if ($neg) {
$this->content = "! ({$this->content})";
}
}
return parent::genCode();
}
/**
* Range operatior '..'
*/
function cvtRange()
{
list($leftText, $left) = $this->getLeftArg();
list($rightText, $right) = $this->getRightArg();
$this->content = "range($leftText, $rightText)";
return parent::genCode();
}
/**
* Convert 'x' (string repeat) operator.
*/
private function cvtStrRepeat()
{
list($leftText, $left) = $this->getLeftArg();
list($rightText, $right) = $this->getRightArg();
$this->content = "str_repeat($leftText, $rightText)";
$this->preWs = $left->preWs;
return parent::genCode();
}
}
class PpiTokenComment extends PpiToken
{
public function anaContext()
{
$this->setContext('neutral');
}
function genCode()
{
if (! $this->converted) {
$comment = preg_replace('/^#/', '//', $this->content);
$this->content = $comment;
// If block comment, convert style to PHPDOC
if (preg_match('/^\/\/####/', $comment, $matches)) {
$this->content = "/**";
$obj = $this->next;
$first = true;
$open = true;
while ($obj !== null && ($obj instanceof PpiTokenComment
|| $obj instanceof PpiTokenNewline)) {
$s = $obj->content;
/* # # */
if ($obj instanceof PpiTokenNewline) {
// Pass through newlines
} elseif (preg_match('/^#\s*#$/', $s, $matches)) {
// Remove blanks if directly following top
if (! $first) {
$obj->content = " *";
$obj->converted = true;
} else {
$obj->cancel();
$obj->next->cancel(); // Newline
}
/* # text # */
} elseif (preg_match('/^#(\s+)(.*)#\s*$/', $s, $matches)) {
$first = false;
$obj->content = " *{$matches[1]}" . rtrim($matches[2]);
$obj->converted = true;
// Remove "SubName - ", which was old comment convention
$obj->content = preg_replace('/^(\s+\*\s+)\w+ - /',
'\1', $obj->content);
/* ####### */
} elseif (preg_match('/^#+\s*$/', $s, $matches)) {
$first = false;
$obj->content = " */";
$obj->converted = true;
// If previous was blank, delete it
$last = $obj->prev->prev;
if (preg_match('/^\s*\*\s*$/', $last->content)) {
$last->cancel();
$last->next->cancel(); // Newline
}
$open = false;
break;
}
$obj = $obj->next;
}
// If was unclosed, go ahead and close it.
if ($open) {
$obj->content .= "\n */\n";
}
}
}
return parent::genCode();
}
}
/**
* string like "qw(abc def)";
*/
class PpiTokenQuoteLikeWords extends PpiTokenQuoteLike
{
public function anaContext()
{
$this->setContextChain('array');
}
function genCode()
{
if (! $this->converted) {
$d = trim(substr($this->content, 3, -1));
$list = preg_split('/\s+/', $d);
$this->content = '[ ' . implode(', ', array_map(function ($s) {
return (is_numeric($s)) ? $s : "'$s'";
}, $list)) . ' ]';
}
return parent::genCode();
}
}
/**
* Process general token word, this is where a lot of the action takes
* place.
*/
class PpiTokenWord extends PpiToken
{
public function anaContext()
{
switch ($this->content) {
case 'shift':
case 'unshift':
case 'pop':
case 'push':
case 'split':
case 'delete':
case 'keys':
case 'sort':
$this->setContextChain('array');
break;
case 'join':
case 'uc':
case 'lc':
case 'ucwords':
$this->setContextChain('string');
break;
case 'defined':
case 'length':
$this->setContextChain('scalar');
break;
case 'my':
// If next token is parenthesis or start of array, then use
// array context
$node = $this->getNextNonWs();
if ($node->startContent == '(' ||
substr($node->content, 0, 1) == '@') {
$this->setContext('array');
} else {
$this->setContext('scalar');
}
break;
case 'return':
// Check for the "return (1, 2)" case.
// Scalar: return (1)
// Array: return (1, 2)
$context = 'scalar';
$obj = $this->next;
if ($obj->startContent == '(') {
foreach ($obj->next->children as $child) {
if ($child->content == ',') {
$context = 'array';
break;
}
}
}
$this->setContext($context);
break;
default:
// Some sort of function, most likely
$this->setContext('scalar');
break;
}
}
function genCode()
{
if (! $this->converted) {
$word = $this->content;
if (strpos($word, '::') !== false) {
return $this->tokenWordPackageName();
}
if ($this->parent instanceof PpiStatementExpression) {
// Check for bareword hash index. Sibling should be null,
// which means word is by itself.
if ($this->parent->parent instanceof PpiStructureSubscript
&& $this->prevSibling === null
&& $this->nextSibling === null) {
return $this->quoteBareWord();
}
// Check for a bareword key: abc => 'def'
if ($this->next->content == '=>') {
return $this->quoteBareWord();
}
}
if (! $this->isReservedWord($word)) {
// Looks like some sort of function name or variable
$this->content = $this->cvtCamelCase($word);
return parent::genCode();
}
if ($this->parent instanceof PpiStatement) {
switch($word) {
case 'my': $this->tokenWordMy(); break;
case 'split': $this->tokenWordSplit(); break;
case 'shift':
$this->tokenWordWithArg('array_shift(%s)');
break;
case 'pop':
$this->tokenWordWithArg('array_pop(%s)');
break;
case 'uc':
$this->tokenWordWithArg('strtoupper(%s)');
break;
case 'lc':
$this->tokenWordWithArg('strtolower(%s)');
break;
case 'delete':
$this->tokenWordWithArg('unset(%s)');
break;
case 'keys':
$this->tokenWordWithArg('array_keys(%s)');
break;
case 'close':
$this->tokenWordWithArg('close(%s)');
break;
case 'sort':
$this->tokenWordWithArg('$fake/*check:sort(%s)*/');
break;
case 'bless':
$this->tokenWordCommentOut();
break;
case 'grep':
$this->tokenWordGrep();
break;
case 'unshift':
$this->content = 'array_unshift';
break;
case 'push':
$this->content = 'array_push';
break;
case 'length':
$this->tokenWordWithArg('strlen(%s)');
break;
case 'int':
$this->tokenWordWithArg('floor(%s)');
break;
case 'defined':
$this->tokenWordWithArg('/*check*/isset(%s)');
break;
case 'sub': $this->tokenWordSub(); break;
case 'package': $this->tokenWordPackage(); break;
case 'require': $this->tokenWordUse(); break;
case 'use': $this->tokenWordUse(); break;
case 'elsif': $this->content = 'elseif'; break;
case 'foreach': $this->tokenWordForeach(); break;
case 'if':
$this->tokenWordConditionals();
break;
case 'unless':
$this->tokenWordConditionals();
break;
case 'STDERR':
case 'local':
$this->killContentAndWs();
break;
case 'goto':
$this->tokenWordGoto();
break;
case 'last':
$this->tokenWordLast();
break;
case 'next':
$this->tokenWordNext();
break;
case 'chop':
$this->tokenLValueWithArg('/*check:chop*/substr(%s, 0, -1)');
break;
case 'chomp':
$this->tokenLValueWithArg("/*check:chomp*/preg_replace('/\\n$/', '', %s)");
break;
case 'eval':
$this->tokenWordEval();
break;
}
} else {
print "Not statement: {$this->content}\n";
}
}
return parent::genCode();
}
private function quoteBareWord()
{
$word = $this->content;
// Put quotes around bareweord
$c = substr($word, 0, 1);
if ($c != '"' && $c != "'") {
$this->content = "'$word'";
}
return parent::genCode();
}
private function tokenWordSub()
{
$parent = $this->parent;
// Need to look at context
if ($parent instanceof PpiStatementVariable
|| $this->prev->content == '=') {
// Anonymous function
$this->content = 'function ()';
} elseif ($parent instanceof PpiStatementSub) {
// Function context
$argList = [];
$obj = $this->getNextNonWs();
// Get the name of the function
$name = $this->cvtCamelCase($obj->content);
if ($this->isPhpReservedWord($name)) {
$name = 'x' . ucfirst($name);
}
$obj->cancel();
$obj = $obj->getNextNonWs();
// If parentheses follow the function (prototype), just kill it
if ($obj instanceof PpiTokenPrototype) {
$obj->cancelAll();
$obj = $obj->getNextNonWs();
}
if ($obj instanceof PpiStructureBlock) {
// Try to figure out an argument list
// First check for the easy case of "my ($var, $var2) = @_;"
$obj = $obj->getNextNonWs();
$firstObj = $obj;
$argList = [];
$found = false;
$saveObj = $obj;
if ($obj instanceof PpiStatementVariable) {
$max = 200;
$child = $obj->children[0];
$lastChild = end($obj->children);
while (! $child->isSemicolon() && --$max > 0) {
if ($child->content == '=' &&
$child->next->content == '@_') {
$child = $child->next;
$found = true;
// Cancel object and all children