-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScanner.php
More file actions
478 lines (368 loc) · 11.6 KB
/
Copy pathScanner.php
File metadata and controls
478 lines (368 loc) · 11.6 KB
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
<?php
namespace TaskChecker\TextScanner;
class Scanner
{
private $text;
private $position;
private $skipTokens = array(
T_COMMENT => true,
T_DOC_COMMENT => true,
T_WHITESPACE => true //,
// T_ML_COMMENT => true
);
private $literalTokens = array(
T_CONSTANT_ENCAPSED_STRING => true,
T_ENCAPSED_AND_WHITESPACE => true,
T_INLINE_HTML => true,
T_STRING_VARNAME => true
);
private $openExpression = array(
'(' => true,
'[' => true,
);
private $closeExpression = array(
'}' => true,
']' => true,
')' => true,
';' => true,
T_CLOSE_TAG => true
);
public function __construct(TokenArray $text, $position = 0)
{
$this->text = $text;
$this->position = $this->skipForward($position);
}
public function getPosition()
{
return $this->position;
}
public function getText()
{
return $this->text;
}
private function skipForward($position)
{
$end = $this->text->count();
while ($position < $end) {
$token = $this->text->getTokenIdAt($position);
if (!isset($this->skipTokens[$token])) {
break;
}
$position ++;
}
return $position;
}
private function getNextPos($pos)
{
return $this->skipForward($pos + 1);
}
private function getEnd()
{
return $this->text->count();
}
public function next()
{
if (!$this->isEnd()) {
$this->position = $this->skipForward($this->position + 1);
}
return $this;
}
public function isEnd()
{
return $this->text->isEnd($this->position);
}
public function getTokenId()
{
return $this->text->getTokenIdAt($this->position);
}
public function getTokenString()
{
return $this->text->getTokenStringAt($this->position);
}
public function findToken($tokenId)
{
$pos = $this->position;
$tokenId = TokenArray::convertTokenId($tokenId);
if ($this->findTokenPos($tokenId, $pos)) {
$this->position = $pos;
return true;
}
return false;
}
public function findAny(array $tokens)
{
$tokens = array_map('TextScanner\\TokenArray::convertTokenId', $tokens);
$pos = $this->position;
if ($this->findAnyTokenPos($tokens, $pos)) {
$this->position = $pos;
return true;
}
return false;
}
private function findAnyTokenPos(array $tokenIds, &$pos)
{
$end = $this->getEnd();
$tokenHash = array_flip($tokenIds);
for (; $pos < $end; $pos = $this->getNextPos($pos)) {
$id = $this->text->getTokenIdAt($pos);
if (isset($tokenHash[$id]) &&
!$this->isLiteral($id)) {
return true;
}
}
return false;
}
private function findTokenPos($tokenId, &$pos)
{
return $this->findAnyTokenPos(array($tokenId), $pos);
}
private function isLiteral($tokenId)
{
return isset($this->literalTokens[$tokenId]);
}
private function consumeSequence(array $args, &$position)
{
$end = $this->getEnd();
foreach ($args as $string) {
$id = $this->text->getTokenIdAt($position);
if ($position >= $end || $id != $string || $this->isLiteral($id)) {
return false;
}
$position = $this->getNextPos($position);
}
return true;
}
public function findSequence($arg1)
{
$args = func_get_args();
$args = array_map('TokenArray::convertTokenId', $args);
$pos = $this->position;
while ($this->findTokenPos($arg1, $pos)) {
if ($this->consumeSequence($args, $pos)) {
$this->position = $pos;
return true;
}
}
return false;
}
public function readTokenId($id)
{
$string = TokenArray::convertTokenId($id);
$pos = $this->position;
if ($this->consumeToken($pos, $this->getEnd(), $string)) {
$this->next();
return true;
}
return false;
}
public function readTokenString($string)
{
$pos = $this->position;
if ($this->getTokenString() == $string) {
$this->next();
return true;
}
return false;
}
private function consumeToken(&$pos, $end, $id)
{
if ($pos >= $end ||
$this->isLiteral($this->text->getTokenIdAt($pos)) ||
$this->text->getTokenIdAt($pos) != $id) {
return false;
}
$pos = $this->getNextPos($pos);
return true;
}
private function consumeBlock(&$pos, $end, &$valid)
{
assert($this->text->getTokenStringAt($pos) == '{');
$pos = $this->skipForward($pos);
while ($pos < $end && $valid) {
$this->consumeExpression($pos, $valid);
if (!$valid) {
break;
}
$string = $this->text->getTokenStringAt($pos);
$pos = $this->skipForward($pos + 1);
if ($string == ';') {
continue;
}
if ($string == '}') {
return $pos;
}
$valid = false;
break;
}
$valid = false;
}
private function consumeExpression(&$pos, $end, &$valid)
{
while ($pos < $end && $valid) {
$tokenId = $this->text->getTokenIdAt($pos);
// End expression
if (isset($this->closeExpression[$tokenId])) {
return true;
}
// Start string
if ($tokenId == '"' || $tokenId == T_START_HEREDOC) {
$this->consumeStringAt($pos, $end, $valid);
continue;
}
// Start bracketed expr
if ($tokenId == '(' || $tokenId == '[') {
$this->consumeBracketedExpression($pos, $end, $valid);
continue;
}
$pos = $this->getNextPos($pos);
}
$valid = false;
}
private function consumeStringAt(&$pos, $end, &$valid)
{
$startToken = $this->text->getTokenIdAt($pos);
$pos = $this->getNextPos($pos);
assert($startToken == T_START_HEREDOC || $startToken = '"');
$endToken = $startToken == T_START_HEREDOC ? T_END_HEREDOC : '"';
while ($valid && $pos < $end) {
$token = $this->text->getTokenIdAt($pos);
if ($token == T_CURLY_OPEN || $token == T_DOLLAR_OPEN_CURLY_BRACES) {
$this->consumeInnerExpression($pos, $end, $valid);
continue;
}
$pos = $this->getNextPos($pos);
if ($token == $endToken) {
return;
}
}
// Unexpected break
$valid = false;
}
private function consumeInnerExpression(&$pos, $end, &$valid)
{
$pos = $this->getNextPos($pos);
while ($valid && $pos < $end) {
$this->consumeExpression($pos, $end, $valid);
if (!$valid) {
return false;
}
$id = $this->text->getTokenIdAt($pos);
if ($id == '}') {
$pos = $this->getNextPos($pos);
return;
}
}
$valid = false;
}
private function consumeBracketedExpression(&$pos, $end, &$valid)
{
$startToken = $this->text->getTokenIdAt($pos);
assert($startToken == '[' || $startToken == '(');
$endToken = $startToken == '[' ? ']' : ')';
$pos = $this->getNextPos($pos);
$this->consumeExpression($pos, $end, $valid);
if (!$valid) {
return;
}
$id = $this->text->getTokenIdAt($pos);
if ($id == $endToken) {
$pos = $this->getNextPos($pos);
return;
}
$valid = false;
}
public function matchExpression()
{
$pos = $this->position;
$valid = true;
$this->consumeExpression($pos, $this->getEnd(), $valid);
if ($valid) {
$start = $this->position;
$this->position = $pos;
return Range::createExcludingEnd($this->text, $start, $pos);
}
return null;
}
public function matchForContents()
{
$start = $pos = $this->position;
$end = $this->getEnd();
$valid = true;
for ($i = 0; $i < 3; $i++) {
$this->consumeExpression($pos, $this->getEnd(), $valid);
if (!$valid) {
return null;
}
if ($i < 2) {
if (!$this->consumeToken($pos, $end, ';')) {
return null;
}
}
}
$end = $pos;
$this->position = $pos;
return Range::createExcludingEnd($this->text, $start, $end);
}
/**
* When matches, advaces position and returns a range
* Otherwise returns null
*/
public function matchValue()
{
$start = $pos = $this->position;
$end = $this->getEnd();
$this->consumeValue($pos, $end, $found);
if (!$found) {
return null;
}
$this->position = $pos;
return Range::createExcludingEnd($this->text, $start, $pos);
}
private function consumeValue(&$pos, $end, &$found)
{
/*
* Value is:
* T_ARRAY ( EXPR ) array(1, 2, 3)
* T_CONSTANT_ENCAPSED_STRING 'hello'
* T_DNUMBER 2.12
* " STRING_CONTENT " "Hello {$world[0]->call()}"
* T_LNUMBER 0x20, 120
* T_START_HEREDOC STR_CONTENT T_END_HEREDOC
*/
$found = false;
$firstToken = $this->text->getTokenIdAt($pos);
switch ($firstToken) {
case T_CONSTANT_ENCAPSED_STRING:
case T_DNUMBER:
case T_LNUMBER:
$pos = $this->getNextPos($pos);
$found = true;
return;
case T_ARRAY:
$pos = $this->getNextPos($pos);
$secondToken = $this->text->getTokenIdAt($pos);
if ($secondToken != '(') {
return;
}
$found = true;
$this->consumeBracketedExpression($pos, $end, $found);
return;
case T_START_HEREDOC:
case '"':
$found = true;
$this->consumeStringAt($pos, $end, $found);
return;
default:
// not found
}
}
public function goWhere(callable $predicate)
{
for ( ; !$this->isEnd(); $this->next()) {
if ($predicate($this)) {
return true;
}
}
return false;
}
}