-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathclass.js
604 lines (573 loc) · 16.8 KB
/
class.js
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
/**
* 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 class
* ```ebnf
* class ::= class_scope? T_CLASS T_STRING (T_EXTENDS NAMESPACE_NAME)? (T_IMPLEMENTS (NAMESPACE_NAME ',')* NAMESPACE_NAME)? '{' CLASS_BODY '}'
* ```
*/
read_class_declaration_statement: function (attrs) {
const result = this.node("class");
const flag = this.read_class_modifiers();
// graceful mode : ignore token & go next
if (this.token !== this.tok.T_CLASS) {
this.error(this.tok.T_CLASS);
this.next();
return null;
}
this.next().expect(this.tok.T_STRING);
let propName = this.node("identifier");
const name = this.text();
this.next();
propName = propName(name);
const propExtends = this.read_extends_from();
const propImplements = this.read_implements_list();
this.expect("{");
const body = this.next().read_class_body(true, false);
const node = result(propName, propExtends, propImplements, body, flag);
if (attrs) node.attrGroups = attrs;
return node;
},
read_class_modifiers: function () {
const modifier = this.read_class_modifier({
readonly: 0,
final_or_abstract: 0,
});
return [0, 0, modifier.final_or_abstract, modifier.readonly];
},
read_class_modifier: function (memo) {
if (this.token === this.tok.T_READ_ONLY) {
this.next();
memo.readonly = 1;
memo = this.read_class_modifier(memo);
} else if (
memo.final_or_abstract === 0 &&
this.token === this.tok.T_ABSTRACT
) {
this.next();
memo.final_or_abstract = 1;
memo = this.read_class_modifier(memo);
} else if (
memo.final_or_abstract === 0 &&
this.token === this.tok.T_FINAL
) {
this.next();
memo.final_or_abstract = 2;
memo = this.read_class_modifier(memo);
}
return memo;
},
/*
* Reads a class body
* ```ebnf
* class_body ::= (member_flags? (T_VAR | T_STRING | T_FUNCTION))*
* ```
*/
read_class_body: function (allow_variables, allow_enum_cases) {
let result = [];
let attrs = [];
while (this.token !== this.EOF && this.token !== "}") {
if (this.token === this.tok.T_COMMENT) {
result.push(this.read_comment());
continue;
}
if (this.token === this.tok.T_DOC_COMMENT) {
result.push(this.read_doc_comment());
continue;
}
// check T_USE trait
if (this.token === this.tok.T_USE) {
result = result.concat(this.read_trait_use_statement());
continue;
}
// check enum cases
if (allow_enum_cases && this.token === this.tok.T_CASE) {
const enumcase = this.read_enum_case();
if (this.expect(";")) {
this.next();
}
result = result.concat(enumcase);
continue;
}
if (this.token === this.tok.T_ATTRIBUTE) {
attrs = this.read_attr_list();
}
const locStart = this.position();
// read member flags
const flags = this.read_member_flags(false);
// check constant
if (this.token === this.tok.T_CONST) {
const constants = this.read_constant_list(flags, attrs);
if (this.expect(";")) {
this.next();
}
result = result.concat(constants);
continue;
}
// jump over T_VAR then land on T_VARIABLE
if (allow_variables && this.token === this.tok.T_VAR) {
this.next().expect(this.tok.T_VARIABLE);
flags[0] = null; // public (as null)
flags[1] = 0; // non static var
}
if (this.token === this.tok.T_FUNCTION) {
// reads a function
result.push(this.read_function(false, flags, attrs, locStart));
attrs = [];
} else if (
allow_variables &&
(this.token === this.tok.T_VARIABLE ||
(this.version >= 801 && this.token === this.tok.T_READ_ONLY) ||
// support https://wiki.php.net/rfc/typed_properties_v2
(this.version >= 704 &&
(this.token === "?" ||
this.token === this.tok.T_ARRAY ||
this.token === this.tok.T_CALLABLE ||
this.token === this.tok.T_NAMESPACE ||
this.token === this.tok.T_NAME_FULLY_QUALIFIED ||
this.token === this.tok.T_NAME_QUALIFIED ||
this.token === this.tok.T_NAME_RELATIVE ||
this.token === this.tok.T_NS_SEPARATOR ||
this.token === this.tok.T_STRING)))
) {
// reads a variable
const variables = this.read_variable_list(flags, attrs);
attrs = [];
this.expect(";");
this.next();
result = result.concat(variables);
} else {
// raise an error
this.error([
this.tok.T_CONST,
...(allow_variables ? [this.tok.T_VARIABLE] : []),
...(allow_enum_cases ? [this.tok.T_CASE] : []),
this.tok.T_FUNCTION,
]);
// ignore token
this.next();
}
}
this.expect("}");
this.next();
return result;
},
/*
* Reads variable list
* ```ebnf
* variable_list ::= (variable_declaration ',')* variable_declaration
* ```
*/
read_variable_list: function (flags, attrs) {
const result = this.node("propertystatement");
const properties = this.read_list(
/*
* Reads a variable declaration
*
* ```ebnf
* variable_declaration ::= T_VARIABLE '=' scalar
* ```
*/
function read_variable_declaration() {
const result = this.node("property");
let readonly = false;
if (this.token === this.tok.T_READ_ONLY) {
readonly = true;
this.next();
}
const [nullable, type] = this.read_optional_type();
this.expect(this.tok.T_VARIABLE);
let propName = this.node("identifier");
const name = this.text().substring(1); // ignore $
this.next();
propName = propName(name);
if (this.token === ";" || this.token === ",") {
return result(propName, null, readonly, nullable, type, attrs || []);
} else if (this.token === "=") {
// https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L815
return result(
propName,
this.next().read_expr(),
readonly,
nullable,
type,
attrs || []
);
} else {
this.expect([",", ";", "="]);
return result(propName, null, nullable, type, attrs || []);
}
},
","
);
return result(null, properties, flags);
},
/*
* Reads constant list
* ```ebnf
* constant_list ::= T_CONST (constant_declaration ',')* constant_declaration
* ```
*/
read_constant_list: function (flags, attrs) {
if (this.expect(this.tok.T_CONST)) {
this.next();
}
const result = this.node("classconstant");
const items = this.read_list(
/*
* Reads a constant declaration
*
* ```ebnf
* constant_declaration ::= (T_STRING | IDENTIFIER) '=' expr
* ```
* @return {Constant} [:link:](AST.md#constant)
*/
function read_constant_declaration() {
const result = this.node("constant");
let constName = null;
let value = null;
if (
this.token === this.tok.T_STRING ||
(this.version >= 700 && this.is("IDENTIFIER"))
) {
constName = this.node("identifier");
const name = this.text();
this.next();
constName = constName(name);
} else {
this.expect("IDENTIFIER");
}
if (this.expect("=")) {
value = this.next().read_expr();
}
return result(constName, value);
},
","
);
return result(null, items, flags, attrs || []);
},
/*
* Read member flags
* @return array
* 1st index : 0 => public, 1 => protected, 2 => private
* 2nd index : 0 => instance member, 1 => static member
* 3rd index : 0 => normal, 1 => abstract member, 2 => final member
*/
read_member_flags: function (asInterface) {
const result = [-1, -1, -1];
if (this.is("T_MEMBER_FLAGS")) {
let idx = 0,
val = 0;
do {
switch (this.token) {
case this.tok.T_PUBLIC:
idx = 0;
val = 0;
break;
case this.tok.T_PROTECTED:
idx = 0;
val = 1;
break;
case this.tok.T_PRIVATE:
idx = 0;
val = 2;
break;
case this.tok.T_STATIC:
idx = 1;
val = 1;
break;
case this.tok.T_ABSTRACT:
idx = 2;
val = 1;
break;
case this.tok.T_FINAL:
idx = 2;
val = 2;
break;
}
if (asInterface) {
if (idx == 0 && val == 2) {
// an interface can't be private
this.expect([this.tok.T_PUBLIC, this.tok.T_PROTECTED]);
val = -1;
} else if (idx == 2 && val == 1) {
// an interface cant be abstract
this.error();
val = -1;
}
}
if (result[idx] !== -1) {
// already defined flag
this.error();
} else if (val !== -1) {
result[idx] = val;
}
} while (this.next().is("T_MEMBER_FLAGS"));
}
if (result[1] == -1) result[1] = 0;
if (result[2] == -1) result[2] = 0;
return result;
},
/*
* optional_type:
* /- empty -/ { $$ = NULL; }
* | type_expr { $$ = $1; }
* ;
*
* type_expr:
* type { $$ = $1; }
* | '?' type { $$ = $2; $$->attr |= ZEND_TYPE_NULLABLE; }
* | union_type { $$ = $1; }
* ;
*
* type:
* T_ARRAY { $$ = zend_ast_create_ex(ZEND_AST_TYPE, IS_ARRAY); }
* | T_CALLABLE { $$ = zend_ast_create_ex(ZEND_AST_TYPE, IS_CALLABLE); }
* | name { $$ = $1; }
* ;
*
* union_type:
* type '|' type { $$ = zend_ast_create_list(2, ZEND_AST_TYPE_UNION, $1, $3); }
* | union_type '|' type { $$ = zend_ast_list_add($1, $3); }
* ;
*/
read_optional_type: function () {
let nullable = false;
if (this.token === "?") {
nullable = true;
this.next();
}
let type = this.read_types();
if (nullable && !type) {
this.raiseError(
"Expecting a type definition combined with nullable operator"
);
}
if (!nullable && !type) {
return [false, null];
}
if (this.token === "|") {
type = [type];
do {
this.next();
const variant = this.read_type();
if (!variant) {
this.raiseError("Expecting a type definition");
break;
}
type.push(variant);
} while (this.token === "|");
}
return [nullable, type];
},
/*
* reading an interface
* ```ebnf
* interface ::= T_INTERFACE T_STRING (T_EXTENDS (NAMESPACE_NAME ',')* NAMESPACE_NAME)? '{' INTERFACE_BODY '}'
* ```
*/
read_interface_declaration_statement: function (attrs) {
const result = this.node("interface");
if (this.token !== this.tok.T_INTERFACE) {
this.error(this.tok.T_INTERFACE);
this.next();
return null;
}
this.next().expect(this.tok.T_STRING);
let propName = this.node("identifier");
const name = this.text();
this.next();
propName = propName(name);
const propExtends = this.read_interface_extends_list();
this.expect("{");
const body = this.next().read_interface_body();
return result(propName, propExtends, body, attrs || []);
},
/*
* Reads an interface body
* ```ebnf
* interface_body ::= (member_flags? (T_CONST | T_FUNCTION))*
* ```
*/
read_interface_body: function () {
let result = [],
attrs = [];
while (this.token !== this.EOF && this.token !== "}") {
if (this.token === this.tok.T_COMMENT) {
result.push(this.read_comment());
continue;
}
if (this.token === this.tok.T_DOC_COMMENT) {
result.push(this.read_doc_comment());
continue;
}
const locStart = this.position();
attrs = this.read_attr_list();
// read member flags
const flags = this.read_member_flags(true);
// check constant
if (this.token == this.tok.T_CONST) {
const constants = this.read_constant_list(flags, attrs);
if (this.expect(";")) {
this.next();
}
result = result.concat(constants);
attrs = [];
} else if (this.token === this.tok.T_FUNCTION) {
// reads a function
const method = this.read_function_declaration(
2,
flags,
attrs,
locStart
);
method.parseFlags(flags);
result.push(method);
if (this.expect(";")) {
this.next();
}
attrs = [];
} else {
// raise an error
this.error([this.tok.T_CONST, this.tok.T_FUNCTION]);
this.next();
}
}
if (this.expect("}")) {
this.next();
}
return result;
},
/*
* reading a trait
* ```ebnf
* trait ::= T_TRAIT T_STRING (T_EXTENDS (NAMESPACE_NAME ',')* NAMESPACE_NAME)? '{' FUNCTION* '}'
* ```
*/
read_trait_declaration_statement: function () {
const result = this.node("trait");
// graceful mode : ignore token & go next
if (this.token !== this.tok.T_TRAIT) {
this.error(this.tok.T_TRAIT);
this.next();
return null;
}
this.next().expect(this.tok.T_STRING);
let propName = this.node("identifier");
const name = this.text();
this.next();
propName = propName(name);
this.expect("{");
const body = this.next().read_class_body(true, false);
return result(propName, body);
},
/*
* reading a use statement
* ```ebnf
* trait_use_statement ::= namespace_name (',' namespace_name)* ('{' trait_use_alias '}')?
* ```
*/
read_trait_use_statement: function () {
// defines use statements
const node = this.node("traituse");
this.expect(this.tok.T_USE) && this.next();
const traits = [this.read_namespace_name()];
let adaptations = null;
while (this.token === ",") {
traits.push(this.next().read_namespace_name());
}
if (this.token === "{") {
adaptations = [];
// defines alias statements
while (this.next().token !== this.EOF) {
if (this.token === "}") break;
adaptations.push(this.read_trait_use_alias());
this.expect(";");
}
if (this.expect("}")) {
this.next();
}
} else {
if (this.expect(";")) {
this.next();
}
}
return node(traits, adaptations);
},
/*
* Reading trait alias
* ```ebnf
* trait_use_alias ::= namespace_name ( T_DOUBLE_COLON T_STRING )? (T_INSTEADOF namespace_name) | (T_AS member_flags? T_STRING)
* ```
* name list : https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L303
* trait adaptation : https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L742
*/
read_trait_use_alias: function () {
const node = this.node();
let trait = null;
let method;
if (this.is("IDENTIFIER")) {
method = this.node("identifier");
const methodName = this.text();
this.next();
method = method(methodName);
} else {
method = this.read_namespace_name();
if (this.token === this.tok.T_DOUBLE_COLON) {
this.next();
if (
this.token === this.tok.T_STRING ||
(this.version >= 700 && this.is("IDENTIFIER"))
) {
trait = method;
method = this.node("identifier");
const methodName = this.text();
this.next();
method = method(methodName);
} else {
this.expect(this.tok.T_STRING);
}
} else {
// convert identifier as string
method = method.name;
}
}
// handle trait precedence
if (this.token === this.tok.T_INSTEADOF) {
return node(
"traitprecedence",
trait,
method,
this.next().read_name_list()
);
} else if (this.token === this.tok.T_AS) {
// handle trait alias
let flags = null;
let alias = null;
if (this.next().is("T_MEMBER_FLAGS")) {
flags = this.read_member_flags();
}
if (
this.token === this.tok.T_STRING ||
(this.version >= 700 && this.is("IDENTIFIER"))
) {
alias = this.node("identifier");
const name = this.text();
this.next();
alias = alias(name);
} else if (flags === false) {
// no visibility flags and no name => too bad
this.expect(this.tok.T_STRING);
}
return node("traitalias", trait, method, alias, flags);
}
// handle errors
this.expect([this.tok.T_AS, this.tok.T_INSTEADOF]);
return node("traitalias", trait, method, null, null);
},
};