From e87d6d372bdc39f9e66fc68b0503cb4c7b8dbb65 Mon Sep 17 00:00:00 2001 From: z7zmey Date: Mon, 2 Apr 2018 00:43:27 +0300 Subject: [PATCH] pretty printer: print PropertyList, Property, Return --- printer/printer.go | 46 ++++++++++++++++++++++++++++----- printer/printer_test.go | 57 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 6 deletions(-) diff --git a/printer/printer.go b/printer/printer.go index 08a697f..2625cbb 100644 --- a/printer/printer.go +++ b/printer/printer.go @@ -340,11 +340,17 @@ func getPrintFuncByNode(n node.Node) func(o io.Writer, n node.Node) { return printStmtLabel case *stmt.Namespace: 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: return printStmtStmtList - case *stmt.Nop: - return printStmtNop case *stmt.Use: 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) { nn := n.(*stmt.StmtList) printNodes(o, nn.Stmts) } -func printStmtNop(o io.Writer, n node.Node) { - io.WriteString(o, ";\n") -} - func printStmtUse(o io.Writer, n node.Node) { nn := n.(*stmt.Use) diff --git a/printer/printer_test.go b/printer/printer_test.go index 099852c..b4d46f5 100644 --- a/printer/printer_test.go +++ b/printer/printer_test.go @@ -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) { o := bytes.NewBufferString("")