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,21 +122,27 @@ nodes := &stmt.StmtList{
&name.NamePart{Value: "Bar"},
},
},
Extends: &name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Baz"},
},
},
Extends: &stmt.ClassExtends{
ClassName: &name.Name{
Parts: []node.Node{
&name.NamePart{
Value: "Baz"
},
},
},
},
Stmts: []node.Node{
&stmt.ClassMethod{
Modifiers: []node.Node{
&node.Identifier{Value: "public"},
},
MethodName: &node.Identifier{Value: "greet"},
Stmts: []node.Node{
&stmt.Echo{
Exprs: []node.Node{
&scalar.String{Value: "'Hello world'"},
Stmt: &stmt.StmtList{
Stmts: []node.Node{
&stmt.Echo{
Exprs: []node.Node{
&scalar.String{Value: "'Hello world'"},
},
},
},
},

View File

@ -1,16 +1,40 @@
package comment
import "github.com/z7zmey/php-parser/node"
import (
"github.com/z7zmey/php-parser/position"
)
// Comment represents comment lines in the code
type Comment interface {
String() string
// Comment aggrigates information about comment /**
type Comment struct {
value string
position *position.Position
tokenName TokenName
}
// Comments a collection of comment groups assigned to nodes
type Comments map[node.Node][]Comment
// AddComments add comment group to the collection
func (c Comments) AddComments(node node.Node, comments []Comment) {
c[node] = append(c[node], comments...)
// NewComment - Comment constructor
func NewComment(value string, pos *position.Position) *Comment {
return &Comment{
value,
pos,
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 (
"testing"
"github.com/z7zmey/php-parser/position"
"github.com/z7zmey/php-parser/comment"
"github.com/z7zmey/php-parser/node"
)
func TestComments(t *testing.T) {
n := node.NewIdentifier("test")
func TestCommentGetPosition(t *testing.T) {
expected := position.NewPosition(0, 0, 0, 0)
commentGroup := []comment.Comment{
comment.NewDocComment("/** hello world */"),
comment.NewPlainComment("// hello world"),
}
comment := comment.NewComment("/** hello world */", expected)
comments := comment.Comments{}
comments.AddComments(n, commentGroup)
actual := comment.Position()
if comments[n][0].String() != "/** hello world */" {
t.Errorf("expected and actual are not equal\n")
}
if comments[n][1].String() != "// hello world" {
if expected != actual {
t.Errorf("expected and actual are not equal\n")
}
}
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")
}
}

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"
"github.com/z7zmey/php-parser/position"
"github.com/z7zmey/php-parser/token"
"github.com/z7zmey/php-parser/scanner"
)
// Error parsing error
type Error struct {
Msg string
Pos position.Position
Pos *position.Position
}
// NewError creates and returns new Error
func NewError(msg string, t token.Token) *Error {
func NewError(msg string, t *scanner.Token) *Error {
return &Error{
Msg: msg,
Pos: position.Position{
StartLine: t.StartLine,
EndLine: t.EndLine,
StartPos: t.StartPos,
EndPos: t.EndPos,
},
Pos: t.Position(),
}
}

View File

@ -7,7 +7,7 @@ import (
"github.com/z7zmey/php-parser/position"
"github.com/z7zmey/php-parser/errors"
"github.com/z7zmey/php-parser/token"
"github.com/z7zmey/php-parser/scanner"
"github.com/kylelemons/godebug/pretty"
)
@ -26,37 +26,22 @@ func assertEqual(t *testing.T, expected interface{}, actual interface{}) {
}
func TestConstructor(t *testing.T) {
token := token.Token{
Value: "test",
StartLine: 1,
EndLine: 2,
StartPos: 3,
EndPos: 4,
}
pos := position.NewPosition(1, 2, 3, 4)
token := scanner.NewToken(`test`, pos)
actual := errors.NewError("message", token)
expected := &errors.Error{
Msg: "message",
Pos: position.Position{
StartLine: 1,
EndLine: 2,
StartPos: 3,
EndPos: 4,
},
Pos: pos,
}
assertEqual(t, expected, actual)
}
func TestPrint(t *testing.T) {
token := token.Token{
Value: "test",
StartLine: 1,
EndLine: 2,
StartPos: 3,
EndPos: 4,
}
pos := position.NewPosition(1, 2, 3, 4)
token := scanner.NewToken(`test`, pos)
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) {
src := `<? $a =& $b;`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &assign.Reference{
@ -57,7 +57,7 @@ func TestReference(t *testing.T) {
func TestReferenceNew(t *testing.T) {
src := `<? $a =& new Foo;`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &assign.Reference{
@ -88,7 +88,7 @@ func TestReferenceNew(t *testing.T) {
func TestReferenceArgs(t *testing.T) {
src := `<? $a =& new Foo($b);`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &assign.Reference{
@ -99,11 +99,13 @@ func TestReferenceArgs(t *testing.T) {
&name.NamePart{Value: "Foo"},
},
},
Arguments: []node.Node{
&node.Argument{
Variadic: false,
IsReference: false,
Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
ArgumentList: &node.ArgumentList{
Arguments: []node.Node{
&node.Argument{
Variadic: false,
IsReference: false,
Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
},
},
},
},
@ -126,7 +128,7 @@ func TestReferenceArgs(t *testing.T) {
func TestAssign(t *testing.T) {
src := `<? $a = $b;`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &assign.Assign{
@ -151,7 +153,7 @@ func TestAssign(t *testing.T) {
func TestBitwiseAnd(t *testing.T) {
src := `<? $a &= $b;`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &assign.BitwiseAnd{
@ -176,7 +178,7 @@ func TestBitwiseAnd(t *testing.T) {
func TestBitwiseOr(t *testing.T) {
src := `<? $a |= $b;`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &assign.BitwiseOr{
@ -201,7 +203,7 @@ func TestBitwiseOr(t *testing.T) {
func TestBitwiseXor(t *testing.T) {
src := `<? $a ^= $b;`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &assign.BitwiseXor{
@ -226,7 +228,7 @@ func TestBitwiseXor(t *testing.T) {
func TestConcat(t *testing.T) {
src := `<? $a .= $b;`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &assign.Concat{
@ -251,7 +253,7 @@ func TestConcat(t *testing.T) {
func TestDiv(t *testing.T) {
src := `<? $a /= $b;`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &assign.Div{
@ -276,7 +278,7 @@ func TestDiv(t *testing.T) {
func TestMinus(t *testing.T) {
src := `<? $a -= $b;`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &assign.Minus{
@ -301,7 +303,7 @@ func TestMinus(t *testing.T) {
func TestMod(t *testing.T) {
src := `<? $a %= $b;`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &assign.Mod{
@ -326,7 +328,7 @@ func TestMod(t *testing.T) {
func TestMul(t *testing.T) {
src := `<? $a *= $b;`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &assign.Mul{
@ -351,7 +353,7 @@ func TestMul(t *testing.T) {
func TestPlus(t *testing.T) {
src := `<? $a += $b;`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &assign.Plus{
@ -376,7 +378,7 @@ func TestPlus(t *testing.T) {
func TestPow(t *testing.T) {
src := `<? $a **= $b;`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &assign.Pow{
@ -401,7 +403,7 @@ func TestPow(t *testing.T) {
func TestShiftLeft(t *testing.T) {
src := `<? $a <<= $b;`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &assign.ShiftLeft{
@ -426,7 +428,7 @@ func TestShiftLeft(t *testing.T) {
func TestShiftRight(t *testing.T) {
src := `<? $a >>= $b;`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &assign.ShiftRight{

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -7,17 +7,17 @@ import (
// MethodCall node
type MethodCall struct {
Variable node.Node
Method node.Node
Arguments []node.Node
Variable node.Node
Method node.Node
ArgumentList *node.ArgumentList
}
// 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{
Variable,
Method,
Arguments,
ArgumentList,
}
}
@ -43,13 +43,9 @@ func (n *MethodCall) Walk(v walker.Visitor) {
n.Method.Walk(vv)
}
if n.Arguments != nil {
vv := v.GetChildrenVisitor("Arguments")
for _, nn := range n.Arguments {
if nn != nil {
nn.Walk(vv)
}
}
if n.ArgumentList != nil {
vv := v.GetChildrenVisitor("ArgumentList")
n.ArgumentList.Walk(vv)
}
v.LeaveNode(n)

View File

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

@ -7,17 +7,17 @@ import (
// StaticCall node
type StaticCall struct {
Class node.Node
Call node.Node
Arguments []node.Node
Class node.Node
Call node.Node
ArgumentList *node.ArgumentList
}
// 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{
Class,
Call,
Arguments,
ArgumentList,
}
}
@ -43,13 +43,9 @@ func (n *StaticCall) Walk(v walker.Visitor) {
n.Call.Walk(vv)
}
if n.Arguments != nil {
vv := v.GetChildrenVisitor("Arguments")
for _, nn := range n.Arguments {
if nn != nil {
nn.Walk(vv)
}
}
if n.ArgumentList != nil {
vv := v.GetChildrenVisitor("ArgumentList")
n.ArgumentList.Walk(vv)
}
v.LeaveNode(n)

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -20,7 +20,7 @@ import (
func TestFunctionCall(t *testing.T) {
src := `<? foo();`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &expr.FunctionCall{
@ -29,7 +29,7 @@ func TestFunctionCall(t *testing.T) {
&name.NamePart{Value: "foo"},
},
},
Arguments: []node.Node{},
ArgumentList: &node.ArgumentList{},
},
},
},
@ -49,7 +49,7 @@ func TestFunctionCall(t *testing.T) {
func TestFunctionCallRelative(t *testing.T) {
src := `<? namespace\foo();`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &expr.FunctionCall{
@ -58,7 +58,7 @@ func TestFunctionCallRelative(t *testing.T) {
&name.NamePart{Value: "foo"},
},
},
Arguments: []node.Node{},
ArgumentList: &node.ArgumentList{},
},
},
},
@ -78,7 +78,7 @@ func TestFunctionCallRelative(t *testing.T) {
func TestFunctionFullyQualified(t *testing.T) {
src := `<? \foo([]);`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &expr.FunctionCall{
@ -87,12 +87,14 @@ func TestFunctionFullyQualified(t *testing.T) {
&name.NamePart{Value: "foo"},
},
},
Arguments: []node.Node{
&node.Argument{
Variadic: false,
IsReference: false,
Expr: &expr.ShortArray{
Items: []node.Node{},
ArgumentList: &node.ArgumentList{
Arguments: []node.Node{
&node.Argument{
Variadic: false,
IsReference: false,
Expr: &expr.ShortArray{
Items: []node.Node{},
},
},
},
},
@ -115,17 +117,19 @@ func TestFunctionFullyQualified(t *testing.T) {
func TestFunctionCallVar(t *testing.T) {
src := `<? $foo(yield $a);`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &expr.FunctionCall{
Function: &expr.Variable{VarName: &node.Identifier{Value: "foo"}},
Arguments: []node.Node{
&node.Argument{
Variadic: false,
IsReference: false,
Expr: &expr.Yield{
Value: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
ArgumentList: &node.ArgumentList{
Arguments: []node.Node{
&node.Argument{
Variadic: false,
IsReference: false,
Expr: &expr.Yield{
Value: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
},
},
},
},
@ -148,7 +152,7 @@ func TestFunctionCallVar(t *testing.T) {
func TestFunctionCallExprArg(t *testing.T) {
src := `<? ceil($foo/3);`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &expr.FunctionCall{
@ -157,13 +161,15 @@ func TestFunctionCallExprArg(t *testing.T) {
&name.NamePart{Value: "ceil"},
},
},
Arguments: []node.Node{
&node.Argument{
Variadic: false,
IsReference: false,
Expr: &binary.Div{
Left: &expr.Variable{VarName: &node.Identifier{Value: "foo"}},
Right: &scalar.Lnumber{Value: "3"},
ArgumentList: &node.ArgumentList{
Arguments: []node.Node{
&node.Argument{
Variadic: false,
IsReference: false,
Expr: &binary.Div{
Left: &expr.Variable{VarName: &node.Identifier{Value: "foo"}},
Right: &scalar.Lnumber{Value: "3"},
},
},
},
},

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -16,7 +16,7 @@ import (
func TestYield(t *testing.T) {
src := `<? yield;`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &expr.Yield{},
@ -38,7 +38,7 @@ func TestYield(t *testing.T) {
func TestYieldVal(t *testing.T) {
src := `<? yield $a;`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &expr.Yield{
@ -62,7 +62,7 @@ func TestYieldVal(t *testing.T) {
func TestYieldKeyVal(t *testing.T) {
src := `<? yield $a => $b;`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &expr.Yield{
@ -87,7 +87,7 @@ func TestYieldKeyVal(t *testing.T) {
func TestYieldExpr(t *testing.T) {
src := `<? yield 1;`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &expr.Yield{
@ -111,7 +111,7 @@ func TestYieldExpr(t *testing.T) {
func TestYieldKeyExpr(t *testing.T) {
src := `<? yield $a => 1;`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &expr.Yield{
@ -136,7 +136,7 @@ func TestYieldKeyExpr(t *testing.T) {
func TestYieldFrom(t *testing.T) {
src := `<? yield from $a;`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
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) {
src := `<? foo();`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &expr.FunctionCall{
Function: &name.Name{
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) {
src := `<? \foo();`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &expr.FunctionCall{
Function: &name.FullyQualified{
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) {
src := `<? namespace\foo();`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &expr.FunctionCall{
Function: &name.Relative{
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) {
src := `<? "test $var";`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &scalar.Encapsed{
@ -43,7 +43,7 @@ func TestSimpleVar(t *testing.T) {
func TestSimpleVarOneChar(t *testing.T) {
src := `<? "test $a";`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &scalar.Encapsed{
@ -70,7 +70,7 @@ func TestSimpleVarOneChar(t *testing.T) {
func TestSimpleVarEndsEcapsed(t *testing.T) {
src := `<? "test $var\"";`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &scalar.Encapsed{
@ -98,7 +98,7 @@ func TestSimpleVarEndsEcapsed(t *testing.T) {
func TestStringVarCurveOpen(t *testing.T) {
src := `<? "=$a{$b}";`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &scalar.Encapsed{
@ -126,7 +126,7 @@ func TestStringVarCurveOpen(t *testing.T) {
func TestSimpleVarPropertyFetch(t *testing.T) {
src := `<? "test $foo->bar()";`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &scalar.Encapsed{
@ -157,7 +157,7 @@ func TestSimpleVarPropertyFetch(t *testing.T) {
func TestDollarOpenCurlyBraces(t *testing.T) {
src := `<? "test ${foo}";`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &scalar.Encapsed{
@ -184,7 +184,7 @@ func TestDollarOpenCurlyBraces(t *testing.T) {
func TestDollarOpenCurlyBracesDimNumber(t *testing.T) {
src := `<? "test ${foo[0]}";`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &scalar.Encapsed{
@ -214,16 +214,16 @@ func TestDollarOpenCurlyBracesDimNumber(t *testing.T) {
func TestCurlyOpenMethodCall(t *testing.T) {
src := `<? "test {$foo->bar()}";`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &scalar.Encapsed{
Parts: []node.Node{
&scalar.EncapsedStringPart{Value: "test "},
&expr.MethodCall{
Variable: &expr.Variable{VarName: &node.Identifier{Value: "foo"}},
Method: &node.Identifier{Value: "bar"},
Arguments: []node.Node{},
Variable: &expr.Variable{VarName: &node.Identifier{Value: "foo"}},
Method: &node.Identifier{Value: "bar"},
ArgumentList: &node.ArgumentList{},
},
},
},

View File

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

View File

@ -15,7 +15,7 @@ func TestMagicConstant(t *testing.T) {
// TODO: test all magic constants
src := `<? __DIR__;`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
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) {
src := `<? 1234567890123456789;`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &scalar.Lnumber{Value: "1234567890123456789"},
@ -51,7 +51,7 @@ func TestLNumber(t *testing.T) {
func TestDNumber(t *testing.T) {
src := `<? 12345678901234567890;`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &scalar.Dnumber{Value: "12345678901234567890"},
@ -73,7 +73,7 @@ func TestDNumber(t *testing.T) {
func TestFloat(t *testing.T) {
src := `<? 0.;`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &scalar.Dnumber{Value: "0."},
@ -95,7 +95,7 @@ func TestFloat(t *testing.T) {
func TestBinaryLNumber(t *testing.T) {
src := `<? 0b0111111111111111111111111111111111111111111111111111111111111111;`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &scalar.Lnumber{Value: "0b0111111111111111111111111111111111111111111111111111111111111111"},
@ -117,7 +117,7 @@ func TestBinaryLNumber(t *testing.T) {
func TestBinaryDNumber(t *testing.T) {
src := `<? 0b1111111111111111111111111111111111111111111111111111111111111111;`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &scalar.Dnumber{Value: "0b1111111111111111111111111111111111111111111111111111111111111111"},
@ -139,7 +139,7 @@ func TestBinaryDNumber(t *testing.T) {
func TestHLNumber(t *testing.T) {
src := `<? 0x007111111111111111;`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &scalar.Lnumber{Value: "0x007111111111111111"},
@ -161,7 +161,7 @@ func TestHLNumber(t *testing.T) {
func TestHDNumber(t *testing.T) {
src := `<? 0x8111111111111111;`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &scalar.Dnumber{Value: "0x8111111111111111"},

View File

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

View File

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

View File

@ -7,15 +7,15 @@ import (
// AltSwitch node
type AltSwitch struct {
Cond node.Node
Cases []node.Node
Cond node.Node
CaseList *CaseList
}
// NewAltSwitch node constructor
func NewAltSwitch(Cond node.Node, Cases []node.Node) *AltSwitch {
func NewAltSwitch(Cond node.Node, CaseList *CaseList) *AltSwitch {
return &AltSwitch{
Cond,
Cases,
CaseList,
}
}
@ -36,13 +36,9 @@ func (n *AltSwitch) Walk(v walker.Visitor) {
n.Cond.Walk(vv)
}
if n.Cases != nil {
vv := v.GetChildrenVisitor("Cases")
for _, nn := range n.Cases {
if nn != nil {
nn.Walk(vv)
}
}
if n.CaseList != nil {
vv := v.GetChildrenVisitor("CaseList")
n.CaseList.Walk(vv)
}
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
ClassName node.Node
Modifiers []node.Node
Args []node.Node
Extends node.Node
Implements []node.Node
ArgumentList *node.ArgumentList
Extends *ClassExtends
Implements *ClassImplements
Stmts []node.Node
}
// 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{
PhpDocComment,
ClassName,
Modifiers,
Args,
ArgumentList,
Extends,
Implements,
Stmts,
@ -57,13 +57,9 @@ func (n *Class) Walk(v walker.Visitor) {
}
}
if n.Args != nil {
vv := v.GetChildrenVisitor("Args")
for _, nn := range n.Args {
if nn != nil {
nn.Walk(vv)
}
}
if n.ArgumentList != nil {
vv := v.GetChildrenVisitor("ArgumentList")
n.ArgumentList.Walk(vv)
}
if n.Extends != nil {
@ -73,11 +69,7 @@ func (n *Class) Walk(v walker.Visitor) {
if n.Implements != nil {
vv := v.GetChildrenVisitor("Implements")
for _, nn := range n.Implements {
if nn != nil {
nn.Walk(vv)
}
}
n.Implements.Walk(vv)
}
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
Params []node.Node
ReturnType node.Node
Stmts []node.Node
Stmt node.Node
}
// 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{
ReturnsRef,
PhpDocComment,
@ -25,7 +25,7 @@ func NewClassMethod(MethodName node.Node, Modifiers []node.Node, ReturnsRef bool
Modifiers,
Params,
ReturnType,
Stmts,
Stmt,
}
}
@ -72,13 +72,9 @@ func (n *ClassMethod) Walk(v walker.Visitor) {
n.ReturnType.Walk(vv)
}
if n.Stmts != nil {
vv := v.GetChildrenVisitor("Stmts")
for _, nn := range n.Stmts {
if nn != nil {
nn.Walk(vv)
}
}
if n.Stmt != nil {
vv := v.GetChildrenVisitor("Stmt")
n.Stmt.Walk(vv)
}
v.LeaveNode(n)

View File

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

View File

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

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

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -21,21 +21,23 @@ func TestAltSwitch(t *testing.T) {
endswitch;
`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.AltSwitch{
Cond: &scalar.Lnumber{Value: "1"},
Cases: []node.Node{
&stmt.Case{
Cond: &scalar.Lnumber{Value: "1"},
Stmts: []node.Node{},
},
&stmt.Default{
Stmts: []node.Node{},
},
&stmt.Case{
Cond: &scalar.Lnumber{Value: "2"},
Stmts: []node.Node{},
CaseList: &stmt.CaseList{
Cases: []node.Node{
&stmt.Case{
Cond: &scalar.Lnumber{Value: "1"},
Stmts: []node.Node{},
},
&stmt.Default{
Stmts: []node.Node{},
},
&stmt.Case{
Cond: &scalar.Lnumber{Value: "2"},
Stmts: []node.Node{},
},
},
},
},
@ -61,18 +63,20 @@ func TestAltSwitchSemicolon(t *testing.T) {
endswitch;
`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.AltSwitch{
Cond: &scalar.Lnumber{Value: "1"},
Cases: []node.Node{
&stmt.Case{
Cond: &scalar.Lnumber{Value: "1"},
Stmts: []node.Node{},
},
&stmt.Case{
Cond: &scalar.Lnumber{Value: "2"},
Stmts: []node.Node{},
CaseList: &stmt.CaseList{
Cases: []node.Node{
&stmt.Case{
Cond: &scalar.Lnumber{Value: "1"},
Stmts: []node.Node{},
},
&stmt.Case{
Cond: &scalar.Lnumber{Value: "2"},
Stmts: []node.Node{},
},
},
},
},
@ -98,21 +102,23 @@ func TestSwitch(t *testing.T) {
}
`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Switch{
Cond: &scalar.Lnumber{Value: "1"},
Cases: []node.Node{
&stmt.Case{
Cond: &scalar.Lnumber{Value: "1"},
Stmts: []node.Node{
&stmt.Break{},
CaseList: &stmt.CaseList{
Cases: []node.Node{
&stmt.Case{
Cond: &scalar.Lnumber{Value: "1"},
Stmts: []node.Node{
&stmt.Break{},
},
},
},
&stmt.Case{
Cond: &scalar.Lnumber{Value: "2"},
Stmts: []node.Node{
&stmt.Break{},
&stmt.Case{
Cond: &scalar.Lnumber{Value: "2"},
Stmts: []node.Node{
&stmt.Break{},
},
},
},
},
@ -139,21 +145,23 @@ func TestSwitchSemicolon(t *testing.T) {
}
`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Switch{
Cond: &scalar.Lnumber{Value: "1"},
Cases: []node.Node{
&stmt.Case{
Cond: &scalar.Lnumber{Value: "1"},
Stmts: []node.Node{
&stmt.Break{},
CaseList: &stmt.CaseList{
Cases: []node.Node{
&stmt.Case{
Cond: &scalar.Lnumber{Value: "1"},
Stmts: []node.Node{
&stmt.Break{},
},
},
},
&stmt.Case{
Cond: &scalar.Lnumber{Value: "2"},
Stmts: []node.Node{
&stmt.Break{},
&stmt.Case{
Cond: &scalar.Lnumber{Value: "2"},
Stmts: []node.Node{
&stmt.Break{},
},
},
},
},

View File

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

View File

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

View File

@ -2,9 +2,10 @@ package stmt_test
import (
"bytes"
"github.com/z7zmey/php-parser/node/name"
"testing"
"github.com/z7zmey/php-parser/node/name"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/node/stmt"
"github.com/z7zmey/php-parser/php5"
@ -14,7 +15,7 @@ import (
func TestTraitUse(t *testing.T) {
src := `<? class Foo { use Bar; }`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Class{
PhpDocComment: "",
@ -48,7 +49,7 @@ func TestTraitUse(t *testing.T) {
func TestTraitsUse(t *testing.T) {
src := `<? class Foo { use Bar, Baz; }`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Class{
PhpDocComment: "",
@ -87,7 +88,7 @@ func TestTraitsUse(t *testing.T) {
func TestTraitsUseEmptyAdaptations(t *testing.T) {
src := `<? class Foo { use Bar, Baz {} }`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Class{
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) {
src := `<? class Foo { use Bar, Baz { one as public; } }`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Class{
PhpDocComment: "",
@ -145,12 +147,14 @@ func TestTraitsUseModifier(t *testing.T) {
},
},
},
Adaptations: []node.Node{
&stmt.TraitUseAlias{
Ref: &stmt.TraitMethodRef{
Method: &node.Identifier{Value: "one"},
TraitAdaptationList: &stmt.TraitAdaptationList{
Adaptations: []node.Node{
&stmt.TraitUseAlias{
Ref: &stmt.TraitMethodRef{
Method: &node.Identifier{Value: "one"},
},
Modifier: &node.Identifier{Value: "public"},
},
Modifier: &node.Identifier{Value: "public"},
},
},
},
@ -173,7 +177,7 @@ func TestTraitsUseModifier(t *testing.T) {
func TestTraitsUseAliasModifier(t *testing.T) {
src := `<? class Foo { use Bar, Baz { one as public two; } }`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Class{
PhpDocComment: "",
@ -192,13 +196,15 @@ func TestTraitsUseAliasModifier(t *testing.T) {
},
},
},
Adaptations: []node.Node{
&stmt.TraitUseAlias{
Ref: &stmt.TraitMethodRef{
Method: &node.Identifier{Value: "one"},
TraitAdaptationList: &stmt.TraitAdaptationList{
Adaptations: []node.Node{
&stmt.TraitUseAlias{
Ref: &stmt.TraitMethodRef{
Method: &node.Identifier{Value: "one"},
},
Modifier: &node.Identifier{Value: "public"},
Alias: &node.Identifier{Value: "two"},
},
Modifier: &node.Identifier{Value: "public"},
Alias: &node.Identifier{Value: "two"},
},
},
},
@ -221,7 +227,7 @@ func TestTraitsUseAliasModifier(t *testing.T) {
func TestTraitsUseAdaptions(t *testing.T) {
src := `<? class Foo { use Bar, Baz { Bar::one insteadof Baz, Quux; Baz::one as two; } }`
expected := &stmt.StmtList{
expected := &node.Root{
Stmts: []node.Node{
&stmt.Class{
PhpDocComment: "",
@ -240,39 +246,41 @@ func TestTraitsUseAdaptions(t *testing.T) {
},
},
},
Adaptations: []node.Node{
&stmt.TraitUsePrecedence{
Ref: &stmt.TraitMethodRef{
Trait: &name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Bar"},
TraitAdaptationList: &stmt.TraitAdaptationList{
Adaptations: []node.Node{
&stmt.TraitUsePrecedence{
Ref: &stmt.TraitMethodRef{
Trait: &name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Bar"},
},
},
Method: &node.Identifier{Value: "one"},
},
Method: &node.Identifier{Value: "one"},
},
Insteadof: []node.Node{
&name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Baz"},
Insteadof: []node.Node{
&name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Baz"},
},
},
},
&name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Quux"},
&name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Quux"},
},
},
},
},
},
&stmt.TraitUseAlias{
Ref: &stmt.TraitMethodRef{
Trait: &name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Baz"},
&stmt.TraitUseAlias{
Ref: &stmt.TraitMethodRef{
Trait: &name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Baz"},
},
},
Method: &node.Identifier{Value: "one"},
},
Method: &node.Identifier{Value: "one"},
Alias: &node.Identifier{Value: "two"},
},
Alias: &node.Identifier{Value: "two"},
},
},
},

View File

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

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