pretty printer: print If, InlinHtml

This commit is contained in:
z7zmey 2018-04-01 17:07:07 +03:00
parent a34fcc1937
commit 8fd369b047
2 changed files with 146 additions and 0 deletions

View File

@ -330,6 +330,10 @@ func getPrintFuncByNode(n node.Node) func(o io.Writer, n node.Node) {
return printStmtGroupUse return printStmtGroupUse
case *stmt.HaltCompiler: case *stmt.HaltCompiler:
return printStmtHaltCompiler return printStmtHaltCompiler
case *stmt.If:
return printStmtIf
case *stmt.InlineHtml:
return printStmtInlineHTML
case *stmt.StmtList: case *stmt.StmtList:
return printStmtStmtList return printStmtStmtList
@ -1689,6 +1693,43 @@ func printStmtHaltCompiler(o io.Writer, n node.Node) {
io.WriteString(o, "__halt_compiler();\n") io.WriteString(o, "__halt_compiler();\n")
} }
func printStmtIf(o io.Writer, n node.Node) {
nn := n.(*stmt.If)
io.WriteString(o, "if (")
Print(o, nn.Cond)
io.WriteString(o, ")")
switch s := nn.Stmt.(type) {
case *stmt.Nop:
Print(o, s)
break
case *stmt.StmtList:
io.WriteString(o, " {\n")
printNodes(o, s.Stmts)
io.WriteString(o, "}\n")
default:
io.WriteString(o, "\n")
Print(o, s)
}
if nn.ElseIf != nil {
printNodes(o, nn.ElseIf)
}
if nn.Else != nil {
Print(o, nn.Else)
}
}
func printStmtInlineHTML(o io.Writer, n node.Node) {
nn := n.(*stmt.InlineHtml)
io.WriteString(o, "?>")
io.WriteString(o, nn.Value)
io.WriteString(o, "<?php\n")
}
func printStmtStmtList(o io.Writer, n node.Node) { func printStmtStmtList(o io.Writer, n node.Node) {
nn := n.(*stmt.StmtList) nn := n.(*stmt.StmtList)

View File

@ -2765,6 +2765,111 @@ func TestPrintHaltCompiler(t *testing.T) {
} }
} }
func TestPrintIfExpression(t *testing.T) {
o := bytes.NewBufferString("")
printer.Print(o, &stmt.If{
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
Stmt: &stmt.Expression{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
},
ElseIf: []node.Node{
&stmt.ElseIf{
Cond: &expr.Variable{VarName: &node.Identifier{Value: "c"}},
Stmt: &stmt.StmtList{
Stmts: []node.Node{
&stmt.Expression{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "d"}},
},
},
},
},
&stmt.ElseIf{
Cond: &expr.Variable{VarName: &node.Identifier{Value: "e"}},
Stmt: &stmt.Nop{},
},
},
Else: &stmt.Else{
Stmt: &stmt.Expression{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "f"}},
},
},
})
expected := `if ($a)
$b;
elseif ($c) {
$d;
}
elseif ($e);
else
$f;
`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrintIfStmtList(t *testing.T) {
o := bytes.NewBufferString("")
printer.Print(o, &stmt.If{
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
Stmt: &stmt.StmtList{
Stmts: []node.Node{
&stmt.Expression{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
},
},
},
})
expected := `if ($a) {
$b;
}
`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrintIfNop(t *testing.T) {
o := bytes.NewBufferString("")
printer.Print(o, &stmt.If{
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
Stmt: &stmt.Nop{},
})
expected := `if ($a);
`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrintInlineHtml(t *testing.T) {
o := bytes.NewBufferString("")
printer.Print(o, &stmt.InlineHtml{
Value: "test",
})
expected := `?>test<?php
`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrintStmtList(t *testing.T) { func TestPrintStmtList(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")