#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

@@ -1,19 +1,33 @@
package scalar
import "github.com/z7zmey/php-parser/walker"
import (
"github.com/z7zmey/php-parser/position"
"github.com/z7zmey/php-parser/walker"
)
// Dnumber node
type Dnumber struct {
Value string
Position *position.Position
Value string
}
// NewDnumber node constructor
func NewDnumber(Value string) *Dnumber {
return &Dnumber{
Value,
Value: Value,
}
}
// SetPosition sets node position
func (n *Dnumber) SetPosition(p *position.Position) {
n.Position = p
}
// GetPosition returns node positions
func (n *Dnumber) GetPosition() *position.Position {
return n.Position
}
// Attributes returns node attributes as map
func (n *Dnumber) Attributes() map[string]interface{} {
return map[string]interface{}{

View File

@@ -2,21 +2,33 @@ package scalar
import (
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/position"
"github.com/z7zmey/php-parser/walker"
)
// Encapsed node
type Encapsed struct {
Parts []node.Node
Position *position.Position
Parts []node.Node
}
// NewEncapsed node constructor
func NewEncapsed(Parts []node.Node) *Encapsed {
return &Encapsed{
Parts,
Parts: Parts,
}
}
// SetPosition sets node position
func (n *Encapsed) SetPosition(p *position.Position) {
n.Position = p
}
// GetPosition returns node positions
func (n *Encapsed) GetPosition() *position.Position {
return n.Position
}
// Attributes returns node attributes as map
func (n *Encapsed) Attributes() map[string]interface{} {
return nil
@@ -38,4 +50,6 @@ func (n *Encapsed) Walk(v walker.Visitor) {
}
v.LeaveChildList("Parts", n)
}
v.LeaveNode(n)
}

View File

@@ -1,19 +1,33 @@
package scalar
import "github.com/z7zmey/php-parser/walker"
import (
"github.com/z7zmey/php-parser/position"
"github.com/z7zmey/php-parser/walker"
)
// EncapsedStringPart node
type EncapsedStringPart struct {
Value string
Position *position.Position
Value string
}
// NewEncapsedStringPart node constructor
func NewEncapsedStringPart(Value string) *EncapsedStringPart {
return &EncapsedStringPart{
Value,
Value: Value,
}
}
// SetPosition sets node position
func (n *EncapsedStringPart) SetPosition(p *position.Position) {
n.Position = p
}
// GetPosition returns node positions
func (n *EncapsedStringPart) GetPosition() *position.Position {
return n.Position
}
// Attributes returns node attributes as map
func (n *EncapsedStringPart) Attributes() map[string]interface{} {
return map[string]interface{}{

View File

@@ -2,23 +2,35 @@ package scalar
import (
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/position"
"github.com/z7zmey/php-parser/walker"
)
// Heredoc node
type Heredoc struct {
Label string
Parts []node.Node
Position *position.Position
Label string
Parts []node.Node
}
// NewHeredoc node constructor
func NewHeredoc(Label string, Parts []node.Node) *Heredoc {
return &Heredoc{
Label,
Parts,
Label: Label,
Parts: Parts,
}
}
// SetPosition sets node position
func (n *Heredoc) SetPosition(p *position.Position) {
n.Position = p
}
// GetPosition returns node positions
func (n *Heredoc) GetPosition() *position.Position {
return n.Position
}
// Attributes returns node attributes as map
func (n *Heredoc) Attributes() map[string]interface{} {
return map[string]interface{}{
@@ -42,4 +54,6 @@ func (n *Heredoc) Walk(v walker.Visitor) {
}
v.LeaveChildList("Parts", n)
}
v.LeaveNode(n)
}

View File

@@ -1,19 +1,33 @@
package scalar
import "github.com/z7zmey/php-parser/walker"
import (
"github.com/z7zmey/php-parser/position"
"github.com/z7zmey/php-parser/walker"
)
// Lnumber node
type Lnumber struct {
Value string
Position *position.Position
Value string
}
// NewLnumber node constructor
func NewLnumber(Value string) *Lnumber {
return &Lnumber{
Value,
Value: Value,
}
}
// SetPosition sets node position
func (n *Lnumber) SetPosition(p *position.Position) {
n.Position = p
}
// GetPosition returns node positions
func (n *Lnumber) GetPosition() *position.Position {
return n.Position
}
// Attributes returns node attributes as map
func (n *Lnumber) Attributes() map[string]interface{} {
return map[string]interface{}{

View File

@@ -1,19 +1,33 @@
package scalar
import "github.com/z7zmey/php-parser/walker"
import (
"github.com/z7zmey/php-parser/position"
"github.com/z7zmey/php-parser/walker"
)
// MagicConstant node
type MagicConstant struct {
Value string
Position *position.Position
Value string
}
// NewMagicConstant node constructor
func NewMagicConstant(Value string) *MagicConstant {
return &MagicConstant{
Value,
Value: Value,
}
}
// SetPosition sets node position
func (n *MagicConstant) SetPosition(p *position.Position) {
n.Position = p
}
// GetPosition returns node positions
func (n *MagicConstant) GetPosition() *position.Position {
return n.Position
}
// Attributes returns node attributes as map
func (n *MagicConstant) Attributes() map[string]interface{} {
return map[string]interface{}{

View File

@@ -1,19 +1,33 @@
package scalar
import "github.com/z7zmey/php-parser/walker"
import (
"github.com/z7zmey/php-parser/position"
"github.com/z7zmey/php-parser/walker"
)
// String node
type String struct {
Value string
Position *position.Position
Value string
}
// NewString node constructor
func NewString(Value string) *String {
return &String{
Value,
Value: Value,
}
}
// 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 map[string]interface{}{

View File

@@ -5,6 +5,7 @@ import (
"testing"
"github.com/z7zmey/php-parser/node/expr"
"github.com/z7zmey/php-parser/position"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/node/scalar"
@@ -17,12 +18,54 @@ func TestSimpleVar(t *testing.T) {
src := `<? "test $var";`
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: &scalar.Encapsed{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 14,
},
Parts: []node.Node{
&scalar.EncapsedStringPart{Value: "test "},
&expr.Variable{VarName: &node.Identifier{Value: "var"}},
&scalar.EncapsedStringPart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 5,
EndPos: 9,
},
Value: "test ",
},
&expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 10,
EndPos: 13,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 10,
EndPos: 13,
},
Value: "var",
},
},
},
},
},
@@ -44,12 +87,54 @@ func TestSimpleVarOneChar(t *testing.T) {
src := `<? "test $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: &scalar.Encapsed{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 12,
},
Parts: []node.Node{
&scalar.EncapsedStringPart{Value: "test "},
&expr.Variable{VarName: &node.Identifier{Value: "a"}},
&scalar.EncapsedStringPart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 5,
EndPos: 9,
},
Value: "test ",
},
&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",
},
},
},
},
},
@@ -71,13 +156,63 @@ func TestSimpleVarEndsEcapsed(t *testing.T) {
src := `<? "test $var\"";`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 17,
},
Stmts: []node.Node{
&stmt.Expression{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 17,
},
Expr: &scalar.Encapsed{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 16,
},
Parts: []node.Node{
&scalar.EncapsedStringPart{Value: "test "},
&expr.Variable{VarName: &node.Identifier{Value: "var"}},
&scalar.EncapsedStringPart{Value: "\\\""},
&scalar.EncapsedStringPart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 5,
EndPos: 9,
},
Value: "test ",
},
&expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 10,
EndPos: 13,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 10,
EndPos: 13,
},
Value: "var",
},
},
&scalar.EncapsedStringPart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 14,
EndPos: 15,
},
Value: "\\\"",
},
},
},
},
@@ -99,13 +234,71 @@ func TestStringVarCurveOpen(t *testing.T) {
src := `<? "=$a{$b}";`
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: &scalar.Encapsed{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 12,
},
Parts: []node.Node{
&scalar.EncapsedStringPart{Value: "="},
&expr.Variable{VarName: &node.Identifier{Value: "a"}},
&expr.Variable{VarName: &node.Identifier{Value: "b"}},
&scalar.EncapsedStringPart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 5,
EndPos: 5,
},
Value: "=",
},
&expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 6,
EndPos: 7,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 6,
EndPos: 7,
},
Value: "a",
},
},
&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: "b",
},
},
},
},
},
@@ -127,16 +320,80 @@ func TestSimpleVarPropertyFetch(t *testing.T) {
src := `<? "test $foo->bar()";`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 22,
},
Stmts: []node.Node{
&stmt.Expression{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 22,
},
Expr: &scalar.Encapsed{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 21,
},
Parts: []node.Node{
&scalar.EncapsedStringPart{Value: "test "},
&expr.PropertyFetch{
Variable: &expr.Variable{VarName: &node.Identifier{Value: "foo"}},
Property: &node.Identifier{Value: "bar"},
&scalar.EncapsedStringPart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 5,
EndPos: 9,
},
Value: "test ",
},
&expr.PropertyFetch{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 10,
EndPos: 18,
},
Variable: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 10,
EndPos: 13,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 10,
EndPos: 13,
},
Value: "foo",
},
},
Property: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 16,
EndPos: 18,
},
Value: "bar",
},
},
&scalar.EncapsedStringPart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 19,
EndPos: 20,
},
Value: "()",
},
&scalar.EncapsedStringPart{Value: "()"},
},
},
},
@@ -158,12 +415,54 @@ func TestDollarOpenCurlyBraces(t *testing.T) {
src := `<? "test ${foo}";`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 17,
},
Stmts: []node.Node{
&stmt.Expression{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 17,
},
Expr: &scalar.Encapsed{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 16,
},
Parts: []node.Node{
&scalar.EncapsedStringPart{Value: "test "},
&expr.Variable{VarName: &node.Identifier{Value: "foo"}},
&scalar.EncapsedStringPart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 5,
EndPos: 9,
},
Value: "test ",
},
&expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 10,
EndPos: 15,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 12,
EndPos: 14,
},
Value: "foo",
},
},
},
},
},
@@ -185,14 +484,70 @@ func TestDollarOpenCurlyBracesDimNumber(t *testing.T) {
src := `<? "test ${foo[0]}";`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 20,
},
Stmts: []node.Node{
&stmt.Expression{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 20,
},
Expr: &scalar.Encapsed{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 19,
},
Parts: []node.Node{
&scalar.EncapsedStringPart{Value: "test "},
&scalar.EncapsedStringPart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 5,
EndPos: 9,
},
Value: "test ",
},
&expr.ArrayDimFetch{
Variable: &expr.Variable{VarName: &node.Identifier{Value: "foo"}},
Dim: &scalar.Lnumber{Value: "0"},
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 10,
EndPos: 18,
},
Variable: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 12,
EndPos: 14,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 12,
EndPos: 14,
},
Value: "foo",
},
},
Dim: &scalar.Lnumber{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 16,
EndPos: 16,
},
Value: "0",
},
},
},
},
@@ -215,15 +570,78 @@ func TestCurlyOpenMethodCall(t *testing.T) {
src := `<? "test {$foo->bar()}";`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 24,
},
Stmts: []node.Node{
&stmt.Expression{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 24,
},
Expr: &scalar.Encapsed{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 23,
},
Parts: []node.Node{
&scalar.EncapsedStringPart{Value: "test "},
&scalar.EncapsedStringPart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 5,
EndPos: 9,
},
Value: "test ",
},
&expr.MethodCall{
Variable: &expr.Variable{VarName: &node.Identifier{Value: "foo"}},
Method: &node.Identifier{Value: "bar"},
ArgumentList: &node.ArgumentList{},
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 11,
EndPos: 21,
},
Variable: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 11,
EndPos: 14,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 11,
EndPos: 14,
},
Value: "foo",
},
},
Method: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 17,
EndPos: 19,
},
Value: "bar",
},
ArgumentList: &node.ArgumentList{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 20,
EndPos: 21,
},
},
},
},
},

View File

@@ -5,6 +5,7 @@ import (
"testing"
"github.com/z7zmey/php-parser/node/expr"
"github.com/z7zmey/php-parser/position"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/node/scalar"
@@ -20,14 +21,64 @@ LBL;
`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 3,
StartPos: 7,
EndPos: 24,
},
Stmts: []node.Node{
&stmt.Expression{
Position: &position.Position{
StartLine: 1,
EndLine: 3,
StartPos: 7,
EndPos: 24,
},
Expr: &scalar.Heredoc{
Position: &position.Position{
StartLine: 1,
EndLine: 3,
StartPos: 7,
EndPos: 23,
},
Label: "LBL",
Parts: []node.Node{
&scalar.EncapsedStringPart{Value: "test "},
&expr.Variable{VarName: &node.Identifier{Value: "var"}},
&scalar.EncapsedStringPart{Value: "\n"},
&scalar.EncapsedStringPart{
Position: &position.Position{
StartLine: 2,
EndLine: 2,
StartPos: 11,
EndPos: 15,
},
Value: "test ",
},
&expr.Variable{
Position: &position.Position{
StartLine: 2,
EndLine: 2,
StartPos: 16,
EndPos: 19,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 2,
EndLine: 2,
StartPos: 16,
EndPos: 19,
},
Value: "var",
},
},
&scalar.EncapsedStringPart{
Position: &position.Position{
StartLine: 2,
EndLine: 2,
StartPos: 20,
EndPos: 20,
},
Value: "\n",
},
},
},
},
@@ -52,14 +103,64 @@ LBL;
`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 3,
StartPos: 7,
EndPos: 26,
},
Stmts: []node.Node{
&stmt.Expression{
Position: &position.Position{
StartLine: 1,
EndLine: 3,
StartPos: 7,
EndPos: 26,
},
Expr: &scalar.Heredoc{
Position: &position.Position{
StartLine: 1,
EndLine: 3,
StartPos: 7,
EndPos: 25,
},
Label: "\"LBL\"",
Parts: []node.Node{
&scalar.EncapsedStringPart{Value: "test "},
&expr.Variable{VarName: &node.Identifier{Value: "var"}},
&scalar.EncapsedStringPart{Value: "\n"},
&scalar.EncapsedStringPart{
Position: &position.Position{
StartLine: 2,
EndLine: 2,
StartPos: 13,
EndPos: 17,
},
Value: "test ",
},
&expr.Variable{
Position: &position.Position{
StartLine: 2,
EndLine: 2,
StartPos: 18,
EndPos: 21,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 2,
EndLine: 2,
StartPos: 18,
EndPos: 21,
},
Value: "var",
},
},
&scalar.EncapsedStringPart{
Position: &position.Position{
StartLine: 2,
EndLine: 2,
StartPos: 22,
EndPos: 22,
},
Value: "\n",
},
},
},
},
@@ -84,12 +185,38 @@ LBL;
`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 3,
StartPos: 7,
EndPos: 26,
},
Stmts: []node.Node{
&stmt.Expression{
Position: &position.Position{
StartLine: 1,
EndLine: 3,
StartPos: 7,
EndPos: 26,
},
Expr: &scalar.Heredoc{
Position: &position.Position{
StartLine: 1,
EndLine: 3,
StartPos: 7,
EndPos: 25,
},
Label: "'LBL'",
Parts: []node.Node{
&scalar.EncapsedStringPart{Value: "test $var\n"},
&scalar.EncapsedStringPart{
Position: &position.Position{
StartLine: 2,
EndLine: 2,
StartPos: 13,
EndPos: 22,
},
Value: "test $var\n",
},
},
},
},
@@ -113,9 +240,27 @@ CAD;
`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 2,
StartPos: 7,
EndPos: 14,
},
Stmts: []node.Node{
&stmt.Expression{
Position: &position.Position{
StartLine: 1,
EndLine: 2,
StartPos: 7,
EndPos: 14,
},
Expr: &scalar.Heredoc{
Position: &position.Position{
StartLine: 1,
EndLine: 2,
StartPos: 7,
EndPos: 13,
},
Label: "CAD",
},
},
@@ -140,12 +285,38 @@ CAD;
`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 3,
StartPos: 7,
EndPos: 21,
},
Stmts: []node.Node{
&stmt.Expression{
Position: &position.Position{
StartLine: 1,
EndLine: 3,
StartPos: 7,
EndPos: 21,
},
Expr: &scalar.Heredoc{
Position: &position.Position{
StartLine: 1,
EndLine: 3,
StartPos: 7,
EndPos: 20,
},
Label: "CAD",
Parts: []node.Node{
&scalar.EncapsedStringPart{Value: "\thello\n"},
&scalar.EncapsedStringPart{
Position: &position.Position{
StartLine: 2,
EndLine: 2,
StartPos: 11,
EndPos: 17,
},
Value: "\thello\n",
},
},
},
},

View File

@@ -9,6 +9,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 TestMagicConstant(t *testing.T) {
@@ -16,9 +17,29 @@ func TestMagicConstant(t *testing.T) {
src := `<? __DIR__;`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 11,
},
Stmts: []node.Node{
&stmt.Expression{
Expr: &scalar.MagicConstant{Value: "__DIR__"},
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 11,
},
Expr: &scalar.MagicConstant{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 10,
},
Value: "__DIR__",
},
},
},
}

