forked from glayzzle/php-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatement.js
More file actions
407 lines (383 loc) · 12.1 KB
/
statement.js
File metadata and controls
407 lines (383 loc) · 12.1 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
/**
* Copyright (C) 2018 Glayzzle (BSD3 License)
* @authors https://github.com/glayzzle/php-parser/graphs/contributors
* @url http://glayzzle.com
*/
"use strict";
module.exports = {
/**
* reading a list of top statements (helper for top_statement*)
* ```ebnf
* top_statements ::= top_statement*
* ```
*/
read_top_statements: function() {
let result = [];
while (this.token !== this.EOF && this.token !== "}") {
const statement = this.read_top_statement();
if (statement) {
if (Array.isArray(statement)) {
result = result.concat(statement);
} else {
result.push(statement);
}
}
}
return result;
},
/**
* reading a top statement
* ```ebnf
* top_statement ::=
* namespace | function | class
* | interface | trait
* | use_statements | const_list
* | statement
* ```
*/
read_top_statement: function() {
switch (this.token) {
case this.tok.T_FUNCTION:
return this.read_function(false, false);
// optional flags
case this.tok.T_ABSTRACT:
case this.tok.T_FINAL:
case this.tok.T_CLASS:
return this.read_class_declaration_statement();
case this.tok.T_INTERFACE:
return this.read_interface_declaration_statement();
case this.tok.T_TRAIT:
return this.read_trait_declaration_statement();
case this.tok.T_USE:
return this.read_use_statement();
case this.tok.T_CONST: {
const result = this.node("constantstatement");
const items = this.next().read_const_list();
this.expectEndOfStatement();
return result(null, items);
}
case this.tok.T_NAMESPACE:
return this.read_namespace();
case this.tok.T_HALT_COMPILER: {
const result = this.node("halt");
if (this.next().expect("(")) this.next();
if (this.expect(")")) this.next();
this.expect(";");
this.lexer.done = true;
return result(this.lexer._input.substring(this.lexer.offset));
}
default:
return this.read_statement();
}
},
/**
* reads a list of simple inner statements (helper for inner_statement*)
* ```ebnf
* inner_statements ::= inner_statement*
* ```
*/
read_inner_statements: function() {
let result = [];
while (this.token != this.EOF && this.token !== "}") {
const statement = this.read_inner_statement();
if (statement) {
if (Array.isArray(statement)) {
result = result.concat(statement);
} else {
result.push(statement);
}
}
}
return result;
},
/**
* Reads a list of constants declaration
* ```ebnf
* const_list ::= T_CONST T_STRING '=' expr (',' T_STRING '=' expr)* ';'
* ```
*/
read_const_list: function() {
return this.read_list(
function() {
this.expect(this.tok.T_STRING);
const result = this.node("constant");
let constName = this.node("identifier");
const name = this.text();
this.next();
constName = constName(name);
if (this.expect("=")) {
return result(constName, this.next().read_expr());
} else {
// fallback
return result(constName, null);
}
},
",",
false
);
},
/**
* Reads a list of constants declaration
* ```ebnf
* declare_list ::= IDENTIFIER '=' expr (',' IDENTIFIER '=' expr)*
* ```
* @retrurn {Array}
*/
read_declare_list: function() {
const result = [];
while (this.token != this.EOF && this.token !== ")") {
this.expect(this.tok.T_STRING);
const directive = this.node("declaredirective");
let key = this.node("identifier");
const name = this.text();
this.next();
key = key(name);
let value = null;
if (this.expect("=")) {
value = this.next().read_expr();
}
result.push(directive(key, value));
if (this.token !== ",") break;
this.next();
}
return result;
},
/**
* reads a simple inner statement
* ```ebnf
* inner_statement ::= '{' inner_statements '}' | token
* ```
*/
read_inner_statement: function() {
switch (this.token) {
case this.tok.T_FUNCTION:
return this.read_function(false, false);
// optional flags
case this.tok.T_ABSTRACT:
case this.tok.T_FINAL:
case this.tok.T_CLASS:
return this.read_class_declaration_statement();
case this.tok.T_INTERFACE:
return this.read_interface_declaration_statement();
case this.tok.T_TRAIT:
return this.read_trait_declaration_statement();
case this.tok.T_HALT_COMPILER: {
this.raiseError(
"__HALT_COMPILER() can only be used from the outermost scope"
);
// fallback : returns a node but does not stop the parsing
let node = this.node("halt");
this.next().expect("(") && this.next();
this.expect(")") && this.next();
node = node(this.lexer._input.substring(this.lexer.offset));
this.expect(";") && this.next();
return node;
}
default:
return this.read_statement();
}
},
/**
* Reads statements
*/
read_statement: function() {
switch (this.token) {
case "{":
return this.read_code_block(false);
case this.tok.T_IF:
return this.read_if();
case this.tok.T_SWITCH:
return this.read_switch();
case this.tok.T_FOR:
return this.read_for();
case this.tok.T_FOREACH:
return this.read_foreach();
case this.tok.T_WHILE:
return this.read_while();
case this.tok.T_DO:
return this.read_do();
case this.tok.T_COMMENT:
return this.read_comment();
case this.tok.T_DOC_COMMENT:
return this.read_doc_comment();
case this.tok.T_RETURN: {
const result = this.node("return");
this.next();
const expr = this.read_optional_expr(";");
this.expectEndOfStatement();
return result(expr);
}
// https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L429
case this.tok.T_BREAK:
case this.tok.T_CONTINUE: {
const result = this.node(
this.token === this.tok.T_CONTINUE ? "continue" : "break"
);
this.next();
const level = this.read_optional_expr(";");
this.expectEndOfStatement();
return result(level);
}
case this.tok.T_GLOBAL: {
const result = this.node("global");
const items = this.next().read_list(this.read_simple_variable, ",");
this.expectEndOfStatement();
return result(items);
}
case this.tok.T_STATIC: {
const current = [this.token, this.lexer.getState()];
const result = this.node();
if (this.next().token === this.tok.T_DOUBLE_COLON) {
// static keyword for a class
this.lexer.tokens.push(current);
const expr = this.next().read_expr();
this.expectEndOfStatement(expr);
return result("expressionstatement", expr);
}
if (this.token === this.tok.T_FUNCTION) {
return this.read_function(true, [0, 1, 0]);
}
const items = this.read_variable_declarations();
this.expectEndOfStatement();
return result("static", items);
}
case this.tok.T_ECHO: {
const result = this.node("echo");
const text = this.text();
const shortForm = text === "<?=" || text === "<%=";
const expressions = this.next().read_function_list(this.read_expr, ",");
this.expectEndOfStatement();
return result(expressions, shortForm);
}
case this.tok.T_INLINE_HTML: {
const value = this.text();
let prevChar =
this.lexer.yylloc.first_offset > 0
? this.lexer._input[this.lexer.yylloc.first_offset - 1]
: null;
const fixFirstLine = prevChar === "\r" || prevChar === "\n";
// revert back the first stripped line
if (fixFirstLine) {
if (
prevChar === "\n" &&
this.lexer.yylloc.first_offset > 1 &&
this.lexer._input[this.lexer.yylloc.first_offset - 2] === "\r"
) {
prevChar = "\r\n";
}
}
const result = this.node("inline");
this.next();
return result(value, fixFirstLine ? prevChar + value : value);
}
case this.tok.T_UNSET: {
const result = this.node("unset");
this.next().expect("(") && this.next();
const variables = this.read_function_list(this.read_variable, ",");
this.expect(")") && this.next();
this.expect(";") && this.next();
return result(variables);
}
case this.tok.T_DECLARE: {
const result = this.node("declare");
const body = [];
let mode;
this.next().expect("(") && this.next();
const directives = this.read_declare_list();
this.expect(")") && this.next();
if (this.token === ":") {
this.next();
while (
this.token != this.EOF &&
this.token !== this.tok.T_ENDDECLARE
) {
// @todo : check declare_statement from php / not valid
body.push(this.read_top_statement());
}
this.expect(this.tok.T_ENDDECLARE) && this.next();
this.expectEndOfStatement();
mode = this.ast.declare.MODE_SHORT;
} else if (this.token === "{") {
this.next();
while (this.token != this.EOF && this.token !== "}") {
// @todo : check declare_statement from php / not valid
body.push(this.read_top_statement());
}
this.expect("}") && this.next();
mode = this.ast.declare.MODE_BLOCK;
} else {
this.expect(";") && this.next();
mode = this.ast.declare.MODE_NONE;
}
return result(directives, body, mode);
}
case this.tok.T_TRY:
return this.read_try();
case this.tok.T_THROW: {
const result = this.node("throw");
const expr = this.next().read_expr();
this.expectEndOfStatement();
return result(expr);
}
// ignore this (extra ponctuation)
case ";": {
this.next();
return null;
}
case this.tok.T_STRING: {
const result = this.node();
const current = [this.token, this.lexer.getState()];
const labelNameText = this.text();
let labelName = this.node("identifier");
// AST : https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L457
if (this.next().token === ":") {
labelName = labelName(labelNameText);
this.next();
return result("label", labelName);
} else {
labelName.destroy();
}
// default fallback expr / T_STRING '::' (etc...)
result.destroy();
this.lexer.tokens.push(current);
const statement = this.node("expressionstatement");
const expr = this.next().read_expr();
this.expectEndOfStatement(expr);
return statement(expr);
}
case this.tok.T_GOTO: {
const result = this.node("goto");
let labelName = null;
if (this.next().expect(this.tok.T_STRING)) {
labelName = this.node("identifier");
const name = this.text();
this.next();
labelName = labelName(name);
this.expectEndOfStatement();
}
return result(labelName);
}
default: {
// default fallback expr
const statement = this.node("expressionstatement");
const expr = this.read_expr();
this.expectEndOfStatement(expr);
return statement(expr);
}
}
},
/**
* ```ebnf
* code_block ::= '{' (inner_statements | top_statements) '}'
* ```
*/
read_code_block: function(top) {
const result = this.node("block");
this.expect("{") && this.next();
const body = top
? this.read_top_statements()
: this.read_inner_statements();
this.expect("}") && this.next();
return result(null, body);
}
};