Merge branch 'comments3'

This commit is contained in:
z7zmey 2018-06-07 15:06:54 +03:00
commit af379a61dd
138 changed files with 17339 additions and 10027 deletions

View File

@ -122,9 +122,13 @@ nodes := &stmt.StmtList{
&name.NamePart{Value: "Bar"}, &name.NamePart{Value: "Bar"},
}, },
}, },
Extends: &name.Name{ Extends: &stmt.ClassExtends{
ClassName: &name.Name{
Parts: []node.Node{ Parts: []node.Node{
&name.NamePart{Value: "Baz"}, &name.NamePart{
Value: "Baz"
},
},
}, },
}, },
Stmts: []node.Node{ Stmts: []node.Node{
@ -133,6 +137,7 @@ nodes := &stmt.StmtList{
&node.Identifier{Value: "public"}, &node.Identifier{Value: "public"},
}, },
MethodName: &node.Identifier{Value: "greet"}, MethodName: &node.Identifier{Value: "greet"},
Stmt: &stmt.StmtList{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Echo{ &stmt.Echo{
Exprs: []node.Node{ Exprs: []node.Node{
@ -144,6 +149,7 @@ nodes := &stmt.StmtList{
}, },
}, },
}, },
},
} }
file := os.Stdout file := os.Stdout

View File

@ -1,16 +1,40 @@
package comment package comment
import "github.com/z7zmey/php-parser/node" import (
"github.com/z7zmey/php-parser/position"
)
// Comment represents comment lines in the code // Comment aggrigates information about comment /**
type Comment interface { type Comment struct {
String() string value string
position *position.Position
tokenName TokenName
} }
// Comments a collection of comment groups assigned to nodes // NewComment - Comment constructor
type Comments map[node.Node][]Comment func NewComment(value string, pos *position.Position) *Comment {
return &Comment{
// AddComments add comment group to the collection value,
func (c Comments) AddComments(node node.Node, comments []Comment) { pos,
c[node] = append(c[node], comments...) UnknownToken,
}
}
// SetTokenName sets token name
func (c *Comment) SetTokenName(tokenName TokenName) {
c.tokenName = tokenName
}
// TokenName returns token name
func (c *Comment) TokenName() TokenName {
return c.tokenName
}
func (c *Comment) String() string {
return c.value
}
// Position returns comment position
func (c *Comment) Position() *position.Position {
return c.position
} }

View File

@ -3,25 +3,31 @@ package comment_test
import ( import (
"testing" "testing"
"github.com/z7zmey/php-parser/position"
"github.com/z7zmey/php-parser/comment" "github.com/z7zmey/php-parser/comment"
"github.com/z7zmey/php-parser/node"
) )
func TestComments(t *testing.T) { func TestCommentGetPosition(t *testing.T) {
n := node.NewIdentifier("test") expected := position.NewPosition(0, 0, 0, 0)
commentGroup := []comment.Comment{ comment := comment.NewComment("/** hello world */", expected)
comment.NewDocComment("/** hello world */"),
comment.NewPlainComment("// hello world"),
}
comments := comment.Comments{} actual := comment.Position()
comments.AddComments(n, commentGroup)
if comments[n][0].String() != "/** hello world */" { if expected != actual {
t.Errorf("expected and actual are not equal\n") t.Errorf("expected and actual are not equal\n")
} }
if comments[n][1].String() != "// hello world" { }
func TestCommentPrint(t *testing.T) {
expected := "/** hello world */"
comment := comment.NewComment(expected, nil)
actual := comment.String()
if expected != actual {
t.Errorf("expected and actual are not equal\n") t.Errorf("expected and actual are not equal\n")
} }
} }

View File

@ -1,17 +0,0 @@
package comment
// DocComment represents comments that start /**
type DocComment struct {
value string
}
// NewDocComment - DocComment constructor
func NewDocComment(value string) *DocComment {
return &DocComment{
value,
}
}
func (c *DocComment) String() string {
return c.value
}

View File

@ -1,17 +0,0 @@
package comment
// PlainComment represents comments that dont start /**
type PlainComment struct {
value string
}
// NewPlainComment - PlainComment constructor
func NewPlainComment(value string) *PlainComment {
return &PlainComment{
value,
}
}
func (c *PlainComment) String() string {
return c.value
}

328
comment/tokenNames.go Normal file
View File

@ -0,0 +1,328 @@
package comment
// TokenName is used to specify a comment position
type TokenName int
const (
UnknownToken TokenName = iota
IncludeToken
IncludeOnceToken
ExitToken
IfToken
LnumberToken
DnumberToken
StringToken
StringVarnameToken
VariableToken
NumStringToken
InlineHTMLToken
EncapsedAndWhitespaceToken
ConstantEncapsedStringToken
EchoToken
DoToken
WhileToken
EndwhileToken
ForInitSemicolonToken
ForCondSemicolonToken
ForToken
EndforToken
ForeachToken
EndforeachToken
DeclareToken
EnddeclareToken
AsToken
SwitchToken
EndswitchToken
CaseToken
DefaultToken
BreakToken
ContinueToken
GotoToken
FunctionToken
ConstToken
ReturnToken
TryToken
CatchToken
FinallyToken
ThrowToken
UseToken
InsteadofToken
GlobalToken
VarToken
UnsetToken
IssetToken
EmptyToken
ClassToken
TraitToken
InterfaceToken
ExtendsToken
ImplementsToken
DoubleArrowToken
ListToken
ArrayToken
CallableToken
ClassCToken
TraitCToken
MethodCToken
FuncCToken
LineToken
FileToken
StartHeredocToken
DollarOpenCurlyBracesToken
CurlyOpenToken
PaamayimNekudotayimToken
NamespaceToken
NsCToken
DirToken
NsSeparatorToken
EllipsisToken
EvalToken
RequireToken
RequireOnceToken
LogicalOrToken
LogicalXorToken
LogicalAndToken
InstanceofToken
NewToken
CloneToken
ElseifToken
ElseToken
EndifToken
PrintToken
YieldToken
StaticToken
AbstractToken
FinalToken
PrivateToken
ProtectedToken
PublicToken
IncToken
DecToken
YieldFromToken
ObjectOperatorToken
IntCastToken
DoubleCastToken
StringCastToken
ArrayCastToken
ObjectCastToken
BoolCastToken
UnsetCastToken
CoalesceToken
SpaceshipToken
PlusEqualToken
MinusEqualToken
MulEqualToken
PowEqualToken
DivEqualToken
ConcatEqualToken
ModEqualToken
AndEqualToken
OrEqualToken
XorEqualToken
SlEqualToken
SrEqualToken
BooleanOrToken
BooleanAndToken
PowToken
SlToken
SrToken
IsIdenticalToken
IsNotIdenticalToken
IsEqualToken
IsNotEqualToken
IsSmallerOrEqualToken
IsGreaterOrEqualToken
HaltCompilerToken
IdentifierToken
CaseSeparatorToken // ';' or ':'
DoubleQuoteToken // '"'
BackquoteToken // '`'
OpenCurlyBracesToken // '{'
CloseCurlyBracesToken // '}'
SemiColonToken // ';'
ColonToken // ':'
OpenParenthesisToken // '('
CloseParenthesisToken // ')'
OpenSquareBracket // '['
CloseSquareBracket // ']'
QuestionMarkToken // '?'
AmpersandToken // '&'
MinusToken // '-'
PlusToken // '+'
ExclamationMarkToken // '!'
TildeToken // '~'
AtToken // '@'
DollarToken // '$'
CommaToken // ','
VerticalBarToken // '|'
EqualToken // '='
CaretToken // '^'
AsteriskToken // '*'
SlashToken // '/'
PercentToken // '%'
LessToken // '<'
GreaterToken // '>'
DotToken // '.'
)
var TokenNames = map[TokenName]string{
UnknownToken: "UnknownToken",
IncludeToken: "IncludeToken",
IncludeOnceToken: "IncludeOnceToken",
ExitToken: "ExitToken",
IfToken: "IfToken",
LnumberToken: "LnumberToken",
DnumberToken: "DnumberToken",
StringToken: "StringToken",
StringVarnameToken: "StringVarnameToken",
VariableToken: "VariableToken",
NumStringToken: "NumStringToken",
InlineHTMLToken: "InlineHTMLToken",
EncapsedAndWhitespaceToken: "EncapsedAndWhitespaceToken",
ConstantEncapsedStringToken: "ConstantEncapsedStringToken",
EchoToken: "EchoToken",
DoToken: "DoToken",
WhileToken: "WhileToken",
EndwhileToken: "EndwhileToken",
ForInitSemicolonToken: "ForInitSemicolonToken",
ForCondSemicolonToken: "ForCondSemicolonToken",
ForToken: "ForToken",
EndforToken: "EndforToken",
ForeachToken: "ForeachToken",
EndforeachToken: "EndforeachToken",
DeclareToken: "DeclareToken",
EnddeclareToken: "EnddeclareToken",
AsToken: "AsToken",
SwitchToken: "SwitchToken",
EndswitchToken: "EndswitchToken",
CaseToken: "CaseToken",
DefaultToken: "DefaultToken",
BreakToken: "BreakToken",
ContinueToken: "ContinueToken",
GotoToken: "GotoToken",
FunctionToken: "FunctionToken",
ConstToken: "ConstToken",
ReturnToken: "ReturnToken",
TryToken: "TryToken",
CatchToken: "CatchToken",
FinallyToken: "FinallyToken",
ThrowToken: "ThrowToken",
UseToken: "UseToken",
InsteadofToken: "InsteadofToken",
GlobalToken: "GlobalToken",
VarToken: "VarToken",
UnsetToken: "UnsetToken",
IssetToken: "IssetToken",
EmptyToken: "EmptyToken",
ClassToken: "ClassToken",
TraitToken: "TraitToken",
InterfaceToken: "InterfaceToken",
ExtendsToken: "ExtendsToken",
ImplementsToken: "ImplementsToken",
DoubleArrowToken: "DoubleArrowToken",
ListToken: "ListToken",
ArrayToken: "ArrayToken",
CallableToken: "CallableToken",
ClassCToken: "ClassCToken",
TraitCToken: "TraitCToken",
MethodCToken: "MethodCToken",
FuncCToken: "FuncCToken",
LineToken: "LineToken",
FileToken: "FileToken",
StartHeredocToken: "StartHeredocToken",
DollarOpenCurlyBracesToken: "DollarOpenCurlyBracesToken",
CurlyOpenToken: "CurlyOpenToken",
PaamayimNekudotayimToken: "PaamayimNekudotayimToken",
NamespaceToken: "NamespaceToken",
NsCToken: "NsCToken",
DirToken: "DirToken",
NsSeparatorToken: "NsSeparatorToken",
EllipsisToken: "EllipsisToken",
EvalToken: "EvalToken",
RequireToken: "RequireToken",
RequireOnceToken: "RequireOnceToken",
LogicalOrToken: "LogicalOrToken",
LogicalXorToken: "LogicalXorToken",
LogicalAndToken: "LogicalAndToken",
InstanceofToken: "InstanceofToken",
NewToken: "NewToken",
CloneToken: "CloneToken",
ElseifToken: "ElseifToken",
ElseToken: "ElseToken",
EndifToken: "EndifToken",
PrintToken: "PrintToken",
YieldToken: "YieldToken",
StaticToken: "StaticToken",
AbstractToken: "AbstractToken",
FinalToken: "FinalToken",
PrivateToken: "PrivateToken",
ProtectedToken: "ProtectedToken",
PublicToken: "PublicToken",
IncToken: "IncToken",
DecToken: "DecToken",
YieldFromToken: "YieldFromToken",
ObjectOperatorToken: "ObjectOperatorToken",
IntCastToken: "IntCastToken",
DoubleCastToken: "DoubleCastToken",
StringCastToken: "StringCastToken",
ArrayCastToken: "ArrayCastToken",
ObjectCastToken: "ObjectCastToken",
BoolCastToken: "BoolCastToken",
UnsetCastToken: "UnsetCastToken",
CoalesceToken: "CoalesceToken",
SpaceshipToken: "SpaceshipToken",
PlusEqualToken: "PlusEqualToken",
MinusEqualToken: "MinusEqualToken",
MulEqualToken: "MulEqualToken",
PowEqualToken: "PowEqualToken",
DivEqualToken: "DivEqualToken",
ConcatEqualToken: "ConcatEqualToken",
ModEqualToken: "ModEqualToken",
AndEqualToken: "AndEqualToken",
OrEqualToken: "OrEqualToken",
XorEqualToken: "XorEqualToken",
SlEqualToken: "SlEqualToken",
SrEqualToken: "SrEqualToken",
BooleanOrToken: "BooleanOrToken",
BooleanAndToken: "BooleanAndToken",
PowToken: "PowToken",
SlToken: "SlToken",
SrToken: "SrToken",
IsIdenticalToken: "IsIdenticalToken",
IsNotIdenticalToken: "IsNotIdenticalToken",
IsEqualToken: "IsEqualToken",
IsNotEqualToken: "IsNotEqualToken",
IsSmallerOrEqualToken: "IsSmallerOrEqualToken",
IsGreaterOrEqualToken: "IsGreaterOrEqualToken",
HaltCompilerToken: "HaltCompilerToken",
IdentifierToken: "IdentifierToken",
CaseSeparatorToken: "CaseSeparatorToken",
DoubleQuoteToken: "DoubleQuoteToken",
BackquoteToken: "BackquoteToken",
OpenCurlyBracesToken: "OpenCurlyBracesToken",
CloseCurlyBracesToken: "CloseCurlyBracesToken",
SemiColonToken: "SemiColonToken",
ColonToken: "ColonToken",
OpenParenthesisToken: "OpenParenthesisToken",
CloseParenthesisToken: "CloseParenthesisToken",
OpenSquareBracket: "OpenSquareBracket",
CloseSquareBracket: "CloseSquareBracket",
QuestionMarkToken: "QuestionMarkToken",
AmpersandToken: "AmpersandToken",
MinusToken: "MinusToken",
PlusToken: "PlusToken",
ExclamationMarkToken: "ExclamationMarkToken",
TildeToken: "TildeToken",
AtToken: "AtToken",
DollarToken: "DollarToken",
CommaToken: "CommaToken",
VerticalBarToken: "VerticalBarToken",
EqualToken: "EqualToken",
CaretToken: "CaretToken",
AsteriskToken: "AsteriskToken",
SlashToken: "SlashToken",
PercentToken: "PercentToken",
LessToken: "LessToken",
GreaterToken: "GreaterToken",
DotToken: "DotToken",
}

View File

@ -4,25 +4,20 @@ import (
"fmt" "fmt"
"github.com/z7zmey/php-parser/position" "github.com/z7zmey/php-parser/position"
"github.com/z7zmey/php-parser/token" "github.com/z7zmey/php-parser/scanner"
) )
// Error parsing error // Error parsing error
type Error struct { type Error struct {
Msg string Msg string
Pos position.Position Pos *position.Position
} }
// NewError creates and returns new Error // NewError creates and returns new Error
func NewError(msg string, t token.Token) *Error { func NewError(msg string, t *scanner.Token) *Error {
return &Error{ return &Error{
Msg: msg, Msg: msg,
Pos: position.Position{ Pos: t.Position(),
StartLine: t.StartLine,
EndLine: t.EndLine,
StartPos: t.StartPos,
EndPos: t.EndPos,
},
} }
} }

View File

