create expr.Reference node

[wip] create expr.Reference node

[wip] create expr.Reference node

[wip] create expr.Reference node

fix
This commit is contained in:
z7zmey
2018-05-14 18:09:11 +03:00
parent ac74ae3225
commit ec0ef26bd6
22 changed files with 1408 additions and 1445 deletions

View File

@@ -7,15 +7,13 @@ import (
// ArrayItem node
type ArrayItem struct {
ByRef bool
Key node.Node
Val node.Node
Key node.Node
Val node.Node
}
// NewArrayItem node constructor
func NewArrayItem(Key node.Node, Val node.Node, ByRef bool) *ArrayItem {
func NewArrayItem(Key node.Node, Val node.Node) *ArrayItem {
return &ArrayItem{
ByRef,
Key,
Val,
}
@@ -23,9 +21,7 @@ func NewArrayItem(Key node.Node, Val node.Node, ByRef bool) *ArrayItem {
// Attributes returns node attributes as map
func (n *ArrayItem) Attributes() map[string]interface{} {
return map[string]interface{}{
"ByRef": n.ByRef,
}
return nil
}
// Walk traverses nodes

View File

@@ -7,23 +7,19 @@ import (
// ClosureUse node
type ClosureUse struct {
ByRef bool
Variable node.Node
}
// NewClosureUse node constructor
func NewClosureUse(Variable node.Node, ByRef bool) *ClosureUse {
func NewClosureUse(Variable node.Node) *ClosureUse {
return &ClosureUse{
ByRef,
Variable,
}
}
// Attributes returns node attributes as map
func (n *ClosureUse) Attributes() map[string]interface{} {
return map[string]interface{}{
"ByRef": n.ByRef,
}
return nil
}
// Walk traverses nodes

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

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

View File

@@ -47,8 +47,7 @@ func TestArrayItem(t *testing.T) {
Expr: &expr.Array{
Items: []node.Node{
&expr.ArrayItem{
ByRef: false,
Val: &scalar.Lnumber{Value: "1"},
Val: &scalar.Lnumber{Value: "1"},
},
},
},
@@ -76,13 +75,11 @@ func TestArrayItems(t *testing.T) {
Expr: &expr.Array{
Items: []node.Node{
&expr.ArrayItem{
ByRef: false,
Key: &scalar.Lnumber{Value: "1"},
Val: &scalar.Lnumber{Value: "1"},
Key: &scalar.Lnumber{Value: "1"},
Val: &scalar.Lnumber{Value: "1"},
},
&expr.ArrayItem{
ByRef: true,
Val: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
Val: &expr.Reference{Variable: &expr.Variable{VarName: &node.Identifier{Value: "b"}}},
},
},
},

View File

@@ -66,12 +66,10 @@ func TestClosureUse(t *testing.T) {
},
Uses: []node.Node{
&expr.ClosureUse{
ByRef: false,
Variable: &expr.Variable{VarName: &node.Identifier{Value: "c"}},
},
&expr.ClosureUse{
ByRef: true,
Variable: &expr.Variable{VarName: &node.Identifier{Value: "d"}},
Variable: &expr.Reference{Variable: &expr.Variable{VarName: &node.Identifier{Value: "d"}}},
},
},
Stmts: []node.Node{},
@@ -115,11 +113,9 @@ func TestClosureUse2(t *testing.T) {
},
Uses: []node.Node{
&expr.ClosureUse{
ByRef: true,
Variable: &expr.Variable{VarName: &node.Identifier{Value: "c"}},
Variable: &expr.Reference{Variable: &expr.Variable{VarName: &node.Identifier{Value: "c"}}},
},
&expr.ClosureUse{
ByRef: false,
Variable: &expr.Variable{VarName: &node.Identifier{Value: "d"}},
},
},

View File

@@ -51,8 +51,7 @@ func TestList(t *testing.T) {
Variable: &expr.List{
Items: []node.Node{
&expr.ArrayItem{
ByRef: false,
Val: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
Val: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
},
},
},
@@ -83,7 +82,6 @@ func TestListArrayIndex(t *testing.T) {
Variable: &expr.List{
Items: []node.Node{
&expr.ArrayItem{
ByRef: false,
Val: &expr.ArrayDimFetch{
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
},
@@ -117,12 +115,10 @@ func TestListList(t *testing.T) {
Variable: &expr.List{
Items: []node.Node{
&expr.ArrayItem{
ByRef: false,
Val: &expr.List{
Items: []node.Node{
&expr.ArrayItem{
ByRef: false,
Val: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
Val: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
},
},
},

View File

@@ -0,0 +1,41 @@
package expr_test
import (
"bytes"
"testing"
"github.com/z7zmey/php-parser/node/expr"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/node/stmt"
"github.com/z7zmey/php-parser/php5"
"github.com/z7zmey/php-parser/php7"
)
func TestForeachWithRef(t *testing.T) {
t.Helper()
src := `<? foreach ($a as $k => &$v) {}`
expected := &node.Root{
Stmts: []node.Node{
&stmt.Foreach{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
Key: &expr.Variable{VarName: &node.Identifier{Value: "k"}},
Variable: &expr.Reference{Variable: &expr.Variable{VarName: &node.Identifier{Value: "v"}}},
Stmt: &stmt.StmtList{
Stmts: []node.Node{},
},
},
},
}
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)
}

View File

@@ -47,8 +47,7 @@ func TestShortArrayItem(t *testing.T) {
Expr: &expr.ShortArray{
Items: []node.Node{
&expr.ArrayItem{
ByRef: false,
Val: &scalar.Lnumber{Value: "1"},
Val: &scalar.Lnumber{Value: "1"},
},
},
},
@@ -76,13 +75,11 @@ func TestShortArrayItems(t *testing.T) {
Expr: &expr.ShortArray{
Items: []node.Node{
&expr.ArrayItem{
ByRef: false,
Key: &scalar.Lnumber{Value: "1"},
Val: &scalar.Lnumber{Value: "1"},
Key: &scalar.Lnumber{Value: "1"},
Val: &scalar.Lnumber{Value: "1"},
},
&expr.ArrayItem{
ByRef: true,
Val: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
Val: &expr.Reference{Variable: &expr.Variable{VarName: &node.Identifier{Value: "b"}}},
},
},
},

View File

@@ -23,8 +23,7 @@ func TestShortList(t *testing.T) {
Variable: &expr.ShortList{
Items: []node.Node{
&expr.ArrayItem{
ByRef: false,
Val: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
Val: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
},
},
},
@@ -50,7 +49,6 @@ func TestShortListArrayIndex(t *testing.T) {
Variable: &expr.ShortList{
Items: []node.Node{
&expr.ArrayItem{
ByRef: false,
Val: &expr.ArrayDimFetch{
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
},
@@ -79,12 +77,10 @@ func TestShortListList(t *testing.T) {
Variable: &expr.ShortList{
Items: []node.Node{
&expr.ArrayItem{
ByRef: false,
Val: &expr.List{
Items: []node.Node{
&expr.ArrayItem{
ByRef: false,
Val: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
Val: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
},
},
},

View File

@@ -32,20 +32,18 @@ var nodesToTest = []struct {
},
{
&expr.ArrayItem{
ByRef: false,
Key: &scalar.String{Value: "key"},
Val: &scalar.Lnumber{Value: "1"},
Key: &scalar.String{Value: "key"},
Val: &scalar.Lnumber{Value: "1"},
},
[]string{"Key", "Val"},
map[string]interface{}{"ByRef": false},
map[string]interface{}{},
},
{
&expr.Array{
Items: []node.Node{
&expr.ArrayItem{
ByRef: false,
Key: &scalar.String{Value: "key"},
Val: &scalar.Lnumber{Value: "1"},
Key: &scalar.String{Value: "key"},
Val: &scalar.Lnumber{Value: "1"},
},
},
},
@@ -83,11 +81,10 @@ var nodesToTest = []struct {
},
{
&expr.ClosureUse{
ByRef: false,
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
},
[]string{"Variable"},
map[string]interface{}{"ByRef": false},
map[string]interface{}{},
},
{
&expr.Closure{
@@ -252,6 +249,13 @@ var nodesToTest = []struct {
[]string{"Variable", "Property"},
map[string]interface{}{},
},
{
&expr.Reference{
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
},
[]string{"Variable"},
map[string]interface{}{},
},
{
&expr.RequireOnce{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},

View File

@@ -7,7 +7,6 @@ import (
// AltForeach node
type AltForeach struct {
ByRef bool
Expr node.Node
Key node.Node
Variable node.Node
@@ -15,9 +14,8 @@ type AltForeach struct {
}
// NewAltForeach node constructor
func NewAltForeach(Expr node.Node, Key node.Node, Variable node.Node, Stmt node.Node, ByRef bool) *AltForeach {
func NewAltForeach(Expr node.Node, Key node.Node, Variable node.Node, Stmt node.Node) *AltForeach {
return &AltForeach{
ByRef,
Expr,
Key,
Variable,
@@ -27,9 +25,7 @@ func NewAltForeach(Expr node.Node, Key node.Node, Variable node.Node, Stmt node.
// Attributes returns node attributes as map
func (n *AltForeach) Attributes() map[string]interface{} {
return map[string]interface{}{
"ByRef": n.ByRef,
}
return nil
}
// Walk traverses nodes

View File

@@ -7,7 +7,6 @@ import (
// Foreach node
type Foreach struct {
ByRef bool
Expr node.Node
Key node.Node
Variable node.Node
@@ -15,9 +14,8 @@ type Foreach struct {
}
// NewForeach node constructor
func NewForeach(Expr node.Node, Key node.Node, Variable node.Node, Stmt node.Node, ByRef bool) *Foreach {
func NewForeach(Expr node.Node, Key node.Node, Variable node.Node, Stmt node.Node) *Foreach {
return &Foreach{
ByRef,
Expr,
Key,
Variable,
@@ -27,9 +25,7 @@ func NewForeach(Expr node.Node, Key node.Node, Variable node.Node, Stmt node.Nod
// Attributes returns node attributes as map
func (n *Foreach) Attributes() map[string]interface{} {
return map[string]interface{}{
"ByRef": n.ByRef,
}
return nil
}
// Walk traverses nodes

View File

@@ -140,10 +140,9 @@ func TestForeachWithRef(t *testing.T) {
expected := &node.Root{
Stmts: []node.Node{
&stmt.Foreach{
ByRef: true,
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
Key: &expr.Variable{VarName: &node.Identifier{Value: "k"}},
Variable: &expr.Variable{VarName: &node.Identifier{Value: "v"}},
Variable: &expr.Reference{Variable: &expr.Variable{VarName: &node.Identifier{Value: "v"}}},
Stmt: &stmt.StmtList{Stmts: []node.Node{}},
},
},
@@ -166,14 +165,12 @@ func TestForeachWithList(t *testing.T) {
expected := &node.Root{
Stmts: []node.Node{
&stmt.Foreach{
ByRef: false,
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
Key: &expr.Variable{VarName: &node.Identifier{Value: "k"}},
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
Key: &expr.Variable{VarName: &node.Identifier{Value: "k"}},
Variable: &expr.List{
Items: []node.Node{
&expr.ArrayItem{
ByRef: false,
Val: &expr.Variable{VarName: &node.Identifier{Value: "v"}},
Val: &expr.Variable{VarName: &node.Identifier{Value: "v"}},
},
},
},

View File

@@ -224,25 +224,23 @@ var nodesToTest = []struct {
},
{
&stmt.Foreach{
ByRef: true,
Expr: &stmt.Expression{},
Key: &expr.Variable{},
Variable: &expr.Variable{},
Stmt: &stmt.StmtList{},
},
[]string{"Expr", "Key", "Variable", "Stmt"},
map[string]interface{}{"ByRef": true},
map[string]interface{}{},
},
{
&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},
map[string]interface{}{},
},
{
&stmt.Function{