diff --git a/internal/php5/parser_test.go b/internal/php5/parser_test.go index 889fb3c..928496a 100644 --- a/internal/php5/parser_test.go +++ b/internal/php5/parser_test.go @@ -11513,14 +11513,14 @@ func TestStmtContinue(t *testing.T) { }, }, }, - Expr: &ast.ParserBrackets{ + Expr: &ast.ExprBrackets{ Position: &position.Position{ StartLine: 1, EndLine: 1, StartPos: 23, EndPos: 26, }, - OpenBracketTkn: &token.Token{ + OpenParenthesisTkn: &token.Token{ ID: token.ID(40), Value: []byte("("), Position: &position.Position{ @@ -11530,7 +11530,7 @@ func TestStmtContinue(t *testing.T) { EndPos: 24, }, }, - Child: &ast.ScalarLnumber{ + Expr: &ast.ScalarLnumber{ Position: &position.Position{ StartLine: 1, EndLine: 1, @@ -11549,7 +11549,7 @@ func TestStmtContinue(t *testing.T) { }, Value: []byte("3"), }, - CloseBracketTkn: &token.Token{ + CloseParenthesisTkn: &token.Token{ ID: token.ID(41), Value: []byte(")"), Position: &position.Position{ @@ -12598,14 +12598,14 @@ func TestStmtEcho_Parenthesis(t *testing.T) { }, }, Exprs: []ast.Vertex{ - &ast.ParserBrackets{ + &ast.ExprBrackets{ Position: &position.Position{ StartLine: 1, EndLine: 1, StartPos: 7, EndPos: 11, }, - OpenBracketTkn: &token.Token{ + OpenParenthesisTkn: &token.Token{ ID: token.ID(40), Value: []byte("("), Position: &position.Position{ @@ -12615,7 +12615,7 @@ func TestStmtEcho_Parenthesis(t *testing.T) { EndPos: 8, }, }, - Child: &ast.ExprVariable{ + Expr: &ast.ExprVariable{ Position: &position.Position{ StartLine: 1, EndLine: 1, @@ -12642,7 +12642,7 @@ func TestStmtEcho_Parenthesis(t *testing.T) { Value: []byte("$a"), }, }, - CloseBracketTkn: &token.Token{ + CloseParenthesisTkn: &token.Token{ ID: token.ID(41), Value: []byte(")"), Position: &position.Position{ @@ -12694,14 +12694,14 @@ func TestStmtExpression(t *testing.T) { StartPos: 3, EndPos: 9, }, - Expr: &ast.ParserBrackets{ + Expr: &ast.ExprBrackets{ Position: &position.Position{ StartLine: 1, EndLine: 1, StartPos: 3, EndPos: 8, }, - OpenBracketTkn: &token.Token{ + OpenParenthesisTkn: &token.Token{ ID: token.ID(40), Value: []byte("("), Position: &position.Position{ @@ -12733,14 +12733,14 @@ func TestStmtExpression(t *testing.T) { }, }, }, - Child: &ast.ParserBrackets{ + Expr: &ast.ExprBrackets{ Position: &position.Position{ StartLine: 1, EndLine: 1, StartPos: 4, EndPos: 7, }, - OpenBracketTkn: &token.Token{ + OpenParenthesisTkn: &token.Token{ ID: token.ID(40), Value: []byte("("), Position: &position.Position{ @@ -12750,7 +12750,7 @@ func TestStmtExpression(t *testing.T) { EndPos: 5, }, }, - Child: &ast.ScalarLnumber{ + Expr: &ast.ScalarLnumber{ Position: &position.Position{ StartLine: 1, EndLine: 1, @@ -12769,7 +12769,7 @@ func TestStmtExpression(t *testing.T) { }, Value: []byte("1"), }, - CloseBracketTkn: &token.Token{ + CloseParenthesisTkn: &token.Token{ ID: token.ID(41), Value: []byte(")"), Position: &position.Position{ @@ -12780,7 +12780,7 @@ func TestStmtExpression(t *testing.T) { }, }, }, - CloseBracketTkn: &token.Token{ + CloseParenthesisTkn: &token.Token{ ID: token.ID(41), Value: []byte(")"), Position: &position.Position{ @@ -29993,14 +29993,14 @@ func TestStmtBreak(t *testing.T) { }, }, }, - Expr: &ast.ParserBrackets{ + Expr: &ast.ExprBrackets{ Position: &position.Position{ StartLine: 1, EndLine: 1, StartPos: 20, EndPos: 23, }, - OpenBracketTkn: &token.Token{ + OpenParenthesisTkn: &token.Token{ ID: token.ID(40), Value: []byte("("), Position: &position.Position{ @@ -30010,7 +30010,7 @@ func TestStmtBreak(t *testing.T) { EndPos: 21, }, }, - Child: &ast.ScalarLnumber{ + Expr: &ast.ScalarLnumber{ Position: &position.Position{ StartLine: 1, EndLine: 1, @@ -30029,7 +30029,7 @@ func TestStmtBreak(t *testing.T) { }, Value: []byte("3"), }, - CloseBracketTkn: &token.Token{ + CloseParenthesisTkn: &token.Token{ ID: token.ID(41), Value: []byte(")"), Position: &position.Position{ @@ -31402,14 +31402,14 @@ func TestExprClone_Brackets(t *testing.T) { }, }, }, - Expr: &ast.ParserBrackets{ + Expr: &ast.ExprBrackets{ Position: &position.Position{ StartLine: 1, EndLine: 1, StartPos: 8, EndPos: 12, }, - OpenBracketTkn: &token.Token{ + OpenParenthesisTkn: &token.Token{ ID: token.ID(40), Value: []byte("("), Position: &position.Position{ @@ -31419,7 +31419,7 @@ func TestExprClone_Brackets(t *testing.T) { EndPos: 9, }, }, - Child: &ast.ExprVariable{ + Expr: &ast.ExprVariable{ Position: &position.Position{ StartLine: 1, EndLine: 1, @@ -31446,7 +31446,7 @@ func TestExprClone_Brackets(t *testing.T) { Value: []byte("$a"), }, }, - CloseBracketTkn: &token.Token{ + CloseParenthesisTkn: &token.Token{ ID: token.ID(41), Value: []byte(")"), Position: &position.Position{ @@ -38335,14 +38335,14 @@ func TestExprPrint(t *testing.T) { }, }, }, - Expr: &ast.ParserBrackets{ + Expr: &ast.ExprBrackets{ Position: &position.Position{ StartLine: 1, EndLine: 1, StartPos: 8, EndPos: 12, }, - OpenBracketTkn: &token.Token{ + OpenParenthesisTkn: &token.Token{ ID: token.ID(40), Value: []byte("("), Position: &position.Position{ @@ -38352,7 +38352,7 @@ func TestExprPrint(t *testing.T) { EndPos: 9, }, }, - Child: &ast.ExprVariable{ + Expr: &ast.ExprVariable{ Position: &position.Position{ StartLine: 1, EndLine: 1, @@ -38379,7 +38379,7 @@ func TestExprPrint(t *testing.T) { Value: []byte("$a"), }, }, - CloseBracketTkn: &token.Token{ + CloseParenthesisTkn: &token.Token{ ID: token.ID(41), Value: []byte(")"), Position: &position.Position{ diff --git a/internal/php5/php5.go b/internal/php5/php5.go index 4e5baf8..ce0b005 100644 Binary files a/internal/php5/php5.go and b/internal/php5/php5.go differ diff --git a/internal/php5/php5.y b/internal/php5/php5.y index a9d05ee..245e18a 100644 --- a/internal/php5/php5.y +++ b/internal/php5/php5.y @@ -787,9 +787,9 @@ unticked_statement: $$ = &ast.StmtIf{ Position: pos, IfTkn: $1, - OpenParenthesisTkn: $2.(*ast.ParserBrackets).OpenBracketTkn, - Cond: $2.(*ast.ParserBrackets).Child, - CloseParenthesisTkn: $2.(*ast.ParserBrackets).CloseBracketTkn, + OpenParenthesisTkn: $2.(*ast.ExprBrackets).OpenParenthesisTkn, + Cond: $2.(*ast.ExprBrackets).Expr, + CloseParenthesisTkn: $2.(*ast.ExprBrackets).CloseParenthesisTkn, Stmt: $3, ElseIf: $4, Else: $5, @@ -800,9 +800,9 @@ unticked_statement: $$ = &ast.StmtIf{ Position: yylex.(*Parser).builder.NewTokensPosition($1, $8), IfTkn: $1, - OpenParenthesisTkn: $2.(*ast.ParserBrackets).OpenBracketTkn, - Cond: $2.(*ast.ParserBrackets).Child, - CloseParenthesisTkn: $2.(*ast.ParserBrackets).CloseBracketTkn, + OpenParenthesisTkn: $2.(*ast.ExprBrackets).OpenParenthesisTkn, + Cond: $2.(*ast.ExprBrackets).Expr, + CloseParenthesisTkn: $2.(*ast.ExprBrackets).CloseParenthesisTkn, ColonTkn: $3, Stmt: &ast.StmtStmtList{ Position: yylex.(*Parser).builder.NewNodeListPosition($4), @@ -817,9 +817,9 @@ unticked_statement: | T_WHILE parenthesis_expr while_statement { $3.(*ast.StmtWhile).WhileTkn = $1 - $3.(*ast.StmtWhile).OpenParenthesisTkn = $2.(*ast.ParserBrackets).OpenBracketTkn - $3.(*ast.StmtWhile).Cond = $2.(*ast.ParserBrackets).Child - $3.(*ast.StmtWhile).CloseParenthesisTkn = $2.(*ast.ParserBrackets).CloseBracketTkn + $3.(*ast.StmtWhile).OpenParenthesisTkn = $2.(*ast.ExprBrackets).OpenParenthesisTkn + $3.(*ast.StmtWhile).Cond = $2.(*ast.ExprBrackets).Expr + $3.(*ast.StmtWhile).CloseParenthesisTkn = $2.(*ast.ExprBrackets).CloseParenthesisTkn $3.(*ast.StmtWhile).Position = yylex.(*Parser).builder.NewTokenNodePosition($1, $3) $$ = $3 @@ -831,9 +831,9 @@ unticked_statement: DoTkn: $1, Stmt: $2, WhileTkn: $3, - OpenParenthesisTkn: $4.(*ast.ParserBrackets).OpenBracketTkn, - Cond: $4.(*ast.ParserBrackets).Child, - CloseParenthesisTkn: $4.(*ast.ParserBrackets).CloseBracketTkn, + OpenParenthesisTkn: $4.(*ast.ExprBrackets).OpenParenthesisTkn, + Cond: $4.(*ast.ExprBrackets).Expr, + CloseParenthesisTkn: $4.(*ast.ExprBrackets).CloseParenthesisTkn, SemiColonTkn: $5, } } @@ -857,9 +857,9 @@ unticked_statement: | T_SWITCH parenthesis_expr switch_case_list { $3.(*ast.StmtSwitch).SwitchTkn = $1 - $3.(*ast.StmtSwitch).OpenParenthesisTkn = $2.(*ast.ParserBrackets).OpenBracketTkn - $3.(*ast.StmtSwitch).Cond = $2.(*ast.ParserBrackets).Child - $3.(*ast.StmtSwitch).CloseParenthesisTkn = $2.(*ast.ParserBrackets).CloseBracketTkn + $3.(*ast.StmtSwitch).OpenParenthesisTkn = $2.(*ast.ExprBrackets).OpenParenthesisTkn + $3.(*ast.StmtSwitch).Cond = $2.(*ast.ExprBrackets).Expr + $3.(*ast.StmtSwitch).CloseParenthesisTkn = $2.(*ast.ExprBrackets).CloseParenthesisTkn $3.(*ast.StmtSwitch).Position = yylex.(*Parser).builder.NewTokenNodePosition($1, $3) $$ = $3 @@ -1717,9 +1717,9 @@ elseif_list: $$ = append($1, &ast.StmtElseIf{ Position: yylex.(*Parser).builder.NewTokenNodePosition($2, $4), ElseIfTkn: $2, - OpenParenthesisTkn: $3.(*ast.ParserBrackets).OpenBracketTkn, - Cond: $3.(*ast.ParserBrackets).Child, - CloseParenthesisTkn: $3.(*ast.ParserBrackets).CloseBracketTkn, + OpenParenthesisTkn: $3.(*ast.ExprBrackets).OpenParenthesisTkn, + Cond: $3.(*ast.ExprBrackets).Expr, + CloseParenthesisTkn: $3.(*ast.ExprBrackets).CloseParenthesisTkn, Stmt: $4, }) } @@ -1736,9 +1736,9 @@ new_elseif_list: $$ = append($1, &ast.StmtElseIf{ Position: yylex.(*Parser).builder.NewTokenNodeListPosition($2, $5), ElseIfTkn: $2, - OpenParenthesisTkn: $3.(*ast.ParserBrackets).OpenBracketTkn, - Cond: $3.(*ast.ParserBrackets).Child, - CloseParenthesisTkn: $3.(*ast.ParserBrackets).CloseBracketTkn, + OpenParenthesisTkn: $3.(*ast.ExprBrackets).OpenParenthesisTkn, + Cond: $3.(*ast.ExprBrackets).Expr, + CloseParenthesisTkn: $3.(*ast.ExprBrackets).CloseParenthesisTkn, ColonTkn: $4, Stmt: &ast.StmtStmtList{ Position: yylex.(*Parser).builder.NewNodeListPosition($5), @@ -3214,11 +3214,11 @@ expr_without_variable: } | '(' new_expr ')' instance_call { - $$ = &ast.ParserBrackets{ + $$ = &ast.ExprBrackets{ Position: yylex.(*Parser).builder.NewTokensPosition($1, $3), - OpenBracketTkn: $1, - Child: $2, - CloseBracketTkn: $3, + OpenParenthesisTkn: $1, + Expr: $2, + CloseParenthesisTkn: $3, } for _, n := range($4) { @@ -3336,9 +3336,9 @@ expr_without_variable: exit.Position = yylex.(*Parser).builder.NewTokenPosition($1) } else { exit.Position = yylex.(*Parser).builder.NewTokenNodePosition($1, $2) - exit.OpenParenthesisTkn = $2.(*ast.ParserBrackets).OpenBracketTkn - exit.Expr = $2.(*ast.ParserBrackets).Child - exit.CloseParenthesisTkn = $2.(*ast.ParserBrackets).CloseBracketTkn + exit.OpenParenthesisTkn = $2.(*ast.ExprBrackets).OpenParenthesisTkn + exit.Expr = $2.(*ast.ExprBrackets).Expr + exit.CloseParenthesisTkn = $2.(*ast.ExprBrackets).CloseParenthesisTkn } $$ = exit @@ -3916,10 +3916,10 @@ exit_expr: } | '(' ')' { - $$ = &ast.ParserBrackets{ + $$ = &ast.ExprBrackets{ Position: yylex.(*Parser).builder.NewTokensPosition($1, $2), - OpenBracketTkn: $1, - CloseBracketTkn: $2, + OpenParenthesisTkn: $1, + CloseParenthesisTkn: $2, } } | parenthesis_expr @@ -4464,11 +4464,11 @@ static_operation: } | '(' static_scalar_value ')' { - $$ = &ast.ParserBrackets{ + $$ = &ast.ExprBrackets{ Position: yylex.(*Parser).builder.NewTokensPosition($1, $3), - OpenBracketTkn: $1, - Child: $2, - CloseBracketTkn: $3, + OpenParenthesisTkn: $1, + Expr: $2, + CloseParenthesisTkn: $3, } } ; @@ -4662,20 +4662,20 @@ expr: parenthesis_expr: '(' expr ')' { - $$ = &ast.ParserBrackets{ + $$ = &ast.ExprBrackets{ Position: yylex.(*Parser).builder.NewTokensPosition($1, $3), - OpenBracketTkn: $1, - Child: $2, - CloseBracketTkn: $3, + OpenParenthesisTkn: $1, + Expr: $2, + CloseParenthesisTkn: $3, } } | '(' yield_expr ')' { - $$ = &ast.ParserBrackets{ + $$ = &ast.ExprBrackets{ Position: yylex.(*Parser).builder.NewTokensPosition($1, $3), - OpenBracketTkn: $1, - Child: $2, - CloseBracketTkn: $3, + OpenParenthesisTkn: $1, + Expr: $2, + CloseParenthesisTkn: $3, } } ; diff --git a/internal/php7/parser_test.go b/internal/php7/parser_test.go index b2bf948..4957b16 100644 --- a/internal/php7/parser_test.go +++ b/internal/php7/parser_test.go @@ -13048,14 +13048,14 @@ func TestStmtContinue(t *testing.T) { }, }, }, - Expr: &ast.ParserBrackets{ + Expr: &ast.ExprBrackets{ Position: &position.Position{ StartLine: 1, EndLine: 1, StartPos: 23, EndPos: 26, }, - OpenBracketTkn: &token.Token{ + OpenParenthesisTkn: &token.Token{ ID: token.ID(40), Value: []byte("("), Position: &position.Position{ @@ -13065,7 +13065,7 @@ func TestStmtContinue(t *testing.T) { EndPos: 24, }, }, - Child: &ast.ScalarLnumber{ + Expr: &ast.ScalarLnumber{ Position: &position.Position{ StartLine: 1, EndLine: 1, @@ -13084,7 +13084,7 @@ func TestStmtContinue(t *testing.T) { }, Value: []byte("3"), }, - CloseBracketTkn: &token.Token{ + CloseParenthesisTkn: &token.Token{ ID: token.ID(41), Value: []byte(")"), Position: &position.Position{ @@ -14133,14 +14133,14 @@ func TestStmtEcho_Parenthesis(t *testing.T) { }, }, Exprs: []ast.Vertex{ - &ast.ParserBrackets{ + &ast.ExprBrackets{ Position: &position.Position{ StartLine: 1, EndLine: 1, StartPos: 7, EndPos: 11, }, - OpenBracketTkn: &token.Token{ + OpenParenthesisTkn: &token.Token{ ID: token.ID(40), Value: []byte("("), Position: &position.Position{ @@ -14150,7 +14150,7 @@ func TestStmtEcho_Parenthesis(t *testing.T) { EndPos: 8, }, }, - Child: &ast.ExprVariable{ + Expr: &ast.ExprVariable{ Position: &position.Position{ StartLine: 1, EndLine: 1, @@ -14177,7 +14177,7 @@ func TestStmtEcho_Parenthesis(t *testing.T) { Value: []byte("$a"), }, }, - CloseBracketTkn: &token.Token{ + CloseParenthesisTkn: &token.Token{ ID: token.ID(41), Value: []byte(")"), Position: &position.Position{ @@ -33925,14 +33925,14 @@ func TestStmtBreak(t *testing.T) { }, }, }, - Expr: &ast.ParserBrackets{ + Expr: &ast.ExprBrackets{ Position: &position.Position{ StartLine: 1, EndLine: 1, StartPos: 20, EndPos: 23, }, - OpenBracketTkn: &token.Token{ + OpenParenthesisTkn: &token.Token{ ID: token.ID(40), Value: []byte("("), Position: &position.Position{ @@ -33942,7 +33942,7 @@ func TestStmtBreak(t *testing.T) { EndPos: 21, }, }, - Child: &ast.ScalarLnumber{ + Expr: &ast.ScalarLnumber{ Position: &position.Position{ StartLine: 1, EndLine: 1, @@ -33961,7 +33961,7 @@ func TestStmtBreak(t *testing.T) { }, Value: []byte("3"), }, - CloseBracketTkn: &token.Token{ + CloseParenthesisTkn: &token.Token{ ID: token.ID(41), Value: []byte(")"), Position: &position.Position{ @@ -35900,14 +35900,14 @@ func TestExprClone_Brackets(t *testing.T) { }, }, }, - Expr: &ast.ParserBrackets{ + Expr: &ast.ExprBrackets{ Position: &position.Position{ StartLine: 1, EndLine: 1, StartPos: 8, EndPos: 12, }, - OpenBracketTkn: &token.Token{ + OpenParenthesisTkn: &token.Token{ ID: token.ID(40), Value: []byte("("), Position: &position.Position{ @@ -35917,7 +35917,7 @@ func TestExprClone_Brackets(t *testing.T) { EndPos: 9, }, }, - Child: &ast.ExprVariable{ + Expr: &ast.ExprVariable{ Position: &position.Position{ StartLine: 1, EndLine: 1, @@ -35944,7 +35944,7 @@ func TestExprClone_Brackets(t *testing.T) { Value: []byte("$a"), }, }, - CloseBracketTkn: &token.Token{ + CloseParenthesisTkn: &token.Token{ ID: token.ID(41), Value: []byte(")"), Position: &position.Position{ @@ -43295,14 +43295,14 @@ func TestExprPrint(t *testing.T) { }, }, }, - Expr: &ast.ParserBrackets{ + Expr: &ast.ExprBrackets{ Position: &position.Position{ StartLine: 1, EndLine: 1, StartPos: 8, EndPos: 12, }, - OpenBracketTkn: &token.Token{ + OpenParenthesisTkn: &token.Token{ ID: token.ID(40), Value: []byte("("), Position: &position.Position{ @@ -43312,7 +43312,7 @@ func TestExprPrint(t *testing.T) { EndPos: 9, }, }, - Child: &ast.ExprVariable{ + Expr: &ast.ExprVariable{ Position: &position.Position{ StartLine: 1, EndLine: 1, @@ -43339,7 +43339,7 @@ func TestExprPrint(t *testing.T) { Value: []byte("$a"), }, }, - CloseBracketTkn: &token.Token{ + CloseParenthesisTkn: &token.Token{ ID: token.ID(41), Value: []byte(")"), Position: &position.Position{ diff --git a/internal/php7/php7.go b/internal/php7/php7.go index 72e8ddc..5e50fce 100644 Binary files a/internal/php7/php7.go and b/internal/php7/php7.go differ diff --git a/internal/php7/php7.y b/internal/php7/php7.y index dbb58b7..f388612 100644 --- a/internal/php7/php7.y +++ b/internal/php7/php7.y @@ -2990,11 +2990,11 @@ expr_without_variable: } | '(' expr ')' { - $$ = &ast.ParserBrackets{ + $$ = &ast.ExprBrackets{ Position: yylex.(*Parser).builder.NewTokensPosition($1, $3), - OpenBracketTkn: $1, - Child: $2, - CloseBracketTkn: $3, + OpenParenthesisTkn: $1, + Expr: $2, + CloseParenthesisTkn: $3, } } | new_expr @@ -3101,9 +3101,9 @@ expr_without_variable: exit.Position = yylex.(*Parser).builder.NewTokenPosition($1) } else { exit.Position = yylex.(*Parser).builder.NewTokenNodePosition($1, $2) - exit.OpenParenthesisTkn = $2.(*ast.ParserBrackets).OpenBracketTkn - exit.Expr = $2.(*ast.ParserBrackets).Child - exit.CloseParenthesisTkn = $2.(*ast.ParserBrackets).CloseBracketTkn + exit.OpenParenthesisTkn = $2.(*ast.ExprBrackets).OpenParenthesisTkn + exit.Expr = $2.(*ast.ExprBrackets).Expr + exit.CloseParenthesisTkn = $2.(*ast.ExprBrackets).CloseParenthesisTkn } $$ = exit @@ -3407,11 +3407,11 @@ exit_expr: } | '(' optional_expr ')' { - $$ = &ast.ParserBrackets{ + $$ = &ast.ExprBrackets{ Position: yylex.(*Parser).builder.NewTokensPosition($1, $3), - OpenBracketTkn: $1, - Child: $2, - CloseBracketTkn: $3, + OpenParenthesisTkn: $1, + Expr: $2, + CloseParenthesisTkn: $3, } } ; @@ -3684,11 +3684,11 @@ dereferencable: } | '(' expr ')' { - $$ = &ast.ParserBrackets{ + $$ = &ast.ExprBrackets{ Position: yylex.(*Parser).builder.NewTokensPosition($1, $3), - OpenBracketTkn: $1, - Child: $2, - CloseBracketTkn: $3, + OpenParenthesisTkn: $1, + Expr: $2, + CloseParenthesisTkn: $3, } } | dereferencable_scalar @@ -3704,11 +3704,11 @@ callable_expr: } | '(' expr ')' { - $$ = &ast.ParserBrackets{ + $$ = &ast.ExprBrackets{ Position: yylex.(*Parser).builder.NewTokensPosition($1, $3), - OpenBracketTkn: $1, - Child: $2, - CloseBracketTkn: $3, + OpenParenthesisTkn: $1, + Expr: $2, + CloseParenthesisTkn: $3, } } | dereferencable_scalar diff --git a/pkg/ast/ast.go b/pkg/ast/ast.go index 5c5494a..2debd18 100644 --- a/pkg/ast/ast.go +++ b/pkg/ast/ast.go @@ -82,6 +82,7 @@ type NodeVisitor interface { ExprArrayDimFetch(n *ExprArrayDimFetch) ExprArrayItem(n *ExprArrayItem) ExprArrowFunction(n *ExprArrowFunction) + ExprBrackets(n *ExprBrackets) ExprBitwiseNot(n *ExprBitwiseNot) ExprBooleanNot(n *ExprBooleanNot) ExprClassConstFetch(n *ExprClassConstFetch) diff --git a/pkg/ast/node.go b/pkg/ast/node.go index 290a51a..cf71bcd 100644 --- a/pkg/ast/node.go +++ b/pkg/ast/node.go @@ -1243,6 +1243,21 @@ func (n *ExprBooleanNot) GetPosition() *position.Position { return n.Position } +type ExprBrackets struct { + Position *position.Position + OpenParenthesisTkn *token.Token + Expr Vertex + CloseParenthesisTkn *token.Token +} + +func (n *ExprBrackets) Accept(v NodeVisitor) { + v.ExprBrackets(n) +} + +func (n *ExprBrackets) GetPosition() *position.Position { + return n.Position +} + // ExprClassConstFetch node type ExprClassConstFetch struct { Position *position.Position diff --git a/pkg/ast/traverser/dfs.go b/pkg/ast/traverser/dfs.go index 7538f71..c41d2eb 100644 --- a/pkg/ast/traverser/dfs.go +++ b/pkg/ast/traverser/dfs.go @@ -1109,6 +1109,18 @@ func (t *DFS) Traverse(n ast.Vertex) { t.Traverse(nn.Expr) t.visitor.Leave("Expr", true) } + case *ast.ExprBrackets: + if nn == nil { + return + } + if !t.visitor.EnterNode(nn) { + return + } + if nn.Expr != nil { + t.visitor.Enter("Expr", true) + t.Traverse(nn.Expr) + t.visitor.Leave("Expr", true) + } case *ast.ExprClassConstFetch: if nn == nil { return diff --git a/pkg/ast/visitor/dumper.go b/pkg/ast/visitor/dumper.go index dde094a..b9c8af6 100644 --- a/pkg/ast/visitor/dumper.go +++ b/pkg/ast/visitor/dumper.go @@ -1108,6 +1108,19 @@ func (v *Dumper) ExprBooleanNot(n *ast.ExprBooleanNot) { v.print(v.indent, "},\n") } +func (v *Dumper) ExprBrackets(n *ast.ExprBrackets) { + v.print(0, "&ast.ExprBrackets{\n") + v.indent++ + + v.dumpPosition(n.Position) + v.dumpToken("OpenParenthesisTkn", n.OpenParenthesisTkn) + v.dumpVertex("Expr", n.Expr) + v.dumpToken("CloseParenthesisTkn", n.CloseParenthesisTkn) + + v.indent-- + v.print(v.indent, "},\n") +} + func (v *Dumper) ExprClassConstFetch(n *ast.ExprClassConstFetch) { v.print(0, "&ast.ExprClassConstFetch{\n") v.indent++ diff --git a/pkg/ast/visitor/formatter.go b/pkg/ast/visitor/formatter.go index c698359..193f8b1 100644 --- a/pkg/ast/visitor/formatter.go +++ b/pkg/ast/visitor/formatter.go @@ -1134,6 +1134,12 @@ func (f *formatter) ExprBooleanNot(n *ast.ExprBooleanNot) { n.Expr.Accept(f) } +func (f *formatter) ExprBrackets(n *ast.ExprBrackets) { + n.OpenParenthesisTkn = f.newToken('(', []byte("(")) + n.Expr.Accept(f) + n.CloseParenthesisTkn = f.newToken(')', []byte(")")) +} + func (f *formatter) ExprClassConstFetch(n *ast.ExprClassConstFetch) { n.Class.Accept(f) n.DoubleColonTkn = f.newToken(token.T_PAAMAYIM_NEKUDOTAYIM, []byte("::")) diff --git a/pkg/ast/visitor/formatter_test.go b/pkg/ast/visitor/formatter_test.go index 8180fdb..fd92dff 100644 --- a/pkg/ast/visitor/formatter_test.go +++ b/pkg/ast/visitor/formatter_test.go @@ -3571,6 +3571,31 @@ func TestFormatter_ExprBooleanNot(t *testing.T) { } } +func TestFormatter_ExprBrackets(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprBrackets{ + Expr: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `($foo)` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + func TestFormatter_ExprClassConstFetch(t *testing.T) { o := bytes.NewBufferString("") diff --git a/pkg/ast/visitor/null.go b/pkg/ast/visitor/null.go index 6623a75..8b8cd62 100644 --- a/pkg/ast/visitor/null.go +++ b/pkg/ast/visitor/null.go @@ -270,6 +270,10 @@ func (v *Null) ExprBooleanNot(_ *ast.ExprBooleanNot) { // do nothing } +func (v *Null) ExprBrackets(_ *ast.ExprBrackets) { + // do nothing +} + func (v *Null) ExprClassConstFetch(_ *ast.ExprClassConstFetch) { // do nothing } diff --git a/pkg/ast/visitor/printer.go b/pkg/ast/visitor/printer.go index 23b23f1..a57729e 100644 --- a/pkg/ast/visitor/printer.go +++ b/pkg/ast/visitor/printer.go @@ -668,6 +668,12 @@ func (p *printer) ExprBooleanNot(n *ast.ExprBooleanNot) { p.printNode(n.Expr) } +func (p *printer) ExprBrackets(n *ast.ExprBrackets) { + p.printToken(n.OpenParenthesisTkn, nil) + p.printNode(n.Expr) + p.printToken(n.CloseParenthesisTkn, nil) +} + func (p *printer) ExprClassConstFetch(n *ast.ExprClassConstFetch) { p.printNode(n.Class) p.printToken(n.DoubleColonTkn, []byte("::")) diff --git a/pkg/ast/visitor/printer_test.go b/pkg/ast/visitor/printer_test.go index 621ea27..1d4bcb1 100644 --- a/pkg/ast/visitor/printer_test.go +++ b/pkg/ast/visitor/printer_test.go @@ -1696,6 +1696,25 @@ func TestPrinterPrintExprBooleanNot(t *testing.T) { } } +func TestPrinterPrintExprBracket(t *testing.T) { + o := bytes.NewBufferString("") + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n := &ast.ExprBooleanNot{ + Expr: &ast.ExprVariable{ + VarName: &ast.Identifier{Value: []byte("$var")}, + }, + } + n.Accept(p) + + expected := `!$var` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + func TestPrinterPrintExprClassConstFetch(t *testing.T) { o := bytes.NewBufferString("")