[refactoring] update ast structure of "If", "ElseIf", "Else" nodes

This commit is contained in:
Vadym Slizov
2020-09-03 22:18:06 +03:00
parent 954208510e
commit 6976388a82
21 changed files with 1777 additions and 1817 deletions

View File

@@ -297,16 +297,10 @@ func (p *PrettyPrinter) printNode(n ast.Vertex) {
// stmt
case *ast.StmtAltElseIf:
p.printStmtAltElseIf(n)
case *ast.StmtAltElse:
p.printStmtAltElse(n)
case *ast.StmtAltFor:
p.printStmtAltFor(n)
case *ast.StmtAltForeach:
p.printStmtAltForeach(n)
case *ast.StmtAltIf:
p.printStmtAltIf(n)
case *ast.StmtAltSwitch:
p.printStmtAltSwitch(n)
case *ast.StmtAltWhile:
@@ -1348,7 +1342,7 @@ func (p *PrettyPrinter) printExprYield(n ast.Vertex) {
// smtm
func (p *PrettyPrinter) printStmtAltElseIf(n ast.Vertex) {
nn := n.(*ast.StmtAltElseIf)
nn := n.(*ast.StmtElseIf)
io.WriteString(p.w, "elseif (")
p.Print(nn.Cond)
@@ -1361,7 +1355,7 @@ func (p *PrettyPrinter) printStmtAltElseIf(n ast.Vertex) {
}
func (p *PrettyPrinter) printStmtAltElse(n ast.Vertex) {
nn := n.(*ast.StmtAltElse)
nn := n.(*ast.StmtElse)
io.WriteString(p.w, "else :")
@@ -1415,7 +1409,7 @@ func (p *PrettyPrinter) printStmtAltForeach(n ast.Vertex) {
}
func (p *PrettyPrinter) printStmtAltIf(n ast.Vertex) {
nn := n.(*ast.StmtAltIf)
nn := n.(*ast.StmtIf)
io.WriteString(p.w, "if (")
p.Print(nn.Cond)
@@ -1687,6 +1681,11 @@ func (p *PrettyPrinter) printStmtEcho(n ast.Vertex) {
func (p *PrettyPrinter) printStmtElseif(n ast.Vertex) {
nn := n.(*ast.StmtElseIf)
if nn.Alt {
p.printStmtAltElseIf(nn)
return
}
io.WriteString(p.w, "elseif (")
p.Print(nn.Cond)
io.WriteString(p.w, ")")
@@ -1710,6 +1709,11 @@ func (p *PrettyPrinter) printStmtElseif(n ast.Vertex) {
func (p *PrettyPrinter) printStmtElse(n ast.Vertex) {
nn := n.(*ast.StmtElse)
if nn.Alt {
p.printStmtAltElse(nn)
return
}
io.WriteString(p.w, "else")
switch s := nn.Stmt.(type) {
@@ -1854,6 +1858,11 @@ func (p *PrettyPrinter) printStmtHaltCompiler(n ast.Vertex) {
func (p *PrettyPrinter) printStmtIf(n ast.Vertex) {
nn := n.(*ast.StmtIf)
if nn.Alt {
p.printStmtAltIf(nn)
return
}
io.WriteString(p.w, "if (")
p.Print(nn.Cond)
io.WriteString(p.w, ")")

View File

@@ -2090,7 +2090,8 @@ func TestPrintAltElseIf(t *testing.T) {
o := bytes.NewBufferString("")
p := printer.NewPrettyPrinter(o, " ")
p.Print(&ast.StmtAltElseIf{
p.Print(&ast.StmtElseIf{
Alt: true,
Cond: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}},
Stmt: &ast.StmtStmtList{
Stmts: []ast.Vertex{
@@ -2112,7 +2113,8 @@ func TestPrintAltElseIfEmpty(t *testing.T) {
o := bytes.NewBufferString("")
p := printer.NewPrettyPrinter(o, " ")
p.Print(&ast.StmtAltElseIf{
p.Print(&ast.StmtElseIf{
Alt: true,
Cond: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}},
Stmt: &ast.StmtStmtList{},
})
@@ -2129,7 +2131,8 @@ func TestPrintAltElse(t *testing.T) {
o := bytes.NewBufferString("")
p := printer.NewPrettyPrinter(o, " ")
p.Print(&ast.StmtAltElse{
p.Print(&ast.StmtElse{
Alt: true,
Stmt: &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}},
@@ -2150,7 +2153,8 @@ func TestPrintAltElseEmpty(t *testing.T) {
o := bytes.NewBufferString("")
p := printer.NewPrettyPrinter(o, " ")
p.Print(&ast.StmtAltElse{
p.Print(&ast.StmtElse{
Alt: true,
Stmt: &ast.StmtStmtList{},
})
@@ -2236,7 +2240,8 @@ func TestPrintAltIf(t *testing.T) {
p := printer.NewPrettyPrinter(o, " ")
p.Print(&ast.StmtNamespace{
Stmts: []ast.Vertex{
&ast.StmtAltIf{
&ast.StmtIf{
Alt: true,
Cond: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}},
Stmt: &ast.StmtStmtList{
Stmts: []ast.Vertex{
@@ -2244,7 +2249,8 @@ func TestPrintAltIf(t *testing.T) {
},
},
ElseIf: []ast.Vertex{
&ast.StmtAltElseIf{
&ast.StmtElseIf{
Alt: true,
Cond: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}},
Stmt: &ast.StmtStmtList{
Stmts: []ast.Vertex{
@@ -2252,12 +2258,14 @@ func TestPrintAltIf(t *testing.T) {
},
},
},
&ast.StmtAltElseIf{
&ast.StmtElseIf{
Alt: true,
Cond: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("c")}},
Stmt: &ast.StmtStmtList{},
},
},
Else: &ast.StmtAltElse{
Else: &ast.StmtElse{
Alt: true,
Stmt: &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}},

View File

@@ -359,16 +359,10 @@ func (p *Printer) printNode(n ast.Vertex) {
// stmt
case *ast.StmtAltElseIf:
p.printStmtAltElseIf(n)
case *ast.StmtAltElse:
p.printStmtAltElse(n)
case *ast.StmtAltFor:
p.printStmtAltFor(n)
case *ast.StmtAltForeach:
p.printStmtAltForeach(n)
case *ast.StmtAltIf:
p.printStmtAltIf(n)
case *ast.StmtAltSwitch:
p.printStmtAltSwitch(n)
case *ast.StmtAltWhile:
@@ -1981,68 +1975,6 @@ func (p *Printer) printExprYield(n ast.Vertex) {
// smtm
func (p *Printer) printStmtAltElseIf(n ast.Vertex) {
nn := n.(*ast.StmtAltElseIf)
p.printFreeFloating(nn, token.Start)
io.WriteString(p.w, "elseif")
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, ")")
}
stmtList, _ := nn.Stmt.(*ast.StmtStmtList)
brackets, ok := nn.Stmt.(*ast.ParserBrackets)
if ok {
p.printFreeFloating(brackets, token.Start)
stmtList = brackets.Child.(*ast.StmtStmtList)
} else {
io.WriteString(p.w, ":")
}
p.printFreeFloating(stmtList, token.Stmts)
p.printNodes(stmtList.Stmts)
p.printFreeFloating(stmtList, token.End)
if ok {
p.printFreeFloating(brackets, token.End)
}
p.printFreeFloating(nn, token.End)
}
func (p *Printer) printStmtAltElse(n ast.Vertex) {
nn := n.(*ast.StmtAltElse)
p.printFreeFloating(nn, token.Start)
io.WriteString(p.w, "else")
stmtList, _ := nn.Stmt.(*ast.StmtStmtList)
brackets, ok := nn.Stmt.(*ast.ParserBrackets)
if ok {
p.printFreeFloating(brackets, token.Start)
stmtList = brackets.Child.(*ast.StmtStmtList)
} else {
io.WriteString(p.w, ":")
}
p.printFreeFloating(stmtList, token.Stmts)
p.printNodes(stmtList.Stmts)
p.printFreeFloating(stmtList, token.End)
if ok {
p.printFreeFloating(brackets, token.End)
}
p.printFreeFloating(nn, token.End)
}
func (p *Printer) printStmtAltFor(n ast.Vertex) {
nn := n.(*ast.StmtAltFor)
p.printFreeFloating(nn, token.Start)
@@ -2124,59 +2056,6 @@ func (p *Printer) printStmtAltForeach(n ast.Vertex) {
p.printFreeFloating(nn, token.End)
}
func (p *Printer) printStmtAltIf(n ast.Vertex) {
nn := n.(*ast.StmtAltIf)
p.printFreeFloating(nn, token.Start)
io.WriteString(p.w, "if")
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, ")")
}
stmtList, _ := nn.Stmt.(*ast.StmtStmtList)
brackets, ok := nn.Stmt.(*ast.ParserBrackets)
if ok {
p.printFreeFloating(brackets, token.Start)
stmtList = brackets.Child.(*ast.StmtStmtList)
} else {
io.WriteString(p.w, ":")
}
p.printFreeFloating(stmtList, token.Stmts)
p.printNodes(stmtList.Stmts)
p.printFreeFloating(stmtList, token.End)
if ok {
p.printFreeFloating(brackets, token.End)
}
for _, elseif := range nn.ElseIf {
p.Print(elseif)
}
if nn.Else != nil {
p.Print(nn.Else)
}
if !ok {
io.WriteString(p.w, "endif")
}
p.printFreeFloating(nn, token.SemiColon)
if nn.GetNode().Tokens.IsEmpty() {
io.WriteString(p.w, ";")
}
p.printFreeFloating(nn, token.End)
}
func (p *Printer) printStmtAltSwitch(n ast.Vertex) {
nn := n.(*ast.StmtAltSwitch)
p.printFreeFloating(nn, token.Start)
@@ -2579,42 +2458,54 @@ func (p *Printer) printStmtEcho(n ast.Vertex) {
p.printFreeFloating(nn, token.End)
}
func (p *Printer) printStmtElseif(n ast.Vertex) {
nn := n.(*ast.StmtElseIf)
p.printFreeFloating(nn, token.Start)
io.WriteString(p.w, "elseif")
if _, ok := nn.Cond.(*ast.ParserBrackets); !ok {
io.WriteString(p.w, "(")
func (p *Printer) printStmtElseif(n *ast.StmtElseIf) {
if n.Alt {
p.printStmtAltElseIf(n)
return
}
p.Print(nn.Cond)
p.printToken(n.ElseIfTkn, "elseif")
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(nn.Stmt)
p.printFreeFloating(nn, token.End)
p.Print(n.Stmt)
}
func (p *Printer) printStmtElse(n ast.Vertex) {
nn := n.(*ast.StmtElse)
p.printFreeFloating(nn, token.Start)
func (p *Printer) printStmtAltElseIf(n *ast.StmtElseIf) {
p.printToken(n.ElseIfTkn, "elseif")
p.printToken(n.OpenParenthesisTkn, "(")
p.Print(n.Cond)
p.printToken(n.CloseParenthesisTkn, ")")
p.printToken(n.ColonTkn, ":")
io.WriteString(p.w, "else")
if stmtList, ok := n.Stmt.(*ast.StmtStmtList); ok {
p.printNodes(stmtList.Stmts)
} else {
p.Print(n.Stmt)
}
}
if _, ok := nn.Stmt.(*ast.StmtStmtList); !ok {
if nn.Stmt.GetNode().Tokens.IsEmpty() {
io.WriteString(p.w, " ")
}
func (p *Printer) printStmtElse(n *ast.StmtElse) {
if n.Alt {
p.printStmtAltElse(n)
return
}
p.Print(nn.Stmt)
p.printToken(n.ElseTkn, "else")
p.bufStart = " "
p.Print(n.Stmt)
}
p.printFreeFloating(nn, token.End)
func (p *Printer) printStmtAltElse(n *ast.StmtElse) {
p.printToken(n.ElseTkn, "else")
p.printToken(n.ColonTkn, ":")
if stmtList, ok := n.Stmt.(*ast.StmtStmtList); ok {
p.printNodes(stmtList.Stmts)
} else {
p.Print(n.Stmt)
}
}
func (p *Printer) printStmtExpression(n ast.Vertex) {
@@ -2789,33 +2680,40 @@ func (p *Printer) printStmtHaltCompiler(n *ast.StmtHaltCompiler) {
p.printToken(n.SemiColonTkn, ";")
}
func (p *Printer) printStmtIf(n ast.Vertex) {
nn := n.(*ast.StmtIf)
p.printFreeFloating(nn, token.Start)
io.WriteString(p.w, "if")
if _, ok := nn.Cond.(*ast.ParserBrackets); !ok {
io.WriteString(p.w, "(")
func (p *Printer) printStmtIf(n *ast.StmtIf) {
if n.Alt {
p.printStmtAltIf(n)
return
}
p.Print(nn.Cond)
p.printToken(n.IfTkn, "if")
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)
p.printNodes(n.ElseIf)
p.Print(n.Else)
}
func (p *Printer) printStmtAltIf(n *ast.StmtIf) {
p.printToken(n.IfTkn, "if")
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.printNodes(n.ElseIf)
p.Print(n.Else)
if nn.ElseIf != nil {
p.printNodes(nn.ElseIf)
}
if nn.Else != nil {
p.Print(nn.Else)
}
p.printFreeFloating(nn, token.End)
p.printToken(n.EndIfTkn, "endif")
p.printToken(n.SemiColonTkn, ";")
}
func (p *Printer) printStmtInlineHTML(n ast.Vertex) {
@@ -2900,7 +2798,7 @@ func (p *Printer) printStmtNamespace(n *ast.StmtNamespace) {
}
func (p *Printer) printStmtNop(n ast.Vertex) {
p.printFreeFloating(n, token.Start)
p.printFreeFloatingOrDefault(n, token.Start, p.bufStart)
p.printFreeFloating(n, token.SemiColon)
if n.GetNode().Tokens.IsEmpty() {
io.WriteString(p.w, ";")

View File

@@ -743,7 +743,8 @@ func TestParseAndPrintPhp5Yield(t *testing.T) {
// test stmt
func TestParseAndPrintPhp5AltIf(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
if ( 1 ) :
// do nothing
elseif ( 2 ) :
@@ -1064,7 +1065,8 @@ func TestParseAndPrintPhp5HaltCompiler(t *testing.T) {
}
func TestParseAndPrintPhp5IfElseIfElse(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
if ( 1 ) ;
elseif ( 2 ) {
;

View File

@@ -856,7 +856,8 @@ func TestParseAndPrintYield(t *testing.T) {
// test stmt
func TestParseAndPrintAltIf(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
if ( 1 ) :
// do nothing
elseif ( 2 ) :
@@ -1197,7 +1198,8 @@ func TestParseAndPrintHaltCompiler(t *testing.T) {
}
func TestParseAndPrintIfElseIfElse(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
if ( 1 ) ;
elseif ( 2 ) {
;

View File

@@ -2565,7 +2565,8 @@ func TestPrinterPrintAltElseIf(t *testing.T) {
o := bytes.NewBufferString("")
p := printer.NewPrinter(o)
p.Print(&ast.StmtAltElseIf{
p.Print(&ast.StmtElseIf{
Alt: true,
Cond: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$a")},
},
@@ -2590,7 +2591,8 @@ func TestPrinterPrintAltElseIfEmpty(t *testing.T) {
o := bytes.NewBufferString("")
p := printer.NewPrinter(o)
p.Print(&ast.StmtAltElseIf{
p.Print(&ast.StmtElseIf{
Alt: true,
Cond: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$a")},
},
@@ -2609,7 +2611,8 @@ func TestPrinterPrintAltElse(t *testing.T) {
o := bytes.NewBufferString("")
p := printer.NewPrinter(o)
p.Print(&ast.StmtAltElse{
p.Print(&ast.StmtElse{
Alt: true,
Stmt: &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{
@@ -2631,7 +2634,8 @@ func TestPrinterPrintAltElseEmpty(t *testing.T) {
o := bytes.NewBufferString("")
p := printer.NewPrinter(o)
p.Print(&ast.StmtAltElse{
p.Print(&ast.StmtElse{
Alt: true,
Stmt: &ast.StmtStmtList{},
})
@@ -2715,7 +2719,8 @@ func TestPrinterPrintAltIf(t *testing.T) {
o := bytes.NewBufferString("")
p := printer.NewPrinter(o)
p.Print(&ast.StmtAltIf{
p.Print(&ast.StmtIf{
Alt: true,
Cond: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$a")},
},
@@ -2727,7 +2732,8 @@ func TestPrinterPrintAltIf(t *testing.T) {
},
},
ElseIf: []ast.Vertex{
&ast.StmtAltElseIf{
&ast.StmtElseIf{
Alt: true,
Cond: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$b")},
},
@@ -2739,14 +2745,16 @@ func TestPrinterPrintAltIf(t *testing.T) {
},
},
},
&ast.StmtAltElseIf{
&ast.StmtElseIf{
Alt: true,
Cond: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$c")},
},
Stmt: &ast.StmtStmtList{},
},
},
Else: &ast.StmtAltElse{
Else: &ast.StmtElse{
Alt: true,
Stmt: &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{
@@ -3453,7 +3461,7 @@ func TestPrinterPrintStmtElseStmts(t *testing.T) {
},
})
expected := `else{;}`
expected := `else {;}`
actual := o.String()
if expected != actual {