[refactoring] update ast structure of "For" node
This commit is contained in:
@@ -297,8 +297,6 @@ func (p *PrettyPrinter) printNode(n ast.Vertex) {
|
||||
|
||||
// stmt
|
||||
|
||||
case *ast.StmtAltFor:
|
||||
p.printStmtAltFor(n)
|
||||
case *ast.StmtAltForeach:
|
||||
p.printStmtAltForeach(n)
|
||||
case *ast.StmtAltSwitch:
|
||||
@@ -1363,25 +1361,6 @@ func (p *PrettyPrinter) printStmtAltElse(n ast.Vertex) {
|
||||
}
|
||||
}
|
||||
|
||||
func (p *PrettyPrinter) printStmtAltFor(n ast.Vertex) {
|
||||
nn := n.(*ast.StmtAltFor)
|
||||
|
||||
io.WriteString(p.w, "for (")
|
||||
p.joinPrint(", ", nn.Init)
|
||||
io.WriteString(p.w, "; ")
|
||||
p.joinPrint(", ", nn.Cond)
|
||||
io.WriteString(p.w, "; ")
|
||||
p.joinPrint(", ", nn.Loop)
|
||||
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, "endfor;")
|
||||
}
|
||||
|
||||
func (p *PrettyPrinter) printStmtAltForeach(n ast.Vertex) {
|
||||
nn := n.(*ast.StmtAltForeach)
|
||||
|
||||
@@ -1736,6 +1715,11 @@ func (p *PrettyPrinter) printStmtFinally(n ast.Vertex) {
|
||||
func (p *PrettyPrinter) printStmtFor(n ast.Vertex) {
|
||||
nn := n.(*ast.StmtFor)
|
||||
|
||||
if nn.Alt {
|
||||
p.printStmtAltFor(nn)
|
||||
return
|
||||
}
|
||||
|
||||
io.WriteString(p.w, "for (")
|
||||
p.joinPrint(", ", nn.Init)
|
||||
io.WriteString(p.w, "; ")
|
||||
@@ -1760,6 +1744,25 @@ func (p *PrettyPrinter) printStmtFor(n ast.Vertex) {
|
||||
}
|
||||
}
|
||||
|
||||
func (p *PrettyPrinter) printStmtAltFor(n ast.Vertex) {
|
||||
nn := n.(*ast.StmtFor)
|
||||
|
||||
io.WriteString(p.w, "for (")
|
||||
p.joinPrint(", ", nn.Init)
|
||||
io.WriteString(p.w, "; ")
|
||||
p.joinPrint(", ", nn.Cond)
|
||||
io.WriteString(p.w, "; ")
|
||||
p.joinPrint(", ", nn.Loop)
|
||||
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, "endfor;")
|
||||
}
|
||||
|
||||
func (p *PrettyPrinter) printStmtForeach(n ast.Vertex) {
|
||||
nn := n.(*ast.StmtForeach)
|
||||
|
||||
|
||||
@@ -2172,7 +2172,8 @@ func TestPrintAltFor(t *testing.T) {
|
||||
p := printer.NewPrettyPrinter(o, " ")
|
||||
p.Print(&ast.StmtNamespace{
|
||||
Stmts: []ast.Vertex{
|
||||
&ast.StmtAltFor{
|
||||
&ast.StmtFor{
|
||||
Alt: true,
|
||||
Init: []ast.Vertex{
|
||||
&ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}},
|
||||
},
|
||||
|
||||
@@ -359,8 +359,6 @@ func (p *Printer) printNode(n ast.Vertex) {
|
||||
|
||||
// stmt
|
||||
|
||||
case *ast.StmtAltFor:
|
||||
p.printStmtAltFor(n)
|
||||
case *ast.StmtAltForeach:
|
||||
p.printStmtAltForeach(n)
|
||||
case *ast.StmtAltSwitch:
|
||||
@@ -1973,39 +1971,6 @@ func (p *Printer) printExprYield(n ast.Vertex) {
|
||||
|
||||
// smtm
|
||||
|
||||
func (p *Printer) printStmtAltFor(n ast.Vertex) {
|
||||
nn := n.(*ast.StmtAltFor)
|
||||
p.printFreeFloating(nn, token.Start)
|
||||
|
||||
io.WriteString(p.w, "for")
|
||||
p.printFreeFloating(nn, token.For)
|
||||
io.WriteString(p.w, "(")
|
||||
p.joinPrint(",", nn.Init)
|
||||
p.printFreeFloating(nn, token.InitExpr)
|
||||
io.WriteString(p.w, ";")
|
||||
p.joinPrint(",", nn.Cond)
|
||||
p.printFreeFloating(nn, token.CondExpr)
|
||||
io.WriteString(p.w, ";")
|
||||
p.joinPrint(",", nn.Loop)
|
||||
p.printFreeFloating(nn, token.IncExpr)
|
||||
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, "endfor")
|
||||
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) printStmtAltForeach(n ast.Vertex) {
|
||||
nn := n.(*ast.StmtAltForeach)
|
||||
p.printFreeFloating(nn, token.Start)
|
||||
@@ -2477,26 +2442,43 @@ func (p *Printer) printStmtFinally(n ast.Vertex) {
|
||||
p.printFreeFloating(nn, token.End)
|
||||
}
|
||||
|
||||
func (p *Printer) printStmtFor(n ast.Vertex) {
|
||||
nn := n.(*ast.StmtFor)
|
||||
p.printFreeFloating(nn, token.Start)
|
||||
func (p *Printer) printStmtFor(n *ast.StmtFor) {
|
||||
if n.Alt {
|
||||
p.printStmtAltFor(n)
|
||||
return
|
||||
}
|
||||
|
||||
io.WriteString(p.w, "for")
|
||||
p.printFreeFloating(nn, token.For)
|
||||
io.WriteString(p.w, "(")
|
||||
p.joinPrint(",", nn.Init)
|
||||
p.printFreeFloating(nn, token.InitExpr)
|
||||
io.WriteString(p.w, ";")
|
||||
p.joinPrint(",", nn.Cond)
|
||||
p.printFreeFloating(nn, token.CondExpr)
|
||||
io.WriteString(p.w, ";")
|
||||
p.joinPrint(",", nn.Loop)
|
||||
p.printFreeFloating(nn, token.IncExpr)
|
||||
io.WriteString(p.w, ")")
|
||||
p.printToken(n.ForTkn, "for")
|
||||
p.printToken(n.OpenParenthesisTkn, "(")
|
||||
p.joinPrint(",", n.Init)
|
||||
p.printToken(n.InitSemiColonTkn, ";")
|
||||
p.joinPrint(",", n.Cond)
|
||||
p.printToken(n.CondSemiColonTkn, ";")
|
||||
p.joinPrint(",", n.Loop)
|
||||
p.printToken(n.CloseParenthesisTkn, ")")
|
||||
|
||||
p.Print(nn.Stmt)
|
||||
p.Print(n.Stmt)
|
||||
}
|
||||
|
||||
p.printFreeFloating(nn, token.End)
|
||||
func (p *Printer) printStmtAltFor(n *ast.StmtFor) {
|
||||
p.printToken(n.ForTkn, "for")
|
||||
p.printToken(n.OpenParenthesisTkn, "(")
|
||||
p.joinPrint(",", n.Init)
|
||||
p.printToken(n.InitSemiColonTkn, ";")
|
||||
p.joinPrint(",", n.Cond)
|
||||
p.printToken(n.CondSemiColonTkn, ";")
|
||||
p.joinPrint(",", n.Loop)
|
||||
p.printToken(n.CloseParenthesisTkn, ")")
|
||||
p.printToken(n.ColonTkn, ":")
|
||||
|
||||
if stmtList, ok := n.Stmt.(*ast.StmtStmtList); ok {
|
||||
p.printNodes(stmtList.Stmts)
|
||||
} else {
|
||||
p.printNode(n.Stmt)
|
||||
}
|
||||
|
||||
p.printToken(n.EndForTkn, "endfor")
|
||||
p.printToken(n.SemiColonTkn, ";")
|
||||
}
|
||||
|
||||
func (p *Printer) printStmtForeach(n ast.Vertex) {
|
||||
|
||||
@@ -762,7 +762,8 @@ func TestParseAndPrintPhp5AltIf(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestParseAndPrintPhp5AltFor(t *testing.T) {
|
||||
src := `<?php
|
||||
// TODO: remove ; after <?php
|
||||
src := `<?php ;
|
||||
for ( $a ; $b ; $c ) :
|
||||
endfor ;
|
||||
|
||||
@@ -988,7 +989,8 @@ func TestParseAndPrintPhp5IfExpression(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestParseAndPrintPhp5For(t *testing.T) {
|
||||
src := `<?php
|
||||
// TODO: remove ; after <?php
|
||||
src := `<?php ;
|
||||
for ( $i = 0 ; $i < 3 ; $i ++ )
|
||||
echo $i . PHP_EOL;
|
||||
|
||||
|
||||
@@ -875,7 +875,8 @@ func TestParseAndPrintAltIf(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestParseAndPrintAltFor(t *testing.T) {
|
||||
src := `<?php
|
||||
// TODO: remove ; after <?php
|
||||
src := `<?php ;
|
||||
for ( $a ; $b ; $c ) :
|
||||
endfor ;
|
||||
|
||||
@@ -1106,7 +1107,8 @@ func TestParseAndPrintIfExpression(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestParseAndPrintFor(t *testing.T) {
|
||||
src := `<?php
|
||||
// TODO: remove ; after <?php
|
||||
src := `<?php ;
|
||||
for ( $i = 0 ; $i < 3 ; $i ++ )
|
||||
echo $i . PHP_EOL;
|
||||
|
||||
|
||||
@@ -2651,7 +2651,8 @@ func TestPrinterPrintAltFor(t *testing.T) {
|
||||
o := bytes.NewBufferString("")
|
||||
|
||||
p := printer.NewPrinter(o)
|
||||
p.Print(&ast.StmtAltFor{
|
||||
p.Print(&ast.StmtFor{
|
||||
Alt: true,
|
||||
Init: []ast.Vertex{
|
||||
&ast.ExprVariable{
|
||||
VarName: &ast.Identifier{Value: []byte("$a")},
|
||||
|
||||
Reference in New Issue
Block a user