[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: start:
top_statement_list top_statement_list
{ {
yylex.(*Parser).rootNode = &ast.Root{ast.Node{}, $1} yylex.(*Parser).rootNode = &ast.Root{
yylex.(*Parser).rootNode.GetNode().Position = position.NewNodeListPosition($1) Node: ast.Node{
Position: position.NewNodeListPosition($1),
yylex.(*Parser).setFreeFloating(yylex.(*Parser).rootNode, token.End, yylex.(*Parser).currentToken.SkippedTokens) },
Stmts: $1,
EndTkn: yylex.(*Parser).currentToken,
}
} }
; ;

BIN
internal/php7/php7.go generated

Binary file not shown.

View File

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

View File

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

View File

@ -23,7 +23,8 @@ func (n *Node) GetPosition() *position.Position {
// Root node // Root node
type Root struct { type Root struct {
Node Node
Stmts []Vertex Stmts []Vertex
EndTkn *token.Token
} }
func (n *Root) Accept(v NodeVisitor) { func (n *Root) Accept(v NodeVisitor) {
@ -87,19 +88,6 @@ func (n *Identifier) Accept(v NodeVisitor) {
v.Identifier(n) 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 // Argument node
type Argument struct { type Argument struct {
Node Node
@ -238,15 +226,17 @@ func (n *StmtCatch) Accept(v NodeVisitor) {
// StmtClass node // StmtClass node
type StmtClass struct { type StmtClass struct {
Node Node
Modifiers []Vertex Modifiers []Vertex
ClassTkn *token.Token ClassTkn *token.Token
ClassName Vertex ClassName Vertex
ArgumentList Vertex OpenParenthesisTkn *token.Token
Extends Vertex Arguments []Vertex
Implements Vertex CloseParenthesisTkn *token.Token
OpenCurlyBracket *token.Token Extends Vertex
Stmts []Vertex Implements Vertex
CloseCurlyBracket *token.Token OpenCurlyBracket *token.Token
Stmts []Vertex
CloseCurlyBracket *token.Token
} }
func (n *StmtClass) Accept(v NodeVisitor) { func (n *StmtClass) Accept(v NodeVisitor) {
@ -2098,6 +2088,19 @@ func (n *ParserSeparatedList) Accept(v NodeVisitor) {
v.ParserSeparatedList(n) 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 { type ReturnType struct {
Node Node
ColonTkn *token.Token ColonTkn *token.Token

View File

@ -93,20 +93,6 @@ func (t *DFS) Traverse(n ast.Vertex) {
if !t.visitor.EnterNode(nn) { if !t.visitor.EnterNode(nn) {
return 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: case *ast.Argument:
if nn == nil { if nn == nil {
return return
@ -195,10 +181,12 @@ func (t *DFS) Traverse(n ast.Vertex) {
} }
t.visitor.Leave("Modifiers", false) t.visitor.Leave("Modifiers", false)
} }
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)
} }
if nn.Extends != nil { if nn.Extends != nil {
t.visitor.Enter("Extends", true) 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)) 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) { func (v *Dump) Argument(n *ast.Argument) {
v.printIndentIfNotSingle(v.indent - 1) v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.Argument{\n") v.print("&ast.Argument{\n")

View File

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

View File

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

View File

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