[refactoring] update ast structure of "Root" and "Class" nodes

This commit is contained in:
Vadym Slizov 2020-12-04 11:02:38 +02:00
parent d19b3f609e
commit df1626b7dc
No known key found for this signature in database
GPG Key ID: AEA2A9388EF42A4A
11 changed files with 61 additions and 75 deletions

BIN
internal/php5/php5.go generated

Binary file not shown.

View File

@ -267,10 +267,13 @@ import (
start:
top_statement_list
{
yylex.(*Parser).rootNode = &ast.Root{ast.Node{}, $1}
yylex.(*Parser).rootNode.GetNode().Position = position.NewNodeListPosition($1)
yylex.(*Parser).setFreeFloating(yylex.(*Parser).rootNode, token.End, yylex.(*Parser).currentToken.SkippedTokens)
yylex.(*Parser).rootNode = &ast.Root{
Node: ast.Node{
Position: position.NewNodeListPosition($1),
},
Stmts: $1,
EndTkn: yylex.(*Parser).currentToken,
}
}
;

BIN
internal/php7/php7.go generated

Binary file not shown.

View File

@ -288,12 +288,13 @@ import (
start:
top_statement_list
{
yylex.(*Parser).rootNode = &ast.Root{ast.Node{}, $1}
// save position
yylex.(*Parser).rootNode.GetNode().Position = position.NewNodeListPosition($1)
yylex.(*Parser).setFreeFloating(yylex.(*Parser).rootNode, token.End, yylex.(*Parser).currentToken.SkippedTokens)
yylex.(*Parser).rootNode = &ast.Root{
Node: ast.Node{
Position: position.NewNodeListPosition($1),
},
Stmts: $1,
EndTkn: yylex.(*Parser).currentToken,
}
}
;
@ -2755,7 +2756,9 @@ anonymous_class:
Position: position.NewTokensPosition($1, $8),
},
ClassTkn: $1,
ArgumentList: $2,
OpenParenthesisTkn: $2.(*ast.ArgumentList).OpenParenthesisTkn,
Arguments: $2.(*ast.ArgumentList).Arguments,
CloseParenthesisTkn: $2.(*ast.ArgumentList).OpenParenthesisTkn,
Extends: $3,
Implements: $4,
OpenCurlyBracket: $6,

View File

@ -24,7 +24,6 @@ type NodeVisitor interface {
Variadic(n *Variadic)
Parameter(n *Parameter)
Identifier(n *Identifier)
ArgumentList(n *ArgumentList)
Argument(n *Argument)
StmtBreak(n *StmtBreak)

View File

@ -24,6 +24,7 @@ func (n *Node) GetPosition() *position.Position {
type Root struct {
Node
Stmts []Vertex
EndTkn *token.Token
}
func (n *Root) Accept(v NodeVisitor) {
@ -87,19 +88,6 @@ func (n *Identifier) Accept(v NodeVisitor) {
v.Identifier(n)
}
// ArgumentList node
type ArgumentList struct {
Node
OpenParenthesisTkn *token.Token
Arguments []Vertex
SeparatorTkns []*token.Token
CloseParenthesisTkn *token.Token
}
func (n *ArgumentList) Accept(v NodeVisitor) {
v.ArgumentList(n)
}
// Argument node
type Argument struct {
Node
@ -241,7 +229,9 @@ type StmtClass struct {
Modifiers []Vertex
ClassTkn *token.Token
ClassName Vertex
ArgumentList Vertex
OpenParenthesisTkn *token.Token
Arguments []Vertex
CloseParenthesisTkn *token.Token
Extends Vertex
Implements Vertex
OpenCurlyBracket *token.Token
@ -2098,6 +2088,19 @@ func (n *ParserSeparatedList) Accept(v NodeVisitor) {
v.ParserSeparatedList(n)
}
// ArgumentList node
type ArgumentList struct {
Node
OpenParenthesisTkn *token.Token
Arguments []Vertex
SeparatorTkns []*token.Token
CloseParenthesisTkn *token.Token
}
func (n *ArgumentList) Accept(v NodeVisitor) {
// do nothing
}
type ReturnType struct {
Node
ColonTkn *token.Token

View File

@ -93,20 +93,6 @@ func (t *DFS) Traverse(n ast.Vertex) {
if !t.visitor.EnterNode(nn) {
return
}
case *ast.ArgumentList:
if nn == nil {
return
}
if !t.visitor.EnterNode(nn) {
return
}
if nn.Arguments != nil {
t.visitor.Enter("Arguments", false)
for _, c := range nn.Arguments {
t.Traverse(c)
}
t.visitor.Leave("Arguments", false)
}
case *ast.Argument:
if nn == nil {
return
@ -195,10 +181,12 @@ func (t *DFS) Traverse(n ast.Vertex) {
}
t.visitor.Leave("Modifiers", false)
}
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)
}
if nn.Extends != nil {
t.visitor.Enter("Extends", true)

View File

@ -230,12 +230,6 @@ func (v *Dump) Identifier(n *ast.Identifier) {
v.print(fmt.Sprintf("Value: []byte(%q),\n", n.Value))
}
func (v *Dump) ArgumentList(n *ast.ArgumentList) {
v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.ArgumentList{\n")
v.printNode(n.GetNode())
}
func (v *Dump) Argument(n *ast.Argument) {
v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.Argument{\n")

View File

@ -46,10 +46,6 @@ func (v *Null) Identifier(_ *ast.Identifier) {
// do nothing
}
func (v *Null) ArgumentList(_ *ast.ArgumentList) {
// do nothing
}
func (v *Null) Argument(_ *ast.Argument) {
// do nothing
}

View File

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

View File

@ -2067,10 +2067,10 @@ func (p *Printer) printStmtClass(n ast.Vertex) {
p.Print(nn.ClassName)
}
if nn.ArgumentList != nil {
p.printFreeFloatingOrDefault(nn.ArgumentList, token.Start, "(")
p.joinPrint(",", nn.ArgumentList.(*ast.ArgumentList).Arguments)
p.printFreeFloatingOrDefault(nn.ArgumentList, token.End, ")")
if nn.Arguments != nil {
p.printToken(nn.OpenParenthesisTkn, "(")
p.joinPrint(",", nn.Arguments)
p.printToken(nn.CloseParenthesisTkn, ")")
}
if nn.Extends != nil {