#50: nodes stmt.Exit and stmt.Die was merged

This commit is contained in:
z7zmey
2018-07-10 00:51:02 +03:00
parent a69e899f04
commit 18d6d59292
17 changed files with 605 additions and 572 deletions

View File

@@ -1,61 +0,0 @@
package expr
import (
"github.com/z7zmey/php-parser/meta"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/position"
"github.com/z7zmey/php-parser/walker"
)
// Die node
type Die struct {
Meta []meta.Meta
Position *position.Position
Expr node.Node
}
// NewDie node constructor
func NewDie(Expr node.Node) *Die {
return &Die{
Expr: Expr,
}
}
// SetPosition sets node position
func (n *Die) SetPosition(p *position.Position) {
n.Position = p
}
// GetPosition returns node positions
func (n *Die) GetPosition() *position.Position {
return n.Position
}
func (n *Die) AddMeta(m []meta.Meta) {
n.Meta = append(n.Meta, m...)
}
func (n *Die) GetMeta() []meta.Meta {
return n.Meta
}
// 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 {
v.EnterChildNode("Expr", n)
n.Expr.Walk(v)
v.LeaveChildNode("Expr", n)
}
v.LeaveNode(n)
}

View File

@@ -10,6 +10,7 @@ import (
// Exit node
type Exit struct {
Meta []meta.Meta
Die bool
Position *position.Position
Expr node.Node
}
@@ -41,7 +42,9 @@ func (n *Exit) GetMeta() []meta.Meta {
// Attributes returns node attributes as map
func (n *Exit) Attributes() map[string]interface{} {
return nil
return map[string]interface{}{
"Die": n.Die,
}
}
// Walk traverses nodes

View File

@@ -32,6 +32,7 @@ func TestExit(t *testing.T) {
EndPos: 8,
},
Expr: &expr.Exit{
Die: false,
Position: &position.Position{
StartLine: 1,
EndLine: 1,
@@ -54,6 +55,48 @@ func TestExit(t *testing.T) {
assertEqual(t, expected, actual)
}
func TestExitEmpty(t *testing.T) {
src := `<? exit();`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 10,
},
Stmts: []node.Node{
&stmt.Expression{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 10,
},
Expr: &expr.Exit{
Die: false,
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 9,
},
},
},
},
}
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.Parse()
actual := php7parser.GetRootNode()
assertEqual(t, expected, actual)
// php5parser := php5.NewParser(bytes.NewBufferString(src), "test.php")
// php5parser.Parse()
// actual = php5parser.GetRootNode()
// assertEqual(t, expected, actual)
}
func TestExitExpr(t *testing.T) {
src := `<? exit($a);`
@@ -73,6 +116,7 @@ func TestExitExpr(t *testing.T) {
EndPos: 12,
},
Expr: &expr.Exit{
Die: false,
Position: &position.Position{
StartLine: 1,
EndLine: 1,
@@ -130,7 +174,8 @@ func TestDie(t *testing.T) {
StartPos: 4,
EndPos: 7,
},
Expr: &expr.Die{
Expr: &expr.Exit{
Die: true,
Position: &position.Position{
StartLine: 1,
EndLine: 1,
@@ -153,6 +198,48 @@ func TestDie(t *testing.T) {
assertEqual(t, expected, actual)
}
func TestDieEmpty(t *testing.T) {
src := `<? die();`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 9,
},
Stmts: []node.Node{
&stmt.Expression{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 9,
},
Expr: &expr.Exit{
Die: true,
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 8,
},
},
},
},
}
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.Parse()
actual := php7parser.GetRootNode()
assertEqual(t, expected, actual)
php5parser := php5.NewParser(bytes.NewBufferString(src), "test.php")
php5parser.Parse()
actual = php5parser.GetRootNode()
assertEqual(t, expected, actual)
}
func TestDieExpr(t *testing.T) {
src := `<? die($a);`
@@ -171,7 +258,8 @@ func TestDieExpr(t *testing.T) {
StartPos: 4,
EndPos: 11,
},
Expr: &expr.Die{
Expr: &expr.Exit{
Die: true,
Position: &position.Position{
StartLine: 1,
EndLine: 1,

View File

@@ -21,7 +21,6 @@ var nodes = []node.Node{
&expr.ClosureUse{},
&expr.Closure{},
&expr.ConstFetch{},
&expr.Die{},
&expr.Empty{},
&expr.ErrorSuppress{},
&expr.Eval{},

View File

@@ -128,17 +128,11 @@ var nodesToTest = []struct {
},
{
&expr.Exit{
Die: true,
Expr: &expr.Variable{},
},
[]string{"Expr"},
map[string]interface{}{},
},
{
&expr.Die{
Expr: &expr.Variable{},
},
[]string{"Expr"},
map[string]interface{}{},
map[string]interface{}{"Die": true},
},
{
&expr.FunctionCall{