[refactoring] update ast structure of "Eval", "Exit", "FunctionCall", "Include" and "IncludeOnce" nodes

This commit is contained in:
Vadym Slizov 2020-12-01 11:58:09 +02:00
parent 2d240e9475
commit 9b122de8bf
No known key found for this signature in database
GPG Key ID: AEA2A9388EF42A4A
9 changed files with 151 additions and 151 deletions

BIN
internal/php5/php5.go generated

Binary file not shown.

View File

@ -2,7 +2,6 @@
package php5 package php5
import ( import (
"bytes"
"strconv" "strconv"
"github.com/z7zmey/php-parser/internal/position" "github.com/z7zmey/php-parser/internal/position"
@ -3872,21 +3871,20 @@ expr_without_variable:
} }
| T_EXIT exit_expr | T_EXIT exit_expr
{ {
$$ = &ast.ExprExit{ast.Node{}, false, $2} exit := &ast.ExprExit{
DieTkn: $1,
if (bytes.EqualFold($1.Value, []byte("die"))) {
$$.(*ast.ExprExit).Die = true
} }
// save position
if $2 == nil { if $2 == nil {
$$.GetNode().Position = position.NewTokenPosition($1) exit.Node.Position = position.NewTokenPosition($1)
} else { } else {
$$.GetNode().Position = position.NewTokenNodePosition($1, $2) exit.Node.Position = position.NewTokenNodePosition($1, $2)
exit.OpenParenthesisTkn = $2.(*ast.ParserBrackets).OpenBracketTkn
exit.Expr = $2.(*ast.ParserBrackets).Child
exit.CloseParenthesisTkn = $2.(*ast.ParserBrackets).CloseBracketTkn
} }
// save comments $$ = exit
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
} }
| '@' expr | '@' expr
{ {
@ -4228,45 +4226,57 @@ lexical_var_list:
function_call: function_call:
namespace_name function_call_parameter_list namespace_name function_call_parameter_list
{ {
name := &ast.NameName{ $$ = &ast.ExprFunctionCall{
Node: ast.Node{ Node: ast.Node{
Position: position.NewNodeListPosition($1), Position: position.NewNodeListNodePosition($1, $2),
}, },
Parts: $1, Function: &ast.NameName{
Node: ast.Node{
Position: position.NewNodeListPosition($1),
},
Parts: $1,
},
OpenParenthesisTkn: $2.(*ast.ArgumentList).OpenParenthesisTkn,
Arguments: $2.(*ast.ArgumentList).Arguments,
CloseParenthesisTkn: $2.(*ast.ArgumentList).OpenParenthesisTkn,
} }
$$ = &ast.ExprFunctionCall{ast.Node{}, name, $2.(*ast.ArgumentList)}
// save position
$$.GetNode().Position = position.NewNodesPosition(name, $2)
} }
| T_NAMESPACE T_NS_SEPARATOR namespace_name function_call_parameter_list | T_NAMESPACE T_NS_SEPARATOR namespace_name function_call_parameter_list
{ {
name := &ast.NameRelative{ $$ = &ast.ExprFunctionCall{
Node: ast.Node{ Node: ast.Node{
Position: position.NewTokenNodeListPosition($1, $3), Position: position.NewTokenNodePosition($1, $4),
}, },
NsTkn: $1, Function: &ast.NameRelative{
NsSeparatorTkn: $2, Node: ast.Node{
Parts: $3, Position: position.NewTokenNodeListPosition($1, $3),
},
NsTkn: $1,
NsSeparatorTkn: $2,
Parts: $3,
},
OpenParenthesisTkn: $4.(*ast.ArgumentList).OpenParenthesisTkn,
Arguments: $4.(*ast.ArgumentList).Arguments,
CloseParenthesisTkn: $4.(*ast.ArgumentList).OpenParenthesisTkn,
} }
$$ = &ast.ExprFunctionCall{ast.Node{}, name, $4.(*ast.ArgumentList)}
// save position
$$.GetNode().Position = position.NewNodesPosition(name, $4)
} }
| T_NS_SEPARATOR namespace_name function_call_parameter_list | T_NS_SEPARATOR namespace_name function_call_parameter_list
{ {
name := &ast.NameFullyQualified{ $$ = &ast.ExprFunctionCall{
Node: ast.Node{ Node: ast.Node{
Position: position.NewTokenNodeListPosition($1, $2), Position: position.NewTokenNodePosition($1, $3),
}, },
NsSeparatorTkn: $1, Function: &ast.NameFullyQualified{
Parts: $2, Node: ast.Node{
Position: position.NewTokenNodeListPosition($1, $2),
},
NsSeparatorTkn: $1,
Parts: $2,
},
OpenParenthesisTkn: $3.(*ast.ArgumentList).OpenParenthesisTkn,
Arguments: $3.(*ast.ArgumentList).Arguments,
CloseParenthesisTkn: $3.(*ast.ArgumentList).OpenParenthesisTkn,
} }
$$ = &ast.ExprFunctionCall{ast.Node{}, name, $3.(*ast.ArgumentList)}
// save position
$$.GetNode().Position = position.NewNodesPosition(name, $3)
} }
| class_name T_PAAMAYIM_NEKUDOTAYIM variable_name function_call_parameter_list | class_name T_PAAMAYIM_NEKUDOTAYIM variable_name function_call_parameter_list
{ {
@ -4314,13 +4324,15 @@ function_call:
} }
| variable_without_objects function_call_parameter_list | variable_without_objects function_call_parameter_list
{ {
$$ = &ast.ExprFunctionCall{ast.Node{}, $1, $2.(*ast.ArgumentList)} $$ = &ast.ExprFunctionCall{
Node: ast.Node{
// save position Position: position.NewNodesPosition($1, $2),
$$.GetNode().Position = position.NewNodesPosition($1, $2) },
Function: $1,
// save comments OpenParenthesisTkn: $2.(*ast.ArgumentList).OpenParenthesisTkn,
yylex.(*Parser).MoveFreeFloating($1, $$) Arguments: $2.(*ast.ArgumentList).Arguments,
CloseParenthesisTkn: $2.(*ast.ArgumentList).OpenParenthesisTkn,
}
} }
; ;
@ -6355,43 +6367,35 @@ internal_functions_in_yacc:
} }
| T_INCLUDE expr | T_INCLUDE expr
{ {
$$ = &ast.ExprInclude{ast.Node{}, $2} $$ = &ast.ExprInclude{
Node: ast.Node{
// save position Position: position.NewTokenNodePosition($1, $2),
$$.GetNode().Position = position.NewTokenNodePosition($1, $2) },
IncludeTkn: $1,
// save comments Expr: $2,
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) }
} }
| T_INCLUDE_ONCE expr | T_INCLUDE_ONCE expr
{ {
$$ = &ast.ExprIncludeOnce{ast.Node{}, $2} $$ = &ast.ExprIncludeOnce{
Node: ast.Node{
// save position Position: position.NewTokenNodePosition($1, $2),
$$.GetNode().Position = position.NewTokenNodePosition($1, $2) },
IncludeTkn: $1,
// save comments Expr: $2,
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) }
} }
| T_EVAL '(' expr ')' | T_EVAL '(' expr ')'
{ {
exprBrackets := &ast.ParserBrackets{ $$ = &ast.ExprEval{
Node: ast.Node{ Node: ast.Node{
Position: position.NewTokensPosition($2, $4), Position: position.NewTokensPosition($1, $4),
}, },
OpenBracketTkn: $2, EvalTkn: $1,
Child: $3, OpenParenthesisTkn: $2,
CloseBracketTkn: $4, Expr: $3,
CloseParenthesisTkn: $4,
} }
$$ = &ast.ExprEval{ast.Node{}, exprBrackets}
// save position
$$.GetNode().Position = position.NewTokensPosition($1, $4)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
yylex.(*Parser).setFreeFloatingTokens(exprBrackets, token.Start, $2.SkippedTokens)
yylex.(*Parser).setFreeFloatingTokens(exprBrackets, token.End, $4.SkippedTokens)
} }
| T_REQUIRE expr | T_REQUIRE expr
{ {

BIN
internal/php7/php7.go generated

Binary file not shown.

View File

@ -2,7 +2,6 @@
package php7 package php7
import ( import (
"bytes"
"strconv" "strconv"
"github.com/z7zmey/php-parser/internal/position" "github.com/z7zmey/php-parser/internal/position"
@ -3533,21 +3532,20 @@ expr_without_variable:
} }
| T_EXIT exit_expr | T_EXIT exit_expr
{ {
$$ = &ast.ExprExit{ast.Node{}, false, $2} exit := &ast.ExprExit{
DieTkn: $1,
if (bytes.EqualFold($1.Value, []byte("die"))) {
$$.(*ast.ExprExit).Die = true
} }
// save position
if $2 == nil { if $2 == nil {
$$.GetNode().Position = position.NewTokenPosition($1) exit.Node.Position = position.NewTokenPosition($1)
} else { } else {
$$.GetNode().Position = position.NewTokenNodePosition($1, $2) exit.Node.Position = position.NewTokenNodePosition($1, $2)
exit.OpenParenthesisTkn = $2.(*ast.ParserBrackets).OpenBracketTkn
exit.Expr = $2.(*ast.ParserBrackets).Child
exit.CloseParenthesisTkn = $2.(*ast.ParserBrackets).CloseBracketTkn
} }
// save comments $$ = exit
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
} }
| '@' expr | '@' expr
{ {
@ -3776,13 +3774,15 @@ lexical_var:
function_call: function_call:
name argument_list name argument_list
{ {
$$ = &ast.ExprFunctionCall{ast.Node{}, $1, $2.(*ast.ArgumentList)} $$ = &ast.ExprFunctionCall{
Node: ast.Node{
// save position Position: position.NewNodesPosition($1, $2),
$$.GetNode().Position = position.NewNodesPosition($1, $2) },
Function: $1,
// save comments OpenParenthesisTkn: $2.(*ast.ArgumentList).OpenParenthesisTkn,
yylex.(*Parser).MoveFreeFloating($1, $$) Arguments: $2.(*ast.ArgumentList).Arguments,
CloseParenthesisTkn: $2.(*ast.ArgumentList).OpenParenthesisTkn,
}
} }
| class_name T_PAAMAYIM_NEKUDOTAYIM member_name argument_list | class_name T_PAAMAYIM_NEKUDOTAYIM member_name argument_list
{ {
@ -3808,13 +3808,15 @@ function_call:
} }
| callable_expr argument_list | callable_expr argument_list
{ {
$$ = &ast.ExprFunctionCall{ast.Node{}, $1, $2.(*ast.ArgumentList)} $$ = &ast.ExprFunctionCall{
Node: ast.Node{
// save position Position: position.NewNodesPosition($1, $2),
$$.GetNode().Position = position.NewNodesPosition($1, $2) },
Function: $1,
// save comments OpenParenthesisTkn: $2.(*ast.ArgumentList).OpenParenthesisTkn,
yylex.(*Parser).MoveFreeFloating($1, $$) Arguments: $2.(*ast.ArgumentList).Arguments,
CloseParenthesisTkn: $2.(*ast.ArgumentList).OpenParenthesisTkn,
}
} }
; ;
@ -4912,43 +4914,35 @@ internal_functions_in_yacc:
} }
| T_INCLUDE expr | T_INCLUDE expr
{ {
$$ = &ast.ExprInclude{ast.Node{}, $2} $$ = &ast.ExprInclude{
Node: ast.Node{
// save position Position: position.NewTokenNodePosition($1, $2),
$$.GetNode().Position = position.NewTokenNodePosition($1, $2) },
IncludeTkn: $1,
// save comments Expr: $2,
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) }
} }
| T_INCLUDE_ONCE expr | T_INCLUDE_ONCE expr
{ {
$$ = &ast.ExprIncludeOnce{ast.Node{}, $2} $$ = &ast.ExprIncludeOnce{
Node: ast.Node{
// save position Position: position.NewTokenNodePosition($1, $2),
$$.GetNode().Position = position.NewTokenNodePosition($1, $2) },
IncludeTkn: $1,
// save comments Expr: $2,
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) }
} }
| T_EVAL '(' expr ')' | T_EVAL '(' expr ')'
{ {
exprBrackets := &ast.ParserBrackets{ $$ = &ast.ExprEval{
Node: ast.Node{ Node: ast.Node{
Position: position.NewTokensPosition($2, $4), Position: position.NewTokensPosition($1, $4),
}, },
OpenBracketTkn: $2, EvalTkn: $1,
Child: $3, OpenParenthesisTkn: $2,
CloseBracketTkn: $4, Expr: $3,
CloseParenthesisTkn: $4,
} }
$$ = &ast.ExprEval{ast.Node{}, exprBrackets}
// save position
$$.GetNode().Position = position.NewTokensPosition($1, $4)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
yylex.(*Parser).setFreeFloatingTokens(exprBrackets, token.Start, $2.SkippedTokens)
yylex.(*Parser).setFreeFloatingTokens(exprBrackets, token.End, $4.SkippedTokens)
} }
| T_REQUIRE expr | T_REQUIRE expr
{ {

View File

@ -1114,7 +1114,10 @@ func (n *ExprErrorSuppress) Accept(v NodeVisitor) {
// ExprEval node // ExprEval node
type ExprEval struct { type ExprEval struct {
Node Node
Expr Vertex EvalTkn *token.Token
OpenParenthesisTkn *token.Token
Expr Vertex
CloseParenthesisTkn *token.Token
} }
func (n *ExprEval) Accept(v NodeVisitor) { func (n *ExprEval) Accept(v NodeVisitor) {
@ -1124,8 +1127,10 @@ func (n *ExprEval) Accept(v NodeVisitor) {
// ExprExit node // ExprExit node
type ExprExit struct { type ExprExit struct {
Node Node
Die bool DieTkn *token.Token
Expr Vertex OpenParenthesisTkn *token.Token
Expr Vertex
CloseParenthesisTkn *token.Token
} }
func (n *ExprExit) Accept(v NodeVisitor) { func (n *ExprExit) Accept(v NodeVisitor) {
@ -1135,8 +1140,10 @@ func (n *ExprExit) Accept(v NodeVisitor) {
// ExprFunctionCall node // ExprFunctionCall node
type ExprFunctionCall struct { type ExprFunctionCall struct {
Node Node
Function Vertex Function Vertex
ArgumentList *ArgumentList OpenParenthesisTkn *token.Token
Arguments []Vertex
CloseParenthesisTkn *token.Token
} }
func (n *ExprFunctionCall) Accept(v NodeVisitor) { func (n *ExprFunctionCall) Accept(v NodeVisitor) {
@ -1146,7 +1153,8 @@ func (n *ExprFunctionCall) Accept(v NodeVisitor) {
// ExprInclude node // ExprInclude node
type ExprInclude struct { type ExprInclude struct {
Node Node
Expr Vertex IncludeTkn *token.Token
Expr Vertex
} }
func (n *ExprInclude) Accept(v NodeVisitor) { func (n *ExprInclude) Accept(v NodeVisitor) {
@ -1156,7 +1164,8 @@ func (n *ExprInclude) Accept(v NodeVisitor) {
// ExprIncludeOnce node // ExprIncludeOnce node
type ExprIncludeOnce struct { type ExprIncludeOnce struct {
Node Node
Expr Vertex IncludeTkn *token.Token
Expr Vertex
} }
func (n *ExprIncludeOnce) Accept(v NodeVisitor) { func (n *ExprIncludeOnce) Accept(v NodeVisitor) {

View File

@ -1303,10 +1303,12 @@ func (t *DFS) Traverse(n ast.Vertex) {
t.Traverse(nn.Function) t.Traverse(nn.Function)
t.visitor.Leave("Function", true) t.visitor.Leave("Function", true)
} }
if nn.ArgumentList != nil { if nn.Arguments != nil {
t.visitor.Enter("ArgumentList", true) t.visitor.Enter("Arguments", false)
t.Traverse(nn.ArgumentList) for _, c := range nn.Arguments {
t.visitor.Leave("ArgumentList", true) t.Traverse(c)
}
t.visitor.Leave("Arguments", false)
} }
case *ast.ExprInclude: case *ast.ExprInclude:
if nn == nil { if nn == nil {

View File

@ -692,11 +692,6 @@ func (v *Dump) ExprExit(n *ast.ExprExit) {
v.printIndentIfNotSingle(v.indent - 1) v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.ExprExit{\n") v.print("&ast.ExprExit{\n")
v.printNode(n.GetNode()) v.printNode(n.GetNode())
if n.Die {
v.printIndent(v.indent)
v.print("Die: true,\n")
}
} }
func (v *Dump) ExprFunctionCall(n *ast.ExprFunctionCall) { func (v *Dump) ExprFunctionCall(n *ast.ExprFunctionCall) {

View File

@ -1074,11 +1074,7 @@ func (p *PrettyPrinter) printExprEval(n ast.Vertex) {
func (p *PrettyPrinter) printExprExit(n ast.Vertex) { func (p *PrettyPrinter) printExprExit(n ast.Vertex) {
nn := n.(*ast.ExprExit) nn := n.(*ast.ExprExit)
if nn.Die { io.WriteString(p.w, "exit(")
io.WriteString(p.w, "die(")
} else {
io.WriteString(p.w, "exit(")
}
p.Print(nn.Expr) p.Print(nn.Expr)
io.WriteString(p.w, ")") io.WriteString(p.w, ")")
} }
@ -1088,7 +1084,7 @@ func (p *PrettyPrinter) printExprFunctionCall(n ast.Vertex) {
p.Print(nn.Function) p.Print(nn.Function)
io.WriteString(p.w, "(") io.WriteString(p.w, "(")
p.joinPrint(", ", nn.ArgumentList.Arguments) p.joinPrint(", ", nn.Arguments)
io.WriteString(p.w, ")") io.WriteString(p.w, ")")
} }

View File

@ -1590,8 +1590,8 @@ func (p *Printer) printExprExit(n ast.Vertex) {
nn := n.(*ast.ExprExit) nn := n.(*ast.ExprExit)
p.printFreeFloating(nn, token.Start) p.printFreeFloating(nn, token.Start)
if nn.Die { if nn.DieTkn != nil {
p.write([]byte("die")) p.write(nn.DieTkn.Value)
} else { } else {
p.write([]byte("exit")) p.write([]byte("exit"))
} }
@ -1611,9 +1611,9 @@ func (p *Printer) printExprFunctionCall(n ast.Vertex) {
p.Print(nn.Function) p.Print(nn.Function)
p.printFreeFloatingOrDefault(nn.ArgumentList, token.Start, "(") p.printToken(nn.OpenParenthesisTkn, "(")
p.joinPrint(",", nn.ArgumentList.Arguments) p.joinPrint(",", nn.Arguments)
p.printFreeFloatingOrDefault(nn.ArgumentList, token.End, ")") p.printToken(nn.CloseParenthesisTkn, ")")
p.printFreeFloating(nn, token.End) p.printFreeFloating(nn, token.End)
} }