pretty printer: print Trait, Try, Unset, UseList, While

This commit is contained in:
z7zmey 2018-04-02 12:41:47 +03:00
parent bd8b8f5b1a
commit 46b7396c02
2 changed files with 245 additions and 1 deletions

View File

@ -366,9 +366,18 @@ func getPrintFuncByNode(n node.Node) func(o io.Writer, n node.Node) {
return printStmtTraitUsePrecedence
case *stmt.TraitUse:
return printStmtTraitUse
case *stmt.Trait:
return printStmtTrait
case *stmt.Try:
return printStmtTry
case *stmt.Unset:
return printStmtUnset
case *stmt.UseList:
return printStmtUseList
case *stmt.Use:
return printStmtUse
case *stmt.While:
return printStmtWhile
}
panic("printer is missing for the node")
@ -1928,6 +1937,50 @@ func printStmtTraitUse(o io.Writer, n node.Node) {
}
}
func printStmtTrait(o io.Writer, n node.Node) {
nn := n.(*stmt.Trait)
io.WriteString(o, "trait ")
Print(o, nn.TraitName)
io.WriteString(o, "\n{\n")
printNodes(o, nn.Stmts)
io.WriteString(o, "}\n")
}
func printStmtTry(o io.Writer, n node.Node) {
nn := n.(*stmt.Try)
io.WriteString(o, "try {\n")
printNodes(o, nn.Stmts)
io.WriteString(o, "}\n")
printNodes(o, nn.Catches)
Print(o, nn.Finally)
}
func printStmtUnset(o io.Writer, n node.Node) {
nn := n.(*stmt.Unset)
io.WriteString(o, "unset(")
joinPrint(", ", o, nn.Vars)
io.WriteString(o, ");\n")
}
func printStmtUseList(o io.Writer, n node.Node) {
nn := n.(*stmt.UseList)
io.WriteString(o, "use ")
if nn.UseType != nil {
Print(o, nn.UseType)
io.WriteString(o, " ")
}
joinPrint(", ", o, nn.Uses)
io.WriteString(o, ";\n")
}
func printStmtUse(o io.Writer, n node.Node) {
nn := n.(*stmt.Use)
@ -1943,3 +1996,24 @@ func printStmtUse(o io.Writer, n node.Node) {
Print(o, nn.Alias)
}
}
func printStmtWhile(o io.Writer, n node.Node) {
nn := n.(*stmt.While)
io.WriteString(o, "while (")
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)
}
}

View File

@ -3241,6 +3241,124 @@ Foo::a as b;
}
}
func TestPrintTrait(t *testing.T) {
o := bytes.NewBufferString("")
printer.Print(o, &stmt.Trait{
TraitName: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}},
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 := `trait Foo
{
public function foo()
{
$a;
}
}
`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrintStmtTry(t *testing.T) {
o := bytes.NewBufferString("")
printer.Print(o, &stmt.Try{
Stmts: []node.Node{
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
},
Catches: []node.Node{
&stmt.Catch{
Types: []node.Node{
&name.Name{Parts: []node.Node{&name.NamePart{Value: "Exception"}}},
&name.FullyQualified{Parts: []node.Node{&name.NamePart{Value: "RuntimeException"}}},
},
Variable: &expr.Variable{VarName: &node.Identifier{Value: "e"}},
Stmts: []node.Node{
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}}},
},
},
},
Finally: &stmt.Finally{
Stmts: []node.Node{
&stmt.Nop{},
},
},
})
expected := `try {
$a;
}
catch (Exception | \RuntimeException $e) {
$b;
}
finally {
;
}
`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrintStmtUset(t *testing.T) {
o := bytes.NewBufferString("")
printer.Print(o, &stmt.Unset{
Vars: []node.Node{
&expr.Variable{VarName: &node.Identifier{Value: "a"}},
&expr.Variable{VarName: &node.Identifier{Value: "b"}},
},
})
expected := `unset($a, $b);
`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrintStmtUseList(t *testing.T) {
o := bytes.NewBufferString("")
printer.Print(o, &stmt.UseList{
UseType: &node.Identifier{Value: "function"},
Uses: []node.Node{
&stmt.Use{
Use: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}},
Alias: &node.Identifier{Value: "Bar"},
},
&stmt.Use{
Use: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Baz"}}},
},
},
})
expected := "use function Foo as Bar, Baz;\n"
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrintUse(t *testing.T) {
o := bytes.NewBufferString("")
@ -3257,3 +3375,55 @@ func TestPrintUse(t *testing.T) {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrintWhileStmtList(t *testing.T) {
o := bytes.NewBufferString("")
printer.Print(o, &stmt.While{
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
Stmt: &stmt.StmtList{
Stmts: []node.Node{
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
},
},
})
expected := "while ($a) {\n$a;\n}\n"
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrintWhileExpression(t *testing.T) {
o := bytes.NewBufferString("")
printer.Print(o, &stmt.While{
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
Stmt: &stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
})
expected := "while ($a)\n$a;\n"
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrintWhileNop(t *testing.T) {
o := bytes.NewBufferString("")
printer.Print(o, &stmt.While{
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
Stmt: &stmt.Nop{},
})
expected := "while ($a);\n"
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}