1%{
2/*
3   +----------------------------------------------------------------------+
4   | Zend Engine                                                          |
5   +----------------------------------------------------------------------+
6   | Copyright (c) 1998-2015 Zend Technologies Ltd. (http://www.zend.com) |
7   +----------------------------------------------------------------------+
8   | This source file is subject to version 2.00 of the Zend license,     |
9   | that is bundled with this package in the file LICENSE, and is        |
10   | available through the world-wide-web at the following url:           |
11   | http://www.zend.com/license/2_00.txt.                                |
12   | If you did not receive a copy of the Zend license and are unable to  |
13   | obtain it through the world-wide-web, please send a note to          |
14   | license@zend.com so we can mail you a copy immediately.              |
15   +----------------------------------------------------------------------+
16   | Authors: Andi Gutmans <andi@zend.com>                                |
17   |          Zeev Suraski <zeev@zend.com>                                |
18   |          Nikita Popov <nikic@php.net>                                |
19   +----------------------------------------------------------------------+
20*/
21
22/* $Id$ */
23
24#include "zend_compile.h"
25#include "zend.h"
26#include "zend_list.h"
27#include "zend_globals.h"
28#include "zend_API.h"
29#include "zend_constants.h"
30#include "zend_language_scanner.h"
31
32#define YYSIZE_T size_t
33#define yytnamerr zend_yytnamerr
34static YYSIZE_T zend_yytnamerr(char*, const char*);
35
36#define YYERROR_VERBOSE
37#define YYSTYPE zend_parser_stack_elem
38
39#ifdef _MSC_VER
40#define YYMALLOC malloc
41#define YYFREE free
42#endif
43
44%}
45
46%pure_parser
47%expect 0
48
49%code requires {
50}
51
52%destructor { zend_ast_destroy($$); } <ast>
53%destructor { if ($$) zend_string_release($$); } <str>
54
55%left T_INCLUDE T_INCLUDE_ONCE T_EVAL T_REQUIRE T_REQUIRE_ONCE
56%left ','
57%left T_LOGICAL_OR
58%left T_LOGICAL_XOR
59%left T_LOGICAL_AND
60%right T_PRINT
61%right T_YIELD
62%right T_DOUBLE_ARROW
63%right T_YIELD_FROM
64%left '=' T_PLUS_EQUAL T_MINUS_EQUAL T_MUL_EQUAL T_DIV_EQUAL T_CONCAT_EQUAL T_MOD_EQUAL T_AND_EQUAL T_OR_EQUAL T_XOR_EQUAL T_SL_EQUAL T_SR_EQUAL T_POW_EQUAL
65%left '?' ':'
66%right T_COALESCE
67%left T_BOOLEAN_OR
68%left T_BOOLEAN_AND
69%left '|'
70%left '^'
71%left '&'
72%nonassoc T_IS_EQUAL T_IS_NOT_EQUAL T_IS_IDENTICAL T_IS_NOT_IDENTICAL T_SPACESHIP
73%nonassoc '<' T_IS_SMALLER_OR_EQUAL '>' T_IS_GREATER_OR_EQUAL
74%left T_SL T_SR
75%left '+' '-' '.'
76%left '*' '/' '%'
77%right '!'
78%nonassoc T_INSTANCEOF
79%right '~' T_INC T_DEC T_INT_CAST T_DOUBLE_CAST T_STRING_CAST T_ARRAY_CAST T_OBJECT_CAST T_BOOL_CAST T_UNSET_CAST '@'
80%right T_POW
81%right '['
82%nonassoc T_NEW T_CLONE
83%left T_NOELSE
84%left T_ELSEIF
85%left T_ELSE
86%left T_ENDIF
87%right T_STATIC T_ABSTRACT T_FINAL T_PRIVATE T_PROTECTED T_PUBLIC
88
89%token <ast> T_LNUMBER   "integer number (T_LNUMBER)"
90%token <ast> T_DNUMBER   "floating-point number (T_DNUMBER)"
91%token <ast> T_STRING    "identifier (T_STRING)"
92%token <ast> T_VARIABLE  "variable (T_VARIABLE)"
93%token <ast> T_INLINE_HTML
94%token <ast> T_ENCAPSED_AND_WHITESPACE  "quoted-string and whitespace (T_ENCAPSED_AND_WHITESPACE)"
95%token <ast> T_CONSTANT_ENCAPSED_STRING "quoted-string (T_CONSTANT_ENCAPSED_STRING)"
96%token <ast> T_STRING_VARNAME "variable name (T_STRING_VARNAME)"
97%token <ast> T_NUM_STRING "number (T_NUM_STRING)"
98
99%token END 0 "end of file"
100%token T_INCLUDE      "include (T_INCLUDE)"
101%token T_INCLUDE_ONCE "include_once (T_INCLUDE_ONCE)"
102%token T_EVAL         "eval (T_EVAL)"
103%token T_REQUIRE      "require (T_REQUIRE)"
104%token T_REQUIRE_ONCE "require_once (T_REQUIRE_ONCE)"
105%token T_LOGICAL_OR   "or (T_LOGICAL_OR)"
106%token T_LOGICAL_XOR  "xor (T_LOGICAL_XOR)"
107%token T_LOGICAL_AND  "and (T_LOGICAL_AND)"
108%token T_PRINT        "print (T_PRINT)"
109%token T_YIELD        "yield (T_YIELD)"
110%token T_YIELD_FROM   "yield from (T_YIELD_FROM)"
111%token T_PLUS_EQUAL   "+= (T_PLUS_EQUAL)"
112%token T_MINUS_EQUAL  "-= (T_MINUS_EQUAL)"
113%token T_MUL_EQUAL    "*= (T_MUL_EQUAL)"
114%token T_DIV_EQUAL    "/= (T_DIV_EQUAL)"
115%token T_CONCAT_EQUAL ".= (T_CONCAT_EQUAL)"
116%token T_MOD_EQUAL    "%= (T_MOD_EQUAL)"
117%token T_AND_EQUAL    "&= (T_AND_EQUAL)"
118%token T_OR_EQUAL     "|= (T_OR_EQUAL)"
119%token T_XOR_EQUAL    "^= (T_XOR_EQUAL)"
120%token T_SL_EQUAL     "<<= (T_SL_EQUAL)"
121%token T_SR_EQUAL     ">>= (T_SR_EQUAL)"
122%token T_BOOLEAN_OR   "|| (T_BOOLEAN_OR)"
123%token T_BOOLEAN_AND  "&& (T_BOOLEAN_AND)"
124%token T_IS_EQUAL     "== (T_IS_EQUAL)"
125%token T_IS_NOT_EQUAL "!= (T_IS_NOT_EQUAL)"
126%token T_IS_IDENTICAL "=== (T_IS_IDENTICAL)"
127%token T_IS_NOT_IDENTICAL "!== (T_IS_NOT_IDENTICAL)"
128%token T_IS_SMALLER_OR_EQUAL "<= (T_IS_SMALLER_OR_EQUAL)"
129%token T_IS_GREATER_OR_EQUAL ">= (T_IS_GREATER_OR_EQUAL)"
130%token T_SPACESHIP "<=> (T_SPACESHIP)"
131%token T_SL "<< (T_SL)"
132%token T_SR ">> (T_SR)"
133%token T_INSTANCEOF  "instanceof (T_INSTANCEOF)"
134%token T_INC "++ (T_INC)"
135%token T_DEC "-- (T_DEC)"
136%token T_INT_CAST    "(int) (T_INT_CAST)"
137%token T_DOUBLE_CAST "(double) (T_DOUBLE_CAST)"
138%token T_STRING_CAST "(string) (T_STRING_CAST)"
139%token T_ARRAY_CAST  "(array) (T_ARRAY_CAST)"
140%token T_OBJECT_CAST "(object) (T_OBJECT_CAST)"
141%token T_BOOL_CAST   "(bool) (T_BOOL_CAST)"
142%token T_UNSET_CAST  "(unset) (T_UNSET_CAST)"
143%token T_NEW       "new (T_NEW)"
144%token T_CLONE     "clone (T_CLONE)"
145%token T_EXIT      "exit (T_EXIT)"
146%token T_IF        "if (T_IF)"
147%token T_ELSEIF    "elseif (T_ELSEIF)"
148%token T_ELSE      "else (T_ELSE)"
149%token T_ENDIF     "endif (T_ENDIF)"
150%token T_ECHO       "echo (T_ECHO)"
151%token T_DO         "do (T_DO)"
152%token T_WHILE      "while (T_WHILE)"
153%token T_ENDWHILE   "endwhile (T_ENDWHILE)"
154%token T_FOR        "for (T_FOR)"
155%token T_ENDFOR     "endfor (T_ENDFOR)"
156%token T_FOREACH    "foreach (T_FOREACH)"
157%token T_ENDFOREACH "endforeach (T_ENDFOREACH)"
158%token T_DECLARE    "declare (T_DECLARE)"
159%token T_ENDDECLARE "enddeclare (T_ENDDECLARE)"
160%token T_AS         "as (T_AS)"
161%token T_SWITCH     "switch (T_SWITCH)"
162%token T_ENDSWITCH  "endswitch (T_ENDSWITCH)"
163%token T_CASE       "case (T_CASE)"
164%token T_DEFAULT    "default (T_DEFAULT)"
165%token T_BREAK      "break (T_BREAK)"
166%token T_CONTINUE   "continue (T_CONTINUE)"
167%token T_GOTO       "goto (T_GOTO)"
168%token T_FUNCTION   "function (T_FUNCTION)"
169%token T_CONST      "const (T_CONST)"
170%token T_RETURN     "return (T_RETURN)"
171%token T_TRY        "try (T_TRY)"
172%token T_CATCH      "catch (T_CATCH)"
173%token T_FINALLY    "finally (T_FINALLY)"
174%token T_THROW      "throw (T_THROW)"
175%token T_USE        "use (T_USE)"
176%token T_INSTEADOF  "insteadof (T_INSTEADOF)"
177%token T_GLOBAL     "global (T_GLOBAL)"
178%token T_STATIC     "static (T_STATIC)"
179%token T_ABSTRACT   "abstract (T_ABSTRACT)"
180%token T_FINAL      "final (T_FINAL)"
181%token T_PRIVATE    "private (T_PRIVATE)"
182%token T_PROTECTED  "protected (T_PROTECTED)"
183%token T_PUBLIC     "public (T_PUBLIC)"
184%token T_VAR        "var (T_VAR)"
185%token T_UNSET      "unset (T_UNSET)"
186%token T_ISSET      "isset (T_ISSET)"
187%token T_EMPTY      "empty (T_EMPTY)"
188%token T_HALT_COMPILER "__halt_compiler (T_HALT_COMPILER)"
189%token T_CLASS      "class (T_CLASS)"
190%token T_TRAIT      "trait (T_TRAIT)"
191%token T_INTERFACE  "interface (T_INTERFACE)"
192%token T_EXTENDS    "extends (T_EXTENDS)"
193%token T_IMPLEMENTS "implements (T_IMPLEMENTS)"
194%token T_OBJECT_OPERATOR "-> (T_OBJECT_OPERATOR)"
195%token T_DOUBLE_ARROW    "=> (T_DOUBLE_ARROW)"
196%token T_LIST            "list (T_LIST)"
197%token T_ARRAY           "array (T_ARRAY)"
198%token T_CALLABLE        "callable (T_CALLABLE)"
199%token T_LINE            "__LINE__ (T_LINE)"
200%token T_FILE            "__FILE__ (T_FILE)"
201%token T_DIR             "__DIR__ (T_DIR)"
202%token T_CLASS_C         "__CLASS__ (T_CLASS_C)"
203%token T_TRAIT_C         "__TRAIT__ (T_TRAIT_C)"
204%token T_METHOD_C        "__METHOD__ (T_METHOD_C)"
205%token T_FUNC_C          "__FUNCTION__ (T_FUNC_C)"
206%token T_COMMENT         "comment (T_COMMENT)"
207%token T_DOC_COMMENT     "doc comment (T_DOC_COMMENT)"
208%token T_OPEN_TAG        "open tag (T_OPEN_TAG)"
209%token T_OPEN_TAG_WITH_ECHO "open tag with echo (T_OPEN_TAG_WITH_ECHO)"
210%token T_CLOSE_TAG       "close tag (T_CLOSE_TAG)"
211%token T_WHITESPACE      "whitespace (T_WHITESPACE)"
212%token T_START_HEREDOC   "heredoc start (T_START_HEREDOC)"
213%token T_END_HEREDOC     "heredoc end (T_END_HEREDOC)"
214%token T_DOLLAR_OPEN_CURLY_BRACES "${ (T_DOLLAR_OPEN_CURLY_BRACES)"
215%token T_CURLY_OPEN      "{$ (T_CURLY_OPEN)"
216%token T_PAAMAYIM_NEKUDOTAYIM ":: (T_PAAMAYIM_NEKUDOTAYIM)"
217%token T_NAMESPACE       "namespace (T_NAMESPACE)"
218%token T_NS_C            "__NAMESPACE__ (T_NS_C)"
219%token T_NS_SEPARATOR    "\\ (T_NS_SEPARATOR)"
220%token T_ELLIPSIS        "... (T_ELLIPSIS)"
221%token T_COALESCE        "?? (T_COALESCE)"
222%token T_POW             "** (T_POW)"
223%token T_POW_EQUAL       "**= (T_POW_EQUAL)"
224
225/* Token used to force a parse error from the lexer */
226%token T_ERROR
227
228%type <ast> top_statement namespace_name name statement function_declaration_statement
229%type <ast> class_declaration_statement trait_declaration_statement
230%type <ast> interface_declaration_statement interface_extends_list
231%type <ast> group_use_declaration inline_use_declarations inline_use_declaration
232%type <ast> mixed_group_use_declaration use_declaration const_decl inner_statement
233%type <ast> expr optional_expr while_statement for_statement foreach_variable
234%type <ast> foreach_statement declare_statement finally_statement unset_variable variable
235%type <ast> extends_from parameter optional_type argument expr_without_variable global_var
236%type <ast> static_var class_statement trait_adaptation trait_precedence trait_alias
237%type <ast> absolute_trait_method_reference trait_method_reference property echo_expr
238%type <ast> new_expr anonymous_class class_name class_name_reference simple_variable
239%type <ast> internal_functions_in_yacc
240%type <ast> exit_expr scalar backticks_expr lexical_var function_call member_name property_name
241%type <ast> variable_class_name dereferencable_scalar constant dereferencable
242%type <ast> callable_expr callable_variable static_member new_variable
243%type <ast> assignment_list_element array_pair encaps_var encaps_var_offset isset_variables
244%type <ast> top_statement_list use_declarations const_list inner_statement_list if_stmt
245%type <ast> alt_if_stmt for_exprs switch_case_list global_var_list static_var_list
246%type <ast> echo_expr_list unset_variables catch_list parameter_list class_statement_list
247%type <ast> implements_list case_list if_stmt_without_else
248%type <ast> non_empty_parameter_list argument_list non_empty_argument_list property_list
249%type <ast> class_const_list class_const_decl name_list trait_adaptations method_body non_empty_for_exprs
250%type <ast> ctor_arguments alt_if_stmt_without_else trait_adaptation_list lexical_vars
251%type <ast> lexical_var_list encaps_list array_pair_list non_empty_array_pair_list
252%type <ast> assignment_list isset_variable type return_type
253%type <ast> identifier
254
255%type <num> returns_ref function is_reference is_variadic variable_modifiers
256%type <num> method_modifiers non_empty_member_modifiers member_modifier
257%type <num> class_modifiers class_modifier use_type
258
259%type <str> backup_doc_comment
260
261%% /* Rules */
262
263start:
264    top_statement_list  { CG(ast) = $1; }
265;
266
267reserved_non_modifiers:
268      T_INCLUDE | T_INCLUDE_ONCE | T_EVAL | T_REQUIRE | T_REQUIRE_ONCE | T_LOGICAL_OR | T_LOGICAL_XOR | T_LOGICAL_AND
269    | T_INSTANCEOF | T_NEW | T_CLONE | T_EXIT | T_IF | T_ELSEIF | T_ELSE | T_ENDIF | T_ECHO | T_DO | T_WHILE | T_ENDWHILE
270    | T_FOR | T_ENDFOR | T_FOREACH | T_ENDFOREACH | T_DECLARE | T_ENDDECLARE | T_AS | T_TRY | T_CATCH | T_FINALLY
271    | T_THROW | T_USE | T_INSTEADOF | T_GLOBAL | T_VAR | T_UNSET | T_ISSET | T_EMPTY | T_CONTINUE | T_GOTO
272    | T_FUNCTION | T_CONST | T_RETURN | T_PRINT | T_YIELD | T_LIST | T_SWITCH | T_ENDSWITCH | T_CASE | T_DEFAULT | T_BREAK
273    | T_ARRAY | T_CALLABLE | T_EXTENDS | T_IMPLEMENTS | T_NAMESPACE | T_TRAIT | T_INTERFACE | T_CLASS
274;
275
276semi_reserved:
277      reserved_non_modifiers
278    | T_STATIC | T_ABSTRACT | T_FINAL | T_PRIVATE | T_PROTECTED | T_PUBLIC
279;
280
281identifier:
282        T_STRING { $$ = $1; }
283    |   semi_reserved  {
284            zval zv;
285            zend_lex_tstring(&zv);
286            $$ = zend_ast_create_zval(&zv);
287        }
288;
289
290top_statement_list:
291        top_statement_list top_statement { $$ = zend_ast_list_add($1, $2); }
292    |   /* empty */ { $$ = zend_ast_create_list(0, ZEND_AST_STMT_LIST); }
293;
294
295namespace_name:
296        T_STRING                                { $$ = $1; }
297    |   namespace_name T_NS_SEPARATOR T_STRING  { $$ = zend_ast_append_str($1, $3); }
298;
299
300name:
301        namespace_name                              { $$ = $1; $$->attr = ZEND_NAME_NOT_FQ; }
302    |   T_NAMESPACE T_NS_SEPARATOR namespace_name   { $$ = $3; $$->attr = ZEND_NAME_RELATIVE; }
303    |   T_NS_SEPARATOR namespace_name               { $$ = $2; $$->attr = ZEND_NAME_FQ; }
304;
305
306top_statement:
307        statement                           { $$ = $1; }
308    |   function_declaration_statement      { $$ = $1; }
309    |   class_declaration_statement         { $$ = $1; }
310    |   trait_declaration_statement         { $$ = $1; }
311    |   interface_declaration_statement     { $$ = $1; }
312    |   T_HALT_COMPILER '(' ')' ';'
313            { $$ = zend_ast_create(ZEND_AST_HALT_COMPILER,
314                  zend_ast_create_zval_from_long(zend_get_scanned_file_offset()));
315              zend_stop_lexing(); }
316    |   T_NAMESPACE namespace_name ';'
317            { $$ = zend_ast_create(ZEND_AST_NAMESPACE, $2, NULL);
318              RESET_DOC_COMMENT(); }
319    |   T_NAMESPACE namespace_name { RESET_DOC_COMMENT(); }
320        '{' top_statement_list '}'
321            { $$ = zend_ast_create(ZEND_AST_NAMESPACE, $2, $5); }
322    |   T_NAMESPACE { RESET_DOC_COMMENT(); }
323        '{' top_statement_list '}'
324            { $$ = zend_ast_create(ZEND_AST_NAMESPACE, NULL, $4); }
325    |   T_USE mixed_group_use_declaration ';'       { $$ = $2; }
326    |   T_USE use_type group_use_declaration ';'    { $$ = $3; $$->attr = $2; }
327    |   T_USE use_declarations ';'                  { $$ = $2; $$->attr = T_CLASS; }
328    |   T_USE use_type use_declarations ';'         { $$ = $3; $$->attr = $2; }
329    |   T_CONST const_list ';'                      { $$ = $2; }
330;
331
332use_type:
333        T_FUNCTION      { $$ = T_FUNCTION; }
334    |   T_CONST         { $$ = T_CONST; }
335;
336
337group_use_declaration:
338    namespace_name T_NS_SEPARATOR '{' use_declarations '}'
339        {$$ = zend_ast_create(ZEND_AST_GROUP_USE, $1, $4); }
340;
341
342mixed_group_use_declaration:
343    namespace_name T_NS_SEPARATOR '{' inline_use_declarations '}'
344        {$$ = zend_ast_create(ZEND_AST_GROUP_USE, $1, $4);}
345;
346
347inline_use_declarations:
348        inline_use_declarations ',' inline_use_declaration
349            { $$ = zend_ast_list_add($1, $3); }
350    |   inline_use_declaration
351            { $$ = zend_ast_create_list(1, ZEND_AST_USE, $1); }
352;
353
354inline_use_declaration:
355        use_declaration { $$ = $1; $$->attr = T_CLASS; }
356    |   use_type use_declaration { $$ = $2; $$->attr = $1; }
357;
358
359use_declarations:
360        use_declarations ',' use_declaration
361            { $$ = zend_ast_list_add($1, $3); }
362    |   use_declaration
363            { $$ = zend_ast_create_list(1, ZEND_AST_USE, $1); }
364;
365
366use_declaration:
367        namespace_name
368            { $$ = zend_ast_create(ZEND_AST_USE_ELEM, $1, NULL); }
369    |   namespace_name T_AS T_STRING
370            { $$ = zend_ast_create(ZEND_AST_USE_ELEM, $1, $3); }
371    |   T_NS_SEPARATOR namespace_name
372            { $$ = zend_ast_create(ZEND_AST_USE_ELEM, $2, NULL); }
373    |   T_NS_SEPARATOR namespace_name T_AS T_STRING
374            { $$ = zend_ast_create(ZEND_AST_USE_ELEM, $2, $4); }
375;
376
377const_list:
378        const_list ',' const_decl { $$ = zend_ast_list_add($1, $3); }
379    |   const_decl { $$ = zend_ast_create_list(1, ZEND_AST_CONST_DECL, $1); }
380;
381
382inner_statement_list:
383        inner_statement_list inner_statement
384            { $$ = zend_ast_list_add($1, $2); }
385    |   /* empty */
386            { $$ = zend_ast_create_list(0, ZEND_AST_STMT_LIST); }
387;
388
389
390inner_statement:
391        statement { $$ = $1; }
392    |   function_declaration_statement      { $$ = $1; }
393    |   class_declaration_statement         { $$ = $1; }
394    |   trait_declaration_statement         { $$ = $1; }
395    |   interface_declaration_statement     { $$ = $1; }
396    |   T_HALT_COMPILER '(' ')' ';'
397            { $$ = NULL; zend_error_noreturn(E_COMPILE_ERROR,
398                  "__HALT_COMPILER() can only be used from the outermost scope"); }
399;
400
401
402statement:
403        '{' inner_statement_list '}' { $$ = $2; }
404    |   if_stmt { $$ = $1; }
405    |   alt_if_stmt { $$ = $1; }
406    |   T_WHILE '(' expr ')' while_statement
407            { $$ = zend_ast_create(ZEND_AST_WHILE, $3, $5); }
408    |   T_DO statement T_WHILE '(' expr ')' ';'
409            { $$ = zend_ast_create(ZEND_AST_DO_WHILE, $2, $5); }
410    |   T_FOR '(' for_exprs ';' for_exprs ';' for_exprs ')' for_statement
411            { $$ = zend_ast_create(ZEND_AST_FOR, $3, $5, $7, $9); }
412    |   T_SWITCH '(' expr ')' switch_case_list
413            { $$ = zend_ast_create(ZEND_AST_SWITCH, $3, $5); }
414    |   T_BREAK optional_expr ';'       { $$ = zend_ast_create(ZEND_AST_BREAK, $2); }
415    |   T_CONTINUE optional_expr ';'    { $$ = zend_ast_create(ZEND_AST_CONTINUE, $2); }
416    |   T_RETURN optional_expr ';'      { $$ = zend_ast_create(ZEND_AST_RETURN, $2); }
417    |   T_GLOBAL global_var_list ';'    { $$ = $2; }
418    |   T_STATIC static_var_list ';'    { $$ = $2; }
419    |   T_ECHO echo_expr_list ';'       { $$ = $2; }
420    |   T_INLINE_HTML { $$ = zend_ast_create(ZEND_AST_ECHO, $1); }
421    |   expr ';' { $$ = $1; }
422    |   T_UNSET '(' unset_variables ')' ';' { $$ = $3; }
423    |   T_FOREACH '(' expr T_AS foreach_variable ')' foreach_statement
424            { $$ = zend_ast_create(ZEND_AST_FOREACH, $3, $5, NULL, $7); }
425    |   T_FOREACH '(' expr T_AS foreach_variable T_DOUBLE_ARROW foreach_variable ')'
426        foreach_statement
427            { $$ = zend_ast_create(ZEND_AST_FOREACH, $3, $7, $5, $9); }
428    |   T_DECLARE '(' const_list ')'
429            { zend_handle_encoding_declaration($3); }
430        declare_statement
431            { $$ = zend_ast_create(ZEND_AST_DECLARE, $3, $6); }
432    |   ';' /* empty statement */ { $$ = NULL; }
433    |   T_TRY '{' inner_statement_list '}' catch_list finally_statement
434            { $$ = zend_ast_create(ZEND_AST_TRY, $3, $5, $6); }
435    |   T_THROW expr ';' { $$ = zend_ast_create(ZEND_AST_THROW, $2); }
436    |   T_GOTO T_STRING ';' { $$ = zend_ast_create(ZEND_AST_GOTO, $2); }
437    |   T_STRING ':' { $$ = zend_ast_create(ZEND_AST_LABEL, $1); }
438;
439
440catch_list:
441        /* empty */
442            { $$ = zend_ast_create_list(0, ZEND_AST_CATCH_LIST); }
443    |   catch_list T_CATCH '(' name T_VARIABLE ')' '{' inner_statement_list '}'
444            { $$ = zend_ast_list_add($1, zend_ast_create(ZEND_AST_CATCH, $4, $5, $8)); }
445;
446
447finally_statement:
448        /* empty */ { $$ = NULL; }
449    |   T_FINALLY '{' inner_statement_list '}' { $$ = $3; }
450;
451
452unset_variables:
453        unset_variable { $$ = zend_ast_create_list(1, ZEND_AST_STMT_LIST, $1); }
454    |   unset_variables ',' unset_variable { $$ = zend_ast_list_add($1, $3); }
455;
456
457unset_variable:
458        variable { $$ = zend_ast_create(ZEND_AST_UNSET, $1); }
459;
460
461function_declaration_statement:
462    function returns_ref T_STRING '(' parameter_list ')' return_type
463    backup_doc_comment '{' inner_statement_list '}'
464        { $$ = zend_ast_create_decl(ZEND_AST_FUNC_DECL, $2, $1, $8,
465              zend_ast_get_str($3), $5, NULL, $10, $7); }
466;
467
468is_reference:
469        /* empty */ { $$ = 0; }
470    |   '&'         { $$ = ZEND_PARAM_REF; }
471;
472
473is_variadic:
474        /* empty */ { $$ = 0; }
475    |   T_ELLIPSIS  { $$ = ZEND_PARAM_VARIADIC; }
476;
477
478class_declaration_statement:
479        class_modifiers T_CLASS { $<num>$ = CG(zend_lineno); }
480        T_STRING extends_from implements_list backup_doc_comment '{' class_statement_list '}'
481            { $$ = zend_ast_create_decl(ZEND_AST_CLASS, $1, $<num>3, $7, zend_ast_get_str($4), $5, $6, $9, NULL); }
482    |   T_CLASS { $<num>$ = CG(zend_lineno); }
483        T_STRING extends_from implements_list backup_doc_comment '{' class_statement_list '}'
484            { $$ = zend_ast_create_decl(ZEND_AST_CLASS, 0, $<num>2, $6, zend_ast_get_str($3), $4, $5, $8, NULL); }
485;
486
487class_modifiers:
488        class_modifier                  { $$ = $1; }
489    |   class_modifiers class_modifier  { $$ = zend_add_class_modifier($1, $2); }
490;
491
492class_modifier:
493        T_ABSTRACT      { $$ = ZEND_ACC_EXPLICIT_ABSTRACT_CLASS; }
494    |   T_FINAL         { $$ = ZEND_ACC_FINAL; }
495;
496
497trait_declaration_statement:
498        T_TRAIT { $<num>$ = CG(zend_lineno); }
499        T_STRING backup_doc_comment '{' class_statement_list '}'
500            { $$ = zend_ast_create_decl(ZEND_AST_CLASS, ZEND_ACC_TRAIT, $<num>2, $4, zend_ast_get_str($3), NULL, NULL, $6, NULL); }
501;
502
503interface_declaration_statement:
504        T_INTERFACE { $<num>$ = CG(zend_lineno); }
505        T_STRING interface_extends_list backup_doc_comment '{' class_statement_list '}'
506            { $$ = zend_ast_create_decl(ZEND_AST_CLASS, ZEND_ACC_INTERFACE, $<num>2, $5, zend_ast_get_str($3), NULL, $4, $7, NULL); }
507;
508
509extends_from:
510        /* empty */     { $$ = NULL; }
511    |   T_EXTENDS name  { $$ = $2; }
512;
513
514interface_extends_list:
515        /* empty */         { $$ = NULL; }
516    |   T_EXTENDS name_list { $$ = $2; }
517;
518
519implements_list:
520        /* empty */             { $$ = NULL; }
521    |   T_IMPLEMENTS name_list  { $$ = $2; }
522;
523
524foreach_variable:
525        variable            { $$ = $1; }
526    |   '&' variable        { $$ = zend_ast_create(ZEND_AST_REF, $2); }
527    |   T_LIST '(' assignment_list ')' { $$ = $3; }
528;
529
530for_statement:
531        statement { $$ = $1; }
532    |   ':' inner_statement_list T_ENDFOR ';' { $$ = $2; }
533;
534
535foreach_statement:
536        statement { $$ = $1; }
537    |   ':' inner_statement_list T_ENDFOREACH ';' { $$ = $2; }
538;
539
540declare_statement:
541        statement { $$ = $1; }
542    |   ':' inner_statement_list T_ENDDECLARE ';' { $$ = $2; }
543;
544
545switch_case_list:
546        '{' case_list '}'                   { $$ = $2; }
547    |   '{' ';' case_list '}'               { $$ = $3; }
548    |   ':' case_list T_ENDSWITCH ';'       { $$ = $2; }
549    |   ':' ';' case_list T_ENDSWITCH ';'   { $$ = $3; }
550;
551
552case_list:
553        /* empty */ { $$ = zend_ast_create_list(0, ZEND_AST_SWITCH_LIST); }
554    |   case_list T_CASE expr case_separator inner_statement_list
555            { $$ = zend_ast_list_add($1, zend_ast_create(ZEND_AST_SWITCH_CASE, $3, $5)); }
556    |   case_list T_DEFAULT case_separator inner_statement_list
557            { $$ = zend_ast_list_add($1, zend_ast_create(ZEND_AST_SWITCH_CASE, NULL, $4)); }
558;
559
560case_separator:
561        ':'
562    |   ';'
563;
564
565
566while_statement:
567        statement { $$ = $1; }
568    |   ':' inner_statement_list T_ENDWHILE ';' { $$ = $2; }
569;
570
571
572if_stmt_without_else:
573        T_IF '(' expr ')' statement
574            { $$ = zend_ast_create_list(1, ZEND_AST_IF,
575                  zend_ast_create(ZEND_AST_IF_ELEM, $3, $5)); }
576    |   if_stmt_without_else T_ELSEIF '(' expr ')' statement
577            { $$ = zend_ast_list_add($1,
578                  zend_ast_create(ZEND_AST_IF_ELEM, $4, $6)); }
579;
580
581if_stmt:
582        if_stmt_without_else %prec T_NOELSE { $$ = $1; }
583    |   if_stmt_without_else T_ELSE statement
584            { $$ = zend_ast_list_add($1, zend_ast_create(ZEND_AST_IF_ELEM, NULL, $3)); }
585;
586
587alt_if_stmt_without_else:
588        T_IF '(' expr ')' ':' inner_statement_list
589            { $$ = zend_ast_create_list(1, ZEND_AST_IF,
590                  zend_ast_create(ZEND_AST_IF_ELEM, $3, $6)); }
591    |   alt_if_stmt_without_else T_ELSEIF '(' expr ')' ':' inner_statement_list
592            { $$ = zend_ast_list_add($1,
593                  zend_ast_create(ZEND_AST_IF_ELEM, $4, $7)); }
594;
595
596alt_if_stmt:
597        alt_if_stmt_without_else T_ENDIF ';' { $$ = $1; }
598    |   alt_if_stmt_without_else T_ELSE ':' inner_statement_list T_ENDIF ';'
599            { $$ = zend_ast_list_add($1,
600                  zend_ast_create(ZEND_AST_IF_ELEM, NULL, $4)); }
601;
602
603parameter_list:
604        non_empty_parameter_list { $$ = $1; }
605    |   /* empty */ { $$ = zend_ast_create_list(0, ZEND_AST_PARAM_LIST); }
606;
607
608
609non_empty_parameter_list:
610        parameter
611            { $$ = zend_ast_create_list(1, ZEND_AST_PARAM_LIST, $1); }
612    |   non_empty_parameter_list ',' parameter
613            { $$ = zend_ast_list_add($1, $3); }
614;
615
616parameter:
617        optional_type is_reference is_variadic T_VARIABLE
618            { $$ = zend_ast_create_ex(ZEND_AST_PARAM, $2 | $3, $1, $4, NULL); }
619    |   optional_type is_reference is_variadic T_VARIABLE '=' expr
620            { $$ = zend_ast_create_ex(ZEND_AST_PARAM, $2 | $3, $1, $4, $6); }
621;
622
623
624optional_type:
625        /* empty */ { $$ = NULL; }
626    |   type        { $$ = $1; }
627;
628
629type:
630        T_ARRAY     { $$ = zend_ast_create_ex(ZEND_AST_TYPE, IS_ARRAY); }
631    |   T_CALLABLE  { $$ = zend_ast_create_ex(ZEND_AST_TYPE, IS_CALLABLE); }
632    |   name        { $$ = $1; }
633;
634
635return_type:
636        /* empty */ { $$ = NULL; }
637    |   ':' type    { $$ = $2; }
638;
639
640argument_list:
641        '(' ')' { $$ = zend_ast_create_list(0, ZEND_AST_ARG_LIST); }
642    |   '(' non_empty_argument_list ')' { $$ = $2; }
643;
644
645non_empty_argument_list:
646        argument
647            { $$ = zend_ast_create_list(1, ZEND_AST_ARG_LIST, $1); }
648    |   non_empty_argument_list ',' argument
649            { $$ = zend_ast_list_add($1, $3); }
650;
651
652argument:
653        expr            { $$ = $1; }
654    |   T_ELLIPSIS expr { $$ = zend_ast_create(ZEND_AST_UNPACK, $2); }
655;
656
657global_var_list:
658        global_var_list ',' global_var { $$ = zend_ast_list_add($1, $3); }
659    |   global_var { $$ = zend_ast_create_list(1, ZEND_AST_STMT_LIST, $1); }
660;
661
662global_var:
663    simple_variable
664        { $$ = zend_ast_create(ZEND_AST_GLOBAL, zend_ast_create(ZEND_AST_VAR, $1)); }
665;
666
667
668static_var_list:
669        static_var_list ',' static_var { $$ = zend_ast_list_add($1, $3); }
670    |   static_var { $$ = zend_ast_create_list(1, ZEND_AST_STMT_LIST, $1); }
671;
672
673static_var:
674        T_VARIABLE          { $$ = zend_ast_create(ZEND_AST_STATIC, $1, NULL); }
675    |   T_VARIABLE '=' expr { $$ = zend_ast_create(ZEND_AST_STATIC, $1, $3); }
676;
677
678
679class_statement_list:
680        class_statement_list class_statement
681            { $$ = zend_ast_list_add($1, $2); }
682    |   /* empty */
683            { $$ = zend_ast_create_list(0, ZEND_AST_STMT_LIST); }
684;
685
686
687class_statement:
688        variable_modifiers property_list ';'
689            { $$ = zend_ast_append_doc_comment($2); $$->attr = $1; }
690    |   T_CONST class_const_list ';'
691            { $$ = $2; RESET_DOC_COMMENT(); }
692    |   T_USE name_list trait_adaptations
693            { $$ = zend_ast_create(ZEND_AST_USE_TRAIT, $2, $3); }
694    |   method_modifiers function returns_ref identifier '(' parameter_list ')'
695        return_type backup_doc_comment method_body
696            { $$ = zend_ast_create_decl(ZEND_AST_METHOD, $3 | $1, $2, $9,
697                  zend_ast_get_str($4), $6, NULL, $10, $8); }
698;
699
700name_list:
701        name { $$ = zend_ast_create_list(1, ZEND_AST_NAME_LIST, $1); }
702    |   name_list ',' name { $$ = zend_ast_list_add($1, $3); }
703;
704
705trait_adaptations:
706        ';'                             { $$ = NULL; }
707    |   '{' '}'                         { $$ = NULL; }
708    |   '{' trait_adaptation_list '}'   { $$ = $2; }
709;
710
711trait_adaptation_list:
712        trait_adaptation
713            { $$ = zend_ast_create_list(1, ZEND_AST_TRAIT_ADAPTATIONS, $1); }
714    |   trait_adaptation_list trait_adaptation
715            { $$ = zend_ast_list_add($1, $2); }
716;
717
718trait_adaptation:
719        trait_precedence ';'    { $$ = $1; }
720    |   trait_alias ';'         { $$ = $1; }
721;
722
723trait_precedence:
724    absolute_trait_method_reference T_INSTEADOF name_list
725        { $$ = zend_ast_create(ZEND_AST_TRAIT_PRECEDENCE, $1, $3); }
726;
727
728trait_alias:
729        trait_method_reference T_AS T_STRING
730            { $$ = zend_ast_create_ex(ZEND_AST_TRAIT_ALIAS, 0, $1, $3); }
731    |   trait_method_reference T_AS reserved_non_modifiers
732            { zval zv; zend_lex_tstring(&zv); $$ = zend_ast_create_ex(ZEND_AST_TRAIT_ALIAS, 0, $1, zend_ast_create_zval(&zv)); }
733    |   trait_method_reference T_AS member_modifier identifier
734            { $$ = zend_ast_create_ex(ZEND_AST_TRAIT_ALIAS, $3, $1, $4); }
735    |   trait_method_reference T_AS member_modifier %prec '+'
736            { $$ = zend_ast_create_ex(ZEND_AST_TRAIT_ALIAS, $3, $1, NULL); }
737;
738
739trait_method_reference:
740        identifier
741            { $$ = zend_ast_create(ZEND_AST_METHOD_REFERENCE, NULL, $1); }
742    |   absolute_trait_method_reference { $$ = $1; }
743;
744
745absolute_trait_method_reference:
746    name T_PAAMAYIM_NEKUDOTAYIM identifier
747        { $$ = zend_ast_create(ZEND_AST_METHOD_REFERENCE, $1, $3); }
748;
749
750method_body:
751        ';' /* abstract method */       { $$ = NULL; }
752    |   '{' inner_statement_list '}'    { $$ = $2; }
753;
754
755variable_modifiers:
756        non_empty_member_modifiers      { $$ = $1; }
757    |   T_VAR                           { $$ = ZEND_ACC_PUBLIC; }
758;
759
760method_modifiers:
761        /* empty */                     { $$ = ZEND_ACC_PUBLIC; }
762    |   non_empty_member_modifiers
763            { $$ = $1; if (!($$ & ZEND_ACC_PPP_MASK)) { $$ |= ZEND_ACC_PUBLIC; } }
764;
765
766non_empty_member_modifiers:
767        member_modifier         { $$ = $1; }
768    |   non_empty_member_modifiers member_modifier
769            { $$ = zend_add_member_modifier($1, $2); }
770;
771
772member_modifier:
773        T_PUBLIC                { $$ = ZEND_ACC_PUBLIC; }
774    |   T_PROTECTED             { $$ = ZEND_ACC_PROTECTED; }
775    |   T_PRIVATE               { $$ = ZEND_ACC_PRIVATE; }
776    |   T_STATIC                { $$ = ZEND_ACC_STATIC; }
777    |   T_ABSTRACT              { $$ = ZEND_ACC_ABSTRACT; }
778    |   T_FINAL                 { $$ = ZEND_ACC_FINAL; }
779;
780
781property_list:
782        property_list ',' property { $$ = zend_ast_list_add($1, $3); }
783    |   property { $$ = zend_ast_create_list(1, ZEND_AST_PROP_DECL, $1); }
784;
785
786property:
787        T_VARIABLE { $$ = zend_ast_create(ZEND_AST_PROP_ELEM, $1, NULL); }
788    |   T_VARIABLE '=' expr
789            { $$ = zend_ast_create(ZEND_AST_PROP_ELEM, $1, $3); }
790;
791
792class_const_list:
793        class_const_list ',' class_const_decl { $$ = zend_ast_list_add($1, $3); }
794    |   class_const_decl { $$ = zend_ast_create_list(1, ZEND_AST_CLASS_CONST_DECL, $1); }
795;
796
797class_const_decl:
798    identifier '=' expr { $$ = zend_ast_create(ZEND_AST_CONST_ELEM, $1, $3); }
799;
800
801const_decl:
802    T_STRING '=' expr { $$ = zend_ast_create(ZEND_AST_CONST_ELEM, $1, $3); }
803;
804
805echo_expr_list:
806        echo_expr_list ',' echo_expr { $$ = zend_ast_list_add($1, $3); }
807    |   echo_expr { $$ = zend_ast_create_list(1, ZEND_AST_STMT_LIST, $1); }
808;
809echo_expr:
810    expr { $$ = zend_ast_create(ZEND_AST_ECHO, $1); }
811;
812
813for_exprs:
814        /* empty */         { $$ = NULL; }
815    |   non_empty_for_exprs { $$ = $1; }
816;
817
818non_empty_for_exprs:
819        non_empty_for_exprs ',' expr { $$ = zend_ast_list_add($1, $3); }
820    |   expr { $$ = zend_ast_create_list(1, ZEND_AST_EXPR_LIST, $1); }
821;
822
823anonymous_class:
824        T_CLASS { $<num>$ = CG(zend_lineno); } ctor_arguments
825        extends_from implements_list backup_doc_comment '{' class_statement_list '}' {
826            zend_ast *decl = zend_ast_create_decl(
827                ZEND_AST_CLASS, ZEND_ACC_ANON_CLASS, $<num>2, $6, NULL,
828                $4, $5, $8, NULL);
829            $$ = zend_ast_create(ZEND_AST_NEW, decl, $3);
830        }
831;
832
833new_expr:
834        T_NEW class_name_reference ctor_arguments
835            { $$ = zend_ast_create(ZEND_AST_NEW, $2, $3); }
836    |   T_NEW anonymous_class
837            { $$ = $2; }
838;
839
840expr_without_variable:
841        T_LIST '(' assignment_list ')' '=' expr
842            { $$ = zend_ast_create(ZEND_AST_ASSIGN, $3, $6); }
843    |   variable '=' expr
844            { $$ = zend_ast_create(ZEND_AST_ASSIGN, $1, $3); }
845    |   variable '=' '&' variable
846            { $$ = zend_ast_create(ZEND_AST_ASSIGN_REF, $1, $4); }
847    |   T_CLONE expr { $$ = zend_ast_create(ZEND_AST_CLONE, $2); }
848    |   variable T_PLUS_EQUAL expr
849            { $$ = zend_ast_create_assign_op(ZEND_ASSIGN_ADD, $1, $3); }
850    |   variable T_MINUS_EQUAL expr
851            { $$ = zend_ast_create_assign_op(ZEND_ASSIGN_SUB, $1, $3); }
852    |   variable T_MUL_EQUAL expr
853            { $$ = zend_ast_create_assign_op(ZEND_ASSIGN_MUL, $1, $3); }
854    |   variable T_POW_EQUAL expr
855            { $$ = zend_ast_create_assign_op(ZEND_ASSIGN_POW, $1, $3); }
856    |   variable T_DIV_EQUAL expr
857            { $$ = zend_ast_create_assign_op(ZEND_ASSIGN_DIV, $1, $3); }
858    |   variable T_CONCAT_EQUAL expr
859            { $$ = zend_ast_create_assign_op(ZEND_ASSIGN_CONCAT, $1, $3); }
860    |   variable T_MOD_EQUAL expr
861            { $$ = zend_ast_create_assign_op(ZEND_ASSIGN_MOD, $1, $3); }
862    |   variable T_AND_EQUAL expr
863            { $$ = zend_ast_create_assign_op(ZEND_ASSIGN_BW_AND, $1, $3); }
864    |   variable T_OR_EQUAL expr
865            { $$ = zend_ast_create_assign_op(ZEND_ASSIGN_BW_OR, $1, $3); }
866    |   variable T_XOR_EQUAL expr
867            { $$ = zend_ast_create_assign_op(ZEND_ASSIGN_BW_XOR, $1, $3); }
868    |   variable T_SL_EQUAL expr
869            { $$ = zend_ast_create_assign_op(ZEND_ASSIGN_SL, $1, $3); }
870    |   variable T_SR_EQUAL expr
871            { $$ = zend_ast_create_assign_op(ZEND_ASSIGN_SR, $1, $3); }
872    |   variable T_INC { $$ = zend_ast_create(ZEND_AST_POST_INC, $1); }
873    |   T_INC variable { $$ = zend_ast_create(ZEND_AST_PRE_INC, $2); }
874    |   variable T_DEC { $$ = zend_ast_create(ZEND_AST_POST_DEC, $1); }
875    |   T_DEC variable { $$ = zend_ast_create(ZEND_AST_PRE_DEC, $2); }
876    |   expr T_BOOLEAN_OR expr
877            { $$ = zend_ast_create(ZEND_AST_OR, $1, $3); }
878    |   expr T_BOOLEAN_AND expr
879            { $$ = zend_ast_create(ZEND_AST_AND, $1, $3); }
880    |   expr T_LOGICAL_OR expr
881            { $$ = zend_ast_create(ZEND_AST_OR, $1, $3); }
882    |   expr T_LOGICAL_AND expr
883            { $$ = zend_ast_create(ZEND_AST_AND, $1, $3); }
884    |   expr T_LOGICAL_XOR expr
885            { $$ = zend_ast_create_binary_op(ZEND_BOOL_XOR, $1, $3); }
886    |   expr '|' expr   { $$ = zend_ast_create_binary_op(ZEND_BW_OR, $1, $3); }
887    |   expr '&' expr   { $$ = zend_ast_create_binary_op(ZEND_BW_AND, $1, $3); }
888    |   expr '^' expr   { $$ = zend_ast_create_binary_op(ZEND_BW_XOR, $1, $3); }
889    |   expr '.' expr   { $$ = zend_ast_create_binary_op(ZEND_CONCAT, $1, $3); }
890    |   expr '+' expr   { $$ = zend_ast_create_binary_op(ZEND_ADD, $1, $3); }
891    |   expr '-' expr   { $$ = zend_ast_create_binary_op(ZEND_SUB, $1, $3); }
892    |   expr '*' expr   { $$ = zend_ast_create_binary_op(ZEND_MUL, $1, $3); }
893    |   expr T_POW expr { $$ = zend_ast_create_binary_op(ZEND_POW, $1, $3); }
894    |   expr '/' expr   { $$ = zend_ast_create_binary_op(ZEND_DIV, $1, $3); }
895    |   expr '%' expr   { $$ = zend_ast_create_binary_op(ZEND_MOD, $1, $3); }
896    |   expr T_SL expr  { $$ = zend_ast_create_binary_op(ZEND_SL, $1, $3); }
897    |   expr T_SR expr  { $$ = zend_ast_create_binary_op(ZEND_SR, $1, $3); }
898    |   '+' expr %prec T_INC { $$ = zend_ast_create(ZEND_AST_UNARY_PLUS, $2); }
899    |   '-' expr %prec T_INC { $$ = zend_ast_create(ZEND_AST_UNARY_MINUS, $2); }
900    |   '!' expr { $$ = zend_ast_create_ex(ZEND_AST_UNARY_OP, ZEND_BOOL_NOT, $2); }
901    |   '~' expr { $$ = zend_ast_create_ex(ZEND_AST_UNARY_OP, ZEND_BW_NOT, $2); }
902    |   expr T_IS_IDENTICAL expr
903            { $$ = zend_ast_create_binary_op(ZEND_IS_IDENTICAL, $1, $3); }
904    |   expr T_IS_NOT_IDENTICAL expr
905            { $$ = zend_ast_create_binary_op(ZEND_IS_NOT_IDENTICAL, $1, $3); }
906    |   expr T_IS_EQUAL expr
907            { $$ = zend_ast_create_binary_op(ZEND_IS_EQUAL, $1, $3); }
908    |   expr T_IS_NOT_EQUAL expr
909            { $$ = zend_ast_create_binary_op(ZEND_IS_NOT_EQUAL, $1, $3); }
910    |   expr '<' expr
911            { $$ = zend_ast_create_binary_op(ZEND_IS_SMALLER, $1, $3); }
912    |   expr T_IS_SMALLER_OR_EQUAL expr
913            { $$ = zend_ast_create_binary_op(ZEND_IS_SMALLER_OR_EQUAL, $1, $3); }
914    |   expr '>' expr
915            { $$ = zend_ast_create(ZEND_AST_GREATER, $1, $3); }
916    |   expr T_IS_GREATER_OR_EQUAL expr
917            { $$ = zend_ast_create(ZEND_AST_GREATER_EQUAL, $1, $3); }
918    |   expr T_SPACESHIP expr
919            { $$ = zend_ast_create_binary_op(ZEND_SPACESHIP, $1, $3); }
920    |   expr T_INSTANCEOF class_name_reference
921            { $$ = zend_ast_create(ZEND_AST_INSTANCEOF, $1, $3); }
922    |   '(' expr ')' { $$ = $2; }
923    |   new_expr { $$ = $1; }
924    |   expr '?' expr ':' expr
925            { $$ = zend_ast_create(ZEND_AST_CONDITIONAL, $1, $3, $5); }
926    |   expr '?' ':' expr
927            { $$ = zend_ast_create(ZEND_AST_CONDITIONAL, $1, NULL, $4); }
928    |   expr T_COALESCE expr
929            { $$ = zend_ast_create(ZEND_AST_COALESCE, $1, $3); }
930    |   internal_functions_in_yacc { $$ = $1; }
931    |   T_INT_CAST expr     { $$ = zend_ast_create_cast(IS_LONG, $2); }
932    |   T_DOUBLE_CAST expr  { $$ = zend_ast_create_cast(IS_DOUBLE, $2); }
933    |   T_STRING_CAST expr  { $$ = zend_ast_create_cast(IS_STRING, $2); }
934    |   T_ARRAY_CAST expr   { $$ = zend_ast_create_cast(IS_ARRAY, $2); }
935    |   T_OBJECT_CAST expr  { $$ = zend_ast_create_cast(IS_OBJECT, $2); }
936    |   T_BOOL_CAST expr    { $$ = zend_ast_create_cast(_IS_BOOL, $2); }
937    |   T_UNSET_CAST expr   { $$ = zend_ast_create_cast(IS_NULL, $2); }
938    |   T_EXIT exit_expr    { $$ = zend_ast_create(ZEND_AST_EXIT, $2); }
939    |   '@' expr            { $$ = zend_ast_create(ZEND_AST_SILENCE, $2); }
940    |   scalar { $$ = $1; }
941    |   '`' backticks_expr '`' { $$ = zend_ast_create(ZEND_AST_SHELL_EXEC, $2); }
942    |   T_PRINT expr { $$ = zend_ast_create(ZEND_AST_PRINT, $2); }
943    |   T_YIELD { $$ = zend_ast_create(ZEND_AST_YIELD, NULL, NULL); }
944    |   T_YIELD expr { $$ = zend_ast_create(ZEND_AST_YIELD, $2, NULL); }
945    |   T_YIELD expr T_DOUBLE_ARROW expr { $$ = zend_ast_create(ZEND_AST_YIELD, $4, $2); }
946    |   T_YIELD_FROM expr { $$ = zend_ast_create(ZEND_AST_YIELD_FROM, $2); }
947    |   function returns_ref '(' parameter_list ')' lexical_vars return_type
948        backup_doc_comment '{' inner_statement_list '}'
949            { $$ = zend_ast_create_decl(ZEND_AST_CLOSURE, $2, $1, $8,
950                  zend_string_init("{closure}", sizeof("{closure}") - 1, 0),
951                  $4, $6, $10, $7); }
952    |   T_STATIC function returns_ref '(' parameter_list ')' lexical_vars
953        return_type backup_doc_comment '{' inner_statement_list '}'
954            { $$ = zend_ast_create_decl(ZEND_AST_CLOSURE, $3 | ZEND_ACC_STATIC, $2, $9,
955                  zend_string_init("{closure}", sizeof("{closure}") - 1, 0),
956                  $5, $7, $11, $8); }
957;
958
959function:
960    T_FUNCTION { $$ = CG(zend_lineno); }
961;
962
963backup_doc_comment:
964    /* empty */ { $$ = CG(doc_comment); CG(doc_comment) = NULL; }
965;
966
967returns_ref:
968        /* empty */ { $$ = 0; }
969    |   '&'         { $$ = ZEND_ACC_RETURN_REFERENCE; }
970;
971
972lexical_vars:
973        /* empty */ { $$ = NULL; }
974    |   T_USE '(' lexical_var_list ')' { $$ = $3; }
975;
976
977lexical_var_list:
978        lexical_var_list ',' lexical_var { $$ = zend_ast_list_add($1, $3); }
979    |   lexical_var { $$ = zend_ast_create_list(1, ZEND_AST_CLOSURE_USES, $1); }
980;
981
982lexical_var:
983        T_VARIABLE      { $$ = $1; }
984    |   '&' T_VARIABLE  { $$ = $2; $$->attr = 1; }
985;
986
987function_call:
988        name argument_list
989            { $$ = zend_ast_create(ZEND_AST_CALL, $1, $2); }
990    |   class_name T_PAAMAYIM_NEKUDOTAYIM member_name argument_list
991            { $$ = zend_ast_create(ZEND_AST_STATIC_CALL, $1, $3, $4); }
992    |   variable_class_name T_PAAMAYIM_NEKUDOTAYIM member_name argument_list
993            { $$ = zend_ast_create(ZEND_AST_STATIC_CALL, $1, $3, $4); }
994    |   callable_expr argument_list
995            { $$ = zend_ast_create(ZEND_AST_CALL, $1, $2); }
996;
997
998class_name:
999        T_STATIC
1000            { zval zv; ZVAL_STRINGL(&zv, "static", sizeof("static")-1);
1001              $$ = zend_ast_create_zval_ex(&zv, ZEND_NAME_NOT_FQ); }
1002    |   name { $$ = $1; }
1003;
1004
1005class_name_reference:
1006        class_name      { $$ = $1; }
1007    |   new_variable    { $$ = $1; }
1008;
1009
1010exit_expr:
1011        /* empty */             { $$ = NULL; }
1012    |   '(' optional_expr ')'   { $$ = $2; }
1013;
1014
1015backticks_expr:
1016        /* empty */
1017            { $$ = zend_ast_create_zval_from_str(STR_EMPTY_ALLOC()); }
1018    |   T_ENCAPSED_AND_WHITESPACE { $$ = $1; }
1019    |   encaps_list { $$ = $1; }
1020;
1021
1022
1023ctor_arguments:
1024        /* empty */ { $$ = zend_ast_create_list(0, ZEND_AST_ARG_LIST); }
1025    |   argument_list { $$ = $1; }
1026;
1027
1028
1029dereferencable_scalar:
1030        T_ARRAY '(' array_pair_list ')' { $$ = $3; }
1031    |   '[' array_pair_list ']'         { $$ = $2; }
1032    |   T_CONSTANT_ENCAPSED_STRING      { $$ = $1; }
1033;
1034
1035scalar:
1036        T_LNUMBER   { $$ = $1; }
1037    |   T_DNUMBER   { $$ = $1; }
1038    |   T_LINE      { $$ = zend_ast_create_ex(ZEND_AST_MAGIC_CONST, T_LINE); }
1039    |   T_FILE      { $$ = zend_ast_create_ex(ZEND_AST_MAGIC_CONST, T_FILE); }
1040    |   T_DIR       { $$ = zend_ast_create_ex(ZEND_AST_MAGIC_CONST, T_DIR); }
1041    |   T_TRAIT_C   { $$ = zend_ast_create_ex(ZEND_AST_MAGIC_CONST, T_TRAIT_C); }
1042    |   T_METHOD_C  { $$ = zend_ast_create_ex(ZEND_AST_MAGIC_CONST, T_METHOD_C); }
1043    |   T_FUNC_C    { $$ = zend_ast_create_ex(ZEND_AST_MAGIC_CONST, T_FUNC_C); }
1044    |   T_NS_C      { $$ = zend_ast_create_ex(ZEND_AST_MAGIC_CONST, T_NS_C); }
1045    |   T_CLASS_C   { $$ = zend_ast_create_ex(ZEND_AST_MAGIC_CONST, T_CLASS_C); }
1046    |   T_START_HEREDOC T_ENCAPSED_AND_WHITESPACE T_END_HEREDOC { $$ = $2; }
1047    |   T_START_HEREDOC T_END_HEREDOC
1048            { $$ = zend_ast_create_zval_from_str(STR_EMPTY_ALLOC()); }
1049    |   '"' encaps_list '"'     { $$ = $2; }
1050    |   T_START_HEREDOC encaps_list T_END_HEREDOC { $$ = $2; }
1051    |   dereferencable_scalar   { $$ = $1; }
1052    |   constant            { $$ = $1; }
1053;
1054
1055constant:
1056        name { $$ = zend_ast_create(ZEND_AST_CONST, $1); }
1057    |   class_name T_PAAMAYIM_NEKUDOTAYIM identifier
1058            { $$ = zend_ast_create(ZEND_AST_CLASS_CONST, $1, $3); }
1059    |   variable_class_name T_PAAMAYIM_NEKUDOTAYIM identifier
1060            { $$ = zend_ast_create(ZEND_AST_CLASS_CONST, $1, $3); }
1061;
1062
1063possible_comma:
1064        /* empty */
1065    |   ','
1066;
1067
1068expr:
1069        variable                    { $$ = $1; }
1070    |   expr_without_variable       { $$ = $1; }
1071;
1072
1073optional_expr:
1074        /* empty */ { $$ = NULL; }
1075    |   expr        { $$ = $1; }
1076;
1077
1078variable_class_name:
1079    dereferencable { $$ = $1; }
1080;
1081
1082dereferencable:
1083        variable                { $$ = $1; }
1084    |   '(' expr ')'            { $$ = $2; }
1085    |   dereferencable_scalar   { $$ = $1; }
1086;
1087
1088callable_expr:
1089        callable_variable       { $$ = $1; }
1090    |   '(' expr ')'            { $$ = $2; }
1091    |   dereferencable_scalar   { $$ = $1; }
1092;
1093
1094callable_variable:
1095        simple_variable
1096            { $$ = zend_ast_create(ZEND_AST_VAR, $1); }
1097    |   dereferencable '[' optional_expr ']'
1098            { $$ = zend_ast_create(ZEND_AST_DIM, $1, $3); }
1099    |   constant '[' optional_expr ']'
1100            { $$ = zend_ast_create(ZEND_AST_DIM, $1, $3); }
1101    |   dereferencable '{' expr '}'
1102            { $$ = zend_ast_create(ZEND_AST_DIM, $1, $3); }
1103    |   dereferencable T_OBJECT_OPERATOR property_name argument_list
1104            { $$ = zend_ast_create(ZEND_AST_METHOD_CALL, $1, $3, $4); }
1105    |   function_call { $$ = $1; }
1106;
1107
1108variable:
1109        callable_variable
1110            { $$ = $1; }
1111    |   static_member
1112            { $$ = $1; }
1113    |   dereferencable T_OBJECT_OPERATOR property_name
1114            { $$ = zend_ast_create(ZEND_AST_PROP, $1, $3); }
1115;
1116
1117simple_variable:
1118        T_VARIABLE          { $$ = $1; }
1119    |   '$' '{' expr '}'    { $$ = $3; }
1120    |   '$' simple_variable { $$ = zend_ast_create(ZEND_AST_VAR, $2); }
1121;
1122
1123static_member:
1124        class_name T_PAAMAYIM_NEKUDOTAYIM simple_variable
1125            { $$ = zend_ast_create(ZEND_AST_STATIC_PROP, $1, $3); }
1126    |   variable_class_name T_PAAMAYIM_NEKUDOTAYIM simple_variable
1127            { $$ = zend_ast_create(ZEND_AST_STATIC_PROP, $1, $3); }
1128;
1129
1130new_variable:
1131        simple_variable
1132            { $$ = zend_ast_create(ZEND_AST_VAR, $1); }
1133    |   new_variable '[' optional_expr ']'
1134            { $$ = zend_ast_create(ZEND_AST_DIM, $1, $3); }
1135    |   new_variable '{' expr '}'
1136            { $$ = zend_ast_create(ZEND_AST_DIM, $1, $3); }
1137    |   new_variable T_OBJECT_OPERATOR property_name
1138            { $$ = zend_ast_create(ZEND_AST_PROP, $1, $3); }
1139    |   class_name T_PAAMAYIM_NEKUDOTAYIM simple_variable
1140            { $$ = zend_ast_create(ZEND_AST_STATIC_PROP, $1, $3); }
1141    |   new_variable T_PAAMAYIM_NEKUDOTAYIM simple_variable
1142            { $$ = zend_ast_create(ZEND_AST_STATIC_PROP, $1, $3); }
1143;
1144
1145member_name:
1146        identifier { $$ = $1; }
1147    |   '{' expr '}'    { $$ = $2; }
1148    |   simple_variable { $$ = zend_ast_create(ZEND_AST_VAR, $1); }
1149;
1150
1151property_name:
1152        T_STRING { $$ = $1; }
1153    |   '{' expr '}'    { $$ = $2; }
1154    |   simple_variable { $$ = zend_ast_create(ZEND_AST_VAR, $1); }
1155;
1156
1157assignment_list:
1158        assignment_list ',' assignment_list_element
1159            { $$ = zend_ast_list_add($1, $3); }
1160    |   assignment_list_element
1161            { $$ = zend_ast_create_list(1, ZEND_AST_LIST, $1); }
1162;
1163
1164assignment_list_element:
1165        variable                        { $$ = $1; }
1166    |   T_LIST '(' assignment_list ')'  { $$ = $3; }
1167    |   /* empty */                     { $$ = NULL; }
1168;
1169
1170
1171array_pair_list:
1172        /* empty */ { $$ = zend_ast_create_list(0, ZEND_AST_ARRAY); }
1173    |   non_empty_array_pair_list possible_comma { $$ = $1; }
1174;
1175
1176non_empty_array_pair_list:
1177        non_empty_array_pair_list ',' array_pair
1178            { $$ = zend_ast_list_add($1, $3); }
1179    |   array_pair
1180            { $$ = zend_ast_create_list(1, ZEND_AST_ARRAY, $1); }
1181;
1182
1183array_pair:
1184        expr T_DOUBLE_ARROW expr
1185            { $$ = zend_ast_create(ZEND_AST_ARRAY_ELEM, $3, $1); }
1186    |   expr { $$ = zend_ast_create(ZEND_AST_ARRAY_ELEM, $1, NULL); }
1187    |   expr T_DOUBLE_ARROW '&' variable
1188            { $$ = zend_ast_create_ex(ZEND_AST_ARRAY_ELEM, 1, $4, $1); }
1189    |   '&' variable
1190            { $$ = zend_ast_create_ex(ZEND_AST_ARRAY_ELEM, 1, $2, NULL); }
1191;
1192
1193encaps_list:
1194        encaps_list encaps_var
1195            { $$ = zend_ast_list_add($1, $2); }
1196    |   encaps_list T_ENCAPSED_AND_WHITESPACE
1197            { $$ = zend_ast_list_add($1, $2); }
1198    |   encaps_var
1199            { $$ = zend_ast_create_list(1, ZEND_AST_ENCAPS_LIST, $1); }
1200    |   T_ENCAPSED_AND_WHITESPACE encaps_var
1201            { $$ = zend_ast_create_list(2, ZEND_AST_ENCAPS_LIST, $1, $2); }
1202;
1203
1204encaps_var:
1205        T_VARIABLE
1206            { $$ = zend_ast_create(ZEND_AST_VAR, $1); }
1207    |   T_VARIABLE '[' encaps_var_offset ']'
1208            { $$ = zend_ast_create(ZEND_AST_DIM,
1209                  zend_ast_create(ZEND_AST_VAR, $1), $3); }
1210    |   T_VARIABLE T_OBJECT_OPERATOR T_STRING
1211            { $$ = zend_ast_create(ZEND_AST_PROP,
1212                  zend_ast_create(ZEND_AST_VAR, $1), $3); }
1213    |   T_DOLLAR_OPEN_CURLY_BRACES expr '}'
1214            { $$ = zend_ast_create(ZEND_AST_VAR, $2); }
1215    |   T_DOLLAR_OPEN_CURLY_BRACES T_STRING_VARNAME '}'
1216            { $$ = zend_ast_create(ZEND_AST_VAR, $2); }
1217    |   T_DOLLAR_OPEN_CURLY_BRACES T_STRING_VARNAME '[' expr ']' '}'
1218            { $$ = zend_ast_create(ZEND_AST_DIM,
1219                  zend_ast_create(ZEND_AST_VAR, $2), $4); }
1220    |   T_CURLY_OPEN variable '}' { $$ = $2; }
1221;
1222
1223encaps_var_offset:
1224        T_STRING        { $$ = $1; }
1225    |   T_NUM_STRING    { $$ = $1; }
1226    |   T_VARIABLE      { $$ = zend_ast_create(ZEND_AST_VAR, $1); }
1227;
1228
1229
1230internal_functions_in_yacc:
1231        T_ISSET '(' isset_variables ')' { $$ = $3; }
1232    |   T_EMPTY '(' expr ')' { $$ = zend_ast_create(ZEND_AST_EMPTY, $3); }
1233    |   T_INCLUDE expr
1234            { $$ = zend_ast_create_ex(ZEND_AST_INCLUDE_OR_EVAL, ZEND_INCLUDE, $2); }
1235    |   T_INCLUDE_ONCE expr
1236            { $$ = zend_ast_create_ex(ZEND_AST_INCLUDE_OR_EVAL, ZEND_INCLUDE_ONCE, $2); }
1237    |   T_EVAL '(' expr ')'
1238            { $$ = zend_ast_create_ex(ZEND_AST_INCLUDE_OR_EVAL, ZEND_EVAL, $3); }
1239    |   T_REQUIRE expr
1240            { $$ = zend_ast_create_ex(ZEND_AST_INCLUDE_OR_EVAL, ZEND_REQUIRE, $2); }
1241    |   T_REQUIRE_ONCE expr
1242            { $$ = zend_ast_create_ex(ZEND_AST_INCLUDE_OR_EVAL, ZEND_REQUIRE_ONCE, $2); }
1243;
1244
1245isset_variables:
1246        isset_variable { $$ = $1; }
1247    |   isset_variables ',' isset_variable
1248            { $$ = zend_ast_create(ZEND_AST_AND, $1, $3); }
1249;
1250
1251isset_variable:
1252        expr { $$ = zend_ast_create(ZEND_AST_ISSET, $1); }
1253;
1254
1255%%
1256
1257/* Copy to YYRES the contents of YYSTR after stripping away unnecessary
1258   quotes and backslashes, so that it's suitable for yyerror.  The
1259   heuristic is that double-quoting is unnecessary unless the string
1260   contains an apostrophe, a comma, or backslash (other than
1261   backslash-backslash).  YYSTR is taken from yytname.  If YYRES is
1262   null, do not copy; instead, return the length of what the result
1263   would have been.  */
1264static YYSIZE_T zend_yytnamerr(char *yyres, const char *yystr)
1265{
1266    if (!yyres) {
1267        return yystrlen(yystr);
1268    }
1269    {
1270        if (CG(parse_error) == 0) {
1271            char buffer[120];
1272            const unsigned char *end, *str, *tok1 = NULL, *tok2 = NULL;
1273            unsigned int len = 0, toklen = 0, yystr_len;
1274
1275            CG(parse_error) = 1;
1276
1277            if (LANG_SCNG(yy_text)[0] == 0 &&
1278                LANG_SCNG(yy_leng) == 1 &&
1279                memcmp(yystr, "\"end of file\"", sizeof("\"end of file\"") - 1) == 0) {
1280                yystpcpy(yyres, "end of file");
1281                return sizeof("end of file")-1;
1282            }
1283
1284            str = LANG_SCNG(yy_text);
1285            end = memchr(str, '\n', LANG_SCNG(yy_leng));
1286            yystr_len = (unsigned int)yystrlen(yystr);
1287
1288            if ((tok1 = memchr(yystr, '(', yystr_len)) != NULL
1289                && (tok2 = zend_memrchr(yystr, ')', yystr_len)) != NULL) {
1290                toklen = (tok2 - tok1) + 1;
1291            } else {
1292                tok1 = tok2 = NULL;
1293                toklen = 0;
1294            }
1295
1296            if (end == NULL) {
1297                len = LANG_SCNG(yy_leng) > 30 ? 30 : LANG_SCNG(yy_leng);
1298            } else {
1299                len = (end - str) > 30 ? 30 : (end - str);
1300            }
1301            if (toklen) {
1302                snprintf(buffer, sizeof(buffer), "'%.*s' %.*s", len, str, toklen, tok1);
1303            } else {
1304                snprintf(buffer, sizeof(buffer), "'%.*s'", len, str);
1305            }
1306            yystpcpy(yyres, buffer);
1307            return len + (toklen ? toklen + 1 : 0) + 2;
1308        }
1309    }
1310    if (*yystr == '"') {
1311        YYSIZE_T yyn = 0;
1312        const char *yyp = yystr;
1313
1314        for (; *++yyp != '"'; ++yyn) {
1315            yyres[yyn] = *yyp;
1316        }
1317        yyres[yyn] = '\0';
1318        return yyn;
1319    }
1320    yystpcpy(yyres, yystr);
1321    return strlen(yystr);
1322}
1323
1324/*
1325 * Local variables:
1326 * tab-width: 4
1327 * c-basic-offset: 4
1328 * indent-tabs-mode: t
1329 * End:
1330 */
1331