[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: new_expr:
T_NEW class_name_reference ctor_arguments T_NEW class_name_reference ctor_arguments
{ {
if $3 != nil { if $3 != nil {
$$ = &ast.ExprNew{ast.Node{}, $2, $3.(*ast.ArgumentList)} $$ = &ast.ExprNew{
$$.GetNode().Position = position.NewTokenNodePosition($1, $3) Node: ast.Node{
} else { Position: position.NewTokenNodePosition($1, $3),
$$ = &ast.ExprNew{ast.Node{}, $2, nil} },
$$.GetNode().Position = position.NewTokenNodePosition($1, $2) 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 | variable '=' '&' T_NEW class_name_reference ctor_arguments
{ {
var _new *ast.ExprNew var _new *ast.ExprNew
if $3 != nil {
if $6 != nil { _new = &ast.ExprNew{
_new = &ast.ExprNew{ast.Node{}, $5, $6.(*ast.ArgumentList)} Node: ast.Node{
} else { Position: position.NewTokenNodePosition($4, $6),
_new = &ast.ExprNew{ast.Node{}, $5, nil} },
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} $$ = &ast.ExprAssignReference{ast.Node{}, $1, _new}
// save position // save position
@ -3360,45 +3383,43 @@ expr_without_variable:
} }
| rw_variable T_INC | rw_variable T_INC
{ {
$$ = &ast.ExprPostInc{ast.Node{}, $1} $$ = &ast.ExprPostInc{
Node: ast.Node{
// save position Position: position.NewNodeTokenPosition($1, $2),
$$.GetNode().Position = position.NewNodeTokenPosition($1, $2) },
Var: $1,
// save comments IncTkn: $2,
yylex.(*Parser).MoveFreeFloating($1, $$) }
yylex.(*Parser).setFreeFloating($$, token.Var, $2.SkippedTokens)
} }
| T_INC rw_variable | T_INC rw_variable
{ {
$$ = &ast.ExprPreInc{ast.Node{}, $2} $$ = &ast.ExprPreInc{
Node: ast.Node{
// save position Position: position.NewTokenNodePosition($1, $2),
$$.GetNode().Position = position.NewTokenNodePosition($1, $2) },
IncTkn: $1,
// save comments Var: $2,
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) }
} }
| rw_variable T_DEC | rw_variable T_DEC
{ {
$$ = &ast.ExprPostDec{ast.Node{}, $1} $$ = &ast.ExprPostDec{
Node: ast.Node{
// save position Position: position.NewNodeTokenPosition($1, $2),
$$.GetNode().Position = position.NewNodeTokenPosition($1, $2) },
Var: $1,
// save comments DecTkn: $2,
yylex.(*Parser).MoveFreeFloating($1, $$) }
yylex.(*Parser).setFreeFloating($$, token.Var, $2.SkippedTokens)
} }
| T_DEC rw_variable | T_DEC rw_variable
{ {
$$ = &ast.ExprPreDec{ast.Node{}, $2} $$ = &ast.ExprPreDec{
Node: ast.Node{
// save position Position: position.NewTokenNodePosition($1, $2),
$$.GetNode().Position = position.NewTokenNodePosition($1, $2) },
DecTkn: $1,
// save comments Var: $2,
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) }
} }
| expr T_BOOLEAN_OR expr | 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 T_NEW class_name_reference ctor_arguments
{ {
if $3 != nil { if $3 != nil {
$$ = &ast.ExprNew{ast.Node{}, $2, $3.(*ast.ArgumentList)} $$ = &ast.ExprNew{
$$.GetNode().Position = position.NewTokenNodePosition($1, $3) Node: ast.Node{
} else { Position: position.NewTokenNodePosition($1, $3),
$$ = &ast.ExprNew{ast.Node{}, $2, nil} },
$$.GetNode().Position = position.NewTokenNodePosition($1, $2) 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 | T_NEW anonymous_class
{ {
$$ = &ast.ExprNew{ast.Node{}, $2, nil} $$ = &ast.ExprNew{
Node: ast.Node{
// save position Position: position.NewTokenNodePosition($1, $2),
$$.GetNode().Position = position.NewTokenNodePosition($1, $2) },
NewTkn: $1,
// save comments Class: $2,
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) }
} }
; ;
@ -3018,45 +3028,43 @@ expr_without_variable:
} }
| variable T_INC | variable T_INC
{ {
$$ = &ast.ExprPostInc{ast.Node{}, $1} $$ = &ast.ExprPostInc{
Node: ast.Node{
// save position Position: position.NewNodeTokenPosition($1, $2),
$$.GetNode().Position = position.NewNodeTokenPosition($1, $2) },
Var: $1,
// save comments IncTkn: $2,
yylex.(*Parser).MoveFreeFloating($1, $$) }
yylex.(*Parser).setFreeFloating($$, token.Var, $2.SkippedTokens)
} }
| T_INC variable | T_INC variable
{ {
$$ = &ast.ExprPreInc{ast.Node{}, $2} $$ = &ast.ExprPreInc{
Node: ast.Node{
// save position Position: position.NewTokenNodePosition($1, $2),
$$.GetNode().Position = position.NewTokenNodePosition($1, $2) },
IncTkn: $1,
// save comments Var: $2,
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) }
} }
| variable T_DEC | variable T_DEC
{ {
$$ = &ast.ExprPostDec{ast.Node{}, $1} $$ = &ast.ExprPostDec{
Node: ast.Node{
// save position Position: position.NewNodeTokenPosition($1, $2),
$$.GetNode().Position = position.NewNodeTokenPosition($1, $2) },
Var: $1,
// save comments DecTkn: $2,
yylex.(*Parser).MoveFreeFloating($1, $$) }
yylex.(*Parser).setFreeFloating($$, token.Var, $2.SkippedTokens)
} }
| T_DEC variable | T_DEC variable
{ {
$$ = &ast.ExprPreDec{ast.Node{}, $2} $$ = &ast.ExprPreDec{
Node: ast.Node{
// save position Position: position.NewTokenNodePosition($1, $2),
$$.GetNode().Position = position.NewTokenNodePosition($1, $2) },
DecTkn: $1,
// save comments Var: $2,
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) }
} }
| expr T_BOOLEAN_OR expr | expr T_BOOLEAN_OR expr
{ {

View File

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

View File

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

View File

@ -1143,9 +1143,9 @@ func (p *PrettyPrinter) printExprNew(n ast.Vertex) {
io.WriteString(p.w, "new ") io.WriteString(p.w, "new ")
p.Print(nn.Class) p.Print(nn.Class)
if nn.ArgumentList != nil { if nn.Arguments != nil {
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

@ -1710,10 +1710,10 @@ func (p *Printer) printExprNew(n ast.Vertex) {
p.bufStart = " " p.bufStart = " "
p.Print(nn.Class) p.Print(nn.Class)
if nn.ArgumentList != nil { if nn.Arguments != nil {
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)