diff --git a/support/xhpast/ast.hpp b/support/xhpast/ast.hpp index a84a839..e7ff74f 100644 --- a/support/xhpast/ast.hpp +++ b/support/xhpast/ast.hpp @@ -1,85 +1,96 @@ #pragma once #include #include #include #include #include "astnode.hpp" class yy_extra_type { public: yy_extra_type() { + first_lineno = 0; lineno = 1; terminated = false; used = false; + last_token = -1; + insert_token = -1; + heredoc_yyleng = -1; + heredoc_data = (char *) 0; short_tags = true; asp_tags = false; idx_expr = false; include_debug = false; expecting_xhp_class_statements = false; + old_expecting_xhp_class_statements = false; + used_attributes = false; list_size = 0; colon_hack = false; pushStack(); } bool short_tags; // `short_open_tag` in php.ini bool asp_tags; // `asp_tags` in php.ini bool idx_expr; // allow code like `foo()['bar']` - bool include_debug; // include line numbers and file names in XHP object creation size_t first_lineno; // line number before scanning the current token size_t lineno; // current line number being scanned. std::string error; // description of error (if terminated true) bool terminated; // becomes true when the parser terminates with an error bool used; // were any XHP-specific extensions found in this code? int last_token; // the last token to be returned by the scanner int insert_token; // insert this token without reading from buffer - size_t heredoc_yyleng; // last length of yytext while scannling + size_t heredoc_yyleng; // last length of yytext while scanning const char* heredoc_data; // where our heredoc data starts std::string heredoc_label; // heredoc sentinel label std::stack curly_stack; // tokens appearing before a { bool expecting_xhp_class_statements; // when we're one level deep in a class - bool old_expecting_xhp_class_statements; // store old value while inside class method bool used_attributes; // did this class use the `attribute` keyword unsigned int list_size; bool colon_hack; + // Include line numbers and file names in XHP object creation. + bool include_debug; + + // Store old value while inside class method. + bool old_expecting_xhp_class_statements; + xhpast::token_list_t token_list; /* Utility functions for checking proper tag closing */ bool haveTag() { return !tag_stack.front().empty(); } const std::string &peekTag() { return tag_stack.front().front(); } void pushTag(const std::string &tag) { tag_stack.front().push_front(tag); } void popTag() { tag_stack.front().pop_front(); } void pushStack() { tag_stack.push_front(std::deque()); } void popStack() { tag_stack.pop_front(); } protected: std::deque > tag_stack; }; #define YYSTYPE xhpast::Node * #define YY_HEADER_EXPORT_START_CONDITIONS #define YY_EXTRA_TYPE yy_extra_type* #include "parser.yacc.hpp" #ifndef FLEX_SCANNER #include "scanner.lex.hpp" #endif int xhpparse(void*, YYSTYPE *); void xhp_new_push_state(int s, struct yyguts_t* yyg); void xhp_new_pop_state(struct yyguts_t* yyg); void xhp_set_state(int s, struct yyguts_t* yyg); diff --git a/support/xhpast/astnode.hpp b/support/xhpast/astnode.hpp index 52e34ee..e44a23e 100644 --- a/support/xhpast/astnode.hpp +++ b/support/xhpast/astnode.hpp @@ -1,99 +1,106 @@ #pragma once #include #include #include #include namespace xhpast { class Token; typedef std::list token_list_t; class Token { public: unsigned int type; std::string value; unsigned int lineno; unsigned int n; Token(unsigned int type, char *value, unsigned int n) : type(type), value(value), + lineno(0), n(n) { } }; class Node; typedef std::list node_list_t; class Node { public: unsigned int type; int l_tok; int r_tok; node_list_t children; Node() : type(0), l_tok(-1), r_tok(-1) {}; Node(unsigned int type) : type(type), l_tok(-1), r_tok(-1) {}; Node(unsigned int type, int end_tok) : type(type) { this->l_tok = end_tok; this->r_tok = end_tok; } Node(unsigned int type, int l_tok, int r_tok) : type(type), l_tok(l_tok), r_tok(r_tok) { } Node *appendChild(Node *node) { this->children.push_back(node); return this->expandRange(node); } Node *appendChildren(Node *node) { - for (node_list_t::iterator ii = node->children.begin(); ii != node->children.end(); ++ii) { + for (node_list_t::iterator ii = node->children.begin(); + ii != node->children.end(); + ++ii) { + this->appendChild(*ii); } return this; } Node *firstChild() { if (this->children.empty()) { return NULL; } return *(this->children.begin()); } Node *setType(unsigned int t) { this->type = t; return this; } Node *expandRange(Node *n) { if (!n) { - fprintf(stderr, "Trying to expandRange() a null node to one of type %d\n", this->type); + fprintf( + stderr, + "Trying to expandRange() a null node to one of type %d\n", + this->type); exit(1); }; if (n->l_tok != -1 && (n->l_tok < this->l_tok || (this->l_tok == -1))) { this->l_tok = n->l_tok; } if (n->r_tok != -1 && (n->r_tok > this->r_tok || (this->r_tok == -1))) { this->r_tok = n->r_tok; } return this; } }; } diff --git a/support/xhpast/parser.y b/support/xhpast/parser.y index 186fe54..06af207 100644 --- a/support/xhpast/parser.y +++ b/support/xhpast/parser.y @@ -1,2672 +1,2739 @@ %{ /* * If you modify this grammar, please update the version number in * ./xhpast.cpp and libphutil/src/parser/xhpast/bin/xhpast_parse.php */ #include "ast.hpp" #include "node_names.hpp" // PHP's if/else rules use right reduction rather than left reduction which // means while parsing nested if/else's the stack grows until it the last // statement is read. This is annoying, particularly because of a quirk in // bison. // http://www.gnu.org/software/bison/manual/html_node/Memory-Management.html // Apparently if you compile a bison parser with g++ it can no longer grow // the stack. The work around is to just make your initial stack ridiculously // large. Unfortunately that increases memory usage while parsing which is // dumb. Anyway, putting a TODO here to fix PHP's if/else grammar. #define YYINITDEPTH 500 %} %{ #undef yyextra #define yyextra static_cast(xhpastget_extra(yyscanner)) #undef yylineno #define yylineno yyextra->first_lineno #define push_state(s) xhp_new_push_state(s, (struct yyguts_t*) yyscanner) #define pop_state() xhp_new_pop_state((struct yyguts_t*) yyscanner) #define set_state(s) xhp_set_state(s, (struct yyguts_t*) yyscanner) #define NNEW(t) \ (new xhpast::Node(t)) #define NTYPE(n, type) \ ((n)->setType(type)) #define NMORE(n, end) \ ((n)->expandRange(end)) #define NSPAN(n, type, end) \ (NMORE(NTYPE((n), type), end)) #define NEXPAND(l, n, r) \ ((n)->expandRange(l)->expandRange(r)) using namespace std; static void yyerror(void* yyscanner, void* _, const char* error) { if (yyextra->terminated) { return; } yyextra->terminated = true; yyextra->error = error; } /* TODO: Restore this. static void replacestr(string &source, const string &find, const string &rep) { size_t j; while ((j = source.find(find)) != std::string::npos) { source.replace(j, find.length(), rep); } } */ %} %expect 5 // 2: PHP's if/else grammar // 7: expr '[' dim_offset ']' -- shift will default to first grammar %name-prefix = "xhpast" %pure-parser %parse-param { void* yyscanner } %parse-param { xhpast::Node** root } %lex-param { void* yyscanner } %error-verbose %left T_INCLUDE T_INCLUDE_ONCE T_EVAL T_REQUIRE T_REQUIRE_ONCE %left ',' %left T_LOGICAL_OR %left T_LOGICAL_XOR %left T_LOGICAL_AND %right T_PRINT -%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 +%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 %left '?' ':' %left T_BOOLEAN_OR %left T_BOOLEAN_AND %left '|' %left '^' %left '&' %nonassoc T_IS_EQUAL T_IS_NOT_EQUAL T_IS_IDENTICAL T_IS_NOT_IDENTICAL %nonassoc '<' T_IS_SMALLER_OR_EQUAL '>' T_IS_GREATER_OR_EQUAL %left T_SL T_SR %left '+' '-' '.' %left '*' '/' '%' %right '!' %nonassoc T_INSTANCEOF -%right '~' T_INC T_DEC T_INT_CAST T_DOUBLE_CAST T_STRING_CAST T_UNICODE_CAST T_BINARY_CAST T_ARRAY_CAST T_OBJECT_CAST T_BOOL_CAST T_UNSET_CAST '@' +%right '~' T_INC + T_DEC + T_INT_CAST + T_DOUBLE_CAST + T_STRING_CAST + T_UNICODE_CAST + T_BINARY_CAST + T_ARRAY_CAST + T_OBJECT_CAST + T_BOOL_CAST + T_UNSET_CAST + '@' %right '[' %nonassoc T_NEW T_CLONE %token T_EXIT %token T_IF %left T_ELSEIF %left T_ELSE %left T_ENDIF %token T_LNUMBER %token T_DNUMBER %token T_STRING %token T_STRING_VARNAME /* unused in XHP: `foo` in `"$foo"` */ %token T_VARIABLE %token T_NUM_STRING /* unused in XHP: `0` in `"$foo[0]"` */ %token T_INLINE_HTML %token T_CHARACTER /* unused in vanilla PHP */ %token T_BAD_CHARACTER /* unused in vanilla PHP */ %token T_ENCAPSED_AND_WHITESPACE /* unused in XHP: ` ` in `" "` */ -%token T_CONSTANT_ENCAPSED_STRING /* overloaded in XHP; replaces '"' encaps_list '"' */ +%token T_CONSTANT_ENCAPSED_STRING /* overloaded in XHP; + replaces '"' encaps_list '"' */ %token T_BACKTICKS_EXPR /* new in XHP; replaces '`' backticks_expr '`' */ %token T_ECHO %token T_DO %token T_WHILE %token T_ENDWHILE %token T_FOR %token T_ENDFOR %token T_FOREACH %token T_ENDFOREACH %token T_DECLARE %token T_ENDDECLARE %token T_AS %token T_SWITCH %token T_ENDSWITCH %token T_CASE %token T_DEFAULT %token T_BREAK %token T_CONTINUE %token T_GOTO %token T_FUNCTION %token T_CONST %token T_RETURN %token T_TRY %token T_CATCH %token T_THROW %token T_USE %token T_GLOBAL %right T_STATIC T_ABSTRACT T_FINAL T_PRIVATE T_PROTECTED T_PUBLIC %token T_VAR %token T_UNSET %token T_ISSET %token T_EMPTY %token T_HALT_COMPILER %token T_CLASS %token T_INTERFACE %token T_EXTENDS %token T_IMPLEMENTS %token T_OBJECT_OPERATOR %token T_DOUBLE_ARROW %token T_LIST %token T_ARRAY %token T_CLASS_C %token T_METHOD_C %token T_FUNC_C %token T_LINE %token T_FILE %token T_COMMENT %token T_DOC_COMMENT %token T_OPEN_TAG %token T_OPEN_TAG_WITH_ECHO %token T_OPEN_TAG_FAKE %token T_CLOSE_TAG %token T_WHITESPACE %token T_START_HEREDOC /* unused in XHP; replaced with T_HEREDOC */ %token T_END_HEREDOC /* unused in XHP; replaced with T_HEREDOC */ -%token T_HEREDOC /* new in XHP; replaces start_heredoc encaps_list T_END_HEREDOC */ +%token T_HEREDOC /* new in XHP; + replaces start_heredoc encaps_list T_END_HEREDOC */ %token T_DOLLAR_OPEN_CURLY_BRACES /* unused in XHP: `${` in `"${foo}"` */ %token T_CURLY_OPEN /* unused in XHP: `{$` in `"{$foo}"` */ %token T_PAAMAYIM_NEKUDOTAYIM %token T_BINARY_DOUBLE /* unsused in XHP: `b"` in `b"foo"` */ %token T_BINARY_HEREDOC /* unsused in XHP: `b<<<` in `b<<appendChild($1); } ; top_statement_list: top_statement_list top_statement { $$ = $1->appendChild($2); } | /* empty */ { $$ = NNEW(n_STATEMENT_LIST); } ; namespace_name: T_STRING { $$ = NTYPE($1, n_SYMBOL_NAME); } | namespace_name T_NS_SEPARATOR T_STRING { $$ = NMORE($1, $3); } ; top_statement: statement | function_declaration_statement | class_declaration_statement | T_HALT_COMPILER '(' ')' ';' { $1 = NSPAN($1, n_HALT_COMPILER, $3); $$ = NNEW(n_STATEMENT)->appendChild($1); NMORE($$, $4); } | T_NAMESPACE namespace_name ';' { NSPAN($1, n_NAMESPACE, $2); $1->appendChild($2); $1->appendChild(NNEW(n_EMPTY)); $$ = NNEW(n_STATEMENT)->appendChild($1); NMORE($$, $3); } | T_NAMESPACE namespace_name '{' top_statement_list '}' { NSPAN($1, n_NAMESPACE, $5); $1->appendChild($2); $1->appendChild(NEXPAND($3, $4, $5)); $$ = NNEW(n_STATEMENT)->appendChild($1); } | T_NAMESPACE '{' top_statement_list '}' { NSPAN($1, n_NAMESPACE, $4); $1->appendChild(NNEW(n_EMPTY)); NMORE($3, $4); NMORE($3, $2); $1->appendChild($3); $$ = NNEW(n_STATEMENT)->appendChild($1); } | T_USE use_declarations ';' { NSPAN($1, n_USE, $2); $1->appendChild($2); $$ = NNEW(n_STATEMENT)->appendChild($1); NMORE($$, $3); } | constant_declaration ';' { $$ = NNEW(n_STATEMENT)->appendChild($1); NMORE($$, $2); } ; use_declarations: use_declarations ',' use_declaration { $$ = $1->appendChild($3); } | use_declaration { $$ = NNEW(n_USE_LIST); $$->appendChild($1); } ; use_declaration: namespace_name { $$ = NNEW(n_USE); $$->appendChild($1); $$->appendChild(NNEW(n_EMPTY)); } | namespace_name T_AS T_STRING { $$ = NNEW(n_USE); $$->appendChild($1); NTYPE($3, n_STRING); $$->appendChild($3); } | T_NS_SEPARATOR namespace_name { $$ = NNEW(n_USE); NMORE($2, $1); $$->appendChild($2); $$->appendChild(NNEW(n_EMPTY)); } | T_NS_SEPARATOR namespace_name T_AS T_STRING { $$ = NNEW(n_USE); NMORE($2, $1); $$->appendChild($2); NTYPE($4, n_STRING); $$->appendChild($4); } ; constant_declaration: constant_declaration ',' T_STRING '=' static_scalar { NMORE($$, $5); $$->appendChild( NNEW(n_CONSTANT_DECLARATION) ->appendChild(NTYPE($3, n_STRING)) ->appendChild($5)); } | T_CONST T_STRING '=' static_scalar { NSPAN($$, n_CONSTANT_DECLARATION_LIST, $4); $$->appendChild( NNEW(n_CONSTANT_DECLARATION) ->appendChild(NTYPE($2, n_STRING)) ->appendChild($4)); } ; inner_statement_list: inner_statement_list inner_statement { $$ = $1->appendChild($2); } | /* empty */ { $$ = NNEW(n_STATEMENT_LIST); } ; inner_statement: statement | function_declaration_statement | class_declaration_statement | T_HALT_COMPILER '(' ')' ';' { $1 = NSPAN($1, n_HALT_COMPILER, $3); $$ = NNEW(n_STATEMENT)->appendChild($1); NMORE($$, $4); } ; statement: unticked_statement | T_STRING ':' { NTYPE($1, n_STRING); $$ = NNEW(n_LABEL); $$->appendChild($1); NMORE($$, $2); } | T_OPEN_TAG { $$ = NTYPE($1, n_OPEN_TAG); } | T_OPEN_TAG_WITH_ECHO { $$ = NTYPE($1, n_OPEN_TAG); } | T_CLOSE_TAG { $$ = NTYPE($1, n_CLOSE_TAG); } ; unticked_statement: '{' inner_statement_list '}' { $$ = NEXPAND($1, $2, $3); } | T_IF '(' expr ')' statement elseif_list else_single { $$ = NNEW(n_CONDITION_LIST); $1 = NTYPE($1, n_IF); $1->appendChild(NSPAN($2, n_CONTROL_CONDITION, $4)->appendChild($3)); $1->appendChild($5); $$->appendChild($1); $$->appendChildren($6); // Hacks: merge a list of if (x) { } else if (y) { } into a single condition // list instead of a condition tree. if ($7->type == n_EMPTY) { // Ignore. } else if ($7->type == n_ELSE) { xhpast::Node *stype = $7->firstChild()->firstChild(); if (stype && stype->type == n_CONDITION_LIST) { NTYPE(stype->firstChild(), n_ELSEIF); stype->firstChild()->l_tok = $7->l_tok; $$->appendChildren(stype); } else { $$->appendChild($7); } } else { $$->appendChild($7); } $$ = NNEW(n_STATEMENT)->appendChild($$); } -| T_IF '(' expr ')' ':' inner_statement_list new_elseif_list new_else_single T_ENDIF ';' { +| T_IF '(' expr ')' ':' + inner_statement_list + new_elseif_list + new_else_single + T_ENDIF ';' { $$ = NNEW(n_CONDITION_LIST); NTYPE($1, n_IF); $1->appendChild(NSPAN($2, n_CONTROL_CONDITION, $4)->appendChild($3)); $1->appendChild($6); $$->appendChild($1); $$->appendChildren($7); $$->appendChild($8); NMORE($$, $9); $$ = NNEW(n_STATEMENT)->appendChild($$); NMORE($$, $10); } | T_WHILE '(' expr ')' while_statement { NTYPE($1, n_WHILE); $1->appendChild(NSPAN($2, n_CONTROL_CONDITION, $4)->appendChild($3)); $1->appendChild($5); $$ = NNEW(n_STATEMENT)->appendChild($1); } | T_DO statement T_WHILE '(' expr ')' ';' { NTYPE($1, n_DO_WHILE); $1->appendChild($2); $1->appendChild(NSPAN($4, n_CONTROL_CONDITION, $6)->appendChild($5)); $$ = NNEW(n_STATEMENT)->appendChild($1); NMORE($$, $7); } | T_FOR '(' for_expr ';' for_expr ';' for_expr ')' for_statement { NTYPE($1, n_FOR); NSPAN($2, n_FOR_EXPRESSION, $8) ->appendChild($3) ->appendChild($5) ->appendChild($7); $1->appendChild($2); $1->appendChild($9); $$ = NNEW(n_STATEMENT)->appendChild($1); } | T_SWITCH '(' expr ')' switch_case_list { NTYPE($1, n_SWITCH); $1->appendChild(NSPAN($2, n_CONTROL_CONDITION, $4)->appendChild($3)); $1->appendChild($5); $$ = NNEW(n_STATEMENT)->appendChild($1); } | T_BREAK ';' { NTYPE($1, n_BREAK); $1->appendChild(NNEW(n_EMPTY)); $$ = NNEW(n_STATEMENT)->appendChild($1); NMORE($$, $2); } | T_BREAK expr ';' { NTYPE($1, n_BREAK); $1->appendChild($2); $$ = NNEW(n_STATEMENT)->appendChild($1); NMORE($$, $3); } | T_CONTINUE ';' { NTYPE($1, n_CONTINUE); $1->appendChild(NNEW(n_EMPTY)); $$ = NNEW(n_STATEMENT)->appendChild($1); NMORE($$, $2); } | T_CONTINUE expr ';' { NTYPE($1, n_CONTINUE); $1->appendChild($2); $$ = NNEW(n_STATEMENT)->appendChild($1); NMORE($$, $3); } | T_RETURN ';' { NTYPE($1, n_RETURN); $1->appendChild(NNEW(n_EMPTY)); $$ = NNEW(n_STATEMENT)->appendChild($1); NMORE($$, $2); } | T_RETURN expr_without_variable ';' { NTYPE($1, n_RETURN); $1->appendChild($2); $$ = NNEW(n_STATEMENT)->appendChild($1); NMORE($$, $3); } | T_RETURN variable ';' { NTYPE($1, n_RETURN); $1->appendChild($2); $$ = NNEW(n_STATEMENT)->appendChild($1); NMORE($$, $3); } | T_GLOBAL global_var_list ';' { NMORE($2, $1); $$ = NNEW(n_STATEMENT)->appendChild($2); NMORE($$, $3); } | T_STATIC static_var_list ';' { NMORE($2, $1); $$ = NNEW(n_STATEMENT)->appendChild($2); NMORE($$, $3); } | T_ECHO echo_expr_list ';' { NMORE($2, $1); $$ = NNEW(n_STATEMENT)->appendChild($2); NMORE($$, $3); } | T_INLINE_HTML { NTYPE($1, n_INLINE_HTML); $$ = $1; } | expr ';' { $$ = NNEW(n_STATEMENT)->appendChild($1); NMORE($$, $2); } | yield_expr ';' { $$ = NNEW(n_STATEMENT)->appendChild($1); NMORE($$, $2); } | T_UNSET '(' unset_variables ')' ';' { NMORE($3, $4); NMORE($3, $1); $$ = NNEW(n_STATEMENT)->appendChild($3); NMORE($$, $5); } -| T_FOREACH '(' variable T_AS foreach_variable foreach_optional_arg ')' foreach_statement { +| T_FOREACH '(' variable T_AS foreach_variable foreach_optional_arg ')' + foreach_statement { NTYPE($1, n_FOREACH); NSPAN($2, n_FOREACH_EXPRESSION, $7); $2->appendChild($3); if ($6->type == n_EMPTY) { $2->appendChild($6); $2->appendChild($5); } else { $2->appendChild($5); $2->appendChild($6); } $1->appendChild($2); $1->appendChild($8); $$ = NNEW(n_STATEMENT)->appendChild($1); } -| T_FOREACH '(' expr_without_variable T_AS variable foreach_optional_arg ')' foreach_statement { +| T_FOREACH '(' expr_without_variable T_AS variable foreach_optional_arg ')' + foreach_statement { NTYPE($1, n_FOREACH); NSPAN($2, n_FOREACH_EXPRESSION, $7); $2->appendChild($3); if ($6->type == n_EMPTY) { $2->appendChild($6); $2->appendChild($5); } else { $2->appendChild($5); $2->appendChild($6); } $1->appendChild($2); $1->appendChild($8); $$ = NNEW(n_STATEMENT)->appendChild($1); } | T_DECLARE '(' declare_list ')' declare_statement { NTYPE($1, n_DECLARE); $1->appendChild($3); $1->appendChild($5); $$ = NNEW(n_STATEMENT)->appendChild($1); } | ';' /* empty statement */ { $$ = NNEW(n_STATEMENT)->appendChild(NNEW(n_EMPTY)); NMORE($$, $1); } -| T_TRY '{' inner_statement_list '}' T_CATCH '(' fully_qualified_class_name T_VARIABLE ')' '{' inner_statement_list '}' additional_catches finally_statement { +| T_TRY '{' inner_statement_list '}' + T_CATCH '(' fully_qualified_class_name T_VARIABLE ')' + '{' inner_statement_list '}' + additional_catches + finally_statement { NTYPE($1, n_TRY); $1->appendChild(NEXPAND($2, $3, $4)); NTYPE($5, n_CATCH); $5->appendChild($7); $5->appendChild(NTYPE($8, n_VARIABLE)); $5->appendChild(NEXPAND($10, $11, $12)); $1->appendChild(NNEW(n_CATCH_LIST)->appendChild($5)->appendChildren($13)); $1->appendChild($14); $$ = NNEW(n_STATEMENT)->appendChild($1); } | T_THROW expr ';' { NTYPE($1, n_THROW); $1->appendChild($2); $$ = NNEW(n_STATEMENT)->appendChild($1); NMORE($$, $3); } | T_GOTO T_STRING ';' { NTYPE($1, n_GOTO); NTYPE($2, n_STRING); $1->appendChild($2); $$ = NNEW(n_STATEMENT)->appendChild($1); NMORE($$, $3); } ; additional_catches: non_empty_additional_catches | /* empty */ { $$ = NNEW(n_EMPTY); } ; finally_statement: /* empty */ { $$ = NNEW(n_EMPTY); } | T_FINALLY '{' inner_statement_list '}' { NTYPE($1, n_FINALLY); $1->appendChild($3); NMORE($1, $4); $$ = $1; } ; non_empty_additional_catches: additional_catch { $$ = NNEW(n_CATCH_LIST); $$->appendChild($1); } | non_empty_additional_catches additional_catch { $1->appendChild($2); $$ = $1; } ; additional_catch: - T_CATCH '(' fully_qualified_class_name T_VARIABLE ')' '{' inner_statement_list '}' { + T_CATCH '(' fully_qualified_class_name T_VARIABLE ')' + '{' inner_statement_list '}' { NTYPE($1, n_CATCH); $1->appendChild($3); $1->appendChild(NTYPE($4, n_VARIABLE)); $1->appendChild(NEXPAND($6, $7, $8)); NMORE($1, $8); $$ = $1; } ; unset_variables: unset_variable { $$ = NNEW(n_UNSET_LIST); $$->appendChild($1); } | unset_variables ',' unset_variable { $1->appendChild($3); $$ = $1; } ; unset_variable: variable ; function_declaration_statement: unticked_function_declaration_statement ; class_declaration_statement: unticked_class_declaration_statement ; is_reference: /* empty */ { $$ = NNEW(n_EMPTY); } | '&' { $$ = NTYPE($1, n_REFERENCE); } ; unticked_function_declaration_statement: - function is_reference T_STRING '(' parameter_list ')' '{' inner_statement_list '}' { + function is_reference T_STRING + '(' parameter_list ')' '{' inner_statement_list '}' { NSPAN($1, n_FUNCTION_DECLARATION, $9); $1->appendChild(NNEW(n_EMPTY)); $1->appendChild($2); $1->appendChild(NTYPE($3, n_STRING)); $1->appendChild(NEXPAND($4, $5, $6)); $$->appendChild(NNEW(n_EMPTY)); $1->appendChild(NEXPAND($7, $8, $9)); $$ = NNEW(n_STATEMENT)->appendChild($1); } ; unticked_class_declaration_statement: - class_entry_type T_STRING extends_from implements_list '{' class_statement_list '}' { + class_entry_type T_STRING extends_from implements_list + '{' class_statement_list '}' { $$ = NNEW(n_CLASS_DECLARATION); $$->appendChild($1); $$->appendChild(NTYPE($2, n_CLASS_NAME)); $$->appendChild($3); $$->appendChild($4); $$->appendChild(NEXPAND($5, $6, $7)); NMORE($$, $7); $$ = NNEW(n_STATEMENT)->appendChild($$); } | interface_entry T_STRING interface_extends_list '{' class_statement_list '}' { $$ = NNEW(n_INTERFACE_DECLARATION); $$->appendChild(NNEW(n_CLASS_ATTRIBUTES)); NMORE($$, $1); $$->appendChild(NTYPE($2, n_CLASS_NAME)); $$->appendChild($3); $$->appendChild(NNEW(n_EMPTY)); $$->appendChild(NEXPAND($4, $5, $6)); NMORE($$, $6); $$ = NNEW(n_STATEMENT)->appendChild($$); } ; class_entry_type: T_CLASS { NTYPE($1, n_CLASS_ATTRIBUTES); $$ = $1; } | T_ABSTRACT T_CLASS { NTYPE($2, n_CLASS_ATTRIBUTES); NMORE($2, $1); $2->appendChild(NTYPE($1, n_STRING)); $$ = $2; } | T_FINAL T_CLASS { NTYPE($2, n_CLASS_ATTRIBUTES); NMORE($2, $1); $2->appendChild(NTYPE($1, n_STRING)); $$ = $2; } | T_TRAIT { $$ = NNEW(n_CLASS_ATTRIBUTES); $$->appendChild(NTYPE($1, n_STRING)); } ; extends_from: /* empty */ { $$ = NNEW(n_EMPTY); } | T_EXTENDS fully_qualified_class_name { $$ = NTYPE($1, n_EXTENDS_LIST)->appendChild($2); } ; interface_entry: T_INTERFACE ; interface_extends_list: /* empty */ { $$ = NNEW(n_EMPTY); } | T_EXTENDS interface_list { NTYPE($1, n_EXTENDS_LIST); $1->appendChildren($2); $$ = $1; } ; implements_list: /* empty */ { $$ = NNEW(n_EMPTY); } | T_IMPLEMENTS interface_list { NTYPE($1, n_IMPLEMENTS_LIST); $1->appendChildren($2); $$ = $1; } ; interface_list: fully_qualified_class_name { $$ = NNEW(n_IMPLEMENTS_LIST)->appendChild($1); } | interface_list ',' fully_qualified_class_name { $$ = $1->appendChild($3); } ; foreach_optional_arg: /* empty */ { $$ = NNEW(n_EMPTY); } | T_DOUBLE_ARROW foreach_variable { $$ = $2; } ; foreach_variable: variable | '&' variable { NTYPE($1, n_VARIABLE_REFERENCE); $1->appendChild($2); $$ = $1; } ; for_statement: statement | ':' inner_statement_list T_ENDFOR ';' { NMORE($2, $1); NMORE($2, $4); $$ = $2; } ; foreach_statement: statement | ':' inner_statement_list T_ENDFOREACH ';' { NMORE($2, $1); NMORE($2, $4); $$ = $2; } ; declare_statement: statement | ':' inner_statement_list T_ENDDECLARE ';' { NMORE($2, $1); NMORE($2, $4); $$ = $2; } ; declare_list: T_STRING '=' static_scalar { $$ = NNEW(n_DECLARE_DECLARATION); $$->appendChild(NTYPE($1, n_STRING)); $$->appendChild($3); $$ = NNEW(n_DECLARE_DECLARATION_LIST)->appendChild($$); } | declare_list ',' T_STRING '=' static_scalar { $$ = NNEW(n_DECLARE_DECLARATION); $$->appendChild(NTYPE($3, n_STRING)); $$->appendChild($5); $1->appendChild($$); $$ = $1; } ; switch_case_list: '{' case_list '}' { $$ = NEXPAND($1, $2, $3); } | '{' ';' case_list '}' { // ...why does this rule exist? NTYPE($2, n_STATEMENT); $1->appendChild(NNEW(n_EMPTY)); $$ = NNEW(n_STATEMENT_LIST)->appendChild($2); $$->appendChildren($3); NEXPAND($1, $$, $4); } | ':' case_list T_ENDSWITCH ';' { NMORE($2, $4); NMORE($2, $1); $$ = $2; } | ':' ';' case_list T_ENDSWITCH ';' { NTYPE($2, n_STATEMENT); $1->appendChild(NNEW(n_EMPTY)); $$ = NNEW(n_STATEMENT_LIST)->appendChild($2); $$->appendChildren($3); NMORE($$, $5); NMORE($$, $1); } ; case_list: /* empty */ { $$ = NNEW(n_STATEMENT_LIST); } | case_list T_CASE expr case_separator inner_statement_list { NTYPE($2, n_CASE); $2->appendChild($3); $2->appendChild($5); $1->appendChild($2); $$ = $1; } | case_list T_DEFAULT case_separator inner_statement_list { NTYPE($2, n_DEFAULT); $2->appendChild($4); $1->appendChild($2); $$ = $1; } ; case_separator: ':' | ';' ; while_statement: statement | ':' inner_statement_list T_ENDWHILE ';' { NMORE($2, $4); NMORE($2, $1); $$ = $2; } ; elseif_list: /* empty */ { $$ = NNEW(n_CONDITION_LIST); } | elseif_list T_ELSEIF '(' expr ')' statement { NTYPE($2, n_ELSEIF); $2->appendChild(NSPAN($3, n_CONTROL_CONDITION, $5)->appendChild($4)); $2->appendChild($6); $$ = $1->appendChild($2); } ; new_elseif_list: /* empty */ { $$ = NNEW(n_CONDITION_LIST); } | new_elseif_list T_ELSEIF '(' expr ')' ':' inner_statement_list { NTYPE($2, n_ELSEIF); $2->appendChild($4); $2->appendChild($7); $$ = $1->appendChild($2); } ; else_single: /* empty */ { $$ = NNEW(n_EMPTY); } | T_ELSE statement { NTYPE($1, n_ELSE); $1->appendChild($2); $$ = $1; } ; new_else_single: /* empty */ { $$ = NNEW(n_EMPTY); } | T_ELSE ':' inner_statement_list { NTYPE($1, n_ELSE); $1->appendChild($3); $$ = $1; } ; parameter_list: non_empty_parameter_list | /* empty */ { $$ = NNEW(n_DECLARATION_PARAMETER_LIST); } ; non_empty_parameter_list: optional_class_type T_VARIABLE { $$ = NNEW(n_DECLARATION_PARAMETER); $$->appendChild($1); $$->appendChild(NTYPE($2, n_VARIABLE)); $$->appendChild(NNEW(n_EMPTY)); $$ = NNEW(n_DECLARATION_PARAMETER_LIST)->appendChild($$); } | optional_class_type '&' T_VARIABLE { $$ = NNEW(n_DECLARATION_PARAMETER); $$->appendChild($1); $$->appendChild(NTYPE($2, n_VARIABLE_REFERENCE)); $2->appendChild(NTYPE($3, n_VARIABLE)); $$->appendChild(NNEW(n_EMPTY)); $$ = NNEW(n_DECLARATION_PARAMETER_LIST)->appendChild($$); } | optional_class_type '&' T_VARIABLE '=' static_scalar { $$ = NNEW(n_DECLARATION_PARAMETER); $$->appendChild($1); $$->appendChild(NTYPE($2, n_VARIABLE_REFERENCE)); $2->appendChild(NTYPE($3, n_VARIABLE)); $$->appendChild($5); $$ = NNEW(n_DECLARATION_PARAMETER_LIST)->appendChild($$); } | optional_class_type T_VARIABLE '=' static_scalar { $$ = NNEW(n_DECLARATION_PARAMETER); $$->appendChild($1); $$->appendChild(NTYPE($2, n_VARIABLE)); $$->appendChild($4); $$ = NNEW(n_DECLARATION_PARAMETER_LIST)->appendChild($$); } | non_empty_parameter_list ',' optional_class_type T_VARIABLE { $$ = NNEW(n_DECLARATION_PARAMETER); $$->appendChild($3); $$->appendChild(NTYPE($4, n_VARIABLE)); $$->appendChild(NNEW(n_EMPTY)); $$ = $1->appendChild($$); } | non_empty_parameter_list ',' optional_class_type '&' T_VARIABLE { $$ = NNEW(n_DECLARATION_PARAMETER); $$->appendChild($3); $$->appendChild(NTYPE($4, n_VARIABLE_REFERENCE)); $4->appendChild(NTYPE($5, n_VARIABLE)); $$->appendChild(NNEW(n_EMPTY)); $$ = $1->appendChild($$); } -| non_empty_parameter_list ',' optional_class_type '&' T_VARIABLE '=' static_scalar { +| non_empty_parameter_list ',' optional_class_type '&' + T_VARIABLE '=' static_scalar { $$ = NNEW(n_DECLARATION_PARAMETER); $$->appendChild($3); $$->appendChild(NTYPE($4, n_VARIABLE_REFERENCE)); $4->appendChild(NTYPE($5, n_VARIABLE)); $$->appendChild($7); $$ = $1->appendChild($$); } -| non_empty_parameter_list ',' optional_class_type T_VARIABLE '=' static_scalar { +| non_empty_parameter_list ',' optional_class_type + T_VARIABLE '=' static_scalar { $$ = NNEW(n_DECLARATION_PARAMETER); $$->appendChild($3); $$->appendChild(NTYPE($4, n_VARIABLE)); $$->appendChild($6); $$ = $1->appendChild($$); } ; optional_class_type: /* empty */ { $$ = NNEW(n_EMPTY); } | fully_qualified_class_name { $$ = $1; } | T_ARRAY { $$ = NTYPE($1, n_TYPE_NAME); } | T_CALLABLE { $$ = NTYPE($1, n_TYPE_NAME); } ; function_call_parameter_list: non_empty_function_call_parameter_list | /* empty */ { $$ = NNEW(n_CALL_PARAMETER_LIST); } ; non_empty_function_call_parameter_list: expr_without_variable { $$ = NNEW(n_CALL_PARAMETER_LIST)->appendChild($1); } | variable { $$ = NNEW(n_CALL_PARAMETER_LIST)->appendChild($1); } | '&' w_variable { NTYPE($1, n_VARIABLE_REFERENCE); $1->appendChild($2); $$ = NNEW(n_CALL_PARAMETER_LIST)->appendChild($1); } | non_empty_function_call_parameter_list ',' expr_without_variable { $$ = $1->appendChild($3); } | non_empty_function_call_parameter_list ',' variable { $$ = $1->appendChild($3); } | non_empty_function_call_parameter_list ',' '&' w_variable { NTYPE($3, n_VARIABLE_REFERENCE); $3->appendChild($4); $$ = $1->appendChild($3); } ; global_var_list: global_var_list ',' global_var { $1->appendChild($3); $$ = $1; } | global_var { $$ = NNEW(n_GLOBAL_DECLARATION_LIST); $$->appendChild($1); } ; global_var: T_VARIABLE { $$ = NTYPE($1, n_VARIABLE); } | '$' r_variable { $$ = NTYPE($1, n_VARIABLE_VARIABLE); $$->appendChild($2); } | '$' '{' expr '}' { $$ = NTYPE($1, n_VARIABLE_VARIABLE); $$->appendChild($3); } ; static_var_list: static_var_list ',' T_VARIABLE { NTYPE($3, n_VARIABLE); $$ = NNEW(n_STATIC_DECLARATION); $$->appendChild($3); $$->appendChild(NNEW(n_EMPTY)); $$ = $1->appendChild($$); } | static_var_list ',' T_VARIABLE '=' static_scalar { NTYPE($3, n_VARIABLE); $$ = NNEW(n_STATIC_DECLARATION); $$->appendChild($3); $$->appendChild($5); $$ = $1->appendChild($$); } | T_VARIABLE { NTYPE($1, n_VARIABLE); $$ = NNEW(n_STATIC_DECLARATION); $$->appendChild($1); $$->appendChild(NNEW(n_EMPTY)); $$ = NNEW(n_STATIC_DECLARATION_LIST)->appendChild($$); } | T_VARIABLE '=' static_scalar { NTYPE($1, n_VARIABLE); $$ = NNEW(n_STATIC_DECLARATION); $$->appendChild($1); $$->appendChild($3); $$ = NNEW(n_STATIC_DECLARATION_LIST)->appendChild($$); } ; class_statement_list: class_statement_list class_statement { $$ = $1->appendChild($2); } | /* empty */ { $$ = NNEW(n_STATEMENT_LIST); } ; class_statement: variable_modifiers class_variable_declaration ';' { $$ = NNEW(n_CLASS_MEMBER_DECLARATION_LIST); $$->appendChild($1); $$->appendChildren($2); $$ = NNEW(n_STATEMENT)->appendChild($$); NMORE($$, $3); } | class_constant_declaration ';' { $$ = NNEW(n_STATEMENT)->appendChild($1); NMORE($$, $2); } | trait_use_statement { $$ = $1; } | method_modifiers function { - yyextra->old_expecting_xhp_class_statements = yyextra->expecting_xhp_class_statements; + yyextra->old_expecting_xhp_class_statements = + yyextra->expecting_xhp_class_statements; yyextra->expecting_xhp_class_statements = false; } is_reference T_STRING '(' parameter_list ')' method_body { - yyextra->expecting_xhp_class_statements = yyextra->old_expecting_xhp_class_statements; + yyextra->expecting_xhp_class_statements = + yyextra->old_expecting_xhp_class_statements; $$ = NNEW(n_METHOD_DECLARATION); NMORE($$, $2); $$->appendChild($1); $$->appendChild($4); $$->appendChild(NTYPE($5, n_STRING)); $$->appendChild(NEXPAND($6, $7, $8)); $$->appendChild(NNEW(n_EMPTY)); $$->appendChild($9); $$ = NNEW(n_STATEMENT)->appendChild($$); } ; trait_use_statement: T_USE trait_list trait_adaptations { $$ = NTYPE($1, n_TRAIT_USE); $$->appendChildren($2); $$->appendChild($3); } ; trait_list: fully_qualified_class_name { $$ = NNEW(n_TRAIT_USE_LIST)->appendChild($1); } | trait_list ',' fully_qualified_class_name { $$ = $1->appendChild($3); } ; trait_adaptations: ';' { $$ = NNEW(n_EMPTY); } | '{' trait_adaptation_list '}' { $$ = NEXPAND($1, $2, $3); } ; trait_adaptation_list: /* empty */ { $$ = NNEW(n_TRAIT_ADAPTATION_LIST); } | non_empty_trait_adaptation_list { $$ = $1; } ; non_empty_trait_adaptation_list: trait_adaptation_statement { $$ = NNEW(n_TRAIT_ADAPTATION_LIST); $$->appendChild($1); } | non_empty_trait_adaptation_list trait_adaptation_statement { $1->appendChild($2); $$ = $1; } ; trait_adaptation_statement: trait_precedence ';' { $$ = NMORE($1, $2); } | trait_alias ';' { $$ = NMORE($1, $2); } ; trait_precedence: trait_method_reference_fully_qualified T_INSTEADOF trait_reference_list { $$ = NNEW(n_TRAIT_INSTEADOF); $$->appendChild($1); $$->appendChild($3); } ; trait_reference_list: fully_qualified_class_name { $$ = NNEW(n_TRAIT_REFERENCE_LIST); $$->appendChild($1); } | trait_reference_list ',' fully_qualified_class_name { $1->appendChild($3); $$ = $1; } ; trait_method_reference: T_STRING { $$ = NNEW(n_TRAIT_METHOD_REFERENCE); $$->appendChild(NTYPE($1, n_STRING)); } | trait_method_reference_fully_qualified { $$ = $1; } ; trait_method_reference_fully_qualified: fully_qualified_class_name T_PAAMAYIM_NEKUDOTAYIM T_STRING { NTYPE($2, n_TRAIT_METHOD_REFERENCE); NEXPAND($1, $2, NTYPE($3, n_STRING)); $$ = $2; } ; trait_alias: trait_method_reference T_AS trait_modifiers T_STRING { $$ = NNEW(n_TRAIT_AS); $$->appendChild($1); $$->appendChild($3); $$->appendChild(NTYPE($4, n_STRING)); } | trait_method_reference T_AS member_modifier { $$ = NNEW(n_TRAIT_AS); $$->appendChild($1); $$->appendChild($3); $$->appendChild(NNEW(n_EMPTY)); } ; trait_modifiers: /* empty */ { $$ = NNEW(n_EMPTY); } | member_modifier { $$ = NNEW(n_METHOD_MODIFIER_LIST); $$->appendChild(NTYPE($1, n_STRING)); } ; method_body: ';' /* abstract method */ { $$ = NNEW(n_EMPTY); } | '{' inner_statement_list '}' { $$ = NEXPAND($1, $2, $3); } ; variable_modifiers: non_empty_member_modifiers | T_VAR { $$ = NNEW(n_CLASS_MEMBER_MODIFIER_LIST); $$->appendChild(NTYPE($1, n_STRING)); } ; method_modifiers: /* empty */ { $$ = NNEW(n_METHOD_MODIFIER_LIST); } | non_empty_member_modifiers { NTYPE($1, n_METHOD_MODIFIER_LIST); $$ = $1; } ; non_empty_member_modifiers: member_modifier { $$ = NNEW(n_CLASS_MEMBER_MODIFIER_LIST); $$->appendChild(NTYPE($1, n_STRING)); } | non_empty_member_modifiers member_modifier { $$ = $1->appendChild(NTYPE($2, n_STRING)); } ; member_modifier: T_PUBLIC | T_PROTECTED | T_PRIVATE | T_STATIC | T_ABSTRACT | T_FINAL ; class_variable_declaration: class_variable_declaration ',' T_VARIABLE { $$ = NNEW(n_CLASS_MEMBER_DECLARATION); $$->appendChild(NTYPE($3, n_VARIABLE)); $$->appendChild(NNEW(n_EMPTY)); $$ = $1->appendChild($$); } | class_variable_declaration ',' T_VARIABLE '=' static_scalar { $$ = NNEW(n_CLASS_MEMBER_DECLARATION); $$->appendChild(NTYPE($3, n_VARIABLE)); $$->appendChild($5); $$ = $1->appendChild($$); } | T_VARIABLE { $$ = NNEW(n_CLASS_MEMBER_DECLARATION); $$->appendChild(NTYPE($1, n_VARIABLE)); $$->appendChild(NNEW(n_EMPTY)); $$ = NNEW(n_CLASS_MEMBER_DECLARATION_LIST)->appendChild($$); } | T_VARIABLE '=' static_scalar { $$ = NNEW(n_CLASS_MEMBER_DECLARATION); $$->appendChild(NTYPE($1, n_VARIABLE)); $$->appendChild($3); $$ = NNEW(n_CLASS_MEMBER_DECLARATION_LIST)->appendChild($$); } ; class_constant_declaration: class_constant_declaration ',' T_STRING '=' static_scalar { $$ = NNEW(n_CLASS_CONSTANT_DECLARATION); $$->appendChild(NTYPE($3, n_STRING)); $$->appendChild($5); $1->appendChild($$); $$ = $1; } | T_CONST T_STRING '=' static_scalar { NTYPE($1, n_CLASS_CONSTANT_DECLARATION_LIST); $$ = NNEW(n_CLASS_CONSTANT_DECLARATION); $$->appendChild(NTYPE($2, n_STRING)); $$->appendChild($4); $1->appendChild($$); $$ = $1; } ; echo_expr_list: echo_expr_list ',' expr { $1->appendChild($3); } | expr { $$ = NNEW(n_ECHO_LIST); $$->appendChild($1); } ; for_expr: /* empty */ { $$ = NNEW(n_EMPTY); } | non_empty_for_expr ; non_empty_for_expr: non_empty_for_expr ',' expr { $1->appendChild($3); } | expr { $$ = NNEW(n_EXPRESSION_LIST); $$->appendChild($1); } ; expr_without_variable: T_LIST '(' assignment_list ')' '=' expr { NTYPE($1, n_LIST); $1->appendChild(NEXPAND($2, $3, $4)); $$ = NNEW(n_BINARY_EXPRESSION); $$->appendChild($1); $$->appendChild(NTYPE($5, n_OPERATOR)); $$->appendChild($6); } | variable '=' expr { $$ = NNEW(n_BINARY_EXPRESSION); $$->appendChild($1); $$->appendChild(NTYPE($2, n_OPERATOR)); $$->appendChild($3); } | variable '=' '&' variable { $$ = NNEW(n_BINARY_EXPRESSION); $$->appendChild($1); $$->appendChild(NTYPE($2, n_OPERATOR)); NTYPE($3, n_VARIABLE_REFERENCE); $3->appendChild($4); $$->appendChild($3); } | variable '=' '&' T_NEW class_name_reference ctor_arguments { $$ = NNEW(n_BINARY_EXPRESSION); $$->appendChild($1); $$->appendChild(NTYPE($2, n_OPERATOR)); NTYPE($4, n_NEW); $4->appendChild($5); $4->appendChild($6); NTYPE($3, n_VARIABLE_REFERENCE); $3->appendChild($4); $$->appendChild($3); } | T_CLONE expr { $$ = NNEW(n_UNARY_PREFIX_EXPRESSION); $$->appendChild(NTYPE($1, n_OPERATOR)); $$->appendChild($2); } | variable T_PLUS_EQUAL expr { $$ = NNEW(n_BINARY_EXPRESSION); $$->appendChild($1); $$->appendChild(NTYPE($2, n_OPERATOR)); $$->appendChild($3); } | variable T_MINUS_EQUAL expr { $$ = NNEW(n_BINARY_EXPRESSION); $$->appendChild($1); $$->appendChild(NTYPE($2, n_OPERATOR)); $$->appendChild($3); } | variable T_MUL_EQUAL expr { $$ = NNEW(n_BINARY_EXPRESSION); $$->appendChild($1); $$->appendChild(NTYPE($2, n_OPERATOR)); $$->appendChild($3); } | variable T_DIV_EQUAL expr { $$ = NNEW(n_BINARY_EXPRESSION); $$->appendChild($1); $$->appendChild(NTYPE($2, n_OPERATOR)); $$->appendChild($3); } | variable T_CONCAT_EQUAL expr { $$ = NNEW(n_BINARY_EXPRESSION); $$->appendChild($1); $$->appendChild(NTYPE($2, n_OPERATOR)); $$->appendChild($3); } | variable T_MOD_EQUAL expr { $$ = NNEW(n_BINARY_EXPRESSION); $$->appendChild($1); $$->appendChild(NTYPE($2, n_OPERATOR)); $$->appendChild($3); } | variable T_AND_EQUAL expr { $$ = NNEW(n_BINARY_EXPRESSION); $$->appendChild($1); $$->appendChild(NTYPE($2, n_OPERATOR)); $$->appendChild($3); } | variable T_OR_EQUAL expr { $$ = NNEW(n_BINARY_EXPRESSION); $$->appendChild($1); $$->appendChild(NTYPE($2, n_OPERATOR)); $$->appendChild($3); } | variable T_XOR_EQUAL expr { $$ = NNEW(n_BINARY_EXPRESSION); $$->appendChild($1); $$->appendChild(NTYPE($2, n_OPERATOR)); $$->appendChild($3); } | variable T_SL_EQUAL expr { $$ = NNEW(n_BINARY_EXPRESSION); $$->appendChild($1); $$->appendChild(NTYPE($2, n_OPERATOR)); $$->appendChild($3); } | variable T_SR_EQUAL expr { $$ = NNEW(n_BINARY_EXPRESSION); $$->appendChild($1); $$->appendChild(NTYPE($2, n_OPERATOR)); $$->appendChild($3); } | rw_variable T_INC { $$ = NNEW(n_UNARY_POSTFIX_EXPRESSION); $$->appendChild($1); $$->appendChild(NTYPE($2, n_OPERATOR)); } | T_INC rw_variable { $$ = NNEW(n_UNARY_PREFIX_EXPRESSION); $$->appendChild(NTYPE($1, n_OPERATOR)); $$->appendChild($2); } | rw_variable T_DEC { $$ = NNEW(n_UNARY_POSTFIX_EXPRESSION); $$->appendChild($1); $$->appendChild(NTYPE($2, n_OPERATOR)); } | T_DEC rw_variable { $$ = NNEW(n_UNARY_PREFIX_EXPRESSION); $$->appendChild(NTYPE($1, n_OPERATOR)); $$->appendChild($2); } | expr T_BOOLEAN_OR expr { $$ = NNEW(n_BINARY_EXPRESSION); $$->appendChild($1); $$->appendChild(NTYPE($2, n_OPERATOR)); $$->appendChild($3); } | expr T_BOOLEAN_AND expr { $$ = NNEW(n_BINARY_EXPRESSION); $$->appendChild($1); $$->appendChild(NTYPE($2, n_OPERATOR)); $$->appendChild($3); } | expr T_LOGICAL_OR expr { $$ = NNEW(n_BINARY_EXPRESSION); $$->appendChild($1); $$->appendChild(NTYPE($2, n_OPERATOR)); $$->appendChild($3); } | expr T_LOGICAL_AND expr { $$ = NNEW(n_BINARY_EXPRESSION); $$->appendChild($1); $$->appendChild(NTYPE($2, n_OPERATOR)); $$->appendChild($3); } | expr T_LOGICAL_XOR expr { $$ = NNEW(n_BINARY_EXPRESSION); $$->appendChild($1); $$->appendChild(NTYPE($2, n_OPERATOR)); $$->appendChild($3); } | expr '|' expr { $$ = NNEW(n_BINARY_EXPRESSION); $$->appendChild($1); $$->appendChild(NTYPE($2, n_OPERATOR)); $$->appendChild($3); } | expr '&' expr { $$ = NNEW(n_BINARY_EXPRESSION); $$->appendChild($1); $$->appendChild(NTYPE($2, n_OPERATOR)); $$->appendChild($3); } | expr '^' expr { $$ = NNEW(n_BINARY_EXPRESSION); $$->appendChild($1); $$->appendChild(NTYPE($2, n_OPERATOR)); $$->appendChild($3); } | expr '.' expr { /* The concatenation operator generates n_CONCATENATION_LIST instead of n_BINARY_EXPRESSION because we tend to run into stack depth issues in a lot of real-world cases otherwise (e.g., in PHP and JSON decoders). */ if ($1->type == n_CONCATENATION_LIST && $3->type == n_CONCATENATION_LIST) { $1->appendChild(NTYPE($2, n_OPERATOR)); $1->appendChildren($3); $$ = $1; } else if ($1->type == n_CONCATENATION_LIST) { $1->appendChild(NTYPE($2, n_OPERATOR)); $1->appendChild($3); $$ = $1; } else if ($3->type == n_CONCATENATION_LIST) { $$ = NNEW(n_CONCATENATION_LIST); $$->appendChild($1); $$->appendChild(NTYPE($2, n_OPERATOR)); $$->appendChildren($3); } else { $$ = NNEW(n_CONCATENATION_LIST); $$->appendChild($1); $$->appendChild(NTYPE($2, n_OPERATOR)); $$->appendChild($3); } } | expr '+' expr { $$ = NNEW(n_BINARY_EXPRESSION); $$->appendChild($1); $$->appendChild(NTYPE($2, n_OPERATOR)); $$->appendChild($3); } | expr '-' expr { $$ = NNEW(n_BINARY_EXPRESSION); $$->appendChild($1); $$->appendChild(NTYPE($2, n_OPERATOR)); $$->appendChild($3); } | expr '*' expr { $$ = NNEW(n_BINARY_EXPRESSION); $$->appendChild($1); $$->appendChild(NTYPE($2, n_OPERATOR)); $$->appendChild($3); } | expr '/' expr { $$ = NNEW(n_BINARY_EXPRESSION); $$->appendChild($1); $$->appendChild(NTYPE($2, n_OPERATOR)); $$->appendChild($3); } | expr '%' expr { $$ = NNEW(n_BINARY_EXPRESSION); $$->appendChild($1); $$->appendChild(NTYPE($2, n_OPERATOR)); $$->appendChild($3); } | expr T_SL expr { $$ = NNEW(n_BINARY_EXPRESSION); $$->appendChild($1); $$->appendChild(NTYPE($2, n_OPERATOR)); $$->appendChild($3); } | expr T_SR expr { $$ = NNEW(n_BINARY_EXPRESSION); $$->appendChild($1); $$->appendChild(NTYPE($2, n_OPERATOR)); $$->appendChild($3); } | '+' expr %prec T_INC { $$ = NNEW(n_UNARY_PREFIX_EXPRESSION); $$->appendChild(NTYPE($1, n_OPERATOR)); $$->appendChild($2); } | '-' expr %prec T_INC { $$ = NNEW(n_UNARY_PREFIX_EXPRESSION); $$->appendChild(NTYPE($1, n_OPERATOR)); $$->appendChild($2); } | '!' expr { $$ = NNEW(n_UNARY_PREFIX_EXPRESSION); $$->appendChild(NTYPE($1, n_OPERATOR)); $$->appendChild($2); } | '~' expr { $$ = NNEW(n_UNARY_PREFIX_EXPRESSION); $$->appendChild(NTYPE($1, n_OPERATOR)); $$->appendChild($2); } | expr T_IS_IDENTICAL expr { $$ = NNEW(n_BINARY_EXPRESSION); $$->appendChild($1); $$->appendChild(NTYPE($2, n_OPERATOR)); $$->appendChild($3); } | expr T_IS_NOT_IDENTICAL expr { $$ = NNEW(n_BINARY_EXPRESSION); $$->appendChild($1); $$->appendChild(NTYPE($2, n_OPERATOR)); $$->appendChild($3); } | expr T_IS_EQUAL expr { $$ = NNEW(n_BINARY_EXPRESSION); $$->appendChild($1); $$->appendChild(NTYPE($2, n_OPERATOR)); $$->appendChild($3); } | expr T_IS_NOT_EQUAL expr { $$ = NNEW(n_BINARY_EXPRESSION); $$->appendChild($1); $$->appendChild(NTYPE($2, n_OPERATOR)); $$->appendChild($3); } | expr '<' expr { $$ = NNEW(n_BINARY_EXPRESSION); $$->appendChild($1); $$->appendChild(NTYPE($2, n_OPERATOR)); $$->appendChild($3); } | expr T_IS_SMALLER_OR_EQUAL expr { $$ = NNEW(n_BINARY_EXPRESSION); $$->appendChild($1); $$->appendChild(NTYPE($2, n_OPERATOR)); $$->appendChild($3); } | expr '>' expr { $$ = NNEW(n_BINARY_EXPRESSION); $$->appendChild($1); $$->appendChild(NTYPE($2, n_OPERATOR)); $$->appendChild($3); } | expr T_IS_GREATER_OR_EQUAL expr { $$ = NNEW(n_BINARY_EXPRESSION); $$->appendChild($1); $$->appendChild(NTYPE($2, n_OPERATOR)); $$->appendChild($3); } | expr T_INSTANCEOF class_name_reference { $$ = NNEW(n_BINARY_EXPRESSION); $$->appendChild($1); $$->appendChild(NTYPE($2, n_OPERATOR)); $$->appendChild($3); } | parenthesis_expr | new_expr | expr '?' expr ':' expr { $$ = NNEW(n_TERNARY_EXPRESSION); $$->appendChild($1); $$->appendChild($3); $$->appendChild($5); } | expr '?' ':' expr { $$ = NNEW(n_TERNARY_EXPRESSION); $$->appendChild($1); $$->appendChild(NNEW(n_EMPTY)); $$->appendChild($4); } | internal_functions_in_yacc | T_INT_CAST expr { $$ = NNEW(n_CAST_EXPRESSION); $$->appendChild(NTYPE($1, n_CAST)); $$->appendChild($2); } | T_DOUBLE_CAST expr { $$ = NNEW(n_CAST_EXPRESSION); $$->appendChild(NTYPE($1, n_CAST)); $$->appendChild($2); } | T_STRING_CAST expr { $$ = NNEW(n_CAST_EXPRESSION); $$->appendChild(NTYPE($1, n_CAST)); $$->appendChild($2); } | T_ARRAY_CAST expr { $$ = NNEW(n_CAST_EXPRESSION); $$->appendChild(NTYPE($1, n_CAST)); $$->appendChild($2); } | T_OBJECT_CAST expr { $$ = NNEW(n_CAST_EXPRESSION); $$->appendChild(NTYPE($1, n_CAST)); $$->appendChild($2); } | T_BOOL_CAST expr { $$ = NNEW(n_CAST_EXPRESSION); $$->appendChild(NTYPE($1, n_CAST)); $$->appendChild($2); } | T_UNSET_CAST expr { $$ = NNEW(n_CAST_EXPRESSION); $$->appendChild(NTYPE($1, n_CAST)); $$->appendChild($2); } | T_EXIT exit_expr { $$ = NNEW(n_UNARY_PREFIX_EXPRESSION); $$->appendChild(NTYPE($1, n_OPERATOR)); $$->appendChild($2); } | '@' expr { $$ = NNEW(n_UNARY_PREFIX_EXPRESSION); $$->appendChild(NTYPE($1, n_OPERATOR)); $$->appendChild($2); } | T_BACKTICKS_EXPR { NTYPE($1, n_BACKTICKS_EXPRESSION); $$ = $1; } | scalar | combined_scalar_offset | combined_scalar | T_PRINT expr { $$ = NNEW(n_UNARY_PREFIX_EXPRESSION); $$->appendChild(NTYPE($1, n_OPERATOR)); $$->appendChild($2); } | T_YIELD { NTYPE($1, n_YIELD); $1->appendChild(NNEW(n_EMPTY)); $1->appendChild(NNEW(n_EMPTY)); $$ = $1; } -| function is_reference '(' parameter_list ')' lexical_vars '{' inner_statement_list '}' { +| function is_reference + '(' parameter_list ')' + lexical_vars + '{' inner_statement_list '}' { NSPAN($1, n_FUNCTION_DECLARATION, $9); $1->appendChild(NNEW(n_EMPTY)); $1->appendChild($2); $1->appendChild(NNEW(n_EMPTY)); $1->appendChild(NEXPAND($3, $4, $5)); $$->appendChild($6); $1->appendChild(NEXPAND($7, $8, $9)); $$ = $1; } -| T_STATIC function is_reference '(' parameter_list ')' lexical_vars '{' inner_statement_list '}' { +| T_STATIC function is_reference + '(' parameter_list ')' + lexical_vars + '{' inner_statement_list '}' { NSPAN($2, n_FUNCTION_DECLARATION, $10); NMORE($2, $1); $$ = NNEW(n_FUNCTION_MODIFIER_LIST); $$->appendChild(NTYPE($1, n_STRING)); $2->appendChild($1); $2->appendChild(NNEW(n_EMPTY)); $2->appendChild($3); $2->appendChild(NNEW(n_EMPTY)); $2->appendChild(NEXPAND($4, $5, $6)); $2->appendChild($7); $2->appendChild(NEXPAND($8, $9, $10)); $$ = $2; } ; yield_expr: T_YIELD expr_without_variable { NTYPE($1, n_YIELD); $2->appendChild(NNEW(n_EMPTY)); $1->appendChild($2); $$ = $1; } | T_YIELD variable { NTYPE($1, n_YIELD); $2->appendChild(NNEW(n_EMPTY)); $1->appendChild($2); $$ = $1; } | T_YIELD expr T_DOUBLE_ARROW expr_without_variable { NTYPE($1, n_YIELD); $1->appendChild($2); $1->appendChild($4); $$ = $1; } | T_YIELD expr T_DOUBLE_ARROW variable { NTYPE($1, n_YIELD); $1->appendChild($2); $1->appendChild($4); $$ = $1; } ; function: T_FUNCTION ; lexical_vars: /* empty */ { $$ = NNEW(n_EMPTY); } | T_USE '(' lexical_var_list ')' { NTYPE($1, n_LEXICAL_VARIABLE_LIST); $1->appendChildren($3); $$ = $1; } ; lexical_var_list: lexical_var_list ',' T_VARIABLE { $$ = $1->appendChild(NTYPE($3, n_VARIABLE)); } | lexical_var_list ',' '&' T_VARIABLE { NTYPE($3, n_VARIABLE_REFERENCE); $3->appendChild(NTYPE($4, n_VARIABLE)); $$ = $1->appendChild($3); } | T_VARIABLE { $$ = NNEW(n_LEXICAL_VARIABLE_LIST); $$->appendChild(NTYPE($1, n_VARIABLE)); } | '&' T_VARIABLE { NTYPE($1, n_VARIABLE_REFERENCE); $1->appendChild(NTYPE($2, n_VARIABLE)); $$ = NNEW(n_LEXICAL_VARIABLE_LIST); $$->appendChild($1); } ; function_call: namespace_name '(' function_call_parameter_list ')' { $$ = NNEW(n_FUNCTION_CALL); $$->appendChild($1); $$->appendChild(NEXPAND($2, $3, $4)); } -| T_NAMESPACE T_NS_SEPARATOR namespace_name '(' function_call_parameter_list ')' { +| T_NAMESPACE T_NS_SEPARATOR namespace_name + '(' function_call_parameter_list ')' { NMORE($3, $1); $$ = NNEW(n_FUNCTION_CALL); $$->appendChild($3); $$->appendChild(NEXPAND($4, $5, $6)); } | T_NS_SEPARATOR namespace_name '(' function_call_parameter_list ')' { NMORE($2, $1); $$ = NNEW(n_FUNCTION_CALL); $$->appendChild($2); $$->appendChild(NEXPAND($3, $4, $5)); } -| class_name T_PAAMAYIM_NEKUDOTAYIM T_STRING '(' function_call_parameter_list ')' { +| class_name T_PAAMAYIM_NEKUDOTAYIM T_STRING + '(' function_call_parameter_list ')' { $$ = NNEW(n_CLASS_STATIC_ACCESS); $$->appendChild($1); $$->appendChild(NTYPE($3, n_STRING)); $$ = NNEW(n_FUNCTION_CALL)->appendChild($$); $$->appendChild(NEXPAND($4, $5, $6)); } -| variable_class_name T_PAAMAYIM_NEKUDOTAYIM T_STRING '(' function_call_parameter_list ')' { +| variable_class_name T_PAAMAYIM_NEKUDOTAYIM T_STRING + '(' function_call_parameter_list ')' { $$ = NNEW(n_CLASS_STATIC_ACCESS); $$->appendChild($1); $$->appendChild(NTYPE($3, n_STRING)); $$ = NNEW(n_FUNCTION_CALL)->appendChild($$); $$->appendChild(NEXPAND($4, $5, $6)); } -| variable_class_name T_PAAMAYIM_NEKUDOTAYIM variable_without_objects '(' function_call_parameter_list ')' { +| variable_class_name T_PAAMAYIM_NEKUDOTAYIM variable_without_objects + '(' function_call_parameter_list ')' { $$ = NNEW(n_CLASS_STATIC_ACCESS); $$->appendChild($1); $$->appendChild(NTYPE($3, n_STRING)); $$ = NNEW(n_FUNCTION_CALL)->appendChild($$); $$->appendChild(NEXPAND($4, $5, $6)); } -| class_name T_PAAMAYIM_NEKUDOTAYIM variable_without_objects '(' function_call_parameter_list ')' { +| class_name T_PAAMAYIM_NEKUDOTAYIM variable_without_objects + '(' function_call_parameter_list ')' { $$ = NNEW(n_CLASS_STATIC_ACCESS); $$->appendChild($1); $$->appendChild(NTYPE($3, n_STRING)); $$ = NNEW(n_FUNCTION_CALL)->appendChild($$); $$->appendChild(NEXPAND($4, $5, $6)); } | variable_without_objects '(' function_call_parameter_list ')' { $$ = NNEW(n_FUNCTION_CALL); $$->appendChild($1); $$->appendChild(NEXPAND($2, $3, $4)); } ; class_name: T_STATIC { $$ = NTYPE($1, n_CLASS_NAME); } | namespace_name { $$ = NTYPE($1, n_CLASS_NAME); } | T_NAMESPACE T_NS_SEPARATOR namespace_name { NMORE($3, $1); $$ = NTYPE($3, n_CLASS_NAME); } | T_NS_SEPARATOR namespace_name { NMORE($2, $1); $$ = NTYPE($2, n_CLASS_NAME); } ; fully_qualified_class_name: namespace_name { $$ = NTYPE($1, n_CLASS_NAME); } | T_NAMESPACE T_NS_SEPARATOR namespace_name { NMORE($3, $1); $$ = NTYPE($3, n_CLASS_NAME); } | T_NS_SEPARATOR namespace_name { NMORE($2, $1); $$ = NTYPE($2, n_CLASS_NAME); } ; class_name_reference: class_name | dynamic_class_name_reference ; dynamic_class_name_reference: - base_variable T_OBJECT_OPERATOR object_property dynamic_class_name_variable_properties { + base_variable + T_OBJECT_OPERATOR + object_property + dynamic_class_name_variable_properties { $$ = NNEW(n_OBJECT_PROPERTY_ACCESS); $$->appendChild($1); $$->appendChild($3); - for (xhpast::node_list_t::iterator ii = $4->children.begin(); ii != $4->children.end(); ++ii) { + for (xhpast::node_list_t::iterator ii = $4->children.begin(); + ii != $4->children.end(); + ++ii) { + $$ = NNEW(n_OBJECT_PROPERTY_ACCESS)->appendChild($$); $$->appendChild(*ii); } } | base_variable ; dynamic_class_name_variable_properties: dynamic_class_name_variable_properties dynamic_class_name_variable_property { $$ = $1->appendChild($2); } | /* empty */ { $$ = NNEW(n_EMPTY); } ; dynamic_class_name_variable_property: T_OBJECT_OPERATOR object_property { $$ = $2; } ; exit_expr: /* empty */ { $$ = NNEW(n_EMPTY); } | '(' ')' { NSPAN($1, n_EMPTY, $2); $$ = $1; } | '(' expr ')' { NSPAN($1, n_PARENTHETICAL_EXPRESSION, $3); $1->appendChild($2); $$ = $1; } ; ctor_arguments: /* empty */ { $$ = NNEW(n_EMPTY); } | '(' function_call_parameter_list ')' { $$ = NEXPAND($1, $2, $3); } ; common_scalar: T_LNUMBER { $$ = NTYPE($1, n_NUMERIC_SCALAR); } | T_DNUMBER { $$ = NTYPE($1, n_NUMERIC_SCALAR); } | T_CONSTANT_ENCAPSED_STRING { $$ = NTYPE($1, n_STRING_SCALAR); } | T_LINE { $$ = NTYPE($1, n_MAGIC_SCALAR); } | T_FILE { $$ = NTYPE($1, n_MAGIC_SCALAR); } | T_DIR { $$ = NTYPE($1, n_MAGIC_SCALAR); } | T_CLASS_C { $$ = NTYPE($1, n_MAGIC_SCALAR); } | T_METHOD_C { $$ = NTYPE($1, n_MAGIC_SCALAR); } | T_TRAIT_C { $$ = NTYPE($1, n_MAGIC_SCALAR); } | T_FUNC_C { $$ = NTYPE($1, n_MAGIC_SCALAR); } | T_NS_C { $$ = NTYPE($1, n_MAGIC_SCALAR); } | T_HEREDOC { $$ = NTYPE($1, n_HEREDOC); } ; static_scalar: /* compile-time evaluated scalars */ common_scalar | namespace_name | T_NAMESPACE T_NS_SEPARATOR namespace_name { NMORE($3, $1); $$ = $3; } | T_NS_SEPARATOR namespace_name { NMORE($2, $1); $$ = $2; } | '+' static_scalar { $$ = NNEW(n_UNARY_PREFIX_EXPRESSION); $$->appendChild(NTYPE($1, n_OPERATOR)); $$->appendChild($2); } | '-' static_scalar { $$ = NNEW(n_UNARY_PREFIX_EXPRESSION); $$->appendChild(NTYPE($1, n_OPERATOR)); $$->appendChild($2); } | T_ARRAY '(' static_array_pair_list ')' { NTYPE($1, n_ARRAY_LITERAL); $1->appendChild($3); NMORE($1, $4); $$ = $1; } | '[' static_array_pair_list ']' { NTYPE($1, n_ARRAY_LITERAL); $1->appendChild($2); NMORE($1, $3); $$ = $1; } | static_class_constant ; static_class_constant: class_name T_PAAMAYIM_NEKUDOTAYIM T_STRING { $$ = NNEW(n_CLASS_STATIC_ACCESS); $$->appendChild($1); $$->appendChild(NTYPE($3, n_STRING)); } ; scalar: T_STRING_VARNAME | class_constant | namespace_name | T_NAMESPACE T_NS_SEPARATOR namespace_name { $$ = NMORE($3, $1); } | T_NS_SEPARATOR namespace_name { $$ = NMORE($2, $1); } | common_scalar ; static_array_pair_list: /* empty */ { $$ = NNEW(n_ARRAY_VALUE_LIST); } | non_empty_static_array_pair_list possible_comma { $$ = NMORE($1, $2); } ; possible_comma: /* empty */ { $$ = NNEW(n_EMPTY); } | ',' ; non_empty_static_array_pair_list: - non_empty_static_array_pair_list ',' static_scalar T_DOUBLE_ARROW static_scalar { + non_empty_static_array_pair_list + ',' + static_scalar + T_DOUBLE_ARROW + static_scalar { $$ = NNEW(n_ARRAY_VALUE); $$->appendChild($3); $$->appendChild($5); $$ = $1->appendChild($$); } | non_empty_static_array_pair_list ',' static_scalar { $$ = NNEW(n_ARRAY_VALUE); $$->appendChild(NNEW(n_EMPTY)); $$->appendChild($3); $$ = $1->appendChild($$); } | static_scalar T_DOUBLE_ARROW static_scalar { $$ = NNEW(n_ARRAY_VALUE); $$->appendChild($1); $$->appendChild($3); $$ = NNEW(n_ARRAY_VALUE_LIST)->appendChild($$); } | static_scalar { $$ = NNEW(n_ARRAY_VALUE); $$->appendChild(NNEW(n_EMPTY)); $$->appendChild($1); $$ = NNEW(n_ARRAY_VALUE_LIST)->appendChild($$); } ; expr: r_variable | expr_without_variable ; r_variable: variable ; w_variable: variable ; rw_variable: variable ; variable: - base_variable_with_function_calls T_OBJECT_OPERATOR object_property method_or_not variable_properties { + base_variable_with_function_calls + T_OBJECT_OPERATOR + object_property method_or_not + variable_properties { $$ = NNEW(n_OBJECT_PROPERTY_ACCESS); $$->appendChild($1); $$->appendChild($3); if ($4->type != n_EMPTY) { $$ = NNEW(n_METHOD_CALL)->appendChild($$); $$->appendChild($4); } - for (xhpast::node_list_t::iterator ii = $5->children.begin(); ii != $5->children.end(); ++ii) { + for (xhpast::node_list_t::iterator ii = $5->children.begin(); + ii != $5->children.end(); + ++ii) { + if ((*ii)->type == n_CALL_PARAMETER_LIST) { $$ = NNEW(n_METHOD_CALL)->appendChild($$); $$->appendChild((*ii)); } else { $$ = NNEW(n_OBJECT_PROPERTY_ACCESS)->appendChild($$); $$->appendChild((*ii)); } } } | base_variable_with_function_calls ; variable_properties: variable_properties variable_property { $$ = $1->appendChildren($2); } | /* empty */ { $$ = NNEW(n_EMPTY); } ; variable_property: T_OBJECT_OPERATOR object_property method_or_not { $$ = NNEW(n_EMPTY); $$->appendChild($2); if ($3->type != n_EMPTY) { $$->appendChild($3); } } ; array_method_dereference: array_method_dereference '[' dim_offset ']' { $$ = NNEW(n_INDEX_ACCESS); $$->appendChild($1); $$->appendChild($3); NMORE($$, $4); } | method '[' dim_offset ']' { $$ = NNEW(n_INDEX_ACCESS); $$->appendChild($1); $$->appendChild($3); NMORE($$, $4); } ; method: '(' function_call_parameter_list ')' { $$ = NEXPAND($1, $2, $3); } ; method_or_not: method | array_method_dereference | /* empty */ { $$ = NNEW(n_EMPTY); } ; variable_without_objects: reference_variable | simple_indirect_reference reference_variable { xhpast::Node *last = $1; NMORE($1, $2); while (last->firstChild() && last->firstChild()->type == n_VARIABLE_VARIABLE) { NMORE(last, $2); last = last->firstChild(); } last->appendChild($2); $$ = $1; } ; static_member: class_name T_PAAMAYIM_NEKUDOTAYIM variable_without_objects { $$ = NNEW(n_CLASS_STATIC_ACCESS); $$->appendChild($1); $$->appendChild($3); } | variable_class_name T_PAAMAYIM_NEKUDOTAYIM variable_without_objects { $$ = NNEW(n_CLASS_STATIC_ACCESS); $$->appendChild($1); $$->appendChild($3); } ; variable_class_name: reference_variable ; array_function_dereference: array_function_dereference '[' dim_offset ']' { $$ = NNEW(n_INDEX_ACCESS); $$->appendChild($1); $$->appendChild($3); NMORE($$, $4); } | function_call '[' dim_offset ']' { $$ = NNEW(n_INDEX_ACCESS); $$->appendChild($1); $$->appendChild($3); NMORE($$, $4); } ; base_variable_with_function_calls: base_variable | array_function_dereference | function_call ; base_variable: reference_variable | '(' new_expr ')' { $$ = NEXPAND($1, $2, $3); } | simple_indirect_reference reference_variable { xhpast::Node *last = $1; NMORE($1, $2); while (last->firstChild() && last->firstChild()->type == n_VARIABLE_VARIABLE) { NMORE(last, $2); last = last->firstChild(); } last->appendChild($2); $$ = $1; } | static_member ; reference_variable: reference_variable '[' dim_offset ']' { $$ = NNEW(n_INDEX_ACCESS); $$->appendChild($1); $$->appendChild($3); NMORE($$, $4); } | reference_variable '{' expr '}' { $$ = NNEW(n_INDEX_ACCESS); $$->appendChild($1); $$->appendChild($3); NMORE($$, $4); } | compound_variable ; compound_variable: T_VARIABLE { NTYPE($1, n_VARIABLE); } | '$' '{' expr '}' { NSPAN($1, n_VARIABLE_EXPRESSION, $4); $1->appendChild($3); $$ = $1; } ; dim_offset: /* empty */ { $$ = NNEW(n_EMPTY); } | expr { $$ = $1; } ; object_property: object_dim_list | variable_without_objects ; object_dim_list: object_dim_list '[' dim_offset ']' { $$ = NNEW(n_INDEX_ACCESS); $$->appendChild($1); $$->appendChild($3); NMORE($$, $4); } | object_dim_list '{' expr '}' { $$ = NNEW(n_INDEX_ACCESS); $$->appendChild($1); $$->appendChild($3); NMORE($$, $4); } | variable_name ; variable_name: T_STRING { NTYPE($1, n_STRING); $$ = $1; } | '{' expr '}' { $$ = NEXPAND($1, $2, $3); } ; simple_indirect_reference: '$' { $$ = NTYPE($1, n_VARIABLE_VARIABLE); } | simple_indirect_reference '$' { $2 = NTYPE($2, n_VARIABLE_VARIABLE); xhpast::Node *last = $1; while (last->firstChild() && last->firstChild()->type == n_VARIABLE_VARIABLE) { last = last->firstChild(); } last->appendChild($2); $$ = $1; } ; assignment_list: assignment_list ',' assignment_list_element { $$ = $1->appendChild($3); } | assignment_list_element { $$ = NNEW(n_ASSIGNMENT_LIST); $$->appendChild($1); } ; assignment_list_element: variable | T_LIST '(' assignment_list ')' { $$ = NNEW(n_LIST); $$->appendChild($3); NMORE($$, $4); } | /* empty */ { $$ = NNEW(n_EMPTY); } ; array_pair_list: /* empty */ { $$ = NNEW(n_ARRAY_VALUE_LIST); } | non_empty_array_pair_list possible_comma { $$ = NMORE($1, $2); } ; non_empty_array_pair_list: non_empty_array_pair_list ',' expr T_DOUBLE_ARROW expr { $$ = NNEW(n_ARRAY_VALUE); $$->appendChild($3); $$->appendChild($5); $$ = $1->appendChild($$); } | non_empty_array_pair_list ',' expr { $$ = NNEW(n_ARRAY_VALUE); $$->appendChild(NNEW(n_EMPTY)); $$->appendChild($3); $$ = $1->appendChild($$); } | expr T_DOUBLE_ARROW expr { $$ = NNEW(n_ARRAY_VALUE); $$->appendChild($1); $$->appendChild($3); $$ = NNEW(n_ARRAY_VALUE_LIST)->appendChild($$); } | expr { $$ = NNEW(n_ARRAY_VALUE); $$->appendChild(NNEW(n_EMPTY)); $$->appendChild($1); $$ = NNEW(n_ARRAY_VALUE_LIST)->appendChild($$); } | non_empty_array_pair_list ',' expr T_DOUBLE_ARROW '&' w_variable { $$ = NNEW(n_ARRAY_VALUE); $$->appendChild($3); $$->appendChild(NTYPE($5, n_VARIABLE_REFERENCE)->appendChild($6)); $$ = $1->appendChild($$); } | non_empty_array_pair_list ',' '&' w_variable { $$ = NNEW(n_ARRAY_VALUE); $$->appendChild(NNEW(n_EMPTY)); $$->appendChild(NTYPE($3, n_VARIABLE_REFERENCE)->appendChild($4)); $$ = $1->appendChild($$); } | expr T_DOUBLE_ARROW '&' w_variable { $$ = NNEW(n_ARRAY_VALUE); $$->appendChild($1); $$->appendChild(NTYPE($3, n_VARIABLE_REFERENCE)->appendChild($4)); $$ = NNEW(n_ARRAY_VALUE_LIST)->appendChild($$); } | '&' w_variable { $$ = NNEW(n_ARRAY_VALUE); $$->appendChild(NNEW(n_EMPTY)); $$->appendChild(NTYPE($1, n_VARIABLE_REFERENCE)->appendChild($2)); $$ = NNEW(n_ARRAY_VALUE_LIST)->appendChild($$); } ; internal_functions_in_yacc: T_ISSET '(' isset_variables ')' { NTYPE($1, n_SYMBOL_NAME); NSPAN($2, n_CALL_PARAMETER_LIST, $4); $2->appendChildren($3); $$ = NNEW(n_FUNCTION_CALL); $$->appendChild($1); $$->appendChild($2); } | T_EMPTY '(' variable ')' { NTYPE($1, n_SYMBOL_NAME); NSPAN($2, n_CALL_PARAMETER_LIST, $4); $2->appendChild($3); $$ = NNEW(n_FUNCTION_CALL); $$->appendChild($1); $$->appendChild($2); } | T_INCLUDE expr { $$ = NTYPE($1, n_INCLUDE_FILE)->appendChild($2); } | T_INCLUDE_ONCE expr { $$ = NTYPE($1, n_INCLUDE_FILE)->appendChild($2); } | T_EVAL '(' expr ')' { NTYPE($1, n_SYMBOL_NAME); NSPAN($2, n_CALL_PARAMETER_LIST, $4); $2->appendChild($3); $$ = NNEW(n_FUNCTION_CALL); $$->appendChild($1); $$->appendChild($2); } | T_REQUIRE expr { $$ = NTYPE($1, n_INCLUDE_FILE)->appendChild($2); } | T_REQUIRE_ONCE expr { $$ = NTYPE($1, n_INCLUDE_FILE)->appendChild($2); } ; isset_variables: variable { $$ = NNEW(n_EMPTY); $$->appendChild($1); } | isset_variables ',' variable { $$ = $1->appendChild($3); } ; parenthesis_expr: '(' expr ')' { NSPAN($1, n_PARENTHETICAL_EXPRESSION, $3); $1->appendChild($2); $$ = $1; } | '(' yield_expr ')' { $$ = NEXPAND($1, $2, $3); } ; combined_scalar_offset: combined_scalar '[' dim_offset ']' { $$ = NNEW(n_INDEX_ACCESS); $$->appendChild($1); $$->appendChild($3); NMORE($$, $4); } | combined_scalar_offset '[' dim_offset ']' { $$ = NNEW(n_INDEX_ACCESS); $$->appendChild($1); $$->appendChild($3); NMORE($$, $4); } | T_CONSTANT_ENCAPSED_STRING '[' dim_offset ']' { $$ = NNEW(n_INDEX_ACCESS); $$->appendChild(NTYPE($1, n_STRING_SCALAR)); $$->appendChild($3); NMORE($$, $4); } ; combined_scalar: T_ARRAY '(' array_pair_list ')' { NTYPE($1, n_ARRAY_LITERAL); $1->appendChild($3); NMORE($1, $4); $$ = $1; } | '[' array_pair_list ']' { NTYPE($1, n_ARRAY_LITERAL); $1->appendChild($2); NMORE($1, $3); $$ = $1; } ; new_expr: T_NEW class_name_reference ctor_arguments { NTYPE($1, n_NEW); $1->appendChild($2); $1->appendChild($3); $$ = $1; } ; class_constant: class_name T_PAAMAYIM_NEKUDOTAYIM T_STRING { $$ = NNEW(n_CLASS_STATIC_ACCESS); $$->appendChild($1); $$->appendChild(NTYPE($3, n_STRING)); } | variable_class_name T_PAAMAYIM_NEKUDOTAYIM T_STRING { $$ = NNEW(n_CLASS_STATIC_ACCESS); $$->appendChild($1); $$->appendChild(NTYPE($3, n_STRING)); } ; %% const char* yytokname(int tok) { if (tok < 255) { return NULL; } return yytname[YYTRANSLATE(tok)]; } diff --git a/support/xhpast/scanner.l b/support/xhpast/scanner.l index 5992231..28cf40f 100644 --- a/support/xhpast/scanner.l +++ b/support/xhpast/scanner.l @@ -1,514 +1,536 @@ %{ #include "ast.hpp" #define push_state(s) xhp_new_push_state(s, yyg) #define pop_state() xhp_new_pop_state(yyg) #define set_state(s) xhp_set_state(s, yyg) #define last_token() yyextra->last_token #define YY_USER_ACTION \ if (!yyg->yy_more_len) \ yyextra->first_lineno = yyextra->lineno; #define pttok(t, txt) \ - yyextra->token_list.push_back(new xhpast::Token(t, txt, yyextra->list_size++)); \ + yyextra->token_list.push_back( \ + new xhpast::Token(t, txt, yyextra->list_size++)); \ *yylval = new xhpast::Node(0, yyextra->list_size - 1); #define ptok(t) \ pttok(t, yytext); #define tok(t) \ ptok(t); \ return yy_token(t, yyg) #define YY_USER_INIT \ if (yyextra->insert_token) { \ yyg->yy_init = 0; \ int ft = yyextra->insert_token; \ yyextra->insert_token = 0; \ return yy_token(ft, yyg); \ } using namespace std; const char* yytokname(int tok); static int yy_token(int tok, struct yyguts_t* yyg); static void yy_scan_newlines(const char* text, struct yyguts_t* yyg); %} %option prefix="xhpast" %option reentrant /* PHP allows IF or if */ %option case-insensitive %option noyywrap nodefault %option stack %option bison-bridge %option 8bit /* I think an interactive scanner is required because of the bison state * pushing we do. I'm putting an explicit interactive declaration here in case * someone tries adding -CF or whatever to the make flags. */ %option interactive /* The different lexing states. Note that the transitions are done either * in the lex actions, or in a generic manner in yy_token(). */ %s PHP %s PHP_COMMENT %s PHP_EOL_COMMENT %s PHP_DOC_COMMENT %s PHP_HEREDOC_START %s PHP_HEREDOC_NSTART %s PHP_HEREDOC_NEWLINE %s PHP_NO_RESERVED_WORDS %s PHP_NO_RESERVED_WORDS_PERSIST %s PHP_ LNUM [0-9]+ DNUM ([0-9]*"."[0-9]+)|([0-9]+"."[0-9]*) EXPONENT_DNUM (({LNUM}|{DNUM})[eE][+-]?{LNUM}) HNUM "0x"[0-9a-fA-F]+ BNUM "0b"[01]+ LABEL [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]* BYTE (.|\n) WHITESPACE [ \n\r\t]+ TABS_AND_SPACES [ \t]* NEWLINE ("\r\n"|"\n"|"\r") %% /* Open / close PHP + inline HTML */ { "short_tags) { tok(T_OPEN_TAG); } else { tok(T_INLINE_HTML); } } "short_tags) { tok(T_OPEN_TAG_WITH_ECHO); } else { tok(T_INLINE_HTML); } } "<%" { if (yyextra->asp_tags) { tok(T_OPEN_TAG); } else { tok(T_INLINE_HTML); } } "<%=" { if (yyextra->asp_tags) { tok(T_OPEN_TAG_WITH_ECHO); } else { tok(T_INLINE_HTML); } } "<"|[^<]* { yy_scan_newlines(yytext, yyg); tok(T_INLINE_HTML); } } { ("?>"|""){NEWLINE}? { yy_scan_newlines(yytext + 2, yyg); tok(T_CLOSE_TAG); } "%>" { if (yyextra->asp_tags) { tok(T_CLOSE_TAG); } else { yyless(1); tok(yytext[0]); } } } /* Comments and whitespace */ { "#"|"//" { push_state(PHP_EOL_COMMENT); yymore(); } "/**"{WHITESPACE} { yy_scan_newlines(yytext + 3, yyg); push_state(PHP_DOC_COMMENT); yymore(); } "/*" { push_state(PHP_COMMENT); yymore(); } {WHITESPACE}+ { yy_scan_newlines(yytext, yyg); ptok(T_WHITESPACE); } } <> { ptok(T_COMMENT); pop_state(); } { {NEWLINE} { ++yyextra->lineno; ptok(T_COMMENT); pop_state(); } [^\r\n?]+ yymore(); "?>" { yyless(yyleng - 2); ptok(T_COMMENT); pop_state(); } . yymore(); } { {NEWLINE} { ++yyextra->lineno; yymore(); } [^*\r\n]+|"*" yymore(); } "*/" { ptok(T_DOC_COMMENT); pop_state(); } <> { ptok(T_DOC_COMMENT); pop_state(); } "*/" { ptok(T_COMMENT); pop_state(); } <> { ptok(T_COMMENT); pop_state(); } /* Reserved words */ { include tok(T_INCLUDE); include_once tok(T_INCLUDE_ONCE); eval tok(T_EVAL); require tok(T_REQUIRE); require_once tok(T_REQUIRE_ONCE); or tok(T_LOGICAL_OR); xor tok(T_LOGICAL_XOR); and tok(T_LOGICAL_AND); print tok(T_PRINT); instanceof tok(T_INSTANCEOF); new tok(T_NEW); clone tok(T_CLONE); exit tok(T_EXIT); if tok(T_IF); elseif tok(T_ELSEIF); else tok(T_ELSE); endif tok(T_ENDIF); echo tok(T_ECHO); do tok(T_DO); while tok(T_WHILE); endwhile tok(T_ENDWHILE); for tok(T_FOR); endfor tok(T_ENDFOR); foreach tok(T_FOREACH); endforeach tok(T_ENDFOREACH); declare tok(T_DECLARE); enddeclare tok(T_ENDDECLARE); as tok(T_AS); switch tok(T_SWITCH); endswitch tok(T_ENDSWITCH); case tok(T_CASE); default tok(T_DEFAULT); break tok(T_BREAK); continue tok(T_CONTINUE); goto tok(T_GOTO); function tok(T_FUNCTION); const tok(T_CONST); return tok(T_RETURN); try tok(T_TRY); catch tok(T_CATCH); throw tok(T_THROW); use tok(T_USE); global tok(T_GLOBAL); static tok(T_STATIC); abstract tok(T_ABSTRACT); final tok(T_FINAL); private tok(T_PRIVATE); protected tok(T_PROTECTED); public tok(T_PUBLIC); var tok(T_VAR); unset tok(T_UNSET); isset tok(T_ISSET); empty tok(T_EMPTY); __halt_compiler tok(T_HALT_COMPILER); class tok(T_CLASS); interface tok(T_INTERFACE); extends tok(T_EXTENDS); implements tok(T_IMPLEMENTS); list tok(T_LIST); array tok(T_ARRAY); __class__ tok(T_CLASS_C); __method__ tok(T_METHOD_C); __function__ tok(T_FUNC_C); __line__ tok(T_LINE); __file__ tok(T_FILE); namespace tok(T_NAMESPACE); __namespace__ tok(T_NS_C); __dir__ tok(T_DIR); insteadof tok(T_INSTEADOF); callable tok(T_CALLABLE); trait tok(T_TRAIT); __trait__ tok(T_TRAIT_C); yield tok(T_YIELD); finally tok(T_FINALLY); } /* Operators */ { "+=" tok(T_PLUS_EQUAL); "-=" tok(T_MINUS_EQUAL); "*=" tok(T_MUL_EQUAL); "/=" tok(T_DIV_EQUAL); ".=" tok(T_CONCAT_EQUAL); "%=" tok(T_MOD_EQUAL); "&=" tok(T_AND_EQUAL); "|=" tok(T_OR_EQUAL); "^=" tok(T_XOR_EQUAL); "<<=" tok(T_SL_EQUAL); ">>=" tok(T_SR_EQUAL); "||" tok(T_BOOLEAN_OR); "&&" tok(T_BOOLEAN_AND); "==" tok(T_IS_EQUAL); "!="|"<>" tok(T_IS_NOT_EQUAL); "===" tok(T_IS_IDENTICAL); "!==" tok(T_IS_NOT_IDENTICAL); "<=" tok(T_IS_SMALLER_OR_EQUAL); ">=" tok(T_IS_GREATER_OR_EQUAL); "<<" tok(T_SL); ">>" tok(T_SR); "++" tok(T_INC); "--" tok(T_DEC); "->" tok(T_OBJECT_OPERATOR); "=>" tok(T_DOUBLE_ARROW); "::" tok(T_PAAMAYIM_NEKUDOTAYIM); "\\" tok(T_NS_SEPARATOR); } /* Casts */ { "("{TABS_AND_SPACES}(int|integer){TABS_AND_SPACES}")" tok(T_INT_CAST); - "("{TABS_AND_SPACES}(real|double|float){TABS_AND_SPACES}")" tok(T_DOUBLE_CAST); + "("{TABS_AND_SPACES}(real|double|float){TABS_AND_SPACES}")" + tok(T_DOUBLE_CAST); "("{TABS_AND_SPACES}(string|binary){TABS_AND_SPACES}")" tok(T_STRING_CAST); "("{TABS_AND_SPACES}array{TABS_AND_SPACES}")" tok(T_ARRAY_CAST); "("{TABS_AND_SPACES}object{TABS_AND_SPACES}")" tok(T_OBJECT_CAST); "("{TABS_AND_SPACES}(bool|boolean){TABS_AND_SPACES}")" tok(T_BOOL_CAST); "("{TABS_AND_SPACES}unset{TABS_AND_SPACES}")" tok(T_UNSET_CAST); } - /* Scalars (parsing these doesn't really matter since we just pass them through literally) */ +// Scalars (parsing these doesn't really matter since we just pass +// them through literally) { {LNUM}|{HNUM}|{BNUM} tok(T_LNUMBER); {DNUM}|{EXPONENT_DNUM} tok(T_DNUMBER); {LABEL} tok(T_STRING); "$"{LABEL} tok(T_VARIABLE); b?'(\\.|\\\n|[^\\']+)*'|b?\"(\\.|\\\n|[^\\\"]+)*\" { yy_scan_newlines(yytext, yyg); tok(T_CONSTANT_ENCAPSED_STRING); } `[^`]*` { yy_scan_newlines(yytext, yyg); tok(T_BACKTICKS_EXPR); } } /* (HERE|NOW)DOC's */ -b?"<<<"{TABS_AND_SPACES} { +b?"<<< + "{TABS_AND_SPACES} { + push_state(PHP_HEREDOC_START); yyextra->heredoc_yyleng = yyleng; yymore(); } { "'"{LABEL}"'"|\"{LABEL}\" { // Create a new string for the heredoc label. Since we're using yymore above // yytext will actually start at the "<<<" and not the label. Use of // heredoc_yyleng jumps past that. Then we add 1 to get past the " or '. The // match is similar to calculate length. - yyextra->heredoc_label = string(yytext + yyextra->heredoc_yyleng + 1, yyleng - yyextra->heredoc_yyleng - 2); + yyextra->heredoc_label = string( + yytext + yyextra->heredoc_yyleng + 1, + yyleng - yyextra->heredoc_yyleng - 2); set_state(PHP_HEREDOC_NSTART); yyextra->heredoc_yyleng = yyleng; yymore(); } {LABEL} { yyextra->heredoc_label = string(yytext + yyextra->heredoc_yyleng); set_state(PHP_HEREDOC_NSTART); yyextra->heredoc_yyleng = yyleng; yymore(); } } {NEWLINE} { yyextra->heredoc_yyleng = yyleng; set_state(PHP_HEREDOC_NEWLINE); yymore(); } { {LABEL};?{NEWLINE} { - if (strncmp(yyextra->heredoc_label.c_str(), yytext + yyextra->heredoc_yyleng, yyextra->heredoc_label.size()) == 0) { + if (strncmp( + yyextra->heredoc_label.c_str(), + yytext + yyextra->heredoc_yyleng, yyextra->heredoc_label.size()) == 0) { + switch (yytext[yyextra->heredoc_yyleng + yyextra->heredoc_label.size()]) { case ';': case '\n': case '\r': - yyless(yyleng - (yyleng - yyextra->heredoc_yyleng - yyextra->heredoc_label.size())); + yyless( + yyleng - ( + yyleng - + yyextra->heredoc_yyleng - + yyextra->heredoc_label.size())); pop_state(); tok(T_HEREDOC); } } ++yyextra->lineno; yyextra->heredoc_yyleng = yyleng; yymore(); } [^\r\n]+ { yyextra->heredoc_yyleng = yyleng; yymore(); } {NEWLINE} { ++yyextra->lineno; yyextra->heredoc_yyleng = yyleng; yymore(); } } /* Other */ <*>{BYTE} { tok(yytext[0]); // fix unused function warnings yy_top_state(NULL); yyunput(0, 0, NULL); } %% #ifdef DEBUG static const char* yy_state_name(int state) { switch (state) { case INITIAL: return "INITIAL"; case PHP: return "PHP"; case PHP_COMMENT: return "PHP_COMMENT"; case PHP_EOL_COMMENT: return "PHP_EOL_COMMENT"; case PHP_DOC_COMMENT: return "PHP_DOC_COMMENT"; case PHP_HEREDOC_START: return "PHP_HEREDOC_START"; case PHP_HEREDOC_NSTART: return "PHP_HEREDOC_NSTART"; case PHP_HEREDOC_NEWLINE: return "PHP_HEREDOC_NEWLINE"; case PHP_NO_RESERVED_WORDS: return "PHP_NO_RESERVED_WORDS"; case PHP_NO_RESERVED_WORDS_PERSIST: return "PHP_NO_RESERVED_WORDS_PERSIST"; default: return "???"; } } static void yy_log_token(int tok) { const char* tokname = yytokname(tok); if (tokname) { fprintf(stderr, "--> %s\n", tokname); } else { fprintf(stderr, "--> '%c'\n", tok); } } #endif static int yy_token(int tok, yyguts_t* yyg) { if (YY_START == PHP_NO_RESERVED_WORDS) { pop_state(); } switch (tok) { case T_OPEN_TAG: case T_OPEN_TAG_WITH_ECHO: case T_OPEN_TAG_FAKE: push_state(PHP); break; case T_CLOSE_TAG: pop_state(); // We need to return a ';', not a T_CLOSE_TAG, because a construct like // "" is valid and there are about a billion parser rules // which terminate with ';' so making a new rule like // "semicolon_or_close_tag" would be hard. The token in yylval has the // correct type and value, we just don't generate a node. return ';'; // In PHP it's ok to use keywords such as 'if' as field names // or function names. case T_OBJECT_OPERATOR: case T_FUNCTION: push_state(PHP_NO_RESERVED_WORDS); break; case T_PAAMAYIM_NEKUDOTAYIM: if (yyextra->colon_hack) { yyextra->colon_hack = false; } else { push_state(PHP_NO_RESERVED_WORDS); } break; case '{': // not used anymore yyextra->curly_stack.push(tok); break; } #ifdef DEBUG yy_log_token(tok); #endif return yyextra->last_token = tok; } static inline void yy_scan_newlines(const char* text, struct yyguts_t* yyg) { for (; *text; ++text) { if (*text == '\r') { if (text[1] == '\n') { ++text; } ++yyextra->lineno; } else if (*text == '\n') { ++yyextra->lineno; } } } void xhp_new_push_state(int s, struct yyguts_t* yyg) { #ifdef DEBUG - fprintf(stderr, "--> PUSH(%s -> %s)\n", yy_state_name(YY_START), yy_state_name(s)); + fprintf( + stderr, + "--> PUSH(%s -> %s)\n", + yy_state_name(YY_START), + yy_state_name(s)); #endif yy_push_state(s, yyg); } void xhp_new_pop_state(struct yyguts_t* yyg) { #ifdef DEBUG int s = YY_START; #endif yy_pop_state(yyg); #ifdef DEBUG - fprintf(stderr, "--> POP(%s -> %s)\n", yy_state_name(s), yy_state_name(YY_START)); + fprintf( + stderr, + "--> POP(%s -> %s)\n", + yy_state_name(s), + yy_state_name(YY_START)); #endif } void xhp_set_state(int s, struct yyguts_t* yyg) { #ifdef DEBUG fprintf(stderr, "--> SET(%s)\n", yy_state_name(s)); #endif BEGIN(s); } diff --git a/support/xhpast/xhpast.cpp b/support/xhpast/xhpast.cpp index d12a752..e354fd6 100644 --- a/support/xhpast/xhpast.cpp +++ b/support/xhpast/xhpast.cpp @@ -1,128 +1,127 @@ #include "ast.hpp" #include #include #include #include #include using namespace std; int xhpastparse(void*, xhpast::Node **); int xhpast_process(std::string &in); void print_node(xhpast::Node *node); int main(int argc, char* argv[]) { - vector files; - if (argc != 1) { - //coupling: modify also libphutil/src/parser/xhpast/bin/PhutilXHPASTBinary.php + // Coupling: modify also src/parser/xhpast/bin/PhutilXHPASTBinary.php cout << "5.5.8/1h\n"; return 0; } ifstream inputFile; istream *inputStream; inputStream = &cin; std::stringbuf sb; *inputStream >> noskipws >> &sb; std::string buffer = sb.str(); inputFile.close(); return xhpast_process(buffer); } int xhpast_process(std::string &in) { char *buffer; in.reserve(in.size() + 1); buffer = const_cast(in.c_str()); buffer[in.size() + 1] = 0; // need double NULL for scan_buffer void* scanner; yy_extra_type extra; extra.idx_expr = true;//flags.idx_expr; extra.include_debug = true;//flags.include_debug; extra.insert_token = 0;//flags.eval ? T_OPEN_TAG_FAKE : 0; extra.short_tags = true;//flags.short_tags; extra.asp_tags = false;//flags.asp_tags; xhpast::Node *root = NULL; xhpastlex_init(&scanner); xhpastset_extra(&extra, scanner); xhpast_scan_buffer(buffer, in.size() + 2, scanner); xhpastparse(scanner, &root); xhpastlex_destroy(scanner); if (extra.terminated) { fprintf( stderr, "XHPAST Parse Error: %s on line %d\n", extra.error.c_str(), (int)extra.lineno); return 1; } printf("{"); printf("\"tree\":"); if (root) { // Extend the right token for the root node to the end of the concrete // token stream. This ensure all tokens appear in the tree. If we don't // do this and the file ends in tokens which don't go to the parser (like // comments and whitespace) they won't be represented in the tree. root->r_tok = (extra.token_list.size() - 1); print_node(root); } else { printf("null"); } printf(","); printf("\"stream\":"); printf("["); if (!extra.token_list.empty()) { for (xhpast::token_list_t::iterator ii = extra.token_list.begin();;) { printf("[%d, %d]", (*ii)->type, (int)(*ii)->value.length()); if (++ii != extra.token_list.end()) { printf(","); } else { break; } } } printf("]"); printf("}\n"); return 0; } void print_node(xhpast::Node *node) { int l = -1; - int r = -1; if (node->l_tok != -1) { l = node->l_tok; } if (l == -1) { - printf("[%d]", node->type); + printf("[%u]", node->type); } else { + int r = -1; + if (node->r_tok != -1) { r = node->r_tok; } - printf("[%d, %d, %d", node->type, l, r); + printf("[%u, %d, %d", node->type, l, r); if (!node->children.empty()) { printf(", ["); for (xhpast::node_list_t::iterator ii = node->children.begin();;) { print_node(*ii); if (++ii != node->children.end()) { printf(","); } else { break; } } printf("]"); } printf("]"); } }