split Foreach and AltForeach nodes

This commit is contained in:
z7zmey 2018-02-18 20:29:33 +02:00
parent 3836a86a47
commit d0296f78e3
10 changed files with 1214 additions and 1095 deletions

BIN
node/stmt/debug.test Executable file

Binary file not shown.

View File

@ -0,0 +1,63 @@
package stmt
import (
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/walker"
)
// AltForeach node
type AltForeach struct {
ByRef bool
Expr node.Node
Key node.Node
Variable node.Node
Stmt node.Node
}
// NewAltForeach node constuctor
func NewAltForeach(Expr node.Node, Key node.Node, Variable node.Node, Stmt node.Node, ByRef bool) *AltForeach {
return &AltForeach{
ByRef,
Expr,
Key,
Variable,
Stmt,
}
}
// Attributes returns node attributes as map
func (n *AltForeach) Attributes() map[string]interface{} {
return map[string]interface{}{
"ByRef": n.ByRef,
}
}
// Walk traverses nodes
// Walk is invoked recursively until v.EnterNode returns true
func (n *AltForeach) Walk(v walker.Visitor) {
if v.EnterNode(n) == false {
return
}
if n.Expr != nil {
vv := v.GetChildrenVisitor("Expr")
n.Expr.Walk(vv)
}
if n.Key != nil {
vv := v.GetChildrenVisitor("Key")
n.Key.Walk(vv)
}
if n.Variable != nil {
vv := v.GetChildrenVisitor("Variable")
n.Variable.Walk(vv)
}
if n.Stmt != nil {
vv := v.GetChildrenVisitor("Stmt")
n.Stmt.Walk(vv)
}
v.LeaveNode(n)
}

View File

