forked from SiliconEngine/PerlToPHP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPpiElement.php
1032 lines (909 loc) · 27.9 KB
/
PpiElement.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
/**
* Base class for all PPI classes.
*
* @author Tim Behrendsen <tim@siliconengine.com>
* @created 2016-10-14
*/
class PpiElement
{
public $id;
public $lineNum;
public $level;
public $precedence; // Token expression precedence
public $root;
public $parent;
public $children = [];
public $next;
public $prev;
public $nextSibling;
public $prevSibling;
public $converter; // Main converter object
public $preWs = ''; // Whitespace preceding token
public $endPreWs = ''; // Whitespace preceding end content
public $content = '';
public $startContent = '';
public $endContent = '';
private $precList = [
1 => [ '[', '(', '{' ],
2 => [ '->' ],
3 => [ '++', '--' ],
4 => [ '**' ],
5 => [ '!', '~', '\\', 'and', 'unary', '+', 'and', '-' ],
6 => [ '=~', '!~' ],
7 => [ '*', '/', '%', 'x' ],
8 => [ '+', '-', '.' ],
9 => [ '<<', '>>' ],
10 => [ 'named_unary_operators' ],
11 => [ '<', '>', '<=', '>=', 'lt', 'gt', 'le', 'ge' ],
12 => [ '==', '!=', '<=>', 'eq', 'ne', 'cmp', '~~' ],
13 => [ '&' ],
14 => [ '|', '^' ],
15 => [ '&&' ],
16 => [ '||', '//' ],
17 => [ '..', ' ...' ],
18 => [ '?:' ],
19 => [ '=', '+=', '-=', '*=', 'etc.', 'goto', 'last', 'next', 'redo', 'dump' ],
20 => [ ',', '=>' ],
21 => [ 'list_operators' ],
22 => [ 'not' ],
23 => [ 'and' ],
24 => [ 'or', 'xor' ],
25 => [ 'if', 'unless' ],
30 => [ ';' ]
];
private $phpReservedWords = [
'abstract', 'and', 'array', 'as', 'break', 'callable', 'case',
'catch', 'class', 'clone', 'const', 'continue', 'declare',
'default', 'die', 'do', 'echo', 'else', 'elseif', 'empty',
'enddeclare', 'endfor', 'endforeach', 'endif', 'endswitch',
'endwhile', 'eval', 'exit', 'extends', 'final', 'finally',
'for', 'foreach', 'function', 'global', 'goto', 'if', 'implements',
'include', 'include_once', 'instanceof', 'insteadof', 'interface',
'isset', 'list', 'namespace', 'new', 'or', 'print', 'private',
'protected', 'public', 'require', 'require_once', 'return',
'static', 'switch', 'throw', 'trait', 'try', 'unset', 'use',
'var', 'while', 'xor', 'yield'
];
private $perlReservedWords = [ 'length', 'setpgrp', 'endgrent',
'link', 'setpriority', 'endhostent', 'listen', 'setprotoent',
'endnetent', 'local', 'setpwent', 'endprotoent', 'localtime',
'setservent', 'endpwent', 'log', 'setsockopt', 'endservent',
'lstat', 'shift', 'eof', 'map', 'shmctl', 'eval', 'mkdir', 'shmget',
'exec', 'msgctl', 'shmread', 'exists', 'msgget', 'shmwrite',
'exit', 'msgrcv', 'shutdown', 'fcntl', 'msgsnd', 'sin', 'fileno',
'my', 'sleep', 'flock', 'next', 'socket', 'fork', 'not', 'socketpair',
'format', 'oct', 'sort', 'formline', 'open', 'splice', 'getc',
'opendir', 'split', 'getgrent', 'ord', 'sprintf', 'getgrgid',
'our', 'sqrt', 'getgrnam', 'pack', 'srand', 'gethostbyaddr',
'pipe', 'stat', 'gethostbyname', 'pop', 'state', 'gethostent',
'pos', 'study', 'getlogin', 'print', 'substr', 'getnetbyaddr',
'printf', 'symlink', 'abs', 'getnetbyname', 'prototype', 'syscall',
'accept', 'getnetent', 'push', 'sysopen', 'alarm', 'getpeername',
'quotemeta', 'sysread', 'atan2', 'getpgrp', 'rand', 'sysseek',
'AUTOLOAD', 'getppid', 'read', 'system', 'BEGIN', 'getpriority',
'readdir', 'syswrite', 'bind', 'getprotobyname', 'readline',
'tell', 'binmode', 'getprotobynumber', 'readlink', 'telldir',
'bless', 'getprotoent', 'readpipe', 'tie', 'break', 'getpwent',
'recv', 'tied', 'caller', 'getpwnam', 'redo', 'time', 'chdir',
'getpwuid', 'ref', 'times', 'CHECK', 'getservbyname', 'rename',
'truncate', 'chmod', 'getservbyport', 'require', 'uc', 'chomp',
'getservent', 'reset', 'ucfirst', 'chop', 'getsockname', 'return',
'umask', 'chown', 'getsockopt', 'reverse', 'undef', 'chr', 'glob',
'rewinddir', 'UNITCHECK', 'chroot', 'gmtime', 'rindex', 'unlink',
'close', 'goto', 'rmdir', 'unpack', 'closedir', 'grep', 'say',
'unshift', 'connect', 'hex', 'scalar', 'untie', 'cos', 'index',
'seek', 'use', 'crypt', 'INIT', 'seekdir', 'utime', 'dbmclose',
'int', 'select', 'values', 'dbmopen', 'ioctl', 'semctl', 'vec',
'defined', 'join', 'semget', 'wait', 'delete', 'keys', 'semop',
'waitpid', 'DESTROY', 'kill', 'send', 'wantarray', 'die', 'last',
'setgrent', 'warn', 'dump', 'lc', 'sethostent', 'write', 'each',
'lcfirst', 'setnetent',
'__DATA__', 'else', 'lock', 'qw', '__END__', 'elsif', 'lt', 'qx',
'__FILE__', 'eq', 'm', 's', '__LINE__', 'exp', 'ne', 'sub',
'__PACKAGE__', 'for', 'no', 'tr', 'and', 'foreach', 'or', 'unless',
'cmp', 'ge', 'package', 'until', 'continue', 'gt', 'q', 'while',
'CORE', 'if', 'qq', 'xor', 'do', 'le', 'qr', 'y', 'STDERR'
];
/**
* What context the node is in: neutral, array, hash, scalar, string
*/
public $context = null;
/**
* Converter has been run
*/
public $converted = false;
/**
* Whether this node's content should be canceled.
*/
public $cancel = false;
/**
* Default code generator: just mark as converted, and return content.
*/
public function genCode()
{
$this->converted = true;
return $this->content;
}
/**
* Analyze lexical tree and figure out context for each node
*/
public function analyzeTreeContext()
{
// Try and get the context if we can already determine it
if ($this->context === null) {
$this->anaContext();
}
foreach ($this->children as $child) {
if (! $child->cancel) {
$child->analyzeTreeContext();
}
}
// Try again to get the context if the children gave us a clue
if ($this->context === null) {
$this->anaContext();
}
}
public function setContext(
$context)
{
if ($context !== null && $this->context === null) {
$this->context = $context;
// debug
// $this->context = $context . '/' . $this->id;
}
}
public function anaContext()
{
return;
}
/**
* Set context for node and all unset parent nodes
*/
public function setContextChain(
$context)
{
$node = $this;
while ($node !== null && $node->context === null) {
$node->context = $context;
$node = $node->parent;
}
return;
}
/**
* Get content for node and all children.
*/
public function getRecursiveContent()
{
// Call all the converters first, otherwise we might grab content
// before it can be modified by a converter.
$this->callRecursiveConverters();
// Get the content recursively.
return $this->recurseContent();
}
private function recurseContent()
{
if (! $this->converted) {
$this->genCode();
}
$s = '';
if (! $this->cancel) {
$s = $this->preWs . $this->startContent . $this->content;
}
foreach ($this->children as $child) {
$s .= $child->recurseContent();
}
if (! $this->cancel) {
$s .= $this->endPreWs . $this->endContent;
}
return $s;
}
/**
* Call converter for node and all children.
*/
public function callRecursiveConverters()
{
$this->genCode();
foreach ($this->children as $child) {
$child->callRecursiveConverters();
}
}
/**
* Cancel this element from generating data
*/
public function cancel()
{
$this->cancel = true;
$this->converted = true;
}
public function uncancel()
{
$this->cancel = false;
}
/**
* Cancel all objects from current until passed object found.
*/
public function cancelUntil(
$last)
{
$obj = $this;
for(;;) {
$obj->cancel();
if ($obj === $last) {
break;
}
$obj = $obj->next;
}
return;
}
/**
* cancelAll - Cancel token and all children
*/
public function cancelAll()
{
$this->cancel();
foreach ($this->children as $child) {
$child->cancelAll();
}
}
/**
* Remove content and following white space
*/
public function killContentAndWs()
{
$this->content = ''; // Keep indent whitespace
for ($obj = $this; $obj = $obj->next; ++$obj) {
if ($obj instanceof PpiToken || $obj instanceof PpiStructure) {
$obj->preWs = '';
break;
}
}
return;
}
/**
* getNextToken() - Return next token or structure
*/
public function getNextToken()
{
$obj = $this;
while (! ($obj instanceof PpiToken || $obj instanceof PpiStructure)) {
$obj = $obj->next;
}
return $obj;
}
/**
* Peek ahead (n) tokens and return them.
*/
public function peekAhead(
$count,
$options = null)
{
$skipWs = isset($options['skip_ws']);
if ($count == 0) {
return [];
}
$obj = $this;
$list = [];
do {
repeat:
$obj = $obj->next;
if ($obj === null) {
break;
}
if ($skipWs && $obj instanceof PpiTokenWhitespace) {
goto repeat;
}
$list[] = $obj;
} while (--$count);
return $list;
}
/**
* Peek behind (n) tokens and return them.
*/
public function peekBehind(
$count,
$options = null)
{
$skipWs = isset($options['skip_ws']);
if ($count == 0) {
return [];
}
$obj = $this;
$list = [];
do {
repeat:
$obj = $obj->prev;
if ($obj === null) {
break;
}
if ($skipWs && $obj instanceof PpiTokenWhitespace) {
goto repeat;
}
$list[] = $obj;
} while (--$count);
return $list;
}
/**
* Quick test for white space or comment
*/
public function isWs()
{
// Note we treat root node as ws, makes things easier
return ($this instanceof PpiTokenWhitespace
|| $this instanceof PpiTokenComment
|| $this instanceof PpiDocument);
}
/**
* Skip whitespace after this object and return object;
*/
public function skipWs()
{
$obj = $this;
while ($obj !== null && $obj instanceof PpiTokenWhitespace) {
$obj = $obj->next;
}
return $obj;
}
public function skipWhitespace()
{
return $this->skipWs();
}
/**
* Get next token that isn't whitespace
*/
public function getNextNonWs()
{
$obj = $this;
do {
$obj = $obj->next;
} while ($obj !== null && $obj instanceof PpiTokenWhitespace);
return $obj;
}
/**
* Get previous token that isn't whitespace
*/
public function getPrevNonWs()
{
$obj = $this;
do {
$obj = $obj->prev;
} while ($obj !== null && $obj instanceof PpiTokenWhitespace);
return $obj;
}
/**
* Get next sibling that isn't whitespace. If hits end, goes up to
* parent to continue.
*/
public function getNextSiblingUpTree()
{
$obj = $this;
do {
if ($obj->nextSibling === null) {
$obj = $obj->parent;
} else {
$obj = $obj->nextSibling;
}
} while ($obj !== null && $obj instanceof PpiTokenWhitespace);
return $obj;
}
/**
* Get next sibling that isn't whitespace. Returns null if hit end.
*/
public function getNextSiblingNonWs()
{
$obj = $this;
do {
$obj = $obj->nextSibling;
} while ($obj !== null && $obj instanceof PpiTokenWhitespace);
return $obj;
}
/**
* Get previous sibling that isn't whitespace. If hits front, goes
* up to parent to continue.
*/
public function getPrevSiblingUpTree()
{
$obj = $this;
do {
if ($obj->prevSibling === null) {
$obj = $obj->parent;
} else {
$obj = $obj->prevSibling;
}
} while ($obj !== null && $obj instanceof PpiTokenWhitespace);
return $obj;
}
/**
* Get previous sibling that isn't whitespace. If hits front, returns null.
*/
public function getPrevSiblingNonWs()
{
$obj = $this;
do {
$obj = $obj->prevSibling;
} while ($obj !== null && $obj instanceof PpiTokenWhitespace);
return $obj;
}
/**
* Find lowest end leaf of a node's children
*/
public function getLastLeaf()
{
$obj = $this;
while (count($obj->children)) {
$obj = end($obj->children);
}
return $obj;
}
/**
* Check if object is a new line
*/
public function isNewline()
{
return $this instanceof PpiTokenNewline;
}
/**
* Move until newline found.
*/
public function findNewline()
{
$obj = $this;
while ($obj !== null && ! ($obj instanceof PpiTokenNewline)) {
$obj = $obj->next;
}
return $obj;
}
/**
* Extract entire line of elements.
*/
public function isolateLineElements()
{
$leftList = [];
$rightList = [];
$obj = $this;
do {
$obj = $obj->prev;
$leftList[] = $obj;
} while (! $obj->isNewline());
$obj = $this;
do {
$obj = $obj->next;
$rightList[] = $obj;
} while (! $obj->isNewline());
$ret = new \stdClass;
$ret->leftList = array_reverse($leftList);
$ret->rightList = $rightList;
return $ret;
}
/**
* Check if node is within a subroutine
*/
public function isWithinSub()
{
$obj = $this;
while ($obj !== null) {
if ($obj instanceof PpiStatementSub) {
return true;
}
$obj = $obj->parent;
}
return false;
}
/**
* Check if node has a semicolon at the end. Note that nodes might
* have text before the semicolon if they had modifications.
*/
public function isSemicolon()
{
return substr($this->content, -1, 1) == ';';
}
/**
* Figure out the indent for the line of the current token.
*/
public function getIndent()
{
// Scan backward for newline
$obj = $this;
while ($obj->prev !== null && ! $obj->isNewline()) {
$obj = $obj->prev;
}
// Scan forward and find first token/structure
do {
$obj = $obj->next;
} while (! ($obj instanceof PpiToken || $obj instanceof PpiStructure));
return strlen($this->tabExpand($obj->preWs));
}
/**
* Expand tab characters in string.
*/
public function tabExpand($s)
{
$tabStop = 8;
while (strpos($s, "\t") !== false) {
$s = preg_replace_callback('/(.*?)(\t+)/',
function ($matches) use ($tabStop) {
return $matches[1] . str_repeat(' ', strlen($matches[2]) *
$tabStop - strlen($matches[1]) % $tabStop);
}, $s);
}
return $s;
}
/**
* Convert underscored name to CamelCase. First character is set to lower
* case. If all uppercase, then is preserved that way.
*/
public function cvtCamelCase(
$name)
{
// Just keep names that start with underscore
if (substr($name, 0, 1) == '_') {
return $name;
}
// If all uppercase, leave it alone
if (strtoupper($name) == $name) {
return $name;
}
if (strpos($name, '_') !== false) {
return lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ',
strtolower($name)))));
}
// If no underscores, just lc first letter.
return lcfirst($name);
}
public function cvtPackageName(
$name)
{
$words = explode('::', $name);
$words = array_map(function ($w) {
if (in_array(strtolower($w), $this->phpReservedWords)) {
return 'X' . strtolower($w);
}
return ucfirst($w);
}, $words);
return implode('\\', $words);
}
public function isReservedWord(
$word)
{
return in_array($word, $this->perlReservedWords);
}
public function isPhpReservedWord(
$word)
{
return in_array($word, $this->phpReservedWords);
}
public function fmtObj(
$level = 0)
{
$content = $this->content;
if ($this->startContent !== '') {
$content = "[ $this->startContent ] $content";
}
if ($this->endContent !== '') {
if ($this->endPreWs !== '') {
$s = $this->endPreWs;
$s = str_replace("\t", '\t', $s);
$s = str_replace("\n", '\n', $s);
$content = "$content '$s':";
}
$content = "$content [ $this->endContent ]";
}
$content = str_replace("\t", '\t', $content);
$content = str_replace("\n", '\n', $content);
if ($this->preWs !== '') {
$s = $this->preWs;
$s = str_replace("\t", '\t', $s);
$s = str_replace("\n", '\n', $s);
$content = "'$s': $content";
}
return sprintf('%3d%s%-12s %-40s %s', $this->id,
$this->cancel ? '-' : ' ', $this->context ?: 'null',
str_repeat(' ', $level*2) . get_class($this), $content);
}
/**
* Add passed object as our sibling on the right.
*/
public function insertRightSibling(
$newObj)
{
$curNext = $this->next;
$this->next = $newObj;
$newObj->prev = $this;
if ($curNext !== null) {
$curNext->prev = $newObj;
$newObj->next = $curNext;
}
$curNext = $this->nextSibling;
$this->nextSibling = $newObj;
$newObj->prevSibling = $this;
if ($curNext !== null) {
$curNext->prevSibling = $newObj;
$newObj->nextSibling = $curNext;
}
// Insert into parent's child list
$parent = $this->parent;
for ($i = 0; $i < count($parent->children); ++$i) {
if ($parent->children[$i] === $this) {
array_splice($parent->children, $i+1, 0, [$newObj]);
break;
}
}
$newObj->parent = $parent;
return;
}
/**
* Add passed object as our sibling on the left.
*/
public function insertLeftSibling(
$newObj)
{
$curPrev = $this->prev;
$this->prev = $newObj;
$newObj->next = $this;
if ($curPrev !== null) {
$curPrev->next = $newObj;
$newObj->prev = $curPrev;
}
$curPrev = $this->prevSibling;
$this->prevSibling = $newObj;
$newObj->nextSibling = $this;
if ($curPrev !== null) {
$curPrev->nextSibling = $newObj;
$newObj->prevSibling = $curPrev;
}
// Insert into parent's child list
$parent = $this->parent;
for ($i = 0; $i < count($parent->children); ++$i) {
if ($parent->children[$i] === $this) {
array_splice($parent->children, $i, 0, [$newObj]);
break;
}
}
$newObj->parent = $parent;
return;
}
/**
* Insert text to the left of the token.
*/
public function insertLeftText(
$text)
{
$obj = Converter::copyNewClass($this, 'PpiToken', true);
$obj->content = $text;
$this->insertLeftSibling($obj);
return $obj;
}
/**
* Insert text to the right of the token.
*/
public function insertRightText(
$text)
{
$obj = Converter::copyNewClass($this, 'PpiToken', true);
$obj->content = $text;
$this->insertRightSibling($obj);
return $obj;
}
/**
* Delete current object from the lexical tree
*/
public function delete()
{
$curNext = $this->next;
$curPrev = $this->prev;
if ($curNext !== null) {
$curNext->prev = $curPrev;
}
if ($curPrev !== null) {
$curPrev->next = $curNext;
}
$curNext = $this->nextSibling;
$curPrev = $this->prevSibling;
if ($curNext !== null) {
$curNext->prevSibling = $curPrev;
}
if ($curPrev !== null) {
$curPrev->nextSibling = $curNext;
}
// Delete from parent's child list
$parent = $this->parent;
for ($i = 0; $i < count($parent->children); ++$i) {
if ($parent->children[$i] === $this) {
array_splice($parent->children, $i, 1);
break;
}
}
return;
}
/**
* Set expression precedence for token into internal variable.
*/
public function setPrecedence()
{
$this->precedence = $this->getPrecedence();
}
/**
* Get expression precedence for token
*/
public function getPrecedence()
{
static $oprPrec = null;
$listOperators = [ 'sort' ];
if ($oprPrec === null) {
$oprPrec = [];
foreach ($this->precList as $level => $keywords) {
foreach ($keywords as $word) {
$oprPrec[$word] = $level;
}
}
}
$token = $this->startContent ?: $this->content;
if ($this instanceof PpiTokenRegexp) {
$token = '//';
} elseif (in_array($token, $listOperators)) {
$token = 'list_operators';
} elseif (! isset($oprPrec[$token])
&& preg_match('/^-{0,1}\w+$/', $this->content)) {
// Bareword, possible function
$token = 'named_unary_operators';
}
return isset($oprPrec[$token]) ? $oprPrec[$token] : 0;
}
/**
* Figure out left argument of this operator by examining the
* the precedence.
*/
function getLeftArg(
$options = [])
{
//print "({$this->content}) LEFT THIS: {$this->fmtObj()}\n";
//print Converter::dumpStruct($this->root);
$prec = $this->precedence;
//print "({$this->content}) LEFT PREC: $prec\n";
$noCancel = ! empty($options['no_cancel']);
$noTrim = ! empty($options['no_trim']);
// Scan backward and look for token with higher precedence so we
// can isolate the argument.
$scan = $this->prevSibling;
if ($scan === null) {
return [ 'start' => null, 'content' => '' ];
}
while ($scan->prevSibling !== null
&& $scan->prevSibling->precedence <= $prec) {
//print "({$this->content}) LEFT SCAN {$scan->prevSibling->precedence}: {$scan->prevSibling->fmtObj()}\n";
$scan = $scan->prevSibling;
}
//if ($scan->prevSibling !== null) {
//print "({$this->content}) LEFT STOP {$scan->prevSibling->precedence}: {$scan->prevSibling->fmtObj()}\n";
//} else {
//print "({$this->content}) LEFT STOP NULL\n";
//}
// Might've scanned back to a newline, skip past it
while ($scan->isWs()) {
$scan = $scan->nextSibling;
}
$start = $scan;
// Call all the converters before we gather the content
for ($scan = $start; $scan !== $this; $scan = $scan->nextSibling) {
$scan->callRecursiveConverters();
}
// Gather the content
$content = '';
for ($scan = $start; $scan !== $this; $scan = $scan->nextSibling) {
$content .= $scan->getRecursiveContent();
if (! $noCancel) {
$scan->cancelAll();
}
}
if (! $noTrim) {
$content = trim($content);
}
return [ $content, $start ];
}
/**
* Figure out right argument of this operator by examining the
* the precedence.
*/
function getRightArg(
$options = [])
{
//print "({$this->content}) RIGHT THIS: {$this->fmtObj()}\n";
//print Converter::dumpStruct($this->root);
$prec = $this->precedence;
$noCancel = ! empty($options['no_cancel']);
$noTrim = ! empty($options['no_trim']);
// Scan forward and look for token with higher precedence so we
// can isolate the argument.
$content = '';
if ($this->nextSibling === null) {
return [ 'end' => null, 'content' => '' ];
}
//print "({$this->content}) RIGHT PREC: $prec\n";
$scan = $this;
do {
$scan = $scan->nextSibling;
//if ($scan->nextSibling !== null) {
//$scanPrec = $scan->nextSibling->precedence;
//print "({$this->content}) RIGHT SCAN $scanPrec: {$scan->fmtObj()}\n";
//}
} while ($scan->nextSibling !== null
&& $scan->nextSibling->precedence <= $prec);
$last = $scan;
//print "({$this->content}) RIGHT LAST: {$last->fmtObj()}\n";
//print "({$this->content}) RIGHT CALL CONVERTERS\n";
// Call all the converters before we gather the text
$scan = $this;
do {
$scan = $scan->nextSibling;
$scan->callRecursiveConverters();
} while ($scan !== $last);
//print "({$this->content}) RIGHT GATHER TEXT\n";
// Gather the text
$scan = $this;
do {
$scan = $scan->nextSibling;
$content .= $scan->getRecursiveContent();
} while ($scan !== $last);
//print "({$this->content}) RIGHT CANCEL\n";
if (! $noCancel) {
$scan = $this;
do {
$scan = $scan->nextSibling;
$scan->cancelAll();
} while ($scan !== $last);
}
if (! $noTrim) {
$content = trim($content);
}
return [ $content, $last ];
}
/**
* Utility function to remove a set of parentheses or brackets on ends
*/
public function stripParensOrBrackets(
$s,
$options = [])
{
// Remove all levels of brackets/parens
$global = ! empty($options['global']);
// Test if there are matching parentheses or brackets around
// expression.
do {
$s = trim($s);
$a = substr($s, 0, 1);
$b = substr($s, -1, 1);
if (! (($a == '(' && $b == ')') || ($a == '[' && $b == ']'))) {
break;
}
$s = substr($s, 1, -1);
} while ($global);
return trim($s);