@ -7,7 +7,7 @@ import (
"github.com/z7zmey/php-parser/position" "github.com/z7zmey/php-parser/position"
"github.com/z7zmey/php-parser/errors" "github.com/z7zmey/php-parser/errors"
"github.com/z7zmey/php-parser/token" "github.com/z7zmey/php-parser/scanner"
"github.com/kylelemons/godebug/pretty" "github.com/kylelemons/godebug/pretty"
) )
@ -26,37 +26,22 @@ func assertEqual(t *testing.T, expected interface{}, actual interface{}) {
} }
func TestConstructor(t *testing.T) { func TestConstructor(t *testing.T) {
token := token.Token{ pos := position.NewPosition(1, 2, 3, 4)
Value: "test", token := scanner.NewToken(`test`, pos)
StartLine: 1,
EndLine: 2,
StartPos: 3,
EndPos: 4,
}
actual := errors.NewError("message", token) actual := errors.NewError("message", token)
expected := &errors.Error{ expected := &errors.Error{
Msg: "message", Msg: "message",
Pos: position.Position{ Pos: pos,
StartLine: 1,
EndLine: 2,
StartPos: 3,
EndPos: 4,
},
} }
assertEqual(t, expected, actual) assertEqual(t, expected, actual)
} }
func TestPrint(t *testing.T) { func TestPrint(t *testing.T) {
token := token.Token{ pos := position.NewPosition(1, 2, 3, 4)
Value: "test", token := scanner.NewToken(`test`, pos)
StartLine: 1,
EndLine: 2,
StartPos: 3,
EndPos: 4,
}
Error := errors.NewError("message", token) Error := errors.NewError("message", token)

View File

@ -32,7 +32,7 @@ func assertEqual(t *testing.T, expected interface{}, actual interface{}) {
func TestReference(t *testing.T) { func TestReference(t *testing.T) {
src := `<? $a =& $b;` src := `<? $a =& $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &assign.Reference{ Expr: &assign.Reference{
@ -57,7 +57,7 @@ func TestReference(t *testing.T) {
func TestReferenceNew(t *testing.T) { func TestReferenceNew(t *testing.T) {
src := `<? $a =& new Foo;` src := `<? $a =& new Foo;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &assign.Reference{ Expr: &assign.Reference{
@ -88,7 +88,7 @@ func TestReferenceNew(t *testing.T) {
func TestReferenceArgs(t *testing.T) { func TestReferenceArgs(t *testing.T) {
src := `<? $a =& new Foo($b);` src := `<? $a =& new Foo($b);`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &assign.Reference{ Expr: &assign.Reference{
@ -99,6 +99,7 @@ func TestReferenceArgs(t *testing.T) {
&name.NamePart{Value: "Foo"}, &name.NamePart{Value: "Foo"},
}, },
}, },
ArgumentList: &node.ArgumentList{
Arguments: []node.Node{ Arguments: []node.Node{
&node.Argument{ &node.Argument{
Variadic: false, Variadic: false,
@ -110,6 +111,7 @@ func TestReferenceArgs(t *testing.T) {
}, },
}, },
}, },
},
} }
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php") php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
@ -126,7 +128,7 @@ func TestReferenceArgs(t *testing.T) {
func TestAssign(t *testing.T) { func TestAssign(t *testing.T) {
src := `<? $a = $b;` src := `<? $a = $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &assign.Assign{ Expr: &assign.Assign{
@ -151,7 +153,7 @@ func TestAssign(t *testing.T) {
func TestBitwiseAnd(t *testing.T) { func TestBitwiseAnd(t *testing.T) {
src := `<? $a &= $b;` src := `<? $a &= $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &assign.BitwiseAnd{ Expr: &assign.BitwiseAnd{
@ -176,7 +178,7 @@ func TestBitwiseAnd(t *testing.T) {
func TestBitwiseOr(t *testing.T) { func TestBitwiseOr(t *testing.T) {
src := `<? $a |= $b;` src := `<? $a |= $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &assign.BitwiseOr{ Expr: &assign.BitwiseOr{
@ -201,7 +203,7 @@ func TestBitwiseOr(t *testing.T) {
func TestBitwiseXor(t *testing.T) { func TestBitwiseXor(t *testing.T) {
src := `<? $a ^= $b;` src := `<? $a ^= $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &assign.BitwiseXor{ Expr: &assign.BitwiseXor{
@ -226,7 +228,7 @@ func TestBitwiseXor(t *testing.T) {
func TestConcat(t *testing.T) { func TestConcat(t *testing.T) {
src := `<? $a .= $b;` src := `<? $a .= $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &assign.Concat{ Expr: &assign.Concat{
@ -251,7 +253,7 @@ func TestConcat(t *testing.T) {
func TestDiv(t *testing.T) { func TestDiv(t *testing.T) {
src := `<? $a /= $b;` src := `<? $a /= $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &assign.Div{ Expr: &assign.Div{
@ -276,7 +278,7 @@ func TestDiv(t *testing.T) {
func TestMinus(t *testing.T) { func TestMinus(t *testing.T) {
src := `<? $a -= $b;` src := `<? $a -= $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &assign.Minus{ Expr: &assign.Minus{
@ -301,7 +303,7 @@ func TestMinus(t *testing.T) {
func TestMod(t *testing.T) { func TestMod(t *testing.T) {
src := `<? $a %= $b;` src := `<? $a %= $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &assign.Mod{ Expr: &assign.Mod{
@ -326,7 +328,7 @@ func TestMod(t *testing.T) {
func TestMul(t *testing.T) { func TestMul(t *testing.T) {
src := `<? $a *= $b;` src := `<? $a *= $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &assign.Mul{ Expr: &assign.Mul{
@ -351,7 +353,7 @@ func TestMul(t *testing.T) {
func TestPlus(t *testing.T) { func TestPlus(t *testing.T) {
src := `<? $a += $b;` src := `<? $a += $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &assign.Plus{ Expr: &assign.Plus{
@ -376,7 +378,7 @@ func TestPlus(t *testing.T) {
func TestPow(t *testing.T) { func TestPow(t *testing.T) {
src := `<? $a **= $b;` src := `<? $a **= $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &assign.Pow{ Expr: &assign.Pow{
@ -401,7 +403,7 @@ func TestPow(t *testing.T) {
func TestShiftLeft(t *testing.T) { func TestShiftLeft(t *testing.T) {
src := `<? $a <<= $b;` src := `<? $a <<= $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &assign.ShiftLeft{ Expr: &assign.ShiftLeft{
@ -426,7 +428,7 @@ func TestShiftLeft(t *testing.T) {
func TestShiftRight(t *testing.T) { func TestShiftRight(t *testing.T) {
src := `<? $a >>= $b;` src := `<? $a >>= $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &assign.ShiftRight{ Expr: &assign.ShiftRight{

View File

@ -30,7 +30,7 @@ func assertEqual(t *testing.T, expected interface{}, actual interface{}) {
func TestBitwiseAnd(t *testing.T) { func TestBitwiseAnd(t *testing.T) {
src := `<? $a & $b;` src := `<? $a & $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &binary.BitwiseAnd{ Expr: &binary.BitwiseAnd{
@ -55,7 +55,7 @@ func TestBitwiseAnd(t *testing.T) {
func TestBitwiseOr(t *testing.T) { func TestBitwiseOr(t *testing.T) {
src := `<? $a | $b;` src := `<? $a | $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &binary.BitwiseOr{ Expr: &binary.BitwiseOr{
@ -80,7 +80,7 @@ func TestBitwiseOr(t *testing.T) {
func TestBitwiseXor(t *testing.T) { func TestBitwiseXor(t *testing.T) {
src := `<? $a ^ $b;` src := `<? $a ^ $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &binary.BitwiseXor{ Expr: &binary.BitwiseXor{
@ -105,7 +105,7 @@ func TestBitwiseXor(t *testing.T) {
func TestBooleanAnd(t *testing.T) { func TestBooleanAnd(t *testing.T) {
src := `<? $a && $b;` src := `<? $a && $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &binary.BooleanAnd{ Expr: &binary.BooleanAnd{
@ -130,7 +130,7 @@ func TestBooleanAnd(t *testing.T) {
func TestBooleanOr(t *testing.T) { func TestBooleanOr(t *testing.T) {
src := `<? $a || $b;` src := `<? $a || $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &binary.BooleanOr{ Expr: &binary.BooleanOr{
@ -155,7 +155,7 @@ func TestBooleanOr(t *testing.T) {
func TestCoalesce(t *testing.T) { func TestCoalesce(t *testing.T) {
src := `<? $a ?? $b;` src := `<? $a ?? $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &binary.Coalesce{ Expr: &binary.Coalesce{
@ -175,7 +175,7 @@ func TestCoalesce(t *testing.T) {
func TestConcat(t *testing.T) { func TestConcat(t *testing.T) {
src := `<? $a . $b;` src := `<? $a . $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &binary.Concat{ Expr: &binary.Concat{
@ -200,7 +200,7 @@ func TestConcat(t *testing.T) {
func TestDiv(t *testing.T) { func TestDiv(t *testing.T) {
src := `<? $a / $b;` src := `<? $a / $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &binary.Div{ Expr: &binary.Div{
@ -225,7 +225,7 @@ func TestDiv(t *testing.T) {
func TestEqual(t *testing.T) { func TestEqual(t *testing.T) {
src := `<? $a == $b;` src := `<? $a == $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &binary.Equal{ Expr: &binary.Equal{
@ -250,7 +250,7 @@ func TestEqual(t *testing.T) {
func TestGreaterOrEqual(t *testing.T) { func TestGreaterOrEqual(t *testing.T) {
src := `<? $a >= $b;` src := `<? $a >= $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &binary.GreaterOrEqual{ Expr: &binary.GreaterOrEqual{
@ -275,7 +275,7 @@ func TestGreaterOrEqual(t *testing.T) {
func TestGreater(t *testing.T) { func TestGreater(t *testing.T) {
src := `<? $a > $b;` src := `<? $a > $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &binary.Greater{ Expr: &binary.Greater{
@ -300,7 +300,7 @@ func TestGreater(t *testing.T) {
func TestIdentical(t *testing.T) { func TestIdentical(t *testing.T) {
src := `<? $a === $b;` src := `<? $a === $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &binary.Identical{ Expr: &binary.Identical{
@ -325,7 +325,7 @@ func TestIdentical(t *testing.T) {
func TestLogicalAnd(t *testing.T) { func TestLogicalAnd(t *testing.T) {
src := `<? $a and $b;` src := `<? $a and $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &binary.LogicalAnd{ Expr: &binary.LogicalAnd{
@ -350,7 +350,7 @@ func TestLogicalAnd(t *testing.T) {
func TestLogicalOr(t *testing.T) { func TestLogicalOr(t *testing.T) {
src := `<? $a or $b;` src := `<? $a or $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &binary.LogicalOr{ Expr: &binary.LogicalOr{
@ -375,7 +375,7 @@ func TestLogicalOr(t *testing.T) {
func TestLogicalXor(t *testing.T) { func TestLogicalXor(t *testing.T) {
src := `<? $a xor $b;` src := `<? $a xor $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &binary.LogicalXor{ Expr: &binary.LogicalXor{
@ -400,7 +400,7 @@ func TestLogicalXor(t *testing.T) {
func TestMinus(t *testing.T) { func TestMinus(t *testing.T) {
src := `<? $a - $b;` src := `<? $a - $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &binary.Minus{ Expr: &binary.Minus{
@ -425,7 +425,7 @@ func TestMinus(t *testing.T) {
func TestMod(t *testing.T) { func TestMod(t *testing.T) {
src := `<? $a % $b;` src := `<? $a % $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &binary.Mod{ Expr: &binary.Mod{
@ -450,7 +450,7 @@ func TestMod(t *testing.T) {
func TestMul(t *testing.T) { func TestMul(t *testing.T) {
src := `<? $a * $b;` src := `<? $a * $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &binary.Mul{ Expr: &binary.Mul{
@ -475,7 +475,7 @@ func TestMul(t *testing.T) {
func TestNotEqual(t *testing.T) { func TestNotEqual(t *testing.T) {
src := `<? $a != $b;` src := `<? $a != $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &binary.NotEqual{ Expr: &binary.NotEqual{
@ -500,7 +500,7 @@ func TestNotEqual(t *testing.T) {
func TestNotIdentical(t *testing.T) { func TestNotIdentical(t *testing.T) {
src := `<? $a !== $b;` src := `<? $a !== $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &binary.NotIdentical{ Expr: &binary.NotIdentical{
@ -525,7 +525,7 @@ func TestNotIdentical(t *testing.T) {
func TestPlus(t *testing.T) { func TestPlus(t *testing.T) {
src := `<? $a + $b;` src := `<? $a + $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &binary.Plus{ Expr: &binary.Plus{
@ -550,7 +550,7 @@ func TestPlus(t *testing.T) {
func TestPow(t *testing.T) { func TestPow(t *testing.T) {
src := `<? $a ** $b;` src := `<? $a ** $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &binary.Pow{ Expr: &binary.Pow{
@ -575,7 +575,7 @@ func TestPow(t *testing.T) {
func TestShiftLeft(t *testing.T) { func TestShiftLeft(t *testing.T) {
src := `<? $a << $b;` src := `<? $a << $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &binary.ShiftLeft{ Expr: &binary.ShiftLeft{
@ -600,7 +600,7 @@ func TestShiftLeft(t *testing.T) {
func TestShiftRight(t *testing.T) { func TestShiftRight(t *testing.T) {
src := `<? $a >> $b;` src := `<? $a >> $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &binary.ShiftRight{ Expr: &binary.ShiftRight{
@ -625,7 +625,7 @@ func TestShiftRight(t *testing.T) {
func TestSmallerOrEqual(t *testing.T) { func TestSmallerOrEqual(t *testing.T) {
src := `<? $a <= $b;` src := `<? $a <= $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &binary.SmallerOrEqual{ Expr: &binary.SmallerOrEqual{
@ -650,7 +650,7 @@ func TestSmallerOrEqual(t *testing.T) {
func TestSmaller(t *testing.T) { func TestSmaller(t *testing.T) {
src := `<? $a < $b;` src := `<? $a < $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &binary.Smaller{ Expr: &binary.Smaller{
@ -675,7 +675,7 @@ func TestSmaller(t *testing.T) {
func TestSpaceship(t *testing.T) { func TestSpaceship(t *testing.T) {
src := `<? $a <=> $b;` src := `<? $a <=> $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &binary.Spaceship{ Expr: &binary.Spaceship{

View File

@ -30,7 +30,7 @@ func assertEqual(t *testing.T, expected interface{}, actual interface{}) {
func TestArray(t *testing.T) { func TestArray(t *testing.T) {
src := `<? (array)$a;` src := `<? (array)$a;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &cast.Array{ Expr: &cast.Array{
@ -54,7 +54,7 @@ func TestArray(t *testing.T) {
func TestBool(t *testing.T) { func TestBool(t *testing.T) {
src := `<? (boolean)$a;` src := `<? (boolean)$a;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &cast.Bool{ Expr: &cast.Bool{
@ -78,7 +78,7 @@ func TestBool(t *testing.T) {
func TestBoolShort(t *testing.T) { func TestBoolShort(t *testing.T) {
src := `<? (bool)$a;` src := `<? (bool)$a;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &cast.Bool{ Expr: &cast.Bool{
@ -102,7 +102,7 @@ func TestBoolShort(t *testing.T) {
func TestDouble(t *testing.T) { func TestDouble(t *testing.T) {
src := `<? (double)$a;` src := `<? (double)$a;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &cast.Double{ Expr: &cast.Double{
@ -126,7 +126,7 @@ func TestDouble(t *testing.T) {
func TestCastFloat(t *testing.T) { func TestCastFloat(t *testing.T) {
src := `<? (float)$a;` src := `<? (float)$a;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &cast.Double{ Expr: &cast.Double{
@ -150,7 +150,7 @@ func TestCastFloat(t *testing.T) {
func TestInt(t *testing.T) { func TestInt(t *testing.T) {
src := `<? (integer)$a;` src := `<? (integer)$a;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &cast.Int{ Expr: &cast.Int{
@ -174,7 +174,7 @@ func TestInt(t *testing.T) {
func TestIntShort(t *testing.T) { func TestIntShort(t *testing.T) {
src := `<? (int)$a;` src := `<? (int)$a;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &cast.Int{ Expr: &cast.Int{
@ -198,7 +198,7 @@ func TestIntShort(t *testing.T) {
func TestObject(t *testing.T) { func TestObject(t *testing.T) {
src := `<? (object)$a;` src := `<? (object)$a;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &cast.Object{ Expr: &cast.Object{
@ -222,7 +222,7 @@ func TestObject(t *testing.T) {
func TestString(t *testing.T) { func TestString(t *testing.T) {
src := `<? (string)$a;` src := `<? (string)$a;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &cast.String{ Expr: &cast.String{
@ -246,7 +246,7 @@ func TestString(t *testing.T) {
func TestUnset(t *testing.T) { func TestUnset(t *testing.T) {
src := `<? (unset)$a;` src := `<? (unset)$a;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &cast.Unset{ Expr: &cast.Unset{

View File

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

View File

@ -11,19 +11,19 @@ type Closure struct {
Static bool Static bool
PhpDocComment string PhpDocComment string
Params []node.Node Params []node.Node
Uses []node.Node ClosureUse *ClosureUse
ReturnType node.Node ReturnType node.Node
Stmts []node.Node Stmts []node.Node
} }
// NewClosure node constructor // NewClosure node constructor
func NewClosure(Params []node.Node, Uses []node.Node, ReturnType node.Node, Stmts []node.Node, Static bool, ReturnsRef bool, PhpDocComment string) *Closure { func NewClosure(Params []node.Node, ClosureUse *ClosureUse, ReturnType node.Node, Stmts []node.Node, Static bool, ReturnsRef bool, PhpDocComment string) *Closure {
return &Closure{ return &Closure{
ReturnsRef, ReturnsRef,
Static, Static,
PhpDocComment, PhpDocComment,
Params, Params,
Uses, ClosureUse,
ReturnType, ReturnType,
Stmts, Stmts,
} }
@ -54,13 +54,9 @@ func (n *Closure) Walk(v walker.Visitor) {
} }
} }
if n.Uses != nil { if n.ClosureUse != nil {
vv := v.GetChildrenVisitor("Uses") vv := v.GetChildrenVisitor("ClosureUse")
for _, nn := range n.Uses { n.ClosureUse.Walk(vv)
if nn != nil {
nn.Walk(vv)
}
}
} }
if n.ReturnType != nil { if n.ReturnType != nil {

View File

@ -7,23 +7,19 @@ import (
// ClosureUse node // ClosureUse node
type ClosureUse struct { type ClosureUse struct {
ByRef bool Uses []node.Node
Variable node.Node
} }
// NewClosureUse node constructor // NewClosureUse node constructor
func NewClosureUse(Variable node.Node, ByRef bool) *ClosureUse { func NewClosureUse(Uses []node.Node) *ClosureUse {
return &ClosureUse{ return &ClosureUse{
ByRef, Uses,
Variable,
} }
} }
// Attributes returns node attributes as map // Attributes returns node attributes as map
func (n *ClosureUse) Attributes() map[string]interface{} { func (n *ClosureUse) Attributes() map[string]interface{} {
return map[string]interface{}{ return nil
"ByRef": n.ByRef,
}
} }
// Walk traverses nodes // Walk traverses nodes
@ -33,9 +29,13 @@ func (n *ClosureUse) Walk(v walker.Visitor) {
return return
} }
if n.Variable != nil { if n.Uses != nil {
vv := v.GetChildrenVisitor("Variable") vv := v.GetChildrenVisitor("Uses")
n.Variable.Walk(vv) for _, nn := range n.Uses {
if nn != nil {
nn.Walk(vv)
}
}
} }
v.LeaveNode(n) v.LeaveNode(n)

View File

@ -8,14 +8,14 @@ import (
// FunctionCall node // FunctionCall node
type FunctionCall struct { type FunctionCall struct {
Function node.Node Function node.Node
Arguments []node.Node ArgumentList *node.ArgumentList
} }
// NewFunctionCall node constructor // NewFunctionCall node constructor
func NewFunctionCall(Function node.Node, Arguments []node.Node) *FunctionCall { func NewFunctionCall(Function node.Node, ArgumentList *node.ArgumentList) *FunctionCall {
return &FunctionCall{ return &FunctionCall{
Function, Function,
Arguments, ArgumentList,
} }
} }
@ -36,13 +36,9 @@ func (n *FunctionCall) Walk(v walker.Visitor) {
n.Function.Walk(vv) n.Function.Walk(vv)
} }
if n.Arguments != nil { if n.ArgumentList != nil {
vv := v.GetChildrenVisitor("Arguments") vv := v.GetChildrenVisitor("ArgumentList")
for _, nn := range n.Arguments { n.ArgumentList.Walk(vv)
if nn != nil {
nn.Walk(vv)
}
}
} }
v.LeaveNode(n) v.LeaveNode(n)

View File

@ -9,15 +9,15 @@ import (
type MethodCall struct { type MethodCall struct {
Variable node.Node Variable node.Node
Method node.Node Method node.Node
Arguments []node.Node ArgumentList *node.ArgumentList
} }
// NewMethodCall node constructor // NewMethodCall node constructor
func NewMethodCall(Variable node.Node, Method node.Node, Arguments []node.Node) *MethodCall { func NewMethodCall(Variable node.Node, Method node.Node, ArgumentList *node.ArgumentList) *MethodCall {
return &MethodCall{ return &MethodCall{
Variable, Variable,
Method, Method,
Arguments, ArgumentList,
} }
} }
@ -43,13 +43,9 @@ func (n *MethodCall) Walk(v walker.Visitor) {
n.Method.Walk(vv) n.Method.Walk(vv)
} }
if n.Arguments != nil { if n.ArgumentList != nil {
vv := v.GetChildrenVisitor("Arguments") vv := v.GetChildrenVisitor("ArgumentList")
for _, nn := range n.Arguments { n.ArgumentList.Walk(vv)
if nn != nil {
nn.Walk(vv)
}
}
} }
v.LeaveNode(n) v.LeaveNode(n)

View File

@ -8,14 +8,14 @@ import (
// New node // New node
type New struct { type New struct {
Class node.Node Class node.Node
Arguments []node.Node ArgumentList *node.ArgumentList
} }
// NewNew node constructor // NewNew node constructor
func NewNew(Class node.Node, Arguments []node.Node) *New { func NewNew(Class node.Node, ArgumentList *node.ArgumentList) *New {
return &New{ return &New{
Class, Class,
Arguments, ArgumentList,
} }
} }
@ -36,13 +36,9 @@ func (n *New) Walk(v walker.Visitor) {
n.Class.Walk(vv) n.Class.Walk(vv)
} }
if n.Arguments != nil { if n.ArgumentList != nil {
vv := v.GetChildrenVisitor("Arguments") vv := v.GetChildrenVisitor("ArgumentList")
for _, nn := range n.Arguments { n.ArgumentList.Walk(vv)
if nn != nil {
nn.Walk(vv)
}
}
} }
v.LeaveNode(n) v.LeaveNode(n)

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

@ -9,15 +9,15 @@ import (
type StaticCall struct { type StaticCall struct {
Class node.Node Class node.Node
Call node.Node Call node.Node
Arguments []node.Node ArgumentList *node.ArgumentList
} }
// NewStaticCall node constructor // NewStaticCall node constructor
func NewStaticCall(Class node.Node, Call node.Node, Arguments []node.Node) *StaticCall { func NewStaticCall(Class node.Node, Call node.Node, ArgumentList *node.ArgumentList) *StaticCall {
return &StaticCall{ return &StaticCall{
Class, Class,
Call, Call,
Arguments, ArgumentList,
} }
} }
@ -43,13 +43,9 @@ func (n *StaticCall) Walk(v walker.Visitor) {
n.Call.Walk(vv) n.Call.Walk(vv)
} }
if n.Arguments != nil { if n.ArgumentList != nil {
vv := v.GetChildrenVisitor("Arguments") vv := v.GetChildrenVisitor("ArgumentList")
for _, nn := range n.Arguments { n.ArgumentList.Walk(vv)
if nn != nil {
nn.Walk(vv)
}
}
} }
v.LeaveNode(n) v.LeaveNode(n)

View File

@ -31,7 +31,7 @@ func assertEqual(t *testing.T, expected interface{}, actual interface{}) {
func TestArrayDimFetch(t *testing.T) { func TestArrayDimFetch(t *testing.T) {
src := `<? $a[1];` src := `<? $a[1];`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.ArrayDimFetch{ Expr: &expr.ArrayDimFetch{
@ -56,7 +56,7 @@ func TestArrayDimFetch(t *testing.T) {
func TestArrayDimFetchNested(t *testing.T) { func TestArrayDimFetchNested(t *testing.T) {
src := `<? $a[1][2];` src := `<? $a[1][2];`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.ArrayDimFetch{ Expr: &expr.ArrayDimFetch{

View File

@ -17,7 +17,7 @@ import (
func TestArray(t *testing.T) { func TestArray(t *testing.T) {
src := `<? array();` src := `<? array();`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.Array{ Expr: &expr.Array{
@ -41,13 +41,12 @@ func TestArray(t *testing.T) {
func TestArrayItem(t *testing.T) { func TestArrayItem(t *testing.T) {
src := `<? array(1);` src := `<? array(1);`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.Array{ Expr: &expr.Array{
Items: []node.Node{ Items: []node.Node{
&expr.ArrayItem{ &expr.ArrayItem{
ByRef: false,
Val: &scalar.Lnumber{Value: "1"}, Val: &scalar.Lnumber{Value: "1"},
}, },
}, },
@ -70,19 +69,17 @@ func TestArrayItem(t *testing.T) {
func TestArrayItems(t *testing.T) { func TestArrayItems(t *testing.T) {
src := `<? array(1=>1, &$b,);` src := `<? array(1=>1, &$b,);`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.Array{ Expr: &expr.Array{
Items: []node.Node{ Items: []node.Node{
&expr.ArrayItem{ &expr.ArrayItem{
ByRef: false,
Key: &scalar.Lnumber{Value: "1"}, Key: &scalar.Lnumber{Value: "1"},
Val: &scalar.Lnumber{Value: "1"}, Val: &scalar.Lnumber{Value: "1"},
}, },
&expr.ArrayItem{ &expr.ArrayItem{
ByRef: true, Val: &expr.Reference{Variable: &expr.Variable{VarName: &node.Identifier{Value: "b"}}},
Val: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
}, },
}, },
}, },

View File

@ -15,7 +15,7 @@ import (
func TestBitwiseNot(t *testing.T) { func TestBitwiseNot(t *testing.T) {
src := `<? ~$a;` src := `<? ~$a;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.BitwiseNot{ Expr: &expr.BitwiseNot{

View File

@ -15,7 +15,7 @@ import (
func TestBooleanNot(t *testing.T) { func TestBooleanNot(t *testing.T) {
src := `<? !$a;` src := `<? !$a;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.BooleanNot{ Expr: &expr.BooleanNot{

View File

@ -17,7 +17,7 @@ import (
func TestClassConstFetch(t *testing.T) { func TestClassConstFetch(t *testing.T) {
src := `<? Foo::Bar;` src := `<? Foo::Bar;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.ClassConstFetch{ Expr: &expr.ClassConstFetch{
@ -46,7 +46,7 @@ func TestClassConstFetch(t *testing.T) {
func TestStaticClassConstFetch(t *testing.T) { func TestStaticClassConstFetch(t *testing.T) {
src := `<? static::bar;` src := `<? static::bar;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.ClassConstFetch{ Expr: &expr.ClassConstFetch{

View File

@ -15,7 +15,7 @@ import (
func TestCloneBrackets(t *testing.T) { func TestCloneBrackets(t *testing.T) {
src := `<? clone($a);` src := `<? clone($a);`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.Clone{ Expr: &expr.Clone{
@ -39,7 +39,7 @@ func TestCloneBrackets(t *testing.T) {
func TestClone(t *testing.T) { func TestClone(t *testing.T) {
src := `<? clone $a;` src := `<? clone $a;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.Clone{ Expr: &expr.Clone{

View File

@ -17,14 +17,13 @@ import (
func TestClosure(t *testing.T) { func TestClosure(t *testing.T) {
src := `<? function(){};` src := `<? function(){};`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.Closure{ Expr: &expr.Closure{
ReturnsRef: false, ReturnsRef: false,
Static: false, Static: false,
PhpDocComment: "", PhpDocComment: "",
Uses: []node.Node{},
Stmts: []node.Node{}, Stmts: []node.Node{},
}, },
}, },
@ -45,7 +44,7 @@ func TestClosure(t *testing.T) {
func TestClosureUse(t *testing.T) { func TestClosureUse(t *testing.T) {
src := `<? function($a, $b) use ($c, &$d) {};` src := `<? function($a, $b) use ($c, &$d) {};`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.Closure{ Expr: &expr.Closure{
@ -64,14 +63,10 @@ func TestClosureUse(t *testing.T) {
Variable: &expr.Variable{VarName: &node.Identifier{Value: "b"}}, Variable: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
}, },
}, },
ClosureUse: &expr.ClosureUse{
Uses: []node.Node{ Uses: []node.Node{
&expr.ClosureUse{ &expr.Variable{VarName: &node.Identifier{Value: "c"}},
ByRef: false, &expr.Reference{Variable: &expr.Variable{VarName: &node.Identifier{Value: "d"}}},
Variable: &expr.Variable{VarName: &node.Identifier{Value: "c"}},
},
&expr.ClosureUse{
ByRef: true,
Variable: &expr.Variable{VarName: &node.Identifier{Value: "d"}},
}, },
}, },
Stmts: []node.Node{}, Stmts: []node.Node{},
@ -94,7 +89,7 @@ func TestClosureUse(t *testing.T) {
func TestClosureUse2(t *testing.T) { func TestClosureUse2(t *testing.T) {
src := `<? function($a, $b) use (&$c, $d) {};` src := `<? function($a, $b) use (&$c, $d) {};`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.Closure{ Expr: &expr.Closure{
@ -113,14 +108,10 @@ func TestClosureUse2(t *testing.T) {
Variable: &expr.Variable{VarName: &node.Identifier{Value: "b"}}, Variable: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
}, },
}, },
ClosureUse: &expr.ClosureUse{
Uses: []node.Node{ Uses: []node.Node{
&expr.ClosureUse{ &expr.Reference{Variable: &expr.Variable{VarName: &node.Identifier{Value: "c"}}},
ByRef: true, &expr.Variable{VarName: &node.Identifier{Value: "d"}},
Variable: &expr.Variable{VarName: &node.Identifier{Value: "c"}},
},
&expr.ClosureUse{
ByRef: false,
Variable: &expr.Variable{VarName: &node.Identifier{Value: "d"}},
}, },
}, },
Stmts: []node.Node{}, Stmts: []node.Node{},
@ -143,14 +134,13 @@ func TestClosureUse2(t *testing.T) {
func TestClosureReturnType(t *testing.T) { func TestClosureReturnType(t *testing.T) {
src := `<? function(): void {};` src := `<? function(): void {};`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.Closure{ Expr: &expr.Closure{
ReturnsRef: false, ReturnsRef: false,
Static: false, Static: false,
PhpDocComment: "", PhpDocComment: "",
Uses: []node.Node{},
ReturnType: &name.Name{ ReturnType: &name.Name{
Parts: []node.Node{&name.NamePart{Value: "void"}}, Parts: []node.Node{&name.NamePart{Value: "void"}},
}, },

View File

@ -17,7 +17,7 @@ import (
func TestConstFetch(t *testing.T) { func TestConstFetch(t *testing.T) {
src := `<? foo;` src := `<? foo;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.ConstFetch{ Expr: &expr.ConstFetch{
@ -41,7 +41,7 @@ func TestConstFetch(t *testing.T) {
func TestConstFetchRelative(t *testing.T) { func TestConstFetchRelative(t *testing.T) {
src := `<? namespace\foo;` src := `<? namespace\foo;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.ConstFetch{ Expr: &expr.ConstFetch{
@ -65,7 +65,7 @@ func TestConstFetchRelative(t *testing.T) {
func TestConstFetchFullyQualified(t *testing.T) { func TestConstFetchFullyQualified(t *testing.T) {
src := `<? \foo;` src := `<? \foo;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.ConstFetch{ Expr: &expr.ConstFetch{

View File

@ -15,7 +15,7 @@ import (
func TestEmpty(t *testing.T) { func TestEmpty(t *testing.T) {
src := `<? empty($a);` src := `<? empty($a);`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.Empty{ Expr: &expr.Empty{

View File

@ -15,7 +15,7 @@ import (
func TestErrorSuppress(t *testing.T) { func TestErrorSuppress(t *testing.T) {
src := `<? @$a;` src := `<? @$a;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.ErrorSuppress{ Expr: &expr.ErrorSuppress{

View File

@ -15,7 +15,7 @@ import (
func TestEval(t *testing.T) { func TestEval(t *testing.T) {
src := `<? eval($a);` src := `<? eval($a);`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.Eval{ Expr: &expr.Eval{

View File

@ -15,7 +15,7 @@ import (
func TestExit(t *testing.T) { func TestExit(t *testing.T) {
src := `<? exit;` src := `<? exit;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.Exit{}, Expr: &expr.Exit{},
@ -37,7 +37,7 @@ func TestExit(t *testing.T) {
func TestExitExpr(t *testing.T) { func TestExitExpr(t *testing.T) {
src := `<? exit($a);` src := `<? exit($a);`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.Exit{ Expr: &expr.Exit{
@ -61,7 +61,7 @@ func TestExitExpr(t *testing.T) {
func TestDie(t *testing.T) { func TestDie(t *testing.T) {
src := `<? die;` src := `<? die;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.Die{}, Expr: &expr.Die{},
@ -83,7 +83,7 @@ func TestDie(t *testing.T) {
func TestDieExpr(t *testing.T) { func TestDieExpr(t *testing.T) {
src := `<? die($a);` src := `<? die($a);`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.Die{ Expr: &expr.Die{

View File

@ -20,7 +20,7 @@ import (
func TestFunctionCall(t *testing.T) { func TestFunctionCall(t *testing.T) {
src := `<? foo();` src := `<? foo();`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.FunctionCall{ Expr: &expr.FunctionCall{
@ -29,7 +29,7 @@ func TestFunctionCall(t *testing.T) {
&name.NamePart{Value: "foo"}, &name.NamePart{Value: "foo"},
}, },
}, },
Arguments: []node.Node{}, ArgumentList: &node.ArgumentList{},
}, },
}, },
}, },
@ -49,7 +49,7 @@ func TestFunctionCall(t *testing.T) {
func TestFunctionCallRelative(t *testing.T) { func TestFunctionCallRelative(t *testing.T) {
src := `<? namespace\foo();` src := `<? namespace\foo();`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.FunctionCall{ Expr: &expr.FunctionCall{
@ -58,7 +58,7 @@ func TestFunctionCallRelative(t *testing.T) {
&name.NamePart{Value: "foo"}, &name.NamePart{Value: "foo"},
}, },
}, },
Arguments: []node.Node{}, ArgumentList: &node.ArgumentList{},
}, },
}, },
}, },
@ -78,7 +78,7 @@ func TestFunctionCallRelative(t *testing.T) {
func TestFunctionFullyQualified(t *testing.T) { func TestFunctionFullyQualified(t *testing.T) {
src := `<? \foo([]);` src := `<? \foo([]);`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.FunctionCall{ Expr: &expr.FunctionCall{
@ -87,6 +87,7 @@ func TestFunctionFullyQualified(t *testing.T) {
&name.NamePart{Value: "foo"}, &name.NamePart{Value: "foo"},
}, },
}, },
ArgumentList: &node.ArgumentList{
Arguments: []node.Node{ Arguments: []node.Node{
&node.Argument{ &node.Argument{
Variadic: false, Variadic: false,
@ -99,6 +100,7 @@ func TestFunctionFullyQualified(t *testing.T) {
}, },
}, },
}, },
},
} }
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php") php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
@ -115,11 +117,12 @@ func TestFunctionFullyQualified(t *testing.T) {
func TestFunctionCallVar(t *testing.T) { func TestFunctionCallVar(t *testing.T) {
src := `<? $foo(yield $a);` src := `<? $foo(yield $a);`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.FunctionCall{ Expr: &expr.FunctionCall{
Function: &expr.Variable{VarName: &node.Identifier{Value: "foo"}}, Function: &expr.Variable{VarName: &node.Identifier{Value: "foo"}},
ArgumentList: &node.ArgumentList{
Arguments: []node.Node{ Arguments: []node.Node{
&node.Argument{ &node.Argument{
Variadic: false, Variadic: false,
@ -132,6 +135,7 @@ func TestFunctionCallVar(t *testing.T) {
}, },
}, },
}, },
},
} }
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php") php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
@ -148,7 +152,7 @@ func TestFunctionCallVar(t *testing.T) {
func TestFunctionCallExprArg(t *testing.T) { func TestFunctionCallExprArg(t *testing.T) {
src := `<? ceil($foo/3);` src := `<? ceil($foo/3);`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.FunctionCall{ Expr: &expr.FunctionCall{
@ -157,6 +161,7 @@ func TestFunctionCallExprArg(t *testing.T) {
&name.NamePart{Value: "ceil"}, &name.NamePart{Value: "ceil"},
}, },
}, },
ArgumentList: &node.ArgumentList{
Arguments: []node.Node{ Arguments: []node.Node{
&node.Argument{ &node.Argument{
Variadic: false, Variadic: false,
@ -170,6 +175,7 @@ func TestFunctionCallExprArg(t *testing.T) {
}, },
}, },
}, },
},
} }
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php") php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")

View File

@ -15,7 +15,7 @@ import (
func TestPostDec(t *testing.T) { func TestPostDec(t *testing.T) {
src := `<? $a--;` src := `<? $a--;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.PostDec{ Expr: &expr.PostDec{
@ -39,7 +39,7 @@ func TestPostDec(t *testing.T) {
func TestPostInc(t *testing.T) { func TestPostInc(t *testing.T) {
src := `<? $a++;` src := `<? $a++;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.PostInc{ Expr: &expr.PostInc{
@ -63,7 +63,7 @@ func TestPostInc(t *testing.T) {
func TestPreDec(t *testing.T) { func TestPreDec(t *testing.T) {
src := `<? --$a;` src := `<? --$a;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.PreDec{ Expr: &expr.PreDec{
@ -87,7 +87,7 @@ func TestPreDec(t *testing.T) {
func TestPreInc(t *testing.T) { func TestPreInc(t *testing.T) {
src := `<? ++$a;` src := `<? ++$a;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.PreInc{ Expr: &expr.PreInc{

View File

@ -15,7 +15,7 @@ import (
func TestInclude(t *testing.T) { func TestInclude(t *testing.T) {
src := `<? include $a;` src := `<? include $a;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.Include{ Expr: &expr.Include{
@ -39,7 +39,7 @@ func TestInclude(t *testing.T) {
func TestIncludeOnce(t *testing.T) { func TestIncludeOnce(t *testing.T) {
src := `<? include_once $a;` src := `<? include_once $a;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.IncludeOnce{ Expr: &expr.IncludeOnce{
@ -63,7 +63,7 @@ func TestIncludeOnce(t *testing.T) {
func TestRequire(t *testing.T) { func TestRequire(t *testing.T) {
src := `<? require $a;` src := `<? require $a;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.Require{ Expr: &expr.Require{
@ -87,7 +87,7 @@ func TestRequire(t *testing.T) {
func TestRequireOnce(t *testing.T) { func TestRequireOnce(t *testing.T) {
src := `<? require_once $a;` src := `<? require_once $a;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.RequireOnce{ Expr: &expr.RequireOnce{

View File

@ -17,7 +17,7 @@ import (
func TestInstanceOf(t *testing.T) { func TestInstanceOf(t *testing.T) {
src := `<? $a instanceof Foo;` src := `<? $a instanceof Foo;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.InstanceOf{ Expr: &expr.InstanceOf{
@ -46,7 +46,7 @@ func TestInstanceOf(t *testing.T) {
func TestInstanceOfRelative(t *testing.T) { func TestInstanceOfRelative(t *testing.T) {
src := `<? $a instanceof namespace\Foo;` src := `<? $a instanceof namespace\Foo;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.InstanceOf{ Expr: &expr.InstanceOf{
@ -75,7 +75,7 @@ func TestInstanceOfRelative(t *testing.T) {
func TestInstanceOfFullyQualified(t *testing.T) { func TestInstanceOfFullyQualified(t *testing.T) {
src := `<? $a instanceof \Foo;` src := `<? $a instanceof \Foo;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.InstanceOf{ Expr: &expr.InstanceOf{

View File

@ -15,7 +15,7 @@ import (
func TestIsset(t *testing.T) { func TestIsset(t *testing.T) {
src := `<? isset($a);` src := `<? isset($a);`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.Isset{ Expr: &expr.Isset{
@ -41,7 +41,7 @@ func TestIsset(t *testing.T) {
func TestIssetVariables(t *testing.T) { func TestIssetVariables(t *testing.T) {
src := `<? isset($a, $b);` src := `<? isset($a, $b);`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.Isset{ Expr: &expr.Isset{

View File

@ -17,7 +17,7 @@ import (
func TestEmptyList(t *testing.T) { func TestEmptyList(t *testing.T) {
src := `<? list() = $b;` src := `<? list() = $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &assign.Assign{ Expr: &assign.Assign{
@ -44,14 +44,13 @@ func TestEmptyList(t *testing.T) {
func TestList(t *testing.T) { func TestList(t *testing.T) {
src := `<? list($a) = $b;` src := `<? list($a) = $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &assign.Assign{ Expr: &assign.Assign{
Variable: &expr.List{ Variable: &expr.List{
Items: []node.Node{ Items: []node.Node{
&expr.ArrayItem{ &expr.ArrayItem{
ByRef: false,
Val: &expr.Variable{VarName: &node.Identifier{Value: "a"}}, Val: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
}, },
}, },
@ -76,14 +75,13 @@ func TestList(t *testing.T) {
func TestListArrayIndex(t *testing.T) { func TestListArrayIndex(t *testing.T) {
src := `<? list($a[]) = $b;` src := `<? list($a[]) = $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &assign.Assign{ Expr: &assign.Assign{
Variable: &expr.List{ Variable: &expr.List{
Items: []node.Node{ Items: []node.Node{
&expr.ArrayItem{ &expr.ArrayItem{
ByRef: false,
Val: &expr.ArrayDimFetch{ Val: &expr.ArrayDimFetch{
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}}, Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
}, },
@ -110,18 +108,16 @@ func TestListArrayIndex(t *testing.T) {
func TestListList(t *testing.T) { func TestListList(t *testing.T) {
src := `<? list(list($a)) = $b;` src := `<? list(list($a)) = $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &assign.Assign{ Expr: &assign.Assign{
Variable: &expr.List{ Variable: &expr.List{
Items: []node.Node{ Items: []node.Node{
&expr.ArrayItem{ &expr.ArrayItem{
ByRef: false,
Val: &expr.List{ Val: &expr.List{
Items: []node.Node{ Items: []node.Node{
&expr.ArrayItem{ &expr.ArrayItem{
ByRef: false,
Val: &expr.Variable{VarName: &node.Identifier{Value: "a"}}, Val: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
}, },
}, },

View File

@ -15,13 +15,13 @@ import (
func TestMethodCall(t *testing.T) { func TestMethodCall(t *testing.T) {
src := `<? $a->foo();` src := `<? $a->foo();`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.MethodCall{ Expr: &expr.MethodCall{
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}}, Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
Method: &node.Identifier{Value: "foo"}, Method: &node.Identifier{Value: "foo"},
Arguments: []node.Node{}, ArgumentList: &node.ArgumentList{},
}, },
}, },
}, },

View File

@ -17,7 +17,7 @@ import (
func TestNew(t *testing.T) { func TestNew(t *testing.T) {
src := `<? new Foo;` src := `<? new Foo;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.New{ Expr: &expr.New{
@ -45,7 +45,7 @@ func TestNew(t *testing.T) {
func TestNewRelative(t *testing.T) { func TestNewRelative(t *testing.T) {
src := `<? new namespace\Foo();` src := `<? new namespace\Foo();`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.New{ Expr: &expr.New{
@ -54,7 +54,7 @@ func TestNewRelative(t *testing.T) {
&name.NamePart{Value: "Foo"}, &name.NamePart{Value: "Foo"},
}, },
}, },
Arguments: []node.Node{}, ArgumentList: &node.ArgumentList{},
}, },
}, },
}, },
@ -74,7 +74,7 @@ func TestNewRelative(t *testing.T) {
func TestNewFullyQualified(t *testing.T) { func TestNewFullyQualified(t *testing.T) {
src := `<? new \Foo();` src := `<? new \Foo();`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.New{ Expr: &expr.New{
@ -83,7 +83,7 @@ func TestNewFullyQualified(t *testing.T) {
&name.NamePart{Value: "Foo"}, &name.NamePart{Value: "Foo"},
}, },
}, },
Arguments: []node.Node{}, ArgumentList: &node.ArgumentList{},
}, },
}, },
}, },
@ -103,16 +103,18 @@ func TestNewFullyQualified(t *testing.T) {
func TestNewAnonymous(t *testing.T) { func TestNewAnonymous(t *testing.T) {
src := `<? new class ($a, ...$b) {};` src := `<? new class ($a, ...$b) {};`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.New{ Expr: &expr.New{
Class: &stmt.Class{ Class: &stmt.Class{
PhpDocComment: "", PhpDocComment: "",
Args: []node.Node{ ArgumentList: &node.ArgumentList{
Arguments: []node.Node{
&node.Argument{Variadic: false, Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}}, &node.Argument{Variadic: false, Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
&node.Argument{Variadic: true, Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}}}, &node.Argument{Variadic: true, Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}}},
}, },
},
Stmts: []node.Node{}, Stmts: []node.Node{},
}, },
}, },

View File

@ -15,7 +15,7 @@ import (
func TestPrint(t *testing.T) { func TestPrint(t *testing.T) {
src := `<? print($a);` src := `<? print($a);`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.Print{ Expr: &expr.Print{

View File

@ -15,7 +15,7 @@ import (
func TestPropertyFetch(t *testing.T) { func TestPropertyFetch(t *testing.T) {
src := `<? $a->foo;` src := `<? $a->foo;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.PropertyFetch{ Expr: &expr.PropertyFetch{

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

@ -17,7 +17,7 @@ import (
func TestShellExec(t *testing.T) { func TestShellExec(t *testing.T) {
src := "<? `cmd $a`;" src := "<? `cmd $a`;"
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.ShellExec{ Expr: &expr.ShellExec{

View File

@ -17,7 +17,7 @@ import (
func TestShortArray(t *testing.T) { func TestShortArray(t *testing.T) {
src := `<? [];` src := `<? [];`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.ShortArray{ Expr: &expr.ShortArray{
@ -41,13 +41,12 @@ func TestShortArray(t *testing.T) {
func TestShortArrayItem(t *testing.T) { func TestShortArrayItem(t *testing.T) {
src := `<? [1];` src := `<? [1];`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.ShortArray{ Expr: &expr.ShortArray{
Items: []node.Node{ Items: []node.Node{
&expr.ArrayItem{ &expr.ArrayItem{
ByRef: false,
Val: &scalar.Lnumber{Value: "1"}, Val: &scalar.Lnumber{Value: "1"},
}, },
}, },
@ -70,19 +69,17 @@ func TestShortArrayItem(t *testing.T) {
func TestShortArrayItems(t *testing.T) { func TestShortArrayItems(t *testing.T) {
src := `<? [1=>1, &$b,];` src := `<? [1=>1, &$b,];`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.ShortArray{ Expr: &expr.ShortArray{
Items: []node.Node{ Items: []node.Node{
&expr.ArrayItem{ &expr.ArrayItem{
ByRef: false,
Key: &scalar.Lnumber{Value: "1"}, Key: &scalar.Lnumber{Value: "1"},
Val: &scalar.Lnumber{Value: "1"}, Val: &scalar.Lnumber{Value: "1"},
}, },
&expr.ArrayItem{ &expr.ArrayItem{
ByRef: true, Val: &expr.Reference{Variable: &expr.Variable{VarName: &node.Identifier{Value: "b"}}},
Val: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
}, },
}, },
}, },

View File

@ -16,14 +16,13 @@ import (
func TestShortList(t *testing.T) { func TestShortList(t *testing.T) {
src := `<? [$a] = $b;` src := `<? [$a] = $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &assign.Assign{ Expr: &assign.Assign{
Variable: &expr.ShortList{ Variable: &expr.ShortList{
Items: []node.Node{ Items: []node.Node{
&expr.ArrayItem{ &expr.ArrayItem{
ByRef: false,
Val: &expr.Variable{VarName: &node.Identifier{Value: "a"}}, Val: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
}, },
}, },
@ -43,14 +42,13 @@ func TestShortList(t *testing.T) {
func TestShortListArrayIndex(t *testing.T) { func TestShortListArrayIndex(t *testing.T) {
src := `<? [$a[]] = $b;` src := `<? [$a[]] = $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &assign.Assign{ Expr: &assign.Assign{
Variable: &expr.ShortList{ Variable: &expr.ShortList{
Items: []node.Node{ Items: []node.Node{
&expr.ArrayItem{ &expr.ArrayItem{
ByRef: false,
Val: &expr.ArrayDimFetch{ Val: &expr.ArrayDimFetch{
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}}, Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
}, },
@ -72,18 +70,16 @@ func TestShortListArrayIndex(t *testing.T) {
func TestShortListList(t *testing.T) { func TestShortListList(t *testing.T) {
src := `<? [list($a)] = $b;` src := `<? [list($a)] = $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &assign.Assign{ Expr: &assign.Assign{
Variable: &expr.ShortList{ Variable: &expr.ShortList{
Items: []node.Node{ Items: []node.Node{
&expr.ArrayItem{ &expr.ArrayItem{
ByRef: false,
Val: &expr.List{ Val: &expr.List{
Items: []node.Node{ Items: []node.Node{
&expr.ArrayItem{ &expr.ArrayItem{
ByRef: false,
Val: &expr.Variable{VarName: &node.Identifier{Value: "a"}}, Val: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
}, },
}, },

View File

@ -17,7 +17,7 @@ import (
func TestStaticCall(t *testing.T) { func TestStaticCall(t *testing.T) {
src := `<? Foo::bar();` src := `<? Foo::bar();`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.StaticCall{ Expr: &expr.StaticCall{
@ -27,7 +27,7 @@ func TestStaticCall(t *testing.T) {
}, },
}, },
Call: &node.Identifier{Value: "bar"}, Call: &node.Identifier{Value: "bar"},
Arguments: []node.Node{}, ArgumentList: &node.ArgumentList{},
}, },
}, },
}, },
@ -47,7 +47,7 @@ func TestStaticCall(t *testing.T) {
func TestStaticCallRelative(t *testing.T) { func TestStaticCallRelative(t *testing.T) {
src := `<? namespace\Foo::bar();` src := `<? namespace\Foo::bar();`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.StaticCall{ Expr: &expr.StaticCall{
@ -57,7 +57,7 @@ func TestStaticCallRelative(t *testing.T) {
}, },
}, },
Call: &node.Identifier{Value: "bar"}, Call: &node.Identifier{Value: "bar"},
Arguments: []node.Node{}, ArgumentList: &node.ArgumentList{},
}, },
}, },
}, },
@ -77,7 +77,7 @@ func TestStaticCallRelative(t *testing.T) {
func TestStaticCallFullyQualified(t *testing.T) { func TestStaticCallFullyQualified(t *testing.T) {
src := `<? \Foo::bar();` src := `<? \Foo::bar();`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.StaticCall{ Expr: &expr.StaticCall{
@ -87,7 +87,7 @@ func TestStaticCallFullyQualified(t *testing.T) {
}, },
}, },
Call: &node.Identifier{Value: "bar"}, Call: &node.Identifier{Value: "bar"},
Arguments: []node.Node{}, ArgumentList: &node.ArgumentList{},
}, },
}, },
}, },
@ -107,7 +107,7 @@ func TestStaticCallFullyQualified(t *testing.T) {
func TestStaticCallVar(t *testing.T) { func TestStaticCallVar(t *testing.T) {
src := `<? Foo::$bar();` src := `<? Foo::$bar();`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.StaticCall{ Expr: &expr.StaticCall{
@ -117,7 +117,7 @@ func TestStaticCallVar(t *testing.T) {
}, },
}, },
Call: &expr.Variable{VarName: &node.Identifier{Value: "bar"}}, Call: &expr.Variable{VarName: &node.Identifier{Value: "bar"}},
Arguments: []node.Node{}, ArgumentList: &node.ArgumentList{},
}, },
}, },
}, },
@ -137,13 +137,13 @@ func TestStaticCallVar(t *testing.T) {
func TestStaticCallVarVar(t *testing.T) { func TestStaticCallVarVar(t *testing.T) {
src := `<? $foo::$bar();` src := `<? $foo::$bar();`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.StaticCall{ Expr: &expr.StaticCall{
Class: &expr.Variable{VarName: &node.Identifier{Value: "foo"}}, Class: &expr.Variable{VarName: &node.Identifier{Value: "foo"}},
Call: &expr.Variable{VarName: &node.Identifier{Value: "bar"}}, Call: &expr.Variable{VarName: &node.Identifier{Value: "bar"}},
Arguments: []node.Node{}, ArgumentList: &node.ArgumentList{},
}, },
}, },
}, },

View File

@ -16,7 +16,7 @@ import (
func TestStaticPropertyFetch(t *testing.T) { func TestStaticPropertyFetch(t *testing.T) {
src := `<? Foo::$bar;` src := `<? Foo::$bar;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.StaticPropertyFetch{ Expr: &expr.StaticPropertyFetch{
@ -45,7 +45,7 @@ func TestStaticPropertyFetch(t *testing.T) {
func TestStaticPropertyFetchRelative(t *testing.T) { func TestStaticPropertyFetchRelative(t *testing.T) {
src := `<? namespace\Foo::$bar;` src := `<? namespace\Foo::$bar;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.StaticPropertyFetch{ Expr: &expr.StaticPropertyFetch{
@ -74,7 +74,7 @@ func TestStaticPropertyFetchRelative(t *testing.T) {
func TestStaticPropertyFetchFullyQualified(t *testing.T) { func TestStaticPropertyFetchFullyQualified(t *testing.T) {
src := `<? \Foo::$bar;` src := `<? \Foo::$bar;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.StaticPropertyFetch{ Expr: &expr.StaticPropertyFetch{

View File

@ -15,7 +15,7 @@ import (
func TestTernary(t *testing.T) { func TestTernary(t *testing.T) {
src := `<? $a ? $b : $c;` src := `<? $a ? $b : $c;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.Ternary{ Expr: &expr.Ternary{
@ -41,7 +41,7 @@ func TestTernary(t *testing.T) {
func TestTernarySimple(t *testing.T) { func TestTernarySimple(t *testing.T) {
src := `<? $a ? : $c;` src := `<? $a ? : $c;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.Ternary{ Expr: &expr.Ternary{
@ -66,7 +66,7 @@ func TestTernarySimple(t *testing.T) {
func TestTernaryNestedTrue(t *testing.T) { func TestTernaryNestedTrue(t *testing.T) {
src := `<? $a ? $b ? $c : $d : $e;` src := `<? $a ? $b ? $c : $d : $e;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.Ternary{ Expr: &expr.Ternary{
@ -96,7 +96,7 @@ func TestTernaryNestedTrue(t *testing.T) {
func TestTernaryNestedCond(t *testing.T) { func TestTernaryNestedCond(t *testing.T) {
src := `<? $a ? $b : $c ? $d : $e;` src := `<? $a ? $b : $c ? $d : $e;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.Ternary{ Expr: &expr.Ternary{

View File

@ -15,7 +15,7 @@ import (
func TestUnaryMinus(t *testing.T) { func TestUnaryMinus(t *testing.T) {
src := `<? -$a;` src := `<? -$a;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.UnaryMinus{ Expr: &expr.UnaryMinus{
@ -39,7 +39,7 @@ func TestUnaryMinus(t *testing.T) {
func TestUnaryPlus(t *testing.T) { func TestUnaryPlus(t *testing.T) {
src := `<? +$a;` src := `<? +$a;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.UnaryPlus{ Expr: &expr.UnaryPlus{

View File

@ -15,7 +15,7 @@ import (
func TestVariable(t *testing.T) { func TestVariable(t *testing.T) {
src := `<? $a;` src := `<? $a;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}, Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
@ -37,7 +37,7 @@ func TestVariable(t *testing.T) {
func TestVariableVariable(t *testing.T) { func TestVariableVariable(t *testing.T) {
src := `<? $$a;` src := `<? $$a;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.Variable{VarName: &expr.Variable{VarName: &node.Identifier{Value: "a"}}}, Expr: &expr.Variable{VarName: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},

View File

@ -32,18 +32,16 @@ var nodesToTest = []struct {
}, },
{ {
&expr.ArrayItem{ &expr.ArrayItem{
ByRef: false,
Key: &scalar.String{Value: "key"}, Key: &scalar.String{Value: "key"},
Val: &scalar.Lnumber{Value: "1"}, Val: &scalar.Lnumber{Value: "1"},
}, },
[]string{"Key", "Val"}, []string{"Key", "Val"},
map[string]interface{}{"ByRef": false}, map[string]interface{}{},
}, },
{ {
&expr.Array{ &expr.Array{
Items: []node.Node{ Items: []node.Node{
&expr.ArrayItem{ &expr.ArrayItem{
ByRef: false,
Key: &scalar.String{Value: "key"}, Key: &scalar.String{Value: "key"},
Val: &scalar.Lnumber{Value: "1"}, Val: &scalar.Lnumber{Value: "1"},
}, },
@ -83,11 +81,12 @@ var nodesToTest = []struct {
}, },
{ {
&expr.ClosureUse{ &expr.ClosureUse{
ByRef: false, Uses: []node.Node{
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}}, &expr.Variable{VarName: &node.Identifier{Value: "a"}},
}, },
[]string{"Variable"}, },
map[string]interface{}{"ByRef": false}, []string{"Uses"},
map[string]interface{}{},
}, },
{ {
&expr.Closure{ &expr.Closure{
@ -95,11 +94,11 @@ var nodesToTest = []struct {
Static: false, Static: false,
PhpDocComment: "", PhpDocComment: "",
Params: []node.Node{&node.Parameter{}}, Params: []node.Node{&node.Parameter{}},
Uses: []node.Node{&expr.ClosureUse{}}, ClosureUse: &expr.ClosureUse{},
ReturnType: &name.Name{}, ReturnType: &name.Name{},
Stmts: []node.Node{&stmt.Nop{}}, Stmts: []node.Node{&stmt.Nop{}},
}, },
[]string{"Params", "Uses", "ReturnType", "Stmts"}, []string{"Params", "ClosureUse", "ReturnType", "Stmts"},
map[string]interface{}{"ReturnsRef": true, "Static": false, "PhpDocComment": ""}, map[string]interface{}{"ReturnsRef": true, "Static": false, "PhpDocComment": ""},
}, },
{ {
@ -147,9 +146,9 @@ var nodesToTest = []struct {
{ {
&expr.FunctionCall{ &expr.FunctionCall{
Function: &expr.Variable{VarName: &node.Identifier{Value: "a"}}, Function: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
Arguments: []node.Node{&node.Argument{}}, ArgumentList: &node.ArgumentList{},
}, },
[]string{"Function", "Arguments"}, []string{"Function", "ArgumentList"},
map[string]interface{}{}, map[string]interface{}{},
}, },
{ {
@ -196,17 +195,17 @@ var nodesToTest = []struct {
&expr.MethodCall{ &expr.MethodCall{
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}}, Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
Method: &node.Identifier{Value: "foo"}, Method: &node.Identifier{Value: "foo"},
Arguments: []node.Node{&node.Argument{}}, ArgumentList: &node.ArgumentList{},
}, },
[]string{"Variable", "Method", "Arguments"}, []string{"Variable", "Method", "ArgumentList"},
map[string]interface{}{}, map[string]interface{}{},
}, },
{ {
&expr.New{ &expr.New{
Class: &name.Name{}, Class: &name.Name{},
Arguments: []node.Node{&node.Argument{}}, ArgumentList: &node.ArgumentList{},
}, },
[]string{"Class", "Arguments"}, []string{"Class", "ArgumentList"},
map[string]interface{}{}, map[string]interface{}{},
}, },
{ {
@ -252,6 +251,13 @@ var nodesToTest = []struct {
[]string{"Variable", "Property"}, []string{"Variable", "Property"},
map[string]interface{}{}, map[string]interface{}{},
}, },
{
&expr.Reference{
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
},
[]string{"Variable"},
map[string]interface{}{},
},
{ {
&expr.RequireOnce{ &expr.RequireOnce{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}, Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
@ -297,9 +303,9 @@ var nodesToTest = []struct {
&expr.StaticCall{ &expr.StaticCall{
Class: &name.Name{}, Class: &name.Name{},
Call: &node.Identifier{Value: "foo"}, Call: &node.Identifier{Value: "foo"},
Arguments: []node.Node{&node.Argument{}}, ArgumentList: &node.ArgumentList{},
}, },
[]string{"Class", "Call", "Arguments"}, []string{"Class", "Call", "ArgumentList"},
map[string]interface{}{}, map[string]interface{}{},
}, },
{ {

View File

@ -16,7 +16,7 @@ import (
func TestYield(t *testing.T) { func TestYield(t *testing.T) {
src := `<? yield;` src := `<? yield;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.Yield{}, Expr: &expr.Yield{},
@ -38,7 +38,7 @@ func TestYield(t *testing.T) {
func TestYieldVal(t *testing.T) { func TestYieldVal(t *testing.T) {
src := `<? yield $a;` src := `<? yield $a;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.Yield{ Expr: &expr.Yield{
@ -62,7 +62,7 @@ func TestYieldVal(t *testing.T) {
func TestYieldKeyVal(t *testing.T) { func TestYieldKeyVal(t *testing.T) {
src := `<? yield $a => $b;` src := `<? yield $a => $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.Yield{ Expr: &expr.Yield{
@ -87,7 +87,7 @@ func TestYieldKeyVal(t *testing.T) {
func TestYieldExpr(t *testing.T) { func TestYieldExpr(t *testing.T) {
src := `<? yield 1;` src := `<? yield 1;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.Yield{ Expr: &expr.Yield{
@ -111,7 +111,7 @@ func TestYieldExpr(t *testing.T) {
func TestYieldKeyExpr(t *testing.T) { func TestYieldKeyExpr(t *testing.T) {
src := `<? yield $a => 1;` src := `<? yield $a => 1;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.Yield{ Expr: &expr.Yield{
@ -136,7 +136,7 @@ func TestYieldKeyExpr(t *testing.T) {
func TestYieldFrom(t *testing.T) { func TestYieldFrom(t *testing.T) {
src := `<? yield from $a;` src := `<? yield from $a;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.YieldFrom{ Expr: &expr.YieldFrom{

41
node/n_argument_list.go Normal file
View File

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

41
node/n_root.go Normal file
View File

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

View File

@ -31,14 +31,14 @@ func assertEqual(t *testing.T, expected interface{}, actual interface{}) {
func TestName(t *testing.T) { func TestName(t *testing.T) {
src := `<? foo();` src := `<? foo();`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.FunctionCall{ Expr: &expr.FunctionCall{
Function: &name.Name{ Function: &name.Name{
Parts: []node.Node{&name.NamePart{Value: "foo"}}, Parts: []node.Node{&name.NamePart{Value: "foo"}},
}, },
Arguments: []node.Node{}, ArgumentList: &node.ArgumentList{},
}, },
}, },
}, },
@ -58,14 +58,14 @@ func TestName(t *testing.T) {
func TestFullyQualified(t *testing.T) { func TestFullyQualified(t *testing.T) {
src := `<? \foo();` src := `<? \foo();`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.FunctionCall{ Expr: &expr.FunctionCall{
Function: &name.FullyQualified{ Function: &name.FullyQualified{
Parts: []node.Node{&name.NamePart{Value: "foo"}}, Parts: []node.Node{&name.NamePart{Value: "foo"}},
}, },
Arguments: []node.Node{}, ArgumentList: &node.ArgumentList{},
}, },
}, },
}, },
@ -85,14 +85,14 @@ func TestFullyQualified(t *testing.T) {
func TestRelative(t *testing.T) { func TestRelative(t *testing.T) {
src := `<? namespace\foo();` src := `<? namespace\foo();`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.FunctionCall{ Expr: &expr.FunctionCall{
Function: &name.Relative{ Function: &name.Relative{
Parts: []node.Node{&name.NamePart{Value: "foo"}}, Parts: []node.Node{&name.NamePart{Value: "foo"}},
}, },
Arguments: []node.Node{}, ArgumentList: &node.ArgumentList{},
}, },
}, },
}, },

View File

@ -16,7 +16,7 @@ import (
func TestSimpleVar(t *testing.T) { func TestSimpleVar(t *testing.T) {
src := `<? "test $var";` src := `<? "test $var";`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &scalar.Encapsed{ Expr: &scalar.Encapsed{
@ -43,7 +43,7 @@ func TestSimpleVar(t *testing.T) {
func TestSimpleVarOneChar(t *testing.T) { func TestSimpleVarOneChar(t *testing.T) {
src := `<? "test $a";` src := `<? "test $a";`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &scalar.Encapsed{ Expr: &scalar.Encapsed{
@ -70,7 +70,7 @@ func TestSimpleVarOneChar(t *testing.T) {
func TestSimpleVarEndsEcapsed(t *testing.T) { func TestSimpleVarEndsEcapsed(t *testing.T) {
src := `<? "test $var\"";` src := `<? "test $var\"";`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &scalar.Encapsed{ Expr: &scalar.Encapsed{
@ -98,7 +98,7 @@ func TestSimpleVarEndsEcapsed(t *testing.T) {
func TestStringVarCurveOpen(t *testing.T) { func TestStringVarCurveOpen(t *testing.T) {
src := `<? "=$a{$b}";` src := `<? "=$a{$b}";`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &scalar.Encapsed{ Expr: &scalar.Encapsed{
@ -126,7 +126,7 @@ func TestStringVarCurveOpen(t *testing.T) {
func TestSimpleVarPropertyFetch(t *testing.T) { func TestSimpleVarPropertyFetch(t *testing.T) {
src := `<? "test $foo->bar()";` src := `<? "test $foo->bar()";`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &scalar.Encapsed{ Expr: &scalar.Encapsed{
@ -157,7 +157,7 @@ func TestSimpleVarPropertyFetch(t *testing.T) {
func TestDollarOpenCurlyBraces(t *testing.T) { func TestDollarOpenCurlyBraces(t *testing.T) {
src := `<? "test ${foo}";` src := `<? "test ${foo}";`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &scalar.Encapsed{ Expr: &scalar.Encapsed{
@ -184,7 +184,7 @@ func TestDollarOpenCurlyBraces(t *testing.T) {
func TestDollarOpenCurlyBracesDimNumber(t *testing.T) { func TestDollarOpenCurlyBracesDimNumber(t *testing.T) {
src := `<? "test ${foo[0]}";` src := `<? "test ${foo[0]}";`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &scalar.Encapsed{ Expr: &scalar.Encapsed{
@ -214,7 +214,7 @@ func TestDollarOpenCurlyBracesDimNumber(t *testing.T) {
func TestCurlyOpenMethodCall(t *testing.T) { func TestCurlyOpenMethodCall(t *testing.T) {
src := `<? "test {$foo->bar()}";` src := `<? "test {$foo->bar()}";`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &scalar.Encapsed{ Expr: &scalar.Encapsed{
@ -223,7 +223,7 @@ func TestCurlyOpenMethodCall(t *testing.T) {
&expr.MethodCall{ &expr.MethodCall{
Variable: &expr.Variable{VarName: &node.Identifier{Value: "foo"}}, Variable: &expr.Variable{VarName: &node.Identifier{Value: "foo"}},
Method: &node.Identifier{Value: "bar"}, Method: &node.Identifier{Value: "bar"},
Arguments: []node.Node{}, ArgumentList: &node.ArgumentList{},
}, },
}, },
}, },

View File

@ -19,7 +19,7 @@ test $var
LBL; LBL;
` `
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &scalar.Heredoc{ Expr: &scalar.Heredoc{
@ -51,7 +51,7 @@ test $var
LBL; LBL;
` `
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &scalar.Heredoc{ Expr: &scalar.Heredoc{
@ -83,7 +83,7 @@ test $var
LBL; LBL;
` `
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &scalar.Heredoc{ Expr: &scalar.Heredoc{
@ -112,7 +112,7 @@ func TestEmptyHeredoc(t *testing.T) {
CAD; CAD;
` `
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &scalar.Heredoc{ Expr: &scalar.Heredoc{
@ -139,7 +139,7 @@ func TestHeredocScalarString(t *testing.T) {
CAD; CAD;
` `
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &scalar.Heredoc{ Expr: &scalar.Heredoc{

View File

@ -15,7 +15,7 @@ func TestMagicConstant(t *testing.T) {
// TODO: test all magic constants // TODO: test all magic constants
src := `<? __DIR__;` src := `<? __DIR__;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &scalar.MagicConstant{Value: "__DIR__"}, Expr: &scalar.MagicConstant{Value: "__DIR__"},

View File

@ -29,7 +29,7 @@ func assertEqual(t *testing.T, expected interface{}, actual interface{}) {
func TestLNumber(t *testing.T) { func TestLNumber(t *testing.T) {
src := `<? 1234567890123456789;` src := `<? 1234567890123456789;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &scalar.Lnumber{Value: "1234567890123456789"}, Expr: &scalar.Lnumber{Value: "1234567890123456789"},
@ -51,7 +51,7 @@ func TestLNumber(t *testing.T) {
func TestDNumber(t *testing.T) { func TestDNumber(t *testing.T) {
src := `<? 12345678901234567890;` src := `<? 12345678901234567890;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &scalar.Dnumber{Value: "12345678901234567890"}, Expr: &scalar.Dnumber{Value: "12345678901234567890"},
@ -73,7 +73,7 @@ func TestDNumber(t *testing.T) {
func TestFloat(t *testing.T) { func TestFloat(t *testing.T) {
src := `<? 0.;` src := `<? 0.;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &scalar.Dnumber{Value: "0."}, Expr: &scalar.Dnumber{Value: "0."},
@ -95,7 +95,7 @@ func TestFloat(t *testing.T) {
func TestBinaryLNumber(t *testing.T) { func TestBinaryLNumber(t *testing.T) {
src := `<? 0b0111111111111111111111111111111111111111111111111111111111111111;` src := `<? 0b0111111111111111111111111111111111111111111111111111111111111111;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &scalar.Lnumber{Value: "0b0111111111111111111111111111111111111111111111111111111111111111"}, Expr: &scalar.Lnumber{Value: "0b0111111111111111111111111111111111111111111111111111111111111111"},
@ -117,7 +117,7 @@ func TestBinaryLNumber(t *testing.T) {
func TestBinaryDNumber(t *testing.T) { func TestBinaryDNumber(t *testing.T) {
src := `<? 0b1111111111111111111111111111111111111111111111111111111111111111;` src := `<? 0b1111111111111111111111111111111111111111111111111111111111111111;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &scalar.Dnumber{Value: "0b1111111111111111111111111111111111111111111111111111111111111111"}, Expr: &scalar.Dnumber{Value: "0b1111111111111111111111111111111111111111111111111111111111111111"},
@ -139,7 +139,7 @@ func TestBinaryDNumber(t *testing.T) {
func TestHLNumber(t *testing.T) { func TestHLNumber(t *testing.T) {
src := `<? 0x007111111111111111;` src := `<? 0x007111111111111111;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &scalar.Lnumber{Value: "0x007111111111111111"}, Expr: &scalar.Lnumber{Value: "0x007111111111111111"},
@ -161,7 +161,7 @@ func TestHLNumber(t *testing.T) {
func TestHDNumber(t *testing.T) { func TestHDNumber(t *testing.T) {
src := `<? 0x8111111111111111;` src := `<? 0x8111111111111111;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &scalar.Dnumber{Value: "0x8111111111111111"}, Expr: &scalar.Dnumber{Value: "0x8111111111111111"},

View File

@ -14,7 +14,7 @@ import (
func TestDoubleQuotedScalarString(t *testing.T) { func TestDoubleQuotedScalarString(t *testing.T) {
src := `<? "test";` src := `<? "test";`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &scalar.String{Value: "\"test\""}, Expr: &scalar.String{Value: "\"test\""},
@ -35,7 +35,7 @@ func TestDoubleQuotedScalarString(t *testing.T) {
func TestDoubleQuotedScalarStringWithEscapedVar(t *testing.T) { func TestDoubleQuotedScalarStringWithEscapedVar(t *testing.T) {
src := `<? "\$test";` src := `<? "\$test";`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &scalar.String{Value: "\"\\$test\""}, Expr: &scalar.String{Value: "\"\\$test\""},
@ -59,7 +59,7 @@ func TestMultilineDoubleQuotedScalarString(t *testing.T) {
test test
";` ";`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &scalar.String{Value: "\"\n\ttest\n\t\""}, Expr: &scalar.String{Value: "\"\n\ttest\n\t\""},
@ -81,7 +81,7 @@ func TestMultilineDoubleQuotedScalarString(t *testing.T) {
func TestSingleQuotedScalarString(t *testing.T) { func TestSingleQuotedScalarString(t *testing.T) {
src := `<? '$test';` src := `<? '$test';`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &scalar.String{Value: "'$test'"}, Expr: &scalar.String{Value: "'$test'"},
@ -105,7 +105,7 @@ func TestMultilineSingleQuotedScalarString(t *testing.T) {
$test $test
';` ';`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &scalar.String{Value: "'\n\t$test\n\t'"}, Expr: &scalar.String{Value: "'\n\t$test\n\t'"},

View File

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

View File

@ -8,14 +8,14 @@ import (
// AltSwitch node // AltSwitch node
type AltSwitch struct { type AltSwitch struct {
Cond node.Node Cond node.Node
Cases []node.Node CaseList *CaseList
} }
// NewAltSwitch node constructor // NewAltSwitch node constructor
func NewAltSwitch(Cond node.Node, Cases []node.Node) *AltSwitch { func NewAltSwitch(Cond node.Node, CaseList *CaseList) *AltSwitch {
return &AltSwitch{ return &AltSwitch{
Cond, Cond,
Cases, CaseList,
} }
} }
@ -36,13 +36,9 @@ func (n *AltSwitch) Walk(v walker.Visitor) {
n.Cond.Walk(vv) n.Cond.Walk(vv)
} }
if n.Cases != nil { if n.CaseList != nil {
vv := v.GetChildrenVisitor("Cases") vv := v.GetChildrenVisitor("CaseList")
for _, nn := range n.Cases { n.CaseList.Walk(vv)
if nn != nil {
nn.Walk(vv)
}
}
} }
v.LeaveNode(n) v.LeaveNode(n)

42
node/stmt/n_case_list.go Normal file
View File

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

View File

@ -10,19 +10,19 @@ type Class struct {
PhpDocComment string PhpDocComment string
ClassName node.Node ClassName node.Node
Modifiers []node.Node Modifiers []node.Node
Args []node.Node ArgumentList *node.ArgumentList
Extends node.Node Extends *ClassExtends
Implements []node.Node Implements *ClassImplements
Stmts []node.Node Stmts []node.Node
} }
// NewClass node constructor // NewClass node constructor
func NewClass(ClassName node.Node, Modifiers []node.Node, Args []node.Node, Extends node.Node, Implements []node.Node, Stmts []node.Node, PhpDocComment string) *Class { func NewClass(ClassName node.Node, Modifiers []node.Node, ArgumentList *node.ArgumentList, Extends *ClassExtends, Implements *ClassImplements, Stmts []node.Node, PhpDocComment string) *Class {
return &Class{ return &Class{
PhpDocComment, PhpDocComment,
ClassName, ClassName,
Modifiers, Modifiers,
Args, ArgumentList,
Extends, Extends,
Implements, Implements,
Stmts, Stmts,
@ -57,13 +57,9 @@ func (n *Class) Walk(v walker.Visitor) {
} }
} }
if n.Args != nil { if n.ArgumentList != nil {
vv := v.GetChildrenVisitor("Args") vv := v.GetChildrenVisitor("ArgumentList")
for _, nn := range n.Args { n.ArgumentList.Walk(vv)
if nn != nil {
nn.Walk(vv)
}
}
} }
if n.Extends != nil { if n.Extends != nil {
@ -73,11 +69,7 @@ func (n *Class) Walk(v walker.Visitor) {
if n.Implements != nil { if n.Implements != nil {
vv := v.GetChildrenVisitor("Implements") vv := v.GetChildrenVisitor("Implements")
for _, nn := range n.Implements { n.Implements.Walk(vv)
if nn != nil {
nn.Walk(vv)
}
}
} }
if n.Stmts != nil { if n.Stmts != nil {

View File

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

View File

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

View File

@ -13,11 +13,11 @@ type ClassMethod struct {
Modifiers []node.Node Modifiers []node.Node
Params []node.Node Params []node.Node
ReturnType node.Node ReturnType node.Node
Stmts []node.Node Stmt node.Node
} }
// NewClassMethod node constructor // NewClassMethod node constructor
func NewClassMethod(MethodName node.Node, Modifiers []node.Node, ReturnsRef bool, Params []node.Node, ReturnType node.Node, Stmts []node.Node, PhpDocComment string) *ClassMethod { func NewClassMethod(MethodName node.Node, Modifiers []node.Node, ReturnsRef bool, Params []node.Node, ReturnType node.Node, Stmt node.Node, PhpDocComment string) *ClassMethod {
return &ClassMethod{ return &ClassMethod{
ReturnsRef, ReturnsRef,
PhpDocComment, PhpDocComment,
@ -25,7 +25,7 @@ func NewClassMethod(MethodName node.Node, Modifiers []node.Node, ReturnsRef bool
Modifiers, Modifiers,
Params, Params,
ReturnType, ReturnType,
Stmts, Stmt,
} }
} }
@ -72,13 +72,9 @@ func (n *ClassMethod) Walk(v walker.Visitor) {
n.ReturnType.Walk(vv) n.ReturnType.Walk(vv)
} }
if n.Stmts != nil { if n.Stmt != nil {
vv := v.GetChildrenVisitor("Stmts") vv := v.GetChildrenVisitor("Stmt")
for _, nn := range n.Stmts { n.Stmt.Walk(vv)
if nn != nil {
nn.Walk(vv)
}
}
} }
v.LeaveNode(n) v.LeaveNode(n)

View File

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

View File

@ -9,12 +9,12 @@ import (
type Interface struct { type Interface struct {
PhpDocComment string PhpDocComment string
InterfaceName node.Node InterfaceName node.Node
Extends []node.Node Extends *InterfaceExtends
Stmts []node.Node Stmts []node.Node
} }
// NewInterface node constructor // NewInterface node constructor
func NewInterface(InterfaceName node.Node, Extends []node.Node, Stmts []node.Node, PhpDocComment string) *Interface { func NewInterface(InterfaceName node.Node, Extends *InterfaceExtends, Stmts []node.Node, PhpDocComment string) *Interface {
return &Interface{ return &Interface{
PhpDocComment, PhpDocComment,
InterfaceName, InterfaceName,
@ -44,11 +44,7 @@ func (n *Interface) Walk(v walker.Visitor) {
if n.Extends != nil { if n.Extends != nil {
vv := v.GetChildrenVisitor("Extends") vv := v.GetChildrenVisitor("Extends")
for _, nn := range n.Extends { n.Extends.Walk(vv)
if nn != nil {
nn.Walk(vv)
}
}
} }
if n.Stmts != nil { if n.Stmts != nil {

View File

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

View File

@ -8,14 +8,14 @@ import (
// Switch node // Switch node
type Switch struct { type Switch struct {
Cond node.Node Cond node.Node
Cases []node.Node CaseList *CaseList
} }
// NewSwitch node constructor // NewSwitch node constructor
func NewSwitch(Cond node.Node, Cases []node.Node) *Switch { func NewSwitch(Cond node.Node, CaseList *CaseList) *Switch {
return &Switch{ return &Switch{
Cond, Cond,
Cases, CaseList,
} }
} }
@ -36,13 +36,9 @@ func (n *Switch) Walk(v walker.Visitor) {
n.Cond.Walk(vv) n.Cond.Walk(vv)
} }
if n.Cases != nil { if n.CaseList != nil {
vv := v.GetChildrenVisitor("Cases") vv := v.GetChildrenVisitor("CaseList")
for _, nn := range n.Cases { n.CaseList.Walk(vv)
if nn != nil {
nn.Walk(vv)
}
}
} }
v.LeaveNode(n) v.LeaveNode(n)

View File

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

View File

@ -8,14 +8,14 @@ import (
// TraitUse node // TraitUse node
type TraitUse struct { type TraitUse struct {
Traits []node.Node Traits []node.Node
Adaptations []node.Node TraitAdaptationList *TraitAdaptationList
} }
// NewTraitUse node constructor // NewTraitUse node constructor
func NewTraitUse(Traits []node.Node, Adaptations []node.Node) *TraitUse { func NewTraitUse(Traits []node.Node, InnerAdaptationList *TraitAdaptationList) *TraitUse {
return &TraitUse{ return &TraitUse{
Traits, Traits,
Adaptations, InnerAdaptationList,
} }
} }
@ -40,13 +40,9 @@ func (n *TraitUse) Walk(v walker.Visitor) {
} }
} }
if n.Adaptations != nil { if n.TraitAdaptationList != nil {
vv := v.GetChildrenVisitor("Adaptations") vv := v.GetChildrenVisitor("TraitAdaptationList")
for _, nn := range n.Adaptations { n.TraitAdaptationList.Walk(vv)
if nn != nil {
nn.Walk(vv)
}
}
} }
v.LeaveNode(n) v.LeaveNode(n)

View File

@ -33,7 +33,7 @@ func TestAltIf(t *testing.T) {
endif; endif;
` `
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.AltIf{ &stmt.AltIf{
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}}, Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
@ -60,7 +60,7 @@ func TestAltElseIf(t *testing.T) {
endif; endif;
` `
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.AltIf{ &stmt.AltIf{
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}}, Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
@ -93,7 +93,7 @@ func TestAltElse(t *testing.T) {
endif; endif;
` `
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.AltIf{ &stmt.AltIf{
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}}, Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
@ -125,7 +125,7 @@ func TestAltElseElseIf(t *testing.T) {
endif; endif;
` `
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.AltIf{ &stmt.AltIf{
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}}, Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},

View File

@ -15,7 +15,7 @@ import (
func TestClassConstList(t *testing.T) { func TestClassConstList(t *testing.T) {
src := `<? class foo{ public const FOO = 1, BAR = 2; }` src := `<? class foo{ public const FOO = 1, BAR = 2; }`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Class{ &stmt.Class{
ClassName: &node.Identifier{Value: "foo"}, ClassName: &node.Identifier{Value: "foo"},
@ -51,7 +51,7 @@ func TestClassConstList(t *testing.T) {
func TestClassConstListWithoutModifiers(t *testing.T) { func TestClassConstListWithoutModifiers(t *testing.T) {
src := `<? class foo{ const FOO = 1, BAR = 2; }` src := `<? class foo{ const FOO = 1, BAR = 2; }`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Class{ &stmt.Class{
ClassName: &node.Identifier{Value: "foo"}, ClassName: &node.Identifier{Value: "foo"},

View File

@ -2,9 +2,10 @@ package stmt_test
import ( import (
"bytes" "bytes"
"github.com/z7zmey/php-parser/node/name"
"testing" "testing"
"github.com/z7zmey/php-parser/node/name"
"github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/node/stmt" "github.com/z7zmey/php-parser/node/stmt"
"github.com/z7zmey/php-parser/php5" "github.com/z7zmey/php-parser/php5"
@ -14,7 +15,7 @@ import (
func TestSimpleClassMethod(t *testing.T) { func TestSimpleClassMethod(t *testing.T) {
src := `<? class foo{ function bar() {} }` src := `<? class foo{ function bar() {} }`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Class{ &stmt.Class{
ClassName: &node.Identifier{Value: "foo"}, ClassName: &node.Identifier{Value: "foo"},
@ -22,11 +23,13 @@ func TestSimpleClassMethod(t *testing.T) {
&stmt.ClassMethod{ &stmt.ClassMethod{
PhpDocComment: "", PhpDocComment: "",
MethodName: &node.Identifier{Value: "bar"}, MethodName: &node.Identifier{Value: "bar"},
Stmt: &stmt.StmtList{
Stmts: []node.Node{}, Stmts: []node.Node{},
}, },
}, },
}, },
}, },
},
} }
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php") php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
@ -43,7 +46,7 @@ func TestSimpleClassMethod(t *testing.T) {
func TestPrivateProtectedClassMethod(t *testing.T) { func TestPrivateProtectedClassMethod(t *testing.T) {
src := `<? class foo{ final private function bar() {} protected function baz() {} }` src := `<? class foo{ final private function bar() {} protected function baz() {} }`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Class{ &stmt.Class{
ClassName: &node.Identifier{Value: "foo"}, ClassName: &node.Identifier{Value: "foo"},
@ -56,8 +59,10 @@ func TestPrivateProtectedClassMethod(t *testing.T) {
&node.Identifier{Value: "final"}, &node.Identifier{Value: "final"},
&node.Identifier{Value: "private"}, &node.Identifier{Value: "private"},
}, },
Stmt: &stmt.StmtList{
Stmts: []node.Node{}, Stmts: []node.Node{},
}, },
},
&stmt.ClassMethod{ &stmt.ClassMethod{
PhpDocComment: "", PhpDocComment: "",
ReturnsRef: false, ReturnsRef: false,
@ -65,11 +70,13 @@ func TestPrivateProtectedClassMethod(t *testing.T) {
Modifiers: []node.Node{ Modifiers: []node.Node{
&node.Identifier{Value: "protected"}, &node.Identifier{Value: "protected"},
}, },
Stmt: &stmt.StmtList{
Stmts: []node.Node{}, Stmts: []node.Node{},
}, },
}, },
}, },
}, },
},
} }
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php") php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
@ -86,7 +93,7 @@ func TestPrivateProtectedClassMethod(t *testing.T) {
func TestPhp5ClassMethod(t *testing.T) { func TestPhp5ClassMethod(t *testing.T) {
src := `<? class foo{ public static function &bar() {} }` src := `<? class foo{ public static function &bar() {} }`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Class{ &stmt.Class{
ClassName: &node.Identifier{Value: "foo"}, ClassName: &node.Identifier{Value: "foo"},
@ -99,11 +106,13 @@ func TestPhp5ClassMethod(t *testing.T) {
&node.Identifier{Value: "public"}, &node.Identifier{Value: "public"},
&node.Identifier{Value: "static"}, &node.Identifier{Value: "static"},
}, },
Stmt: &stmt.StmtList{
Stmts: []node.Node{}, Stmts: []node.Node{},
}, },
}, },
}, },
}, },
},
} }
php5parser := php5.NewParser(bytes.NewBufferString(src), "test.php") php5parser := php5.NewParser(bytes.NewBufferString(src), "test.php")
@ -115,7 +124,7 @@ func TestPhp5ClassMethod(t *testing.T) {
func TestPhp7ClassMethod(t *testing.T) { func TestPhp7ClassMethod(t *testing.T) {
src := `<? class foo{ public static function &bar(): void {} }` src := `<? class foo{ public static function &bar(): void {} }`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Class{ &stmt.Class{
ClassName: &node.Identifier{Value: "foo"}, ClassName: &node.Identifier{Value: "foo"},
@ -133,11 +142,13 @@ func TestPhp7ClassMethod(t *testing.T) {
&name.NamePart{Value: "void"}, &name.NamePart{Value: "void"},
}, },
}, },
Stmt: &stmt.StmtList{
Stmts: []node.Node{}, Stmts: []node.Node{},
}, },
}, },
}, },
}, },
},
} }
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php") php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
@ -149,7 +160,7 @@ func TestPhp7ClassMethod(t *testing.T) {
func TestAbstractClassMethod(t *testing.T) { func TestAbstractClassMethod(t *testing.T) {
src := `<? abstract class Foo{ abstract public function bar(); }` src := `<? abstract class Foo{ abstract public function bar(); }`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Class{ &stmt.Class{
Modifiers: []node.Node{&node.Identifier{Value: "abstract"}}, Modifiers: []node.Node{&node.Identifier{Value: "abstract"}},
@ -163,6 +174,7 @@ func TestAbstractClassMethod(t *testing.T) {
&node.Identifier{Value: "abstract"}, &node.Identifier{Value: "abstract"},
&node.Identifier{Value: "public"}, &node.Identifier{Value: "public"},
}, },
Stmt: &stmt.Nop{},
}, },
}, },
}, },
@ -183,7 +195,7 @@ func TestAbstractClassMethod(t *testing.T) {
func TestPhp7AbstractClassMethod(t *testing.T) { func TestPhp7AbstractClassMethod(t *testing.T) {
src := `<? abstract class Foo{ public function bar(): void; }` src := `<? abstract class Foo{ public function bar(): void; }`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Class{ &stmt.Class{
Modifiers: []node.Node{&node.Identifier{Value: "abstract"}}, Modifiers: []node.Node{&node.Identifier{Value: "abstract"}},
@ -201,6 +213,7 @@ func TestPhp7AbstractClassMethod(t *testing.T) {
&name.NamePart{Value: "void"}, &name.NamePart{Value: "void"},
}, },
}, },
Stmt: &stmt.Nop{},
}, },
}, },
}, },

View File

@ -2,9 +2,10 @@ package stmt_test
import ( import (
"bytes" "bytes"
"testing"
"github.com/z7zmey/php-parser/node/expr" "github.com/z7zmey/php-parser/node/expr"
"github.com/z7zmey/php-parser/node/name" "github.com/z7zmey/php-parser/node/name"
"testing"
"github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/node/stmt" "github.com/z7zmey/php-parser/node/stmt"
@ -15,7 +16,7 @@ import (
func TestSimpleClass(t *testing.T) { func TestSimpleClass(t *testing.T) {
src := `<? class foo{ }` src := `<? class foo{ }`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Class{ &stmt.Class{
ClassName: &node.Identifier{Value: "foo"}, ClassName: &node.Identifier{Value: "foo"},
@ -38,7 +39,7 @@ func TestSimpleClass(t *testing.T) {
func TestAbstractClass(t *testing.T) { func TestAbstractClass(t *testing.T) {
src := `<? abstract class foo{ }` src := `<? abstract class foo{ }`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Class{ &stmt.Class{
ClassName: &node.Identifier{Value: "foo"}, ClassName: &node.Identifier{Value: "foo"},
@ -64,18 +65,20 @@ func TestAbstractClass(t *testing.T) {
func TestClassExtends(t *testing.T) { func TestClassExtends(t *testing.T) {
src := `<? final class foo extends bar { }` src := `<? final class foo extends bar { }`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Class{ &stmt.Class{
ClassName: &node.Identifier{Value: "foo"}, ClassName: &node.Identifier{Value: "foo"},
Modifiers: []node.Node{ Modifiers: []node.Node{
&node.Identifier{Value: "final"}, &node.Identifier{Value: "final"},
}, },
Extends: &name.Name{ Extends: &stmt.ClassExtends{
ClassName: &name.Name{
Parts: []node.Node{ Parts: []node.Node{
&name.NamePart{Value: "bar"}, &name.NamePart{Value: "bar"},
}, },
}, },
},
Stmts: []node.Node{}, Stmts: []node.Node{},
}, },
}, },
@ -95,20 +98,22 @@ func TestClassExtends(t *testing.T) {
func TestClassImplement(t *testing.T) { func TestClassImplement(t *testing.T) {
src := `<? final class foo implements bar { }` src := `<? final class foo implements bar { }`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Class{ &stmt.Class{
ClassName: &node.Identifier{Value: "foo"}, ClassName: &node.Identifier{Value: "foo"},
Modifiers: []node.Node{ Modifiers: []node.Node{
&node.Identifier{Value: "final"}, &node.Identifier{Value: "final"},
}, },
Implements: []node.Node{ Implements: &stmt.ClassImplements{
InterfaceNames: []node.Node{
&name.Name{ &name.Name{
Parts: []node.Node{ Parts: []node.Node{
&name.NamePart{Value: "bar"}, &name.NamePart{Value: "bar"},
}, },
}, },
}, },
},
Stmts: []node.Node{}, Stmts: []node.Node{},
}, },
}, },
@ -128,14 +133,15 @@ func TestClassImplement(t *testing.T) {
func TestClassImplements(t *testing.T) { func TestClassImplements(t *testing.T) {
src := `<? final class foo implements bar, baz { }` src := `<? final class foo implements bar, baz { }`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Class{ &stmt.Class{
ClassName: &node.Identifier{Value: "foo"}, ClassName: &node.Identifier{Value: "foo"},
Modifiers: []node.Node{ Modifiers: []node.Node{
&node.Identifier{Value: "final"}, &node.Identifier{Value: "final"},
}, },
Implements: []node.Node{ Implements: &stmt.ClassImplements{
InterfaceNames: []node.Node{
&name.Name{ &name.Name{
Parts: []node.Node{ Parts: []node.Node{
&name.NamePart{Value: "bar"}, &name.NamePart{Value: "bar"},
@ -147,6 +153,7 @@ func TestClassImplements(t *testing.T) {
}, },
}, },
}, },
},
Stmts: []node.Node{}, Stmts: []node.Node{},
}, },
}, },
@ -166,18 +173,21 @@ func TestClassImplements(t *testing.T) {
func TestAnonimousClass(t *testing.T) { func TestAnonimousClass(t *testing.T) {
src := `<? new class() extends foo implements bar, baz { };` src := `<? new class() extends foo implements bar, baz { };`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &expr.New{ Expr: &expr.New{
Class: &stmt.Class{ Class: &stmt.Class{
Args: []node.Node{}, ArgumentList: &node.ArgumentList{},
Extends: &name.Name{ Extends: &stmt.ClassExtends{
ClassName: &name.Name{
Parts: []node.Node{ Parts: []node.Node{
&name.NamePart{Value: "foo"}, &name.NamePart{Value: "foo"},
}, },
}, },
Implements: []node.Node{ },
Implements: &stmt.ClassImplements{
InterfaceNames: []node.Node{
&name.Name{ &name.Name{
Parts: []node.Node{ Parts: []node.Node{
&name.NamePart{Value: "bar"}, &name.NamePart{Value: "bar"},
@ -189,6 +199,7 @@ func TestAnonimousClass(t *testing.T) {
}, },
}, },
}, },
},
Stmts: []node.Node{}, Stmts: []node.Node{},
}, },
}, },

View File

@ -15,7 +15,7 @@ import (
func TestConstList(t *testing.T) { func TestConstList(t *testing.T) {
src := `<? const FOO = 1, BAR = 2;` src := `<? const FOO = 1, BAR = 2;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.ConstList{ &stmt.ConstList{
Consts: []node.Node{ Consts: []node.Node{

View File

@ -15,7 +15,7 @@ import (
func TestContinueEmpty(t *testing.T) { func TestContinueEmpty(t *testing.T) {
src := `<? while (1) { continue; }` src := `<? while (1) { continue; }`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.While{ &stmt.While{
Cond: &scalar.Lnumber{Value: "1"}, Cond: &scalar.Lnumber{Value: "1"},
@ -42,7 +42,7 @@ func TestContinueEmpty(t *testing.T) {
func TestContinueLight(t *testing.T) { func TestContinueLight(t *testing.T) {
src := `<? while (1) { continue 2; }` src := `<? while (1) { continue 2; }`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.While{ &stmt.While{
Cond: &scalar.Lnumber{Value: "1"}, Cond: &scalar.Lnumber{Value: "1"},
@ -71,7 +71,7 @@ func TestContinueLight(t *testing.T) {
func TestContinue(t *testing.T) { func TestContinue(t *testing.T) {
src := `<? while (1) { continue(3); }` src := `<? while (1) { continue(3); }`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.While{ &stmt.While{
Cond: &scalar.Lnumber{Value: "1"}, Cond: &scalar.Lnumber{Value: "1"},

View File

@ -15,7 +15,7 @@ import (
func TestDeclare(t *testing.T) { func TestDeclare(t *testing.T) {
src := `<? declare(ticks=1);` src := `<? declare(ticks=1);`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Declare{ &stmt.Declare{
Consts: []node.Node{ Consts: []node.Node{
@ -44,7 +44,7 @@ func TestDeclare(t *testing.T) {
func TestDeclareStmts(t *testing.T) { func TestDeclareStmts(t *testing.T) {
src := `<? declare(ticks=1, strict_types=1) {}` src := `<? declare(ticks=1, strict_types=1) {}`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Declare{ &stmt.Declare{
Consts: []node.Node{ Consts: []node.Node{
@ -80,7 +80,7 @@ func TestDeclareStmts(t *testing.T) {
func TestAltDeclare(t *testing.T) { func TestAltDeclare(t *testing.T) {
src := `<? declare(ticks=1): enddeclare;` src := `<? declare(ticks=1): enddeclare;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Declare{ &stmt.Declare{
Consts: []node.Node{ Consts: []node.Node{

View File

@ -15,7 +15,7 @@ import (
func TestDo(t *testing.T) { func TestDo(t *testing.T) {
src := `<? do {} while(1);` src := `<? do {} while(1);`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Do{ &stmt.Do{
Stmt: &stmt.StmtList{ Stmt: &stmt.StmtList{

View File

@ -17,7 +17,7 @@ import (
func TestSimpleEcho(t *testing.T) { func TestSimpleEcho(t *testing.T) {
src := `<? echo $a, 1;` src := `<? echo $a, 1;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Echo{ &stmt.Echo{
Exprs: []node.Node{ Exprs: []node.Node{
@ -44,7 +44,7 @@ func TestSimpleEcho(t *testing.T) {
func TestEcho(t *testing.T) { func TestEcho(t *testing.T) {
src := `<? echo($a);` src := `<? echo($a);`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Echo{ &stmt.Echo{
Exprs: []node.Node{ Exprs: []node.Node{

View File

@ -15,7 +15,7 @@ import (
func TestExpression(t *testing.T) { func TestExpression(t *testing.T) {
src := `<? 1;` src := `<? 1;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{ &stmt.Expression{
Expr: &scalar.Lnumber{Value: "1"}, Expr: &scalar.Lnumber{Value: "1"},

View File

@ -20,7 +20,7 @@ import (
func TestFor(t *testing.T) { func TestFor(t *testing.T) {
src := `<? for($i = 0; $i < 10; $i++, $i++) {}` src := `<? for($i = 0; $i < 10; $i++, $i++) {}`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.For{ &stmt.For{
Init: []node.Node{ Init: []node.Node{
@ -62,7 +62,7 @@ func TestFor(t *testing.T) {
func TestAltFor(t *testing.T) { func TestAltFor(t *testing.T) {
src := `<? for(; $i < 10; $i++) : endfor;` src := `<? for(; $i < 10; $i++) : endfor;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.AltFor{ &stmt.AltFor{
Cond: []node.Node{ Cond: []node.Node{

View File

@ -15,7 +15,7 @@ import (
func TestForeach(t *testing.T) { func TestForeach(t *testing.T) {
src := `<? foreach ($a as $v) {}` src := `<? foreach ($a as $v) {}`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Foreach{ &stmt.Foreach{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}, Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
@ -39,7 +39,7 @@ func TestForeach(t *testing.T) {
func TestForeachExpr(t *testing.T) { func TestForeachExpr(t *testing.T) {
src := `<? foreach ([] as $v) {}` src := `<? foreach ([] as $v) {}`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Foreach{ &stmt.Foreach{
Expr: &expr.ShortArray{Items: []node.Node{}}, Expr: &expr.ShortArray{Items: []node.Node{}},
@ -63,7 +63,7 @@ func TestForeachExpr(t *testing.T) {
func TestAltForeach(t *testing.T) { func TestAltForeach(t *testing.T) {
src := `<? foreach ($a as $v) : endforeach;` src := `<? foreach ($a as $v) : endforeach;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.AltForeach{ &stmt.AltForeach{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}, Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
@ -87,7 +87,7 @@ func TestAltForeach(t *testing.T) {
func TestForeachWithKey(t *testing.T) { func TestForeachWithKey(t *testing.T) {
src := `<? foreach ($a as $k => $v) {}` src := `<? foreach ($a as $k => $v) {}`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Foreach{ &stmt.Foreach{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}, Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
@ -112,7 +112,7 @@ func TestForeachWithKey(t *testing.T) {
func TestForeachExprWithKey(t *testing.T) { func TestForeachExprWithKey(t *testing.T) {
src := `<? foreach ([] as $k => $v) {}` src := `<? foreach ([] as $k => $v) {}`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Foreach{ &stmt.Foreach{
Expr: &expr.ShortArray{Items: []node.Node{}}, Expr: &expr.ShortArray{Items: []node.Node{}},
@ -137,13 +137,12 @@ func TestForeachExprWithKey(t *testing.T) {
func TestForeachWithRef(t *testing.T) { func TestForeachWithRef(t *testing.T) {
src := `<? foreach ($a as $k => &$v) {}` src := `<? foreach ($a as $k => &$v) {}`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Foreach{ &stmt.Foreach{
ByRef: true,
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}, Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
Key: &expr.Variable{VarName: &node.Identifier{Value: "k"}}, 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{}}, Stmt: &stmt.StmtList{Stmts: []node.Node{}},
}, },
}, },
@ -163,16 +162,14 @@ func TestForeachWithRef(t *testing.T) {
func TestForeachWithList(t *testing.T) { func TestForeachWithList(t *testing.T) {
src := `<? foreach ($a as $k => list($v)) {}` src := `<? foreach ($a as $k => list($v)) {}`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Foreach{ &stmt.Foreach{
ByRef: false,
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}, Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
Key: &expr.Variable{VarName: &node.Identifier{Value: "k"}}, Key: &expr.Variable{VarName: &node.Identifier{Value: "k"}},
Variable: &expr.List{ Variable: &expr.List{
Items: []node.Node{ Items: []node.Node{
&expr.ArrayItem{ &expr.ArrayItem{
ByRef: false,
Val: &expr.Variable{VarName: &node.Identifier{Value: "v"}}, Val: &expr.Variable{VarName: &node.Identifier{Value: "v"}},
}, },
}, },

View File

@ -17,7 +17,7 @@ import (
func TestSimpleFunction(t *testing.T) { func TestSimpleFunction(t *testing.T) {
src := `<? function foo() {}` src := `<? function foo() {}`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Function{ &stmt.Function{
ReturnsRef: false, ReturnsRef: false,
@ -42,7 +42,7 @@ func TestSimpleFunction(t *testing.T) {
func TestFunctionReturn(t *testing.T) { func TestFunctionReturn(t *testing.T) {
src := `<? function foo() {return;}` src := `<? function foo() {return;}`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Function{ &stmt.Function{
ReturnsRef: false, ReturnsRef: false,
@ -69,7 +69,7 @@ func TestFunctionReturn(t *testing.T) {
func TestFunctionReturnVar(t *testing.T) { func TestFunctionReturnVar(t *testing.T) {
src := `<? function foo(array $a, callable $b) {return $a;}` src := `<? function foo(array $a, callable $b) {return $a;}`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Function{ &stmt.Function{
ReturnsRef: false, ReturnsRef: false,
@ -112,7 +112,7 @@ func TestFunctionReturnVar(t *testing.T) {
func TestRefFunction(t *testing.T) { func TestRefFunction(t *testing.T) {
src := `<? function &foo() {return 1;}` src := `<? function &foo() {return 1;}`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Function{ &stmt.Function{
ReturnsRef: true, ReturnsRef: true,
@ -141,7 +141,7 @@ func TestRefFunction(t *testing.T) {
func TestReturnTypeFunction(t *testing.T) { func TestReturnTypeFunction(t *testing.T) {
src := `<? function &foo(): void {}` src := `<? function &foo(): void {}`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Function{ &stmt.Function{
ReturnsRef: true, ReturnsRef: true,

View File

@ -15,7 +15,7 @@ import (
func TestGlobal(t *testing.T) { func TestGlobal(t *testing.T) {
src := `<? global $a;` src := `<? global $a;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Global{ &stmt.Global{
Vars: []node.Node{ Vars: []node.Node{
@ -39,7 +39,7 @@ func TestGlobal(t *testing.T) {
func TestGlobalVars(t *testing.T) { func TestGlobalVars(t *testing.T) {
src := `<? global $a, $b, $$c, ${foo()};` src := `<? global $a, $b, $$c, ${foo()};`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Global{ &stmt.Global{
Vars: []node.Node{ Vars: []node.Node{
@ -53,7 +53,7 @@ func TestGlobalVars(t *testing.T) {
&name.NamePart{Value: "foo"}, &name.NamePart{Value: "foo"},
}, },
}, },
Arguments: []node.Node{}, ArgumentList: &node.ArgumentList{},
}, },
}, },
}, },

View File

@ -13,7 +13,7 @@ import (
func TestGotoLabel(t *testing.T) { func TestGotoLabel(t *testing.T) {
src := `<? a: goto a;` src := `<? a: goto a;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Label{ &stmt.Label{
LabelName: &node.Identifier{Value: "a"}, LabelName: &node.Identifier{Value: "a"},

View File

@ -13,7 +13,7 @@ import (
func TestHaltCompiler(t *testing.T) { func TestHaltCompiler(t *testing.T) {
src := `<? __halt_compiler();` src := `<? __halt_compiler();`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.HaltCompiler{}, &stmt.HaltCompiler{},
}, },

View File

@ -15,7 +15,7 @@ import (
func TestIf(t *testing.T) { func TestIf(t *testing.T) {
src := `<? if ($a) {}` src := `<? if ($a) {}`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.If{ &stmt.If{
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}}, Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
@ -39,7 +39,7 @@ func TestElseIf(t *testing.T) {
src := `<? if ($a) {} elseif ($b) {} src := `<? if ($a) {} elseif ($b) {}
` `
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.If{ &stmt.If{
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}}, Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
@ -68,7 +68,7 @@ func TestElseIf(t *testing.T) {
func TestElse(t *testing.T) { func TestElse(t *testing.T) {
src := `<? if ($a) {} else {}` src := `<? if ($a) {} else {}`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.If{ &stmt.If{
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}}, Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
@ -94,7 +94,7 @@ func TestElse(t *testing.T) {
func TestElseElseIf(t *testing.T) { func TestElseElseIf(t *testing.T) {
src := `<? if ($a) {} elseif ($b) {} elseif ($c) {} else {}` src := `<? if ($a) {} elseif ($b) {} elseif ($c) {} else {}`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.If{ &stmt.If{
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}}, Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
@ -130,7 +130,7 @@ func TestElseElseIf(t *testing.T) {
func TestElseIfElseIfElse(t *testing.T) { func TestElseIfElseIfElse(t *testing.T) {
src := `<? if ($a) {} elseif ($b) {} else if ($c) {} else {}` src := `<? if ($a) {} elseif ($b) {} else if ($c) {} else {}`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.If{ &stmt.If{
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}}, Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},

View File

@ -13,7 +13,7 @@ import (
func TestInlineHtml(t *testing.T) { func TestInlineHtml(t *testing.T) {
src := `<? ?> <div></div>` src := `<? ?> <div></div>`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Nop{}, &stmt.Nop{},
&stmt.InlineHtml{Value: "<div></div>"}, &stmt.InlineHtml{Value: "<div></div>"},

View File

@ -14,7 +14,7 @@ import (
func TestInterface(t *testing.T) { func TestInterface(t *testing.T) {
src := `<? interface Foo {}` src := `<? interface Foo {}`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Interface{ &stmt.Interface{
PhpDocComment: "", PhpDocComment: "",
@ -38,18 +38,20 @@ func TestInterface(t *testing.T) {
func TestInterfaceExtend(t *testing.T) { func TestInterfaceExtend(t *testing.T) {
src := `<? interface Foo extends Bar {}` src := `<? interface Foo extends Bar {}`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Interface{ &stmt.Interface{
PhpDocComment: "", PhpDocComment: "",
InterfaceName: &node.Identifier{Value: "Foo"}, InterfaceName: &node.Identifier{Value: "Foo"},
Extends: []node.Node{ Extends: &stmt.InterfaceExtends{
InterfaceNames: []node.Node{
&name.Name{ &name.Name{
Parts: []node.Node{ Parts: []node.Node{
&name.NamePart{Value: "Bar"}, &name.NamePart{Value: "Bar"},
}, },
}, },
}, },
},
Stmts: []node.Node{}, Stmts: []node.Node{},
}, },
}, },
@ -69,12 +71,13 @@ func TestInterfaceExtend(t *testing.T) {
func TestInterfaceExtends(t *testing.T) { func TestInterfaceExtends(t *testing.T) {
src := `<? interface Foo extends Bar, Baz {}` src := `<? interface Foo extends Bar, Baz {}`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Interface{ &stmt.Interface{
PhpDocComment: "", PhpDocComment: "",
InterfaceName: &node.Identifier{Value: "Foo"}, InterfaceName: &node.Identifier{Value: "Foo"},
Extends: []node.Node{ Extends: &stmt.InterfaceExtends{
InterfaceNames: []node.Node{
&name.Name{ &name.Name{
Parts: []node.Node{ Parts: []node.Node{
&name.NamePart{Value: "Bar"}, &name.NamePart{Value: "Bar"},
@ -86,6 +89,7 @@ func TestInterfaceExtends(t *testing.T) {
}, },
}, },
}, },
},
Stmts: []node.Node{}, Stmts: []node.Node{},
}, },
}, },

View File

@ -14,7 +14,7 @@ import (
func TestNamespace(t *testing.T) { func TestNamespace(t *testing.T) {
src := `<? namespace Foo;` src := `<? namespace Foo;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Namespace{ &stmt.Namespace{
NamespaceName: &name.Name{ NamespaceName: &name.Name{
@ -40,7 +40,7 @@ func TestNamespace(t *testing.T) {
func TestNamespaceStmts(t *testing.T) { func TestNamespaceStmts(t *testing.T) {
src := `<? namespace Foo {}` src := `<? namespace Foo {}`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Namespace{ &stmt.Namespace{
NamespaceName: &name.Name{ NamespaceName: &name.Name{
@ -67,7 +67,7 @@ func TestNamespaceStmts(t *testing.T) {
func TestAnonymousNamespace(t *testing.T) { func TestAnonymousNamespace(t *testing.T) {
src := `<? namespace {}` src := `<? namespace {}`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Namespace{ &stmt.Namespace{
Stmts: []node.Node{}, Stmts: []node.Node{},

View File

@ -15,7 +15,7 @@ import (
func TestProperty(t *testing.T) { func TestProperty(t *testing.T) {
src := `<? class foo {var $a;}` src := `<? class foo {var $a;}`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Class{ &stmt.Class{
ClassName: &node.Identifier{Value: "foo"}, ClassName: &node.Identifier{Value: "foo"},
@ -50,7 +50,7 @@ func TestProperty(t *testing.T) {
func TestProperties(t *testing.T) { func TestProperties(t *testing.T) {
src := `<? class foo {public static $a, $b = 1;}` src := `<? class foo {public static $a, $b = 1;}`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Class{ &stmt.Class{
ClassName: &node.Identifier{Value: "foo"}, ClassName: &node.Identifier{Value: "foo"},
@ -91,7 +91,7 @@ func TestProperties(t *testing.T) {
func TestProperties2(t *testing.T) { func TestProperties2(t *testing.T) {
src := `<? class foo {public static $a = 1, $b;}` src := `<? class foo {public static $a = 1, $b;}`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Class{ &stmt.Class{
ClassName: &node.Identifier{Value: "foo"}, ClassName: &node.Identifier{Value: "foo"},

View File

@ -15,7 +15,7 @@ import (
func TestStaticVar(t *testing.T) { func TestStaticVar(t *testing.T) {
src := `<? static $a;` src := `<? static $a;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Static{ &stmt.Static{
Vars: []node.Node{ Vars: []node.Node{
@ -41,7 +41,7 @@ func TestStaticVar(t *testing.T) {
func TestStaticVars(t *testing.T) { func TestStaticVars(t *testing.T) {
src := `<? static $a, $b = 1;` src := `<? static $a, $b = 1;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Static{ &stmt.Static{
Vars: []node.Node{ Vars: []node.Node{
@ -71,7 +71,7 @@ func TestStaticVars(t *testing.T) {
func TestStaticVars2(t *testing.T) { func TestStaticVars2(t *testing.T) {
src := `<? static $a = 1, $b;` src := `<? static $a = 1, $b;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Static{ &stmt.Static{
Vars: []node.Node{ Vars: []node.Node{

View File

@ -21,10 +21,11 @@ func TestAltSwitch(t *testing.T) {
endswitch; endswitch;
` `
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.AltSwitch{ &stmt.AltSwitch{
Cond: &scalar.Lnumber{Value: "1"}, Cond: &scalar.Lnumber{Value: "1"},
CaseList: &stmt.CaseList{
Cases: []node.Node{ Cases: []node.Node{
&stmt.Case{ &stmt.Case{
Cond: &scalar.Lnumber{Value: "1"}, Cond: &scalar.Lnumber{Value: "1"},
@ -40,6 +41,7 @@ func TestAltSwitch(t *testing.T) {
}, },
}, },
}, },
},
} }
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php") php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
@ -61,10 +63,11 @@ func TestAltSwitchSemicolon(t *testing.T) {
endswitch; endswitch;
` `
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.AltSwitch{ &stmt.AltSwitch{
Cond: &scalar.Lnumber{Value: "1"}, Cond: &scalar.Lnumber{Value: "1"},
CaseList: &stmt.CaseList{
Cases: []node.Node{ Cases: []node.Node{
&stmt.Case{ &stmt.Case{
Cond: &scalar.Lnumber{Value: "1"}, Cond: &scalar.Lnumber{Value: "1"},
@ -77,6 +80,7 @@ func TestAltSwitchSemicolon(t *testing.T) {
}, },
}, },
}, },
},
} }
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php") php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
@ -98,10 +102,11 @@ func TestSwitch(t *testing.T) {
} }
` `
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Switch{ &stmt.Switch{
Cond: &scalar.Lnumber{Value: "1"}, Cond: &scalar.Lnumber{Value: "1"},
CaseList: &stmt.CaseList{
Cases: []node.Node{ Cases: []node.Node{
&stmt.Case{ &stmt.Case{
Cond: &scalar.Lnumber{Value: "1"}, Cond: &scalar.Lnumber{Value: "1"},
@ -118,6 +123,7 @@ func TestSwitch(t *testing.T) {
}, },
}, },
}, },
},
} }
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php") php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
@ -139,10 +145,11 @@ func TestSwitchSemicolon(t *testing.T) {
} }
` `
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Switch{ &stmt.Switch{
Cond: &scalar.Lnumber{Value: "1"}, Cond: &scalar.Lnumber{Value: "1"},
CaseList: &stmt.CaseList{
Cases: []node.Node{ Cases: []node.Node{
&stmt.Case{ &stmt.Case{
Cond: &scalar.Lnumber{Value: "1"}, Cond: &scalar.Lnumber{Value: "1"},
@ -159,6 +166,7 @@ func TestSwitchSemicolon(t *testing.T) {
}, },
}, },
}, },
},
} }
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php") php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")

View File

@ -14,7 +14,7 @@ import (
func TestThrow(t *testing.T) { func TestThrow(t *testing.T) {
src := `<? throw $e;` src := `<? throw $e;`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Throw{ &stmt.Throw{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "e"}}, Expr: &expr.Variable{VarName: &node.Identifier{Value: "e"}},

View File

@ -13,7 +13,7 @@ import (
func TestTrait(t *testing.T) { func TestTrait(t *testing.T) {
src := `<? trait Foo {}` src := `<? trait Foo {}`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Trait{ &stmt.Trait{
PhpDocComment: "", PhpDocComment: "",

View File

@ -2,9 +2,10 @@ package stmt_test
import ( import (
"bytes" "bytes"
"github.com/z7zmey/php-parser/node/name"
"testing" "testing"
"github.com/z7zmey/php-parser/node/name"
"github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/node/stmt" "github.com/z7zmey/php-parser/node/stmt"
"github.com/z7zmey/php-parser/php5" "github.com/z7zmey/php-parser/php5"
@ -14,7 +15,7 @@ import (
func TestTraitUse(t *testing.T) { func TestTraitUse(t *testing.T) {
src := `<? class Foo { use Bar; }` src := `<? class Foo { use Bar; }`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Class{ &stmt.Class{
PhpDocComment: "", PhpDocComment: "",
@ -48,7 +49,7 @@ func TestTraitUse(t *testing.T) {
func TestTraitsUse(t *testing.T) { func TestTraitsUse(t *testing.T) {
src := `<? class Foo { use Bar, Baz; }` src := `<? class Foo { use Bar, Baz; }`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Class{ &stmt.Class{
PhpDocComment: "", PhpDocComment: "",
@ -87,7 +88,7 @@ func TestTraitsUse(t *testing.T) {
func TestTraitsUseEmptyAdaptations(t *testing.T) { func TestTraitsUseEmptyAdaptations(t *testing.T) {
src := `<? class Foo { use Bar, Baz {} }` src := `<? class Foo { use Bar, Baz {} }`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Class{ &stmt.Class{
PhpDocComment: "", PhpDocComment: "",
@ -106,6 +107,7 @@ func TestTraitsUseEmptyAdaptations(t *testing.T) {
}, },
}, },
}, },
TraitAdaptationList: &stmt.TraitAdaptationList{},
}, },
}, },
}, },
@ -126,7 +128,7 @@ func TestTraitsUseEmptyAdaptations(t *testing.T) {
func TestTraitsUseModifier(t *testing.T) { func TestTraitsUseModifier(t *testing.T) {
src := `<? class Foo { use Bar, Baz { one as public; } }` src := `<? class Foo { use Bar, Baz { one as public; } }`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Class{ &stmt.Class{
PhpDocComment: "", PhpDocComment: "",
@ -145,6 +147,7 @@ func TestTraitsUseModifier(t *testing.T) {
}, },
}, },
}, },
TraitAdaptationList: &stmt.TraitAdaptationList{
Adaptations: []node.Node{ Adaptations: []node.Node{
&stmt.TraitUseAlias{ &stmt.TraitUseAlias{
Ref: &stmt.TraitMethodRef{ Ref: &stmt.TraitMethodRef{
@ -157,6 +160,7 @@ func TestTraitsUseModifier(t *testing.T) {
}, },
}, },
}, },
},
} }
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php") php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
@ -173,7 +177,7 @@ func TestTraitsUseModifier(t *testing.T) {
func TestTraitsUseAliasModifier(t *testing.T) { func TestTraitsUseAliasModifier(t *testing.T) {
src := `<? class Foo { use Bar, Baz { one as public two; } }` src := `<? class Foo { use Bar, Baz { one as public two; } }`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Class{ &stmt.Class{
PhpDocComment: "", PhpDocComment: "",
@ -192,6 +196,7 @@ func TestTraitsUseAliasModifier(t *testing.T) {
}, },
}, },
}, },
TraitAdaptationList: &stmt.TraitAdaptationList{
Adaptations: []node.Node{ Adaptations: []node.Node{
&stmt.TraitUseAlias{ &stmt.TraitUseAlias{
Ref: &stmt.TraitMethodRef{ Ref: &stmt.TraitMethodRef{
@ -205,6 +210,7 @@ func TestTraitsUseAliasModifier(t *testing.T) {
}, },
}, },
}, },
},
} }
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php") php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
@ -221,7 +227,7 @@ func TestTraitsUseAliasModifier(t *testing.T) {
func TestTraitsUseAdaptions(t *testing.T) { func TestTraitsUseAdaptions(t *testing.T) {
src := `<? class Foo { use Bar, Baz { Bar::one insteadof Baz, Quux; Baz::one as two; } }` src := `<? class Foo { use Bar, Baz { Bar::one insteadof Baz, Quux; Baz::one as two; } }`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Class{ &stmt.Class{
PhpDocComment: "", PhpDocComment: "",
@ -240,6 +246,7 @@ func TestTraitsUseAdaptions(t *testing.T) {
}, },
}, },
}, },
TraitAdaptationList: &stmt.TraitAdaptationList{
Adaptations: []node.Node{ Adaptations: []node.Node{
&stmt.TraitUsePrecedence{ &stmt.TraitUsePrecedence{
Ref: &stmt.TraitMethodRef{ Ref: &stmt.TraitMethodRef{
@ -279,6 +286,7 @@ func TestTraitsUseAdaptions(t *testing.T) {
}, },
}, },
}, },
},
} }
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php") php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")

View File

@ -17,7 +17,7 @@ func TestTry(t *testing.T) {
try {} try {}
` `
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Try{ &stmt.Try{
Stmts: []node.Node{}, Stmts: []node.Node{},
@ -42,7 +42,7 @@ func TestTryCatch(t *testing.T) {
try {} catch (Exception $e) {} try {} catch (Exception $e) {}
` `
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Try{ &stmt.Try{
Stmts: []node.Node{}, Stmts: []node.Node{},
@ -81,7 +81,7 @@ func TestPhp7TryCatch(t *testing.T) {
try {} catch (Exception|RuntimeException $e) {} try {} catch (Exception|RuntimeException $e) {}
` `
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Try{ &stmt.Try{
Stmts: []node.Node{}, Stmts: []node.Node{},
@ -120,7 +120,7 @@ func TestTryCatchCatch(t *testing.T) {
try {} catch (Exception $e) {} catch (RuntimeException $e) {} try {} catch (Exception $e) {} catch (RuntimeException $e) {}
` `
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Try{ &stmt.Try{
Stmts: []node.Node{}, Stmts: []node.Node{},
@ -172,7 +172,7 @@ func TestTryCatchFinally(t *testing.T) {
try {} catch (Exception $e) {} finally {} try {} catch (Exception $e) {} finally {}
` `
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Try{ &stmt.Try{
Stmts: []node.Node{}, Stmts: []node.Node{},
@ -212,7 +212,7 @@ func TestTryCatchFinally(t *testing.T) {
func TestTryCatchCatchCatch(t *testing.T) { func TestTryCatchCatchCatch(t *testing.T) {
src := `<? try {} catch (Exception $e) {} catch (\RuntimeException $e) {} catch (namespace\AdditionException $e) {}` src := `<? try {} catch (Exception $e) {} catch (\RuntimeException $e) {} catch (namespace\AdditionException $e) {}`
expected := &stmt.StmtList{ expected := &node.Root{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Try{ &stmt.Try{
Stmts: []node.Node{}, Stmts: []node.Node{},

Some files were not shown because too many files have changed in this diff Show More