split For and AltFor nodes

This commit is contained in:
z7zmey 2018-02-18 19:57:54 +02:00
parent d498ea9863
commit 3836a86a47
9 changed files with 1217 additions and 1123 deletions

71
node/stmt/n_alt_for.go Normal file
View File

@ -0,0 +1,71 @@
package stmt
import (
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/walker"
)
// AltFor node
type AltFor struct {
Init []node.Node
Cond []node.Node
Loop []node.Node
Stmt node.Node
}
// NewAltFor node constuctor
func NewAltFor(Init []node.Node, Cond []node.Node, Loop []node.Node, Stmt node.Node) *AltFor {
return &AltFor{
Init,
Cond,
Loop,
Stmt,
}
}
// Attributes returns node attributes as map
func (n *AltFor) Attributes() map[string]interface{} {
return nil
}
// Walk traverses nodes
// Walk is invoked recursively until v.EnterNode returns true
func (n *AltFor) Walk(v walker.Visitor) {
if v.EnterNode(n) == false {
return
}
if n.Init != nil {
vv := v.GetChildrenVisitor("Init")
for _, nn := range n.Init {
if nn != nil {
nn.Walk(vv)
}
}
}
if n.Cond != nil {
vv := v.GetChildrenVisitor("Cond")
for _, nn := range n.Cond {
if nn != nil {
nn.Walk(vv)
}
}
}
if n.Loop != nil {
vv := v.GetChildrenVisitor("Loop")
for _, nn := range n.Loop {
if nn != nil {
nn.Walk(vv)
}
}
}
if n.Stmt != nil {
vv := v.GetChildrenVisitor("Stmt")
n.Stmt.Walk(vv)
}
v.LeaveNode(n)
}

View File

