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