@ -57,7 +57,7 @@ func TestAltForeach(t *testing.T) {
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Foreach{
&stmt.AltForeach{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$v"}},
Stmt: &stmt.StmtList{Stmts: []node.Node{}},

View File

@ -231,6 +231,17 @@ var nodesToTest = []struct {
[]string{"Expr", "Key", "Variable", "Stmt"},
map[string]interface{}{"ByRef": true},
},
{
&stmt.AltForeach{
ByRef: true,
Expr: &stmt.Expression{},
Key: &expr.Variable{},
Variable: &expr.Variable{},
Stmt: &stmt.StmtList{},
},
[]string{"Expr", "Key", "Variable", "Stmt"},
map[string]interface{}{"ByRef": true},
},
{
&stmt.Function{
ReturnsRef: true,

File diff suppressed because it is too large Load Diff

View File

@ -203,7 +203,7 @@ import (
%type <node> variable_name variable_without_objects dynamic_class_name_reference new_expr class_name_reference static_member
%type <node> function_call fully_qualified_class_name combined_scalar combined_scalar_offset general_constant parenthesis_expr
%type <node> exit_expr yield_expr function_declaration_statement class_declaration_statement constant_declaration
%type <node> else_single new_else_single unset_variable foreach_statement declare_statement
%type <node> else_single new_else_single unset_variable declare_statement
%type <node> finally_statement additional_catch unticked_function_declaration_statement unticked_class_declaration_statement
%type <node> optional_class_type parameter class_entry_type extends_from class_statement class_constant_declaration
%type <node> trait_use_statement function_call_parameter trait_adaptation_statement trait_precedence trait_alias
@ -227,7 +227,7 @@ import (
%type <foreachVariable> foreach_variable foreach_optional_arg
%type <nodesWithEndToken> ctor_arguments function_call_parameter_list switch_case_list method_body trait_adaptations
%type <boolWithToken> is_reference is_variadic
%type <altSyntaxNode> while_statement for_statement
%type <altSyntaxNode> while_statement for_statement foreach_statement
%%
@ -720,21 +720,37 @@ unticked_statement:
| T_FOREACH '(' variable T_AS foreach_variable foreach_optional_arg ')' foreach_statement
{
if $6.node == nil {
$$ = stmt.NewForeach($3, nil, $5.node, $8, $5.byRef)
if ($8.isAlt) {
$$ = stmt.NewAltForeach($3, nil, $5.node, $8.node, $5.byRef)
} else {
$$ = stmt.NewForeach($3, nil, $5.node, $8.node, $5.byRef)
}
} else {
$$ = stmt.NewForeach($3, $5.node, $6.node, $8, $6.byRef)
if ($8.isAlt) {
$$ = stmt.NewAltForeach($3, $5.node, $6.node, $8.node, $6.byRef)
} else {
$$ = stmt.NewForeach($3, $5.node, $6.node, $8.node, $6.byRef)
}
}
positions.AddPosition($$, positionBuilder.NewTokenNodePosition($1, $8))
positions.AddPosition($$, positionBuilder.NewTokenNodePosition($1, $8.node))
comments.AddComments($$, $1.Comments())
}
| T_FOREACH '(' expr_without_variable T_AS foreach_variable foreach_optional_arg ')' foreach_statement
{
if $6.node == nil {
$$ = stmt.NewForeach($3, nil, $5.node, $8, $5.byRef)
if ($8.isAlt) {
$$ = stmt.NewAltForeach($3, nil, $5.node, $8.node, $5.byRef)
} else {
$$ = stmt.NewForeach($3, nil, $5.node, $8.node, $5.byRef)
}
} else {
$$ = stmt.NewForeach($3, $5.node, $6.node, $8, $6.byRef)
if ($8.isAlt) {
$$ = stmt.NewAltForeach($3, $5.node, $6.node, $8.node, $6.byRef)
} else {
$$ = stmt.NewForeach($3, $5.node, $6.node, $8.node, $6.byRef)
}
}
positions.AddPosition($$, positionBuilder.NewTokenNodePosition($1, $8))
positions.AddPosition($$, positionBuilder.NewTokenNodePosition($1, $8.node))
comments.AddComments($$, $1.Comments())
}
| T_DECLARE '(' declare_list ')' declare_statement
@ -1027,12 +1043,11 @@ for_statement:
foreach_statement:
statement
{ $$ = $1; }
{ $$ = altSyntaxNode{$1, false} }
| ':' inner_statement_list T_ENDFOREACH ';'
{
$$ = stmt.NewStmtList($2)
positions.AddPosition($$, positionBuilder.NewTokensPosition($1, $4))
comments.AddComments($$, $1.Comments())
$$ = altSyntaxNode{stmt.NewStmtList($2), true}
positions.AddPosition($$.node, positionBuilder.NewTokensPosition($1, $4))
}
;

View File

@ -1078,7 +1078,7 @@ CAD;
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$v"}},
Stmt: &stmt.StmtList{Stmts: []node.Node{}},
},
&stmt.Foreach{
&stmt.AltForeach{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$v"}},
Stmt: &stmt.StmtList{Stmts: []node.Node{}},

File diff suppressed because it is too large Load Diff

View File

@ -209,7 +209,7 @@ import (
%type <node> mixed_group_use_declaration use_declaration unprefixed_use_declaration
%type <node> const_decl inner_statement
%type <node> expr optional_expr
%type <node> foreach_statement declare_statement finally_statement unset_variable variable
%type <node> declare_statement finally_statement unset_variable variable
%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> absolute_trait_method_reference trait_method_reference property echo_expr
@ -246,7 +246,7 @@ import (
%type <str> backup_doc_comment
%type <altSyntaxNode> while_statement for_statement
%type <altSyntaxNode> while_statement for_statement foreach_statement
%%
@ -612,14 +612,22 @@ statement:
}
| T_FOREACH '(' expr T_AS foreach_variable ')' foreach_statement
{
$$ = stmt.NewForeach($3, nil, $5.node, $7, $5.byRef)
positions.AddPosition($$, positionBuilder.NewTokenNodePosition($1, $7))
if ($7.isAlt) {
$$ = stmt.NewAltForeach($3, nil, $5.node, $7.node, $5.byRef)
} else {
$$ = stmt.NewForeach($3, nil, $5.node, $7.node, $5.byRef)
}
positions.AddPosition($$, positionBuilder.NewTokenNodePosition($1, $7.node))
comments.AddComments($$, $1.Comments())
}
| T_FOREACH '(' expr T_AS variable T_DOUBLE_ARROW foreach_variable ')' foreach_statement
{
$$ = stmt.NewForeach($3, $5, $7.node, $9, $7.byRef)
positions.AddPosition($$, positionBuilder.NewTokenNodePosition($1, $9))
if ($9.isAlt) {
$$ = stmt.NewAltForeach($3, $5, $7.node, $9.node, $7.byRef)
} else {
$$ = stmt.NewForeach($3, $5, $7.node, $9.node, $7.byRef)
}
positions.AddPosition($$, positionBuilder.NewTokenNodePosition($1, $9.node))
comments.AddComments($$, $1.Comments())
}
| T_DECLARE '(' const_list ')' declare_statement
@ -851,13 +859,13 @@ for_statement:
;
foreach_statement:
statement { $$ = $1; }
statement
{ $$ = altSyntaxNode{$1, false} }
| ':' inner_statement_list T_ENDFOREACH ';'
{
$$ = stmt.NewStmtList($2)
positions.AddPosition($$, positionBuilder.NewTokensPosition($1, $4))
comments.AddComments($$, $1.Comments())
}
{
$$ = altSyntaxNode{stmt.NewStmtList($2), true}
positions.AddPosition($$.node, positionBuilder.NewTokensPosition($1, $4))
}
;
declare_statement:

View File

@ -1169,7 +1169,7 @@ CAD;
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$v"}},
Stmt: &stmt.StmtList{Stmts: []node.Node{}},
},
&stmt.Foreach{
&stmt.AltForeach{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$v"}},
Stmt: &stmt.StmtList{Stmts: []node.Node{}},