forked from glayzzle/php-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.js
More file actions
534 lines (510 loc) · 13.8 KB
/
function.js
File metadata and controls
534 lines (510 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
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
/**
* Copyright (C) 2018 Glayzzle (BSD3 License)
* @authors https://github.com/glayzzle/php-parser/graphs/contributors
* @url http://glayzzle.com
*/
"use strict";
module.exports = {
/*
* checks if current token is a reference keyword
*/
is_reference() {
if (this.token === "&") {
this.next();
return true;
}
return false;
},
/*
* checks if current token is a variadic keyword
*/
is_variadic() {
if (this.token === this.tok.T_ELLIPSIS) {
this.next();
return true;
}
return false;
},
/*
* reading a function
* ```ebnf
* function ::= function_declaration code_block
* ```
*/
read_function(closure, flag, attrs, locStart) {
const result = this.read_function_declaration(
closure ? 1 : flag ? 2 : 0,
flag && flag[1] === 1,
attrs || [],
locStart,
);
if (flag && flag[2] == 1) {
// abstract function :
result.parseFlags(flag);
if (this.expect(";")) {
this.next();
}
} else {
if (this.expect("{")) {
result.body = this.read_code_block(false);
if (result.loc && result.body.loc) {
result.loc.end = result.body.loc.end;
}
}
if (!closure && flag) {
result.parseFlags(flag);
}
}
return result;
},
/*
* reads a function declaration (without his body)
* ```ebnf
* function_declaration ::= T_FUNCTION '&'? T_STRING '(' parameter_list ')'
* ```
*/
read_function_declaration(type, isStatic, attrs, locStart) {
let nodeName = "function";
if (type === 1) {
nodeName = "closure";
} else if (type === 2) {
nodeName = "method";
}
const result = this.node(nodeName);
if (this.expect(this.tok.T_FUNCTION)) {
this.next();
}
const isRef = this.is_reference();
let name = false,
use = [],
returnType = null,
nullable = false;
if (type !== 1) {
const nameNode = this.node("identifier");
if (type === 2) {
if (this.version >= 700) {
if (this.token === this.tok.T_STRING || this.is("IDENTIFIER")) {
name = this.text();
this.next();
} else if (this.version < 704) {
this.error("IDENTIFIER");
}
} else if (this.token === this.tok.T_STRING) {
name = this.text();
this.next();
} else {
this.error("IDENTIFIER");
}
} else {
if (this.version >= 700) {
if (this.token === this.tok.T_STRING) {
name = this.text();
this.next();
} else if (this.version >= 704) {
if (!this.expect("(")) {
this.next();
}
} else {
this.error(this.tok.T_STRING);
this.next();
}
} else {
if (this.expect(this.tok.T_STRING)) {
name = this.text();
}
this.next();
}
}
name = nameNode(name);
}
if (this.expect("(")) this.next();
const params = this.read_parameter_list(name.name === "__construct");
if (this.expect(")")) this.next();
if (type === 1) {
use = this.read_lexical_vars();
}
if (this.token === ":") {
if (this.next().token === "?") {
nullable = true;
this.next();
}
returnType = this.read_types();
}
const apply_attrgroup_location = (node) => {
node.attrGroups = attrs || [];
if (locStart && node.loc) {
node.loc.start = locStart;
if (node.loc.source) {
node.loc.source = this.lexer._input.substr(
node.loc.start.offset,
node.loc.end.offset - node.loc.start.offset,
);
}
}
return node;
};
if (type === 1) {
// closure
return apply_attrgroup_location(
result(params, isRef, use, returnType, nullable, isStatic),
);
}
return apply_attrgroup_location(
result(name, params, isRef, returnType, nullable),
);
},
read_lexical_vars() {
let result = [];
if (this.token === this.tok.T_USE) {
this.next();
this.expect("(") && this.next();
result = this.read_lexical_var_list();
this.expect(")") && this.next();
}
return result;
},
read_list_with_dangling_comma(item) {
const result = [];
while (this.token != this.EOF) {
result.push(item());
if (this.token == ",") {
this.next();
if (this.version >= 800 && this.token === ")") {
return result;
}
} else if (this.token == ")") {
break;
} else {
this.error([",", ")"]);
break;
}
}
return result;
},
read_lexical_var_list() {
return this.read_list_with_dangling_comma(this.read_lexical_var.bind(this));
},
/*
* ```ebnf
* lexical_var ::= '&'? T_VARIABLE
* ```
*/
read_lexical_var() {
if (this.token === "&") {
return this.read_byref(this.read_lexical_var.bind(this));
}
const result = this.node("variable");
this.expect(this.tok.T_VARIABLE);
const name = this.text().substring(1);
this.next();
return result(name, false);
},
/*
* reads a list of parameters
* ```ebnf
* parameter_list ::= (parameter ',')* parameter?
* ```
*/
read_parameter_list(is_class_constructor) {
if (this.token !== ")") {
let wasVariadic = false;
return this.read_list_with_dangling_comma(
function () {
const parameter = this.read_parameter(is_class_constructor);
if (parameter) {
// variadic parameters can only be defined at the end of the parameter list
if (wasVariadic) {
this.raiseError(
"Unexpected parameter after a variadic parameter",
);
}
if (parameter.variadic) {
wasVariadic = true;
}
}
return parameter;
}.bind(this),
",",
);
}
return [];
},
/*
* ```ebnf
* parameter ::= type? '&'? T_ELLIPSIS? T_VARIABLE ('=' expr)?
* ```
* @see https://github.com/php/php-src/blob/493524454d66adde84e00d249d607ecd540de99f/Zend/zend_language_parser.y#L640
*/
read_parameter(is_class_constructor) {
const node = this.node("parameter");
let parameterName = null;
let value = null;
let nullable = false;
let readonly = false;
let attrs = [];
if (this.token === this.tok.T_ATTRIBUTE) attrs = this.read_attr_list();
if (this.version >= 801 && this.token === this.tok.T_READ_ONLY) {
if (is_class_constructor) {
this.next();
readonly = true;
} else {
this.raiseError(
"readonly properties can be used only on class constructor",
);
}
}
const flags = this.read_promoted();
if (
!readonly &&
this.version >= 801 &&
this.token === this.tok.T_READ_ONLY
) {
if (is_class_constructor) {
this.next();
readonly = true;
} else {
this.raiseError(
"readonly properties can be used only on class constructor",
);
}
}
if (this.token === "?") {
this.next();
nullable = true;
}
const types = this.read_types();
if (nullable && !types) {
this.raiseError(
"Expecting a type definition combined with nullable operator",
);
}
const isRef = this.is_reference();
const isVariadic = this.is_variadic();
if (this.expect(this.tok.T_VARIABLE)) {
parameterName = this.node("identifier");
const name = this.text().substring(1);
this.next();
parameterName = parameterName(name);
}
if (this.token == "=") {
value = this.next().read_expr();
}
let hooks = [];
if (this.version >= 804 && flags && this.token === "{") {
hooks = this.read_property_hooks();
}
const result = node(
parameterName,
types,
value,
isRef,
isVariadic,
readonly,
nullable,
flags,
hooks,
);
if (attrs) result.attrGroups = attrs;
return result;
},
read_types() {
const MODE_UNSET = "unset";
const MODE_UNION = "union";
const MODE_INTERSECTION = "intersection";
const types = [];
let mode = MODE_UNSET;
const node = this.node();
const type = this.read_type();
if (!type) {
node.destroy();
return null;
}
// we have matched a single type
types.push(type);
// is the current token a:
// - | for union type
// - & for intersection type (> php 8.1)
while (this.token === "|" || (this.version >= 801 && this.token === "&")) {
const nextToken = this.peek();
if (
nextToken === this.tok.T_ELLIPSIS ||
nextToken === this.tok.T_VARIABLE
) {
// the next token is part of the variable (or the variable itself),
// we're not gonna match anymore types
break;
}
if (mode === MODE_UNSET) {
// are we in union or intersection "mode"
mode = this.token === "|" ? MODE_UNION : MODE_INTERSECTION;
} else {
// it is not possible to mix "modes"
if (
(mode === MODE_UNION && this.token !== "|") ||
(mode === MODE_INTERSECTION && this.token !== "&")
) {
this.raiseError(
'Unexpect token "' + this.token + '", "|" and "&" can not be mixed',
);
}
}
this.next();
types.push(this.read_type());
}
if (types.length === 1) {
node.destroy();
return types[0];
} else {
return mode === MODE_INTERSECTION
? node("intersectiontype", types)
: node("uniontype", types);
}
},
read_promoted() {
const MODIFIER_PUBLIC = 1;
const MODIFIER_PROTECTED = 2;
const MODIFIER_PRIVATE = 4;
if (this.token === this.tok.T_PUBLIC) {
this.next();
return MODIFIER_PUBLIC;
} else if (this.token === this.tok.T_PROTECTED) {
this.next();
return MODIFIER_PROTECTED;
} else if (this.token === this.tok.T_PRIVATE) {
this.next();
return MODIFIER_PRIVATE;
}
return 0;
},
/*
* Reads a list of arguments
* ```ebnf
* function_argument_list ::= '(' (argument_list (',' argument_list)*)? ')'
* ```
*/
read_argument_list() {
let result = [];
this.expect("(") && this.next();
if (
this.version >= 801 &&
this.token === this.tok.T_ELLIPSIS &&
this.peek() === ")"
) {
result.push(this.node("variadicplaceholder")());
this.next();
} else if (this.token !== ")") {
result = this.read_non_empty_argument_list();
}
this.expect(")") && this.next();
return result;
},
/*
* Reads non empty argument list
*/
read_non_empty_argument_list() {
let wasVariadic = false;
return this.read_function_list(
function () {
const argument = this.read_argument();
if (argument) {
const isVariadic = argument.kind === "variadic";
// variadic arguments can only be followed by other variadic arguments
if (wasVariadic && !isVariadic) {
this.raiseError(
"Unexpected non-variadic argument after a variadic argument",
);
}
if (isVariadic) {
wasVariadic = true;
}
}
return argument;
}.bind(this),
",",
);
},
/*
* ```ebnf
* argument_list ::= T_STRING ':' expr | T_ELLIPSIS? expr
* ```
*/
read_argument() {
if (this.token === this.tok.T_ELLIPSIS) {
return this.node("variadic")(this.next().read_expr());
}
if (
this.token === this.tok.T_STRING ||
Object.values(this.lexer.keywords).includes(this.token)
) {
const nextToken = this.peek();
if (nextToken === ":") {
if (this.version < 800) {
this.raiseError("PHP 8+ is required to use named arguments");
}
return this.node("namedargument")(
this.text(),
this.next().next().read_expr(),
);
}
}
return this.read_expr();
},
/*
* read type hinting
* ```ebnf
* type ::= T_ARRAY | T_CALLABLE | namespace_name
* ```
*/
read_type() {
const result = this.node();
if (this.token === this.tok.T_ARRAY || this.token === this.tok.T_CALLABLE) {
const type = this.text();
this.next();
return result("typereference", type.toLowerCase(), type);
} else if (
this.token === this.tok.T_NAME_RELATIVE ||
this.token === this.tok.T_NAME_QUALIFIED ||
this.token === this.tok.T_NAME_FULLY_QUALIFIED ||
this.token === this.tok.T_STRING ||
this.token === this.tok.T_STATIC
) {
const type = this.text();
const backup = [this.token, this.lexer.getState()];
this.next();
if (
this.token !== this.tok.T_NS_SEPARATOR &&
this.ast.typereference.types.indexOf(type.toLowerCase()) > -1
) {
return result("typereference", type.toLowerCase(), type);
} else {
// rollback a classic namespace
this.lexer.tokens.push(backup);
this.next();
// fix : destroy not consumed node (release comments)
result.destroy();
return this.read_namespace_name();
}
} else if (this.version >= 802 && this.token === "(") {
// DNF type (PHP 8.2+): parenthesized intersection type, e.g. (A&B)|null
this.next(); // consume (
const innerTypes = [];
innerTypes.push(this.read_type());
while (this.token === "&") {
const nextToken = this.peek();
if (
nextToken === this.tok.T_ELLIPSIS ||
nextToken === this.tok.T_VARIABLE
) {
break;
}
this.next();
innerTypes.push(this.read_type());
}
this.expect(")") && this.next(); // consume )
return result("intersectiontype", innerTypes);
}
// fix : destroy not consumed node (release comments)
result.destroy();
return null;
},
};