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

BIN
internal/php7/php7.go generated

Binary file not shown.

View File

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

View File

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

View File

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

View File

@ -692,11 +692,6 @@ func (v *Dump) ExprExit(n *ast.ExprExit) {
v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.ExprExit{\n")
v.printNode(n.GetNode())
if n.Die {
v.printIndent(v.indent)
v.print("Die: true,\n")
}
}
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) {
nn := n.(*ast.ExprExit)
if nn.Die {
io.WriteString(p.w, "die(")
} else {
io.WriteString(p.w, "exit(")
}
io.WriteString(p.w, "exit(")
p.Print(nn.Expr)
io.WriteString(p.w, ")")
}
@ -1088,7 +1084,7 @@ func (p *PrettyPrinter) printExprFunctionCall(n ast.Vertex) {
p.Print(nn.Function)
io.WriteString(p.w, "(")
p.joinPrint(", ", nn.ArgumentList.Arguments)
p.joinPrint(", ", nn.Arguments)
io.WriteString(p.w, ")")
}

View File

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