@ -60,7 +60,7 @@ func TestAltFor(t *testing.T) {
expected := &stmt.StmtList{ expected := &stmt.StmtList{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.For{ &stmt.AltFor{
Cond: []node.Node{ Cond: []node.Node{
&binary_op.Smaller{ &binary_op.Smaller{
Left: &expr.Variable{VarName: &node.Identifier{Value: "$i"}}, Left: &expr.Variable{VarName: &node.Identifier{Value: "$i"}},

View File

@ -210,6 +210,16 @@ var nodesToTest = []struct {
[]string{"Init", "Cond", "Loop", "Stmt"}, []string{"Init", "Cond", "Loop", "Stmt"},
map[string]interface{}{}, map[string]interface{}{},
}, },
{
&stmt.AltFor{
Init: []node.Node{&stmt.Expression{}},
Cond: []node.Node{&stmt.Expression{}},
Loop: []node.Node{&stmt.Expression{}},
Stmt: &stmt.StmtList{},
},
[]string{"Init", "Cond", "Loop", "Stmt"},
map[string]interface{}{},
},
{ {
&stmt.Foreach{ &stmt.Foreach{
ByRef: true, ByRef: 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> 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> 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> exit_expr yield_expr function_declaration_statement class_declaration_statement constant_declaration
%type <node> else_single new_else_single for_statement unset_variable foreach_statement declare_statement %type <node> else_single new_else_single unset_variable foreach_statement declare_statement
%type <node> finally_statement additional_catch unticked_function_declaration_statement unticked_class_declaration_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> 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 %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 <foreachVariable> foreach_variable foreach_optional_arg
%type <nodesWithEndToken> ctor_arguments function_call_parameter_list switch_case_list method_body trait_adaptations %type <nodesWithEndToken> ctor_arguments function_call_parameter_list switch_case_list method_body trait_adaptations
%type <boolWithToken> is_reference is_variadic %type <boolWithToken> is_reference is_variadic
%type <altSyntaxNode> while_statement %type <altSyntaxNode> while_statement for_statement
%% %%
@ -619,8 +619,12 @@ unticked_statement:
} }
| T_FOR '(' for_expr ';' for_expr ';' for_expr ')' for_statement | T_FOR '(' for_expr ';' for_expr ';' for_expr ')' for_statement
{ {
$$ = stmt.NewFor($3, $5, $7, $9) if ($9.isAlt) {
positions.AddPosition($$, positionBuilder.NewTokenNodePosition($1, $9)) $$ = stmt.NewAltFor($3, $5, $7, $9.node)
} else {
$$ = stmt.NewFor($3, $5, $7, $9.node)
}
positions.AddPosition($$, positionBuilder.NewTokenNodePosition($1, $9.node))
comments.AddComments($$, $1.Comments()) comments.AddComments($$, $1.Comments())
} }
| T_SWITCH parenthesis_expr switch_case_list | T_SWITCH parenthesis_expr switch_case_list
@ -1012,12 +1016,11 @@ foreach_variable:
for_statement: for_statement:
statement statement
{ $$ = $1; } { $$ = altSyntaxNode{$1, false} }
| ':' inner_statement_list T_ENDFOR ';' | ':' inner_statement_list T_ENDFOR ';'
{ {
$$ = stmt.NewStmtList($2) $$ = altSyntaxNode{stmt.NewStmtList($2), true}
positions.AddPosition($$, positionBuilder.NewTokensPosition($1, $4)) positions.AddPosition($$.node, positionBuilder.NewTokensPosition($1, $4))
comments.AddComments($$, $1.Comments())
} }
; ;

View File

@ -1054,7 +1054,7 @@ CAD;
}, },
Stmt: &stmt.StmtList{Stmts: []node.Node{}}, Stmt: &stmt.StmtList{Stmts: []node.Node{}},
}, },
&stmt.For{ &stmt.AltFor{
Cond: []node.Node{ Cond: []node.Node{
&binary_op.Smaller{ &binary_op.Smaller{
Left: &expr.Variable{VarName: &node.Identifier{Value: "$i"}}, Left: &expr.Variable{VarName: &node.Identifier{Value: "$i"}},

File diff suppressed because it is too large Load Diff

View File

@ -208,7 +208,7 @@ import (
%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 for_statement %type <node> expr optional_expr
%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
@ -246,7 +246,7 @@ import (
%type <str> backup_doc_comment %type <str> backup_doc_comment
%type <altSyntaxNode> while_statement %type <altSyntaxNode> while_statement for_statement
%% %%
@ -542,8 +542,12 @@ statement:
} }
| T_FOR '(' for_exprs ';' for_exprs ';' for_exprs ')' for_statement | T_FOR '(' for_exprs ';' for_exprs ';' for_exprs ')' for_statement
{ {
$$ = stmt.NewFor($3, $5, $7, $9) if ($9.isAlt) {
positions.AddPosition($$, positionBuilder.NewTokenNodePosition($1, $9)) $$ = stmt.NewAltFor($3, $5, $7, $9.node)
} else {
$$ = stmt.NewFor($3, $5, $7, $9.node)
}
positions.AddPosition($$, positionBuilder.NewTokenNodePosition($1, $9.node))
comments.AddComments($$, $1.Comments()) comments.AddComments($$, $1.Comments())
} }
| T_SWITCH '(' expr ')' switch_case_list | T_SWITCH '(' expr ')' switch_case_list
@ -837,13 +841,13 @@ foreach_variable:
; ;
for_statement: for_statement:
statement { $$ = $1; } statement
| ':' inner_statement_list T_ENDFOR ';' { $$ = altSyntaxNode{$1, false} }
{ | ':' inner_statement_list T_ENDFOR ';'
$$ = stmt.NewStmtList($2) {
positions.AddPosition($$, positionBuilder.NewTokensPosition($1, $4)) $$ = altSyntaxNode{stmt.NewStmtList($2), true}
comments.AddComments($$, $1.Comments()) positions.AddPosition($$.node, positionBuilder.NewTokensPosition($1, $4))
} }
; ;
foreach_statement: foreach_statement:

View File

@ -1147,7 +1147,7 @@ CAD;
}, },
Stmt: &stmt.StmtList{Stmts: []node.Node{}}, Stmt: &stmt.StmtList{Stmts: []node.Node{}},
}, },
&stmt.For{ &stmt.AltFor{
Cond: []node.Node{ Cond: []node.Node{
&binary_op.Smaller{ &binary_op.Smaller{
Left: &expr.Variable{VarName: &node.Identifier{Value: "$i"}}, Left: &expr.Variable{VarName: &node.Identifier{Value: "$i"}},