[refactoring] update ast structure of "If", "ElseIf", "Else" nodes
This commit is contained in:
@@ -27,11 +27,8 @@ type NodeVisitor interface {
|
||||
ArgumentList(n *ArgumentList)
|
||||
Argument(n *Argument)
|
||||
|
||||
StmtAltElse(n *StmtAltElse)
|
||||
StmtAltElseIf(n *StmtAltElseIf)
|
||||
StmtAltFor(n *StmtAltFor)
|
||||
StmtAltForeach(n *StmtAltForeach)
|
||||
StmtAltIf(n *StmtAltIf)
|
||||
StmtAltSwitch(n *StmtAltSwitch)
|
||||
StmtAltWhile(n *StmtAltWhile)
|
||||
StmtBreak(n *StmtBreak)
|
||||
|
||||
@@ -175,27 +175,6 @@ func (n *ScalarString) Accept(v NodeVisitor) {
|
||||
v.ScalarString(n)
|
||||
}
|
||||
|
||||
// StmtAltElse node
|
||||
type StmtAltElse struct {
|
||||
Node
|
||||
Stmt Vertex
|
||||
}
|
||||
|
||||
func (n *StmtAltElse) Accept(v NodeVisitor) {
|
||||
v.StmtAltElse(n)
|
||||
}
|
||||
|
||||
// StmtAltElseIf node
|
||||
type StmtAltElseIf struct {
|
||||
Node
|
||||
Cond Vertex
|
||||
Stmt Vertex
|
||||
}
|
||||
|
||||
func (n *StmtAltElseIf) Accept(v NodeVisitor) {
|
||||
v.StmtAltElseIf(n)
|
||||
}
|
||||
|
||||
// StmtAltFor node
|
||||
type StmtAltFor struct {
|
||||
Node
|
||||
@@ -222,19 +201,6 @@ func (n *StmtAltForeach) Accept(v NodeVisitor) {
|
||||
v.StmtAltForeach(n)
|
||||
}
|
||||
|
||||
// StmtAltIf node
|
||||
type StmtAltIf struct {
|
||||
Node
|
||||
Cond Vertex
|
||||
Stmt Vertex
|
||||
ElseIf []Vertex
|
||||
Else Vertex
|
||||
}
|
||||
|
||||
func (n *StmtAltIf) Accept(v NodeVisitor) {
|
||||
v.StmtAltIf(n)
|
||||
}
|
||||
|
||||
// StmtAltSwitch node
|
||||
type StmtAltSwitch struct {
|
||||
Node
|
||||
@@ -444,7 +410,10 @@ func (n *StmtEcho) Accept(v NodeVisitor) {
|
||||
// StmtElse node
|
||||
type StmtElse struct {
|
||||
Node
|
||||
Stmt Vertex
|
||||
Alt bool
|
||||
ElseTkn *token.Token
|
||||
ColonTkn *token.Token
|
||||
Stmt Vertex
|
||||
}
|
||||
|
||||
func (n *StmtElse) Accept(v NodeVisitor) {
|
||||
@@ -454,8 +423,13 @@ func (n *StmtElse) Accept(v NodeVisitor) {
|
||||
// StmtElseIf node
|
||||
type StmtElseIf struct {
|
||||
Node
|
||||
Cond Vertex
|
||||
Stmt Vertex
|
||||
Alt bool
|
||||
ElseIfTkn *token.Token
|
||||
OpenParenthesisTkn *token.Token
|
||||
Cond Vertex
|
||||
CloseParenthesisTkn *token.Token
|
||||
ColonTkn *token.Token
|
||||
Stmt Vertex
|
||||
}
|
||||
|
||||
func (n *StmtElseIf) Accept(v NodeVisitor) {
|
||||
@@ -558,10 +532,17 @@ func (n *StmtHaltCompiler) Accept(v NodeVisitor) {
|
||||
// StmtIf node
|
||||
type StmtIf struct {
|
||||
Node
|
||||
Cond Vertex
|
||||
Stmt Vertex
|
||||
ElseIf []Vertex
|
||||
Else Vertex
|
||||
Alt bool
|
||||
IfTkn *token.Token
|
||||
OpenParenthesisTkn *token.Token
|
||||
Cond Vertex
|
||||
CloseParenthesisTkn *token.Token
|
||||
ColonTkn *token.Token
|
||||
Stmt Vertex
|
||||
ElseIf []Vertex
|
||||
Else Vertex
|
||||
EndIfTkn *token.Token
|
||||
SemiColonTkn *token.Token
|
||||
}
|
||||
|
||||
func (n *StmtIf) Accept(v NodeVisitor) {
|
||||
@@ -1918,7 +1899,9 @@ func (n *ParserNsSeparator) Accept(v NodeVisitor) {
|
||||
|
||||
type ParserBrackets struct {
|
||||
Node
|
||||
Child Vertex
|
||||
OpenBracketTkn *token.Token
|
||||
Child Vertex
|
||||
CloseBracketTkn *token.Token
|
||||
}
|
||||
|
||||
func (n *ParserBrackets) Accept(v NodeVisitor) {
|
||||
|
||||
@@ -119,35 +119,6 @@ func (t *DFS) Traverse(n ast.Vertex) {
|
||||
t.Traverse(nn.Expr)
|
||||
t.visitor.Leave("Expr", true)
|
||||
}
|
||||
case *ast.StmtAltElse:
|
||||
if nn == nil {
|
||||
return
|
||||
}
|
||||
if !t.visitor.EnterNode(nn) {
|
||||
return
|
||||
}
|
||||
if nn.Stmt != nil {
|
||||
t.visitor.Enter("Stmt", true)
|
||||
t.Traverse(nn.Stmt)
|
||||
t.visitor.Leave("Stmt", true)
|
||||
}
|
||||
case *ast.StmtAltElseIf:
|
||||
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.StmtAltFor:
|
||||
if nn == nil {
|
||||
return
|
||||
@@ -208,35 +179,6 @@ func (t *DFS) Traverse(n ast.Vertex) {
|
||||
t.Traverse(nn.Stmt)
|
||||
t.visitor.Leave("Stmt", true)
|
||||
}
|
||||
case *ast.StmtAltIf:
|
||||
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)
|
||||
}
|
||||
if nn.ElseIf != nil {
|
||||
t.visitor.Enter("ElseIf", false)
|
||||
for _, c := range nn.ElseIf {
|
||||
t.Traverse(c)
|
||||
}
|
||||
t.visitor.Leave("ElseIf", false)
|
||||
}
|
||||
if nn.Else != nil {
|
||||
t.visitor.Enter("Else", true)
|
||||
t.Traverse(nn.Else)
|
||||
t.visitor.Leave("Else", true)
|
||||
}
|
||||
case *ast.StmtAltSwitch:
|
||||
if nn == nil {
|
||||
return
|
||||
|
||||
@@ -252,18 +252,6 @@ func (v *Dump) Argument(n *ast.Argument) {
|
||||
}
|
||||
}
|
||||
|
||||
func (v *Dump) StmtAltElse(n *ast.StmtAltElse) {
|
||||
v.printIndentIfNotSingle(v.indent - 1)
|
||||
v.print("&ast.StmtAltElse{\n")
|
||||
v.printNode(n.GetNode())
|
||||
}
|
||||
|
||||
func (v *Dump) StmtAltElseIf(n *ast.StmtAltElseIf) {
|
||||
v.printIndentIfNotSingle(v.indent - 1)
|
||||
v.print("&ast.StmtAltElseIf{\n")
|
||||
v.printNode(n.GetNode())
|
||||
}
|
||||
|
||||
func (v *Dump) StmtAltFor(n *ast.StmtAltFor) {
|
||||
v.printIndentIfNotSingle(v.indent - 1)
|
||||
v.print("&ast.StmtAltFor{\n")
|
||||
@@ -276,12 +264,6 @@ func (v *Dump) StmtAltForeach(n *ast.StmtAltForeach) {
|
||||
v.printNode(n.GetNode())
|
||||
}
|
||||
|
||||
func (v *Dump) StmtAltIf(n *ast.StmtAltIf) {
|
||||
v.printIndentIfNotSingle(v.indent - 1)
|
||||
v.print("&ast.StmtAltIf{\n")
|
||||
v.printNode(n.GetNode())
|
||||
}
|
||||
|
||||
func (v *Dump) StmtAltSwitch(n *ast.StmtAltSwitch) {
|
||||
v.printIndentIfNotSingle(v.indent - 1)
|
||||
v.print("&ast.StmtAltSwitch{\n")
|
||||
@@ -404,12 +386,22 @@ func (v *Dump) StmtElse(n *ast.StmtElse) {
|
||||
v.printIndentIfNotSingle(v.indent - 1)
|
||||
v.print("&ast.StmtElse{\n")
|
||||
v.printNode(n.GetNode())
|
||||
|
||||
if n.Alt {
|
||||
v.printIndent(v.indent)
|
||||
v.print("Alt: true,\n")
|
||||
}
|
||||
}
|
||||
|
||||
func (v *Dump) StmtElseIf(n *ast.StmtElseIf) {
|
||||
v.printIndentIfNotSingle(v.indent - 1)
|
||||
v.print("&ast.StmtElseIf{\n")
|
||||
v.printNode(n.GetNode())
|
||||
|
||||
if n.Alt {
|
||||
v.printIndent(v.indent)
|
||||
v.print("Alt: true,\n")
|
||||
}
|
||||
}
|
||||
|
||||
func (v *Dump) StmtExpression(n *ast.StmtExpression) {
|
||||
@@ -469,6 +461,11 @@ func (v *Dump) StmtIf(n *ast.StmtIf) {
|
||||
v.printIndentIfNotSingle(v.indent - 1)
|
||||
v.print("&ast.StmtIf{\n")
|
||||
v.printNode(n.GetNode())
|
||||
|
||||
if n.Alt {
|
||||
v.printIndent(v.indent)
|
||||
v.print("Alt: true,\n")
|
||||
}
|
||||
}
|
||||
|
||||
func (v *Dump) StmtInlineHtml(n *ast.StmtInlineHtml) {
|
||||
|
||||
@@ -13,60 +13,6 @@ func (v *FilterParserNodes) EnterNode(n ast.Vertex) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (v *FilterParserNodes) StmtAltIf(n *ast.StmtAltIf) {
|
||||
for {
|
||||
if nn, ok := n.Cond.(*ast.ParserBrackets); ok {
|
||||
n.Cond = nn.Child
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if nn, ok := n.Stmt.(*ast.ParserBrackets); ok {
|
||||
n.Stmt = nn.Child
|
||||
}
|
||||
}
|
||||
|
||||
func (v *FilterParserNodes) StmtAltElseIf(n *ast.StmtAltElseIf) {
|
||||
for {
|
||||
if nn, ok := n.Cond.(*ast.ParserBrackets); ok {
|
||||
n.Cond = nn.Child
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if nn, ok := n.Stmt.(*ast.ParserBrackets); ok {
|
||||
n.Stmt = nn.Child
|
||||
}
|
||||
}
|
||||
|
||||
func (v *FilterParserNodes) StmtAltElse(n *ast.StmtAltElse) {
|
||||
if nn, ok := n.Stmt.(*ast.ParserBrackets); ok {
|
||||
n.Stmt = nn.Child
|
||||
}
|
||||
}
|
||||
|
||||
func (v *FilterParserNodes) StmtIf(n *ast.StmtIf) {
|
||||
for {
|
||||
if nn, ok := n.Cond.(*ast.ParserBrackets); ok {
|
||||
n.Cond = nn.Child
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (v *FilterParserNodes) StmtElseIf(n *ast.StmtElseIf) {
|
||||
for {
|
||||
if nn, ok := n.Cond.(*ast.ParserBrackets); ok {
|
||||
n.Cond = nn.Child
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (v *FilterParserNodes) StmtWhile(n *ast.StmtWhile) {
|
||||
for {
|
||||
if nn, ok := n.Cond.(*ast.ParserBrackets); ok {
|
||||
|
||||
@@ -87,3 +87,29 @@ func (v *FilterTokens) StmtStmtList(n *ast.StmtStmtList) {
|
||||
n.OpenCurlyBracket = nil
|
||||
n.CloseCurlyBracket = nil
|
||||
}
|
||||
|
||||
func (v *FilterTokens) StmtIf(n *ast.StmtIf) {
|
||||
n.IfTkn = nil
|
||||
n.OpenParenthesisTkn = nil
|
||||
n.CloseParenthesisTkn = nil
|
||||
n.ColonTkn = nil
|
||||
n.EndIfTkn = nil
|
||||
n.SemiColonTkn = nil
|
||||
}
|
||||
|
||||
func (v *FilterTokens) StmtElseIf(n *ast.StmtElseIf) {
|
||||
n.ElseIfTkn = nil
|
||||
n.OpenParenthesisTkn = nil
|
||||
n.CloseParenthesisTkn = nil
|
||||
n.ColonTkn = nil
|
||||
}
|
||||
|
||||
func (v *FilterTokens) StmtElse(n *ast.StmtElse) {
|
||||
n.ElseTkn = nil
|
||||
n.ColonTkn = nil
|
||||
}
|
||||
|
||||
func (v *FilterTokens) ParserBrackets(n *ast.ParserBrackets) {
|
||||
n.OpenBracketTkn = nil
|
||||
n.CloseBracketTkn = nil
|
||||
}
|
||||
|
||||
@@ -54,14 +54,6 @@ func (v *Null) Argument(_ *ast.Argument) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
func (v *Null) StmtAltElse(_ *ast.StmtAltElse) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
func (v *Null) StmtAltElseIf(_ *ast.StmtAltElseIf) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
func (v *Null) StmtAltFor(_ *ast.StmtAltFor) {
|
||||
// do nothing
|
||||
}
|
||||
@@ -70,10 +62,6 @@ func (v *Null) StmtAltForeach(_ *ast.StmtAltForeach) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
func (v *Null) StmtAltIf(_ *ast.StmtAltIf) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
func (v *Null) StmtAltSwitch(_ *ast.StmtAltSwitch) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
@@ -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, ")")
|
||||
|
||||
@@ -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")}}},
|
||||
|
||||
@@ -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, ";")
|
||||
|
||||
@@ -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 ) {
|
||||
;
|
||||
|
||||
@@ -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 ) {
|
||||
;
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user