[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
}

View File

@@ -299,8 +299,6 @@ func (p *PrettyPrinter) printNode(n ast.Vertex) {
case *ast.StmtAltForeach:
p.printStmtAltForeach(n)
case *ast.StmtAltSwitch:
p.printStmtAltSwitch(n)
case *ast.StmtBreak:
p.printStmtBreak(n)
case *ast.StmtCase:
@@ -1412,21 +1410,6 @@ func (p *PrettyPrinter) printStmtAltIf(n ast.Vertex) {
io.WriteString(p.w, "endif;")
}
func (p *PrettyPrinter) printStmtAltSwitch(n ast.Vertex) {
nn := n.(*ast.StmtAltSwitch)
io.WriteString(p.w, "switch (")
p.Print(nn.Cond)
io.WriteString(p.w, ") :\n")
s := nn.CaseList.Cases
p.printNodes(s)
io.WriteString(p.w, "\n")
p.printIndent()
io.WriteString(p.w, "endswitch;")
}
func (p *PrettyPrinter) printStmtBreak(n ast.Vertex) {
nn := n.(*ast.StmtBreak)
@@ -2005,17 +1988,37 @@ func (p *PrettyPrinter) printStmtStmtList(n ast.Vertex) {
func (p *PrettyPrinter) printStmtSwitch(n ast.Vertex) {
nn := n.(*ast.StmtSwitch)
if nn.Alt {
p.printStmtAltSwitch(n)
return
}
io.WriteString(p.w, "switch (")
p.Print(nn.Cond)
io.WriteString(p.w, ")")
io.WriteString(p.w, " {\n")
p.printNodes(nn.CaseList.Cases)
p.printNodes(nn.CaseList)
io.WriteString(p.w, "\n")
p.printIndent()
io.WriteString(p.w, "}")
}
func (p *PrettyPrinter) printStmtAltSwitch(n ast.Vertex) {
nn := n.(*ast.StmtSwitch)
io.WriteString(p.w, "switch (")
p.Print(nn.Cond)
io.WriteString(p.w, ") :\n")
s := nn.CaseList
p.printNodes(s)
io.WriteString(p.w, "\n")
p.printIndent()
io.WriteString(p.w, "endswitch;")
}
func (p *PrettyPrinter) printStmtThrow(n ast.Vertex) {
nn := n.(*ast.StmtThrow)

View File

@@ -2300,21 +2300,20 @@ func TestPrintStmtAltSwitch(t *testing.T) {
p := printer.NewPrettyPrinter(o, " ")
p.Print(&ast.StmtNamespace{
Stmts: []ast.Vertex{
&ast.StmtAltSwitch{
&ast.StmtSwitch{
Alt: true,
Cond: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("var")}},
CaseList: &ast.StmtCaseList{
Cases: []ast.Vertex{
&ast.StmtCase{
Cond: &ast.ScalarString{Value: []byte("'a'")},
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}},
},
CaseList: []ast.Vertex{
&ast.StmtCase{
Cond: &ast.ScalarString{Value: []byte("'a'")},
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}},
},
&ast.StmtCase{
Cond: &ast.ScalarString{Value: []byte("'b'")},
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}},
},
},
&ast.StmtCase{
Cond: &ast.ScalarString{Value: []byte("'b'")},
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}},
},
},
},
@@ -3682,19 +3681,17 @@ func TestPrintStmtSwitch(t *testing.T) {
Stmts: []ast.Vertex{
&ast.StmtSwitch{
Cond: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("var")}},
CaseList: &ast.StmtCaseList{
Cases: []ast.Vertex{
&ast.StmtCase{
Cond: &ast.ScalarString{Value: []byte("'a'")},
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}},
},
CaseList: []ast.Vertex{
&ast.StmtCase{
Cond: &ast.ScalarString{Value: []byte("'a'")},
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}},
},
&ast.StmtCase{
Cond: &ast.ScalarString{Value: []byte("'b'")},
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}},
},
},
&ast.StmtCase{
Cond: &ast.ScalarString{Value: []byte("'b'")},
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}},
},
},
},

View File

