#25: save position within node

This commit is contained in:
z7zmey
2018-06-24 10:19:44 +03:00
parent 6ac67675d5
commit 1ebb0c6fad
268 changed files with 53606 additions and 7719 deletions

View File

@@ -2,21 +2,33 @@ package cast
import (
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/position"
"github.com/z7zmey/php-parser/walker"
)
// Array node
type Array struct {
Expr node.Node
Position *position.Position
Expr node.Node
}
// NewArray node constructor
func NewArray(Expr node.Node) *Array {
return &Array{
Expr,
Expr: Expr,
}
}
// SetPosition sets node position
func (n *Array) SetPosition(p *position.Position) {
n.Position = p
}
// GetPosition returns node positions
func (n *Array) GetPosition() *position.Position {
return n.Position
}
// Attributes returns node attributes as map
func (n *Array) Attributes() map[string]interface{} {
return nil

View File

@@ -2,21 +2,33 @@ package cast
import (
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/position"
"github.com/z7zmey/php-parser/walker"
)
// Bool node
type Bool struct {
Expr node.Node
Position *position.Position
Expr node.Node
}
// NewBool node constructor
func NewBool(Expr node.Node) *Bool {
return &Bool{
Expr,
Expr: Expr,
}
}
// SetPosition sets node position
func (n *Bool) SetPosition(p *position.Position) {
n.Position = p
}
// GetPosition returns node positions
func (n *Bool) GetPosition() *position.Position {
return n.Position
}
// Attributes returns node attributes as map
func (n *Bool) Attributes() map[string]interface{} {
return nil

View File

@@ -2,21 +2,33 @@ package cast
import (
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/position"
"github.com/z7zmey/php-parser/walker"
)
// Double node
type Double struct {
Expr node.Node
Position *position.Position
Expr node.Node
}
// NewDouble node constructor
func NewDouble(Expr node.Node) *Double {
return &Double{
Expr,
Expr: Expr,
}
}
// SetPosition sets node position
func (n *Double) SetPosition(p *position.Position) {
n.Position = p
}
// GetPosition returns node positions
func (n *Double) GetPosition() *position.Position {
return n.Position
}
// Attributes returns node attributes as map
func (n *Double) Attributes() map[string]interface{} {
return nil

View File

@@ -2,21 +2,33 @@ package cast
import (
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/position"
"github.com/z7zmey/php-parser/walker"
)
// Int node
type Int struct {
Expr node.Node
Position *position.Position
Expr node.Node
}
// NewInt node constructor
func NewInt(Expr node.Node) *Int {
return &Int{
Expr,
Expr: Expr,
}
}
// SetPosition sets node position
func (n *Int) SetPosition(p *position.Position) {
n.Position = p
}
// GetPosition returns node positions
func (n *Int) GetPosition() *position.Position {
return n.Position
}
// Attributes returns node attributes as map
func (n *Int) Attributes() map[string]interface{} {
return nil

View File

@@ -2,21 +2,33 @@ package cast
import (
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/position"
"github.com/z7zmey/php-parser/walker"
)
// Object node
type Object struct {
Expr node.Node
Position *position.Position
Expr node.Node
}
// NewObject node constructor
func NewObject(Expr node.Node) *Object {
return &Object{
Expr,
Expr: Expr,
}
}
// SetPosition sets node position
func (n *Object) SetPosition(p *position.Position) {
n.Position = p
}
// GetPosition returns node positions
func (n *Object) GetPosition() *position.Position {
return n.Position
}
// Attributes returns node attributes as map
func (n *Object) Attributes() map[string]interface{} {
return nil

View File

@@ -2,21 +2,33 @@ package cast
import (
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/position"
"github.com/z7zmey/php-parser/walker"
)
// String node
type String struct {
Expr node.Node
Position *position.Position
Expr node.Node
}
// NewString node constructor
func NewString(Expr node.Node) *String {
return &String{
Expr,
Expr: Expr,
}
}
// SetPosition sets node position
func (n *String) SetPosition(p *position.Position) {
n.Position = p
}
// GetPosition returns node positions
func (n *String) GetPosition() *position.Position {
return n.Position
}
// Attributes returns node attributes as map
func (n *String) Attributes() map[string]interface{} {
return nil

View File

@@ -2,21 +2,33 @@ package cast
import (
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/position"
"github.com/z7zmey/php-parser/walker"
)
// Unset node
type Unset struct {
Expr node.Node
Position *position.Position
Expr node.Node
}
// NewUnset node constructor
func NewUnset(Expr node.Node) *Unset {
return &Unset{
Expr,
Expr: Expr,
}
}
// SetPosition sets node position
func (n *Unset) SetPosition(p *position.Position) {
n.Position = p
}
// GetPosition returns node positions
func (n *Unset) GetPosition() *position.Position {
return n.Position
}
// Attributes returns node attributes as map
func (n *Unset) Attributes() map[string]interface{} {
return nil

View File

@@ -13,6 +13,7 @@ import (
"github.com/z7zmey/php-parser/node/stmt"
"github.com/z7zmey/php-parser/php5"
"github.com/z7zmey/php-parser/php7"
"github.com/z7zmey/php-parser/position"
)
func assertEqual(t *testing.T, expected interface{}, actual interface{}) {
@@ -31,10 +32,44 @@ func TestArray(t *testing.T) {
src := `<? (array)$a;`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 13,
},
Stmts: []node.Node{
&stmt.Expression{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 13,
},
Expr: &cast.Array{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 12,
},
Expr: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 11,
EndPos: 12,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 11,
EndPos: 12,
},
Value: "a",
},
},
},
},
},
@@ -55,10 +90,44 @@ func TestBool(t *testing.T) {
src := `<? (boolean)$a;`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 15,
},
Stmts: []node.Node{
&stmt.Expression{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 15,
},
Expr: &cast.Bool{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 14,
},
Expr: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 13,
EndPos: 14,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 13,
EndPos: 14,
},
Value: "a",
},
},
},
},
},
@@ -79,10 +148,44 @@ func TestBoolShort(t *testing.T) {
src := `<? (bool)$a;`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 12,
},
Stmts: []node.Node{
&stmt.Expression{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 12,
},
Expr: &cast.Bool{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 11,
},
Expr: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 10,
EndPos: 11,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 10,
EndPos: 11,
},
Value: "a",
},
},
},
},
},
@@ -103,10 +206,44 @@ func TestDouble(t *testing.T) {
src := `<? (double)$a;`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 14,
},
Stmts: []node.Node{
&stmt.Expression{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 14,
},
Expr: &cast.Double{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 13,
},
Expr: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 12,
EndPos: 13,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 12,
EndPos: 13,
},
Value: "a",
},
},
},
},
},
@@ -127,10 +264,44 @@ func TestCastFloat(t *testing.T) {
src := `<? (float)$a;`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 13,
},
Stmts: []node.Node{
&stmt.Expression{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 13,
},
Expr: &cast.Double{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 12,
},
Expr: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 11,
EndPos: 12,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 11,
EndPos: 12,
},
Value: "a",
},
},
},
},
},
@@ -151,10 +322,44 @@ func TestInt(t *testing.T) {
src := `<? (integer)$a;`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 15,
},
Stmts: []node.Node{
&stmt.Expression{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 15,
},
Expr: &cast.Int{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 14,
},
Expr: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 13,
EndPos: 14,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 13,
EndPos: 14,
},
Value: "a",
},
},
},
},
},
@@ -175,10 +380,44 @@ func TestIntShort(t *testing.T) {
src := `<? (int)$a;`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 11,
},
Stmts: []node.Node{
&stmt.Expression{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 11,
},
Expr: &cast.Int{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 10,
},
Expr: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 9,
EndPos: 10,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 9,
EndPos: 10,
},
Value: "a",
},
},
},
},
},
@@ -199,10 +438,44 @@ func TestObject(t *testing.T) {
src := `<? (object)$a;`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 14,
},
Stmts: []node.Node{
&stmt.Expression{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 14,
},
Expr: &cast.Object{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 13,
},
Expr: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 12,
EndPos: 13,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 12,
EndPos: 13,
},
Value: "a",
},
},
},
},
},
@@ -223,10 +496,102 @@ func TestString(t *testing.T) {
src := `<? (string)$a;`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 14,
},
Stmts: []node.Node{
&stmt.Expression{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 14,
},
Expr: &cast.String{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 13,
},
Expr: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 12,
EndPos: 13,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 12,
EndPos: 13,
},
Value: "a",
},
},
},
},
},
}
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 TestBinaryString(t *testing.T) {
src := `<? (binary)$a;`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 14,
},
Stmts: []node.Node{
&stmt.Expression{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 14,
},
Expr: &cast.String{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 13,
},
Expr: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 12,
EndPos: 13,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 12,
EndPos: 13,
},
Value: "a",
},
},
},
},
},
@@ -247,10 +612,44 @@ func TestUnset(t *testing.T) {
src := `<? (unset)$a;`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 13,
},
Stmts: []node.Node{
&stmt.Expression{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 13,
},
Expr: &cast.Unset{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 12,
},
Expr: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 11,
EndPos: 12,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 11,
EndPos: 12,
},
Value: "a",
},
},
},
},
},