pretti printer: print indents

This commit is contained in:
z7zmey 2018-04-03 19:20:55 +03:00
parent a394ea9fcc
commit a514fec90f
2 changed files with 716 additions and 425 deletions

View File

@ -44,13 +44,22 @@ func (p *Printer) joinPrint(glue string, nn []node.Node) {
} }
func (p *Printer) printNodes(nn []node.Node) { func (p *Printer) printNodes(nn []node.Node) {
p.indentDepth++
l := len(nn) - 1 l := len(nn) - 1
for k, n := range nn { for k, n := range nn {
p.printIndent()
p.Print(n) p.Print(n)
if k < l { if k < l {
io.WriteString(p.w, "\n") io.WriteString(p.w, "\n")
} }
} }
p.indentDepth--
}
func (p *Printer) printIndent() {
for i := 0; i < p.indentDepth; i++ {
io.WriteString(p.w, p.indentStr)
}
} }
func (p *Printer) printNode(n node.Node) { func (p *Printer) printNode(n node.Node) {
@ -997,7 +1006,9 @@ func (p *Printer) printExprClosure(n node.Node) {
io.WriteString(p.w, " {\n") io.WriteString(p.w, " {\n")
p.printNodes(nn.Stmts) p.printNodes(nn.Stmts)
io.WriteString(p.w, "\n}") io.WriteString(p.w, "\n")
p.printIndent()
io.WriteString(p.w, "}")
} }
func (p *Printer) printExprConstFetch(n node.Node) { func (p *Printer) printExprConstFetch(n node.Node) {
@ -1314,6 +1325,7 @@ func (p *Printer) printStmtAltFor(n node.Node) {
s := nn.Stmt.(*stmt.StmtList) s := nn.Stmt.(*stmt.StmtList)
p.printNodes(s.Stmts) p.printNodes(s.Stmts)
io.WriteString(p.w, "\n") io.WriteString(p.w, "\n")
p.printIndent()
io.WriteString(p.w, "endfor;") io.WriteString(p.w, "endfor;")
} }
@ -1340,8 +1352,9 @@ func (p *Printer) printStmtAltForeach(n node.Node) {
s := nn.Stmt.(*stmt.StmtList) s := nn.Stmt.(*stmt.StmtList)
p.printNodes(s.Stmts) p.printNodes(s.Stmts)
io.WriteString(p.w, "\n")
io.WriteString(p.w, "\n")
p.printIndent()
io.WriteString(p.w, "endforeach;") io.WriteString(p.w, "endforeach;")
} }
@ -1357,15 +1370,18 @@ func (p *Printer) printStmtAltIf(n node.Node) {
for _, elseif := range nn.ElseIf { for _, elseif := range nn.ElseIf {
io.WriteString(p.w, "\n") io.WriteString(p.w, "\n")
p.printIndent()
p.Print(elseif) p.Print(elseif)
} }
if nn.Else != nil { if nn.Else != nil {
io.WriteString(p.w, "\n") io.WriteString(p.w, "\n")
p.printIndent()
p.Print(nn.Else) p.Print(nn.Else)
} }
io.WriteString(p.w, "\n") io.WriteString(p.w, "\n")
p.printIndent()
io.WriteString(p.w, "endif;") io.WriteString(p.w, "endif;")
} }
@ -1378,8 +1394,9 @@ func (p *Printer) printStmtAltSwitch(n node.Node) {
s := nn.Cases s := nn.Cases
p.printNodes(s) p.printNodes(s)
io.WriteString(p.w, "\n")
io.WriteString(p.w, "\n")
p.printIndent()
io.WriteString(p.w, "endswitch;") io.WriteString(p.w, "endswitch;")
} }
@ -1392,8 +1409,9 @@ func (p *Printer) printStmtAltWhile(n node.Node) {
s := nn.Stmt.(*stmt.StmtList) s := nn.Stmt.(*stmt.StmtList)
p.printNodes(s.Stmts) p.printNodes(s.Stmts)
io.WriteString(p.w, "\n")
io.WriteString(p.w, "\n")
p.printIndent()
io.WriteString(p.w, "endwhile;") io.WriteString(p.w, "endwhile;")
} }
@ -1431,7 +1449,9 @@ func (p *Printer) printStmtCatch(n node.Node) {
p.Print(nn.Variable) p.Print(nn.Variable)
io.WriteString(p.w, ") {\n") io.WriteString(p.w, ") {\n")
p.printNodes(nn.Stmts) p.printNodes(nn.Stmts)
io.WriteString(p.w, "\n}") io.WriteString(p.w, "\n")
p.printIndent()
io.WriteString(p.w, "}")
} }
func (p *Printer) printStmtClassMethod(n node.Node) { func (p *Printer) printStmtClassMethod(n node.Node) {
@ -1457,9 +1477,13 @@ func (p *Printer) printStmtClassMethod(n node.Node) {
p.Print(nn.ReturnType) p.Print(nn.ReturnType)
} }
io.WriteString(p.w, "\n{\n") io.WriteString(p.w, "\n")
p.printIndent()
io.WriteString(p.w, "{\n")
p.printNodes(nn.Stmts) p.printNodes(nn.Stmts)
io.WriteString(p.w, "\n}") io.WriteString(p.w, "\n")
p.printIndent()
io.WriteString(p.w, "}")
} }
func (p *Printer) printStmtClass(n node.Node) { func (p *Printer) printStmtClass(n node.Node) {
@ -1492,9 +1516,13 @@ func (p *Printer) printStmtClass(n node.Node) {
p.joinPrint(", ", nn.Implements) p.joinPrint(", ", nn.Implements)
} }
io.WriteString(p.w, "\n{\n") io.WriteString(p.w, "\n")
p.printIndent()
io.WriteString(p.w, "{\n")
p.printNodes(nn.Stmts) p.printNodes(nn.Stmts)
io.WriteString(p.w, "\n}") io.WriteString(p.w, "\n")
p.printIndent()
io.WriteString(p.w, "}")
} }
func (p *Printer) printStmtClassConstList(n node.Node) { func (p *Printer) printStmtClassConstList(n node.Node) {
@ -1547,7 +1575,10 @@ func (p *Printer) printStmtDeclare(n node.Node) {
p.Print(s) p.Print(s)
default: default:
io.WriteString(p.w, "\n") io.WriteString(p.w, "\n")
p.indentDepth++
p.printIndent()
p.Print(s) p.Print(s)
p.indentDepth--
} }
} }
@ -1572,8 +1603,12 @@ func (p *Printer) printStmtDo(n node.Node) {
io.WriteString(p.w, " ") io.WriteString(p.w, " ")
default: default:
io.WriteString(p.w, "\n") io.WriteString(p.w, "\n")
p.indentDepth++
p.printIndent()
p.Print(s) p.Print(s)
p.indentDepth--
io.WriteString(p.w, "\n") io.WriteString(p.w, "\n")
p.printIndent()
} }
io.WriteString(p.w, "while (") io.WriteString(p.w, "while (")
@ -1604,7 +1639,10 @@ func (p *Printer) printStmtElseif(n node.Node) {
p.Print(s) p.Print(s)
default: default:
io.WriteString(p.w, "\n") io.WriteString(p.w, "\n")
p.indentDepth++
p.printIndent()
p.Print(s) p.Print(s)
p.indentDepth--
} }
} }
@ -1622,7 +1660,10 @@ func (p *Printer) printStmtElse(n node.Node) {
p.Print(s) p.Print(s)
default: default:
io.WriteString(p.w, "\n") io.WriteString(p.w, "\n")
p.indentDepth++
p.printIndent()
p.Print(s) p.Print(s)
p.indentDepth--
} }
} }
@ -1639,7 +1680,9 @@ func (p *Printer) printStmtFinally(n node.Node) {
io.WriteString(p.w, "finally {\n") io.WriteString(p.w, "finally {\n")
p.printNodes(nn.Stmts) p.printNodes(nn.Stmts)
io.WriteString(p.w, "\n}") io.WriteString(p.w, "\n")
p.printIndent()
io.WriteString(p.w, "}")
} }
func (p *Printer) printStmtFor(n node.Node) { func (p *Printer) printStmtFor(n node.Node) {
@ -1662,7 +1705,10 @@ func (p *Printer) printStmtFor(n node.Node) {
p.Print(s) p.Print(s)
default: default:
io.WriteString(p.w, "\n") io.WriteString(p.w, "\n")
p.indentDepth++
p.printIndent()
p.Print(s) p.Print(s)
p.indentDepth--
} }
} }
@ -1693,7 +1739,10 @@ func (p *Printer) printStmtForeach(n node.Node) {
p.Print(s) p.Print(s)
default: default:
io.WriteString(p.w, "\n") io.WriteString(p.w, "\n")
p.indentDepth++
p.printIndent()
p.Print(s) p.Print(s)
p.indentDepth--
} }
} }
@ -1719,7 +1768,9 @@ func (p *Printer) printStmtFunction(n node.Node) {
io.WriteString(p.w, " {\n") io.WriteString(p.w, " {\n")
p.printNodes(nn.Stmts) p.printNodes(nn.Stmts)
io.WriteString(p.w, "\n}") io.WriteString(p.w, "\n")
p.printIndent()
io.WriteString(p.w, "}")
} }
func (p *Printer) printStmtGlobal(n node.Node) { func (p *Printer) printStmtGlobal(n node.Node) {
@ -1774,16 +1825,22 @@ func (p *Printer) printStmtIf(n node.Node) {
p.Print(s) p.Print(s)
default: default:
io.WriteString(p.w, "\n") io.WriteString(p.w, "\n")
p.indentDepth++
p.printIndent()
p.Print(s) p.Print(s)
p.indentDepth--
} }
if nn.ElseIf != nil { if nn.ElseIf != nil {
io.WriteString(p.w, "\n") io.WriteString(p.w, "\n")
p.indentDepth--
p.printNodes(nn.ElseIf) p.printNodes(nn.ElseIf)
p.indentDepth++
} }
if nn.Else != nil { if nn.Else != nil {
io.WriteString(p.w, "\n") io.WriteString(p.w, "\n")
p.printIndent()
p.Print(nn.Else) p.Print(nn.Else)
} }
} }
@ -1811,9 +1868,13 @@ func (p *Printer) printStmtInterface(n node.Node) {
p.joinPrint(", ", nn.Extends) p.joinPrint(", ", nn.Extends)
} }
io.WriteString(p.w, "\n{\n") io.WriteString(p.w, "\n")
p.printIndent()
io.WriteString(p.w, "{\n")
p.printNodes(nn.Stmts) p.printNodes(nn.Stmts)
io.WriteString(p.w, "\n}") io.WriteString(p.w, "\n")
p.printIndent()
io.WriteString(p.w, "}")
} }
func (p *Printer) printStmtLabel(n node.Node) { func (p *Printer) printStmtLabel(n node.Node) {
@ -1836,7 +1897,9 @@ func (p *Printer) printStmtNamespace(n node.Node) {
if nn.Stmts != nil { if nn.Stmts != nil {
io.WriteString(p.w, " {\n") io.WriteString(p.w, " {\n")
p.printNodes(nn.Stmts) p.printNodes(nn.Stmts)
io.WriteString(p.w, "\n}") io.WriteString(p.w, "\n")
p.printIndent()
io.WriteString(p.w, "}")
} else { } else {
io.WriteString(p.w, ";") io.WriteString(p.w, ";")
} }
@ -1897,7 +1960,9 @@ func (p *Printer) printStmtStmtList(n node.Node) {
io.WriteString(p.w, "{\n") io.WriteString(p.w, "{\n")
p.printNodes(nn.Stmts) p.printNodes(nn.Stmts)
io.WriteString(p.w, "\n}") io.WriteString(p.w, "\n")
p.printIndent()
io.WriteString(p.w, "}")
} }
func (p *Printer) printStmtSwitch(n node.Node) { func (p *Printer) printStmtSwitch(n node.Node) {
@ -1909,7 +1974,9 @@ func (p *Printer) printStmtSwitch(n node.Node) {
io.WriteString(p.w, " {\n") io.WriteString(p.w, " {\n")
p.printNodes(nn.Cases) p.printNodes(nn.Cases)
io.WriteString(p.w, "\n}") io.WriteString(p.w, "\n")
p.printIndent()
io.WriteString(p.w, "}")
} }
func (p *Printer) printStmtThrow(n node.Node) { func (p *Printer) printStmtThrow(n node.Node) {
@ -1966,7 +2033,9 @@ func (p *Printer) printStmtTraitUse(n node.Node) {
if nn.Adaptations != nil { if nn.Adaptations != nil {
io.WriteString(p.w, " {\n") io.WriteString(p.w, " {\n")
p.printNodes(nn.Adaptations) p.printNodes(nn.Adaptations)
io.WriteString(p.w, "\n}") io.WriteString(p.w, "\n")
p.printIndent()
io.WriteString(p.w, "}")
} else { } else {
io.WriteString(p.w, ";") io.WriteString(p.w, ";")
} }
@ -1978,9 +2047,13 @@ func (p *Printer) printStmtTrait(n node.Node) {
io.WriteString(p.w, "trait ") io.WriteString(p.w, "trait ")
p.Print(nn.TraitName) p.Print(nn.TraitName)
io.WriteString(p.w, "\n{\n") io.WriteString(p.w, "\n")
p.printIndent()
io.WriteString(p.w, "{\n")
p.printNodes(nn.Stmts) p.printNodes(nn.Stmts)
io.WriteString(p.w, "\n}") io.WriteString(p.w, "\n")
p.printIndent()
io.WriteString(p.w, "}")
} }
func (p *Printer) printStmtTry(n node.Node) { func (p *Printer) printStmtTry(n node.Node) {
@ -1988,15 +2061,20 @@ func (p *Printer) printStmtTry(n node.Node) {
io.WriteString(p.w, "try {\n") io.WriteString(p.w, "try {\n")
p.printNodes(nn.Stmts) p.printNodes(nn.Stmts)
io.WriteString(p.w, "\n}") io.WriteString(p.w, "\n")
p.printIndent()
io.WriteString(p.w, "}")
if nn.Catches != nil { if nn.Catches != nil {
io.WriteString(p.w, "\n") io.WriteString(p.w, "\n")
p.indentDepth--
p.printNodes(nn.Catches) p.printNodes(nn.Catches)
p.indentDepth++
} }
if nn.Finally != nil { if nn.Finally != nil {
io.WriteString(p.w, "\n") io.WriteString(p.w, "\n")
p.printIndent()
p.Print(nn.Finally) p.Print(nn.Finally)
} }
} }
@ -2055,6 +2133,9 @@ func (p *Printer) printStmtWhile(n node.Node) {
p.Print(s) p.Print(s)
default: default:
io.WriteString(p.w, "\n") io.WriteString(p.w, "\n")
p.indentDepth++
p.printIndent()
p.Print(s) p.Print(s)
p.indentDepth--
} }
} }

View File

@ -1252,7 +1252,9 @@ func TestPrintExprClosure(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")
p := printer.NewPrinter(o, " ") p := printer.NewPrinter(o, " ")
p.Print(&expr.Closure{ p.Print(&stmt.Namespace{
Stmts: []node.Node{
&expr.Closure{
Static: true, Static: true,
ReturnsRef: true, ReturnsRef: true,
Params: []node.Node{ Params: []node.Node{
@ -1276,10 +1278,14 @@ func TestPrintExprClosure(t *testing.T) {
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}}, &stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
}, },
},
},
}) })
expected := `static function &(&$var) use (&$a, $b): \Foo { expected := `namespace {
static function &(&$var) use (&$a, $b): \Foo {
$a; $a;
}
}` }`
actual := o.String() actual := o.String()
@ -2006,7 +2012,9 @@ func TestPrintAltFor(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")
p := printer.NewPrinter(o, " ") p := printer.NewPrinter(o, " ")
p.Print(&stmt.AltFor{ p.Print(&stmt.Namespace{
Stmts: []node.Node{
&stmt.AltFor{
Init: []node.Node{ Init: []node.Node{
&expr.Variable{VarName: &node.Identifier{Value: "a"}}, &expr.Variable{VarName: &node.Identifier{Value: "a"}},
}, },
@ -2021,11 +2029,15 @@ func TestPrintAltFor(t *testing.T) {
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "d"}}}, &stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "d"}}},
}, },
}, },
},
},
}) })
expected := `for ($a; $b; $c) : expected := `namespace {
for ($a; $b; $c) :
$d; $d;
endfor;` endfor;
}`
actual := o.String() actual := o.String()
if expected != actual { if expected != actual {
@ -2037,7 +2049,9 @@ func TestPrintAltForeach(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")
p := printer.NewPrinter(o, " ") p := printer.NewPrinter(o, " ")
p.Print(&stmt.AltForeach{ p.Print(&stmt.Namespace{
Stmts: []node.Node{
&stmt.AltForeach{
ByRef: true, ByRef: true,
Expr: &expr.Variable{VarName: &node.Identifier{Value: "var"}}, Expr: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
Key: &expr.Variable{VarName: &node.Identifier{Value: "key"}}, Key: &expr.Variable{VarName: &node.Identifier{Value: "key"}},
@ -2047,11 +2061,15 @@ func TestPrintAltForeach(t *testing.T) {
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "d"}}}, &stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "d"}}},
}, },
}, },
},
},
}) })
expected := `foreach ($var as $key => &$val) : expected := `namespace {
foreach ($var as $key => &$val) :
$d; $d;
endforeach;` endforeach;
}`
actual := o.String() actual := o.String()
if expected != actual { if expected != actual {
@ -2063,7 +2081,9 @@ func TestPrintAltIf(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")
p := printer.NewPrinter(o, " ") p := printer.NewPrinter(o, " ")
p.Print(&stmt.AltIf{ p.Print(&stmt.Namespace{
Stmts: []node.Node{
&stmt.AltIf{
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}}, Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
Stmt: &stmt.StmtList{ Stmt: &stmt.StmtList{
Stmts: []node.Node{ Stmts: []node.Node{
@ -2091,16 +2111,20 @@ func TestPrintAltIf(t *testing.T) {
}, },
}, },
}, },
},
},
}) })
expected := `if ($a) : expected := `namespace {
if ($a) :
$d; $d;
elseif ($b) : elseif ($b) :
$b; $b;
elseif ($c) : elseif ($c) :
else : else :
$b; $b;
endif;` endif;
}`
actual := o.String() actual := o.String()
if expected != actual { if expected != actual {
@ -2112,7 +2136,9 @@ func TestPrintStmtAltSwitch(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")
p := printer.NewPrinter(o, " ") p := printer.NewPrinter(o, " ")
p.Print(&stmt.AltSwitch{ p.Print(&stmt.Namespace{
Stmts: []node.Node{
&stmt.AltSwitch{
Cond: &expr.Variable{VarName: &node.Identifier{Value: "var"}}, Cond: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
Cases: []node.Node{ Cases: []node.Node{
&stmt.Case{ &stmt.Case{
@ -2128,14 +2154,18 @@ func TestPrintStmtAltSwitch(t *testing.T) {
}, },
}, },
}, },
},
},
}) })
expected := `switch ($var) : expected := `namespace {
switch ($var) :
case 'a': case 'a':
$a; $a;
case 'b': case 'b':
$b; $b;
endswitch;` endswitch;
}`
actual := o.String() actual := o.String()
if expected != actual { if expected != actual {
@ -2147,18 +2177,24 @@ func TestPrintAltWhile(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")
p := printer.NewPrinter(o, " ") p := printer.NewPrinter(o, " ")
p.Print(&stmt.AltWhile{ p.Print(&stmt.Namespace{
Stmts: []node.Node{
&stmt.AltWhile{
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}}, Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
Stmt: &stmt.StmtList{ Stmt: &stmt.StmtList{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}}}, &stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}}},
}, },
}, },
},
},
}) })
expected := `while ($a) : expected := `namespace {
while ($a) :
$b; $b;
endwhile;` endwhile;
}`
actual := o.String() actual := o.String()
if expected != actual { if expected != actual {
@ -2223,7 +2259,9 @@ func TestPrintStmtCatch(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")
p := printer.NewPrinter(o, " ") p := printer.NewPrinter(o, " ")
p.Print(&stmt.Catch{ p.Print(&stmt.Namespace{
Stmts: []node.Node{
&stmt.Catch{
Types: []node.Node{ Types: []node.Node{
&name.Name{Parts: []node.Node{&name.NamePart{Value: "Exception"}}}, &name.Name{Parts: []node.Node{&name.NamePart{Value: "Exception"}}},
&name.FullyQualified{Parts: []node.Node{&name.NamePart{Value: "RuntimeException"}}}, &name.FullyQualified{Parts: []node.Node{&name.NamePart{Value: "RuntimeException"}}},
@ -2232,10 +2270,14 @@ func TestPrintStmtCatch(t *testing.T) {
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}}, &stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
}, },
},
},
}) })
expected := `catch (Exception | \RuntimeException $e) { expected := `namespace {
catch (Exception | \RuntimeException $e) {
$a; $a;
}
}` }`
actual := o.String() actual := o.String()
@ -2285,7 +2327,9 @@ func TestPrintStmtClass(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")
p := printer.NewPrinter(o, " ") p := printer.NewPrinter(o, " ")
p.Print(&stmt.Class{ p.Print(&stmt.Namespace{
Stmts: []node.Node{
&stmt.Class{
Modifiers: []node.Node{&node.Identifier{Value: "abstract"}}, Modifiers: []node.Node{&node.Identifier{Value: "abstract"}},
ClassName: &node.Identifier{Value: "Foo"}, ClassName: &node.Identifier{Value: "Foo"},
Extends: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Bar"}}}, Extends: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Bar"}}},
@ -2304,11 +2348,15 @@ func TestPrintStmtClass(t *testing.T) {
}, },
}, },
}, },
},
},
}) })
expected := `abstract class Foo extends Bar implements Baz, Quuz expected := `namespace {
abstract class Foo extends Bar implements Baz, Quuz
{ {
public const FOO = 'bar'; public const FOO = 'bar';
}
}` }`
actual := o.String() actual := o.String()
@ -2321,7 +2369,9 @@ func TestPrintStmtAnonymousClass(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")
p := printer.NewPrinter(o, " ") p := printer.NewPrinter(o, " ")
p.Print(&stmt.Class{ p.Print(&stmt.Namespace{
Stmts: []node.Node{
&stmt.Class{
Modifiers: []node.Node{&node.Identifier{Value: "abstract"}}, Modifiers: []node.Node{&node.Identifier{Value: "abstract"}},
Args: []node.Node{ Args: []node.Node{
&node.Argument{ &node.Argument{
@ -2347,11 +2397,15 @@ func TestPrintStmtAnonymousClass(t *testing.T) {
}, },
}, },
}, },
},
},
}) })
expected := `abstract class($a, $b) extends Bar implements Baz, Quuz expected := `namespace {
abstract class($a, $b) extends Bar implements Baz, Quuz
{ {
public const FOO = 'bar'; public const FOO = 'bar';
}
}` }`
actual := o.String() actual := o.String()
@ -2423,7 +2477,9 @@ func TestPrintStmtDeclareStmts(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")
p := printer.NewPrinter(o, " ") p := printer.NewPrinter(o, " ")
p.Print(&stmt.Declare{ p.Print(&stmt.StmtList{
Stmts: []node.Node{
&stmt.Declare{
Consts: []node.Node{ Consts: []node.Node{
&stmt.Constant{ &stmt.Constant{
ConstantName: &node.Identifier{Value: "FOO"}, ConstantName: &node.Identifier{Value: "FOO"},
@ -2435,10 +2491,14 @@ func TestPrintStmtDeclareStmts(t *testing.T) {
&stmt.Nop{}, &stmt.Nop{},
}, },
}, },
},
},
}) })
expected := `declare(FOO = 'bar') { expected := `{
declare(FOO = 'bar') {
; ;
}
}` }`
actual := o.String() actual := o.String()
@ -2451,7 +2511,9 @@ func TestPrintStmtDeclareExpr(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")
p := printer.NewPrinter(o, " ") p := printer.NewPrinter(o, " ")
p.Print(&stmt.Declare{ p.Print(&stmt.StmtList{
Stmts: []node.Node{
&stmt.Declare{
Consts: []node.Node{ Consts: []node.Node{
&stmt.Constant{ &stmt.Constant{
ConstantName: &node.Identifier{Value: "FOO"}, ConstantName: &node.Identifier{Value: "FOO"},
@ -2459,10 +2521,14 @@ func TestPrintStmtDeclareExpr(t *testing.T) {
}, },
}, },
Stmt: &stmt.Expression{Expr: &scalar.String{Value: "bar"}}, Stmt: &stmt.Expression{Expr: &scalar.String{Value: "bar"}},
},
},
}) })
expected := `declare(FOO = 'bar') expected := `{
'bar';` declare(FOO = 'bar')
'bar';
}`
actual := o.String() actual := o.String()
if expected != actual { if expected != actual {
@ -2531,16 +2597,22 @@ func TestPrintStmtDo_Expression(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")
p := printer.NewPrinter(o, " ") p := printer.NewPrinter(o, " ")
p.Print(&stmt.Do{ p.Print(&stmt.Namespace{
Stmts: []node.Node{
&stmt.Do{
Cond: &scalar.Lnumber{Value: "1"}, Cond: &scalar.Lnumber{Value: "1"},
Stmt: &stmt.Expression{ Stmt: &stmt.Expression{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}, Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
}, },
},
},
}) })
expected := `do expected := `namespace {
do
$a; $a;
while (1);` while (1);
}`
actual := o.String() actual := o.String()
if expected != actual { if expected != actual {
@ -2552,18 +2624,24 @@ func TestPrintStmtDo_StmtList(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")
p := printer.NewPrinter(o, " ") p := printer.NewPrinter(o, " ")
p.Print(&stmt.Do{ p.Print(&stmt.Namespace{
Stmts: []node.Node{
&stmt.Do{
Cond: &scalar.Lnumber{Value: "1"}, Cond: &scalar.Lnumber{Value: "1"},
Stmt: &stmt.StmtList{ Stmt: &stmt.StmtList{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}}, &stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
}, },
}, },
},
},
}) })
expected := `do { expected := `namespace {
do {
$a; $a;
} while (1);` } while (1);
}`
actual := o.String() actual := o.String()
if expected != actual { if expected != actual {
@ -2721,14 +2799,20 @@ func TestPrintStmtFinally(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")
p := printer.NewPrinter(o, " ") p := printer.NewPrinter(o, " ")
p.Print(&stmt.Finally{ p.Print(&stmt.Namespace{
Stmts: []node.Node{
&stmt.Finally{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Nop{}, &stmt.Nop{},
}, },
},
},
}) })
expected := `finally { expected := `namespace {
finally {
; ;
}
}` }`
actual := o.String() actual := o.String()
@ -2741,7 +2825,9 @@ func TestPrintStmtForStmts(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")
p := printer.NewPrinter(o, " ") p := printer.NewPrinter(o, " ")
p.Print(&stmt.For{ p.Print(&stmt.Namespace{
Stmts: []node.Node{
&stmt.For{
Init: []node.Node{ Init: []node.Node{
&expr.Variable{VarName: &node.Identifier{Value: "a"}}, &expr.Variable{VarName: &node.Identifier{Value: "a"}},
&expr.Variable{VarName: &node.Identifier{Value: "b"}}, &expr.Variable{VarName: &node.Identifier{Value: "b"}},
@ -2759,10 +2845,14 @@ func TestPrintStmtForStmts(t *testing.T) {
&stmt.Nop{}, &stmt.Nop{},
}, },
}, },
},
},
}) })
expected := `for ($a, $b; $c, $d; $e, $f) { expected := `namespace {
for ($a, $b; $c, $d; $e, $f) {
; ;
}
}` }`
actual := o.String() actual := o.String()
@ -2775,7 +2865,9 @@ func TestPrintStmtForExpr(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")
p := printer.NewPrinter(o, " ") p := printer.NewPrinter(o, " ")
p.Print(&stmt.For{ p.Print(&stmt.Namespace{
Stmts: []node.Node{
&stmt.For{
Init: []node.Node{ Init: []node.Node{
&expr.Variable{VarName: &node.Identifier{Value: "a"}}, &expr.Variable{VarName: &node.Identifier{Value: "a"}},
}, },
@ -2786,10 +2878,14 @@ func TestPrintStmtForExpr(t *testing.T) {
&expr.Variable{VarName: &node.Identifier{Value: "c"}}, &expr.Variable{VarName: &node.Identifier{Value: "c"}},
}, },
Stmt: &stmt.Expression{Expr: &scalar.String{Value: "bar"}}, Stmt: &stmt.Expression{Expr: &scalar.String{Value: "bar"}},
},
},
}) })
expected := `for ($a; $b; $c) expected := `namespace {
'bar';` for ($a; $b; $c)
'bar';
}`
actual := o.String() actual := o.String()
if expected != actual { if expected != actual {
@ -2826,7 +2922,9 @@ func TestPrintStmtForeachStmts(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")
p := printer.NewPrinter(o, " ") p := printer.NewPrinter(o, " ")
p.Print(&stmt.Foreach{ p.Print(&stmt.Namespace{
Stmts: []node.Node{
&stmt.Foreach{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}, Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
Variable: &expr.Variable{VarName: &node.Identifier{Value: "b"}}, Variable: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
Stmt: &stmt.StmtList{ Stmt: &stmt.StmtList{
@ -2834,10 +2932,14 @@ func TestPrintStmtForeachStmts(t *testing.T) {
&stmt.Nop{}, &stmt.Nop{},
}, },
}, },
},
},
}) })
expected := `foreach ($a as $b) { expected := `namespace {
foreach ($a as $b) {
; ;
}
}` }`
actual := o.String() actual := o.String()
@ -2850,15 +2952,21 @@ func TestPrintStmtForeachExpr(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")
p := printer.NewPrinter(o, " ") p := printer.NewPrinter(o, " ")
p.Print(&stmt.Foreach{ p.Print(&stmt.Namespace{
Stmts: []node.Node{
&stmt.Foreach{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}, Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
Key: &expr.Variable{VarName: &node.Identifier{Value: "k"}}, Key: &expr.Variable{VarName: &node.Identifier{Value: "k"}},
Variable: &expr.Variable{VarName: &node.Identifier{Value: "v"}}, Variable: &expr.Variable{VarName: &node.Identifier{Value: "v"}},
Stmt: &stmt.Expression{Expr: &scalar.String{Value: "bar"}}, Stmt: &stmt.Expression{Expr: &scalar.String{Value: "bar"}},
},
},
}) })
expected := `foreach ($a as $k => $v) expected := `namespace {
'bar';` foreach ($a as $k => $v)
'bar';
}`
actual := o.String() actual := o.String()
if expected != actual { if expected != actual {
@ -2890,7 +2998,9 @@ func TestPrintStmtFunction(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")
p := printer.NewPrinter(o, " ") p := printer.NewPrinter(o, " ")
p.Print(&stmt.Function{ p.Print(&stmt.Namespace{
Stmts: []node.Node{
&stmt.Function{
ReturnsRef: true, ReturnsRef: true,
FunctionName: &node.Identifier{Value: "foo"}, FunctionName: &node.Identifier{Value: "foo"},
Params: []node.Node{ Params: []node.Node{
@ -2904,10 +3014,14 @@ func TestPrintStmtFunction(t *testing.T) {
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Nop{}, &stmt.Nop{},
}, },
},
},
}) })
expected := `function &foo(&$var): \Foo { expected := `namespace {
function &foo(&$var): \Foo {
; ;
}
}` }`
actual := o.String() actual := o.String()
@ -2995,7 +3109,9 @@ func TestPrintIfExpression(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")
p := printer.NewPrinter(o, " ") p := printer.NewPrinter(o, " ")
p.Print(&stmt.If{ p.Print(&stmt.Namespace{
Stmts: []node.Node{
&stmt.If{
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}}, Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
Stmt: &stmt.Expression{ Stmt: &stmt.Expression{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}}, Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
@ -3021,16 +3137,20 @@ func TestPrintIfExpression(t *testing.T) {
Expr: &expr.Variable{VarName: &node.Identifier{Value: "f"}}, Expr: &expr.Variable{VarName: &node.Identifier{Value: "f"}},
}, },
}, },
},
},
}) })
expected := `if ($a) expected := `namespace {
if ($a)
$b; $b;
elseif ($c) { elseif ($c) {
$d; $d;
} }
elseif ($e); elseif ($e);
else else
$f;` $f;
}`
actual := o.String() actual := o.String()
if expected != actual { if expected != actual {
@ -3042,7 +3162,9 @@ func TestPrintIfStmtList(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")
p := printer.NewPrinter(o, " ") p := printer.NewPrinter(o, " ")
p.Print(&stmt.If{ p.Print(&stmt.Namespace{
Stmts: []node.Node{
&stmt.If{
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}}, Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
Stmt: &stmt.StmtList{ Stmt: &stmt.StmtList{
Stmts: []node.Node{ Stmts: []node.Node{
@ -3051,10 +3173,14 @@ func TestPrintIfStmtList(t *testing.T) {
}, },
}, },
}, },
},
},
}) })
expected := `if ($a) { expected := `namespace {
if ($a) {
$b; $b;
}
}` }`
actual := o.String() actual := o.String()
@ -3100,7 +3226,9 @@ func TestPrintInterface(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")
p := printer.NewPrinter(o, " ") p := printer.NewPrinter(o, " ")
p.Print(&stmt.Interface{ p.Print(&stmt.Namespace{
Stmts: []node.Node{
&stmt.Interface{
InterfaceName: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}}, InterfaceName: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}},
Extends: []node.Node{ Extends: []node.Node{
&name.Name{Parts: []node.Node{&name.NamePart{Value: "Bar"}}}, &name.Name{Parts: []node.Node{&name.NamePart{Value: "Bar"}}},
@ -3116,14 +3244,18 @@ func TestPrintInterface(t *testing.T) {
}, },
}, },
}, },
},
},
}) })
expected := `interface Foo extends Bar, Baz expected := `namespace {
interface Foo extends Bar, Baz
{ {
public function foo() public function foo()
{ {
$a; $a;
} }
}
}` }`
actual := o.String() actual := o.String()
@ -3168,15 +3300,21 @@ func TestPrintNamespaceWithStmts(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")
p := printer.NewPrinter(o, " ") p := printer.NewPrinter(o, " ")
p.Print(&stmt.Namespace{ p.Print(&stmt.StmtList{
Stmts: []node.Node{
&stmt.Namespace{
NamespaceName: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}}, NamespaceName: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}},
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}}, &stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
}, },
},
},
}) })
expected := `namespace Foo { expected := `{
namespace Foo {
$a; $a;
}
}` }`
actual := o.String() actual := o.String()
@ -3321,11 +3459,49 @@ $b;
} }
} }
func TestPrintStmtListNested(t *testing.T) {
o := bytes.NewBufferString("")
p := printer.NewPrinter(o, " ")
p.Print(&stmt.StmtList{
Stmts: []node.Node{
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
&stmt.StmtList{
Stmts: []node.Node{
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}}},
&stmt.StmtList{
Stmts: []node.Node{
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "c"}}},
},
},
},
},
},
})
expected := `{
$a;
{
$b;
{
$c;
}
}
}`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrintStmtSwitch(t *testing.T) { func TestPrintStmtSwitch(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")
p := printer.NewPrinter(o, " ") p := printer.NewPrinter(o, " ")
p.Print(&stmt.Switch{ p.Print(&stmt.StmtList{
Stmts: []node.Node{
&stmt.Switch{
Cond: &expr.Variable{VarName: &node.Identifier{Value: "var"}}, Cond: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
Cases: []node.Node{ Cases: []node.Node{
&stmt.Case{ &stmt.Case{
@ -3341,13 +3517,17 @@ func TestPrintStmtSwitch(t *testing.T) {
}, },
}, },
}, },
},
},
}) })
expected := `switch ($var) { expected := `{
switch ($var) {
case 'a': case 'a':
$a; $a;
case 'b': case 'b':
$b; $b;
}
}` }`
actual := o.String() actual := o.String()
@ -3456,7 +3636,9 @@ func TestPrintStmtTraitAdaptations(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")
p := printer.NewPrinter(o, " ") p := printer.NewPrinter(o, " ")
p.Print(&stmt.TraitUse{ p.Print(&stmt.Namespace{
Stmts: []node.Node{
&stmt.TraitUse{
Traits: []node.Node{ Traits: []node.Node{
&name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}}, &name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}},
&name.Name{Parts: []node.Node{&name.NamePart{Value: "Bar"}}}, &name.Name{Parts: []node.Node{&name.NamePart{Value: "Bar"}}},
@ -3470,10 +3652,14 @@ func TestPrintStmtTraitAdaptations(t *testing.T) {
Alias: &node.Identifier{Value: "b"}, Alias: &node.Identifier{Value: "b"},
}, },
}, },
},
},
}) })
expected := `use Foo, Bar { expected := `namespace {
use Foo, Bar {
Foo::a as b; Foo::a as b;
}
}` }`
actual := o.String() actual := o.String()
@ -3486,7 +3672,9 @@ func TestPrintTrait(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")
p := printer.NewPrinter(o, " ") p := printer.NewPrinter(o, " ")
p.Print(&stmt.Trait{ p.Print(&stmt.Namespace{
Stmts: []node.Node{
&stmt.Trait{
TraitName: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}}, TraitName: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}},
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.ClassMethod{ &stmt.ClassMethod{
@ -3498,14 +3686,18 @@ func TestPrintTrait(t *testing.T) {
}, },
}, },
}, },
},
},
}) })
expected := `trait Foo expected := `namespace {
trait Foo
{ {
public function foo() public function foo()
{ {
$a; $a;
} }
}
}` }`
actual := o.String() actual := o.String()
@ -3518,7 +3710,9 @@ func TestPrintStmtTry(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")
p := printer.NewPrinter(o, " ") p := printer.NewPrinter(o, " ")
p.Print(&stmt.Try{ p.Print(&stmt.Namespace{
Stmts: []node.Node{
&stmt.Try{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}}, &stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
}, },
@ -3539,9 +3733,12 @@ func TestPrintStmtTry(t *testing.T) {
&stmt.Nop{}, &stmt.Nop{},
}, },
}, },
},
},
}) })
expected := `try { expected := `namespace {
try {
$a; $a;
} }
catch (Exception | \RuntimeException $e) { catch (Exception | \RuntimeException $e) {
@ -3549,6 +3746,7 @@ $b;
} }
finally { finally {
; ;
}
}` }`
actual := o.String() actual := o.String()
@ -3623,17 +3821,23 @@ func TestPrintWhileStmtList(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")
p := printer.NewPrinter(o, " ") p := printer.NewPrinter(o, " ")
p.Print(&stmt.While{ p.Print(&stmt.Namespace{
Stmts: []node.Node{
&stmt.While{
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}}, Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
Stmt: &stmt.StmtList{ Stmt: &stmt.StmtList{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}}, &stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
}, },
}, },
},
},
}) })
expected := `while ($a) { expected := `namespace {
while ($a) {
$a; $a;
}
}` }`
actual := o.String() actual := o.String()
@ -3646,13 +3850,19 @@ func TestPrintWhileExpression(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")
p := printer.NewPrinter(o, " ") p := printer.NewPrinter(o, " ")
p.Print(&stmt.While{ p.Print(&stmt.Namespace{
Stmts: []node.Node{
&stmt.While{
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}}, Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
Stmt: &stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}}, Stmt: &stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
},
},
}) })
expected := `while ($a) expected := `namespace {
$a;` while ($a)
$a;
}`
actual := o.String() actual := o.String()
if expected != actual { if expected != actual {