pretty printer: print PropertyList, Property, Return

This commit is contained in:
z7zmey 2018-04-02 00:43:27 +03:00
parent bd215f6069
commit e87d6d372b
2 changed files with 97 additions and 6 deletions

View File

@ -340,11 +340,17 @@ func getPrintFuncByNode(n node.Node) func(o io.Writer, n node.Node) {
return printStmtLabel return printStmtLabel
case *stmt.Namespace: case *stmt.Namespace:
return printStmtNamespace return printStmtNamespace
case *stmt.Nop:
return printStmtNop
case *stmt.PropertyList:
return printStmtPropertyList
case *stmt.Property:
return printStmtProperty
case *stmt.Return:
return printStmtReturn
case *stmt.StmtList: case *stmt.StmtList:
return printStmtStmtList return printStmtStmtList
case *stmt.Nop:
return printStmtNop
case *stmt.Use: case *stmt.Use:
return printStmtUse return printStmtUse
} }
@ -1778,16 +1784,44 @@ func printStmtNamespace(o io.Writer, n node.Node) {
} }
} }
func printStmtNop(o io.Writer, n node.Node) {
io.WriteString(o, ";\n")
}
func printStmtPropertyList(o io.Writer, n node.Node) {
nn := n.(*stmt.PropertyList)
joinPrint(" ", o, nn.Modifiers)
io.WriteString(o, " ")
joinPrint(", ", o, nn.Properties)
io.WriteString(o, ";\n")
}
func printStmtProperty(o io.Writer, n node.Node) {
nn := n.(*stmt.Property)
Print(o, nn.Variable)
if nn.Expr != nil {
io.WriteString(o, " = ")
Print(o, nn.Expr)
}
}
func printStmtReturn(o io.Writer, n node.Node) {
nn := n.(*stmt.Return)
io.WriteString(o, "return ")
Print(o, nn.Expr)
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)
printNodes(o, nn.Stmts) printNodes(o, nn.Stmts)
} }
func printStmtNop(o io.Writer, n node.Node) {
io.WriteString(o, ";\n")
}
func printStmtUse(o io.Writer, n node.Node) { func printStmtUse(o io.Writer, n node.Node) {
nn := n.(*stmt.Use) nn := n.(*stmt.Use)

View File

@ -2990,6 +2990,63 @@ func TestPrintNop(t *testing.T) {
} }
} }
func TestPrintPropertyList(t *testing.T) {
o := bytes.NewBufferString("")
printer.Print(o, &stmt.PropertyList{
Modifiers: []node.Node{
&node.Identifier{Value: "public"},
&node.Identifier{Value: "static"},
},
Properties: []node.Node{
&stmt.Property{
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
},
&stmt.Property{
Variable: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
},
},
})
expected := "public static $a, $b;\n"
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrintProperty(t *testing.T) {
o := bytes.NewBufferString("")
printer.Print(o, &stmt.Property{
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
Expr: &scalar.Lnumber{Value: "1"},
})
expected := "$a = 1"
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrintReturn(t *testing.T) {
o := bytes.NewBufferString("")
printer.Print(o, &stmt.Return{
Expr: &scalar.Lnumber{Value: "1"},
})
expected := "return 1;\n"
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrintUse(t *testing.T) { func TestPrintUse(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")