fix break and foreach nodes

This commit is contained in:
vadim 2017-12-22 14:07:38 +02:00
parent ae6fe52005
commit 9f48ca2968
4 changed files with 1389 additions and 1371 deletions

View File

@ -24,6 +24,9 @@ func NewBreak(token token.Token, expr node.Node) node.Node {
func (n Break) Print(out io.Writer, indent string) { func (n Break) Print(out io.Writer, indent string) {
fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.Name, n.token.StartLine, n.token.EndLine, n.token.Value) fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.Name, n.token.StartLine, n.token.EndLine, n.token.Value)
fmt.Fprintf(out, "\n%vexpr:", indent+" ")
n.expr.Print(out, indent+" ") if n.expr != nil {
fmt.Fprintf(out, "\n%vexpr:", indent+" ")
n.expr.Print(out, indent+" ")
}
} }

View File

@ -15,9 +15,10 @@ type Foreach struct {
key node.Node key node.Node
variable node.Node variable node.Node
stmt node.Node stmt node.Node
byRef bool
} }
func NewForeach(token token.Token, expr node.Node, key node.Node, variable node.Node, stmt node.Node) node.Node { func NewForeach(token token.Token, expr node.Node, key node.Node, variable node.Node, stmt node.Node, byRef bool) node.Node {
return Foreach{ return Foreach{
node.SimpleNode{Name: "Foreach", Attributes: make(map[string]string)}, node.SimpleNode{Name: "Foreach", Attributes: make(map[string]string)},
token, token,
@ -25,6 +26,7 @@ func NewForeach(token token.Token, expr node.Node, key node.Node, variable node.
key, key,
variable, variable,
stmt, stmt,
byRef,
} }
} }
@ -42,7 +44,7 @@ func (n Foreach) Print(out io.Writer, indent string) {
} }
if n.variable != nil { if n.variable != nil {
fmt.Fprintf(out, "\n%vvariable:", indent+" ") fmt.Fprintf(out, "\n%vvariable[byRef: %t]:", indent+" ", n.byRef)
n.variable.Print(out, indent+" ") n.variable.Print(out, indent+" ")
} }

File diff suppressed because it is too large Load Diff

View File

