Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 150 | 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:
991 lars 521
     T_ELSEIF '(' expr ')' ':' inner_statement_list
522
         { $$ = Stmt\ElseIf_[$3, $6]; $this->fixupAlternativeElse($$); }
148 lars 523
;
524
 
525
else_single:
526
      /* empty */                                           { $$ = null; }
527
    | T_ELSE statement                                      { $$ = Stmt\Else_[toArray($2)]; }
528
;
529
 
530
new_else_single:
531
      /* empty */                                           { $$ = null; }
991 lars 532
    | T_ELSE ':' inner_statement_list
533
          { $$ = Stmt\Else_[$3]; $this->fixupAlternativeElse($$); }
148 lars 534
;
535
 
536
foreach_variable:
537
      variable                                              { $$ = array($1, false); }
538
    | ampersand variable                                    { $$ = array($2, true); }
539
    | list_expr                                             { $$ = array($1, false); }
540
    | array_short_syntax                                    { $$ = array($1, false); }
541
;
542
 
543
parameter_list:
544
      non_empty_parameter_list optional_comma               { $$ = $1; }
545
    | /* empty */                                           { $$ = array(); }
546
;
547
 
548
non_empty_parameter_list:
549
      parameter                                             { init($1); }
550
    | non_empty_parameter_list ',' parameter                { push($1, $3); }
551
;
552
 
553
optional_property_modifiers:
554
      /* empty */               { $$ = 0; }
555
    | optional_property_modifiers property_modifier
