[refactoring] update ast structure of "Switch", "Case", "Default" nodes; remove "CaseList" node

This commit is contained in:
Vadym Slizov
2020-09-06 12:45:08 +03:00
parent 0e73cd8852
commit f6cb2bff4d
21 changed files with 1882 additions and 2254 deletions

View File

@@ -28,10 +28,8 @@ type NodeVisitor interface {
Argument(n *Argument)
StmtAltForeach(n *StmtAltForeach)
StmtAltSwitch(n *StmtAltSwitch)
StmtBreak(n *StmtBreak)
StmtCase(n *StmtCase)
StmtCaseList(n *StmtCaseList)
StmtCatch(n *StmtCatch)
StmtClass(n *StmtClass)
StmtClassConstList(n *StmtClassConstList)

View File

@@ -188,17 +188,6 @@ func (n *StmtAltForeach) Accept(v NodeVisitor) {
v.StmtAltForeach(n)
}
// StmtAltSwitch node
type StmtAltSwitch struct {
Node
Cond Vertex
CaseList *StmtCaseList
}
func (n *StmtAltSwitch) Accept(v NodeVisitor) {
v.StmtAltSwitch(n)
}
// StmtBreak node
type StmtBreak struct {
Node
@@ -212,24 +201,16 @@ func (n *StmtBreak) Accept(v NodeVisitor) {
// StmtCase node
type StmtCase struct {
Node
Cond Vertex
Stmts []Vertex
CaseTkn *token.Token
Cond Vertex
CaseSeparatorTkn *token.Token
Stmts []Vertex
}
func (n *StmtCase) Accept(v NodeVisitor) {
v.StmtCase(n)
}
// StmtCaseList node
type StmtCaseList struct {
Node
Cases []Vertex
}
func (n *StmtCaseList) Accept(v NodeVisitor) {
v.StmtCaseList(n)
}
// StmtCatch node
type StmtCatch struct {
Node
@@ -355,7 +336,9 @@ func (n *StmtDeclare) Accept(v NodeVisitor) {
// StmtDefault node
type StmtDefault struct {
Node
Stmts []Vertex
DefaultTkn *token.Token
CaseSeparatorTkn *token.Token
Stmts []Vertex
}
func (n *StmtDefault) Accept(v NodeVisitor) {
@@ -674,8 +657,18 @@ func (n *StmtStmtList) Accept(v NodeVisitor) {
// StmtSwitch node
type StmtSwitch struct {
Node
Cond Vertex
CaseList *StmtCaseList
Alt bool
SwitchTkn *token.Token
OpenParenthesisTkn *token.Token
Cond Vertex
CloseParenthesisTkn *token.Token
ColonTkn *token.Token
OpenCurlyBracketTkn *token.Token
CaseSeparatorTkn *token.Token
CaseList []Vertex
CloseCurlyBracketTkn *token.Token
EndSwitchTkn *token.Token
SemiColonTkn *token.Token
}
func (n *StmtSwitch) Accept(v NodeVisitor) {

View File

@@ -146,23 +146,6 @@ func (t *DFS) Traverse(n ast.Vertex) {
t.Traverse(nn.Stmt)
t.visitor.Leave("Stmt", true)
}
case *ast.StmtAltSwitch:
if nn == nil {
return
}
if !t.visitor.EnterNode(nn) {
return
}
if nn.Cond != nil {
t.visitor.Enter("Cond", true)
t.Traverse(nn.Cond)
t.visitor.Leave("Cond", true)
}
if nn.CaseList != nil {
t.visitor.Enter("CaseList", true)
t.Traverse(nn.CaseList)
t.visitor.Leave("CaseList", true)
}
case *ast.StmtBreak:
if nn == nil {
return
@@ -194,20 +177,6 @@ func (t *DFS) Traverse(n ast.Vertex) {
}
t.visitor.Leave("Stmts", false)
}
case *ast.StmtCaseList:
if nn == nil {
return
}
if !t.visitor.EnterNode(nn) {
return
}
if nn.Cases != nil {
t.visitor.Enter("Cases", false)
for _, c := range nn.Cases {
t.Traverse(c)
}
t.visitor.Leave("Cases", false)
}
case *ast.StmtCatch:
if nn == nil {
return
@@ -869,9 +838,11 @@ func (t *DFS) Traverse(n ast.Vertex) {
t.visitor.Leave("Cond", true)
}
if nn.CaseList != nil {
t.visitor.Enter("CaseList", true)
t.Traverse(nn.CaseList)
t.visitor.Leave("CaseList", true)
t.visitor.Enter("CaseList", false)
for _, c := range nn.CaseList {
t.Traverse(c)
}
t.visitor.Leave("CaseList", false)
}
case *ast.StmtThrow:
if nn == nil {

View File

@@ -258,12 +258,6 @@ func (v *Dump) StmtAltForeach(n *ast.StmtAltForeach) {
v.printNode(n.GetNode())
}
func (v *Dump) StmtAltSwitch(n *ast.StmtAltSwitch) {
v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.StmtAltSwitch{\n")
v.printNode(n.GetNode())
}
func (v *Dump) StmtBreak(n *ast.StmtBreak) {
v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.StmtBreak{\n")
@@ -276,12 +270,6 @@ func (v *Dump) StmtCase(n *ast.StmtCase) {
v.printNode(n.GetNode())
}
func (v *Dump) StmtCaseList(n *ast.StmtCaseList) {
v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.StmtCaseList{\n")
v.printNode(n.GetNode())
}
func (v *Dump) StmtCatch(n *ast.StmtCatch) {
v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.StmtCatch{\n")
@@ -540,6 +528,11 @@ func (v *Dump) StmtSwitch(n *ast.StmtSwitch) {
v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.StmtSwitch{\n")
v.printNode(n.GetNode())
if n.Alt {
v.printIndent(v.indent)
v.print("Alt: true,\n")
}
}
func (v *Dump) StmtThrow(n *ast.StmtThrow) {

View File

@@ -13,26 +13,6 @@ func (v *FilterParserNodes) EnterNode(n ast.Vertex) bool {
return true
}
func (v *FilterParserNodes) StmtSwitch(n *ast.StmtSwitch) {
for {
if nn, ok := n.Cond.(*ast.ParserBrackets); ok {
n.Cond = nn.Child
} else {
break
}
}
}
func (v *FilterParserNodes) StmtAltSwitch(n *ast.StmtAltSwitch) {
for {
if nn, ok := n.Cond.(*ast.ParserBrackets); ok {
n.Cond = nn.Child
} else {
break
}
}
}
func (v *FilterParserNodes) ExprExit(n *ast.ExprExit) {
for {
if nn, ok := n.Expr.(*ast.ParserBrackets); ok {

View File

@@ -141,3 +141,25 @@ func (v *FilterTokens) StmtFor(n *ast.StmtFor) {
n.EndForTkn = nil
n.SemiColonTkn = nil
}
func (v *FilterTokens) StmtSwitch(n *ast.StmtSwitch) {
n.SwitchTkn = nil
n.OpenParenthesisTkn = nil
n.CloseParenthesisTkn = nil
n.OpenCurlyBracketTkn = nil
n.CaseSeparatorTkn = nil
n.ColonTkn = nil
n.CloseCurlyBracketTkn = nil
n.EndSwitchTkn = nil
n.SemiColonTkn = nil
}
func (v *FilterTokens) StmtCase(n *ast.StmtCase) {
n.CaseTkn = nil
n.CaseSeparatorTkn = nil
}
func (v *FilterTokens) StmtDefault(n *ast.StmtDefault) {
n.DefaultTkn = nil
n.CaseSeparatorTkn = nil
}

View File

@@ -58,10 +58,6 @@ func (v *Null) StmtAltForeach(_ *ast.StmtAltForeach) {
// do nothing
}
func (v *Null) StmtAltSwitch(_ *ast.StmtAltSwitch) {
// do nothing
}
func (v *Null) StmtBreak(_ *ast.StmtBreak) {
// do nothing
}
@@ -70,10 +66,6 @@ func (v *Null) StmtCase(_ *ast.StmtCase) {
// do nothing
}
func (v *Null) StmtCaseList(_ *ast.StmtCaseList) {
// do nothing
}
func (v *Null) StmtCatch(_ *ast.StmtCatch) {
// do nothing
}