[refactoring] update ast structure of "Switch", "Case", "Default" nodes; remove "CaseList" node
This commit is contained in:
@@ -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)
|
||||
|
||||
|
||||
@@ -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")}}},
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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 : ;
|
||||
|
||||
@@ -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 : ;
|
||||
|
||||
@@ -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")},
|
||||
}},
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user