pretty printer: print Global, Goto, GroupUse, Use, HaltCompiller
This commit is contained in:
parent
483dc5a16e
commit
a34fcc1937
@ -322,11 +322,21 @@ func getPrintFuncByNode(n node.Node) func(o io.Writer, n node.Node) {
|
||||
return printStmtForeach
|
||||
case *stmt.Function:
|
||||
return printStmtFunction
|
||||
case *stmt.Global:
|
||||
return printStmtGlobal
|
||||
case *stmt.Goto:
|
||||
return printStmtGoto
|
||||
case *stmt.GroupUse:
|
||||
return printStmtGroupUse
|
||||
case *stmt.HaltCompiler:
|
||||
return printStmtHaltCompiler
|
||||
|
||||
case *stmt.StmtList:
|
||||
return printStmtStmtList
|
||||
case *stmt.Nop:
|
||||
return printStmtNop
|
||||
case *stmt.Use:
|
||||
return printStmtUse
|
||||
}
|
||||
|
||||
panic("printer is missing for the node")
|
||||
@ -1643,6 +1653,42 @@ func printStmtFunction(o io.Writer, n node.Node) {
|
||||
io.WriteString(o, "}\n")
|
||||
}
|
||||
|
||||
func printStmtGlobal(o io.Writer, n node.Node) {
|
||||
nn := n.(*stmt.Global)
|
||||
|
||||
io.WriteString(o, "global ")
|
||||
joinPrint(", ", o, nn.Vars)
|
||||
io.WriteString(o, ";\n")
|
||||
}
|
||||
|
||||
func printStmtGoto(o io.Writer, n node.Node) {
|
||||
nn := n.(*stmt.Goto)
|
||||
|
||||
io.WriteString(o, "goto ")
|
||||
Print(o, nn.Label)
|
||||
io.WriteString(o, ";\n")
|
||||
}
|
||||
|
||||
func printStmtGroupUse(o io.Writer, n node.Node) {
|
||||
nn := n.(*stmt.GroupUse)
|
||||
|
||||
io.WriteString(o, "use ")
|
||||
|
||||
if nn.UseType != nil {
|
||||
Print(o, nn.UseType)
|
||||
io.WriteString(o, " ")
|
||||
}
|
||||
|
||||
Print(o, nn.Prefix)
|
||||
io.WriteString(o, "\\{")
|
||||
joinPrint(", ", o, nn.UseList)
|
||||
io.WriteString(o, "};\n")
|
||||
}
|
||||
|
||||
func printStmtHaltCompiler(o io.Writer, n node.Node) {
|
||||
io.WriteString(o, "__halt_compiler();\n")
|
||||
}
|
||||
|
||||
func printStmtStmtList(o io.Writer, n node.Node) {
|
||||
nn := n.(*stmt.StmtList)
|
||||
|
||||
@ -1652,3 +1698,19 @@ func printStmtStmtList(o io.Writer, n node.Node) {
|
||||
func printStmtNop(o io.Writer, n node.Node) {
|
||||
io.WriteString(o, ";\n")
|
||||
}
|
||||
|
||||
func printStmtUse(o io.Writer, n node.Node) {
|
||||
nn := n.(*stmt.Use)
|
||||
|
||||
if nn.UseType != nil {
|
||||
Print(o, nn.UseType)
|
||||
io.WriteString(o, " ")
|
||||
}
|
||||
|
||||
Print(o, nn.Use)
|
||||
|
||||
if nn.Alias != nil {
|
||||
io.WriteString(o, " as ")
|
||||
Print(o, nn.Alias)
|
||||
}
|
||||
}
|
||||
|
@ -2666,6 +2666,7 @@ func TestPrintStmtForeachNop(t *testing.T) {
|
||||
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrintStmtFunction(t *testing.T) {
|
||||
o := bytes.NewBufferString("")
|
||||
|
||||
@ -2693,6 +2694,77 @@ func TestPrintStmtFunction(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrintStmtGlobal(t *testing.T) {
|
||||
o := bytes.NewBufferString("")
|
||||
|
||||
printer.Print(o, &stmt.Global{
|
||||
Vars: []node.Node{
|
||||
&expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
||||
&expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
||||
},
|
||||
})
|
||||
|
||||
expected := "global $a, $b;\n"
|
||||
actual := o.String()
|
||||
|
||||
if expected != actual {
|
||||
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrintStmtGoto(t *testing.T) {
|
||||
o := bytes.NewBufferString("")
|
||||
|
||||
printer.Print(o, &stmt.Goto{
|
||||
Label: &node.Identifier{Value: "FOO"},
|
||||
})
|
||||
|
||||
expected := "goto FOO;\n"
|
||||
actual := o.String()
|
||||
|
||||
if expected != actual {
|
||||
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrintStmtGroupUse(t *testing.T) {
|
||||
o := bytes.NewBufferString("")
|
||||
|
||||
printer.Print(o, &stmt.GroupUse{
|
||||
UseType: &node.Identifier{Value: "function"},
|
||||
Prefix: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}},
|
||||
UseList: []node.Node{
|
||||
&stmt.Use{
|
||||
Use: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Bar"}}},
|
||||
Alias: &node.Identifier{Value: "Baz"},
|
||||
},
|
||||
&stmt.Use{
|
||||
Use: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Quuz"}}},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
expected := "use function Foo\\{Bar as Baz, Quuz};\n"
|
||||
actual := o.String()
|
||||
|
||||
if expected != actual {
|
||||
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrintHaltCompiler(t *testing.T) {
|
||||
o := bytes.NewBufferString("")
|
||||
|
||||
printer.Print(o, &stmt.HaltCompiler{})
|
||||
|
||||
expected := "__halt_compiler();\n"
|
||||
actual := o.String()
|
||||
|
||||
if expected != actual {
|
||||
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrintStmtList(t *testing.T) {
|
||||
o := bytes.NewBufferString("")
|
||||
|
||||
@ -2723,3 +2795,20 @@ func TestPrintNop(t *testing.T) {
|
||||
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrintUse(t *testing.T) {
|
||||
o := bytes.NewBufferString("")
|
||||
|
||||
printer.Print(o, &stmt.Use{
|
||||
UseType: &node.Identifier{Value: "function"},
|
||||
Use: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}},
|
||||
Alias: &node.Identifier{Value: "Bar"},
|
||||
})
|
||||
|
||||
expected := "function Foo as Bar"
|
||||
actual := o.String()
|
||||
|
||||
if expected != actual {
|
||||
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user