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
417 lines (371 loc) · 13.8 KB
/
expr.js
File metadata and controls
417 lines (371 loc) · 13.8 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
/**
* Copyright (C) 2014 Glayzzle (BSD3 License)
* @authors https://github.com/glayzzle/php-parser/graphs/contributors
* @url http://glayzzle.com
*/
module.exports = {
read_expr: function() {
var expr = this.read_expr_item();
switch(this.token) {
// binary operations
case '|': return this.node('bin')('|', expr, this.next().read_expr());
case '&': return this.node('bin')('&', expr, this.next().read_expr());
case '^': return ['bin', '^', expr, this.next().read_expr()];
case '.': return ['bin', '.', expr, this.next().read_expr()];
case '+': return ['bin', '+', expr, this.next().read_expr()];
case '-': return ['bin', '-', expr, this.next().read_expr()];
case '*': return ['bin', '*', expr, this.next().read_expr()];
case '/': return ['bin', '/', expr, this.next().read_expr()];
case '%': return ['bin', '%', expr, this.next().read_expr()];
case this.tok.T_POW: return ['bin', '**', expr, this.next().read_expr()];
case this.tok.T_SL: return ['bin', '<<', expr, this.next().read_expr()];
case this.tok.T_SR: return ['bin', '>>', expr, this.next().read_expr()];
// boolean operations
case this.tok.T_BOOLEAN_OR:
case this.tok.T_LOGICAL_OR: return ['bool', '|', expr, this.next().read_expr()];
case this.tok.T_BOOLEAN_AND:
case this.tok.T_LOGICAL_AND: return ['bool', '&', expr, this.next().read_expr()];
case this.tok.T_LOGICAL_XOR: return ['bool', '^', expr, this.next().read_expr()];
case this.tok.T_IS_IDENTICAL: return ['bool', '=', expr, this.next().read_expr()];
case this.tok.T_IS_NOT_IDENTICAL: return ['bool', '!=', expr, this.next().read_expr()];
case this.tok.T_IS_EQUAL: return ['bool', '~', expr, this.next().read_expr()];
case this.tok.T_IS_NOT_EQUAL: return ['bool', '!~', expr, this.next().read_expr()];
case '<': return ['bool', '<', expr, this.next().read_expr()];
case '>': return ['bool', '>', expr, this.next().read_expr()];
case this.tok.T_IS_SMALLER_OR_EQUAL: return ['bool', '<=', expr, this.next().read_expr()];
case this.tok.T_IS_GREATER_OR_EQUAL: return ['bool', '>=', expr, this.next().read_expr()];
case this.tok.T_SPACESHIP: return ['bool', '<=>', expr, this.next().read_expr()];
case this.tok.T_INSTANCEOF: return ['bool', '?', expr, this.next().read_expr()];
// extra operations :
case this.tok.T_COALESCE: // php7 : $username = $_GET['user'] ?? 'nobody';
return ['retif', ['sys', 'isset', expr], expr, this.next().read_expr()];
case '?':
var trueArg = null;
if (this.next().token !== ':') {
trueArg = this.read_expr();
}
this.expect(':').next();
return ['retif', expr, trueArg, this.read_expr()];
}
return expr;
}
/**
* <ebnf>
* Reads an expression
* expr ::= @todo
* </ebnf>
*/
,read_expr_item: function() {
switch(this.token) {
case '@':
return ['silent', this.next().read_expr()];
case '-':
case '+':
case '!':
case '~':
return this.node('unary')(this.token, this.next().read_expr());
case '(':
var expr = this.next().read_expr();
this.expect(')').next();
// 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;
}
case '`':
// https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L1048
var result = this.node('sys');
var expr = this.next().read_encapsed_string('`');
return result('shell', expr);
case this.tok.T_LIST:
var result = this.node('list');
this.next().expect('(').next();
var isInner = this.innerList;
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++) {
if (assignList[i] !== null) {
hasItem = true;
break;
}
}
if (!hasItem) {
this.raiseError(
'Fatal Error : Cannot use empty list on line ' + this.lexer.yylloc.first_line
);
}
this.expect(')').next();
if (!isInner) {
this.innerList = false;
this.expect('=').next();
return result(assignList, this.read_expr());
} else {
return result(assignList, null);
}
case this.tok.T_CLONE:
return this.node('sys')(
'clone', this.next().read_expr()
);
case this.tok.T_INC:
var name = this.next().read_variable();
return ['set', name, ['bin', '+', name, ['number', 1]]];
case this.tok.T_DEC:
var name = this.next().read_variable();
return ['set', name, ['bin', '-', name, ['number', 1]]];
case this.tok.T_NEW:
return this.next().read_new_expr();
case this.tok.T_ISSET:
this.next().expect('(').next();
var expr = this.read_list(this.read_expr, ',');
this.expect(')').next();
return ['sys', 'isset', expr];
case this.tok.T_EMPTY:
this.next().expect('(').next();
var expr = this.read_expr();
this.expect(')').next();
return ['sys', 'empty', expr];
case this.tok.T_INCLUDE:
return (this.node('sys'))(
'include',
this.next().read_expr()
);
case this.tok.T_INCLUDE_ONCE:
return (this.node('sys'))(
'include_once',
this.next().read_expr()
);
case this.tok.T_REQUIRE:
return (this.node('sys'))(
'require',
this.next().read_expr()
);
case this.tok.T_REQUIRE_ONCE:
return (this.node('sys'))(
'require_once',
this.next().read_expr()
);
case this.tok.T_EVAL:
var result = this.node('sys');
this.next().expect('(').next();
var expr = this.read_expr();
this.expect(')').next();
return result('eval', expr);
case this.tok.T_INT_CAST:
return ['cast', 'int', this.next().read_expr()];
case this.tok.T_DOUBLE_CAST:
return ['cast', 'double', this.next().read_expr()];
case this.tok.T_STRING_CAST:
return ['cast', 'string', this.next().read_expr()];
case this.tok.T_ARRAY_CAST:
return ['cast', 'array', this.next().read_expr()];
case this.tok.T_OBJECT_CAST:
return ['cast', 'object', this.next().read_expr()];
case this.tok.T_BOOL_CAST:
return ['cast', 'boolean', this.next().read_expr()];
case this.tok.T_UNSET_CAST:
return ['sys', 'unset', this.next().read_expr()];
case this.tok.T_EXIT:
var result = this.node('sys');
var expr = null;
if ( this.next().token === '(' ) {
if (this.next().token !== ')') {
expr = this.read_expr();
this.expect(')').next();
} else {
this.next();
}
}
return result('exit', expr);
case this.tok.T_PRINT:
return (this.node('sys'))(
'print',
this.next().read_expr()
);
// T_YIELD (expr (T_DOUBLE_ARROW expr)?)?
case this.tok.T_YIELD:
var result = ['yield', null, null];
if (this.next().is('EXPR')) {
// reads the yield return value
result[1] = this.read_expr();
if (this.token === this.tok.T_DOUBLE_ARROW) {
// reads the yield returned key
result[2] = this.next().read_expr();
}
}
return result;
// T_YIELD_FROM expr
case this.tok.T_YIELD_FROM:
return ['yieldfrom', this.next().read_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')) {
expr = this.read_variable();
// VARIABLES SPECIFIC OPERATIONS
switch(this.token) {
case '=':
if (this.next().token == '&') {
if (this.next().token === this.tok.T_NEW) {
return ['link', expr, this.next().read_new_expr()];
} else {
return ['link', expr, this.read_variable()];
}
} else {
var node = this.node('set');
var statement = this.token === this.tok.T_NEW ?
this.next().read_new_expr() : this.read_expr();
return node(expr, statement);
}
// operations :
case this.tok.T_PLUS_EQUAL:
return ['set', expr, ['bin', '+', expr, this.next().read_expr()]];
case this.tok.T_MINUS_EQUAL:
return ['set', expr, ['bin', '-', expr, this.next().read_expr()]];
case this.tok.T_MUL_EQUAL:
return ['set', expr, ['bin', '*', expr, this.next().read_expr()]];
case this.tok.T_POW_EQUAL:
return ['set', expr, ['bin', '**', expr, this.next().read_expr()]];
case this.tok.T_DIV_EQUAL:
return ['set', expr, ['bin', '/', expr, this.next().read_expr()]];
case this.tok.T_CONCAT_EQUAL:
// NB : convert as string and add
return ['set', expr, ['bin', '.', expr, this.next().read_expr()]];
case this.tok.T_MOD_EQUAL:
return ['set', expr, ['bin', '%', expr, this.next().read_expr()]];
case this.tok.T_AND_EQUAL:
return ['set', expr, ['bin', '&', expr, this.next().read_expr()]];
case this.tok.T_OR_EQUAL:
return ['set', expr, ['bin', '|', expr, this.next().read_expr()]];
case this.tok.T_XOR_EQUAL:
return ['set', expr, ['bin', '^', expr, this.next().read_expr()]];
case this.tok.T_SL_EQUAL:
return ['set', expr, ['bin', '<<', expr, this.next().read_expr()]];
case this.tok.T_SR_EQUAL:
return ['set', expr, ['bin', '>>', expr, this.next().read_expr()]];
case this.tok.T_INC:
this.next();
return ['post', '+', expr];
case this.tok.T_DEC:
this.next();
return ['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 {
expr = this.error('EXPR');
this.next();
}
// returns variable | scalar
return expr;
}
/**
* <ebnf>
* new_expr ::= T_NEW (namespace_name function_argument_list) | (T_CLASS ... class declaration)
* </ebnf>
* 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) {
// Annonymous class declaration
var propExtends = false, propImplements = false;
if (this.next().token == this.tok.T_EXTENDS) {
propExtends = this.next().read_namespace_name();
}
if (this.token == this.tok.T_IMPLEMENTS) {
propImplements = this.next().read_list(
this.read_namespace_name,
','
);
}
return result(
false // class name => false : means it's an annonymous class
,propExtends
,propImplements
,this.expect('{').next().read_class_body()
);
} else {
// Already existing class
var name = this.read_class_name_reference();
var args = [];
if (this.token === '(') {
args = this.read_function_argument_list();
}
return result(name, args);
}
}
/**
* Reads a class name
* <ebnf>
* class_name_reference ::= namespace_name | variable
* </ebnf>
*/
,read_class_name_reference: function() {
if (this.token === '\\' || this.token === this.tok.T_STRING) {
var result = this.read_namespace_name();
if (this.token === this.tok.T_DOUBLE_COLON) {
result = this.read_static_getter(result);
} else {
result = ['ns', result];
}
return result;
} else if (this.is('VARIABLE')) {
return this.read_variable(true);
} else {
this.expect([this.tok.T_STRING, 'VARIABLE']);
}
}
/**
* <ebnf>
* assignment_list ::= assignment_list_element (',' assignment_list_element?)*
* </ebnf>
*/
,read_assignment_list: function() {
return this.read_list(
this.read_assignment_list_element, ','
);
}
/**
* <ebnf>
* assignment_list_element ::= expr | expr T_DOUBLE_ARROW expr
* </ebnf>
*/
,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;
}
};