[refactoring] update ast structure of "While" node

This commit is contained in:
Vadym Slizov
2020-09-04 10:33:47 +03:00
parent 6976388a82
commit 3b85f5e82b
21 changed files with 1066 additions and 1162 deletions

View File

@@ -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;")
}

View File

@@ -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{

View File

@@ -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) {

View File

@@ -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 ) ;

View File

@@ -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 ) ;

View File

@@ -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")},
},