split Exit and Die nodes

This commit is contained in:
z7zmey 2018-02-18 19:09:18 +02:00
parent f107e8bb57
commit 1c6633e47d
10 changed files with 437 additions and 399 deletions

38
node/expr/n_die.go Normal file
View File

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

View File

@ -8,22 +8,18 @@ import (
// Exit node // Exit node
type Exit struct { type Exit struct {
Expr node.Node Expr node.Node
IsDie bool
} }
// NewExit node constuctor // NewExit node constuctor
func NewExit(Expr node.Node, IsDie bool) *Exit { func NewExit(Expr node.Node) *Exit {
return &Exit{ return &Exit{
Expr, Expr,
IsDie,
} }
} }
// Attributes returns node attributes as map // Attributes returns node attributes as map
func (n *Exit) Attributes() map[string]interface{} { func (n *Exit) Attributes() map[string]interface{} {
return map[string]interface{}{ return nil
"IsDie": n.IsDie,
}
} }
// Walk traverses nodes // Walk traverses nodes

View File

@ -18,9 +18,7 @@ func TestExit(t *testing.T) {
expected := &stmt.StmtList{ expected := &stmt.StmtList{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.Exit{ Expr: &expr.Exit{},
IsDie: false,
},
}, },
}, },
} }
@ -39,7 +37,6 @@ func TestExitExpr(t *testing.T) {
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.Exit{ Expr: &expr.Exit{
IsDie: false,
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}}, Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
}, },
}, },
@ -59,9 +56,7 @@ func TestDie(t *testing.T) {
expected := &stmt.StmtList{ expected := &stmt.StmtList{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.Exit{ Expr: &expr.Die{},
IsDie: true,
},
}, },
}, },
} }
@ -79,8 +74,7 @@ func TestDieExpr(t *testing.T) {
expected := &stmt.StmtList{ expected := &stmt.StmtList{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.Exit{ Expr: &expr.Die{
IsDie: true,
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}}, Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
}, },
}, },

View File

@ -133,10 +133,16 @@ var nodesToTest = []struct {
{ {
&expr.Exit{ &expr.Exit{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}}, Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
IsDie: false,
}, },
[]string{"Expr"}, []string{"Expr"},
map[string]interface{}{"IsDie": false}, map[string]interface{}{},
},
{
&expr.Die{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
},
[]string{"Expr"},
map[string]interface{}{},
}, },
{ {
&expr.FunctionCall{ &expr.FunctionCall{

File diff suppressed because it is too large Load Diff

View File

@ -2226,7 +2226,11 @@ expr_without_variable:
} }
| T_EXIT exit_expr | T_EXIT exit_expr
{ {
$$ = expr.NewExit($2, strings.EqualFold($1.Value, "die")) if (strings.EqualFold($1.Value, "die")) {
$$ = expr.NewDie($2)
} else {
$$ = expr.NewExit($2)
}
positions.AddPosition($$, positionBuilder.NewTokenNodePosition($1, $2)) positions.AddPosition($$, positionBuilder.NewTokenNodePosition($1, $2))
comments.AddComments($$, $1.Comments()) comments.AddComments($$, $1.Comments())
} }

View File

@ -2119,24 +2119,18 @@ CAD;
}, },
}, },
&stmt.Expression{ &stmt.Expression{
Expr: &expr.Exit{ Expr: &expr.Exit{},
IsDie: false,
},
}, },
&stmt.Expression{ &stmt.Expression{
Expr: &expr.Exit{ Expr: &expr.Exit{
IsDie: false,
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}}, Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
}, },
}, },
&stmt.Expression{ &stmt.Expression{
Expr: &expr.Exit{ Expr: &expr.Die{},
IsDie: true,
},
}, },
&stmt.Expression{ &stmt.Expression{
Expr: &expr.Exit{ Expr: &expr.Die{
IsDie: true,
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}}, Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
}, },
}, },

File diff suppressed because it is too large Load Diff

View File

@ -1844,7 +1844,11 @@ expr_without_variable:
} }
| T_EXIT exit_expr | T_EXIT exit_expr
{ {
$$ = expr.NewExit($2, strings.EqualFold($1.Value, "die")) if (strings.EqualFold($1.Value, "die")) {
$$ = expr.NewDie($2)
} else {
$$ = expr.NewExit($2)
}
positions.AddPosition($$, positionBuilder.NewTokenNodePosition($1, $2)) positions.AddPosition($$, positionBuilder.NewTokenNodePosition($1, $2))
comments.AddComments($$, $1.Comments()) comments.AddComments($$, $1.Comments())
} }

View File

@ -2206,24 +2206,18 @@ CAD;
}, },
}, },
&stmt.Expression{ &stmt.Expression{
Expr: &expr.Exit{ Expr: &expr.Exit{},
IsDie: false,
},
}, },
&stmt.Expression{ &stmt.Expression{
Expr: &expr.Exit{ Expr: &expr.Exit{
IsDie: false,
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}}, Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
}, },
}, },
&stmt.Expression{ &stmt.Expression{
Expr: &expr.Exit{ Expr: &expr.Die{},
IsDie: true,
},
}, },
&stmt.Expression{ &stmt.Expression{
Expr: &expr.Exit{ Expr: &expr.Die{
IsDie: true,
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}}, Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
}, },
}, },