Skip to content

Instantly share code, notes, and snippets.

@danyaPostfactum
Created January 29, 2013 19:27
Show Gist options
  • Save danyaPostfactum/4666910 to your computer and use it in GitHub Desktop.
Save danyaPostfactum/4666910 to your computer and use it in GitHub Desktop.
%pure_parser
%expect 2
%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 '?' ':'
%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_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
%token T_VARIABLE
%token T_NUM_STRING
%token T_INLINE_HTML
%token T_CHARACTER
%token T_BAD_CHARACTER
%token T_ENCAPSED_AND_WHITESPACE
%token T_CONSTANT_ENCAPSED_STRING
%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_INSTEADOF
%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_TRAIT
%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_CALLABLE
%token T_CLASS_C
%token T_TRAIT_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_CLOSE_TAG
%token T_WHITESPACE
%token T_START_HEREDOC
%token T_END_HEREDOC
%token T_DOLLAR_OPEN_CURLY_BRACES
%token T_CURLY_OPEN
%token T_PAAMAYIM_NEKUDOTAYIM
%token T_NAMESPACE
%token T_NS_C
%token T_DIR
%token T_NS_SEPARATOR
%%
start:
top_statement_list { $$ = this.Stmt_Namespace_postprocess($1); }
;
top_statement_list:
top_statement_list top_statement { if (Array.isArray($2)) { $$ = $1.concat( $2); } else { $1.push( $2 ); $$ = $1; }; }
| /* empty */ { $$ = []; }
;
namespace_name:
T_STRING { $$ = [$1]; }
| namespace_name T_NS_SEPARATOR T_STRING { $1.push( $3 ); $$ = $1; }
;
top_statement:
statement { $$ = $1; }
| function_declaration_statement { $$ = $1; }
| class_declaration_statement { $$ = $1; }
| T_HALT_COMPILER
{ $$ = this.Node_Stmt_HaltCompiler(attributes); }
| T_NAMESPACE namespace_name ';' { $$ = this.Node_Stmt_Namespace(this.Node_Name($2, attributes), null, attributes); }
| T_NAMESPACE namespace_name '{' top_statement_list '}' { $$ = this.Node_Stmt_Namespace(this.Node_Name($2, attributes), $4, attributes); }
| T_NAMESPACE '{' top_statement_list '}' { $$ = this.Node_Stmt_Namespace(null, $3, attributes); }
| T_USE use_declarations ';' { $$ = this.Node_Stmt_Use($2, attributes); }
| T_CONST constant_declaration_list ';' { $$ = this.Node_Stmt_Const($2, attributes); }
;
use_declarations:
use_declarations ',' use_declaration { $1.push( $3 ); $$ = $1; }
| use_declaration { $$ = [$1]; }
;
use_declaration:
namespace_name { $$ = this.Node_Stmt_UseUse(this.Node_Name($1, attributes), null, attributes); }
| namespace_name T_AS T_STRING { $$ = this.Node_Stmt_UseUse(this.Node_Name($1, attributes), $3, attributes); }
| T_NS_SEPARATOR namespace_name { $$ = this.Node_Stmt_UseUse(this.Node_Name($2, attributes), null, attributes); }
| T_NS_SEPARATOR namespace_name T_AS T_STRING { $$ = this.Node_Stmt_UseUse(this.Node_Name($2, attributes), $4, attributes); }
;
constant_declaration_list:
constant_declaration_list ',' constant_declaration { $1.push( $3 ); $$ = $1; }
| constant_declaration { $$ = [$1]; }
;
constant_declaration:
T_STRING '=' static_scalar { $$ = this.Node_Const($1, $3, attributes); }
;
inner_statement_list:
inner_statement_list inner_statement { if (Array.isArray($2)) { $$ = $1.concat( $2); } else { $1.push( $2 ); $$ = $1; }; }
| /* empty */ { $$ = []; }
;
inner_statement:
statement { $$ = $1; }
| function_declaration_statement { $$ = $1; }
| class_declaration_statement { $$ = $1; }
| T_HALT_COMPILER { throw new Error('__halt_compiler() can only be used from the outermost scope'); }
;
statement:
'{' inner_statement_list '}' { $$ = $2; }
| T_IF '(' expr ')' statement elseif_list else_single { $$ = this.Node_Stmt_If($3, {'stmts': Array.isArray($5) ? $5 : [$5], 'elseifs': $6, 'Else': $7}, attributes); }
| T_IF '(' expr ')' ':' inner_statement_list new_elseif_list new_else_single T_ENDIF ';'
{ $$ = this.Node_Stmt_If($3, {'stmts': $6, 'elseifs': $7, 'else': $8}, attributes); }
| T_WHILE '(' expr ')' while_statement { $$ = this.Node_Stmt_While($3, $5, attributes); }
| T_DO statement T_WHILE '(' expr ')' ';' { $$ = this.Node_Stmt_Do($5, Array.isArray($2) ? $2 : [$2], attributes); }
| T_FOR '(' for_expr ';' for_expr ';' for_expr ')' for_statement
{ $$ = this.Node_Stmt_For({'init': $3, 'cond': $5, 'loop': $7, 'stmts': $9}, attributes); }
| T_SWITCH '(' expr ')' switch_case_list { $$ = this.Node_Stmt_Switch($3, $5, attributes); }
| T_BREAK ';' { $$ = this.Node_Stmt_Break(null, attributes); }
| T_BREAK expr ';' { $$ = this.Node_Stmt_Break($2, attributes); }
| T_CONTINUE ';' { $$ = this.Node_Stmt_Continue(null, attributes); }
| T_CONTINUE expr ';' { $$ = this.Node_Stmt_Continue($2, attributes); }
| T_RETURN ';' { $$ = this.Node_Stmt_Return(null, attributes); }
| T_RETURN expr ';' { $$ = this.Node_Stmt_Return($2, attributes); }
| T_GLOBAL global_var_list ';' { $$ = this.Node_Stmt_Global($2, attributes); }
| T_STATIC static_var_list ';' { $$ = this.Node_Stmt_Static($2, attributes); }
| T_ECHO expr_list ';' { $$ = this.Node_Stmt_Echo($2, attributes); }
| T_INLINE_HTML { $$ = this.Node_Stmt_InlineHTML($1, attributes); }
| expr ';' { $$ = $1; }
| T_UNSET '(' variables_list ')' ';' { $$ = this.Node_Stmt_Unset($3, attributes); }
| T_FOREACH '(' expr T_AS variable ')' foreach_statement
{ $$ = this.Node_Stmt_Foreach($3, $5, {'keyVar': null, 'byRef': false, 'stmts': $7}, attributes); }
| T_FOREACH '(' expr T_AS '&' variable ')' foreach_statement
{ $$ = this.Node_Stmt_Foreach($3, $6, {'keyVar': null, 'byRef': true, 'stmts': $8}, attributes); }
| T_FOREACH '(' expr T_AS variable T_DOUBLE_ARROW optional_ref variable ')' foreach_statement
{ $$ = this.Node_Stmt_Foreach($3, $8, {'keyVar': $5, 'byRef': $7, 'stmts': $10}, attributes); }
| T_DECLARE '(' declare_list ')' declare_statement { $$ = this.Node_Stmt_Declare($3, $5, attributes); }
| ';' { $$ = []; /* means: no statement */ }
| T_TRY '{' inner_statement_list '}' catches { $$ = this.Node_Stmt_TryCatch($3, $5, attributes); }
| T_THROW expr ';' { $$ = this.Node_Stmt_Throw($2, attributes); }
| T_GOTO T_STRING ';' { $$ = this.Node_Stmt_Goto($2, attributes); }
| T_STRING ':' { $$ = this.Node_Stmt_Label($1, attributes); }
;
catches:
catch { $$ = [$1]; }
| catches catch { $1.push( $2 ); $$ = $1; }
;
catch:
T_CATCH '(' name T_VARIABLE ')' '{' inner_statement_list '}'
{ $$ = this.Node_Stmt_Catch($3, $4.substring( 1 ), $7, attributes); }
;
variables_list:
variable { $$ = [$1]; }
| variables_list ',' variable { $1.push( $3 ); $$ = $1; }
;
optional_ref:
/* empty */ { $$ = false; }
| '&' { $$ = true; }
;
function_declaration_statement:
T_FUNCTION optional_ref T_STRING '(' parameter_list ')' '{' inner_statement_list '}'
{ $$ = this.Node_Stmt_Function($3, {'byRef': $2, 'params': $5, 'stmts': $8}, attributes); }
;
class_declaration_statement:
class_entry_type T_STRING extends_from implements_list '{' class_statement_list '}'
{ $$ = this.Node_Stmt_Class($2, {'type': $1, 'Extends': $3, 'Implements': $4, 'stmts': $6}, attributes); }
| T_INTERFACE T_STRING interface_extends_list '{' class_statement_list '}'
{ $$ = this.Node_Stmt_Interface($2, {'Extends': $3, 'stmts': $5}, attributes); }
| T_TRAIT T_STRING '{' class_statement_list '}'
{ $$ = this.Node_Stmt_Trait($2, $4, attributes); }
;
class_entry_type:
T_CLASS { $$ = 0; }
| T_ABSTRACT T_CLASS { $$ = this.MODIFIER_ABSTRACT; }
| T_FINAL T_CLASS { $$ = this.MODIFIER_FINAL; }
;
extends_from:
/* empty */ { $$ = null; }
| T_EXTENDS name { $$ = $2; }
;
interface_extends_list:
/* empty */ { $$ = []; }
| T_EXTENDS name_list { $$ = $2; }
;
implements_list:
/* empty */ { $$ = []; }
| T_IMPLEMENTS name_list { $$ = $2; }
;
name_list:
name { $$ = [$1]; }
| name_list ',' name { $1.push( $3 ); $$ = $1; }
;
for_statement:
statement { $$ = Array.isArray($1) ? $1 : [$1]; }
| ':' inner_statement_list T_ENDFOR ';' { $$ = $2; }
;
foreach_statement:
statement { $$ = Array.isArray($1) ? $1 : [$1]; }
| ':' inner_statement_list T_ENDFOREACH ';' { $$ = $2; }
;
declare_statement:
statement { $$ = Array.isArray($1) ? $1 : [$1]; }
| ':' inner_statement_list T_ENDDECLARE ';' { $$ = $2; }
;
declare_list:
declare_list_element { $$ = [$1]; }
| declare_list ',' declare_list_element { $1.push( $3 ); $$ = $1; }
;
declare_list_element:
T_STRING '=' static_scalar { $$ = this.Node_Stmt_DeclareDeclare($1, $3, attributes); }
;
switch_case_list:
'{' case_list '}' { $$ = $2; }
| '{' ';' case_list '}' { $$ = $3; }
| ':' case_list T_ENDSWITCH ';' { $$ = $2; }
| ':' ';' case_list T_ENDSWITCH ';' { $$ = $3; }
;
case_list:
/* empty */ { $$ = []; }
| case_list case { $1.push( $2 ); $$ = $1; }
;
case:
T_CASE expr case_separator inner_statement_list { $$ = this.Node_Stmt_Case($2, $4, attributes); }
| T_DEFAULT case_separator inner_statement_list { $$ = this.Node_Stmt_Case(null, $3, attributes); }
;
case_separator:
':'
| ';'
;
while_statement:
statement { $$ = Array.isArray($1) ? $1 : [$1]; }
| ':' inner_statement_list T_ENDWHILE ';' { $$ = $2; }
;
elseif_list:
/* empty */ { $$ = []; }
| elseif_list elseif { $1.push( $2 ); $$ = $1; }
;
elseif:
T_ELSEIF '(' expr ')' statement { $$ = this.Node_Stmt_ElseIf($3, Array.isArray($5) ? $5 : [$5], attributes); }
;
new_elseif_list:
/* empty */ { $$ = []; }
| new_elseif_list new_elseif { $1.push( $2 ); $$ = $1; }
;
new_elseif:
T_ELSEIF '(' expr ')' ':' inner_statement_list { $$ = this.Node_Stmt_ElseIf($3, $6, attributes); }
;
else_single:
/* empty */ { $$ = null; }
| T_ELSE statement { $$ = this.Node_Stmt_Else(Array.isArray($2) ? $2 : [$2], attributes); }
;
new_else_single:
/* empty */ { $$ = null; }
| T_ELSE ':' inner_statement_list { $$ = this.Node_Stmt_Else($3, attributes); }
;
parameter_list:
non_empty_parameter_list { $$ = $1; }
| /* empty */ { $$ = []; }
;
non_empty_parameter_list:
parameter { $$ = [$1]; }
| non_empty_parameter_list ',' parameter { $1.push( $3 ); $$ = $1; }
;
parameter:
optional_class_type optional_ref T_VARIABLE
{ $$ = this.Node_Param($3.substring( 1 ), null, $1, $2, attributes); }
| optional_class_type optional_ref T_VARIABLE '=' static_scalar
{ $$ = this.Node_Param($3.substring( 1 ), $5, $1, $2, attributes); }
;
optional_class_type:
/* empty */ { $$ = null; }
| name { $$ = $1; }
| T_ARRAY { $$ = 'array'; }
| T_CALLABLE { $$ = 'callable'; }
;
argument_list:
non_empty_argument_list { $$ = $1; }
| /* empty */ { $$ = []; }
;
non_empty_argument_list:
argument { $$ = [$1]; }
| non_empty_argument_list ',' argument { $1.push( $3 ); $$ = $1; }
;
argument:
expr { $$ = this.Node_Arg($1, false, attributes); }
| '&' variable { $$ = this.Node_Arg($2, true, attributes); }
;
global_var_list:
global_var_list ',' global_var { $1.push( $3 ); $$ = $1; }
| global_var { $$ = [$1]; }
;
global_var:
T_VARIABLE { $$ = this.Node_Expr_Variable($1.substring( 1 ), attributes); }
| '$' variable { $$ = this.Node_Expr_Variable($2, attributes); }
| '$' '{' expr '}' { $$ = this.Node_Expr_Variable($3, attributes); }
;
static_var_list:
static_var_list ',' static_var { $1.push( $3 ); $$ = $1; }
| static_var { $$ = [$1]; }
;
static_var:
T_VARIABLE { $$ = this.Node_Stmt_StaticVar($1.substring( 1 ), null, attributes); }
| T_VARIABLE '=' static_scalar { $$ = this.Node_Stmt_StaticVar($1.substring( 1 ), $3, attributes); }
;
class_statement_list:
class_statement_list class_statement { $1.push( $2 ); $$ = $1; }
| /* empty */ { $$ = []; }
;
class_statement:
variable_modifiers property_declaration_list ';' { $$ = this.Node_Stmt_Property($1, $2, attributes); }
| T_CONST constant_declaration_list ';' { $$ = this.Node_Stmt_ClassConst($2, attributes); }
| method_modifiers T_FUNCTION optional_ref T_STRING '(' parameter_list ')' method_body
{ $$ = this.Node_Stmt_ClassMethod($4, {'type': $1, 'byRef': $3, 'params': $6, 'stmts': $8}, attributes); }
| T_USE name_list trait_adaptations { $$ = this.Node_Stmt_TraitUse($2, $3, attributes); }
;
trait_adaptations:
';' { $$ = []; }
| '{' trait_adaptation_list '}' { $$ = $2; }
;
trait_adaptation_list:
/* empty */ { $$ = []; }
| trait_adaptation_list trait_adaptation { $1.push( $2 ); $$ = $1; }
;
trait_adaptation:
trait_method_reference_fully_qualified T_INSTEADOF name_list ';'
{ $$ = this.Node_Stmt_TraitUseAdaptation_Precedence($1[0], $1[1], $3, attributes); }
| trait_method_reference T_AS member_modifier T_STRING ';'
{ $$ = this.Node_Stmt_TraitUseAdaptation_Alias($1[0], $1[1], $3, $4, attributes); }
| trait_method_reference T_AS member_modifier ';'
{ $$ = this.Node_Stmt_TraitUseAdaptation_Alias($1[0], $1[1], $3, null, attributes); }
| trait_method_reference T_AS T_STRING ';'
{ $$ = this.Node_Stmt_TraitUseAdaptation_Alias($1[0], $1[1], null, $3, attributes); }
;
trait_method_reference_fully_qualified:
name T_PAAMAYIM_NEKUDOTAYIM T_STRING { $$ = array($1, $3); }
;
trait_method_reference:
trait_method_reference_fully_qualified { $$ = $1; }
| T_STRING { $$ = array(null, $1); }
;
method_body:
';' /* abstract method */ { $$ = null; }
| '{' inner_statement_list '}' { $$ = $2; }
;
variable_modifiers:
non_empty_member_modifiers { $$ = $1; }
| T_VAR { $$ = this.MODIFIER_PUBLIC; }
;
method_modifiers:
/* empty */ { $$ = this.MODIFIER_PUBLIC; }
| non_empty_member_modifiers { $$ = $1; }
;
non_empty_member_modifiers:
member_modifier { $$ = $1; }
| non_empty_member_modifiers member_modifier { this.Stmt_Class_verifyModifier($1, $2); $$ = $1 | $2; }
;
member_modifier:
T_PUBLIC { $$ = this.MODIFIER_PUBLIC; }
| T_PROTECTED { $$ = this.MODIFIER_PROTECTED; }
| T_PRIVATE { $$ = this.MODIFIER_PRIVATE; }
| T_STATIC { $$ = this.MODIFIER_STATIC; }
| T_ABSTRACT { $$ = this.MODIFIER_ABSTRACT; }
| T_FINAL { $$ = this.MODIFIER_FINAL; }
;
property_declaration_list:
property_declaration { $$ = [$1]; }
| property_declaration_list ',' property_declaration { $1.push( $3 ); $$ = $1; }
;
property_declaration:
T_VARIABLE { $$ = this.Node_Stmt_PropertyProperty($1.substring( 1 ), null, attributes); }
| T_VARIABLE '=' static_scalar { $$ = this.Node_Stmt_PropertyProperty($1.substring( 1 ), $3, attributes); }
;
expr_list:
expr_list ',' expr { $1.push( $3 ); $$ = $1; }
| expr { $$ = [$1]; }
;
for_expr:
/* empty */ { $$ = []; }
| expr_list { $$ = $1; }
;
expr:
variable { $$ = $1; }
| T_LIST '(' assignment_list ')' '=' expr { $$ = this.Node_Expr_AssignList($3, $6, attributes); }
| variable '=' expr { $$ = this.Node_Expr_Assign($1, $3, attributes); }
| variable '=' '&' variable { $$ = this.Node_Expr_AssignRef($1, $4, attributes); }
| variable '=' '&' new_expr { $$ = this.Node_Expr_AssignRef($1, $4, attributes); }
| new_expr { $$ = $1; }
| T_CLONE expr { $$ = this.Node_Expr_Clone($2, attributes); }
| variable T_PLUS_EQUAL expr { $$ = this.Node_Expr_AssignPlus($1, $3, attributes); }
| variable T_MINUS_EQUAL expr { $$ = this.Node_Expr_AssignMinus($1, $3, attributes); }
| variable T_MUL_EQUAL expr { $$ = this.Node_Expr_AssignMul($1, $3, attributes); }
| variable T_DIV_EQUAL expr { $$ = this.Node_Expr_AssignDiv($1, $3, attributes); }
| variable T_CONCAT_EQUAL expr { $$ = this.Node_Expr_AssignConcat($1, $3, attributes); }
| variable T_MOD_EQUAL expr { $$ = this.Node_Expr_AssignMod($1, $3, attributes); }
| variable T_AND_EQUAL expr { $$ = this.Node_Expr_AssignBitwiseAnd($1, $3, attributes); }
| variable T_OR_EQUAL expr { $$ = this.Node_Expr_AssignBitwiseOr($1, $3, attributes); }
| variable T_XOR_EQUAL expr { $$ = this.Node_Expr_AssignBitwiseXor($1, $3, attributes); }
| variable T_SL_EQUAL expr { $$ = this.Node_Expr_AssignShiftLeft($1, $3, attributes); }
| variable T_SR_EQUAL expr { $$ = this.Node_Expr_AssignShiftRight($1, $3, attributes); }
| variable T_INC { $$ = this.Node_Expr_PostInc($1, attributes); }
| T_INC variable { $$ = this.Node_Expr_PreInc($2, attributes); }
| variable T_DEC { $$ = this.Node_Expr_PostDec($1, attributes); }
| T_DEC variable { $$ = this.Node_Expr_PreDec($2, attributes); }
| expr T_BOOLEAN_OR expr { $$ = this.Node_Expr_BooleanOr($1, $3, attributes); }
| expr T_BOOLEAN_AND expr { $$ = this.Node_Expr_BooleanAnd($1, $3, attributes); }
| expr T_LOGICAL_OR expr { $$ = this.Node_Expr_LogicalOr($1, $3, attributes); }
| expr T_LOGICAL_AND expr { $$ = this.Node_Expr_LogicalAnd($1, $3, attributes); }
| expr T_LOGICAL_XOR expr { $$ = this.Node_Expr_LogicalXor($1, $3, attributes); }
| expr '|' expr { $$ = this.Node_Expr_BitwiseOr($1, $3, attributes); }
| expr '&' expr { $$ = this.Node_Expr_BitwiseAnd($1, $3, attributes); }
| expr '^' expr { $$ = this.Node_Expr_BitwiseXor($1, $3, attributes); }
| expr '.' expr { $$ = this.Node_Expr_Concat($1, $3, attributes); }
| expr '+' expr { $$ = this.Node_Expr_Plus($1, $3, attributes); }
| expr '-' expr { $$ = this.Node_Expr_Minus($1, $3, attributes); }
| expr '*' expr { $$ = this.Node_Expr_Mul($1, $3, attributes); }
| expr '/' expr { $$ = this.Node_Expr_Div($1, $3, attributes); }
| expr '%' expr { $$ = this.Node_Expr_Mod($1, $3, attributes); }
| expr T_SL expr { $$ = this.Node_Expr_ShiftLeft($1, $3, attributes); }
| expr T_SR expr { $$ = this.Node_Expr_ShiftRight($1, $3, attributes); }
| '+' expr %prec T_INC { $$ = this.Node_Expr_UnaryPlus($2, attributes); }
| '-' expr %prec T_INC { $$ = this.Node_Expr_UnaryMinus($2, attributes); }
| '!' expr { $$ = this.Node_Expr_BooleanNot($2, attributes); }
| '~' expr { $$ = this.Node_Expr_BitwiseNot($2, attributes); }
| expr T_IS_IDENTICAL expr { $$ = this.Node_Expr_Identical($1, $3, attributes); }
| expr T_IS_NOT_IDENTICAL expr { $$ = this.Node_Expr_NotIdentical($1, $3, attributes); }
| expr T_IS_EQUAL expr { $$ = this.Node_Expr_Equal($1, $3, attributes); }
| expr T_IS_NOT_EQUAL expr { $$ = this.Node_Expr_NotEqual($1, $3, attributes); }
| expr '<' expr { $$ = this.Node_Expr_Smaller($1, $3, attributes); }
| expr T_IS_SMALLER_OR_EQUAL expr { $$ = this.Node_Expr_SmallerOrEqual($1, $3, attributes); }
| expr '>' expr { $$ = this.Node_Expr_Greater($1, $3, attributes); }
| expr T_IS_GREATER_OR_EQUAL expr { $$ = this.Node_Expr_GreaterOrEqual($1, $3, attributes); }
| expr T_INSTANCEOF class_name_reference { $$ = this.Node_Expr_Instanceof($1, $3, attributes); }
| '(' expr ')' { $$ = $2; }
/* we need a separate '(' new_expr ')' rule to avoid problems caused by a s/r conflict */
| '(' new_expr ')' { $$ = $2; }
| expr '?' expr ':' expr { $$ = this.Node_Expr_Ternary($1, $3, $5, attributes); }
| expr '?' ':' expr { $$ = this.Node_Expr_Ternary($1, null, $4, attributes); }
| T_ISSET '(' variables_list ')' { $$ = this.Node_Expr_Isset($3, attributes); }
| T_EMPTY '(' variable ')' { $$ = this.Node_Expr_Empty($3, attributes); }
| T_INCLUDE expr { $$ = this.Node_Expr_Include($2, "Node_Expr_Include", attributes); }
| T_INCLUDE_ONCE expr { $$ = this.Node_Expr_Include($2, "Node_Expr_IncludeOnce", attributes); }
| T_EVAL '(' expr ')' { $$ = this.Node_Expr_Eval($3, attributes); }
| T_REQUIRE expr { $$ = this.Node_Expr_Include($2, "Node_Expr_Require", attributes); }
| T_REQUIRE_ONCE expr { $$ = this.Node_Expr_Include($2, "Node_Expr_RequireOnce", attributes); }
| T_INT_CAST expr { $$ = this.Node_Expr_Cast_Int($2, attributes); }
| T_DOUBLE_CAST expr { $$ = this.Node_Expr_Cast_Double($2, attributes); }
| T_STRING_CAST expr { $$ = this.Node_Expr_Cast_String($2, attributes); }
| T_ARRAY_CAST expr { $$ = this.Node_Expr_Cast_Array($2, attributes); }
| T_OBJECT_CAST expr { $$ = this.Node_Expr_Cast_Object($2, attributes); }
| T_BOOL_CAST expr { $$ = this.Node_Expr_Cast_Bool($2, attributes); }
| T_UNSET_CAST expr { $$ = this.Node_Expr_Cast_Unset($2, attributes); }
| T_EXIT exit_expr { $$ = this.Node_Expr_Exit($2, attributes); }
| '@' expr { $$ = this.Node_Expr_ErrorSuppress($2, attributes); }
| scalar { $$ = $1; }
| T_ARRAY '(' array_pair_list ')' { $$ = this.Node_Expr_Array($3, attributes); }
| '[' array_pair_list ']' { $$ = this.Node_Expr_Array($2, attributes); }
| '`' backticks_expr '`' { $$ = this.Node_Expr_ShellExec($2, attributes); }
| T_PRINT expr { $$ = this.Node_Expr_Print($2, attributes); }
| T_FUNCTION optional_ref '(' parameter_list ')' lexical_vars '{' inner_statement_list '}'
{ $$ = this.Node_Expr_Closure({'static': false, 'byRef': $2, 'params': $4, 'uses': $6, 'stmts': $8}, attributes); }
| T_STATIC T_FUNCTION optional_ref '(' parameter_list ')' lexical_vars '{' inner_statement_list '}'
{ $$ = this.Node_Expr_Closure({'static': true, 'byRef': $3, 'params': $5, 'uses': $7, 'stmts': $9}, attributes); }
;
new_expr:
T_NEW class_name_reference ctor_arguments { $$ = this.Node_Expr_New($2, $3, attributes); }
;
lexical_vars:
/* empty */ { $$ = []; }
| T_USE '(' lexical_var_list ')' { $$ = $3; }
;
lexical_var_list:
lexical_var { $$ = [$1]; }
| lexical_var_list ',' lexical_var { $1.push( $3 ); $$ = $1; }
;
lexical_var:
optional_ref T_VARIABLE { $$ = this.Node_Expr_ClosureUse($2.substring( 1 ), $1, attributes); }
;
function_call:
name '(' argument_list ')' { $$ = this.Node_Expr_FuncCall($1, $3, attributes); }
| class_name_or_var T_PAAMAYIM_NEKUDOTAYIM T_STRING '(' argument_list ')'
{ $$ = this.Node_Expr_StaticCall($1, $3, $5, attributes); }
| class_name_or_var T_PAAMAYIM_NEKUDOTAYIM '{' expr '}' '(' argument_list ')'
{ $$ = this.Node_Expr_StaticCall($1, $4, $7, attributes); }
| static_property '(' argument_list ')' {
if ($1.type === "Node_Expr_StaticPropertyFetch") {
$$ = this.Node_Expr_StaticCall($1.Class, this.Node_Expr_Variable($1.name, attributes), $3, attributes);
} else if ($1.type === "Node_Expr_ArrayDimFetch") {
var tmp = $1;
while (tmp.variable.type === "Node_Expr_ArrayDimFetch") {
tmp = tmp.variable;
}
$$ = this.Node_Expr_StaticCall(tmp.variable.Class, $1, $3, attributes);
tmp.variable = this.Node_Expr_Variable(tmp.variable.name, attributes);
} else {
throw new Exception;
}
}
| variable_without_objects '(' argument_list ')'
{ $$ = this.Node_Expr_FuncCall($1, $3, attributes); }
| function_call '[' dim_offset ']' { $$ = this.Node_Expr_ArrayDimFetch($1, $3, attributes); }
/* alternative array syntax missing intentionally */
;
class_name:
T_STATIC { $$ = this.Node_Name('static', attributes); }
| name { $$ = $1; }
;
name:
namespace_name { $$ = this.Node_Name($1, attributes); }
| T_NS_SEPARATOR namespace_name { $$ = this.Node_Name_FullyQualified($2, attributes); }
| T_NAMESPACE T_NS_SEPARATOR namespace_name { $$ = this.Node_Name_Relative($3, attributes); }
;
class_name_reference:
class_name { $$ = $1; }
| dynamic_class_name_reference { $$ = $1; }
;
dynamic_class_name_reference:
object_access_for_dcnr { $$ = $1; }
| base_variable { $$ = $1; }
;
class_name_or_var:
class_name { $$ = $1; }
| reference_variable { $$ = $1; }
;
object_access_for_dcnr:
| base_variable T_OBJECT_OPERATOR object_property
{ $$ = this.Node_Expr_PropertyFetch($1, $3, attributes); }
| object_access_for_dcnr T_OBJECT_OPERATOR object_property
{ $$ = this.Node_Expr_PropertyFetch($1, $3, attributes); }
| object_access_for_dcnr '[' dim_offset ']' { $$ = this.Node_Expr_ArrayDimFetch($1, $3, attributes); }
| object_access_for_dcnr '{' expr '}' { $$ = this.Node_Expr_ArrayDimFetch($1, $3, attributes); }
;
exit_expr:
/* empty */ { $$ = null; }
| '(' ')' { $$ = null; }
| '(' expr ')' { $$ = $2; }
;
backticks_expr:
/* empty */ { $$ = []; }
| T_ENCAPSED_AND_WHITESPACE { $$ = [this.Scalar_String_parseEscapeSequences($1, '`')]; }
| encaps_list { ; $$ = $1; }
;
ctor_arguments:
/* empty */ { $$ = []; }
| '(' argument_list ')' { $$ = $2; }
;
common_scalar:
T_LNUMBER { $$ = this.Node_Scalar_LNumber(this.Scalar_LNumber_parse($1), attributes); }
| T_DNUMBER { $$ = this.Node_Scalar_DNumber(this.Scalar_DNumber_parse($1), attributes); }
| T_CONSTANT_ENCAPSED_STRING { $$ = this.Scalar_String_create($1, attributes); }
| T_LINE { $$ = {type: "Node_Scalar_LineConst", attributes: attributes}; }
| T_FILE { $$ = {type: "Node_Scalar_FileConst", attributes: attributes}; }
| T_DIR { $$ = {type: "Node_Scalar_DirConst", attributes: attributes}; }
| T_CLASS_C { $$ = {type: "Node_Scalar_ClassConst", attributes: attributes}; }
| T_TRAIT_C { $$ = {type: "Node_Scalar_TraitConst", attributes: attributes}; }
| T_METHOD_C { $$ = {type: "Node_Scalar_MethodConst", attributes: attributes}; }
| T_FUNC_C { $$ = {type: "Node_Scalar_FuncConst", attributes: attributes}; }
| T_NS_C { $$ = {type: "Node_Scalar_NSConst", attributes: attributes}; }
| T_START_HEREDOC T_ENCAPSED_AND_WHITESPACE T_END_HEREDOC
{ $$ = this.Node_Scalar_String(this.Scalar_String_parseDocString($1, $2), attributes); }
| T_START_HEREDOC T_END_HEREDOC
{ $$ = this.Node_Scalar_String('', attributes); }
| name { $$ = this.Node_Expr_ConstFetch($1, attributes); }
;
static_scalar: /* compile-time evaluated scalars */
common_scalar { $$ = $1; }
| class_name T_PAAMAYIM_NEKUDOTAYIM T_STRING { $$ = this.Node_Expr_ClassConstFetch($1, $3, attributes); }
| '+' static_scalar { $$ = this.Node_Expr_UnaryPlus($2, attributes); }
| '-' static_scalar { $$ = this.Node_Expr_UnaryMinus($2, attributes); }
| T_ARRAY '(' static_array_pair_list ')' { $$ = this.Node_Expr_Array($3, attributes); }
| '[' static_array_pair_list ']' { $$ = this.Node_Expr_Array($2, attributes); }
;
scalar:
common_scalar { $$ = $1; }
| class_name_or_var T_PAAMAYIM_NEKUDOTAYIM T_STRING { $$ = this.Node_Expr_ClassConstFetch($1, $3, attributes); }
| '"' encaps_list '"'
{ ; $$ = this.Node_Scalar_Encapsed($2, attributes); }
| T_START_HEREDOC encaps_list T_END_HEREDOC
{ ; $$ = this.Node_Scalar_Encapsed($2, attributes); }
;
static_array_pair_list:
/* empty */ { $$ = []; }
| non_empty_static_array_pair_list optional_comma { $$ = $1; }
;
optional_comma:
/* empty */
| ','
;
non_empty_static_array_pair_list:
non_empty_static_array_pair_list ',' static_array_pair { $1.push( $3 ); $$ = $1; }
| static_array_pair { $$ = [$1]; }
;
static_array_pair:
static_scalar T_DOUBLE_ARROW static_scalar { $$ = this.Node_Expr_ArrayItem($3, $1, false, attributes); }
| static_scalar { $$ = this.Node_Expr_ArrayItem($1, null, false, attributes); }
;
variable:
object_access { $$ = $1; }
| base_variable { $$ = $1; }
| function_call { $$ = $1; }
| new_expr_array_deref { $$ = $1; }
;
new_expr_array_deref:
'(' new_expr ')' '[' dim_offset ']' { $$ = this.Node_Expr_ArrayDimFetch($2, $5, attributes); }
| new_expr_array_deref '[' dim_offset ']' { $$ = this.Node_Expr_ArrayDimFetch($1, $3, attributes); }
/* alternative array syntax missing intentionally */
;
object_access:
variable_or_new_expr T_OBJECT_OPERATOR object_property
{ $$ = this.Node_Expr_PropertyFetch($1, $3, attributes); }
| variable_or_new_expr T_OBJECT_OPERATOR object_property '(' argument_list ')'
{ $$ = this.Node_Expr_MethodCall($1, $3, $5, attributes); }
| object_access '(' argument_list ')' { $$ = this.Node_Expr_FuncCall($1, $3, attributes); }
| object_access '[' dim_offset ']' { $$ = this.Node_Expr_ArrayDimFetch($1, $3, attributes); }
| object_access '{' expr '}' { $$ = this.Node_Expr_ArrayDimFetch($1, $3, attributes); }
;
variable_or_new_expr:
variable { $$ = $1; }
| '(' new_expr ')' { $$ = $2; }
;
variable_without_objects:
reference_variable { $$ = $1; }
| '$' variable_without_objects { $$ = this.Node_Expr_Variable($2, attributes); }
;
base_variable:
variable_without_objects { $$ = $1; }
| static_property { $$ = $1; }
;
static_property:
class_name_or_var T_PAAMAYIM_NEKUDOTAYIM '$' reference_variable
{ $$ = this.Node_Expr_StaticPropertyFetch($1, $4, attributes); }
| static_property_with_arrays { $$ = $1; }
;
static_property_with_arrays:
class_name_or_var T_PAAMAYIM_NEKUDOTAYIM T_VARIABLE
{ $$ = this.Node_Expr_StaticPropertyFetch($1, $3.substring( 1 ), attributes); }
| class_name_or_var T_PAAMAYIM_NEKUDOTAYIM '$' '{' expr '}'
{ $$ = this.Node_Expr_StaticPropertyFetch($1, $5, attributes); }
| static_property_with_arrays '[' dim_offset ']' { $$ = this.Node_Expr_ArrayDimFetch($1, $3, attributes); }
| static_property_with_arrays '{' expr '}' { $$ = this.Node_Expr_ArrayDimFetch($1, $3, attributes); }
;
reference_variable:
reference_variable '[' dim_offset ']' { $$ = this.Node_Expr_ArrayDimFetch($1, $3, attributes); }
| reference_variable '{' expr '}' { $$ = this.Node_Expr_ArrayDimFetch($1, $3, attributes); }
| T_VARIABLE { $$ = this.Node_Expr_Variable($1.substring( 1 ), attributes); }
| '$' '{' expr '}' { $$ = this.Node_Expr_Variable($3, attributes); }
;
dim_offset:
/* empty */ { $$ = null; }
| expr { $$ = $1; }
;
object_property:
T_STRING { $$ = $1; }
| '{' expr '}' { $$ = $2; }
| variable_without_objects { $$ = $1; }
;
assignment_list:
assignment_list ',' assignment_list_element { $1.push( $3 ); $$ = $1; }
| assignment_list_element { $$ = [$1]; }
;
assignment_list_element:
variable { $$ = $1; }
| T_LIST '(' assignment_list ')' { $$ = $3; }
| /* empty */ { $$ = null; }
;
array_pair_list:
/* empty */ { $$ = []; }
| non_empty_array_pair_list optional_comma { $$ = $1; }
;
non_empty_array_pair_list:
non_empty_array_pair_list ',' array_pair { $1.push( $3 ); $$ = $1; }
| array_pair { $$ = [$1]; }
;
array_pair:
expr T_DOUBLE_ARROW expr { $$ = this.Node_Expr_ArrayItem($3, $1, false, attributes); }
| expr { $$ = this.Node_Expr_ArrayItem($1, null, false, attributes); }
| expr T_DOUBLE_ARROW '&' variable { $$ = this.Node_Expr_ArrayItem($4, $1, true, attributes); }
| '&' variable { $$ = this.Node_Expr_ArrayItem($2, null, true, attributes); }
;
encaps_list:
encaps_list encaps_var { $1.push( $2 ); $$ = $1; }
| encaps_list T_ENCAPSED_AND_WHITESPACE { $1.push( $2 ); $$ = $1; }
| encaps_var { $$ = [$1]; }
| T_ENCAPSED_AND_WHITESPACE encaps_var { $$ = [$1, $2]; }
;
encaps_var:
T_VARIABLE { $$ = this.Node_Expr_Variable($1.substring( 1 ), attributes); }
| T_VARIABLE '[' encaps_var_offset ']' { $$ = this.Node_Expr_ArrayDimFetch(this.Node_Expr_Variable($1.substring( 1 ), attributes), $3, attributes); }
| T_VARIABLE T_OBJECT_OPERATOR T_STRING { $$ = this.Node_Expr_PropertyFetch(this.Node_Expr_Variable($1.substring( 1 ), attributes), $3, attributes); }
| T_DOLLAR_OPEN_CURLY_BRACES expr '}' { $$ = this.Node_Expr_Variable($2, attributes); }
| T_DOLLAR_OPEN_CURLY_BRACES T_STRING_VARNAME '}' { $$ = this.Node_Expr_Variable($2, attributes); }
| T_DOLLAR_OPEN_CURLY_BRACES T_STRING_VARNAME '[' expr ']' '}'
{ $$ = this.Node_Expr_ArrayDimFetch(this.Node_Expr_Variable($2, attributes), $4, attributes); }
| T_CURLY_OPEN variable '}' { $$ = $2; }
;
encaps_var_offset:
T_STRING { $$ = this.Node_Scalar_String($1, attributes); }
| T_NUM_STRING { $$ = this.Node_Scalar_String($1, attributes); }
| T_VARIABLE { $$ = this.Node_Expr_Variable($1.substring( 1 ), attributes); }
;
%%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment