Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Zur aktuellen Revision | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
%pure_parser
2
%expect 2
3
 
4
%tokens
5
 
6
%%
7
 
8
start:
9
    top_statement_list                                      { $$ = $this->handleNamespaces($1); }
10
;
11
 
12
top_statement_list_ex:
13
      top_statement_list_ex top_statement                   { pushNormalizing($1, $2); }
14
    | /* empty */                                           { init(); }
15
;
16
 
17
top_statement_list:
18
      top_statement_list_ex
19
          { makeZeroLengthNop($nop, $this->lookaheadStartAttributes);
20
            if ($nop !== null) { $1[] = $nop; } $$ = $1; }
21
;
22
 
23
ampersand:
24
      T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG
25
    | T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG
26
;
27
 
28
reserved_non_modifiers:
29
      T_INCLUDE | T_INCLUDE_ONCE | T_EVAL | T_REQUIRE | T_REQUIRE_ONCE | T_LOGICAL_OR | T_LOGICAL_XOR | T_LOGICAL_AND
30
    | T_INSTANCEOF | T_NEW | T_CLONE | T_EXIT | T_IF | T_ELSEIF | T_ELSE | T_ENDIF | T_ECHO | T_DO | T_WHILE
31
    | T_ENDWHILE | T_FOR | T_ENDFOR | T_FOREACH | T_ENDFOREACH | T_DECLARE | T_ENDDECLARE | T_AS | T_TRY | T_CATCH
32
    | T_FINALLY | T_THROW | T_USE | T_INSTEADOF | T_GLOBAL | T_VAR | T_UNSET | T_ISSET | T_EMPTY | T_CONTINUE | T_GOTO
33
    | T_FUNCTION | T_CONST | T_RETURN | T_PRINT | T_YIELD | T_LIST | T_SWITCH | T_ENDSWITCH | T_CASE | T_DEFAULT
34
    | T_BREAK | T_ARRAY | T_CALLABLE | T_EXTENDS | T_IMPLEMENTS | T_NAMESPACE | T_TRAIT | T_INTERFACE | T_CLASS
35
    | T_CLASS_C | T_TRAIT_C | T_FUNC_C | T_METHOD_C | T_LINE | T_FILE | T_DIR | T_NS_C | T_HALT_COMPILER | T_FN
36
    | T_MATCH | T_ENUM
37
;
38
 
39
semi_reserved:
40
      reserved_non_modifiers
41
    | T_STATIC | T_ABSTRACT | T_FINAL | T_PRIVATE | T_PROTECTED | T_PUBLIC | T_READONLY
42
;
43
 
44
identifier_maybe_reserved:
45
      T_STRING                                              { $$ = Node\Identifier[$1]; }
46
    | semi_reserved                                         { $$ = Node\Identifier[$1]; }
47
;
48
 
49
identifier_not_reserved:
50
      T_STRING                                              { $$ = Node\Identifier[$1]; }
51
;
52
 
53
reserved_non_modifiers_identifier:
54
      reserved_non_modifiers                                { $$ = Node\Identifier[$1]; }
55
;
56
 
57
namespace_declaration_name:
58
      T_STRING                                              { $$ = Name[$1]; }
59
    | semi_reserved                                         { $$ = Name[$1]; }
60
    | T_NAME_QUALIFIED                                      { $$ = Name[$1]; }
61
;
62
 
63
namespace_name:
64
      T_STRING                                              { $$ = Name[$1]; }
65
    | T_NAME_QUALIFIED                                      { $$ = Name[$1]; }
66
;
67
 
68
legacy_namespace_name:
69
      namespace_name                                        { $$ = $1; }
70
    | T_NAME_FULLY_QUALIFIED                                { $$ = Name[substr($1, 1)]; }
71
;
72
 
73
plain_variable:
74
      T_VARIABLE                                            { $$ = Expr\Variable[parseVar($1)]; }
75
;
76
 
77
semi:
78
      ';'                                                   { /* nothing */ }
79
    | error                                                 { /* nothing */ }
80
;
81
 
82
no_comma:
83
      /* empty */ { /* nothing */ }
84
    | ',' { $this->emitError(new Error('A trailing comma is not allowed here', attributes())); }
85
;
86
 
87
optional_comma:
88
      /* empty */
89
    | ','
90
;
91
 
92
attribute_decl:
93
      class_name                                            { $$ = Node\Attribute[$1, []]; }
94
    | class_name argument_list                              { $$ = Node\Attribute[$1, $2]; }
95
;
96
 
97
attribute_group:
98
      attribute_decl                                        { init($1); }
99
    | attribute_group ',' attribute_decl                    { push($1, $3); }
100
;
101
 
102
attribute:
103
      T_ATTRIBUTE attribute_group optional_comma ']'        { $$ = Node\AttributeGroup[$2]; }
104
;
105
 
106
attributes:
107
      attribute                                             { init($1); }
108
    | attributes attribute                                  { push($1, $2); }
109
;
110
 
111
optional_attributes:
112
      /* empty */                                           { $$ = []; }
113
    | attributes                                            { $$ = $1; }
114
;
115
 
116
top_statement:
117
      statement                                             { $$ = $1; }
118
    | function_declaration_statement                        { $$ = $1; }
119
    | class_declaration_statement                           { $$ = $1; }
120
    | T_HALT_COMPILER
121
          { $$ = Stmt\HaltCompiler[$this->lexer->handleHaltCompiler()]; }
122
    | T_NAMESPACE namespace_declaration_name semi
123
          { $$ = Stmt\Namespace_[$2, null];
124
            $$->setAttribute('kind', Stmt\Namespace_::KIND_SEMICOLON);
125
            $this->checkNamespace($$); }
126
    | T_NAMESPACE namespace_declaration_name '{' top_statement_list '}'
127
          { $$ = Stmt\Namespace_[$2, $4];
128
            $$->setAttribute('kind', Stmt\Namespace_::KIND_BRACED);
129
            $this->checkNamespace($$); }
130
    | T_NAMESPACE '{' top_statement_list '}'
131
          { $$ = Stmt\Namespace_[null, $3];
132
            $$->setAttribute('kind', Stmt\Namespace_::KIND_BRACED);
133
            $this->checkNamespace($$); }
134
    | T_USE use_declarations semi                           { $$ = Stmt\Use_[$2, Stmt\Use_::TYPE_NORMAL]; }
135
    | T_USE use_type use_declarations semi                  { $$ = Stmt\Use_[$3, $2]; }
136
    | group_use_declaration semi                            { $$ = $1; }
137
    | T_CONST constant_declaration_list semi                { $$ = Stmt\Const_[$2]; }
138
;
139
 
140
use_type:
141
      T_FUNCTION                                            { $$ = Stmt\Use_::TYPE_FUNCTION; }
142
    | T_CONST                                               { $$ = Stmt\Use_::TYPE_CONSTANT; }
143
;
144
 
145
group_use_declaration:
146
      T_USE use_type legacy_namespace_name T_NS_SEPARATOR '{' unprefixed_use_declarations '}'
147
          { $$ = Stmt\GroupUse[$3, $6, $2]; }
148
    | T_USE legacy_namespace_name T_NS_SEPARATOR '{' inline_use_declarations '}'
149
          { $$ = Stmt\GroupUse[$2, $5, Stmt\Use_::TYPE_UNKNOWN]; }
150
;
151
 
152
unprefixed_use_declarations:
153
      non_empty_unprefixed_use_declarations optional_comma  { $$ = $1; }
154
;
155
 
156
non_empty_unprefixed_use_declarations:
157
      non_empty_unprefixed_use_declarations ',' unprefixed_use_declaration
158
          { push($1, $3); }
159
    | unprefixed_use_declaration                            { init($1); }
160
;
161
 
162
use_declarations:
163
      non_empty_use_declarations no_comma                   { $$ = $1; }
164
;
165
 
166
non_empty_use_declarations:
167
      non_empty_use_declarations ',' use_declaration        { push($1, $3); }
168
    | use_declaration                                       { init($1); }
169
;
170
 
171
inline_use_declarations:
172
      non_empty_inline_use_declarations optional_comma      { $$ = $1; }
173
;
174
 
175
non_empty_inline_use_declarations:
176
      non_empty_inline_use_declarations ',' inline_use_declaration
177
          { push($1, $3); }
178
    | inline_use_declaration                                { init($1); }