@ -26,6 +26,11 @@ func Parse(src io.Reader, fName string) node.Node {
return rootnode return rootnode
} }
type foreachVariable struct {
node node.Node
byRef bool
}
%} %}
%union{ %union{
@ -34,6 +39,7 @@ func Parse(src io.Reader, fName string) node.Node {
value string value string
list []node.Node list []node.Node
strings []string strings []string
foreachVariable foreachVariable
} }
%left T_INCLUDE T_INCLUDE_ONCE T_EVAL T_REQUIRE T_REQUIRE_ONCE %left T_INCLUDE T_INCLUDE_ONCE T_EVAL T_REQUIRE T_REQUIRE_ONCE
@ -196,7 +202,7 @@ func Parse(src io.Reader, fName string) node.Node {
%type <node> group_use_declaration inline_use_declaration %type <node> group_use_declaration inline_use_declaration
%type <node> mixed_group_use_declaration use_declaration unprefixed_use_declaration %type <node> mixed_group_use_declaration use_declaration unprefixed_use_declaration
%type <node> const_decl inner_statement %type <node> const_decl inner_statement
%type <node> expr optional_expr while_statement for_statement foreach_variable %type <node> expr optional_expr while_statement for_statement
%type <node> foreach_statement declare_statement finally_statement unset_variable variable %type <node> foreach_statement declare_statement finally_statement unset_variable variable
%type <node> extends_from parameter optional_type argument expr_without_variable global_var %type <node> extends_from parameter optional_type argument expr_without_variable global_var
%type <node> static_var class_statement trait_adaptation trait_precedence trait_alias %type <node> static_var class_statement trait_adaptation trait_precedence trait_alias
@ -217,6 +223,7 @@ func Parse(src io.Reader, fName string) node.Node {
%type <node> member_modifier %type <node> member_modifier
%type <node> use_type %type <node> use_type
%type <foreachVariable> foreach_variable
%type <strings> class_modifiers %type <strings> class_modifiers
%type <list> encaps_list backticks_expr namespace_name catch_name_list catch_list class_const_list %type <list> encaps_list backticks_expr namespace_name catch_name_list catch_list class_const_list
@ -293,7 +300,7 @@ top_statement:
use_type: use_type:
T_FUNCTION { $$ = node.NewIdentifier($1) } T_FUNCTION { $$ = node.NewIdentifier($1) }
| T_CONST { $$ = node.NewSimpleNode("ConstUseType"); } | T_CONST { $$ = node.NewIdentifier($1) }
; ;
group_use_declaration: group_use_declaration:
@ -396,9 +403,9 @@ statement:
| T_UNSET '(' unset_variables possible_comma ')' ';' | T_UNSET '(' unset_variables possible_comma ')' ';'
{ $$ = stmt.NewUnset($1, $3) } { $$ = stmt.NewUnset($1, $3) }
| T_FOREACH '(' expr T_AS foreach_variable ')' foreach_statement | T_FOREACH '(' expr T_AS foreach_variable ')' foreach_statement
{ $$ = stmt.NewForeach($1, $3, nil, $5, $7); } { $$ = stmt.NewForeach($1, $3, nil, $5.node, $7, $5.byRef); }
| T_FOREACH '(' expr T_AS foreach_variable T_DOUBLE_ARROW foreach_variable ')' foreach_statement | T_FOREACH '(' expr T_AS variable T_DOUBLE_ARROW foreach_variable ')' foreach_statement
{ $$ = stmt.NewForeach($1, $3, $5, $7, $9); } { $$ = stmt.NewForeach($1, $3, $5, $7.node, $9, $7.byRef); }
| T_DECLARE '(' const_list ')' declare_statement { $$ = stmt.NewDeclare($1, $3, $5) } | T_DECLARE '(' const_list ')' declare_statement { $$ = stmt.NewDeclare($1, $3, $5) }
| ';' { $$ = stmt.NewNop($1) } | ';' { $$ = stmt.NewNop($1) }
| T_TRY '{' inner_statement_list '}' catch_list finally_statement | T_TRY '{' inner_statement_list '}' catch_list finally_statement
@ -492,10 +499,10 @@ implements_list:
; ;
foreach_variable: foreach_variable:
variable { $$ = $1; } variable { $$ = foreachVariable{$1, false} }
| '&' variable { $$ = node.NewSimpleNode("Ref").Append($2); } | '&' variable { $$ = foreachVariable{$2, true} }
| T_LIST '(' array_pair_list ')' { $$ = expr.NewList($3) } | T_LIST '(' array_pair_list ')' { $$ = foreachVariable{expr.NewList($3), false} }
| '[' array_pair_list ']' { $$ = expr.NewShortList($2) } | '[' array_pair_list ']' { $$ = foreachVariable{expr.NewShortList($2), false} }
; ;
for_statement: for_statement:
@ -933,7 +940,7 @@ function_call:
; ;
class_name: class_name:
T_STATIC { $$ = node.NewSimpleNode("Static") } T_STATIC { $$ = node.NewIdentifier($1) }
| name { $$ = $1; } | name { $$ = $1; }
; ;
@ -943,7 +950,7 @@ class_name_reference:
; ;
exit_expr: exit_expr:
/* empty */ { $$ = node.NewSimpleNode("") } /* empty */ { $$ = nil }
| '(' optional_expr ')' { $$ = $2; } | '(' optional_expr ')' { $$ = $2; }
; ;
@ -998,7 +1005,7 @@ expr:
; ;
optional_expr: optional_expr:
/* empty */ { $$ = node.NewSimpleNode("optional node. TODO: must be nil") } /* empty */ { $$ = nil }
| expr { $$ = $1; } | expr { $$ = $1; }
; ;