forked from glayzzle/php-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpr.js
More file actions
680 lines (618 loc) · 20.5 KB
/
expr.js
File metadata and controls
680 lines (618 loc) · 20.5 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
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
/**
* Copyright (C) 2018 Glayzzle (BSD3 License)
* @authors https://github.com/glayzzle/php-parser/graphs/contributors
* @url http://glayzzle.com
*/
"use strict";
module.exports = {
read_expr: function (expr) {
const result = this.node();
if (this.token === "@") {
if (!expr) {
expr = this.next().read_expr();
}
return result("silent", expr);
}
if (!expr) {
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) {
expr = result(
"bin",
"instanceof",
expr,
this.next().read_class_name_reference()
);
if (
this.token !== ";" &&
this.token !== this.tok.T_INLINE_HTML &&
this.token !== this.EOF
) {
expr = this.read_expr(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 === "?") {
let trueArg = null;
if (this.next().token !== ":") {
trueArg = this.read_expr();
}
this.expect(":") && this.next();
return result("retif", expr, trueArg, this.read_expr());
} else {
// see #193
result.destroy(expr);
}
return expr;
},
/*
* Reads a cast expression
*/
read_expr_cast: function (type) {
return this.node("cast")(type, this.text(), this.next().read_expr());
},
/*
* Read a isset variable
*/
read_isset_variable: function () {
return this.read_expr();
},
/*
* Reads isset variables
*/
read_isset_variables: function () {
return this.read_function_list(this.read_isset_variable, ",");
},
/*
* Reads internal PHP functions
*/
read_internal_functions_in_yacc: function () {
let result = null;
switch (this.token) {
case this.tok.T_ISSET:
{
result = this.node("isset");
if (this.next().expect("(")) {
this.next();
}
const variables = this.read_isset_variables();
if (this.expect(")")) {
this.next();
}
result = result(variables);
}
break;
case this.tok.T_EMPTY:
{
result = this.node("empty");
if (this.next().expect("(")) {
this.next();
}
const expression = this.read_expr();
if (this.expect(")")) {
this.next();
}
result = result(expression);
}
break;
case this.tok.T_INCLUDE:
result = this.node("include")(false, false, this.next().read_expr());
break;
case this.tok.T_INCLUDE_ONCE:
result = this.node("include")(true, false, this.next().read_expr());
break;
case this.tok.T_EVAL:
{
result = this.node("eval");
if (this.next().expect("(")) {
this.next();
}
const expr = this.read_expr();
if (this.expect(")")) {
this.next();
}
result = result(expr);
}
break;
case this.tok.T_REQUIRE:
result = this.node("include")(false, true, this.next().read_expr());
break;
case this.tok.T_REQUIRE_ONCE:
result = this.node("include")(true, true, this.next().read_expr());
break;
}
return result;
},
/*
* Reads optional expression
*/
read_optional_expr: function (stopToken) {
if (this.token !== stopToken) {
return this.read_expr();
}
return null;
},
/*
* Reads exit expression
*/
read_exit_expr: function () {
let expression = null;
if (this.token === "(") {
this.next();
expression = this.read_optional_expr(")");
this.expect(")") && this.next();
}
return expression;
},
/*
* ```ebnf
* Reads an expression
* expr ::= @todo
* ```
*/
read_expr_item: function () {
let result, 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 === "~")
return this.node("unary")("~", this.next().read_expr());
if (this.token === "(") {
expr = this.next().read_expr();
expr.parenthesizedExpression = true;
this.expect(")") && this.next();
return this.handleDereferencable(expr);
}
if (this.token === "`") {
// https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L1048
return this.read_encapsed_string("`");
}
if (this.token === this.tok.T_LIST) {
let assign = null;
const isInner = this.innerList;
result = this.node("list");
if (!isInner) {
assign = this.node("assign");
}
if (this.next().expect("(")) {
this.next();
}
if (!this.innerList) this.innerList = true;
// reads inner items
const assignList = this.read_array_pair_list(false);
if (this.expect(")")) {
this.next();
}
// check if contains at least one assignment statement
let hasItem = false;
for (let i = 0; i < assignList.length; i++) {
if (assignList[i] !== null && assignList[i].kind !== "noop") {
hasItem = true;
break;
}
}
if (!hasItem) {
this.raiseError(
"Fatal Error : Cannot use empty list on line " +
this.lexer.yylloc.first_line
);
}
// handles the node resolution
if (!isInner) {
this.innerList = false;
if (this.expect("=")) {
return assign(
result(assignList, false),
this.next().read_expr(),
"="
);
} else {
// error fallback : list($a, $b);
return result(assignList, false);
}
} else {
return result(assignList, false);
}
}
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));
case this.tok.T_DEC:
return this.node("pre")("-", this.next().read_variable(false, false));
case this.tok.T_NEW:
return this.read_new_expr();
case this.tok.T_ISSET:
case this.tok.T_EMPTY:
case this.tok.T_INCLUDE:
case this.tok.T_INCLUDE_ONCE:
case this.tok.T_EVAL:
case this.tok.T_REQUIRE:
case this.tok.T_REQUIRE_ONCE:
return this.read_internal_functions_in_yacc();
case this.tok.T_INT_CAST:
return this.read_expr_cast("int");
case this.tok.T_DOUBLE_CAST:
return this.read_expr_cast("float");
case this.tok.T_STRING_CAST:
return this.read_expr_cast(
this.text().indexOf("binary") !== -1 ? "binary" : "string"
);
case this.tok.T_ARRAY_CAST:
return this.read_expr_cast("array");
case this.tok.T_OBJECT_CAST:
return this.read_expr_cast("object");
case this.tok.T_BOOL_CAST:
return this.read_expr_cast("bool");
case this.tok.T_UNSET_CAST:
return this.read_expr_cast("unset");
case this.tok.T_EXIT: {
const useDie = this.lexer.yytext.toLowerCase() === "die";
result = this.node("exit");
this.next();
const expression = this.read_exit_expr();
return result(expression, useDie);
}
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: {
let value = null;
let key = null;
result = this.node("yield");
if (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:
result = this.node("yieldfrom");
expr = this.next().read_expr();
return result(expr);
case this.tok.T_FN:
case this.tok.T_FUNCTION:
return this.read_inline_function();
case this.tok.T_STATIC: {
const backup = [this.token, this.lexer.getState()];
this.next();
if (
this.token === this.tok.T_FUNCTION ||
(this.version >= 704 && this.token === this.tok.T_FN)
) {
// handles static function
return this.read_inline_function([0, 1, 0]);
} else {
// rollback
this.lexer.tokens.push(backup);
this.next();
}
}
}
// SCALAR | VARIABLE
if (this.is("VARIABLE")) {
result = this.node();
expr = this.read_variable(false, false);
// https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L877
// should accept only a variable
const isConst =
expr.kind === "identifier" ||
(expr.kind === "staticlookup" && expr.offset.kind === "identifier");
// VARIABLES SPECIFIC OPERATIONS
switch (this.token) {
case "=": {
if (isConst) this.error("VARIABLE");
if (this.next().token == "&") {
return this.read_assignref(result, expr);
}
return result("assign", expr, this.read_expr(), "=");
}
// operations :
case this.tok.T_PLUS_EQUAL:
if (isConst) this.error("VARIABLE");
return result("assign", expr, this.next().read_expr(), "+=");
case this.tok.T_MINUS_EQUAL:
if (isConst) this.error("VARIABLE");
return result("assign", expr, this.next().read_expr(), "-=");
case this.tok.T_MUL_EQUAL:
if (isConst) this.error("VARIABLE");
return result("assign", expr, this.next().read_expr(), "*=");
case this.tok.T_POW_EQUAL:
if (isConst) this.error("VARIABLE");
return result("assign", expr, this.next().read_expr(), "**=");
case this.tok.T_DIV_EQUAL:
if (isConst) this.error("VARIABLE");
return result("assign", expr, this.next().read_expr(), "/=");
case this.tok.T_CONCAT_EQUAL:
if (isConst) this.error("VARIABLE");
return result("assign", expr, this.next().read_expr(), ".=");
case this.tok.T_MOD_EQUAL:
if (isConst) this.error("VARIABLE");
return result("assign", expr, this.next().read_expr(), "%=");
case this.tok.T_AND_EQUAL:
if (isConst) this.error("VARIABLE");
return result("assign", expr, this.next().read_expr(), "&=");
case this.tok.T_OR_EQUAL:
if (isConst) this.error("VARIABLE");
return result("assign", expr, this.next().read_expr(), "|=");
case this.tok.T_XOR_EQUAL:
if (isConst) this.error("VARIABLE");
return result("assign", expr, this.next().read_expr(), "^=");
case this.tok.T_SL_EQUAL:
if (isConst) this.error("VARIABLE");
return result("assign", expr, this.next().read_expr(), "<<=");
case this.tok.T_SR_EQUAL:
if (isConst) this.error("VARIABLE");
return result("assign", expr, this.next().read_expr(), ">>=");
case this.tok.T_COALESCE_EQUAL:
if (isConst) this.error("VARIABLE");
return result("assign", expr, this.next().read_expr(), "??=");
case this.tok.T_INC:
if (isConst) this.error("VARIABLE");
this.next();
return result("post", "+", expr);
case this.tok.T_DEC:
if (isConst) this.error("VARIABLE");
this.next();
return result("post", "-", expr);
default:
// see #193
result.destroy(expr);
}
} else if (this.is("SCALAR")) {
result = this.node();
expr = this.read_scalar();
if (expr.kind === "array" && expr.shortForm && this.token === "=") {
// list assign
const list = this.convertToList(expr);
if (expr.loc) list.loc = expr.loc;
const right = this.next().read_expr();
return result("assign", list, right, "=");
} else {
// see #189 - swap docs on nodes
result.destroy(expr);
}
// classic array
return this.handleDereferencable(expr);
} else {
this.error("EXPR");
this.next();
}
// returns variable | scalar
return expr;
},
/*
* Recursively convert nested array to nested list.
*/
convertToList: function (array) {
const convertedItems = array.items.map((entry) => {
if (
entry.value &&
entry.value.kind === "array" &&
entry.value.shortForm
) {
entry.value = this.convertToList(entry.value);
}
return entry;
});
const node = this.node("list")(convertedItems, true);
if (array.loc) node.loc = array.loc;
if (array.leadingComments) node.leadingComments = array.leadingComments;
if (array.trailingComments) node.trailingComments = array.trailingComments;
return node;
},
/*
* Reads assignment
* @param {*} left
*/
read_assignref: function (result, left) {
this.next();
let right;
if (this.token === this.tok.T_NEW) {
if (this.version >= 700) {
this.error();
}
right = this.read_new_expr();
} else {
right = this.read_variable(false, false);
}
return result("assignref", left, right);
},
/*
*
* inline_function:
* function returns_ref backup_doc_comment '(' parameter_list ')' lexical_vars return_type
* backup_fn_flags '{' inner_statement_list '}' backup_fn_flags
* { $$ = zend_ast_create_decl(ZEND_AST_CLOSURE, $2 | $13, $1, $3,
* zend_string_init("{closure}", sizeof("{closure}") - 1, 0),
* $5, $7, $11, $8); CG(extra_fn_flags) = $9; }
* | fn returns_ref '(' parameter_list ')' return_type backup_doc_comment T_DOUBLE_ARROW backup_fn_flags backup_lex_pos expr backup_fn_flags
* { $$ = zend_ast_create_decl(ZEND_AST_ARROW_FUNC, $2 | $12, $1, $7,
* zend_string_init("{closure}", sizeof("{closure}") - 1, 0), $4, NULL,
* zend_ast_create(ZEND_AST_RETURN, $11), $6);
* ((zend_ast_decl *) $$)->lex_pos = $10;
* CG(extra_fn_flags) = $9; } *
*/
read_inline_function: function (flags) {
if (this.token === this.tok.T_FUNCTION) {
return this.read_function(true, flags);
}
// introduced in PHP 7.4
if (!this.version >= 704) {
this.raiseError("Arrow Functions are not allowed");
}
// as an arrowfunc
const node = this.node("arrowfunc");
// eat T_FN
if (this.expect(this.tok.T_FN)) this.next();
// check the &
const isRef = this.is_reference();
// ...
if (this.expect("(")) this.next();
const params = this.read_parameter_list();
if (this.expect(")")) this.next();
let nullable = false;
let returnType = null;
if (this.token === ":") {
if (this.next().token === "?") {
nullable = true;
this.next();
}
returnType = this.read_type();
}
if (this.expect(this.tok.T_DOUBLE_ARROW)) this.next();
const body = this.read_expr();
return node(
params,
isRef,
body,
returnType,
nullable,
flags ? true : false
);
},
/*
* ```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 () {
const result = this.node("new");
this.expect(this.tok.T_NEW) && this.next();
let args = [];
if (this.token === this.tok.T_CLASS) {
const what = this.node("class");
// Annonymous class declaration
if (this.next().token === "(") {
args = this.read_argument_list();
}
const propExtends = this.read_extends_from();
const propImplements = this.read_implements_list();
let body = null;
if (this.expect("{")) {
body = this.next().read_class_body();
}
return result(
what(null, propExtends, propImplements, body, [0, 0, 0]),
args
);
}
// Already existing class
const name = this.read_new_class_name();
if (this.token === "(") {
args = this.read_argument_list();
}
return result(name, args);
},
/*
* Reads a class name
* ```ebnf
* read_new_class_name ::= namespace_name | variable
* ```
*/
read_new_class_name: function () {
if (
this.token === this.tok.T_NS_SEPARATOR ||
this.token === this.tok.T_STRING ||
this.token === this.tok.T_NAMESPACE
) {
let result = this.read_namespace_name(true);
if (this.token === this.tok.T_DOUBLE_COLON) {
result = this.read_static_getter(result);
}
return result;
} else if (this.is("VARIABLE")) {
return this.read_variable(true, false);
} else {
this.expect([this.tok.T_STRING, "VARIABLE"]);
}
},
handleDereferencable: function (expr) {
while (this.token !== this.EOF) {
if (
this.token === this.tok.T_OBJECT_OPERATOR ||
this.token === this.tok.T_DOUBLE_COLON
) {
expr = this.recursive_variable_chain_scan(expr, false, false, true);
} 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_argument_list());
} else {
return expr;
}
}
return expr;
},
};