View File

@@ -11,6 +11,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{}) {
@@ -30,9 +31,29 @@ func TestLNumber(t *testing.T) {
src := `<? 1234567890123456789;`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 23,
},
Stmts: []node.Node{
&stmt.Expression{
Expr: &scalar.Lnumber{Value: "1234567890123456789"},
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 23,
},
Expr: &scalar.Lnumber{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 22,
},
Value: "1234567890123456789",
},
},
},
}
@@ -52,9 +73,29 @@ func TestDNumber(t *testing.T) {
src := `<? 12345678901234567890;`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 24,
},
Stmts: []node.Node{
&stmt.Expression{
Expr: &scalar.Dnumber{Value: "12345678901234567890"},
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 24,
},
Expr: &scalar.Dnumber{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 23,
},
Value: "12345678901234567890",
},
},
},
}
@@ -74,9 +115,29 @@ func TestFloat(t *testing.T) {
src := `<? 0.;`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 6,
},
Stmts: []node.Node{
&stmt.Expression{
Expr: &scalar.Dnumber{Value: "0."},
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 6,
},
Expr: &scalar.Dnumber{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 5,
},
Value: "0.",
},
},
},
}
@@ -96,9 +157,29 @@ func TestBinaryLNumber(t *testing.T) {
src := `<? 0b0111111111111111111111111111111111111111111111111111111111111111;`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 70,
},
Stmts: []node.Node{
&stmt.Expression{
Expr: &scalar.Lnumber{Value: "0b0111111111111111111111111111111111111111111111111111111111111111"},
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 70,
},
Expr: &scalar.Lnumber{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 69,
},
Value: "0b0111111111111111111111111111111111111111111111111111111111111111",
},
},
},
}
@@ -118,9 +199,29 @@ func TestBinaryDNumber(t *testing.T) {
src := `<? 0b1111111111111111111111111111111111111111111111111111111111111111;`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 70,
},
Stmts: []node.Node{
&stmt.Expression{
Expr: &scalar.Dnumber{Value: "0b1111111111111111111111111111111111111111111111111111111111111111"},
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 70,
},
Expr: &scalar.Dnumber{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 69,
},
Value: "0b1111111111111111111111111111111111111111111111111111111111111111",
},
},
},
}
@@ -140,9 +241,29 @@ func TestHLNumber(t *testing.T) {
src := `<? 0x007111111111111111;`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 24,
},
Stmts: []node.Node{
&stmt.Expression{
Expr: &scalar.Lnumber{Value: "0x007111111111111111"},
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 24,
},
Expr: &scalar.Lnumber{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 23,
},
Value: "0x007111111111111111",
},
},
},
}
@@ -162,9 +283,29 @@ func TestHDNumber(t *testing.T) {
src := `<? 0x8111111111111111;`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 22,
},
Stmts: []node.Node{
&stmt.Expression{
Expr: &scalar.Dnumber{Value: "0x8111111111111111"},
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 22,
},
Expr: &scalar.Dnumber{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 21,
},
Value: "0x8111111111111111",
},
},
},
}

