split Foreach and AltForeach nodes
This commit is contained in:
parent
3836a86a47
commit
d0296f78e3
BIN
node/stmt/debug.test
Executable file
BIN
node/stmt/debug.test
Executable file
Binary file not shown.
63
node/stmt/n_alt_foreach.go
Normal file
63
node/stmt/n_alt_foreach.go
Normal 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)
|
||||||
|
}
|
@ -57,7 +57,7 @@ func TestAltForeach(t *testing.T) {
|
|||||||
|
|
||||||
expected := &stmt.StmtList{
|
expected := &stmt.StmtList{
|
||||||
Stmts: []node.Node{
|
Stmts: []node.Node{
|
||||||
&stmt.Foreach{
|
&stmt.AltForeach{
|
||||||
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
|
||||||
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$v"}},
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$v"}},
|
||||||
Stmt: &stmt.StmtList{Stmts: []node.Node{}},
|
Stmt: &stmt.StmtList{Stmts: []node.Node{}},
|
||||||
|
@ -231,6 +231,17 @@ var nodesToTest = []struct {
|
|||||||
[]string{"Expr", "Key", "Variable", "Stmt"},
|
[]string{"Expr", "Key", "Variable", "Stmt"},
|
||||||
map[string]interface{}{"ByRef": true},
|
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{
|
&stmt.Function{
|
||||||
ReturnsRef: true,
|
ReturnsRef: true,
|
||||||
|
1213
php5/php5.go
1213
php5/php5.go
File diff suppressed because it is too large
Load Diff
39
php5/php5.y
39
php5/php5.y
@ -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 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> 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 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
|
| T_FOREACH '(' variable T_AS foreach_variable foreach_optional_arg ')' foreach_statement
|
||||||
{
|
{
|
||||||
if $6.node == nil {
|
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 {
|
} else {
|
||||||
$$ = stmt.NewForeach($3, $5.node, $6.node, $8, $6.byRef)
|
$$ = stmt.NewForeach($3, nil, $5.node, $8.node, $5.byRef)
|
||||||
}
|
}
|
||||||
positions.AddPosition($$, positionBuilder.NewTokenNodePosition($1, $8))
|
} else {
|
||||||
|
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.node))
|
||||||
comments.AddComments($$, $1.Comments())
|
comments.AddComments($$, $1.Comments())
|
||||||
}
|
}
|
||||||
| T_FOREACH '(' expr_without_variable T_AS foreach_variable foreach_optional_arg ')' foreach_statement
|
| T_FOREACH '(' expr_without_variable T_AS foreach_variable foreach_optional_arg ')' foreach_statement
|
||||||
{
|
{
|
||||||
if $6.node == nil {
|
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 {
|
} else {
|
||||||
$$ = stmt.NewForeach($3, $5.node, $6.node, $8, $6.byRef)
|
$$ = stmt.NewForeach($3, nil, $5.node, $8.node, $5.byRef)
|
||||||
}
|
}
|
||||||
positions.AddPosition($$, positionBuilder.NewTokenNodePosition($1, $8))
|
} else {
|
||||||
|
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.node))
|
||||||
comments.AddComments($$, $1.Comments())
|
comments.AddComments($$, $1.Comments())
|
||||||
}
|
}
|
||||||
| T_DECLARE '(' declare_list ')' declare_statement
|
| T_DECLARE '(' declare_list ')' declare_statement
|
||||||
@ -1027,12 +1043,11 @@ for_statement:
|
|||||||
|
|
||||||
foreach_statement:
|
foreach_statement:
|
||||||
statement
|
statement
|
||||||
{ $$ = $1; }
|
{ $$ = altSyntaxNode{$1, false} }
|
||||||
| ':' inner_statement_list T_ENDFOREACH ';'
|
| ':' inner_statement_list T_ENDFOREACH ';'
|
||||||
{
|
{
|
||||||
$$ = 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())
|
|
||||||
}
|
}
|
||||||
;
|
;
|
||||||
|
|
||||||
|
@ -1078,7 +1078,7 @@ CAD;
|
|||||||
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$v"}},
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$v"}},
|
||||||
Stmt: &stmt.StmtList{Stmts: []node.Node{}},
|
Stmt: &stmt.StmtList{Stmts: []node.Node{}},
|
||||||
},
|
},
|
||||||
&stmt.Foreach{
|
&stmt.AltForeach{
|
||||||
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
|
||||||
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$v"}},
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$v"}},
|
||||||
Stmt: &stmt.StmtList{Stmts: []node.Node{}},
|
Stmt: &stmt.StmtList{Stmts: []node.Node{}},
|
||||||
|
945
php7/php7.go
945
php7/php7.go
File diff suppressed because it is too large
Load Diff
28
php7/php7.y
28
php7/php7.y
@ -209,7 +209,7 @@ import (
|
|||||||
%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
|
%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> 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
|
||||||
%type <node> absolute_trait_method_reference trait_method_reference property echo_expr
|
%type <node> absolute_trait_method_reference trait_method_reference property echo_expr
|
||||||
@ -246,7 +246,7 @@ import (
|
|||||||
|
|
||||||
%type <str> backup_doc_comment
|
%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
|
| T_FOREACH '(' expr T_AS foreach_variable ')' foreach_statement
|
||||||
{
|
{
|
||||||
$$ = stmt.NewForeach($3, nil, $5.node, $7, $5.byRef)
|
if ($7.isAlt) {
|
||||||
positions.AddPosition($$, positionBuilder.NewTokenNodePosition($1, $7))
|
$$ = 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())
|
comments.AddComments($$, $1.Comments())
|
||||||
}
|
}
|
||||||
| T_FOREACH '(' expr T_AS variable T_DOUBLE_ARROW foreach_variable ')' foreach_statement
|
| T_FOREACH '(' expr T_AS variable T_DOUBLE_ARROW foreach_variable ')' foreach_statement
|
||||||
{
|
{
|
||||||
$$ = stmt.NewForeach($3, $5, $7.node, $9, $7.byRef)
|
if ($9.isAlt) {
|
||||||
positions.AddPosition($$, positionBuilder.NewTokenNodePosition($1, $9))
|
$$ = 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())
|
comments.AddComments($$, $1.Comments())
|
||||||
}
|
}
|
||||||
| T_DECLARE '(' const_list ')' declare_statement
|
| T_DECLARE '(' const_list ')' declare_statement
|
||||||
@ -851,12 +859,12 @@ for_statement:
|
|||||||
;
|
;
|
||||||
|
|
||||||
foreach_statement:
|
foreach_statement:
|
||||||
statement { $$ = $1; }
|
statement
|
||||||
|
{ $$ = altSyntaxNode{$1, false} }
|
||||||
| ':' inner_statement_list T_ENDFOREACH ';'
|
| ':' inner_statement_list T_ENDFOREACH ';'
|
||||||
{
|
{
|
||||||
$$ = 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())
|
|
||||||
}
|
}
|
||||||
;
|
;
|
||||||
|
|
||||||
|
@ -1169,7 +1169,7 @@ CAD;
|
|||||||
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$v"}},
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$v"}},
|
||||||
Stmt: &stmt.StmtList{Stmts: []node.Node{}},
|
Stmt: &stmt.StmtList{Stmts: []node.Node{}},
|
||||||
},
|
},
|
||||||
&stmt.Foreach{
|
&stmt.AltForeach{
|
||||||
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
|
||||||
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$v"}},
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$v"}},
|
||||||
Stmt: &stmt.StmtList{Stmts: []node.Node{}},
|
Stmt: &stmt.StmtList{Stmts: []node.Node{}},
|
||||||
|
Loading…
Reference in New Issue
Block a user