556
          { $this->checkModifier($1, $2, #2); $$ = $1 | $2; }
557
;
558
 
559
property_modifier:
560
      T_PUBLIC                  { $$ = Stmt\Class_::MODIFIER_PUBLIC; }
561
    | T_PROTECTED               { $$ = Stmt\Class_::MODIFIER_PROTECTED; }
562
    | T_PRIVATE                 { $$ = Stmt\Class_::MODIFIER_PRIVATE; }
563
    | T_READONLY                { $$ = Stmt\Class_::MODIFIER_READONLY; }
564
;
565
 
566
parameter:
567
      optional_attributes optional_property_modifiers optional_type_without_static
568
      optional_arg_ref optional_ellipsis plain_variable
569
          { $$ = new Node\Param($6, null, $3, $4, $5, attributes(), $2, $1);
570
            $this->checkParam($$); }
571
    | optional_attributes optional_property_modifiers optional_type_without_static
572
      optional_arg_ref optional_ellipsis plain_variable '=' expr
573
          { $$ = new Node\Param($6, $8, $3, $4, $5, attributes(), $2, $1);
574
            $this->checkParam($$); }
575
    | optional_attributes optional_property_modifiers optional_type_without_static
576
      optional_arg_ref optional_ellipsis error
577
          { $$ = new Node\Param(Expr\Error[], null, $3, $4, $5, attributes(), $2, $1); }
578
;
579
 
580
type_expr:
581
      type                                                  { $$ = $1; }
582
    | '?' type                                              { $$ = Node\NullableType[$2]; }
583
    | union_type                                            { $$ = Node\UnionType[$1]; }
584
    | intersection_type                                     { $$ = $1; }
585
;
586
 
587
type:
588
      type_without_static                                   { $$ = $1; }
589
    | T_STATIC                                              { $$ = Node\Name['static']; }
590
;
591
 
592
type_without_static:
593
      name                                                  { $$ = $this->handleBuiltinTypes($1); }
594
    | T_ARRAY                                               { $$ = Node\Identifier['array']; }
595
    | T_CALLABLE                                            { $$ = Node\Identifier['callable']; }
596
;
597
 
598
union_type_element:
599
                type { $$ = $1; }
600
        |        '(' intersection_type ')' { $$ = $2; }
601
;
602
 
603
union_type:
604
      union_type_element '|' union_type_element             { init($1, $3); }
605
    | union_type '|' union_type_element                     { push($1, $3); }
606
;
607
 
608
union_type_without_static_element:
609
                type_without_static { $$ = $1; }
610
        |        '(' intersection_type_without_static ')' { $$ = $2; }
611
;
612
 
613
union_type_without_static:
614
      union_type_without_static_element '|' union_type_without_static_element   { init($1, $3); }
615
    | union_type_without_static '|' union_type_without_static_element           { push($1, $3); }
616
;
617
 
618
intersection_type_list:
619
      type T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG type   { init($1, $3); }
620
    | intersection_type_list T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG type
621
          { push($1, $3); }
622
;
623
 
624
intersection_type:
625
      intersection_type_list { $$ = Node\IntersectionType[$1]; }
626
;
627
 
628
intersection_type_without_static_list:
629
      type_without_static T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG type_without_static
630
          { init($1, $3); }
631
    | intersection_type_without_static_list T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG type_without_static
632
          { push($1, $3); }
633
;
634
 
635
intersection_type_without_static:
636
      intersection_type_without_static_list { $$ = Node\IntersectionType[$1]; }
637
;
638
 
639
type_expr_without_static:
640
      type_without_static                                   { $$ = $1; }
641
    | '?' type_without_static                               { $$ = Node\NullableType[$2]; }
642
    | union_type_without_static                             { $$ = Node\UnionType[$1]; }
643
    | intersection_type_without_static                      { $$ = $1; }
644
;
645
 
646
optional_type_without_static:
647
      /* empty */                                           { $$ = null; }
648
    | type_expr_without_static                              { $$ = $1; }
649
;
650
 
651
optional_return_type:
652
      /* empty */                                           { $$ = null; }
653
    | ':' type_expr                                         { $$ = $2; }
654
    | ':' error                                             { $$ = null; }
655
;
656
 
657
argument_list:
658
      '(' ')'                                               { $$ = array(); }
659
    | '(' non_empty_argument_list optional_comma ')'        { $$ = $2; }
660
    | '(' variadic_placeholder ')'                          { init($2); }
661
;
662
 
663
variadic_placeholder:
664
      T_ELLIPSIS                                            { $$ = Node\VariadicPlaceholder[]; }
665
;
666
 
667
non_empty_argument_list:
668
      argument                                              { init($1); }
669
    | non_empty_argument_list ',' argument                  { push($1, $3); }
670
;
671
 
672
argument:
673
      expr                                                  { $$ = Node\Arg[$1, false, false]; }
674
    | ampersand variable                                    { $$ = Node\Arg[$2, true, false]; }
675
    | T_ELLIPSIS expr                                       { $$ = Node\Arg[$2, false, true]; }
676
    | identifier_maybe_reserved ':' expr
677
          { $$ = new Node\Arg($3, false, false, attributes(), $1); }
678
;
679
 
680
global_var_list:
681
      non_empty_global_var_list no_comma                    { $$ = $1; }
682
;
683
 
684
non_empty_global_var_list:
685
      non_empty_global_var_list ',' global_var              { push($1, $3); }
686
    | global_var                                            { init($1); }
687
;
688
 
689
global_var:
690
      simple_variable                                       { $$ = $1; }
691
;
692
 
693
static_var_list:
694
      non_empty_static_var_list no_comma                    { $$ = $1; }
695
;
696
 
697
non_empty_static_var_list:
698
      non_empty_static_var_list ',' static_var              { push($1, $3); }
699
    | static_var                                            { init($1); }
700
;
701
 
702
static_var:
703
      plain_variable                                        { $$ = Stmt\StaticVar[$1, null]; }
704
    | plain_variable '=' expr                               { $$ = Stmt\StaticVar[$1, $3]; }
705
;
706
 
707
class_statement_list_ex:
708
      class_statement_list_ex class_statement               { if ($2 !== null) { push($1, $2); } }
709
    | /* empty */                                           { init(); }
710
;
711
 
712
class_statement_list:
713
      class_statement_list_ex
714
          { makeZeroLengthNop($nop, $this->lookaheadStartAttributes);
715
            if ($nop !== null) { $1[] = $nop; } $$ = $1; }
716
;
717
 
718
class_statement:
719
      optional_attributes variable_modifiers optional_type_without_static property_declaration_list semi
720
          { $$ = new Stmt\Property($2, $4, attributes(), $3, $1);
721
            $this->checkProperty($$, #2); }
722
    | optional_attributes method_modifiers T_CONST class_const_list semi
723
          { $$ = new Stmt\ClassConst($4, $2, attributes(), $1);
724
            $this->checkClassConst($$, #2); }
725
    | optional_attributes method_modifiers T_FUNCTION optional_ref identifier_maybe_reserved '(' parameter_list ')'
726
      optional_return_type method_body
727
          { $$ = Stmt\ClassMethod[$5, ['type' => $2, 'byRef' => $4, 'params' => $7, 'returnType' => $9, 'stmts' => $10, 'attrGroups' => $1]];
728
            $this->checkClassMethod($$, #2); }
729
    | T_USE class_name_list trait_adaptations               { $$ = Stmt\TraitUse[$2, $3]; }
730
    | optional_attributes T_CASE identifier_maybe_reserved enum_case_expr semi
731
         { $$ = Stmt\EnumCase[$3, $4, $1]; }
732
    | error                                                 { $$ = null; /* will be skipped */ }
733
;
734
 
735
trait_adaptations:
736
      ';'                                                   { $$ = array(); }
737
    | '{' trait_adaptation_list '}'                         { $$ = $2; }
738
;
739
 
740
trait_adaptation_list:
741
      /* empty */                                           { init(); }
742
    | trait_adaptation_list trait_adaptation                { push($1, $2); }
743
;
744
 
745
trait_adaptation:
746
      trait_method_reference_fully_qualified T_INSTEADOF class_name_list ';'
747
          { $$ = Stmt\TraitUseAdaptation\Precedence[$1[0], $1[1], $3]; }
748
    | trait_method_reference T_AS member_modifier identifier_maybe_reserved ';'
749
          { $$ = Stmt\TraitUseAdaptation\Alias[$1[0], $1[1], $3, $4]; }
750
    | trait_method_reference T_AS member_modifier ';'
751
          { $$ = Stmt\TraitUseAdaptation\Alias[$1[0], $1[1], $3, null]; }
752
    | trait_method_reference T_AS identifier_not_reserved ';'
753
          { $$ = Stmt\TraitUseAdaptation\Alias[$1[0], $1[1], null, $3]; }
754
    | trait_method_reference T_AS reserved_non_modifiers_identifier ';'
755
          { $$ = Stmt\TraitUseAdaptation\Alias[$1[0], $1[1], null, $3]; }
756
;
757
 
758
trait_method_reference_fully_qualified:
759
      name T_PAAMAYIM_NEKUDOTAYIM identifier_maybe_reserved { $$ = array($1, $3); }
760
;
761
trait_method_reference:
762
      trait_method_reference_fully_qualified                { $$ = $1; }
763
    | identifier_maybe_reserved                             { $$ = array(null, $1); }
764
;
765
 
766
method_body:
767
      ';' /* abstract method */                             { $$ = null; }
768
    | block_or_error                                        { $$ = $1; }
769
;
770
 
771
variable_modifiers:
772
      non_empty_member_modifiers                            { $$ = $1; }
773
    | T_VAR                                                 { $$ = 0; }
774
;
775
 
776
method_modifiers:
777
      /* empty */                                           { $$ = 0; }
778
    | non_empty_member_modifiers                            { $$ = $1; }
779
;
780
 
781
non_empty_member_modifiers:
782
      member_modifier                                       { $$ = $1; }
783
    | non_empty_member_modifiers member_modifier            { $this->checkModifier($1, $2, #2); $$ = $1 | $2; }
784
;
785
 
786
member_modifier:
787
      T_PUBLIC                                              { $$ = Stmt\Class_::MODIFIER_PUBLIC; }
788
    | T_PROTECTED                                           { $$ = Stmt\Class_::MODIFIER_PROTECTED; }
789
    | T_PRIVATE                                             { $$ = Stmt\Class_::MODIFIER_PRIVATE; }
790
    | T_STATIC                                              { $$ = Stmt\Class_::MODIFIER_STATIC; }
791
    | T_ABSTRACT                                            { $$ = Stmt\Class_::MODIFIER_ABSTRACT; }
792
    | T_FINAL                                               { $$ = Stmt\Class_::MODIFIER_FINAL; }
793
    | T_READONLY                                            { $$ = Stmt\Class_::MODIFIER_READONLY; }
794
;
795
 
796
property_declaration_list:
797
      non_empty_property_declaration_list no_comma          { $$ = $1; }
798
;
799
 
800
non_empty_property_declaration_list:
801
      property_declaration                                  { init($1); }
802
    | non_empty_property_declaration_list ',' property_declaration
803
          { push($1, $3); }
804
;
805
 
806
property_decl_name:
807
      T_VARIABLE                                            { $$ = Node\VarLikeIdentifier[parseVar($1)]; }
808
;
809
 
810
property_declaration:
811
      property_decl_name                                    { $$ = Stmt\PropertyProperty[$1, null]; }
812
    | property_decl_name '=' expr                           { $$ = Stmt\PropertyProperty[$1, $3]; }
813
;
814
 
815
expr_list_forbid_comma:
816
      non_empty_expr_list no_comma                          { $$ = $1; }
817
;
818
 
819
expr_list_allow_comma:
820
      non_empty_expr_list optional_comma                    { $$ = $1; }
821
;
822
 
823
non_empty_expr_list:
824
      non_empty_expr_list ',' expr                          { push($1, $3); }
825
    | expr                                                  { init($1); }
826
;
827
 
828
for_expr:
829
      /* empty */                                           { $$ = array(); }
830
    | expr_list_forbid_comma                                { $$ = $1; }
831
;
832
 
833
expr:
834
      variable                                              { $$ = $1; }
835
    | list_expr '=' expr                                    { $$ = Expr\Assign[$1, $3]; }
836
    | array_short_syntax '=' expr                           { $$ = Expr\Assign[$1, $3]; }
837
    | variable '=' expr                                     { $$ = Expr\Assign[$1, $3]; }
838
    | variable '=' ampersand variable                       { $$ = Expr\AssignRef[$1, $4]; }
839
    | new_expr                                              { $$ = $1; }
840
    | match                                                 { $$ = $1; }
841
    | T_CLONE expr                                          { $$ = Expr\Clone_[$2]; }
842
    | variable T_PLUS_EQUAL expr                            { $$ = Expr\AssignOp\Plus      [$1, $3]; }
843
    | variable T_MINUS_EQUAL expr                           { $$ = Expr\AssignOp\Minus     [$1, $3]; }
844
    | variable T_MUL_EQUAL expr                             { $$ = Expr\AssignOp\Mul       [$1, $3]; }
845
    | variable T_DIV_EQUAL expr                             { $$ = Expr\AssignOp\Div       [$1, $3]; }
846
    | variable T_CONCAT_EQUAL expr                          { $$ = Expr\AssignOp\Concat    [$1, $3]; }
847
    | variable T_MOD_EQUAL expr                             { $$ = Expr\AssignOp\Mod       [$1, $3]; }
848
    | variable T_AND_EQUAL expr                             { $$ = Expr\AssignOp\BitwiseAnd[$1, $3]; }
849
    | variable T_OR_EQUAL expr                              { $$ = Expr\AssignOp\BitwiseOr [$1, $3]; }
850
    | variable T_XOR_EQUAL expr                             { $$ = Expr\AssignOp\BitwiseXor[$1, $3]; }
851
    | variable T_SL_EQUAL expr                              { $$ = Expr\AssignOp\ShiftLeft [$1, $3]; }
852
    | variable T_SR_EQUAL expr                              { $$ = Expr\AssignOp\ShiftRight[$1, $3]; }
853
    | variable T_POW_EQUAL expr                             { $$ = Expr\AssignOp\Pow       [$1, $3]; }
854
    | variable T_COALESCE_EQUAL expr                        { $$ = Expr\AssignOp\Coalesce  [$1, $3]; }
855
    | variable T_INC                                        { $$ = Expr\PostInc[$1]; }
856
    | T_INC variable                                        { $$ = Expr\PreInc [$2]; }
857
    | variable T_DEC                                        { $$ = Expr\PostDec[$1]; }
858
    | T_DEC variable                                        { $$ = Expr\PreDec [$2]; }
859
    | expr T_BOOLEAN_OR expr                                { $$ = Expr\BinaryOp\BooleanOr [$1, $3]; }
860
    | expr T_BOOLEAN_AND expr                               { $$ = Expr\BinaryOp\BooleanAnd[$1, $3]; }
861
    | expr T_LOGICAL_OR expr                                { $$ = Expr\BinaryOp\LogicalOr [$1, $3]; }
862
    | expr T_LOGICAL_AND expr                               { $$ = Expr\BinaryOp\LogicalAnd[$1, $3]; }
863
    | expr T_LOGICAL_XOR expr                               { $$ = Expr\BinaryOp\LogicalXor[$1, $3]; }
864
    | expr '|' expr                                         { $$ = Expr\BinaryOp\BitwiseOr [$1, $3]; }
865
    | expr T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG expr   { $$ = Expr\BinaryOp\BitwiseAnd[$1, $3]; }
866
    | expr T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG expr       { $$ = Expr\BinaryOp\BitwiseAnd[$1, $3]; }
867
    | expr '^' expr                                         { $$ = Expr\BinaryOp\BitwiseXor[$1, $3]; }
868
    | expr '.' expr                                         { $$ = Expr\BinaryOp\Concat    [$1, $3]; }
869
    | expr '+' expr                                         { $$ = Expr\BinaryOp\Plus      [$1, $3]; }
870
    | expr '-' expr                                         { $$ = Expr\BinaryOp\Minus     [$1, $3]; }
871
    | expr '*' expr                                         { $$ = Expr\BinaryOp\Mul       [$1, $3]; }
872
    | expr '/' expr                                         { $$ = Expr\BinaryOp\Div       [$1, $3]; }
873
    | expr '%' expr                                         { $$ = Expr\BinaryOp\Mod       [$1, $3]; }
874
    | expr T_SL expr                                        { $$ = Expr\BinaryOp\ShiftLeft [$1, $3]; }
875
    | expr T_SR expr                                        { $$ = Expr\BinaryOp\ShiftRight[$1, $3]; }
876
    | expr T_POW expr                                       { $$ = Expr\BinaryOp\Pow       [$1, $3]; }
877
    | '+' expr %prec T_INC                                  { $$ = Expr\UnaryPlus [$2]; }
878
    | '-' expr %prec T_INC                                  { $$ = Expr\UnaryMinus[$2]; }
879
    | '!' expr                                              { $$ = Expr\BooleanNot[$2]; }
880
    | '~' expr                                              { $$ = Expr\BitwiseNot[$2]; }
881
    | expr T_IS_IDENTICAL expr                              { $$ = Expr\BinaryOp\Identical     [$1, $3]; }
882
    | expr T_IS_NOT_IDENTICAL expr                          { $$ = Expr\BinaryOp\NotIdentical  [$1, $3]; }
883
    | expr T_IS_EQUAL expr                                  { $$ = Expr\BinaryOp\Equal         [$1, $3]; }
884
    | expr T_IS_NOT_EQUAL expr                              { $$ = Expr\BinaryOp\NotEqual      [$1, $3]; }
885
    | expr T_SPACESHIP expr                                 { $$ = Expr\BinaryOp\Spaceship     [$1, $3]; }
886
    | expr '<' expr                                         { $$ = Expr\BinaryOp\Smaller       [$1, $3]; }
887
    | expr T_IS_SMALLER_OR_EQUAL expr                       { $$ = Expr\BinaryOp\SmallerOrEqual[$1, $3]; }
888
    | expr '>' expr                                         { $$ = Expr\BinaryOp\Greater       [$1, $3]; }
889
    | expr T_IS_GREATER_OR_EQUAL expr                       { $$ = Expr\BinaryOp\GreaterOrEqual[$1, $3]; }
890
    | expr T_INSTANCEOF class_name_reference                { $$ = Expr\Instanceof_[$1, $3]; }
891
    | '(' expr ')'                                          { $$ = $2; }
892
    | expr '?' expr ':' expr                                { $$ = Expr\Ternary[$1, $3,   $5]; }
893
    | expr '?' ':' expr                                     { $$ = Expr\Ternary[$1, null, $4]; }
894
    | expr T_COALESCE expr                                  { $$ = Expr\BinaryOp\Coalesce[$1, $3]; }
895
    | T_ISSET '(' expr_list_allow_comma ')'                 { $$ = Expr\Isset_[$3]; }
896
    | T_EMPTY '(' expr ')'                                  { $$ = Expr\Empty_[$3]; }
897
    | T_INCLUDE expr                                        { $$ = Expr\Include_[$2, Expr\Include_::TYPE_INCLUDE]; }
898
    | T_INCLUDE_ONCE expr                                   { $$ = Expr\Include_[$2, Expr\Include_::TYPE_INCLUDE_ONCE]; }
899
    | T_EVAL '(' expr ')'                                   { $$ = Expr\Eval_[$3]; }
900
    | T_REQUIRE expr                                        { $$ = Expr\Include_[$2, Expr\Include_::TYPE_REQUIRE]; }
901
    | T_REQUIRE_ONCE expr                                   { $$ = Expr\Include_[$2, Expr\Include_::TYPE_REQUIRE_ONCE]; }
902
    | T_INT_CAST expr                                       { $$ = Expr\Cast\Int_    [$2]; }
903
    | T_DOUBLE_CAST expr
904
          { $attrs = attributes();
905
            $attrs['kind'] = $this->getFloatCastKind($1);
906
            $$ = new Expr\Cast\Double($2, $attrs); }
907
    | T_STRING_CAST expr                                    { $$ = Expr\Cast\String_ [$2]; }
908
    | T_ARRAY_CAST expr                                     { $$ = Expr\Cast\Array_  [$2]; }
909
    | T_OBJECT_CAST expr                                    { $$ = Expr\Cast\Object_ [$2]; }
910
    | T_BOOL_CAST expr                                      { $$ = Expr\Cast\Bool_   [$2]; }
911
    | T_UNSET_CAST expr                                     { $$ = Expr\Cast\Unset_  [$2]; }
912
    | T_EXIT exit_expr
913
          { $attrs = attributes();
914
            $attrs['kind'] = strtolower($1) === 'exit' ? Expr\Exit_::KIND_EXIT : Expr\Exit_::KIND_DIE;
915
            $$ = new Expr\Exit_($2, $attrs); }
916
    | '@' expr                                              { $$ = Expr\ErrorSuppress[$2]; }
917
    | scalar                                                { $$ = $1; }
918
    | '`' backticks_expr '`'                                { $$ = Expr\ShellExec[$2]; }
919
    | T_PRINT expr                                          { $$ = Expr\Print_[$2]; }
920
    | T_YIELD                                               { $$ = Expr\Yield_[null, null]; }
921
    | T_YIELD expr                                          { $$ = Expr\Yield_[$2, null]; }
922
    | T_YIELD expr T_DOUBLE_ARROW expr                      { $$ = Expr\Yield_[$4, $2]; }
923
    | T_YIELD_FROM expr                                     { $$ = Expr\YieldFrom[$2]; }
924
    | T_THROW expr                                          { $$ = Expr\Throw_[$2]; }
925
 
926
    | T_FN optional_ref '(' parameter_list ')' optional_return_type T_DOUBLE_ARROW expr %prec T_THROW
927
          { $$ = Expr\ArrowFunction[['static' => false, 'byRef' => $2, 'params' => $4, 'returnType' => $6, 'expr' => $8, 'attrGroups' => []]]; }
928
    | T_STATIC T_FN optional_ref '(' parameter_list ')' optional_return_type T_DOUBLE_ARROW expr %prec T_THROW
929
          { $$ = Expr\ArrowFunction[['static' => true, 'byRef' => $3, 'params' => $5, 'returnType' => $7, 'expr' => $9, 'attrGroups' => []]]; }
930
    | T_FUNCTION optional_ref '(' parameter_list ')' lexical_vars optional_return_type block_or_error
931
          { $$ = Expr\Closure[['static' => false, 'byRef' => $2, 'params' => $4, 'uses' => $6, 'returnType' => $7, 'stmts' => $8, 'attrGroups' => []]]; }
932
    | T_STATIC T_FUNCTION optional_ref '(' parameter_list ')' lexical_vars optional_return_type       block_or_error
933
          { $$ = Expr\Closure[['static' => true, 'byRef' => $3, 'params' => $5, 'uses' => $7, 'returnType' => $8, 'stmts' => $9, 'attrGroups' => []]]; }
934
 
935
    | attributes T_FN optional_ref '(' parameter_list ')' optional_return_type T_DOUBLE_ARROW expr %prec T_THROW
936
          { $$ = Expr\ArrowFunction[['static' => false, 'byRef' => $3, 'params' => $5, 'returnType' => $7, 'expr' => $9, 'attrGroups' => $1]]; }
937
    | attributes T_STATIC T_FN optional_ref '(' parameter_list ')' optional_return_type T_DOUBLE_ARROW expr %prec T_THROW
938
          { $$ = Expr\ArrowFunction[['static' => true, 'byRef' => $4, 'params' => $6, 'returnType' => $8, 'expr' => $10, 'attrGroups' => $1]]; }
939
    | attributes T_FUNCTION optional_ref '(' parameter_list ')' lexical_vars optional_return_type block_or_error
940
          { $$ = Expr\Closure[['static' => false, 'byRef' => $3, 'params' => $5, 'uses' => $7, 'returnType' => $8, 'stmts' => $9, 'attrGroups' => $1]]; }
941
    | attributes T_STATIC T_FUNCTION optional_ref '(' parameter_list ')' lexical_vars optional_return_type       block_or_error
942
          { $$ = Expr\Closure[['static' => true, 'byRef' => $4, 'params' => $6, 'uses' => $8, 'returnType' => $9, 'stmts' => $10, 'attrGroups' => $1]]; }
943
;
944
 
945
anonymous_class:
946
      optional_attributes T_CLASS ctor_arguments extends_from implements_list '{' class_statement_list '}'
947
          { $$ = array(Stmt\Class_[null, ['type' => 0, 'extends' => $4, 'implements' => $5, 'stmts' => $7, 'attrGroups' => $1]], $3);
948
            $this->checkClass($$[0], -1); }
949
;
950
 
951
new_expr:
952
      T_NEW class_name_reference ctor_arguments             { $$ = Expr\New_[$2, $3]; }
953
    | T_NEW anonymous_class
954
          { list($class, $ctorArgs) = $2; $$ = Expr\New_[$class, $ctorArgs]; }
955
;
956
 
957
lexical_vars:
958
      /* empty */                                           { $$ = array(); }
959
    | T_USE '(' lexical_var_list ')'                        { $$ = $3; }
960
;
961
 
962
lexical_var_list:
963
      non_empty_lexical_var_list optional_comma             { $$ = $1; }
964
;
965
 
966
non_empty_lexical_var_list:
967
      lexical_var                                           { init($1); }
968
    | non_empty_lexical_var_list ',' lexical_var            { push($1, $3); }
969
;
970
 
971
lexical_var:
972
      optional_ref plain_variable                           { $$ = Expr\ClosureUse[$2, $1]; }
973
;
974
 
150 lars 975
name_readonly:
976
      T_READONLY                                            { $$ = Name[$1]; }
977
;
978
 
148 lars 979
function_call:
980
      name argument_list                                    { $$ = Expr\FuncCall[$1, $2]; }
150 lars 981
    | name_readonly argument_list                           { $$ = Expr\FuncCall[$1, $2]; }
148 lars 982
    | callable_expr argument_list                           { $$ = Expr\FuncCall[$1, $2]; }
983
    | class_name_or_var T_PAAMAYIM_NEKUDOTAYIM member_name argument_list
984
          { $$ = Expr\StaticCall[$1, $3, $4]; }
985
;
986
 
987
class_name:
988
      T_STATIC                                              { $$ = Name[$1]; }
989
    | name                                                  { $$ = $1; }
990
;
991
 
992
name:
993
      T_STRING                                              { $$ = Name[$1]; }
994
    | T_NAME_QUALIFIED                                      { $$ = Name[$1]; }
995
    | T_NAME_FULLY_QUALIFIED                                { $$ = Name\FullyQualified[substr($1, 1)]; }
996
    | T_NAME_RELATIVE                                       { $$ = Name\Relative[substr($1, 10)]; }
997
;
998
 
999
class_name_reference:
1000
      class_name                                            { $$ = $1; }
1001
    | new_variable                                          { $$ = $1; }
1002
    | '(' expr ')'                                          { $$ = $2; }
1003
    | error                                                 { $$ = Expr\Error[]; $this->errorState = 2; }
1004
;
1005
 
1006
class_name_or_var:
1007
      class_name                                            { $$ = $1; }
1008
    | fully_dereferencable                                  { $$ = $1; }
1009
;
1010
 
1011
exit_expr:
1012
      /* empty */                                           { $$ = null; }
1013
    | '(' optional_expr ')'                                 { $$ = $2; }
1014
;
1015
 
1016
backticks_expr:
1017
      /* empty */                                           { $$ = array(); }
1018
    | T_ENCAPSED_AND_WHITESPACE
1019
          { $$ = array(Scalar\EncapsedStringPart[Scalar\String_::parseEscapeSequences($1, '`')]); }
1020
    | encaps_list                                           { parseEncapsed($1, '`', true); $$ = $1; }
1021
;
1022
 
1023
ctor_arguments:
1024
      /* empty */                                           { $$ = array(); }
1025
    | argument_list                                         { $$ = $1; }
1026
;
1027
 
1028
constant:
1029
      name                                                  { $$ = Expr\ConstFetch[$1]; }
1030
    | T_LINE                                                { $$ = Scalar\MagicConst\Line[]; }
1031
    | T_FILE                                                { $$ = Scalar\MagicConst\File[]; }
1032
    | T_DIR                                                 { $$ = Scalar\MagicConst\Dir[]; }
1033
    | T_CLASS_C                                             { $$ = Scalar\MagicConst\Class_[]; }
1034
    | T_TRAIT_C                                             { $$ = Scalar\MagicConst\Trait_[]; }
1035
    | T_METHOD_C                                            { $$ = Scalar\MagicConst\Method[]; }
1036
    | T_FUNC_C                                              { $$ = Scalar\MagicConst\Function_[]; }
1037
    | T_NS_C                                                { $$ = Scalar\MagicConst\Namespace_[]; }
1038
;
1039
 
1040
class_constant:
1041
      class_name_or_var T_PAAMAYIM_NEKUDOTAYIM identifier_maybe_reserved
1042
          { $$ = Expr\ClassConstFetch[$1, $3]; }
1043
    /* We interpret an isolated FOO:: as an unfinished class constant fetch. It could also be
1044
       an unfinished static property fetch or unfinished scoped call. */
1045
    | class_name_or_var T_PAAMAYIM_NEKUDOTAYIM error
1046
          { $$ = Expr\ClassConstFetch[$1, new Expr\Error(stackAttributes(#3))]; $this->errorState = 2; }
1047
;
1048
 
1049
array_short_syntax:
1050
      '[' array_pair_list ']'
1051
          { $attrs = attributes(); $attrs['kind'] = Expr\Array_::KIND_SHORT;
1052
            $$ = new Expr\Array_($2, $attrs); }
1053
;
1054
 
1055
dereferencable_scalar:
1056
      T_ARRAY '(' array_pair_list ')'
1057
          { $attrs = attributes(); $attrs['kind'] = Expr\Array_::KIND_LONG;
1058
            $$ = new Expr\Array_($3, $attrs); }
1059
    | array_short_syntax                                    { $$ = $1; }
1060
    | T_CONSTANT_ENCAPSED_STRING                            { $$ = Scalar\String_::fromString($1, attributes()); }
1061
    | '"' encaps_list '"'
1062
          { $attrs = attributes(); $attrs['kind'] = Scalar\String_::KIND_DOUBLE_QUOTED;
1063
            parseEncapsed($2, '"', true); $$ = new Scalar\Encapsed($2, $attrs); }
1064
;
1065
 
1066
scalar:
1067
      T_LNUMBER                                             { $$ = $this->parseLNumber($1, attributes()); }
1068
    | T_DNUMBER                                             { $$ = Scalar\DNumber::fromString($1, attributes()); }
1069
    | dereferencable_scalar                                 { $$ = $1; }
1070
    | constant                                              { $$ = $1; }
1071
    | class_constant                                        { $$ = $1; }
1072
    | T_START_HEREDOC T_ENCAPSED_AND_WHITESPACE T_END_HEREDOC
1073
          { $$ = $this->parseDocString($1, $2, $3, attributes(), stackAttributes(#3), true); }
1074
    | T_START_HEREDOC T_END_HEREDOC
1075
          { $$ = $this->parseDocString($1, '', $2, attributes(), stackAttributes(#2), true); }
1076
    | T_START_HEREDOC encaps_list T_END_HEREDOC
1077
          { $$ = $this->parseDocString($1, $2, $3, attributes(), stackAttributes(#3), true); }
1078
;
1079
 
1080
optional_expr:
1081
      /* empty */                                           { $$ = null; }
1082
    | expr                                                  { $$ = $1; }
1083
;
1084
 
1085
fully_dereferencable:
1086
      variable                                              { $$ = $1; }
1087
    | '(' expr ')'                                          { $$ = $2; }
1088
    | dereferencable_scalar                                 { $$ = $1; }
1089
    | class_constant                                        { $$ = $1; }
1090
;
1091
 
1092
array_object_dereferencable:
1093
      fully_dereferencable                                  { $$ = $1; }
1094
    | constant                                              { $$ = $1; }
1095
;
1096
 
1097
callable_expr:
1098
      callable_variable                                     { $$ = $1; }
1099
    | '(' expr ')'                                          { $$ = $2; }
1100
    | dereferencable_scalar                                 { $$ = $1; }
1101
;
1102
 
1103
callable_variable:
1104
      simple_variable                                       { $$ = $1; }
1105
    | array_object_dereferencable '[' optional_expr ']'     { $$ = Expr\ArrayDimFetch[$1, $3]; }
1106
    | array_object_dereferencable '{' expr '}'              { $$ = Expr\ArrayDimFetch[$1, $3]; }
1107
    | function_call                                         { $$ = $1; }
1108
    | array_object_dereferencable T_OBJECT_OPERATOR property_name argument_list
1109
          { $$ = Expr\MethodCall[$1, $3, $4]; }
1110
    | array_object_dereferencable T_NULLSAFE_OBJECT_OPERATOR property_name argument_list
1111
          { $$ = Expr\NullsafeMethodCall[$1, $3, $4]; }
1112
;
1113
 
1114
optional_plain_variable:
1115
      /* empty */                                           { $$ = null; }
1116
    | plain_variable                                        { $$ = $1; }
1117
;
1118
 
1119
variable:
1120
      callable_variable                                     { $$ = $1; }
1121
    | static_member                                         { $$ = $1; }
1122
    | array_object_dereferencable T_OBJECT_OPERATOR property_name
1123
          { $$ = Expr\PropertyFetch[$1, $3]; }
1124
    | array_object_dereferencable T_NULLSAFE_OBJECT_OPERATOR property_name
1125
          { $$ = Expr\NullsafePropertyFetch[$1, $3]; }
1126
;
1127
 
1128
simple_variable:
1129
      plain_variable                                        { $$ = $1; }
1130
    | '$' '{' expr '}'                                      { $$ = Expr\Variable[$3]; }
1131
    | '$' simple_variable                                   { $$ = Expr\Variable[$2]; }
1132
    | '$' error                                             { $$ = Expr\Variable[Expr\Error[]]; $this->errorState = 2; }
1133
;
1134
 
1135
static_member_prop_name:
1136
      simple_variable
1137
          { $var = $1->name; $$ = \is_string($var) ? Node\VarLikeIdentifier[$var] : $var; }
1138
;
1139
 
1140
static_member:
1141
      class_name_or_var T_PAAMAYIM_NEKUDOTAYIM static_member_prop_name
1142
          { $$ = Expr\StaticPropertyFetch[$1, $3]; }
1143
;
1144
 
1145
new_variable:
1146
      simple_variable                                       { $$ = $1; }
1147
    | new_variable '[' optional_expr ']'                    { $$ = Expr\ArrayDimFetch[$1, $3]; }
1148
    | new_variable '{' expr '}'                             { $$ = Expr\ArrayDimFetch[$1, $3]; }
1149
    | new_variable T_OBJECT_OPERATOR property_name          { $$ = Expr\PropertyFetch[$1, $3]; }
1150
    | new_variable T_NULLSAFE_OBJECT_OPERATOR property_name { $$ = Expr\NullsafePropertyFetch[$1, $3]; }
1151
    | class_name T_PAAMAYIM_NEKUDOTAYIM static_member_prop_name
1152
          { $$ = Expr\StaticPropertyFetch[$1, $3]; }
1153
    | new_variable T_PAAMAYIM_NEKUDOTAYIM static_member_prop_name
1154
          { $$ = Expr\StaticPropertyFetch[$1, $3]; }
1155
;
1156
 
1157
member_name:
1158
      identifier_maybe_reserved                             { $$ = $1; }
1159
    | '{' expr '}'                                          { $$ = $2; }
1160
    | simple_variable                                       { $$ = $1; }
1161
;
1162
 
1163
property_name:
1164
      identifier_not_reserved                               { $$ = $1; }
1165
    | '{' expr '}'                                          { $$ = $2; }
1166
    | simple_variable                                       { $$ = $1; }
1167
    | error                                                 { $$ = Expr\Error[]; $this->errorState = 2; }
1168
;
1169
 
1170
list_expr:
1171
      T_LIST '(' inner_array_pair_list ')'                  { $$ = Expr\List_[$3]; }
1172
;
1173
 
1174
array_pair_list:
1175
      inner_array_pair_list
1176
          { $$ = $1; $end = count($$)-1; if ($$[$end] === null) array_pop($$); }
1177
;
1178
 
1179
comma_or_error:
1180
      ','
1181
    | error
1182
          { /* do nothing -- prevent default action of $$=$1. See #551. */ }
1183
;
1184
 
1185
inner_array_pair_list:
1186
      inner_array_pair_list comma_or_error array_pair       { push($1, $3); }
1187
    | array_pair                                            { init($1); }
1188
;
1189
 
1190
array_pair:
1191
      expr                                                  { $$ = Expr\ArrayItem[$1, null, false]; }
1192
    | ampersand variable                                    { $$ = Expr\ArrayItem[$2, null, true]; }
1193
    | list_expr                                             { $$ = Expr\ArrayItem[$1, null, false]; }
1194
    | expr T_DOUBLE_ARROW expr                              { $$ = Expr\ArrayItem[$3, $1,   false]; }
1195
    | expr T_DOUBLE_ARROW ampersand variable                { $$ = Expr\ArrayItem[$4, $1,   true]; }
1196
    | expr T_DOUBLE_ARROW list_expr                         { $$ = Expr\ArrayItem[$3, $1,   false]; }
1197
    | T_ELLIPSIS expr                                       { $$ = Expr\ArrayItem[$2, null, false, attributes(), true]; }
1198
    | /* empty */                                           { $$ = null; }
1199
;
1200
 
1201
encaps_list:
1202
      encaps_list encaps_var                                { push($1, $2); }
1203
    | encaps_list encaps_string_part                        { push($1, $2); }
1204
    | encaps_var                                            { init($1); }
1205
    | encaps_string_part encaps_var                         { init($1, $2); }
1206
;
1207
 
1208
encaps_string_part:
1209
      T_ENCAPSED_AND_WHITESPACE                             { $$ = Scalar\EncapsedStringPart[$1]; }
1210
;
1211
 
1212
encaps_str_varname:
1213
      T_STRING_VARNAME                                      { $$ = Expr\Variable[$1]; }
1214
;
1215
 
1216
encaps_var:
1217
      plain_variable                                        { $$ = $1; }
1218
    | plain_variable '[' encaps_var_offset ']'              { $$ = Expr\ArrayDimFetch[$1, $3]; }
1219
    | plain_variable T_OBJECT_OPERATOR identifier_not_reserved
1220
          { $$ = Expr\PropertyFetch[$1, $3]; }
1221
    | plain_variable T_NULLSAFE_OBJECT_OPERATOR identifier_not_reserved
1222
          { $$ = Expr\NullsafePropertyFetch[$1, $3]; }
1223
    | T_DOLLAR_OPEN_CURLY_BRACES expr '}'                   { $$ = Expr\Variable[$2]; }
1224
    | T_DOLLAR_OPEN_CURLY_BRACES T_STRING_VARNAME '}'       { $$ = Expr\Variable[$2]; }
1225
    | T_DOLLAR_OPEN_CURLY_BRACES encaps_str_varname '[' expr ']' '}'
1226
          { $$ = Expr\ArrayDimFetch[$2, $4]; }
1227
    | T_CURLY_OPEN variable '}'                             { $$ = $2; }
1228
;
1229
 
1230
encaps_var_offset:
1231
      T_STRING                                              { $$ = Scalar\String_[$1]; }
1232
    | T_NUM_STRING                                          { $$ = $this->parseNumString($1, attributes()); }
1233
    | '-' T_NUM_STRING                                      { $$ = $this->parseNumString('-' . $2, attributes()); }
1234
    | plain_variable                                        { $$ = $1; }
1235
;
1236
 
1237
%%