View File

@@ -9,15 +9,36 @@ 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 TestDoubleQuotedScalarString(t *testing.T) {
src := `<? "test";`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 10,
},
Stmts: []node.Node{
&stmt.Expression{
Expr: &scalar.String{Value: "\"test\""},
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 10,
},
Expr: &scalar.String{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 9,
},
Value: "\"test\"",
},
},
},
}
@@ -32,13 +53,34 @@ func TestDoubleQuotedScalarString(t *testing.T) {
actual = php5parser.GetRootNode()
assertEqual(t, expected, actual)
}
func TestDoubleQuotedScalarStringWithEscapedVar(t *testing.T) {
src := `<? "\$test";`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 12,
},
Stmts: []node.Node{
&stmt.Expression{
Expr: &scalar.String{Value: "\"\\$test\""},
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 12,
},
Expr: &scalar.String{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 11,
},
Value: "\"\\$test\"",
},
},
},
}
@@ -60,9 +102,29 @@ func TestMultilineDoubleQuotedScalarString(t *testing.T) {
";`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 3,
StartPos: 4,
EndPos: 14,
},
Stmts: []node.Node{
&stmt.Expression{
Expr: &scalar.String{Value: "\"\n\ttest\n\t\""},
Position: &position.Position{
StartLine: 1,
EndLine: 3,
StartPos: 4,
EndPos: 14,
},
Expr: &scalar.String{
Position: &position.Position{
StartLine: 1,
EndLine: 3,
StartPos: 4,
EndPos: 13,
},
Value: "\"\n\ttest\n\t\"",
},
},
},
}
@@ -82,9 +144,29 @@ func TestSingleQuotedScalarString(t *testing.T) {
src := `<? '$test';`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 11,
},
Stmts: []node.Node{
&stmt.Expression{
Expr: &scalar.String{Value: "'$test'"},
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 11,
},
Expr: &scalar.String{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 10,
},
Value: "'$test'",
},
},
},
}
@@ -106,9 +188,29 @@ func TestMultilineSingleQuotedScalarString(t *testing.T) {
';`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 3,
StartPos: 4,
EndPos: 15,
},
Stmts: []node.Node{
&stmt.Expression{
Expr: &scalar.String{Value: "'\n\t$test\n\t'"},
Position: &position.Position{
StartLine: 1,
EndLine: 3,
StartPos: 4,
EndPos: 15,
},
Expr: &scalar.String{
Position: &position.Position{
StartLine: 1,
EndLine: 3,
StartPos: 4,
EndPos: 14,
},
Value: "'\n\t$test\n\t'",
},
},
},
}