pretty printer: print Interface, Label, Namespace

This commit is contained in:
z7zmey 2018-04-02 00:28:01 +03:00
parent 5edfa095cb
commit bd215f6069
2 changed files with 137 additions and 0 deletions

View File

@ -334,6 +334,12 @@ func getPrintFuncByNode(n node.Node) func(o io.Writer, n node.Node) {
return printStmtIf return printStmtIf
case *stmt.InlineHtml: case *stmt.InlineHtml:
return printStmtInlineHTML return printStmtInlineHTML
case *stmt.Interface:
return printStmtInterface
case *stmt.Label:
return printStmtLabel
case *stmt.Namespace:
return printStmtNamespace
case *stmt.StmtList: case *stmt.StmtList:
return printStmtStmtList return printStmtStmtList
@ -1730,6 +1736,48 @@ func printStmtInlineHTML(o io.Writer, n node.Node) {
io.WriteString(o, "<?php\n") io.WriteString(o, "<?php\n")
} }
func printStmtInterface(o io.Writer, n node.Node) {
nn := n.(*stmt.Interface)
io.WriteString(o, "interface")
if nn.InterfaceName != nil {
io.WriteString(o, " ")
Print(o, nn.InterfaceName)
}
if nn.Extends != nil {
io.WriteString(o, " extends ")
joinPrint(", ", o, nn.Extends)
}
io.WriteString(o, "\n{\n") // TODO: handle indentation
printNodes(o, nn.Stmts)
io.WriteString(o, "}\n") // TODO: handle indentation
}
func printStmtLabel(o io.Writer, n node.Node) {
nn := n.(*stmt.Label)
Print(o, nn.LabelName)
io.WriteString(o, ":\n")
}
func printStmtNamespace(o io.Writer, n node.Node) {
nn := n.(*stmt.Namespace)
io.WriteString(o, "namespace ")
Print(o, nn.NamespaceName)
if nn.Stmts != nil {
io.WriteString(o, " {\n")
printNodes(o, nn.Stmts)
io.WriteString(o, "}\n")
} else {
io.WriteString(o, ";\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

@ -2870,6 +2870,95 @@ func TestPrintInlineHtml(t *testing.T) {
} }
} }
func TestPrintInterface(t *testing.T) {
o := bytes.NewBufferString("")
printer.Print(o, &stmt.Interface{
InterfaceName: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}},
Extends: []node.Node{
&name.Name{Parts: []node.Node{&name.NamePart{Value: "Bar"}}},
&name.Name{Parts: []node.Node{&name.NamePart{Value: "Baz"}}},
},
Stmts: []node.Node{
&stmt.ClassMethod{
Modifiers: []node.Node{&node.Identifier{Value: "public"}},
MethodName: &node.Identifier{Value: "foo"},
Params: []node.Node{},
Stmts: []node.Node{
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
},
},
},
})
expected := `interface Foo extends Bar, Baz
{
public function foo()
{
$a;
}
}
`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrintLabel(t *testing.T) {
o := bytes.NewBufferString("")
printer.Print(o, &stmt.Label{
LabelName: &node.Identifier{Value: "FOO"},
})
expected := `FOO:
`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrintNamespace(t *testing.T) {
o := bytes.NewBufferString("")
printer.Print(o, &stmt.Namespace{
NamespaceName: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}},
})
expected := `namespace Foo;
`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrintNamespaceWithStmts(t *testing.T) {
o := bytes.NewBufferString("")
printer.Print(o, &stmt.Namespace{
NamespaceName: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}},
Stmts: []node.Node{
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
},
})
expected := `namespace Foo {
$a;
}
`
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("")