| 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 |
1
821
821
821
5
816
5
811
5
806
8
798
25
773
7
766
10
756
6
750
3
747
1
746
3
743
1
742
5
737
5
732
6
726
11
715
11
704
1
703
2
701
5
696
1
695
3
692
2
690
1
689
1
688
1
687
7
680
6
674
9
9
8
9
9
665
829
1
828
1
827
7
820
1
819
4
4
4
3
3
3
1
815
35
35
35
35
35
1
34
1
33
1
32
780
13
767
2
2
2
1
2
2
2
2
2
2
2
2
2
2
2
2
2
1
1
1
1
765
1
764
1
1
9
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
2
1
1
1
1
5
5
5
4
3
3
3
1
5
1
2
2
2
2
1
1
2
1
1
1
3
726
726
325
325
325
108
108
1
1
107
108
3
1
1
1
1
3
1
1
1
1
1
1
5
5
1
1
401
391
391
384
1
383
1
382
1
381
10
10
215
9
9
1
1
1
1
1
1
1
1
1
1
8
8
8
8
8
8
7
7
1
7
1
1
3
9
7
7
1
7
| /*!
* Copyright (C) 2017 Glayzzle (BSD3 License)
* @authors https://github.com/glayzzle/php-parser/graphs/contributors
* @url http://glayzzle.com
*/
"use strict";
module.exports = {
read_expr: function() {
var result = this.node();
var expr = this.read_expr_item();
// binary operations
if (this.token === '|')
return result('bin', '|', expr, this.next().read_expr());
if (this.token === '&')
return result('bin', '&', expr, this.next().read_expr());
if (this.token === '^')
return result('bin', '^', expr, this.next().read_expr());
if (this.token === '.')
return result('bin', '.', expr, this.next().read_expr());
if (this.token === '+')
return result('bin', '+', expr, this.next().read_expr());
if (this.token === '-')
return result('bin', '-', expr, this.next().read_expr());
if (this.token === '*')
return result('bin', '*', expr, this.next().read_expr());
if (this.token === '/')
return result('bin', '/', expr, this.next().read_expr());
if (this.token === '%')
return result('bin', '%', expr, this.next().read_expr());
if (this.token === this.tok.T_POW)
return result('bin', '**', expr, this.next().read_expr());
if (this.token === this.tok.T_SL)
return result('bin', '<<', expr, this.next().read_expr());
if (this.token === this.tok.T_SR)
return result('bin', '>>', expr, this.next().read_expr());
// more binary operations (formerly bool)
if (this.token === this.tok.T_BOOLEAN_OR)
return result('bin', '||', expr, this.next().read_expr());
if (this.token === this.tok.T_LOGICAL_OR)
return result('bin', 'or', expr, this.next().read_expr());
if (this.token === this.tok.T_BOOLEAN_AND)
return result('bin', '&&', expr, this.next().read_expr());
if (this.token === this.tok.T_LOGICAL_AND)
return result('bin', 'and', expr, this.next().read_expr());
if (this.token === this.tok.T_LOGICAL_XOR)
return result('bin', 'xor', expr, this.next().read_expr());
if (this.token === this.tok.T_IS_IDENTICAL)
return result('bin', '===', expr, this.next().read_expr());
if (this.token === this.tok.T_IS_NOT_IDENTICAL)
return result('bin', '!==', expr, this.next().read_expr());
if (this.token === this.tok.T_IS_EQUAL)
return result('bin', '==', expr, this.next().read_expr());
if (this.token === this.tok.T_IS_NOT_EQUAL)
return result('bin', '!=', expr, this.next().read_expr());
if (this.token === '<')
return result('bin', '<', expr, this.next().read_expr());
if (this.token === '>')
return result('bin', '>', expr, this.next().read_expr());
if (this.token === this.tok.T_IS_SMALLER_OR_EQUAL)
return result('bin', '<=', expr, this.next().read_expr());
if (this.token === this.tok.T_IS_GREATER_OR_EQUAL)
return result('bin', '>=', expr, this.next().read_expr());
if (this.token === this.tok.T_SPACESHIP)
return result('bin', '<=>', expr, this.next().read_expr());
if (this.token === this.tok.T_INSTANCEOF)
return result('bin', 'instanceof', expr, this.next().read_expr());
// extra operations :
// $username = $_GET['user'] ?? 'nobody';
if (this.token === this.tok.T_COALESCE)
return result('bin', '??', expr, this.next().read_expr());
// extra operations :
// $username = $_GET['user'] ? true : false;
if (this.token === '?') {
var trueArg = null;
if (this.next().token !== ':') {
trueArg = this.read_expr();
}
this.expect(':') && this.next();
return result('retif', expr, trueArg, this.read_expr());
}
return expr;
}
/**
* ```ebnf
* Reads an expression
* expr ::= @todo
* ```
*/
,read_expr_item: function() {
if (this.token === '@')
return this.node('silent')(this.next().read_expr());
if (this.token === '+')
return this.node('unary')('+', this.next().read_expr());
if (this.token === '!')
return this.node('unary')('!', this.next().read_expr());
if (this.token === '~')
return this.node('unary')('~', this.next().read_expr());
if (this.token === '-') {
var result = this.node();
this.next();
if (
this.token === this.tok.T_LNUMBER ||
this.token === this.tok.T_DNUMBER
) {
// negative number
result = result('number', '-' + this.text());
this.next();
return result;
} else {
return result('unary', '-', this.read_expr());
}
}
if (this.token === '(') {
var node = this.node('parenthesis');
var expr = this.next().read_expr();
this.expect(')') && this.next();
expr = node(expr);
// handle dereferencable
if (this.token === this.tok.T_OBJECT_OPERATOR) {
return this.recursive_variable_chain_scan(expr, false);
} else if (this.token === this.tok.T_CURLY_OPEN || this.token === '[') {
return this.read_dereferencable(expr);
} else if (this.token === '(') {
// https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L1118
return this.node('call')(
expr, this.read_function_argument_list()
);
} else {
return expr;
}
}
if (this.token === '`') {
// https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L1048
return this.next().read_encapsed_string('`');
}
if (this.token === this.tok.T_LIST) {
var result = this.node('list'), assign = null;
var isInner = this.innerList;
if (!isInner) {
assign = this.node('assign');
}
Eif (this.next().expect('(')) {
this.next();
}
if (!this.innerList) this.innerList = true;
var assignList = this.read_assignment_list();
// check if contains at least one assignment statement
var hasItem = false;
for(var i = 0; i < assignList.length; i++) {
Eif (assignList[i] !== null) {
hasItem = true;
break;
}
}
Iif (!hasItem) {
this.raiseError(
'Fatal Error : Cannot use empty list on line ' + this.lexer.yylloc.first_line
);
}
Eif (this.expect(')')) {
this.next();
}
if (!isInner) {
this.innerList = false;
Eif (this.expect('=')) {
return assign(
result(assignList),
this.next().read_expr(),
'='
);
} else {
// fallback : list($a, $b);
return result(assignList);
}
} else {
return result(assignList);
}
}
if (this.token === this.tok.T_CLONE)
return this.node('clone')(
this.next().read_expr()
);
switch(this.token) {
case this.tok.T_INC:
return this.node('pre')(
'+', this.next().read_variable(false, false, false)
);
case this.tok.T_DEC:
return this.node('pre')(
'-', this.next().read_variable(false, false, false)
);
case this.tok.T_NEW:
return this.next().read_new_expr();
case this.tok.T_ISSET:
var result = this.node('isset');
Eif (this.next().expect('(')) {
this.next();
}
var args = this.read_list(this.read_expr, ',');
Eif (this.expect(')')) {
this.next();
}
return result(args);
case this.tok.T_EMPTY:
var result = this.node('empty');
Eif (this.next().expect('(')) {
this.next();
}
var arg = this.read_expr();
Eif (this.expect(')')) {
this.next();
}
return result([arg]);
case this.tok.T_INCLUDE:
return this.node('include')(
false, false,
this.next().read_expr()
);
case this.tok.T_INCLUDE_ONCE:
return this.node('include')(
true, false,
this.next().read_expr()
);
case this.tok.T_REQUIRE:
return this.node('include')(
false, true,
this.next().read_expr()
);
case this.tok.T_REQUIRE_ONCE:
return this.node('include')(
true, true,
this.next().read_expr()
);
case this.tok.T_EVAL:
var result = this.node('eval');
Eif (this.next().expect('(')) {
this.next();
}
var expr = this.read_expr();
Eif (this.expect(')')) {
this.next();
}
return result(expr);
case this.tok.T_INT_CAST:
return this.node('cast')('int', this.next().read_expr());
case this.tok.T_DOUBLE_CAST:
return this.node('cast')('double', this.next().read_expr());
case this.tok.T_STRING_CAST:
return this.node('cast')('string', this.next().read_expr());
case this.tok.T_ARRAY_CAST:
return this.node('cast')('array', this.next().read_expr());
case this.tok.T_OBJECT_CAST:
return this.node('cast')('object', this.next().read_expr());
case this.tok.T_BOOL_CAST:
return this.node('cast')('boolean', this.next().read_expr());
case this.tok.T_UNSET_CAST:
return this.node('unset')(
this.next().read_expr()
);
case this.tok.T_EXIT:
var result = this.node('exit');
var status = null;
if ( this.next().token === '(' ) {
if (this.next().token !== ')') {
status = this.read_expr();
Eif (this.expect(')')) {
this.next();
}
} else {
this.next();
}
}
return result(status);
case this.tok.T_PRINT:
return this.node('print')(
this.next().read_expr()
);
// T_YIELD (expr (T_DOUBLE_ARROW expr)?)?
case this.tok.T_YIELD:
var result = this.node('yield'), value = null, key = null;
Eif (this.next().is('EXPR')) {
// reads the yield return value
value = this.read_expr();
if (this.token === this.tok.T_DOUBLE_ARROW) {
// reads the yield returned key
key = value;
value = this.next().read_expr();
}
}
return result(value, key);
// T_YIELD_FROM expr
case this.tok.T_YIELD_FROM:
var result = this.node('yieldfrom');
var expr = this.next().read_expr();
return result(expr);
case this.tok.T_FUNCTION:
// @fixme later - removed static lambda function declarations (colides with static keyword usage)
return this.read_function(true);
}
// SCALAR | VARIABLE
var expr;
if (this.is('VARIABLE')) {
var result = this.node();
expr = this.read_variable(false, false, false);
// VARIABLES SPECIFIC OPERATIONS
switch(this.token) {
case '=':
var right;
if (this.next().token == '&') {
Iif (this.next().token === this.tok.T_NEW) {
right = this.next().read_new_expr();
} else {
right = this.read_variable(false, false, true);
}
} else {
right = this.read_expr();
}
return result('assign', expr, right, '=');
// operations :
case this.tok.T_PLUS_EQUAL:
return result('assign', expr, this.next().read_expr(), '+=');
case this.tok.T_MINUS_EQUAL:
return result('assign', expr, this.next().read_expr(), '-=');
case this.tok.T_MUL_EQUAL:
return result('assign', expr, this.next().read_expr(), '*=');
case this.tok.T_POW_EQUAL:
return result('assign', expr, this.next().read_expr(), '**=');
case this.tok.T_DIV_EQUAL:
return result('assign', expr, this.next().read_expr(), '/=');
case this.tok.T_CONCAT_EQUAL:
return result('assign', expr, this.next().read_expr(), '.=');
case this.tok.T_MOD_EQUAL:
return result('assign', expr, this.next().read_expr(), '%=');
case this.tok.T_AND_EQUAL:
return result('assign', expr, this.next().read_expr(), '&=');
case this.tok.T_OR_EQUAL:
return result('assign', expr, this.next().read_expr(), '|=');
case this.tok.T_XOR_EQUAL:
return result('assign', expr, this.next().read_expr(), '^=');
case this.tok.T_SL_EQUAL:
return result('assign', expr, this.next().read_expr(), '<<=');
case this.tok.T_SR_EQUAL:
return result('assign',expr, this.next().read_expr(), '>>=');
case this.tok.T_INC:
this.next();
return result('post', '+', expr);
case this.tok.T_DEC:
this.next();
return result('post', '-', expr);
}
} else if (this.is('SCALAR')) {
expr = this.read_scalar();
// handle dereferencable
while(this.token !== this.EOF) {
if (this.token === this.tok.T_OBJECT_OPERATOR) {
expr = this.recursive_variable_chain_scan(expr, false);
} else if (this.token === this.tok.T_CURLY_OPEN || this.token === '[') {
expr = this.read_dereferencable(expr);
} else if (this.token === '(') {
// https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L1118
expr = this.node('call')(expr, this.read_function_argument_list());
} else {
return expr;
}
}
} else {
this.error('EXPR');
this.next();
}
// returns variable | scalar
return expr;
}
/**
* ```ebnf
* new_expr ::= T_NEW (namespace_name function_argument_list) | (T_CLASS ... class declaration)
* ```
* https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L850
*/
,read_new_expr: function() {
var result = this.node('new');
if (this.token === this.tok.T_CLASS) {
var what = this.node('class');
// Annonymous class declaration
var propExtends = null, propImplements = null, body = null, args = [];
Iif (this.next().token === '(') {
args = this.read_function_argument_list();
}
Eif (this.token == this.tok.T_EXTENDS) {
propExtends = this.next().read_namespace_name();
}
Eif (this.token == this.tok.T_IMPLEMENTS) {
propImplements = this.next().read_name_list();
}
Eif (this.expect('{')) {
body = this.next().read_class_body();
}
return result(
what(
null
,propExtends
,propImplements
,body
,[0, 0, 0]
), args
);
} else {
// Already existing class
var name = this.read_class_name_reference();
var args = [];
Eif (this.token === '(') {
args = this.read_function_argument_list();
}
return result(name, args);
}
}
/**
* Reads a class name
* ```ebnf
* class_name_reference ::= namespace_name | variable
* ```
*/
,read_class_name_reference: function() {
if (
this.token === this.tok.T_NS_SEPARATOR ||
this.token === this.tok.T_STRING ||
this.token === this.tok.T_NAMESPACE
) {
var result = this.read_namespace_name();
if (this.token === this.tok.T_DOUBLE_COLON) {
result = this.read_static_getter(result);
}
return result;
} else Eif (this.is('VARIABLE')) {
return this.read_variable(true, false, false);
} else {
this.expect([this.tok.T_STRING, 'VARIABLE']);
}
}
/**
* ```ebnf
* assignment_list ::= assignment_list_element (',' assignment_list_element?)*
* ```
*/
,read_assignment_list: function() {
return this.read_list(
this.read_assignment_list_element, ','
);
}
/**
* ```ebnf
* assignment_list_element ::= expr | expr T_DOUBLE_ARROW expr
* ```
*/
,read_assignment_list_element: function() {
if (this.token === ',' || this.token === ')') return null;
var result = this.read_expr_item();
if (this.token === this.tok.T_DOUBLE_ARROW) {
result = [
'key',
result,
this.next().read_expr_item()
];
}
return result;
}
};
|