@@ -361,8 +361,6 @@ func (p *Printer) printNode(n ast.Vertex) {
case *ast.StmtAltForeach:
p.printStmtAltForeach(n)
case *ast.StmtAltSwitch:
p.printStmtAltSwitch(n)
case *ast.StmtBreak:
p.printStmtBreak(n)
case *ast.StmtCase:
@@ -2019,41 +2017,6 @@ func (p *Printer) printStmtAltForeach(n ast.Vertex) {
p.printFreeFloating(nn, token.End)
}
func (p *Printer) printStmtAltSwitch(n ast.Vertex) {
nn := n.(*ast.StmtAltSwitch)
p.printFreeFloating(nn, token.Start)
io.WriteString(p.w, "switch")
if _, ok := nn.Cond.(*ast.ParserBrackets); !ok {
io.WriteString(p.w, "(")
}
p.Print(nn.Cond)
if _, ok := nn.Cond.(*ast.ParserBrackets); !ok {
io.WriteString(p.w, ")")
}
p.printFreeFloating(nn, token.Cond)
io.WriteString(p.w, ":")
p.printFreeFloating(nn.CaseList, token.Start)
p.printFreeFloating(nn.CaseList, token.CaseListStart)
p.printNodes(nn.CaseList.Cases)
p.printFreeFloating(nn.CaseList, token.CaseListEnd)
p.printFreeFloating(nn.CaseList, token.End)
io.WriteString(p.w, "endswitch")
p.printFreeFloating(nn, token.AltEnd)
p.printFreeFloating(nn, token.SemiColon)
if nn.GetNode().Tokens.IsEmpty() {
io.WriteString(p.w, ";")
}
p.printFreeFloating(nn, token.End)
}
func (p *Printer) printStmtBreak(n ast.Vertex) {
nn := n.(*ast.StmtBreak)
p.printFreeFloating(nn, token.Start)
@@ -2075,26 +2038,12 @@ func (p *Printer) printStmtBreak(n ast.Vertex) {
p.printFreeFloating(nn, token.End)
}
func (p *Printer) printStmtCase(n ast.Vertex) {
nn := n.(*ast.StmtCase)
p.printFreeFloating(nn, token.Start)
io.WriteString(p.w, "case")
if nn.Cond.GetNode().Tokens.IsEmpty() {
io.WriteString(p.w, " ")
}
p.Print(nn.Cond)
p.printFreeFloating(nn, token.Expr)
p.printFreeFloating(nn, token.CaseSeparator)
if nn.GetNode().Tokens.IsEmpty() {
io.WriteString(p.w, ":")
}
if len(nn.Stmts) > 0 {
p.printNodes(nn.Stmts)
}
p.printFreeFloating(nn, token.End)
func (p *Printer) printStmtCase(n *ast.StmtCase) {
p.printToken(n.CaseTkn, "case")
p.bufStart = " "
p.Print(n.Cond)
p.printToken(n.CaseSeparatorTkn, ":")
p.printNodes(n.Stmts)
}
func (p *Printer) printStmtCatch(n ast.Vertex) {
@@ -2308,22 +2257,10 @@ func (p *Printer) printStmtDeclare(n ast.Vertex) {
p.printFreeFloating(nn, token.End)
}
func (p *Printer) printStmtDefault(n ast.Vertex) {
nn := n.(*ast.StmtDefault)
p.printFreeFloating(nn, token.Start)
io.WriteString(p.w, "default")
p.printFreeFloating(nn, token.Default)
p.printFreeFloating(nn, token.CaseSeparator)
if nn.GetNode().Tokens.IsEmpty() {
io.WriteString(p.w, ":")
}
if len(nn.Stmts) > 0 {
p.printNodes(nn.Stmts)
}
p.printFreeFloating(nn, token.End)
func (p *Printer) printStmtDefault(n *ast.StmtDefault) {
p.printToken(n.DefaultTkn, "default")
p.printToken(n.CaseSeparatorTkn, ":")
p.printNodes(n.Stmts)
}
func (p *Printer) printStmtDo(n *ast.StmtDo) {
@@ -2828,31 +2765,32 @@ func (p *Printer) printStmtStmtList(n *ast.StmtStmtList) {
p.printToken(n.CloseCurlyBracket, "}")
}
func (p *Printer) printStmtSwitch(n ast.Vertex) {
nn := n.(*ast.StmtSwitch)
p.printFreeFloating(nn, token.Start)
io.WriteString(p.w, "switch")
if _, ok := nn.Cond.(*ast.ParserBrackets); !ok {
io.WriteString(p.w, "(")
func (p *Printer) printStmtSwitch(n *ast.StmtSwitch) {
if n.Alt {
p.printStmtAltSwitch(n)
return
}
p.Print(nn.Cond)
p.printToken(n.SwitchTkn, "switch")
p.printToken(n.OpenParenthesisTkn, "(")
p.Print(n.Cond)
p.printToken(n.CloseParenthesisTkn, ")")
p.printToken(n.OpenCurlyBracketTkn, "{")
p.printToken(n.CaseSeparatorTkn, "")
p.printNodes(n.CaseList)
p.printToken(n.CloseCurlyBracketTkn, "}")
}
if _, ok := nn.Cond.(*ast.ParserBrackets); !ok {
io.WriteString(p.w, ")")
}
p.printFreeFloating(nn.CaseList, token.Start)
io.WriteString(p.w, "{")
p.printFreeFloating(nn.CaseList, token.CaseListStart)
p.printNodes(nn.CaseList.Cases)
p.printFreeFloating(nn.CaseList, token.CaseListEnd)
io.WriteString(p.w, "}")
p.printFreeFloating(nn.CaseList, token.End)
p.printFreeFloating(nn, token.End)
func (p *Printer) printStmtAltSwitch(n *ast.StmtSwitch) {
p.printToken(n.SwitchTkn, "switch")
p.printToken(n.OpenParenthesisTkn, "(")
p.Print(n.Cond)
p.printToken(n.CloseParenthesisTkn, ")")
p.printToken(n.ColonTkn, ":")
p.printToken(n.CaseSeparatorTkn, "")
p.printNodes(n.CaseList)
p.printToken(n.EndSwitchTkn, "endswitch")
p.printToken(n.SemiColonTkn, ";")
}
func (p *Printer) printStmtThrow(n ast.Vertex) {

View File

@@ -791,7 +791,8 @@ func TestParseAndPrintPhp5AltForeach(t *testing.T) {
}
func TestParseAndPrintPhp5AltSwitch(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
switch ( $a ) :
case 1 :
@@ -1232,7 +1233,8 @@ func TestParseAndPrintPhp5StmtList(t *testing.T) {
}
func TestParseAndPrintPhp5Switch(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
switch ( $a ) {
case 1 : ;

View File

@@ -904,7 +904,8 @@ func TestParseAndPrintAltForeach(t *testing.T) {
}
func TestParseAndPrintAltSwitch(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
switch ( $a ) :
case 1 :
@@ -1365,7 +1366,8 @@ func TestParseAndPrintStmtList(t *testing.T) {
}
func TestParseAndPrintSwitch(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
switch ( $a ) {
case 1 : ;

View File

@@ -2778,27 +2778,26 @@ func TestPrinterPrintStmtAltSwitch(t *testing.T) {
o := bytes.NewBufferString("")
p := printer.NewPrinter(o)
p.Print(&ast.StmtAltSwitch{
p.Print(&ast.StmtSwitch{
Cond: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$var")},
},
CaseList: &ast.StmtCaseList{
Cases: []ast.Vertex{
&ast.StmtCase{
Cond: &ast.ScalarString{Value: []byte("'a'")},
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$a")},
}},
},
Alt: true,
CaseList: []ast.Vertex{
&ast.StmtCase{
Cond: &ast.ScalarString{Value: []byte("'a'")},
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$a")},
}},
},
&ast.StmtCase{
Cond: &ast.ScalarString{Value: []byte("'b'")},
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$b")},
}},
},
},
&ast.StmtCase{
Cond: &ast.ScalarString{Value: []byte("'b'")},
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$b")},
}},
},
},
},
@@ -4103,23 +4102,21 @@ func TestPrinterPrintStmtSwitch(t *testing.T) {
Cond: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$var")},
},
CaseList: &ast.StmtCaseList{
Cases: []ast.Vertex{
&ast.StmtCase{
Cond: &ast.ScalarString{Value: []byte("'a'")},
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$a")},
}},
},
CaseList: []ast.Vertex{
&ast.StmtCase{
Cond: &ast.ScalarString{Value: []byte("'a'")},
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$a")},
}},
},
&ast.StmtCase{
Cond: &ast.ScalarString{Value: []byte("'b'")},
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$b")},
}},
},
},
&ast.StmtCase{
Cond: &ast.ScalarString{Value: []byte("'b'")},
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$b")},
}},
},
},
},