[refactoring] update ast structure of "While" node
This commit is contained in:
@@ -30,7 +30,6 @@ type NodeVisitor interface {
|
||||
StmtAltFor(n *StmtAltFor)
|
||||
StmtAltForeach(n *StmtAltForeach)
|
||||
StmtAltSwitch(n *StmtAltSwitch)
|
||||
StmtAltWhile(n *StmtAltWhile)
|
||||
StmtBreak(n *StmtBreak)
|
||||
StmtCase(n *StmtCase)
|
||||
StmtCaseList(n *StmtCaseList)
|
||||
|
||||
@@ -212,17 +212,6 @@ func (n *StmtAltSwitch) Accept(v NodeVisitor) {
|
||||
v.StmtAltSwitch(n)
|
||||
}
|
||||
|
||||
// StmtAltWhile node
|
||||
type StmtAltWhile struct {
|
||||
Node
|
||||
Cond Vertex
|
||||
Stmt Vertex
|
||||
}
|
||||
|
||||
func (n *StmtAltWhile) Accept(v NodeVisitor) {
|
||||
v.StmtAltWhile(n)
|
||||
}
|
||||
|
||||
// StmtBreak node
|
||||
type StmtBreak struct {
|
||||
Node
|
||||
@@ -839,8 +828,15 @@ func (n *StmtUseDeclaration) Accept(v NodeVisitor) {
|
||||
// StmtWhile node
|
||||
type StmtWhile struct {
|
||||
Node
|
||||
Cond Vertex
|
||||
Stmt Vertex
|
||||
Alt bool
|
||||
WhileTkn *token.Token
|
||||
OpenParenthesisTkn *token.Token
|
||||
Cond Vertex
|
||||
CloseParenthesisTkn *token.Token
|
||||
ColonTkn *token.Token
|
||||
Stmt Vertex
|
||||
EndWhileTkn *token.Token
|
||||
SemiColonTkn *token.Token
|
||||
}
|
||||
|
||||
func (n *StmtWhile) Accept(v NodeVisitor) {
|
||||
|
||||
@@ -196,23 +196,6 @@ func (t *DFS) Traverse(n ast.Vertex) {
|
||||
t.Traverse(nn.CaseList)
|
||||
t.visitor.Leave("CaseList", true)
|
||||
}
|
||||
case *ast.StmtAltWhile:
|
||||
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.Stmt != nil {
|
||||
t.visitor.Enter("Stmt", true)
|
||||
t.Traverse(nn.Stmt)
|
||||
t.visitor.Leave("Stmt", true)
|
||||
}
|
||||
case *ast.StmtBreak:
|
||||
if nn == nil {
|
||||
return
|
||||
|
||||
@@ -270,12 +270,6 @@ func (v *Dump) StmtAltSwitch(n *ast.StmtAltSwitch) {
|
||||
v.printNode(n.GetNode())
|
||||
}
|
||||
|
||||
func (v *Dump) StmtAltWhile(n *ast.StmtAltWhile) {
|
||||
v.printIndentIfNotSingle(v.indent - 1)
|
||||
v.print("&ast.StmtAltWhile{\n")
|
||||
v.printNode(n.GetNode())
|
||||
}
|
||||
|
||||
func (v *Dump) StmtBreak(n *ast.StmtBreak) {
|
||||
v.printIndentIfNotSingle(v.indent - 1)
|
||||
v.print("&ast.StmtBreak{\n")
|
||||
@@ -637,6 +631,11 @@ func (v *Dump) StmtWhile(n *ast.StmtWhile) {
|
||||
v.printIndentIfNotSingle(v.indent - 1)
|
||||
v.print("&ast.StmtWhile{\n")
|
||||
v.printNode(n.GetNode())
|
||||
|
||||
if n.Alt {
|
||||
v.printIndent(v.indent)
|
||||
v.print("Alt: true,\n")
|
||||
}
|
||||
}
|
||||
|
||||
func (v *Dump) ExprArray(n *ast.ExprArray) {
|
||||
|
||||
@@ -13,26 +13,6 @@ func (v *FilterParserNodes) EnterNode(n ast.Vertex) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (v *FilterParserNodes) StmtWhile(n *ast.StmtWhile) {
|
||||
for {
|
||||
if nn, ok := n.Cond.(*ast.ParserBrackets); ok {
|
||||
n.Cond = nn.Child
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (v *FilterParserNodes) StmtAltWhile(n *ast.StmtAltWhile) {
|
||||
for {
|
||||
if nn, ok := n.Cond.(*ast.ParserBrackets); ok {
|
||||
n.Cond = nn.Child
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (v *FilterParserNodes) StmtDo(n *ast.StmtDo) {
|
||||
for {
|
||||
if nn, ok := n.Cond.(*ast.ParserBrackets); ok {
|
||||
|
||||
@@ -113,3 +113,12 @@ func (v *FilterTokens) ParserBrackets(n *ast.ParserBrackets) {
|
||||
n.OpenBracketTkn = nil
|
||||
n.CloseBracketTkn = nil
|
||||
}
|
||||
|
||||
func (v *FilterTokens) StmtWhile(n *ast.StmtWhile) {
|
||||
n.WhileTkn = nil
|
||||
n.OpenParenthesisTkn = nil
|
||||
n.CloseParenthesisTkn = nil
|
||||
n.ColonTkn = nil
|
||||
n.EndWhileTkn = nil
|
||||
n.SemiColonTkn = nil
|
||||
}
|
||||
|
||||
@@ -66,10 +66,6 @@ func (v *Null) StmtAltSwitch(_ *ast.StmtAltSwitch) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
func (v *Null) StmtAltWhile(_ *ast.StmtAltWhile) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
func (v *Null) StmtBreak(_ *ast.StmtBreak) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
@@ -303,8 +303,6 @@ func (p *PrettyPrinter) printNode(n ast.Vertex) {
|
||||
p.printStmtAltForeach(n)
|
||||
case *ast.StmtAltSwitch:
|
||||
p.printStmtAltSwitch(n)
|
||||
case *ast.StmtAltWhile:
|
||||
p.printStmtAltWhile(n)
|
||||
case *ast.StmtBreak:
|
||||
p.printStmtBreak(n)
|
||||
case *ast.StmtCase:
|
||||
@@ -1450,21 +1448,6 @@ func (p *PrettyPrinter) printStmtAltSwitch(n ast.Vertex) {
|
||||
io.WriteString(p.w, "endswitch;")
|
||||
}
|
||||
|
||||
func (p *PrettyPrinter) printStmtAltWhile(n ast.Vertex) {
|
||||
nn := n.(*ast.StmtAltWhile)
|
||||
|
||||
io.WriteString(p.w, "while (")
|
||||
p.Print(nn.Cond)
|
||||
io.WriteString(p.w, ") :\n")
|
||||
|
||||
s := nn.Stmt.(*ast.StmtStmtList)
|
||||
p.printNodes(s.Stmts)
|
||||
|
||||
io.WriteString(p.w, "\n")
|
||||
p.printIndent()
|
||||
io.WriteString(p.w, "endwhile;")
|
||||
}
|
||||
|
||||
func (p *PrettyPrinter) printStmtBreak(n ast.Vertex) {
|
||||
nn := n.(*ast.StmtBreak)
|
||||
|
||||
@@ -2190,6 +2173,11 @@ func (p *PrettyPrinter) printStmtUseDeclaration(n ast.Vertex) {
|
||||
func (p *PrettyPrinter) printStmtWhile(n ast.Vertex) {
|
||||
nn := n.(*ast.StmtWhile)
|
||||
|
||||
if nn.Alt {
|
||||
p.printStmtAltWhile(nn)
|
||||
return
|
||||
}
|
||||
|
||||
io.WriteString(p.w, "while (")
|
||||
p.Print(nn.Cond)
|
||||
io.WriteString(p.w, ")")
|
||||
@@ -2209,3 +2197,18 @@ func (p *PrettyPrinter) printStmtWhile(n ast.Vertex) {
|
||||
p.indentDepth--
|
||||
}
|
||||
}
|
||||
|
||||
func (p *PrettyPrinter) printStmtAltWhile(n ast.Vertex) {
|
||||
nn := n.(*ast.StmtWhile)
|
||||
|
||||
io.WriteString(p.w, "while (")
|
||||
p.Print(nn.Cond)
|
||||
io.WriteString(p.w, ") :\n")
|
||||
|
||||
s := nn.Stmt.(*ast.StmtStmtList)
|
||||
p.printNodes(s.Stmts)
|
||||
|
||||
io.WriteString(p.w, "\n")
|
||||
p.printIndent()
|
||||
io.WriteString(p.w, "endwhile;")
|
||||
}
|
||||
|
||||
@@ -2091,7 +2091,7 @@ func TestPrintAltElseIf(t *testing.T) {
|
||||
|
||||
p := printer.NewPrettyPrinter(o, " ")
|
||||
p.Print(&ast.StmtElseIf{
|
||||
Alt: true,
|
||||
Alt: true,
|
||||
Cond: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}},
|
||||
Stmt: &ast.StmtStmtList{
|
||||
Stmts: []ast.Vertex{
|
||||
@@ -2114,7 +2114,7 @@ func TestPrintAltElseIfEmpty(t *testing.T) {
|
||||
|
||||
p := printer.NewPrettyPrinter(o, " ")
|
||||
p.Print(&ast.StmtElseIf{
|
||||
Alt: true,
|
||||
Alt: true,
|
||||
Cond: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}},
|
||||
Stmt: &ast.StmtStmtList{},
|
||||
})
|
||||
@@ -2154,7 +2154,7 @@ func TestPrintAltElseEmpty(t *testing.T) {
|
||||
|
||||
p := printer.NewPrettyPrinter(o, " ")
|
||||
p.Print(&ast.StmtElse{
|
||||
Alt: true,
|
||||
Alt: true,
|
||||
Stmt: &ast.StmtStmtList{},
|
||||
})
|
||||
|
||||
@@ -2241,7 +2241,7 @@ func TestPrintAltIf(t *testing.T) {
|
||||
p.Print(&ast.StmtNamespace{
|
||||
Stmts: []ast.Vertex{
|
||||
&ast.StmtIf{
|
||||
Alt: true,
|
||||
Alt: true,
|
||||
Cond: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}},
|
||||
Stmt: &ast.StmtStmtList{
|
||||
Stmts: []ast.Vertex{
|
||||
@@ -2250,7 +2250,7 @@ func TestPrintAltIf(t *testing.T) {
|
||||
},
|
||||
ElseIf: []ast.Vertex{
|
||||
&ast.StmtElseIf{
|
||||
Alt: true,
|
||||
Alt: true,
|
||||
Cond: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}},
|
||||
Stmt: &ast.StmtStmtList{
|
||||
Stmts: []ast.Vertex{
|
||||
@@ -2259,7 +2259,7 @@ func TestPrintAltIf(t *testing.T) {
|
||||
},
|
||||
},
|
||||
&ast.StmtElseIf{
|
||||
Alt: true,
|
||||
Alt: true,
|
||||
Cond: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("c")}},
|
||||
Stmt: &ast.StmtStmtList{},
|
||||
},
|
||||
@@ -2342,7 +2342,8 @@ func TestPrintAltWhile(t *testing.T) {
|
||||
p := printer.NewPrettyPrinter(o, " ")
|
||||
p.Print(&ast.StmtNamespace{
|
||||
Stmts: []ast.Vertex{
|
||||
&ast.StmtAltWhile{
|
||||
&ast.StmtWhile{
|
||||
Alt: true,
|
||||
Cond: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}},
|
||||
Stmt: &ast.StmtStmtList{
|
||||
Stmts: []ast.Vertex{
|
||||
|
||||
@@ -365,8 +365,6 @@ func (p *Printer) printNode(n ast.Vertex) {
|
||||
p.printStmtAltForeach(n)
|
||||
case *ast.StmtAltSwitch:
|
||||
p.printStmtAltSwitch(n)
|
||||
case *ast.StmtAltWhile:
|
||||
p.printStmtAltWhile(n)
|
||||
case *ast.StmtBreak:
|
||||
p.printStmtBreak(n)
|
||||
case *ast.StmtCase:
|
||||
@@ -2091,39 +2089,6 @@ func (p *Printer) printStmtAltSwitch(n ast.Vertex) {
|
||||
p.printFreeFloating(nn, token.End)
|
||||
}
|
||||
|
||||
func (p *Printer) printStmtAltWhile(n ast.Vertex) {
|
||||
nn := n.(*ast.StmtAltWhile)
|
||||
p.printFreeFloating(nn, token.Start)
|
||||
|
||||
io.WriteString(p.w, "while")
|
||||
|
||||
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, ":")
|
||||
|
||||
s := nn.Stmt.(*ast.StmtStmtList)
|
||||
p.printNodes(s.Stmts)
|
||||
p.printFreeFloating(nn, token.Stmts)
|
||||
|
||||
io.WriteString(p.w, "endwhile")
|
||||
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)
|
||||
@@ -3169,25 +3134,35 @@ func (p *Printer) printStmtUseDeclaration(n *ast.StmtUseDeclaration) {
|
||||
p.printToken(n.CommaTkn, "")
|
||||
}
|
||||
|
||||
func (p *Printer) printStmtWhile(n ast.Vertex) {
|
||||
nn := n.(*ast.StmtWhile)
|
||||
p.printFreeFloating(nn, token.Start)
|
||||
|
||||
io.WriteString(p.w, "while")
|
||||
|
||||
if _, ok := nn.Cond.(*ast.ParserBrackets); !ok {
|
||||
io.WriteString(p.w, "(")
|
||||
func (p *Printer) printStmtWhile(n *ast.StmtWhile) {
|
||||
if n.Alt {
|
||||
p.printStmtAltWhile(n)
|
||||
return
|
||||
}
|
||||
|
||||
p.Print(nn.Cond)
|
||||
p.printToken(n.WhileTkn, "while")
|
||||
p.printToken(n.OpenParenthesisTkn, "(")
|
||||
p.Print(n.Cond)
|
||||
p.printToken(n.CloseParenthesisTkn, ")")
|
||||
|
||||
if _, ok := nn.Cond.(*ast.ParserBrackets); !ok {
|
||||
io.WriteString(p.w, ")")
|
||||
p.Print(n.Stmt)
|
||||
}
|
||||
|
||||
func (p *Printer) printStmtAltWhile(n *ast.StmtWhile) {
|
||||
p.printToken(n.WhileTkn, "while")
|
||||
p.printToken(n.OpenParenthesisTkn, "(")
|
||||
p.Print(n.Cond)
|
||||
p.printToken(n.CloseParenthesisTkn, ")")
|
||||
p.printToken(n.ColonTkn, ":")
|
||||
|
||||
if stmtList, ok := n.Stmt.(*ast.StmtStmtList); ok {
|
||||
p.printNodes(stmtList.Stmts)
|
||||
} else {
|
||||
p.Print(n.Stmt)
|
||||
}
|
||||
|
||||
p.Print(nn.Stmt)
|
||||
|
||||
p.printFreeFloating(nn, token.End)
|
||||
p.printToken(n.EndWhileTkn, "endwhile")
|
||||
p.printToken(n.SemiColonTkn, ";")
|
||||
}
|
||||
|
||||
func (p *Printer) printParserAs(n ast.Vertex) {
|
||||
|
||||
@@ -820,7 +820,8 @@ func TestParseAndPrintPhp5AltSwitch(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestParseAndPrintPhp5AltWhile(t *testing.T) {
|
||||
src := `<?php
|
||||
// TODO: remove ; after <?php
|
||||
src := `<?php ;
|
||||
|
||||
while ( $a ) :
|
||||
// do nothing
|
||||
@@ -1338,7 +1339,8 @@ func TestParseAndPrintPhp5UseList(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestParseAndPrintPhp5While(t *testing.T) {
|
||||
src := `<?php
|
||||
// TODO: remove ; after <?php
|
||||
src := `<?php ;
|
||||
while ( $a ) echo '' ;
|
||||
while ( $a ) { }
|
||||
while ( $a ) ;
|
||||
|
||||
@@ -933,7 +933,8 @@ func TestParseAndPrintAltSwitch(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestParseAndPrintAltWhile(t *testing.T) {
|
||||
src := `<?php
|
||||
// TODO: remove ; after <?php
|
||||
src := `<?php ;
|
||||
|
||||
while ( $a ) :
|
||||
// do nothing
|
||||
@@ -1471,7 +1472,8 @@ func TestParseAndPrintUseList(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestParseAndPrintWhile(t *testing.T) {
|
||||
src := `<?php
|
||||
// TODO: remove ; after <?php
|
||||
src := `<?php ;
|
||||
while ( $a ) echo '' ;
|
||||
while ( $a ) { }
|
||||
while ( $a ) ;
|
||||
|
||||
@@ -2635,7 +2635,7 @@ func TestPrinterPrintAltElseEmpty(t *testing.T) {
|
||||
|
||||
p := printer.NewPrinter(o)
|
||||
p.Print(&ast.StmtElse{
|
||||
Alt: true,
|
||||
Alt: true,
|
||||
Stmt: &ast.StmtStmtList{},
|
||||
})
|
||||
|
||||
@@ -2815,7 +2815,8 @@ func TestPrinterPrintAltWhile(t *testing.T) {
|
||||
o := bytes.NewBufferString("")
|
||||
|
||||
p := printer.NewPrinter(o)
|
||||
p.Print(&ast.StmtAltWhile{
|
||||
p.Print(&ast.StmtWhile{
|
||||
Alt: true,
|
||||
Cond: &ast.ExprVariable{
|
||||
VarName: &ast.Identifier{Value: []byte("$a")},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user