[refactoring] update ast structure of "PostDec", "PostInc", "PreDec", "PreInc" and "New" nodes

This commit is contained in:
Vadym Slizov 2020-12-03 22:04:59 +02:00
parent b1e9f5e167
commit 45e959056b
No known key found for this signature in database
GPG Key ID: AEA2A9388EF42A4A
8 changed files with 140 additions and 102 deletions

BIN
internal/php5/php5.go generated

Binary file not shown.

View File

@ -3123,17 +3123,25 @@ instance_call:
new_expr:
T_NEW class_name_reference ctor_arguments
{
if $3 != nil {
$$ = &ast.ExprNew{ast.Node{}, $2, $3.(*ast.ArgumentList)}
$$.GetNode().Position = position.NewTokenNodePosition($1, $3)
} else {
$$ = &ast.ExprNew{ast.Node{}, $2, nil}
$$.GetNode().Position = position.NewTokenNodePosition($1, $2)
$$ = &ast.ExprNew{
Node: ast.Node{
Position: position.NewTokenNodePosition($1, $3),
},
NewTkn: $1,
Class: $2,
OpenParenthesisTkn: $3.(*ast.ArgumentList).OpenParenthesisTkn,
Arguments: $3.(*ast.ArgumentList).Arguments,
CloseParenthesisTkn: $3.(*ast.ArgumentList).OpenParenthesisTkn,
}
} else {
$$ = &ast.ExprNew{
Node: ast.Node{
Position: position.NewTokenNodePosition($1, $2),
},
Class: $2,
}
}
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
}
;
@ -3195,12 +3203,27 @@ expr_without_variable:
| variable '=' '&' T_NEW class_name_reference ctor_arguments
{
var _new *ast.ExprNew
if $6 != nil {
_new = &ast.ExprNew{ast.Node{}, $5, $6.(*ast.ArgumentList)}
} else {
_new = &ast.ExprNew{ast.Node{}, $5, nil}
if $3 != nil {
_new = &ast.ExprNew{
Node: ast.Node{
Position: position.NewTokenNodePosition($4, $6),
},
NewTkn: $4,
Class: $5,
OpenParenthesisTkn: $6.(*ast.ArgumentList).OpenParenthesisTkn,
Arguments: $6.(*ast.ArgumentList).Arguments,
CloseParenthesisTkn: $6.(*ast.ArgumentList).OpenParenthesisTkn,
}
} else {
_new = &ast.ExprNew{
Node: ast.Node{
Position: position.NewTokenNodePosition($4, $5),
},
NewTkn: $4,
Class: $5,
}
}
$$ = &ast.ExprAssignReference{ast.Node{}, $1, _new}
// save position
@ -3360,45 +3383,43 @@ expr_without_variable:
}
| rw_variable T_INC
{
$$ = &ast.ExprPostInc{ast.Node{}, $1}
// save position
$$.GetNode().Position = position.NewNodeTokenPosition($1, $2)
// save comments
yylex.(*Parser).MoveFreeFloating($1, $$)
yylex.(*Parser).setFreeFloating($$, token.Var, $2.SkippedTokens)
$$ = &ast.ExprPostInc{
Node: ast.Node{
Position: position.NewNodeTokenPosition($1, $2),
},
Var: $1,
IncTkn: $2,
}
}
| T_INC rw_variable
{
$$ = &ast.ExprPreInc{ast.Node{}, $2}
// save position
$$.GetNode().Position = position.NewTokenNodePosition($1, $2)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
$$ = &ast.ExprPreInc{
Node: ast.Node{
Position: position.NewTokenNodePosition($1, $2),
},
IncTkn: $1,
Var: $2,
}
}
| rw_variable T_DEC
{
$$ = &ast.ExprPostDec{ast.Node{}, $1}
// save position
$$.GetNode().Position = position.NewNodeTokenPosition($1, $2)
// save comments
yylex.(*Parser).MoveFreeFloating($1, $$)
yylex.(*Parser).setFreeFloating($$, token.Var, $2.SkippedTokens)
$$ = &ast.ExprPostDec{
Node: ast.Node{
Position: position.NewNodeTokenPosition($1, $2),
},
Var: $1,
DecTkn: $2,
}
}
| T_DEC rw_variable
{
$$ = &ast.ExprPreDec{ast.Node{}, $2}
// save position
$$.GetNode().Position = position.NewTokenNodePosition($1, $2)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
$$ = &ast.ExprPreDec{
Node: ast.Node{
Position: position.NewTokenNodePosition($1, $2),
},
DecTkn: $1,
Var: $2,
}
}
| expr T_BOOLEAN_OR expr
{

BIN
internal/php7/php7.go generated

Binary file not shown.

View File

@ -2778,25 +2778,35 @@ new_expr:
T_NEW class_name_reference ctor_arguments
{
if $3 != nil {
$$ = &ast.ExprNew{ast.Node{}, $2, $3.(*ast.ArgumentList)}
$$.GetNode().Position = position.NewTokenNodePosition($1, $3)
} else {
$$ = &ast.ExprNew{ast.Node{}, $2, nil}
$$.GetNode().Position = position.NewTokenNodePosition($1, $2)
$$ = &ast.ExprNew{
Node: ast.Node{
Position: position.NewTokenNodePosition($1, $3),
},
NewTkn: $1,
Class: $2,
OpenParenthesisTkn: $3.(*ast.ArgumentList).OpenParenthesisTkn,
Arguments: $3.(*ast.ArgumentList).Arguments,
CloseParenthesisTkn: $3.(*ast.ArgumentList).OpenParenthesisTkn,
}
} else {
$$ = &ast.ExprNew{
Node: ast.Node{
Position: position.NewTokenNodePosition($1, $2),
},
NewTkn: $1,
Class: $2,
}
}
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
}
| T_NEW anonymous_class
{
$$ = &ast.ExprNew{ast.Node{}, $2, nil}
// save position
$$.GetNode().Position = position.NewTokenNodePosition($1, $2)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
$$ = &ast.ExprNew{
Node: ast.Node{
Position: position.NewTokenNodePosition($1, $2),
},
NewTkn: $1,
Class: $2,
}
}
;
@ -3018,45 +3028,43 @@ expr_without_variable:
}
| variable T_INC
{
$$ = &ast.ExprPostInc{ast.Node{}, $1}
// save position
$$.GetNode().Position = position.NewNodeTokenPosition($1, $2)
// save comments
yylex.(*Parser).MoveFreeFloating($1, $$)
yylex.(*Parser).setFreeFloating($$, token.Var, $2.SkippedTokens)
$$ = &ast.ExprPostInc{
Node: ast.Node{
Position: position.NewNodeTokenPosition($1, $2),
},
Var: $1,
IncTkn: $2,
}
}
| T_INC variable
{
$$ = &ast.ExprPreInc{ast.Node{}, $2}
// save position
$$.GetNode().Position = position.NewTokenNodePosition($1, $2)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
$$ = &ast.ExprPreInc{
Node: ast.Node{
Position: position.NewTokenNodePosition($1, $2),
},
IncTkn: $1,
Var: $2,
}
}
| variable T_DEC
{
$$ = &ast.ExprPostDec{ast.Node{}, $1}
// save position
$$.GetNode().Position = position.NewNodeTokenPosition($1, $2)
// save comments
yylex.(*Parser).MoveFreeFloating($1, $$)
yylex.(*Parser).setFreeFloating($$, token.Var, $2.SkippedTokens)
$$ = &ast.ExprPostDec{
Node: ast.Node{
Position: position.NewNodeTokenPosition($1, $2),
},
Var: $1,
DecTkn: $2,
}
}
| T_DEC variable
{
$$ = &ast.ExprPreDec{ast.Node{}, $2}
// save position
$$.GetNode().Position = position.NewTokenNodePosition($1, $2)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
$$ = &ast.ExprPreDec{
Node: ast.Node{
Position: position.NewTokenNodePosition($1, $2),
},
DecTkn: $1,
Var: $2,
}
}
| expr T_BOOLEAN_OR expr
{

View File

@ -1230,8 +1230,11 @@ func (n *ExprMethodCall) Accept(v NodeVisitor) {
// ExprNew node
type ExprNew struct {
Node
NewTkn *token.Token
Class Vertex
ArgumentList *ArgumentList
OpenParenthesisTkn *token.Token
Arguments []Vertex
CloseParenthesisTkn *token.Token
}
func (n *ExprNew) Accept(v NodeVisitor) {
@ -1242,6 +1245,7 @@ func (n *ExprNew) Accept(v NodeVisitor) {
type ExprPostDec struct {
Node
Var Vertex
DecTkn *token.Token
}
func (n *ExprPostDec) Accept(v NodeVisitor) {
@ -1252,6 +1256,7 @@ func (n *ExprPostDec) Accept(v NodeVisitor) {
type ExprPostInc struct {
Node
Var Vertex
IncTkn *token.Token
}
func (n *ExprPostInc) Accept(v NodeVisitor) {
@ -1261,6 +1266,7 @@ func (n *ExprPostInc) Accept(v NodeVisitor) {
// ExprPreDec node
type ExprPreDec struct {
Node
DecTkn *token.Token
Var Vertex
}
@ -1271,6 +1277,7 @@ func (n *ExprPreDec) Accept(v NodeVisitor) {
// ExprPreInc node
type ExprPreInc struct {
Node
IncTkn *token.Token
Var Vertex
}

View File

@ -1415,10 +1415,12 @@ func (t *DFS) Traverse(n ast.Vertex) {
t.Traverse(nn.Class)
t.visitor.Leave("Class", 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.ExprPostDec:
if nn == nil {

View File

@ -1143,9 +1143,9 @@ func (p *PrettyPrinter) printExprNew(n ast.Vertex) {
io.WriteString(p.w, "new ")
p.Print(nn.Class)
if nn.ArgumentList != nil {
if nn.Arguments != nil {
io.WriteString(p.w, "(")
p.joinPrint(", ", nn.ArgumentList.Arguments)
p.joinPrint(", ", nn.Arguments)
io.WriteString(p.w, ")")
}
}

View File

@ -1710,10 +1710,10 @@ func (p *Printer) printExprNew(n ast.Vertex) {
p.bufStart = " "
p.Print(nn.Class)
if nn.ArgumentList != nil {
p.printFreeFloatingOrDefault(nn.ArgumentList, token.Start, "(")
p.joinPrint(",", nn.ArgumentList.Arguments)
p.printFreeFloatingOrDefault(nn.ArgumentList, token.End, ")")
if nn.Arguments != nil {
p.printToken(nn.OpenParenthesisTkn, "(")
p.joinPrint(",", nn.Arguments)
p.printToken(nn.CloseParenthesisTkn, ")")
}
p.printFreeFloating(nn, token.End)