remove SimpleNodes and handle num strings in encaps_var_offset

This commit is contained in:
vadim 2017-12-27 15:53:46 +02:00
parent 1d36f39509
commit 84887c66c3
2 changed files with 410 additions and 384 deletions

File diff suppressed because it is too large Load Diff

View File

@ -5,6 +5,7 @@ import (
"io" "io"
"fmt" "fmt"
"strings" "strings"
"strconv"
"github.com/z7zmey/php-parser/token" "github.com/z7zmey/php-parser/token"
"github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/node/scalar" "github.com/z7zmey/php-parser/node/scalar"
@ -216,7 +217,7 @@ type foreachVariable struct {
%type <node> if_stmt %type <node> if_stmt
%type <node> alt_if_stmt %type <node> alt_if_stmt
%type <node> if_stmt_without_else %type <node> if_stmt_without_else
%type <node> class_const_decl name_list %type <node> class_const_decl
%type <node> alt_if_stmt_without_else %type <node> alt_if_stmt_without_else
%type <node> array_pair possible_array_pair %type <node> array_pair possible_array_pair
%type <node> isset_variable type return_type type_expr %type <node> isset_variable type return_type type_expr
@ -234,7 +235,7 @@ type foreachVariable struct {
%type <list> array_pair_list ctor_arguments argument_list non_empty_argument_list top_statement_list %type <list> array_pair_list ctor_arguments argument_list non_empty_argument_list top_statement_list
%type <list> inner_statement_list parameter_list non_empty_parameter_list class_statement_list %type <list> inner_statement_list parameter_list non_empty_parameter_list class_statement_list
%type <list> method_body interface_extends_list implements_list method_modifiers variable_modifiers %type <list> method_body interface_extends_list implements_list method_modifiers variable_modifiers
%type <list> non_empty_member_modifiers %type <list> non_empty_member_modifiers name_list
%% %%
@ -490,12 +491,12 @@ extends_from:
interface_extends_list: interface_extends_list:
/* empty */ { $$ = nil } /* empty */ { $$ = nil }
| T_EXTENDS name_list { $$ = $2.(node.SimpleNode).Children } | T_EXTENDS name_list { $$ = $2 }
; ;
implements_list: implements_list:
/* empty */ { $$ = nil } /* empty */ { $$ = nil }
| T_IMPLEMENTS name_list { $$ = $2.(node.SimpleNode).Children } | T_IMPLEMENTS name_list { $$ = $2 }
; ;
foreach_variable: foreach_variable:
@ -629,13 +630,13 @@ type_expr:
; ;
type: type:
T_ARRAY { $$ = node.NewSimpleNode("array type"); } T_ARRAY { $$ = node.NewIdentifier($1) }
| T_CALLABLE { $$ = node.NewSimpleNode("callable type"); } | T_CALLABLE { $$ = node.NewIdentifier($1) }
| name { $$ = $1; } | name { $$ = $1; }
; ;
return_type: return_type:
/* empty */ { $$ = node.NewSimpleNode("No return type"); } /* empty */ { $$ = nil }
| ':' type_expr { $$ = $2; } | ':' type_expr { $$ = $2; }
; ;
@ -681,7 +682,7 @@ class_statement_list:
class_statement: class_statement:
variable_modifiers property_list ';' { $$ = stmt.NewPropertyList($1, $2) } variable_modifiers property_list ';' { $$ = stmt.NewPropertyList($1, $2) }
| method_modifiers T_CONST class_const_list ';' { $$ = stmt.NewClassConst($2, $1, $3); } | method_modifiers T_CONST class_const_list ';' { $$ = stmt.NewClassConst($2, $1, $3); }
| T_USE name_list trait_adaptations { $$ = stmt.NewTraitUse($1, $2.(node.SimpleNode).Children, $3) } | T_USE name_list trait_adaptations { $$ = stmt.NewTraitUse($1, $2, $3) }
| method_modifiers T_FUNCTION returns_ref identifier '(' parameter_list ')' return_type method_body | method_modifiers T_FUNCTION returns_ref identifier '(' parameter_list ')' return_type method_body
{ {
$$ = stmt.NewClassMethod($4, $1, $3 == "true", $6, $8, $9) $$ = stmt.NewClassMethod($4, $1, $3 == "true", $6, $8, $9)
@ -689,8 +690,8 @@ class_statement:
; ;
name_list: name_list:
name { $$ = node.NewSimpleNode("NameList").Append($1) } name { $$ = []node.Node{$1} }
| name_list ',' name { $$ = $1.Append($3) } | name_list ',' name { $$ = append($1, $3) }
; ;
trait_adaptations: trait_adaptations:
@ -712,8 +713,7 @@ trait_adaptation:
trait_precedence: trait_precedence:
absolute_trait_method_reference T_INSTEADOF name_list absolute_trait_method_reference T_INSTEADOF name_list
{ {
name := name.NewName($3.(node.SimpleNode).Children) $$ = stmt.NewTraitUsePrecedence($1, name.NewName($3))
$$ = stmt.NewTraitUsePrecedence($1, name)
} }
; ;
@ -1066,7 +1066,7 @@ new_variable:
; ;
member_name: member_name:
identifier { $$ = node.NewSimpleNode("MemberName").Attribute("value", $1.Value); } identifier { $$ = node.NewIdentifier($1) }
| '{' expr '}' { $$ = $2; } | '{' expr '}' { $$ = $2; }
| simple_variable { $$ = $1 } | simple_variable { $$ = $1 }
; ;
@ -1136,10 +1136,27 @@ encaps_var:
| T_CURLY_OPEN variable '}' { $$ = $2; } | T_CURLY_OPEN variable '}' { $$ = $2; }
; ;
encaps_var_offset: encaps_var_offset:
T_STRING { $$ = node.NewSimpleNode("OffsetString").Attribute("value", $1.String()) } T_STRING { $$ = scalar.NewString($1) }
| T_NUM_STRING { $$ = node.NewSimpleNode("OffsetNumString").Attribute("value", $1.String()) } | T_NUM_STRING
| '-' T_NUM_STRING { $$ = node.NewSimpleNode("OffsetNegateNumString").Attribute("value", $2.String()) } {
| T_VARIABLE { $$ = node.NewSimpleNode("OffsetVariable").Attribute("value", $1.String()) } // TODO: add option to handle 64 bit integer
if _, err := strconv.Atoi($1.Value); err == nil {
$$ = scalar.NewLnumber($1)
} else {
$$ = scalar.NewString($1)
}
}
| '-' T_NUM_STRING
{
// TODO: add option to handle 64 bit integer
if _, err := strconv.Atoi($2.Value); err == nil {
$$ = expr.NewUnaryMinus(scalar.NewLnumber($2))
} else {
$2.Value = "-"+$2.Value
$$ = scalar.NewString($2)
}
}
| T_VARIABLE { $$ = expr.NewVariable(node.NewIdentifier($1)) }
; ;
internal_functions_in_yacc: internal_functions_in_yacc: