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