179
;
180
 
181
unprefixed_use_declaration:
182
      namespace_name
183
          { $$ = Stmt\UseUse[$1, null, Stmt\Use_::TYPE_UNKNOWN]; $this->checkUseUse($$, #1); }
184
    | namespace_name T_AS identifier_not_reserved
185
          { $$ = Stmt\UseUse[$1, $3, Stmt\Use_::TYPE_UNKNOWN]; $this->checkUseUse($$, #3); }
186
;
187
 
188
use_declaration:
189
      legacy_namespace_name
190
          { $$ = Stmt\UseUse[$1, null, Stmt\Use_::TYPE_UNKNOWN]; $this->checkUseUse($$, #1); }
191
    | legacy_namespace_name T_AS identifier_not_reserved
192
          { $$ = Stmt\UseUse[$1, $3, Stmt\Use_::TYPE_UNKNOWN]; $this->checkUseUse($$, #3); }
193
;
194
 
195
inline_use_declaration:
196
      unprefixed_use_declaration                            { $$ = $1; $$->type = Stmt\Use_::TYPE_NORMAL; }
197
    | use_type unprefixed_use_declaration                   { $$ = $2; $$->type = $1; }
198
;
199
 
200
constant_declaration_list:
201
      non_empty_constant_declaration_list no_comma          { $$ = $1; }
202
;
203
 
204
non_empty_constant_declaration_list:
205
      non_empty_constant_declaration_list ',' constant_declaration
206
          { push($1, $3); }
207
    | constant_declaration                                  { init($1); }
208
;
209
 
210
constant_declaration:
211
    identifier_not_reserved '=' expr                        { $$ = Node\Const_[$1, $3]; }
212
;
213
 
214
class_const_list:
215
      non_empty_class_const_list no_comma                   { $$ = $1; }
216
;
217
 
218
non_empty_class_const_list:
219
      non_empty_class_const_list ',' class_const            { push($1, $3); }
220
    | class_const                                           { init($1); }
221
;
222
 
223
class_const:
224
    identifier_maybe_reserved '=' expr                      { $$ = Node\Const_[$1, $3]; }
225
;
226
 
227
inner_statement_list_ex:
228
      inner_statement_list_ex inner_statement               { pushNormalizing($1, $2); }
229
    | /* empty */                                           { init(); }
230
;
231
 
232
inner_statement_list:
233
      inner_statement_list_ex
234
          { makeZeroLengthNop($nop, $this->lookaheadStartAttributes);
235
            if ($nop !== null) { $1[] = $nop; } $$ = $1; }
236
;
237
 
238
inner_statement:
239
      statement                                             { $$ = $1; }
240
    | function_declaration_statement                        { $$ = $1; }
241
    | class_declaration_statement                           { $$ = $1; }
242
    | T_HALT_COMPILER
243
          { throw new Error('__HALT_COMPILER() can only be used from the outermost scope', attributes()); }
244
;
245
 
246
non_empty_statement:
247
      '{' inner_statement_list '}'
248
    {
249
        if ($2) {
250
            $$ = $2; prependLeadingComments($$);
251
        } else {
252
            makeNop($$, $this->startAttributeStack[#1], $this->endAttributes);
253
            if (null === $$) { $$ = array(); }
254
        }
255
    }
256
    | T_IF '(' expr ')' statement elseif_list else_single
257
          { $$ = Stmt\If_[$3, ['stmts' => toArray($5), 'elseifs' => $6, 'else' => $7]]; }
258
    | T_IF '(' expr ')' ':' inner_statement_list new_elseif_list new_else_single T_ENDIF ';'
259
          { $$ = Stmt\If_[$3, ['stmts' => $6, 'elseifs' => $7, 'else' => $8]]; }
260
    | T_WHILE '(' expr ')' while_statement                  { $$ = Stmt\While_[$3, $5]; }
261
    | T_DO statement T_WHILE '(' expr ')' ';'               { $$ = Stmt\Do_   [$5, toArray($2)]; }
262
    | T_FOR '(' for_expr ';'  for_expr ';' for_expr ')' for_statement
263
          { $$ = Stmt\For_[['init' => $3, 'cond' => $5, 'loop' => $7, 'stmts' => $9]]; }
264
    | T_SWITCH '(' expr ')' switch_case_list                { $$ = Stmt\Switch_[$3, $5]; }
265
    | T_BREAK optional_expr semi                            { $$ = Stmt\Break_[$2]; }
266
    | T_CONTINUE optional_expr semi                         { $$ = Stmt\Continue_[$2]; }
267
    | T_RETURN optional_expr semi                           { $$ = Stmt\Return_[$2]; }
268
    | T_GLOBAL global_var_list semi                         { $$ = Stmt\Global_[$2]; }
269
    | T_STATIC static_var_list semi                         { $$ = Stmt\Static_[$2]; }
270
    | T_ECHO expr_list_forbid_comma semi                    { $$ = Stmt\Echo_[$2]; }
271
    | T_INLINE_HTML                                         { $$ = Stmt\InlineHTML[$1]; }
272
    | expr semi {
273
        $e = $1;
274
        if ($e instanceof Expr\Throw_) {
275
            // For backwards-compatibility reasons, convert throw in statement position into
276
            // Stmt\Throw_ rather than Stmt\Expression(Expr\Throw_).
277
            $$ = Stmt\Throw_[$e->expr];
278
        } else {
279
            $$ = Stmt\Expression[$e];
280
        }
281
    }
282
    | T_UNSET '(' variables_list ')' semi                   { $$ = Stmt\Unset_[$3]; }
283
    | T_FOREACH '(' expr T_AS foreach_variable ')' foreach_statement
284
          { $$ = Stmt\Foreach_[$3, $5[0], ['keyVar' => null, 'byRef' => $5[1], 'stmts' => $7]]; }
285
    | T_FOREACH '(' expr T_AS variable T_DOUBLE_ARROW foreach_variable ')' foreach_statement
286
          { $$ = Stmt\Foreach_[$3, $7[0], ['keyVar' => $5, 'byRef' => $7[1], 'stmts' => $9]]; }
287
    | T_FOREACH '(' expr error ')' foreach_statement
288
          { $$ = Stmt\Foreach_[$3, new Expr\Error(stackAttributes(#4)), ['stmts' => $6]]; }
289
    | T_DECLARE '(' declare_list ')' declare_statement      { $$ = Stmt\Declare_[$3, $5]; }
290
    | T_TRY '{' inner_statement_list '}' catches optional_finally
291
          { $$ = Stmt\TryCatch[$3, $5, $6]; $this->checkTryCatch($$); }
292
    | T_GOTO identifier_not_reserved semi                   { $$ = Stmt\Goto_[$2]; }
293
    | identifier_not_reserved ':'                           { $$ = Stmt\Label[$1]; }
294
    | error                                                 { $$ = array(); /* means: no statement */ }
295
;
296
 
297
statement:
298
      non_empty_statement                                   { $$ = $1; }
299
    | ';'
300
          { makeNop($$, $this->startAttributeStack[#1], $this->endAttributes);
301
            if ($$ === null) $$ = array(); /* means: no statement */ }
302
;
303
 
304
catches:
305
      /* empty */                                           { init(); }
306
    | catches catch                                         { push($1, $2); }
307
;
308
 
309
name_union:
310
      name                                                  { init($1); }
311
    | name_union '|' name                                   { push($1, $3); }
312
;
313
 
314
catch:
315
    T_CATCH '(' name_union optional_plain_variable ')' '{' inner_statement_list '}'
316
        { $$ = Stmt\Catch_[$3, $4, $7]; }
317
;
318
 
319
optional_finally:
320
      /* empty */                                           { $$ = null; }
321
    | T_FINALLY '{' inner_statement_list '}'                { $$ = Stmt\Finally_[$3]; }
322
;
323
 
324
variables_list:
325
      non_empty_variables_list optional_comma               { $$ = $1; }
326
;
327
 
328
non_empty_variables_list:
329
      variable                                              { init($1); }
330
    | non_empty_variables_list ',' variable                 { push($1, $3); }
331
;
332
 
333
optional_ref:
334
      /* empty */                                           { $$ = false; }
335
    | ampersand                                             { $$ = true; }
336
;
337
 
338
optional_arg_ref:
339
      /* empty */                                           { $$ = false; }
340
    | T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG                 { $$ = true; }
341
;
342
 
343
optional_ellipsis:
344
      /* empty */                                           { $$ = false; }
345
    | T_ELLIPSIS                                            { $$ = true; }
346
;
347
 
348
block_or_error:
349
      '{' inner_statement_list '}'                          { $$ = $2; }
350
    | error                                                 { $$ = []; }
351
;
352
 
150 lars 353
identifier_maybe_readonly:
354
      identifier_not_reserved                               { $$ = $1; }
355
    | T_READONLY                                            { $$ = Node\Identifier[$1]; }
356
;
357
 
148 lars 358
function_declaration_statement:
150 lars 359
      T_FUNCTION optional_ref identifier_maybe_readonly '(' parameter_list ')' optional_return_type block_or_error
148 lars 360
          { $$ = Stmt\Function_[$3, ['byRef' => $2, 'params' => $5, 'returnType' => $7, 'stmts' => $8, 'attrGroups' => []]]; }
150 lars 361
    | attributes T_FUNCTION optional_ref identifier_maybe_readonly '(' parameter_list ')' optional_return_type block_or_error
148 lars 362
          { $$ = Stmt\Function_[$4, ['byRef' => $3, 'params' => $6, 'returnType' => $8, 'stmts' => $9, 'attrGroups' => $1]]; }
363
;
364
 
365
class_declaration_statement:
150 lars 366
      class_entry_type identifier_not_reserved extends_from implements_list '{' class_statement_list '}'
367
          { $$ = Stmt\Class_[$2, ['type' => $1, 'extends' => $3, 'implements' => $4, 'stmts' => $6, 'attrGroups' => []]];
368
            $this->checkClass($$, #2); }
369
    | attributes class_entry_type identifier_not_reserved extends_from implements_list '{' class_statement_list '}'
148 lars 370
          { $$ = Stmt\Class_[$3, ['type' => $2, 'extends' => $4, 'implements' => $5, 'stmts' => $7, 'attrGroups' => $1]];
371
            $this->checkClass($$, #3); }
372
    | optional_attributes T_INTERFACE identifier_not_reserved interface_extends_list '{' class_statement_list '}'
373
          { $$ = Stmt\Interface_[$3, ['extends' => $4, 'stmts' => $6, 'attrGroups' => $1]];
374
            $this->checkInterface($$, #3); }
375
    | optional_attributes T_TRAIT identifier_not_reserved '{' class_statement_list '}'
376
          { $$ = Stmt\Trait_[$3, ['stmts' => $5, 'attrGroups' => $1]]; }
377
    | optional_attributes T_ENUM identifier_not_reserved enum_scalar_type implements_list '{' class_statement_list '}'
378
          { $$ = Stmt\Enum_[$3, ['scalarType' => $4, 'implements' => $5, 'stmts' => $7, 'attrGroups' => $1]];
379
            $this->checkEnum($$, #3); }
380
;
381
 
382
enum_scalar_type:
383
      /* empty */                                           { $$ = null; }
384
    | ':' type                                              { $$ = $2; }
385
 
386
enum_case_expr:
387
      /* empty */                                           { $$ = null; }
388
    | '=' expr                                              { $$ = $2; }
389
;
390
 
391
class_entry_type:
392
      T_CLASS                                               { $$ = 0; }
393
    | class_modifiers T_CLASS                               { $$ = $1; }
394
;
395
 
396
class_modifiers:
397
      class_modifier                                        { $$ = $1; }
398
    | class_modifiers class_modifier                        { $this->checkClassModifier($1, $2, #2); $$ = $1 | $2; }
399
;
400
 
401
class_modifier:
402
      T_ABSTRACT                                            { $$ = Stmt\Class_::MODIFIER_ABSTRACT; }
403
    | T_FINAL                                               { $$ = Stmt\Class_::MODIFIER_FINAL; }
404
    | T_READONLY                                            { $$ = Stmt\Class_::MODIFIER_READONLY; }
405
;
406
 
407
extends_from:
408
      /* empty */                                           { $$ = null; }
409
    | T_EXTENDS class_name                                  { $$ = $2; }
410
;
411
 
412
interface_extends_list:
413
      /* empty */                                           { $$ = array(); }
414
    | T_EXTENDS class_name_list                             { $$ = $2; }
415
;
416
 
417
implements_list:
418
      /* empty */                                           { $$ = array(); }
419
    | T_IMPLEMENTS class_name_list                          { $$ = $2; }
420
;
421
 
422
class_name_list:
423
      non_empty_class_name_list no_comma                    { $$ = $1; }
424
;
425
 
426
non_empty_class_name_list:
427
      class_name                                            { init($1); }
428
    | non_empty_class_name_list ',' class_name              { push($1, $3); }
429
;
430
 
431
for_statement:
432
      statement                                             { $$ = toArray($1); }
433
    | ':' inner_statement_list T_ENDFOR ';'                 { $$ = $2; }
434
;
435
 
436
foreach_statement:
437
      statement                                             { $$ = toArray($1); }
438
    | ':' inner_statement_list T_ENDFOREACH ';'             { $$ = $2; }
439
;
440
 
441
declare_statement:
442
      non_empty_statement                                   { $$ = toArray($1); }
443
    | ';'                                                   { $$ = null; }
444
    | ':' inner_statement_list T_ENDDECLARE ';'             { $$ = $2; }
445
;
446
 
447
declare_list:
448
      non_empty_declare_list no_comma                       { $$ = $1; }
449
;
450
 
451
non_empty_declare_list:
452
      declare_list_element                                  { init($1); }
453
    | non_empty_declare_list ',' declare_list_element       { push($1, $3); }
454
;
455
 
456
declare_list_element:
457
      identifier_not_reserved '=' expr                      { $$ = Stmt\DeclareDeclare[$1, $3]; }
458
;
459
 
460
switch_case_list:
461
      '{' case_list '}'                                     { $$ = $2; }
462
    | '{' ';' case_list '}'                                 { $$ = $3; }
463
    | ':' case_list T_ENDSWITCH ';'                         { $$ = $2; }
464
    | ':' ';' case_list T_ENDSWITCH ';'                     { $$ = $3; }
465
;
466
 
467
case_list:
468
      /* empty */                                           { init(); }
469
    | case_list case                                        { push($1, $2); }
470
;
471
 
472
case:
473
      T_CASE expr case_separator inner_statement_list_ex    { $$ = Stmt\Case_[$2, $4]; }
474
    | T_DEFAULT case_separator inner_statement_list_ex      { $$ = Stmt\Case_[null, $3]; }
475
;
476
 
477
case_separator:
478
      ':'
479
    | ';'
480
;
481
 
482
match:
483
      T_MATCH '(' expr ')' '{' match_arm_list '}'           { $$ = Expr\Match_[$3, $6]; }
484
;
485
 
486
match_arm_list:
487
      /* empty */                                           { $$ = []; }
488
    | non_empty_match_arm_list optional_comma               { $$ = $1; }
489
;
490
 
491
non_empty_match_arm_list:
492
      match_arm                                             { init($1); }
493
    | non_empty_match_arm_list ',' match_arm                { push($1, $3); }
494
;
495
 
496
match_arm:
497
      expr_list_allow_comma T_DOUBLE_ARROW expr             { $$ = Node\MatchArm[$1, $3]; }
498
    | T_DEFAULT optional_comma T_DOUBLE_ARROW expr          { $$ = Node\MatchArm[null, $4]; }
499
;
500
 
501
while_statement:
502
      statement                                             { $$ = toArray($1); }
503
    | ':' inner_statement_list T_ENDWHILE ';'               { $$ = $2; }
504
;
505
 
506
elseif_list:
507
      /* empty */                                           { init(); }
508
    | elseif_list elseif                                    { push($1, $2); }
509
;
510
 
511
elseif:
512
      T_ELSEIF '(' expr ')' statement                       { $$ = Stmt\ElseIf_[$3, toArray($5)]; }
513
;
514
 
515
new_elseif_list:
516
      /* empty */                                           { init(); }
517
    | new_elseif_list new_elseif                            { push($1, $2); }
518
;
519
 
520
new_elseif:
521
     T_ELSEIF '(' expr ')' ':' inner_statement_list         { $$ = Stmt\ElseIf_[$3, $6]; }
522
;
523
 
524
else_single:
525
      /* empty */                                           { $$ = null; }
526
    | T_ELSE statement                                      { $$ = Stmt\Else_[toArray($2)]; }
527
;
528
 
529
new_else_single:
530
      /* empty */                                           { $$ = null; }
531
    | T_ELSE ':' inner_statement_list                       { $$ = Stmt\Else_[$3]; }
532
;
533
 
534
foreach_variable:
535
      variable                                              { $$ = array($1, false); }
536
    | ampersand variable                                    { $$ = array($2, true); }
537
    | list_expr                                             { $$ = array($1, false); }
538
    | array_short_syntax                                    { $$ = array($1, false); }
539
;
540
 
541
parameter_list:
542
      non_empty_parameter_list optional_comma               { $$ = $1; }
543
    | /* empty */                                           { $$ = array(); }
544
;
545
 
546
non_empty_parameter_list:
547
      parameter                                             { init($1); }
548
    | non_empty_parameter_list ',' parameter                { push($1, $3); }
549
;
550
 
551
optional_property_modifiers:
552
      /* empty */               { $$ = 0; }
553
    | optional_property_modifiers property_modifier
554
          { $this->checkModifier($1, $2, #2); $$ = $1 | $2; }
555
;
556
 
557
property_modifier:
558
      T_PUBLIC                  { $$ = Stmt\Class_::MODIFIER_PUBLIC; }
559
    | T_PROTECTED               { $$ = Stmt\Class_::MODIFIER_PROTECTED; }
560
    | T_PRIVATE                 { $$ = Stmt\Class_::MODIFIER_PRIVATE; }
561
    | T_READONLY                { $$ = Stmt\Class_::MODIFIER_READONLY; }
562
;
563
 
564
parameter:
565
      optional_attributes optional_property_modifiers optional_type_without_static
566
      optional_arg_ref optional_ellipsis plain_variable
567
          { $$ = new Node\Param($6, null, $3, $4, $5, attributes(), $2, $1);
568
            $this->checkParam($$); }
569
    | optional_attributes optional_property_modifiers optional_type_without_static
570
      optional_arg_ref optional_ellipsis plain_variable '=' expr
571
          { $$ = new Node\Param($6, $8, $3, $4, $5, attributes(), $2, $1);
572
            $this->checkParam($$); }
573
    | optional_attributes optional_property_modifiers optional_type_without_static
574
      optional_arg_ref optional_ellipsis error
575
          { $$ = new Node\Param(Expr\Error[], null, $3, $4, $5, attributes(), $2, $1); }
576
;
577
 
578
type_expr:
579
      type                                                  { $$ = $1; }
580
    | '?' type                                              { $$ = Node\NullableType[$2]; }
581
    | union_type                                            { $$ = Node\UnionType[$1]; }
582
    | intersection_type                                     { $$ = $1; }
583
;
584
 
585
type:
586
      type_without_static                                   { $$ = $1; }
587
    | T_STATIC                                              { $$ = Node\Name['static']; }
588
;
589
 
590
type_without_static:
591
      name                                                  { $$ = $this->handleBuiltinTypes($1); }
592
    | T_ARRAY                                               { $$ = Node\Identifier['array']; }
593
    | T_CALLABLE                                            { $$ = Node\Identifier['callable']; }
594
;
595
 
596
union_type_element:
597
                type { $$ = $1; }
598
        |        '(' intersection_type ')' { $$ = $2; }
599
;
600
 
601
union_type:
602
      union_type_element '|' union_type_element             { init($1, $3); }
603
    | union_type '|' union_type_element                     { push($1, $3); }
604
;
605
 
606
union_type_without_static_element:
607
                type_without_static { $$ = $1; }
608
        |        '(' intersection_type_without_static ')' { $$ = $2; }
609
;
610
 
611
union_type_without_static:
612
      union_type_without_static_element '|' union_type_without_static_element   { init($1, $3); }
613
    | union_type_without_static '|' union_type_without_static_element           { push($1, $3); }
614
;
615
 
616
intersection_type_list:
617
      type T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG type   { init($1, $3); }
618
    | intersection_type_list T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG type
619
          { push($1, $3); }
620
;
621
 
622
intersection_type:
623
      intersection_type_list { $$ = Node\IntersectionType[$1]; }
624
;
625
 
626
intersection_type_without_static_list:
627
      type_without_static T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG type_without_static
628
          { init($1, $3); }
629
    | intersection_type_without_static_list T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG type_without_static
630
          { push($1, $3); }
631
;
632
 
633
intersection_type_without_static:
634
      intersection_type_without_static_list { $$ = Node\IntersectionType[$1]; }
635
;
636
 
637
type_expr_without_static:
638
      type_without_static                                   { $$ = $1; }
639
    | '?' type_without_static                               { $$ = Node\NullableType[$2]; }
640
    | union_type_without_static                             { $$ = Node\UnionType[$1]; }
641
    | intersection_type_without_static                      { $$ = $1; }
642
;
643
 
644
optional_type_without_static:
645
      /* empty */                                           { $$ = null; }
646
    | type_expr_without_static                              { $$ = $1; }
647
;
648
 
649
optional_return_type:
650
      /* empty */                                           { $$ = null; }
651
    | ':' type_expr                                         { $$ = $2; }
652
    | ':' error                                             { $$ = null; }
653
;
654
 
655
argument_list:
656
      '(' ')'                                               { $$ = array(); }
657
    | '(' non_empty_argument_list optional_comma ')'        { $$ = $2; }
658
    | '(' variadic_placeholder ')'                          { init($2); }
659
;
660
 
661
variadic_placeholder:
662
      T_ELLIPSIS                                            { $$ = Node\VariadicPlaceholder[]; }
663
;
664
 
665
non_empty_argument_list:
666
      argument                                              { init($1); }
667
    | non_empty_argument_list ',' argument                  { push($1, $3); }
668
;
669
 
670
argument:
671
      expr                                                  { $$ = Node\Arg[$1, false, false]; }
672
    | ampersand variable                                    { $$ = Node\Arg[$2, true, false]; }
673
    | T_ELLIPSIS expr                                       { $$ = Node\Arg[$2, false, true]; }
674
    | identifier_maybe_reserved ':' expr
675
          { $$ = new Node\Arg($3, false, false, attributes(), $1); }
676
;
677
 
678
global_var_list:
679
      non_empty_global_var_list no_comma                    { $$ = $1; }
680
;
681
 
682
non_empty_global_var_list:
683
      non_empty_global_var_list ',' global_var              { push($1, $3); }
684
    | global_var                                            { init($1); }
685
;
686
 
687
global_var:
688
      simple_variable                                       { $$ = $1; }
689
;
690
 
691
static_var_list:
692
      non_empty_static_var_list no_comma                    { $$ = $1; }
693
;
694
 
695
non_empty_static_var_list:
696
      non_empty_static_var_list ',' static_var              { push($1, $3); }
697
    | static_var                                            { init($1); }
698
;
699
 
700
static_var:
701
      plain_variable                                        { $$ = Stmt\StaticVar[$1, null]; }
702
    | plain_variable '=' expr                               { $$ = Stmt\StaticVar[$1, $3]; }
703
;
704
 
705
class_statement_list_ex:
706
      class_statement_list_ex class_statement               { if ($2 !== null) { push($1, $2); } }
707
    | /* empty */                                           { init(); }
708
;
709
 
710
class_statement_list:
711
      class_statement_list_ex
712
          { makeZeroLengthNop($nop, $this->lookaheadStartAttributes);
713
            if ($nop !== null) { $1[] = $nop; } $$ = $1; }
714
;
715
 
716
class_statement:
717
      optional_attributes variable_modifiers optional_type_without_static property_declaration_list semi
718
          { $$ = new Stmt\Property($2, $4, attributes(), $3, $1);
719
            $this->checkProperty($$, #2); }
720
    | optional_attributes method_modifiers T_CONST class_const_list semi
721
          { $$ = new Stmt\ClassConst($4, $2, attributes(), $1);
722
            $this->checkClassConst($$, #2); }
723
    | optional_attributes method_modifiers T_FUNCTION optional_ref identifier_maybe_reserved '(' parameter_list ')'
724
      optional_return_type method_body
725
          { $$ = Stmt\ClassMethod[$5, ['type' => $2, 'byRef' => $4, 'params' => $7, 'returnType' => $9, 'stmts' => $10, 'attrGroups' => $1]];
726
            $this->checkClassMethod($$, #2); }
727
    | T_USE class_name_list trait_adaptations               { $$ = Stmt\TraitUse[$2, $3]; }
728
    | optional_attributes T_CASE identifier_maybe_reserved enum_case_expr semi
729
         { $$ = Stmt\EnumCase[$3, $4, $1]; }
730
    | error                                                 { $$ = null; /* will be skipped */ }
731
;
732
 
733
trait_adaptations:
734
      ';'                                                   { $$ = array(); }
735
    | '{' trait_adaptation_list '}'                         { $$ = $2; }
736
;
737
 
738
trait_adaptation_list:
739
      /* empty */                                           { init(); }
740
    | trait_adaptation_list trait_adaptation                { push($1, $2); }
741
;
742
 
743
trait_adaptation:
744
      trait_method_reference_fully_qualified T_INSTEADOF class_name_list ';'
745
          { $$ = Stmt\TraitUseAdaptation\Precedence[$1[0], $1[1], $3]; }
746
    | trait_method_reference T_AS member_modifier identifier_maybe_reserved ';'
747
          { $$ = Stmt\TraitUseAdaptation\Alias[$1[0], $1[1], $3, $4]; }
748
    | trait_method_reference T_AS member_modifier ';'
749
          { $$ = Stmt\TraitUseAdaptation\Alias[$1[0], $1[1], $3, null]; }
750
    | trait_method_reference T_AS identifier_not_reserved ';'
751
          { $$ = Stmt\TraitUseAdaptation\Alias[$1[0], $1[1], null, $3]; }
752
    | trait_method_reference T_AS reserved_non_modifiers_identifier ';'
753
          { $$ = Stmt\TraitUseAdaptation\Alias[$1[0], $1[1], null, $3]; }
754
;
755
 
756
trait_method_reference_fully_qualified:
757
      name T_PAAMAYIM_NEKUDOTAYIM identifier_maybe_reserved { $$ = array($1, $3); }
758
;
759
trait_method_reference:
760
      trait_method_reference_fully_qualified                { $$ = $1; }
761
    | identifier_maybe_reserved                             { $$ = array(null, $1); }
762
;
763
 
764
method_body:
765
      ';' /* abstract method */                             { $$ = null; }
766
    | block_or_error                                        { $$ = $1; }
767
;
768
 
769
variable_modifiers:
770
      non_empty_member_modifiers                            { $$ = $1; }
771
    | T_VAR                                                 { $$ = 0; }
772
;
773
 
774
method_modifiers:
775
      /* empty */                                           { $$ = 0; }
776
    | non_empty_member_modifiers                            { $$ = $1; }
777
;
778
 
779
non_empty_member_modifiers:
780
      member_modifier                                       { $$ = $1; }
781
    | non_empty_member_modifiers member_modifier            { $this->checkModifier($1, $2, #2); $$ = $1 | $2; }
782
;
783
 
784
member_modifier:
785
      T_PUBLIC                                              { $$ = Stmt\Class_::MODIFIER_PUBLIC; }
786
    | T_PROTECTED                                           { $$ = Stmt\Class_::MODIFIER_PROTECTED; }
787
    | T_PRIVATE                                             { $$ = Stmt\Class_::MODIFIER_PRIVATE; }
788
    | T_STATIC                                              { $$ = Stmt\Class_::MODIFIER_STATIC; }
789
    | T_ABSTRACT                                            { $$ = Stmt\Class_::MODIFIER_ABSTRACT; }
790
    | T_FINAL                                               { $$ = Stmt\Class_::MODIFIER_FINAL; }
791
    | T_READONLY                                            { $$ = Stmt\Class_::MODIFIER_READONLY; }
792
;
793
 
794
property_declaration_list:
795
      non_empty_property_declaration_list no_comma          { $$ = $1; }
796
;
797
 
798
non_empty_property_declaration_list:
799
      property_declaration                                  { init($1); }
800
    | non_empty_property_declaration_list ',' property_declaration
801
          { push($1, $3); }
802
;
803
 
804
property_decl_name:
805
      T_VARIABLE                                            { $$ = Node\VarLikeIdentifier[parseVar($1)]; }
806
;
807
 
808
property_declaration:
809
      property_decl_name                                    { $$ = Stmt\PropertyProperty[$1, null]; }
810
    | property_decl_name '=' expr                           { $$ = Stmt\PropertyProperty[$1, $3]; }
811
;
812
 
813
expr_list_forbid_comma:
814
      non_empty_expr_list no_comma                          { $$ = $1; }
815
;
816
 
817
expr_list_allow_comma:
818
      non_empty_expr_list optional_comma                    { $$ = $1; }
819
;
820
 
821
non_empty_expr_list:
822
      non_empty_expr_list ',' expr                          { push($1, $3); }
823
    | expr                                                  { init($1); }
824
;
825
 
826
for_expr:
827
      /* empty */                                           { $$ = array(); }
828
    | expr_list_forbid_comma                                { $$ = $1; }
829
;
830
 
831
expr:
832
      variable                                              { $$ = $1; }
833
    | list_expr '=' expr                                    { $$ = Expr\Assign[$1, $3]; }
834
    | array_short_syntax '=' expr                           { $$ = Expr\Assign[$1, $3]; }
835
    | variable '=' expr                                     { $$ = Expr\Assign[$1, $3]; }
836
    | variable '=' ampersand variable                       { $$ = Expr\AssignRef[$1, $4]; }
837
    | new_expr                                              { $$ = $1; }
838
    | match                                                 { $$ = $1; }
839
    | T_CLONE expr                                          { $$ = Expr\Clone_[$2]; }
840
    | variable T_PLUS_EQUAL expr                            { $$ = Expr\AssignOp\Plus      [$1, $3]; }
841
    | variable T_MINUS_EQUAL expr                           { $$ = Expr\AssignOp\Minus     [$1, $3]; }
842
    | variable T_MUL_EQUAL expr                             { $$ = Expr\AssignOp\Mul       [$1, $3]; }
843
    | variable T_DIV_EQUAL expr                             { $$ = Expr\AssignOp\Div       [$1, $3]; }
844
    | variable T_CONCAT_EQUAL expr                          { $$ = Expr\AssignOp\Concat    [$1, $3]; }
845
    | variable T_MOD_EQUAL expr                             { $$ = Expr\AssignOp\Mod       [$1, $3]; }
846
    | variable T_AND_EQUAL expr                             { $$ = Expr\AssignOp\BitwiseAnd[$1, $3]; }
847
    | variable T_OR_EQUAL expr                              { $$ = Expr\AssignOp\BitwiseOr [$1, $3]; }
848
    | variable T_XOR_EQUAL expr                             { $$ = Expr\AssignOp\BitwiseXor[$1, $3]; }
849
    | variable T_SL_EQUAL expr                              { $$ = Expr\AssignOp\ShiftLeft [$1, $3]; }
850
    | variable T_SR_EQUAL expr                              { $$ = Expr\AssignOp\ShiftRight[$1, $3]; }
851
    | variable T_POW_EQUAL expr                             { $$ = Expr\AssignOp\Pow       [$1, $3]; }
852
    | variable T_COALESCE_EQUAL expr                        { $$ = Expr\AssignOp\Coalesce  [$1, $3]; }
853
    | variable T_INC                                        { $$ = Expr\PostInc[$1]; }
854
    | T_INC variable                                        { $$ = Expr\PreInc [$2]; }
855
    | variable T_DEC                                        { $$ = Expr\PostDec[$1]; }
856
    | T_DEC variable                                        { $$ = Expr\PreDec [$2]; }
857
    | expr T_BOOLEAN_OR expr                                { $$ = Expr\BinaryOp\BooleanOr [$1, $3]; }
858
    | expr T_BOOLEAN_AND expr                               { $$ = Expr\BinaryOp\BooleanAnd[$1, $3]; }
859
    | expr T_LOGICAL_OR expr                                { $$ = Expr\BinaryOp\LogicalOr [$1, $3]; }
860
    | expr T_LOGICAL_AND expr                               { $$ = Expr\BinaryOp\LogicalAnd[$1, $3]; }
861
    | expr T_LOGICAL_XOR expr                               { $$ = Expr\BinaryOp\LogicalXor[$1, $3]; }
862
    | expr '|' expr                                         { $$ = Expr\BinaryOp\BitwiseOr [$1, $3]; }
863
    | expr T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG expr   { $$ = Expr\BinaryOp\BitwiseAnd[$1, $3]; }
864
    | expr T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG expr       { $$ = Expr\BinaryOp\BitwiseAnd[$1, $3]; }
865
    | expr '^' expr                                         { $$ = Expr\BinaryOp\BitwiseXor[$1, $3]; }
866
    | expr '.' expr                                         { $$ = Expr\BinaryOp\Concat    [$1, $3]; }
867
    | expr '+' expr                                         { $$ = Expr\BinaryOp\Plus      [$1, $3]; }
868
    | expr '-' expr                                         { $$ = Expr\BinaryOp\Minus     [$1, $3]; }
869
    | expr '*' expr                                         { $$ = Expr\BinaryOp\Mul       [$1, $3]; }
870
    | expr '/' expr                                         { $$ = Expr\BinaryOp\Div       [$1, $3]; }
871
    | expr '%' expr                                         { $$ = Expr\BinaryOp\Mod       [$1, $3]; }
872
    | expr T_SL expr                                        { $$ = Expr\BinaryOp\ShiftLeft [$1, $3]; }
873
    | expr T_SR expr                                        { $$ = Expr\BinaryOp\ShiftRight[$1, $3]; }
874
    | expr T_POW expr                                       { $$ = Expr\BinaryOp\Pow       [$1, $3]; }
875
    | '+' expr %prec T_INC                                  { $$ = Expr\UnaryPlus [$2]; }
876
    | '-' expr %prec T_INC                                  { $$ = Expr\UnaryMinus[$2]; }
877
    | '!' expr                                              { $$ = Expr\BooleanNot[$2]; }
878
    | '~' expr                                              { $$ = Expr\BitwiseNot[$2]; }
879
    | expr T_IS_IDENTICAL expr                              { $$ = Expr\BinaryOp\Identical     [$1, $3]; }
880
    | expr T_IS_NOT_IDENTICAL expr                          { $$ = Expr\BinaryOp\NotIdentical  [$1, $3]; }
881
    | expr T_IS_EQUAL expr                                  { $$ = Expr\BinaryOp\Equal         [$1, $3]; }
882
    | expr T_IS_NOT_EQUAL expr                              { $$ = Expr\BinaryOp\NotEqual      [$1, $3]; }
883
    | expr T_SPACESHIP expr                                 { $$ = Expr\BinaryOp\Spaceship     [$1, $3]; }
884
    | expr '<' expr                                         { $$ = Expr\BinaryOp\Smaller       [$1, $3]; }
885
    | expr T_IS_SMALLER_OR_EQUAL expr                       { $$ = Expr\BinaryOp\SmallerOrEqual[$1, $3]; }
886
    | expr '>' expr                                         { $$ = Expr\BinaryOp\Greater       [$1, $3]; }
887
    | expr T_IS_GREATER_OR_EQUAL expr                       { $$ = Expr\BinaryOp\GreaterOrEqual[$1, $3]; }
888
    | expr T_INSTANCEOF class_name_reference                { $$ = Expr\Instanceof_[$1, $3]; }
889
    | '(' expr ')'                                          { $$ = $2; }
890
    | expr '?' expr ':' expr                                { $$ = Expr\Ternary[$1, $3,   $5]; }
891
    | expr '?' ':' expr                                     { $$ = Expr\Ternary[$1, null, $4]; }
892
    | expr T_COALESCE expr                                  { $$ = Expr\BinaryOp\Coalesce[$1, $3]; }
893
    | T_ISSET '(' expr_list_allow_comma ')'                 { $$ = Expr\Isset_[$3]; }
894
    | T_EMPTY '(' expr ')'                                  { $$ = Expr\Empty_[$3]; }
895
    | T_INCLUDE expr                                        { $$ = Expr\Include_[$2, Expr\Include_::TYPE_INCLUDE]; }
896
    | T_INCLUDE_ONCE expr                                   { $$ = Expr\Include_[$2, Expr\Include_::TYPE_INCLUDE_ONCE]; }
897
    | T_EVAL '(' expr ')'                                   { $$ = Expr\Eval_[$3]; }
898
    | T_REQUIRE expr                                        { $$ = Expr\Include_[$2, Expr\Include_::TYPE_REQUIRE]; }
899
    | T_REQUIRE_ONCE expr                                   { $$ = Expr\Include_[$2, Expr\Include_::TYPE_REQUIRE_ONCE]; }
900
    | T_INT_CAST expr                                       { $$ = Expr\Cast\Int_    [$2]; }
901
    | T_DOUBLE_CAST expr
902
          { $attrs = attributes();
903
            $attrs['kind'] = $this->getFloatCastKind($1);
904
            $$ = new Expr\Cast\Double($2, $attrs); }
905
    | T_STRING_CAST expr                                    { $$ = Expr\Cast\String_ [$2]; }
906
    | T_ARRAY_CAST expr                                     { $$ = Expr\Cast\Array_  [$2]; }
907
    | T_OBJECT_CAST expr                                    { $$ = Expr\Cast\Object_ [$2]; }
908
    | T_BOOL_CAST expr                                      { $$ = Expr\Cast\Bool_   [$2]; }
909
    | T_UNSET_CAST expr                                     { $$ = Expr\Cast\Unset_  [$2]; }
910
    | T_EXIT exit_expr
911
          { $attrs = attributes();
912
            $attrs['kind'] = strtolower($1) === 'exit' ? Expr\Exit_::KIND_EXIT : Expr\Exit_::KIND_DIE;
913
            $$ = new Expr\Exit_($2, $attrs); }
914
    | '@' expr                                              { $$ = Expr\ErrorSuppress[$2]; }
915
    | scalar                                                { $$ = $1; }
916
    | '`' backticks_expr '`'                                { $$ = Expr\ShellExec[$2]; }
917
    | T_PRINT expr                                          { $$ = Expr\Print_[$2]; }
918
    | T_YIELD                                               { $$ = Expr\Yield_[null, null]; }
919
    | T_YIELD expr                                          { $$ = Expr\Yield_[$2, null]; }
920
    | T_YIELD expr T_DOUBLE_ARROW expr                      { $$ = Expr\Yield_[$4, $2]; }
921
    | T_YIELD_FROM expr                                     { $$ = Expr\YieldFrom[$2]; }
922
    | T_THROW expr                                          { $$ = Expr\Throw_[$2]; }
923
 
924
    | T_FN optional_ref '(' parameter_list ')' optional_return_type T_DOUBLE_ARROW expr %prec T_THROW
925
          { $$ = Expr\ArrowFunction[['static' => false, 'byRef' => $2, 'params' => $4, 'returnType' => $6, 'expr' => $8, 'attrGroups' => []]]; }
926
    | T_STATIC T_FN optional_ref '(' parameter_list ')' optional_return_type T_DOUBLE_ARROW expr %prec T_THROW
927
          { $$ = Expr\ArrowFunction[['static' => true, 'byRef' => $3, 'params' => $5, 'returnType' => $7, 'expr' => $9, 'attrGroups' => []]]; }
928
    | T_FUNCTION optional_ref '(' parameter_list ')' lexical_vars optional_return_type block_or_error
929
          { $$ = Expr\Closure[['static' => false, 'byRef' => $2, 'params' => $4, 'uses' => $6, 'returnType' => $7, 'stmts' => $8, 'attrGroups' => []]]; }
930
    | T_STATIC T_FUNCTION optional_ref '(' parameter_list ')' lexical_vars optional_return_type       block_or_error
931
          { $$ = Expr\Closure[['static' => true, 'byRef' => $3, 'params' => $5, 'uses' => $7, 'returnType' => $8, 'stmts' => $9, 'attrGroups' => []]]; }
932
 
933
    | attributes T_FN optional_ref '(' parameter_list ')' optional_return_type T_DOUBLE_ARROW expr %prec T_THROW
934
          { $$ = Expr\ArrowFunction[['static' => false, 'byRef' => $3, 'params' => $5, 'returnType' => $7, 'expr' => $9, 'attrGroups' => $1]]; }
935
    | attributes T_STATIC T_FN optional_ref '(' parameter_list ')' optional_return_type T_DOUBLE_ARROW expr %prec T_THROW
936
          { $$ = Expr\ArrowFunction[['static' => true, 'byRef' => $4, 'params' => $6, 'returnType' => $8, 'expr' => $10, 'attrGroups' => $1]]; }
937
    | attributes T_FUNCTION optional_ref '(' parameter_list ')' lexical_vars optional_return_type block_or_error
938
          { $$ = Expr\Closure[['static' => false, 'byRef' => $3, 'params' => $5, 'uses' => $7, 'returnType' => $8, 'stmts' => $9, 'attrGroups' => $1]]; }
939
    | attributes T_STATIC T_FUNCTION optional_ref '(' parameter_list ')' lexical_vars optional_return_type       block_or_error
940
          { $$ = Expr\Closure[['static' => true, 'byRef' => $4, 'params' => $6, 'uses' => $8, 'returnType' => $9, 'stmts' => $10, 'attrGroups' => $1]]; }
941
;
942
 
943
anonymous_class:
944
      optional_attributes T_CLASS ctor_arguments extends_from implements_list '{' class_statement_list '}'
945
          { $$ = array(Stmt\Class_[null, ['type' => 0, 'extends' => $4, 'implements' => $5, 'stmts' => $7, 'attrGroups' => $1]], $3);
946
            $this->checkClass($$[0], -1); }
947
;
948
 
949
new_expr:
950
      T_NEW class_name_reference ctor_arguments             { $$ = Expr\New_[$2, $3]; }
951
    | T_NEW anonymous_class
952
          { list($class, $ctorArgs) = $2; $$ = Expr\New_[$class, $ctorArgs]; }
953
;
954
 
955
lexical_vars:
956
      /* empty */                                           { $$ = array(); }
957
    | T_USE '(' lexical_var_list ')'                        { $$ = $3; }
958
;
959
 
960
lexical_var_list:
961
      non_empty_lexical_var_list optional_comma             { $$ = $1; }
962
;
963
 
964
non_empty_lexical_var_list:
965
      lexical_var                                           { init($1); }
966
    | non_empty_lexical_var_list ',' lexical_var            { push($1, $3); }
967
;
968
 
969
lexical_var:
970
      optional_ref plain_variable                           { $$ = Expr\ClosureUse[$2, $1]; }
971
;
972
 
150 lars 973
name_readonly:
974
      T_READONLY                                            { $$ = Name[$1]; }
975
;
976
 
148 lars 977
function_call:
978
      name argument_list                                    { $$ = Expr\FuncCall[$1, $2]; }
150 lars 979
    | name_readonly argument_list                           { $$ = Expr\FuncCall[$1, $2]; }
148 lars 980
    | callable_expr argument_list                           { $$ = Expr\FuncCall[$1, $2]; }
981
    | class_name_or_var T_PAAMAYIM_NEKUDOTAYIM member_name argument_list
982
          { $$ = Expr\StaticCall[$1, $3, $4]; }
983
;
984
 
985
class_name:
986
      T_STATIC                                              { $$ = Name[$1]; }
987
    | name                                                  { $$ = $1; }
988
;
989
 
990
name:
991
      T_STRING                                              { $$ = Name[$1]; }
992
    | T_NAME_QUALIFIED                                      { $$ = Name[$1]; }
993
    | T_NAME_FULLY_QUALIFIED                                { $$ = Name\FullyQualified[substr($1, 1)]; }
994
    | T_NAME_RELATIVE                                       { $$ = Name\Relative[substr($1, 10)]; }
995
;
996
 
997
class_name_reference:
998
      class_name                                            { $$ = $1; }
999
    | new_variable                                          { $$ = $1; }
1000
    | '(' expr ')'                                          { $$ = $2; }
1001
    | error                                                 { $$ = Expr\Error[]; $this->errorState = 2; }
1002
;
1003
 
1004
class_name_or_var:
1005
      class_name                                            { $$ = $1; }
1006
    | fully_dereferencable                                  { $$ = $1; }
1007
;
1008
 
1009
exit_expr:
1010
      /* empty */                                           { $$ = null; }
1011
    | '(' optional_expr ')'                                 { $$ = $2; }
1012
;
1013
 
1014
backticks_expr:
1015
      /* empty */                                           { $$ = array(); }
1016
    | T_ENCAPSED_AND_WHITESPACE
1017
          { $$ = array(Scalar\EncapsedStringPart[Scalar\String_::parseEscapeSequences($1, '`')]); }
1018
    | encaps_list                                           { parseEncapsed($1, '`', true); $$ = $1; }
1019
;
1020
 
1021
ctor_arguments:
1022
      /* empty */                                           { $$ = array(); }
1023
    | argument_list                                         { $$ = $1; }
1024
;
1025
 
1026
constant:
1027
      name                                                  { $$ = Expr\ConstFetch[$1]; }
1028
    | T_LINE                                                { $$ = Scalar\MagicConst\Line[]; }
1029
    | T_FILE                                                { $$ = Scalar\MagicConst\File[]; }
1030
    | T_DIR                                                 { $$ = Scalar\MagicConst\Dir[]; }
1031
    | T_CLASS_C                                             { $$ = Scalar\MagicConst\Class_[]; }
1032
    | T_TRAIT_C                                             { $$ = Scalar\MagicConst\Trait_[]; }
1033
    | T_METHOD_C                                            { $$ = Scalar\MagicConst\Method[]; }
1034
    | T_FUNC_C                                              { $$ = Scalar\MagicConst\Function_[]; }
1035
    | T_NS_C                                                { $$ = Scalar\MagicConst\Namespace_[]; }
1036
;
1037
 
1038
class_constant:
1039
      class_name_or_var T_PAAMAYIM_NEKUDOTAYIM identifier_maybe_reserved
1040
          { $$ = Expr\ClassConstFetch[$1, $3]; }
1041
    /* We interpret an isolated FOO:: as an unfinished class constant fetch. It could also be
1042
       an unfinished static property fetch or unfinished scoped call. */
1043
    | class_name_or_var T_PAAMAYIM_NEKUDOTAYIM error
1044
          { $$ = Expr\ClassConstFetch[$1, new Expr\Error(stackAttributes(#3))]; $this->errorState = 2; }
1045
;
1046
 
1047
array_short_syntax:
1048
      '[' array_pair_list ']'
1049
          { $attrs = attributes(); $attrs['kind'] = Expr\Array_::KIND_SHORT;
1050
            $$ = new Expr\Array_($2, $attrs); }
1051
;
1052
 
1053
dereferencable_scalar:
1054
      T_ARRAY '(' array_pair_list ')'
1055
          { $attrs = attributes(); $attrs['kind'] = Expr\Array_::KIND_LONG;
1056
            $$ = new Expr\Array_($3, $attrs); }
1057
    | array_short_syntax                                    { $$ = $1; }
1058
    | T_CONSTANT_ENCAPSED_STRING                            { $$ = Scalar\String_::fromString($1, attributes()); }
1059
    | '"' encaps_list '"'
1060
          { $attrs = attributes(); $attrs['kind'] = Scalar\String_::KIND_DOUBLE_QUOTED;
1061
            parseEncapsed($2, '"', true); $$ = new Scalar\Encapsed($2, $attrs); }
1062
;
1063
 
1064
scalar:
1065
      T_LNUMBER                                             { $$ = $this->parseLNumber($1, attributes()); }
1066
    | T_DNUMBER                                             { $$ = Scalar\DNumber::fromString($1, attributes()); }
1067
    | dereferencable_scalar                                 { $$ = $1; }
1068
    | constant                                              { $$ = $1; }
1069
    | class_constant                                        { $$ = $1; }
1070
    | T_START_HEREDOC T_ENCAPSED_AND_WHITESPACE T_END_HEREDOC
1071
          { $$ = $this->parseDocString($1, $2, $3, attributes(), stackAttributes(#3), true); }
1072
    | T_START_HEREDOC T_END_HEREDOC
1073
          { $$ = $this->parseDocString($1, '', $2, attributes(), stackAttributes(#2), true); }
1074
    | T_START_HEREDOC encaps_list T_END_HEREDOC
1075
          { $$ = $this->parseDocString($1, $2, $3, attributes(), stackAttributes(#3), true); }
1076
;
1077
 
1078
optional_expr:
1079
      /* empty */                                           { $$ = null; }
1080
    | expr                                                  { $$ = $1; }
1081
;
1082
 
1083
fully_dereferencable:
1084
      variable                                              { $$ = $1; }
1085
    | '(' expr ')'                                          { $$ = $2; }
1086
    | dereferencable_scalar                                 { $$ = $1; }
1087
    | class_constant                                        { $$ = $1; }
1088
;
1089
 
1090
array_object_dereferencable:
1091
      fully_dereferencable                                  { $$ = $1; }
1092
    | constant                                              { $$ = $1; }
1093
;
1094
 
1095
callable_expr:
1096
      callable_variable                                     { $$ = $1; }
1097
    | '(' expr ')'                                          { $$ = $2; }
1098
    | dereferencable_scalar                                 { $$ = $1; }
1099
;
1100
 
1101
callable_variable:
1102
      simple_variable                                       { $$ = $1; }
1103
    | array_object_dereferencable '[' optional_expr ']'     { $$ = Expr\ArrayDimFetch[$1, $3]; }
1104
    | array_object_dereferencable '{' expr '}'              { $$ = Expr\ArrayDimFetch[$1, $3]; }
1105
    | function_call                                         { $$ = $1; }
1106
    | array_object_dereferencable T_OBJECT_OPERATOR property_name argument_list
1107
          { $$ = Expr\MethodCall[$1, $3, $4]; }
1108
    | array_object_dereferencable T_NULLSAFE_OBJECT_OPERATOR property_name argument_list
1109
          { $$ = Expr\NullsafeMethodCall[$1, $3, $4]; }
1110
;
1111
 
1112
optional_plain_variable:
1113
      /* empty */                                           { $$ = null; }
1114
    | plain_variable                                        { $$ = $1; }
1115
;
1116
 
1117
variable:
1118
      callable_variable                                     { $$ = $1; }
1119
    | static_member                                         { $$ = $1; }
1120
    | array_object_dereferencable T_OBJECT_OPERATOR property_name
1121
          { $$ = Expr\PropertyFetch[$1, $3]; }
1122
    | array_object_dereferencable T_NULLSAFE_OBJECT_OPERATOR property_name
1123
          { $$ = Expr\NullsafePropertyFetch[$1, $3]; }
1124
;
1125
 
1126
simple_variable:
1127
      plain_variable                                        { $$ = $1; }
1128
    | '$' '{' expr '}'                                      { $$ = Expr\Variable[$3]; }
1129
    | '$' simple_variable                                   { $$ = Expr\Variable[$2]; }
1130
    | '$' error                                             { $$ = Expr\Variable[Expr\Error[]]; $this->errorState = 2; }
1131
;
1132
 
1133
static_member_prop_name:
1134
      simple_variable
1135
          { $var = $1->name; $$ = \is_string($var) ? Node\VarLikeIdentifier[$var] : $var; }
1136
;
1137
 
1138
static_member:
1139
      class_name_or_var T_PAAMAYIM_NEKUDOTAYIM static_member_prop_name
1140
          { $$ = Expr\StaticPropertyFetch[$1, $3]; }
1141
;
1142
 
1143
new_variable:
1144
      simple_variable                                       { $$ = $1; }
1145
    | new_variable '[' optional_expr ']'                    { $$ = Expr\ArrayDimFetch[$1, $3]; }
1146
    | new_variable '{' expr '}'                             { $$ = Expr\ArrayDimFetch[$1, $3]; }
1147
    | new_variable T_OBJECT_OPERATOR property_name          { $$ = Expr\PropertyFetch[$1, $3]; }
1148
    | new_variable T_NULLSAFE_OBJECT_OPERATOR property_name { $$ = Expr\NullsafePropertyFetch[$1, $3]; }
1149
    | class_name T_PAAMAYIM_NEKUDOTAYIM static_member_prop_name
1150
          { $$ = Expr\StaticPropertyFetch[$1, $3]; }
1151
    | new_variable T_PAAMAYIM_NEKUDOTAYIM static_member_prop_name
1152
          { $$ = Expr\StaticPropertyFetch[$1, $3]; }
1153
;
1154
 
1155
member_name:
1156
      identifier_maybe_reserved                             { $$ = $1; }
1157
    | '{' expr '}'                                          { $$ = $2; }
1158
    | simple_variable                                       { $$ = $1; }
1159
;
1160
 
1161
property_name:
1162
      identifier_not_reserved                               { $$ = $1; }
1163
    | '{' expr '}'                                          { $$ = $2; }
1164
    | simple_variable                                       { $$ = $1; }
1165
    | error                                                 { $$ = Expr\Error[]; $this->errorState = 2; }
1166
;
1167
 
1168
list_expr:
1169
      T_LIST '(' inner_array_pair_list ')'                  { $$ = Expr\List_[$3]; }
1170
;
1171
 
1172
array_pair_list:
1173
      inner_array_pair_list
1174
          { $$ = $1; $end = count($$)-1; if ($$[$end] === null) array_pop($$); }
1175
;
1176
 
1177
comma_or_error:
1178
      ','
1179
    | error
1180
          { /* do nothing -- prevent default action of $$=$1. See #551. */ }
1181
;
1182
 
1183
inner_array_pair_list:
1184
      inner_array_pair_list comma_or_error array_pair       { push($1, $3); }
1185
    | array_pair                                            { init($1); }
1186
;
1187
 
1188
array_pair:
1189
      expr                                                  { $$ = Expr\ArrayItem[$1, null, false]; }
1190
    | ampersand variable                                    { $$ = Expr\ArrayItem[$2, null, true]; }
1191
    | list_expr                                             { $$ = Expr\ArrayItem[$1, null, false]; }
1192
    | expr T_DOUBLE_ARROW expr                              { $$ = Expr\ArrayItem[$3, $1,   false]; }
1193
    | expr T_DOUBLE_ARROW ampersand variable                { $$ = Expr\ArrayItem[$4, $1,   true]; }
1194
    | expr T_DOUBLE_ARROW list_expr                         { $$ = Expr\ArrayItem[$3, $1,   false]; }
1195
    | T_ELLIPSIS expr                                       { $$ = Expr\ArrayItem[$2, null, false, attributes(), true]; }
1196
    | /* empty */                                           { $$ = null; }
1197
;
1198
 
1199
encaps_list:
1200
      encaps_list encaps_var                                { push($1, $2); }
1201
    | encaps_list encaps_string_part                        { push($1, $2); }
1202
    | encaps_var                                            { init($1); }
1203
    | encaps_string_part encaps_var                         { init($1, $2); }
1204
;
1205
 
1206
encaps_string_part:
1207
      T_ENCAPSED_AND_WHITESPACE                             { $$ = Scalar\EncapsedStringPart[$1]; }
1208
;
1209
 
1210
encaps_str_varname:
1211
      T_STRING_VARNAME                                      { $$ = Expr\Variable[$1]; }
1212
;
1213
 
1214
encaps_var:
1215
      plain_variable                                        { $$ = $1; }
1216
    | plain_variable '[' encaps_var_offset ']'              { $$ = Expr\ArrayDimFetch[$1, $3]; }
1217
    | plain_variable T_OBJECT_OPERATOR identifier_not_reserved
1218
          { $$ = Expr\PropertyFetch[$1, $3]; }
1219
    | plain_variable T_NULLSAFE_OBJECT_OPERATOR identifier_not_reserved
1220
          { $$ = Expr\NullsafePropertyFetch[$1, $3]; }
1221
    | T_DOLLAR_OPEN_CURLY_BRACES expr '}'                   { $$ = Expr\Variable[$2]; }
1222
    | T_DOLLAR_OPEN_CURLY_BRACES T_STRING_VARNAME '}'       { $$ = Expr\Variable[$2]; }
1223
    | T_DOLLAR_OPEN_CURLY_BRACES encaps_str_varname '[' expr ']' '}'
1224
          { $$ = Expr\ArrayDimFetch[$2, $4]; }
1225
    | T_CURLY_OPEN variable '}'                             { $$ = $2; }
1226
;
1227
 
1228
encaps_var_offset:
1229
      T_STRING                                              { $$ = Scalar\String_[$1]; }
1230
    | T_NUM_STRING                                          { $$ = $this->parseNumString($1, attributes()); }
1231
    | '-' T_NUM_STRING                                      { $$ = $this->parseNumString('-' . $2, attributes()); }
1232
    | plain_variable                                        { $$ = $1; }
1233
;
1234
 
1235
%%