From 7b8b1ce7b970b6ddf1a894cc4081417dcb65a950 Mon Sep 17 00:00:00 2001 From: Vadym Slizov Date: Sun, 20 Dec 2020 01:28:47 +0200 Subject: [PATCH] refactoring: update formatter --- pkg/ast/visitor/formatter.go | 1997 ++++++++ pkg/ast/visitor/formatter_test.go | 6740 ++++++++++++++++++++++++++++ pkg/printer/pretty_printer.go | 2181 --------- pkg/printer/pretty_printer_test.go | 4097 ----------------- 4 files changed, 8737 insertions(+), 6278 deletions(-) create mode 100644 pkg/ast/visitor/formatter.go create mode 100644 pkg/ast/visitor/formatter_test.go delete mode 100644 pkg/printer/pretty_printer.go delete mode 100644 pkg/printer/pretty_printer_test.go diff --git a/pkg/ast/visitor/formatter.go b/pkg/ast/visitor/formatter.go new file mode 100644 index 0000000..31b1475 --- /dev/null +++ b/pkg/ast/visitor/formatter.go @@ -0,0 +1,1997 @@ +package visitor + +import ( + "bytes" + "github.com/z7zmey/php-parser/pkg/ast" + "github.com/z7zmey/php-parser/pkg/token" +) + +type formatterState int + +const ( + FormatterStateHTML formatterState = iota + FormatterStatePHP +) + +type formatter struct { + state formatterState + indent int + freeFloating []*token.Token + + lastSemiColon *token.Token +} + +func NewFormatter() *formatter { + return &formatter{} +} + +func (f *formatter) WithState(state formatterState) *formatter { + f.state = state + return f +} + +func (f *formatter) WithIndent(indent int) *formatter { + f.indent = indent + return f +} + +func (f *formatter) addFreeFloating(id token.ID, val []byte) { + f.freeFloating = append(f.freeFloating, &token.Token{ + ID: id, + Value: val, + }) +} + +func (f *formatter) addIndent() { + if f.indent < 1 { + return + } + + f.freeFloating = append(f.freeFloating, &token.Token{ + ID: token.T_WHITESPACE, + Value: bytes.Repeat([]byte(" "), f.indent), + }) +} + +func (f *formatter) resetFreeFloating() { + f.freeFloating = nil +} + +func (f *formatter) getFreeFloating() []*token.Token { + defer f.resetFreeFloating() + + if f.state == FormatterStateHTML { + t := &token.Token{ + ID: token.T_OPEN_TAG, + Value: []byte("') + } else { + *list = insert(*list, i+insertCounter, &ast.StmtNop{ + SemiColonTkn: &token.Token{ + Value: []byte("?>"), + }, + }) + insertCounter++ + } + } else { + f.addFreeFloating(token.T_WHITESPACE, []byte("\n")) + f.addIndent() + } + + stmt.Accept(f) + } +} + +func (f *formatter) newSemicolonTkn() *token.Token { + f.lastSemiColon = f.newToken(';', []byte(";")) + return f.lastSemiColon +} + +func insert(s []ast.Vertex, k int, vs ...ast.Vertex) []ast.Vertex { + if n := len(s) + len(vs); n <= cap(s) { + s2 := s[:n] + copy(s2[k+len(vs):], s[k:]) + copy(s2[k:], vs) + return s2 + } + s2 := make([]ast.Vertex, len(s)+len(vs)) + copy(s2, s[:k]) + copy(s2[k:], vs) + copy(s2[k+len(vs):], s[k:]) + return s2 +} + +func (f *formatter) Root(n *ast.Root) { + f.addFreeFloating(token.T_WHITESPACE, []byte("\n")) + f.addIndent() + + f.formatStmts(&n.Stmts) +} + +func (f *formatter) Nullable(n *ast.Nullable) { + n.QuestionTkn = f.newToken('?', []byte("?")) + n.Expr.Accept(f) +} + +func (f *formatter) Parameter(n *ast.Parameter) { + if n.Type != nil { + n.Type.Accept(f) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + } + + if n.AmpersandTkn != nil { + n.AmpersandTkn = f.newToken('&', []byte("&")) + } + + if n.VariadicTkn != nil { + n.VariadicTkn = f.newToken(token.T_ELLIPSIS, []byte("...")) + } + + n.Var.Accept(f) + + if n.DefaultValue != nil { + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.EqualTkn = f.newToken('=', []byte("=")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.DefaultValue.Accept(f) + } +} + +func (f *formatter) Identifier(n *ast.Identifier) { + if n.IdentifierTkn == nil { + n.IdentifierTkn = f.newToken(token.T_STRING, n.Value) + } else { + n.IdentifierTkn.FreeFloating = f.getFreeFloating() + } +} + +func (f *formatter) Argument(n *ast.Argument) { + if n.VariadicTkn != nil { + n.VariadicTkn = f.newToken(token.T_ELLIPSIS, []byte("...")) + } + + if n.AmpersandTkn != nil { + n.AmpersandTkn = f.newToken('&', []byte("&")) + } + + n.Expr.Accept(f) +} + +func (f *formatter) StmtBreak(n *ast.StmtBreak) { + n.BreakTkn = f.newToken(token.T_BREAK, []byte("break")) + + if n.Expr != nil { + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.Expr.Accept(f) + } + + n.SemiColonTkn = f.newSemicolonTkn() +} + +func (f *formatter) StmtCase(n *ast.StmtCase) { + n.CaseTkn = f.newToken(token.T_CASE, []byte("case")) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.Cond.Accept(f) + + n.CaseSeparatorTkn = f.newToken(':', []byte(":")) + + f.indent++ + f.formatStmts(&n.Stmts) + f.indent-- +} + +func (f *formatter) StmtCatch(n *ast.StmtCatch) { + n.CatchTkn = f.newToken(token.T_CATCH, []byte("catch")) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.OpenParenthesisTkn = f.newToken('(', []byte("(")) + + n.SeparatorTkns = make([]*token.Token, len(n.Types)-1) + for i, t := range n.Types { + t.Accept(f) + + if i != len(n.Types)-1 { + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.SeparatorTkns[i] = f.newToken('|', []byte("|")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + } + } + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Var.Accept(f) + + n.CloseParenthesisTkn = f.newToken(')', []byte(")")) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.OpenCurlyBracketTkn = f.newToken('{', []byte("{")) + + if len(n.Stmts) > 0 { + f.indent++ + f.formatStmts(&n.Stmts) + f.indent-- + + f.addFreeFloating(token.T_WHITESPACE, []byte("\n")) + f.addIndent() + } + + n.CloseCurlyBracketTkn = f.newToken('}', []byte("}")) +} + +func (f *formatter) StmtClass(n *ast.StmtClass) { + for _, m := range n.Modifiers { + m.Accept(f) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + } + + n.ClassTkn = f.newToken(token.T_CLASS, []byte("class")) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.ClassName.Accept(f) + + n.OpenParenthesisTkn = nil + n.CloseParenthesisTkn = nil + if len(n.Arguments) > 0 { + n.OpenParenthesisTkn = f.newToken('(', []byte("(")) + + n.SeparatorTkns = f.formatList(n.Arguments, ',') + + n.CloseParenthesisTkn = f.newToken(')', []byte(")")) + } + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + if n.Extends != nil { + n.Extends.Accept(f) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + } + + if n.Implements != nil { + n.Implements.Accept(f) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + } + + n.OpenCurlyBracketTkn = f.newToken('{', []byte("{")) + + if len(n.Stmts) > 0 { + f.indent++ + f.formatStmts(&n.Stmts) + f.indent-- + + f.addFreeFloating(token.T_WHITESPACE, []byte("\n")) + f.addIndent() + } + + n.CloseCurlyBracketTkn = f.newToken('}', []byte("}")) +} + +func (f *formatter) StmtClassConstList(n *ast.StmtClassConstList) { + for _, m := range n.Modifiers { + m.Accept(f) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + } + + n.ConstTkn = f.newToken(token.T_CONST, []byte("const")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.SeparatorTkns = f.formatList(n.Consts, ',') + + n.SemiColonTkn = f.newSemicolonTkn() +} + +func (f *formatter) StmtClassExtends(n *ast.StmtClassExtends) { + n.ExtendTkn = f.newToken(token.T_EXTENDS, []byte("extends")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.ClassName.Accept(f) +} + +func (f *formatter) StmtClassImplements(n *ast.StmtClassImplements) { + n.ImplementsTkn = f.newToken(token.T_IMPLEMENTS, []byte("implements")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.SeparatorTkns = f.formatList(n.InterfaceNames, ',') +} + +func (f *formatter) StmtClassMethod(n *ast.StmtClassMethod) { + for _, m := range n.Modifiers { + m.Accept(f) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + } + + n.FunctionTkn = f.newToken(token.T_FUNCTION, []byte("function")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + if n.AmpersandTkn != nil { + n.AmpersandTkn = f.newToken('&', []byte("&")) + } + + n.MethodName.Accept(f) + + n.OpenParenthesisTkn = f.newToken('(', []byte("(")) + + n.SeparatorTkns = nil + if len(n.Params) > 0 { + n.SeparatorTkns = f.formatList(n.Params, ',') + } + + n.CloseParenthesisTkn = f.newToken(')', []byte(")")) + + if n.ReturnType != nil { + n.ColonTkn = f.newToken(':', []byte(":")) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.ReturnType.Accept(f) + } + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.Stmt.Accept(f) +} + +func (f *formatter) StmtConstList(n *ast.StmtConstList) { + n.ConstTkn = f.newToken(token.T_CONST, []byte("const")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.SeparatorTkns = f.formatList(n.Consts, ',') + + n.SemiColonTkn = f.newSemicolonTkn() +} + +func (f *formatter) StmtConstant(n *ast.StmtConstant) { + n.Name.Accept(f) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.EqualTkn = f.newToken('=', []byte("=")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Expr.Accept(f) +} + +func (f *formatter) StmtContinue(n *ast.StmtContinue) { + n.ContinueTkn = f.newToken(token.T_CONTINUE, []byte("continue")) + + if n.Expr != nil { + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.Expr.Accept(f) + } + + n.SemiColonTkn = f.newSemicolonTkn() +} + +func (f *formatter) StmtDeclare(n *ast.StmtDeclare) { + n.ColonTkn = nil + n.EndDeclareTkn = nil + n.SemiColonTkn = nil + + n.DeclareTkn = f.newToken(token.T_DECLARE, []byte("declare")) + n.OpenParenthesisTkn = f.newToken('(', []byte("(")) + + n.SeparatorTkns = nil + if len(n.Consts) > 0 { + n.SeparatorTkns = f.formatList(n.Consts, ',') + } + + n.CloseParenthesisTkn = f.newToken(')', []byte(")")) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.Stmt.Accept(f) +} + +func (f *formatter) StmtDefault(n *ast.StmtDefault) { + n.DefaultTkn = f.newToken(token.T_DEFAULT, []byte("default")) + + n.CaseSeparatorTkn = f.newToken(':', []byte(":")) + + f.indent++ + f.formatStmts(&n.Stmts) + f.indent-- +} + +func (f *formatter) StmtDo(n *ast.StmtDo) { + n.DoTkn = f.newToken(token.T_DO, []byte("do")) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.Stmt.Accept(f) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.WhileTkn = f.newToken(token.T_WHILE, []byte("while")) + + n.OpenParenthesisTkn = f.newToken('(', []byte("(")) + n.Cond.Accept(f) + n.CloseParenthesisTkn = f.newToken(')', []byte(")")) + + n.SemiColonTkn = f.newSemicolonTkn() +} + +func (f *formatter) StmtEcho(n *ast.StmtEcho) { + n.EchoTkn = f.newToken(token.T_ECHO, []byte("echo")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.SeparatorTkns = nil + if len(n.Exprs) > 0 { + n.SeparatorTkns = f.formatList(n.Exprs, ',') + } + + n.SemiColonTkn = f.newSemicolonTkn() +} + +func (f *formatter) StmtElse(n *ast.StmtElse) { + n.ColonTkn = nil + + n.ElseTkn = f.newToken(token.T_ELSE, []byte("else")) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.Stmt.Accept(f) +} + +func (f *formatter) StmtElseIf(n *ast.StmtElseIf) { + n.ColonTkn = nil + + n.ElseIfTkn = f.newToken(token.T_ELSEIF, []byte("elseif")) + + n.OpenParenthesisTkn = f.newToken('(', []byte("(")) + n.Cond.Accept(f) + n.CloseParenthesisTkn = f.newToken(')', []byte(")")) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.Stmt.Accept(f) +} + +func (f *formatter) StmtExpression(n *ast.StmtExpression) { + n.Expr.Accept(f) + n.SemiColonTkn = f.newSemicolonTkn() +} + +func (f *formatter) StmtFinally(n *ast.StmtFinally) { + n.FinallyTkn = f.newToken(token.T_FINALLY, []byte("finally")) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.OpenCurlyBracketTkn = f.newToken('{', []byte("{")) + + if len(n.Stmts) > 0 { + f.indent++ + f.formatStmts(&n.Stmts) + f.indent-- + + f.addFreeFloating(token.T_WHITESPACE, []byte("\n")) + f.addIndent() + } + + n.CloseCurlyBracketTkn = f.newToken('}', []byte("}")) +} + +func (f *formatter) StmtFor(n *ast.StmtFor) { + n.ColonTkn = nil + n.EndForTkn = nil + n.SemiColonTkn = nil + + n.ForTkn = f.newToken(token.T_FOR, []byte("for")) + n.OpenParenthesisTkn = f.newToken('(', []byte("(")) + + n.InitSeparatorTkns = nil + if len(n.Init) > 0 { + n.InitSeparatorTkns = f.formatList(n.Init, ',') + } + + n.InitSemiColonTkn = f.newSemicolonTkn() + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.CondSeparatorTkns = nil + if len(n.Cond) > 0 { + n.CondSeparatorTkns = f.formatList(n.Cond, ',') + } + + n.CondSemiColonTkn = f.newSemicolonTkn() + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.LoopSeparatorTkns = nil + if len(n.Loop) > 0 { + n.LoopSeparatorTkns = f.formatList(n.Loop, ',') + } + + n.CloseParenthesisTkn = f.newToken(')', []byte(")")) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.Stmt.Accept(f) +} + +func (f *formatter) StmtForeach(n *ast.StmtForeach) { + n.ColonTkn = nil + n.EndForeachTkn = nil + n.SemiColonTkn = nil + + n.ForeachTkn = f.newToken(token.T_FOREACH, []byte("foreach")) + + n.OpenParenthesisTkn = f.newToken('(', []byte("(")) + + n.Expr.Accept(f) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.AsTkn = f.newToken(token.T_AS, []byte("as")) + + if n.Key != nil { + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.Key.Accept(f) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.DoubleArrowTkn = f.newToken(token.T_DOUBLE_ARROW, []byte("=>")) + } + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.Var.Accept(f) + + n.CloseParenthesisTkn = f.newToken(')', []byte(")")) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.Stmt.Accept(f) +} + +func (f *formatter) StmtFunction(n *ast.StmtFunction) { + n.FunctionTkn = f.newToken(token.T_FUNCTION, []byte("function")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + if n.AmpersandTkn != nil { + n.AmpersandTkn = f.newToken('&', []byte("&")) + } + + n.FunctionName.Accept(f) + + n.OpenParenthesisTkn = f.newToken('(', []byte("(")) + + n.SeparatorTkns = nil + if len(n.Params) > 0 { + n.SeparatorTkns = f.formatList(n.Params, ',') + } + + n.CloseParenthesisTkn = f.newToken(')', []byte(")")) + + if n.ReturnType != nil { + n.ColonTkn = f.newToken(':', []byte(":")) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.ReturnType.Accept(f) + } + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.OpenCurlyBracketTkn = f.newToken('{', []byte("{")) + + if len(n.Stmts) > 0 { + f.indent++ + f.formatStmts(&n.Stmts) + f.indent-- + + f.addFreeFloating(token.T_WHITESPACE, []byte("\n")) + f.addIndent() + } + + n.CloseCurlyBracketTkn = f.newToken('}', []byte("}")) +} + +func (f *formatter) StmtGlobal(n *ast.StmtGlobal) { + n.GlobalTkn = f.newToken(token.T_GLOBAL, []byte("global")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.SeparatorTkns = nil + if len(n.Vars) > 0 { + n.SeparatorTkns = f.formatList(n.Vars, ',') + } + + n.SemiColonTkn = f.newSemicolonTkn() +} + +func (f *formatter) StmtGoto(n *ast.StmtGoto) { + n.GotoTkn = f.newToken(token.T_GOTO, []byte("goto")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Label.Accept(f) + + n.SemiColonTkn = f.newSemicolonTkn() +} + +func (f *formatter) StmtHaltCompiler(n *ast.StmtHaltCompiler) { + n.HaltCompilerTkn = f.newToken(token.T_HALT_COMPILER, []byte("__halt_compiler")) + n.OpenParenthesisTkn = f.newToken('(', []byte("(")) + n.CloseParenthesisTkn = f.newToken(')', []byte(")")) + n.SemiColonTkn = f.newSemicolonTkn() +} + +func (f *formatter) StmtIf(n *ast.StmtIf) { + n.ColonTkn = nil + n.EndIfTkn = nil + n.SemiColonTkn = nil + + n.IfTkn = f.newToken(token.T_IF, []byte("if")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.OpenParenthesisTkn = f.newToken('(', []byte("(")) + n.Cond.Accept(f) + n.CloseParenthesisTkn = f.newToken(')', []byte(")")) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.Stmt.Accept(f) + + if len(n.ElseIf) > 0 { + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + f.formatList(n.ElseIf, ' ') + } + + if n.Else != nil { + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.Else.Accept(f) + } +} + +func (f *formatter) StmtInlineHtml(n *ast.StmtInlineHtml) { + n.InlineHtmlTkn = f.newToken(token.T_STRING, n.Value) + f.state = FormatterStateHTML +} + +func (f *formatter) StmtInterface(n *ast.StmtInterface) { + n.InterfaceTkn = f.newToken(token.T_INTERFACE, []byte("interface")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.InterfaceName.Accept(f) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + if n.Extends != nil { + n.Extends.Accept(f) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + } + + n.OpenCurlyBracketTkn = f.newToken('{', []byte("{")) + + if len(n.Stmts) > 0 { + f.indent++ + f.formatStmts(&n.Stmts) + f.indent-- + + f.addFreeFloating(token.T_WHITESPACE, []byte("\n")) + f.addIndent() + } + + n.CloseCurlyBracketTkn = f.newToken('}', []byte("}")) +} + +func (f *formatter) StmtInterfaceExtends(n *ast.StmtInterfaceExtends) { + n.ExtendsTkn = f.newToken(token.T_EXTENDS, []byte("extends")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.SeparatorTkns = nil + if len(n.InterfaceNames) > 0 { + n.SeparatorTkns = f.formatList(n.InterfaceNames, ',') + } +} + +func (f *formatter) StmtLabel(n *ast.StmtLabel) { + n.LabelName.Accept(f) + n.ColonTkn = f.newToken(':', []byte(":")) +} + +func (f *formatter) StmtNamespace(n *ast.StmtNamespace) { + n.OpenCurlyBracketTkn = nil + n.CloseCurlyBracketTkn = nil + n.SemiColonTkn = nil + + n.NsTkn = f.newToken(token.T_NAMESPACE, []byte("namespace")) + + if n.Name != nil { + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.Name.Accept(f) + } + + if len(n.Stmts) > 0 { + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.OpenCurlyBracketTkn = f.newToken('{', []byte("{")) + if len(n.Stmts) > 0 { + f.indent++ + f.formatStmts(&n.Stmts) + f.indent-- + + f.addFreeFloating(token.T_WHITESPACE, []byte("\n")) + f.addIndent() + } + n.CloseCurlyBracketTkn = f.newToken('}', []byte("}")) + } else { + n.SemiColonTkn = f.newSemicolonTkn() + } + +} + +func (f *formatter) StmtNop(n *ast.StmtNop) { + n.SemiColonTkn = f.newSemicolonTkn() +} + +func (f *formatter) StmtProperty(n *ast.StmtProperty) { + n.Var.Accept(f) + + if n.Expr != nil { + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.EqualTkn = f.newToken('=', []byte("=")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Expr.Accept(f) + } +} + +func (f *formatter) StmtPropertyList(n *ast.StmtPropertyList) { + for _, m := range n.Modifiers { + m.Accept(f) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + } + + if n.Type != nil { + n.Type.Accept(f) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + } + + n.SeparatorTkns = f.formatList(n.Properties, ',') + + n.SemiColonTkn = f.newSemicolonTkn() +} + +func (f *formatter) StmtReturn(n *ast.StmtReturn) { + n.ReturnTkn = f.newToken(token.T_RETURN, []byte("return")) + + if n.Expr != nil { + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.Expr.Accept(f) + } + + n.SemiColonTkn = f.newSemicolonTkn() +} + +func (f *formatter) StmtStatic(n *ast.StmtStatic) { + n.StaticTkn = f.newToken(token.T_STATIC, []byte("static")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.SeparatorTkns = nil + if len(n.Vars) > 0 { + n.SeparatorTkns = f.formatList(n.Vars, ',') + } + + n.SemiColonTkn = f.newSemicolonTkn() +} + +func (f *formatter) StmtStaticVar(n *ast.StmtStaticVar) { + n.Var.Accept(f) + + if n.Expr != nil { + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.EqualTkn = f.newToken('=', []byte("=")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Expr.Accept(f) + } +} + +func (f *formatter) StmtStmtList(n *ast.StmtStmtList) { + n.OpenCurlyBracketTkn = f.newToken('{', []byte("{")) + + if len(n.Stmts) > 0 { + f.indent++ + f.formatStmts(&n.Stmts) + f.indent-- + + f.addFreeFloating(token.T_WHITESPACE, []byte("\n")) + f.addIndent() + } + + n.CloseCurlyBracketTkn = f.newToken('}', []byte("}")) +} + +func (f *formatter) StmtSwitch(n *ast.StmtSwitch) { + n.CaseSeparatorTkn = nil + n.ColonTkn = nil + n.EndSwitchTkn = nil + n.SemiColonTkn = nil + + n.SwitchTkn = f.newToken(token.T_SWITCH, []byte("switch")) + + n.OpenParenthesisTkn = f.newToken('(', []byte("(")) + n.Cond.Accept(f) + n.CloseParenthesisTkn = f.newToken(')', []byte(")")) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.OpenCurlyBracketTkn = f.newToken('{', []byte("{")) + + if len(n.CaseList) > 0 { + f.indent++ + f.formatStmts(&n.CaseList) + f.indent-- + + f.addFreeFloating(token.T_WHITESPACE, []byte("\n")) + f.addIndent() + } + + n.CloseCurlyBracketTkn = f.newToken('}', []byte("}")) +} + +func (f *formatter) StmtThrow(n *ast.StmtThrow) { + n.ThrowTkn = f.newToken(token.T_THROW, []byte("throw")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Expr.Accept(f) + + n.SemiColonTkn = f.newSemicolonTkn() +} + +func (f *formatter) StmtTrait(n *ast.StmtTrait) { + n.TraitTkn = f.newToken(token.T_TRAIT, []byte("trait")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.TraitName.Accept(f) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + if n.Extends != nil { + n.Extends.Accept(f) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + } + + if n.Implements != nil { + n.Implements.Accept(f) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + } + + n.OpenCurlyBracketTkn = f.newToken('{', []byte("{")) + + if len(n.Stmts) > 0 { + f.indent++ + f.formatStmts(&n.Stmts) + f.indent-- + + f.addFreeFloating(token.T_WHITESPACE, []byte("\n")) + f.addIndent() + } + + n.CloseCurlyBracketTkn = f.newToken('}', []byte("}")) +} + +func (f *formatter) StmtTraitAdaptationList(n *ast.StmtTraitAdaptationList) { + n.OpenCurlyBracketTkn = f.newToken('{', []byte("{")) + + if len(n.Adaptations) > 0 { + f.indent++ + f.formatStmts(&n.Adaptations) + f.indent-- + + f.addFreeFloating(token.T_WHITESPACE, []byte("\n")) + f.addIndent() + } + + n.CloseCurlyBracketTkn = f.newToken('}', []byte("}")) +} + +func (f *formatter) StmtTraitMethodRef(n *ast.StmtTraitMethodRef) { + if n.Trait != nil { + n.Trait.Accept(f) + n.DoubleColonTkn = f.newToken(token.T_PAAMAYIM_NEKUDOTAYIM, []byte("::")) + } + + n.Method.Accept(f) +} + +func (f *formatter) StmtTraitUse(n *ast.StmtTraitUse) { + n.UseTkn = f.newToken(token.T_USE, []byte("use")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.SeparatorTkns = f.formatList(n.Traits, ',') + + if _, ok := n.Adaptations.(*ast.StmtTraitAdaptationList); ok { + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + } + + n.Adaptations.Accept(f) +} + +func (f *formatter) StmtTraitUseAlias(n *ast.StmtTraitUseAlias) { + n.Ref.Accept(f) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.AsTkn = f.newToken(token.T_AS, []byte("as")) + + if n.Modifier != nil { + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.Modifier.Accept(f) + } + + if n.Alias != nil { + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.Alias.Accept(f) + } + + n.SemiColonTkn = f.newSemicolonTkn() +} + +func (f *formatter) StmtTraitUsePrecedence(n *ast.StmtTraitUsePrecedence) { + n.Ref.Accept(f) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.InsteadofTkn = f.newToken(token.T_INSTEADOF, []byte("insteadof")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.SeparatorTkns = f.formatList(n.Insteadof, ',') + + n.SemiColonTkn = f.newSemicolonTkn() +} + +func (f *formatter) StmtTry(n *ast.StmtTry) { + n.TryTkn = f.newToken(token.T_TRY, []byte("try")) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.OpenCurlyBracketTkn = f.newToken('{', []byte("{")) + + if len(n.Stmts) > 0 { + f.indent++ + f.formatStmts(&n.Stmts) + f.indent-- + + f.addFreeFloating(token.T_WHITESPACE, []byte("\n")) + f.addIndent() + } + + n.CloseCurlyBracketTkn = f.newToken('}', []byte("}")) + + for _, catch := range n.Catches { + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + catch.Accept(f) + } + + if n.Finally != nil { + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.Finally.Accept(f) + } +} + +func (f *formatter) StmtUnset(n *ast.StmtUnset) { + n.UnsetTkn = f.newToken(token.T_UNSET, []byte("unset")) + + n.OpenParenthesisTkn = f.newToken('(', []byte("(")) + n.SeparatorTkns = f.formatList(n.Vars, ',') + n.CloseParenthesisTkn = f.newToken(')', []byte(")")) + + n.SemiColonTkn = f.newSemicolonTkn() +} + +func (f *formatter) StmtUse(n *ast.StmtUse) { + n.UseTkn = f.newToken(token.T_USE, []byte("use")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + if n.Type != nil { + n.Type.Accept(f) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + } + + n.SeparatorTkns = f.formatList(n.UseDeclarations, ',') + + n.SemiColonTkn = f.newSemicolonTkn() +} + +func (f *formatter) StmtGroupUse(n *ast.StmtGroupUse) { + n.UseTkn = f.newToken(token.T_USE, []byte("use")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + if n.Type != nil { + n.Type.Accept(f) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + } + + n.LeadingNsSeparatorTkn = nil + + n.Prefix.Accept(f) + n.NsSeparatorTkn = f.newToken(token.T_NS_SEPARATOR, []byte("\\")) + + n.OpenCurlyBracketTkn = f.newToken('{', []byte("{")) + n.SeparatorTkns = f.formatList(n.UseDeclarations, ',') + n.CloseCurlyBracketTkn = f.newToken('}', []byte("}")) + + n.SemiColonTkn = f.newSemicolonTkn() +} + +func (f *formatter) StmtUseDeclaration(n *ast.StmtUseDeclaration) { + if n.Type != nil { + n.Type.Accept(f) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + } + + n.NsSeparatorTkn = nil + + n.Use.Accept(f) + + if n.Alias != nil { + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.AsTkn = f.newToken(token.T_AS, []byte("as")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.Alias.Accept(f) + } +} + +func (f *formatter) StmtWhile(n *ast.StmtWhile) { + n.ColonTkn = nil + n.EndWhileTkn = nil + n.SemiColonTkn = nil + + n.WhileTkn = f.newToken(token.T_WHILE, []byte("while")) + n.OpenParenthesisTkn = f.newToken('(', []byte("(")) + n.Cond.Accept(f) + n.CloseParenthesisTkn = f.newToken(')', []byte(")")) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.Stmt.Accept(f) +} + +func (f *formatter) ExprArray(n *ast.ExprArray) { + n.ArrayTkn = f.newToken(token.T_ARRAY, []byte("array")) + n.OpenBracketTkn = f.newToken('(', []byte("(")) + n.SeparatorTkns = f.formatList(n.Items, ',') + n.CloseBracketTkn = f.newToken(')', []byte(")")) +} + +func (f *formatter) ExprArrayDimFetch(n *ast.ExprArrayDimFetch) { + n.Var.Accept(f) + n.OpenBracketTkn = f.newToken('[', []byte("[")) + n.Dim.Accept(f) + n.CloseBracketTkn = f.newToken(']', []byte("]")) +} + +func (f *formatter) ExprArrayItem(n *ast.ExprArrayItem) { + if n.EllipsisTkn != nil { + n.EllipsisTkn = f.newToken(token.T_ELLIPSIS, []byte("...")) + } + + if n.Key != nil { + n.Key.Accept(f) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.DoubleArrowTkn = f.newToken(token.T_DOUBLE_ARROW, []byte("=>")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + } + + n.Val.Accept(f) +} + +func (f *formatter) ExprArrowFunction(n *ast.ExprArrowFunction) { + if n.StaticTkn != nil { + n.StaticTkn = f.newToken(token.T_STATIC, []byte("static")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + } + + n.FnTkn = f.newToken(token.T_FN, []byte("fn")) + + if n.AmpersandTkn != nil { + n.AmpersandTkn = f.newToken('&', []byte("&")) + } + + n.OpenParenthesisTkn = f.newToken('(', []byte("(")) + n.SeparatorTkns = nil + if len(n.Params) > 0 { + n.SeparatorTkns = f.formatList(n.Params, ',') + } + n.CloseParenthesisTkn = f.newToken(')', []byte(")")) + + n.ColonTkn = nil + if n.ReturnType != nil { + n.ColonTkn = f.newToken(':', []byte(":")) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.ReturnType.Accept(f) + } + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.DoubleArrowTkn = f.newToken(token.T_DOUBLE_ARROW, []byte("=>")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Expr.Accept(f) +} + +func (f *formatter) ExprBitwiseNot(n *ast.ExprBitwiseNot) { + n.TildaTkn = f.newToken('~', []byte("~")) + n.Expr.Accept(f) +} + +func (f *formatter) ExprBooleanNot(n *ast.ExprBooleanNot) { + n.ExclamationTkn = f.newToken('!', []byte("!")) + n.Expr.Accept(f) +} + +func (f *formatter) ExprClassConstFetch(n *ast.ExprClassConstFetch) { + n.Class.Accept(f) + n.DoubleColonTkn = f.newToken(token.T_PAAMAYIM_NEKUDOTAYIM, []byte("::")) + n.ConstantName.Accept(f) +} + +func (f *formatter) ExprClone(n *ast.ExprClone) { + n.CloneTkn = f.newToken(token.T_CLONE, []byte("clone")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.Expr.Accept(f) +} + +func (f *formatter) ExprClosure(n *ast.ExprClosure) { + if n.StaticTkn != nil { + n.StaticTkn = f.newToken(token.T_STATIC, []byte("static")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + } + + n.FunctionTkn = f.newToken(token.T_FN, []byte("function")) + + if n.AmpersandTkn != nil { + n.AmpersandTkn = f.newToken('&', []byte("&")) + } + + n.OpenParenthesisTkn = f.newToken('(', []byte("(")) + n.SeparatorTkns = nil + if len(n.Params) > 0 { + n.SeparatorTkns = f.formatList(n.Params, ',') + } + n.CloseParenthesisTkn = f.newToken(')', []byte(")")) + + if n.ClosureUse != nil { + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.ClosureUse.Accept(f) + } + + n.ColonTkn = nil + if n.ReturnType != nil { + n.ColonTkn = f.newToken(':', []byte(":")) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.ReturnType.Accept(f) + } + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.OpenCurlyBracketTkn = f.newToken('{', []byte("{")) + if len(n.Stmts) > 0 { + f.indent++ + f.formatStmts(&n.Stmts) + f.indent-- + + f.addFreeFloating(token.T_WHITESPACE, []byte("\n")) + f.addIndent() + } + n.CloseCurlyBracketTkn = f.newToken('}', []byte("}")) +} + +func (f *formatter) ExprClosureUse(n *ast.ExprClosureUse) { + n.UseTkn = f.newToken(token.T_USE, []byte("use")) + n.OpenParenthesisTkn = f.newToken('(', []byte("(")) + n.SeparatorTkns = f.formatList(n.Uses, ',') + n.CloseParenthesisTkn = f.newToken(')', []byte(")")) +} + +func (f *formatter) ExprConstFetch(n *ast.ExprConstFetch) { + n.Const.Accept(f) +} + +func (f *formatter) ExprEmpty(n *ast.ExprEmpty) { + n.EmptyTkn = f.newToken(token.T_EMPTY, []byte("empty")) + n.OpenParenthesisTkn = f.newToken('(', []byte("(")) + n.Expr.Accept(f) + n.CloseParenthesisTkn = f.newToken(')', []byte(")")) +} + +func (f *formatter) ExprErrorSuppress(n *ast.ExprErrorSuppress) { + n.AtTkn = f.newToken('@', []byte("@")) + n.Expr.Accept(f) +} + +func (f *formatter) ExprEval(n *ast.ExprEval) { + n.EvalTkn = f.newToken(token.T_EVAL, []byte("eval")) + n.OpenParenthesisTkn = f.newToken('(', []byte("(")) + n.Expr.Accept(f) + n.CloseParenthesisTkn = f.newToken(')', []byte(")")) +} + +func (f *formatter) ExprExit(n *ast.ExprExit) { + n.DieTkn = f.newToken(token.T_EVAL, []byte("exit")) + + n.OpenParenthesisTkn = nil + n.CloseParenthesisTkn = nil + if n.Expr != nil { + n.OpenParenthesisTkn = f.newToken('(', []byte("(")) + n.Expr.Accept(f) + n.CloseParenthesisTkn = f.newToken(')', []byte(")")) + } +} + +func (f *formatter) ExprFunctionCall(n *ast.ExprFunctionCall) { + n.Function.Accept(f) + n.OpenParenthesisTkn = f.newToken('(', []byte("(")) + n.SeparatorTkns = nil + if len(n.Arguments) > 0 { + n.SeparatorTkns = f.formatList(n.Arguments, ',') + } + n.CloseParenthesisTkn = f.newToken(')', []byte(")")) +} + +func (f *formatter) ExprInclude(n *ast.ExprInclude) { + n.IncludeTkn = f.newToken(token.T_INCLUDE, []byte("include")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.Expr.Accept(f) +} + +func (f *formatter) ExprIncludeOnce(n *ast.ExprIncludeOnce) { + n.IncludeTkn = f.newToken(token.T_INCLUDE_ONCE, []byte("include_once")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.Expr.Accept(f) +} + +func (f *formatter) ExprInstanceOf(n *ast.ExprInstanceOf) { + n.Expr.Accept(f) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.InstanceOfTkn = f.newToken(token.T_INSTANCEOF, []byte("instanceof")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Class.Accept(f) +} + +func (f *formatter) ExprIsset(n *ast.ExprIsset) { + n.IssetTkn = f.newToken(token.T_ISSET, []byte("isset")) + n.OpenParenthesisTkn = f.newToken('(', []byte("(")) + n.SeparatorTkns = f.formatList(n.Vars, ',') + n.CloseParenthesisTkn = f.newToken(')', []byte(")")) +} + +func (f *formatter) ExprList(n *ast.ExprList) { + n.ListTkn = f.newToken(token.T_LIST, []byte("list")) + n.OpenBracketTkn = f.newToken('(', []byte("(")) + n.SeparatorTkns = f.formatList(n.Items, ',') + n.CloseBracketTkn = f.newToken(')', []byte(")")) +} + +func (f *formatter) ExprMethodCall(n *ast.ExprMethodCall) { + n.Var.Accept(f) + n.ObjectOperatorTkn = f.newToken(token.T_OBJECT_OPERATOR, []byte("->")) + n.Method.Accept(f) + + n.OpenParenthesisTkn = f.newToken('(', []byte("(")) + n.SeparatorTkns = nil + if len(n.Arguments) > 0 { + n.SeparatorTkns = f.formatList(n.Arguments, ',') + } + n.CloseParenthesisTkn = f.newToken(')', []byte(")")) +} + +func (f *formatter) ExprNew(n *ast.ExprNew) { + n.NewTkn = f.newToken(token.T_NEW, []byte("new")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Class.Accept(f) + + n.SeparatorTkns = nil + n.OpenParenthesisTkn = nil + n.CloseParenthesisTkn = nil + if len(n.Arguments) > 0 { + n.OpenParenthesisTkn = f.newToken('(', []byte("(")) + n.SeparatorTkns = f.formatList(n.Arguments, ',') + n.CloseParenthesisTkn = f.newToken(')', []byte(")")) + } +} + +func (f *formatter) ExprPostDec(n *ast.ExprPostDec) { + n.Var.Accept(f) + n.DecTkn = f.newToken(token.T_DEC, []byte("--")) +} + +func (f *formatter) ExprPostInc(n *ast.ExprPostInc) { + n.Var.Accept(f) + n.IncTkn = f.newToken(token.T_INC, []byte("++")) +} + +func (f *formatter) ExprPreDec(n *ast.ExprPreDec) { + n.DecTkn = f.newToken(token.T_DEC, []byte("--")) + n.Var.Accept(f) +} + +func (f *formatter) ExprPreInc(n *ast.ExprPreInc) { + n.IncTkn = f.newToken(token.T_INC, []byte("++")) + n.Var.Accept(f) +} + +func (f *formatter) ExprPrint(n *ast.ExprPrint) { + n.PrintTkn = f.newToken(token.T_PRINT, []byte("print")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Expr.Accept(f) +} + +func (f *formatter) ExprPropertyFetch(n *ast.ExprPropertyFetch) { + n.Var.Accept(f) + n.ObjectOperatorTkn = f.newToken(token.T_OBJECT_OPERATOR, []byte("->")) + n.Property.Accept(f) +} + +func (f *formatter) ExprReference(n *ast.ExprReference) { + n.AmpersandTkn = f.newToken('&', []byte("&")) + n.Var.Accept(f) +} + +func (f *formatter) ExprRequire(n *ast.ExprRequire) { + n.RequireTkn = f.newToken(token.T_REQUIRE, []byte("require")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.Expr.Accept(f) +} + +func (f *formatter) ExprRequireOnce(n *ast.ExprRequireOnce) { + n.RequireOnceTkn = f.newToken(token.T_REQUIRE_ONCE, []byte("require_once")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.Expr.Accept(f) +} + +func (f *formatter) ExprShellExec(n *ast.ExprShellExec) { + n.OpenBacktickTkn = f.newToken('`', []byte("`")) + for _, p := range n.Parts { + p.Accept(f) + } + n.CloseBacktickTkn = f.newToken('`', []byte("`")) +} + +func (f *formatter) ExprStaticCall(n *ast.ExprStaticCall) { + n.Class.Accept(f) + n.DoubleColonTkn = f.newToken(token.T_PAAMAYIM_NEKUDOTAYIM, []byte("::")) + n.Call.Accept(f) + + n.OpenParenthesisTkn = f.newToken('(', []byte("(")) + n.SeparatorTkns = nil + if len(n.Arguments) > 0 { + n.SeparatorTkns = f.formatList(n.Arguments, ',') + } + n.CloseParenthesisTkn = f.newToken(')', []byte(")")) +} + +func (f *formatter) ExprStaticPropertyFetch(n *ast.ExprStaticPropertyFetch) { + n.Class.Accept(f) + n.DoubleColonTkn = f.newToken(token.T_PAAMAYIM_NEKUDOTAYIM, []byte("::")) + n.Property.Accept(f) +} + +func (f *formatter) ExprTernary(n *ast.ExprTernary) { + n.Condition.Accept(f) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.QuestionTkn = f.newToken('?', []byte("?")) + if n.IfTrue != nil { + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.IfTrue.Accept(f) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + } + n.ColonTkn = f.newToken(':', []byte(":")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.IfFalse.Accept(f) +} + +func (f *formatter) ExprUnaryMinus(n *ast.ExprUnaryMinus) { + n.MinusTkn = f.newToken('-', []byte("-")) + n.Expr.Accept(f) +} + +func (f *formatter) ExprUnaryPlus(n *ast.ExprUnaryPlus) { + n.PlusTkn = f.newToken('+', []byte("+")) + n.Expr.Accept(f) +} + +func (f *formatter) ExprVariable(n *ast.ExprVariable) { + if _, ok := n.VarName.(*ast.Identifier); !ok { + n.DollarTkn = f.newToken('$', []byte("$")) + } + + n.VarName.Accept(f) +} + +func (f *formatter) ExprYield(n *ast.ExprYield) { + n.YieldTkn = f.newToken(token.T_YIELD, []byte("yield")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + if n.Key != nil { + n.Key.Accept(f) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.DoubleArrowTkn = f.newToken(token.T_DOUBLE_ARROW, []byte("=>")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + } + + n.Value.Accept(f) +} + +func (f *formatter) ExprYieldFrom(n *ast.ExprYieldFrom) { + n.YieldFromTkn = f.newToken(token.T_YIELD_FROM, []byte("yield from")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Expr.Accept(f) +} + +func (f *formatter) ExprAssign(n *ast.ExprAssign) { + n.Var.Accept(f) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.EqualTkn = f.newToken('=', []byte("=")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Expr.Accept(f) +} + +func (f *formatter) ExprAssignReference(n *ast.ExprAssignReference) { + n.Var.Accept(f) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.EqualTkn = f.newToken('=', []byte("=")) + n.AmpersandTkn = f.newToken('&', []byte("&")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Expr.Accept(f) +} + +func (f *formatter) ExprAssignBitwiseAnd(n *ast.ExprAssignBitwiseAnd) { + n.Var.Accept(f) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.EqualTkn = f.newToken(token.T_AND_EQUAL, []byte("&=")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Expr.Accept(f) +} + +func (f *formatter) ExprAssignBitwiseOr(n *ast.ExprAssignBitwiseOr) { + n.Var.Accept(f) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.EqualTkn = f.newToken(token.T_OR_EQUAL, []byte("|=")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Expr.Accept(f) +} + +func (f *formatter) ExprAssignBitwiseXor(n *ast.ExprAssignBitwiseXor) { + n.Var.Accept(f) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.EqualTkn = f.newToken(token.T_XOR_EQUAL, []byte("^=")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Expr.Accept(f) +} + +func (f *formatter) ExprAssignCoalesce(n *ast.ExprAssignCoalesce) { + n.Var.Accept(f) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.EqualTkn = f.newToken(token.T_COALESCE_EQUAL, []byte("??=")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Expr.Accept(f) +} + +func (f *formatter) ExprAssignConcat(n *ast.ExprAssignConcat) { + n.Var.Accept(f) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.EqualTkn = f.newToken(token.T_CONCAT_EQUAL, []byte(".=")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Expr.Accept(f) +} + +func (f *formatter) ExprAssignDiv(n *ast.ExprAssignDiv) { + n.Var.Accept(f) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.EqualTkn = f.newToken(token.T_DIV_EQUAL, []byte("/=")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Expr.Accept(f) +} + +func (f *formatter) ExprAssignMinus(n *ast.ExprAssignMinus) { + n.Var.Accept(f) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.EqualTkn = f.newToken(token.T_MINUS_EQUAL, []byte("-=")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Expr.Accept(f) +} + +func (f *formatter) ExprAssignMod(n *ast.ExprAssignMod) { + n.Var.Accept(f) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.EqualTkn = f.newToken(token.T_MOD_EQUAL, []byte("%=")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Expr.Accept(f) +} + +func (f *formatter) ExprAssignMul(n *ast.ExprAssignMul) { + n.Var.Accept(f) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.EqualTkn = f.newToken(token.T_MUL_EQUAL, []byte("*=")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Expr.Accept(f) +} + +func (f *formatter) ExprAssignPlus(n *ast.ExprAssignPlus) { + n.Var.Accept(f) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.EqualTkn = f.newToken(token.T_PLUS_EQUAL, []byte("+=")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Expr.Accept(f) +} + +func (f *formatter) ExprAssignPow(n *ast.ExprAssignPow) { + n.Var.Accept(f) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.EqualTkn = f.newToken(token.T_POW_EQUAL, []byte("**=")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Expr.Accept(f) +} + +func (f *formatter) ExprAssignShiftLeft(n *ast.ExprAssignShiftLeft) { + n.Var.Accept(f) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.EqualTkn = f.newToken(token.T_SL_EQUAL, []byte("<<=")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Expr.Accept(f) +} + +func (f *formatter) ExprAssignShiftRight(n *ast.ExprAssignShiftRight) { + n.Var.Accept(f) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.EqualTkn = f.newToken(token.T_SR_EQUAL, []byte(">>=")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Expr.Accept(f) +} + +func (f *formatter) ExprBinaryBitwiseAnd(n *ast.ExprBinaryBitwiseAnd) { + n.Left.Accept(f) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.OpTkn = f.newToken('&', []byte("&")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Right.Accept(f) +} + +func (f *formatter) ExprBinaryBitwiseOr(n *ast.ExprBinaryBitwiseOr) { + n.Left.Accept(f) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.OpTkn = f.newToken('|', []byte("|")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Right.Accept(f) +} + +func (f *formatter) ExprBinaryBitwiseXor(n *ast.ExprBinaryBitwiseXor) { + n.Left.Accept(f) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.OpTkn = f.newToken('^', []byte("^")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Right.Accept(f) +} + +func (f *formatter) ExprBinaryBooleanAnd(n *ast.ExprBinaryBooleanAnd) { + n.Left.Accept(f) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.OpTkn = f.newToken(token.T_BOOLEAN_AND, []byte("&&")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Right.Accept(f) +} + +func (f *formatter) ExprBinaryBooleanOr(n *ast.ExprBinaryBooleanOr) { + n.Left.Accept(f) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.OpTkn = f.newToken(token.T_BOOLEAN_OR, []byte("||")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Right.Accept(f) +} + +func (f *formatter) ExprBinaryCoalesce(n *ast.ExprBinaryCoalesce) { + n.Left.Accept(f) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.OpTkn = f.newToken(token.T_COALESCE, []byte("??")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Right.Accept(f) +} + +func (f *formatter) ExprBinaryConcat(n *ast.ExprBinaryConcat) { + n.Left.Accept(f) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.OpTkn = f.newToken('.', []byte(".")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Right.Accept(f) +} + +func (f *formatter) ExprBinaryDiv(n *ast.ExprBinaryDiv) { + n.Left.Accept(f) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.OpTkn = f.newToken('/', []byte("/")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Right.Accept(f) +} + +func (f *formatter) ExprBinaryEqual(n *ast.ExprBinaryEqual) { + n.Left.Accept(f) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.OpTkn = f.newToken(token.T_IS_EQUAL, []byte("==")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Right.Accept(f) +} + +func (f *formatter) ExprBinaryGreater(n *ast.ExprBinaryGreater) { + n.Left.Accept(f) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.OpTkn = f.newToken('>', []byte(">")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Right.Accept(f) +} + +func (f *formatter) ExprBinaryGreaterOrEqual(n *ast.ExprBinaryGreaterOrEqual) { + n.Left.Accept(f) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.OpTkn = f.newToken(token.T_IS_GREATER_OR_EQUAL, []byte(">=")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Right.Accept(f) +} + +func (f *formatter) ExprBinaryIdentical(n *ast.ExprBinaryIdentical) { + n.Left.Accept(f) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.OpTkn = f.newToken(token.T_IS_IDENTICAL, []byte("===")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Right.Accept(f) +} + +func (f *formatter) ExprBinaryLogicalAnd(n *ast.ExprBinaryLogicalAnd) { + n.Left.Accept(f) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.OpTkn = f.newToken(token.T_LOGICAL_AND, []byte("and")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Right.Accept(f) +} + +func (f *formatter) ExprBinaryLogicalOr(n *ast.ExprBinaryLogicalOr) { + n.Left.Accept(f) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.OpTkn = f.newToken(token.T_LOGICAL_OR, []byte("or")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Right.Accept(f) +} + +func (f *formatter) ExprBinaryLogicalXor(n *ast.ExprBinaryLogicalXor) { + n.Left.Accept(f) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.OpTkn = f.newToken(token.T_LOGICAL_XOR, []byte("xor")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Right.Accept(f) +} + +func (f *formatter) ExprBinaryMinus(n *ast.ExprBinaryMinus) { + n.Left.Accept(f) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.OpTkn = f.newToken('-', []byte("-")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Right.Accept(f) +} + +func (f *formatter) ExprBinaryMod(n *ast.ExprBinaryMod) { + n.Left.Accept(f) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.OpTkn = f.newToken('%', []byte("%")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Right.Accept(f) +} + +func (f *formatter) ExprBinaryMul(n *ast.ExprBinaryMul) { + n.Left.Accept(f) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.OpTkn = f.newToken('*', []byte("*")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Right.Accept(f) +} + +func (f *formatter) ExprBinaryNotEqual(n *ast.ExprBinaryNotEqual) { + n.Left.Accept(f) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.OpTkn = f.newToken(token.T_IS_NOT_EQUAL, []byte("!=")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Right.Accept(f) +} + +func (f *formatter) ExprBinaryNotIdentical(n *ast.ExprBinaryNotIdentical) { + n.Left.Accept(f) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.OpTkn = f.newToken(token.T_IS_NOT_IDENTICAL, []byte("!==")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Right.Accept(f) +} + +func (f *formatter) ExprBinaryPlus(n *ast.ExprBinaryPlus) { + n.Left.Accept(f) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.OpTkn = f.newToken('+', []byte("+")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Right.Accept(f) +} + +func (f *formatter) ExprBinaryPow(n *ast.ExprBinaryPow) { + n.Left.Accept(f) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.OpTkn = f.newToken(token.T_POW, []byte("**")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Right.Accept(f) +} + +func (f *formatter) ExprBinaryShiftLeft(n *ast.ExprBinaryShiftLeft) { + n.Left.Accept(f) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.OpTkn = f.newToken(token.T_SL, []byte("<<")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Right.Accept(f) +} + +func (f *formatter) ExprBinaryShiftRight(n *ast.ExprBinaryShiftRight) { + n.Left.Accept(f) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.OpTkn = f.newToken(token.T_SR, []byte(">>")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Right.Accept(f) +} + +func (f *formatter) ExprBinarySmaller(n *ast.ExprBinarySmaller) { + n.Left.Accept(f) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.OpTkn = f.newToken('<', []byte("<")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Right.Accept(f) +} + +func (f *formatter) ExprBinarySmallerOrEqual(n *ast.ExprBinarySmallerOrEqual) { + n.Left.Accept(f) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.OpTkn = f.newToken(token.T_IS_SMALLER_OR_EQUAL, []byte("<=")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Right.Accept(f) +} + +func (f *formatter) ExprBinarySpaceship(n *ast.ExprBinarySpaceship) { + n.Left.Accept(f) + + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + n.OpTkn = f.newToken(token.T_SPACESHIP, []byte("<=>")) + f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) + + n.Right.Accept(f) +} + +func (f *formatter) ExprCastArray(n *ast.ExprCastArray) { + n.CastTkn = f.newToken(token.T_ARRAY_CAST, []byte("(array)")) + n.Expr.Accept(f) +} + +func (f *formatter) ExprCastBool(n *ast.ExprCastBool) { + n.CastTkn = f.newToken(token.T_BOOL_CAST, []byte("(bool)")) + n.Expr.Accept(f) +} + +func (f *formatter) ExprCastDouble(n *ast.ExprCastDouble) { + n.CastTkn = f.newToken(token.T_DOUBLE_CAST, []byte("(float)")) + n.Expr.Accept(f) +} + +func (f *formatter) ExprCastInt(n *ast.ExprCastInt) { + n.CastTkn = f.newToken(token.T_INT_CAST, []byte("(integer)")) + n.Expr.Accept(f) +} + +func (f *formatter) ExprCastObject(n *ast.ExprCastObject) { + n.CastTkn = f.newToken(token.T_OBJECT_CAST, []byte("(object)")) + n.Expr.Accept(f) +} + +func (f *formatter) ExprCastString(n *ast.ExprCastString) { + n.CastTkn = f.newToken(token.T_STRING_CAST, []byte("(string)")) + n.Expr.Accept(f) +} + +func (f *formatter) ExprCastUnset(n *ast.ExprCastUnset) { + n.CastTkn = f.newToken(token.T_UNSET_CAST, []byte("(unset)")) + n.Expr.Accept(f) +} + +func (f *formatter) ScalarDnumber(n *ast.ScalarDnumber) { + if n.NumberTkn == nil { + n.NumberTkn = f.newToken(token.T_STRING, n.Value) + } else { + n.NumberTkn.FreeFloating = f.getFreeFloating() + } +} + +func (f *formatter) ScalarEncapsed(n *ast.ScalarEncapsed) { + n.OpenQuoteTkn = f.newToken('"', []byte("\"")) + for _, p := range n.Parts { + p.Accept(f) + } + n.CloseQuoteTkn = f.newToken('"', []byte("\"")) +} + +func (f *formatter) ScalarEncapsedStringPart(n *ast.ScalarEncapsedStringPart) { + if n.EncapsedStrTkn == nil { + n.EncapsedStrTkn = f.newToken(token.T_STRING, n.Value) + } else { + n.EncapsedStrTkn.FreeFloating = f.getFreeFloating() + } +} + +func (f *formatter) ScalarHeredoc(n *ast.ScalarHeredoc) { + n.OpenHeredocTkn = f.newToken(token.T_START_HEREDOC, []byte("<< $val) { + ; + }` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_StmtFunction(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.StmtFunction{ + FunctionName: &ast.Identifier{ + Value: []byte("foo"), + }, + Stmts: []ast.Vertex{ + &ast.StmtNop{}, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `function foo() { + ; + }` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_StmtFunction_Ref(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.StmtFunction{ + AmpersandTkn: &token.Token{}, + FunctionName: &ast.Identifier{ + Value: []byte("foo"), + }, + Stmts: []ast.Vertex{ + &ast.StmtNop{}, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `function &foo() { + ; + }` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_StmtFunction_Params(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.StmtFunction{ + FunctionName: &ast.Identifier{ + Value: []byte("foo"), + }, + Params: []ast.Vertex{ + &ast.Parameter{ + Var: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$a"), + }, + }, + }, + &ast.Parameter{ + Var: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$b"), + }, + }, + }, + }, + Stmts: []ast.Vertex{ + &ast.StmtNop{}, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `function foo($a, $b) { + ; + }` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_StmtFunction_ReturnType(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.StmtFunction{ + FunctionName: &ast.Identifier{ + Value: []byte("foo"), + }, + ReturnType: &ast.NameName{ + Parts: []ast.Vertex{ + &ast.NameNamePart{ + Value: []byte("bar"), + }, + }, + }, + Stmts: []ast.Vertex{ + &ast.StmtNop{}, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `function foo(): bar { + ; + }` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_StmtGlobal(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.StmtGlobal{ + Vars: []ast.Vertex{ + &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$a"), + }, + }, + &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$b"), + }, + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `global $a, $b;` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_StmtGoto(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.StmtGoto{ + Label: &ast.Identifier{ + Value: []byte("FOO"), + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `goto FOO;` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_StmtHaltCompiler(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.StmtHaltCompiler{} + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `__halt_compiler();` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_StmtIf(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.StmtIf{ + Cond: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Stmt: &ast.StmtStmtList{ + Stmts: []ast.Vertex{ + &ast.StmtNop{}, + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `if ($foo) { + ; + }` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_StmtIf_ElseIf(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.StmtIf{ + Cond: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Stmt: &ast.StmtStmtList{ + Stmts: []ast.Vertex{ + &ast.StmtNop{}, + }, + }, + ElseIf: []ast.Vertex{ + &ast.StmtElseIf{ + Cond: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + Stmt: &ast.StmtStmtList{ + Stmts: []ast.Vertex{ + &ast.StmtNop{}, + }, + }, + }, + &ast.StmtElseIf{ + Cond: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$baz"), + }, + }, + Stmt: &ast.StmtStmtList{ + Stmts: []ast.Vertex{ + &ast.StmtNop{}, + }, + }, + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `if ($foo) { + ; + } elseif($bar) { + ; + } elseif($baz) { + ; + }` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_StmtIf_Else(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.StmtIf{ + Cond: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Stmt: &ast.StmtStmtList{ + Stmts: []ast.Vertex{ + &ast.StmtNop{}, + }, + }, + Else: &ast.StmtElse{ + Stmt: &ast.StmtStmtList{ + Stmts: []ast.Vertex{ + &ast.StmtNop{}, + }, + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `if ($foo) { + ; + } else { + ; + }` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_StmtInlineHtml(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.Root{ + Stmts: []ast.Vertex{ + &ast.StmtStmtList{ + Stmts: []ast.Vertex{ + &ast.StmtNop{}, + &ast.StmtInlineHtml{ + Value: []byte("
"), + }, + &ast.StmtEcho{ + Exprs: []ast.Vertex{ + &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + }, + }, + &ast.StmtInlineHtml{ + Value: []byte("
"), + }, + &ast.StmtNop{}, + }, + }, + }, + } + + f := visitor.NewFormatter() + n.Accept(f) + + p := visitor.NewPrinter(o) + n.Accept(p) + + expected := `
$bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprArrayItem_Variadic(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprArrayItem{ + EllipsisTkn: &token.Token{}, + Val: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `...$foo` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprArrowFunction(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprArrowFunction{ + Expr: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `fn() => $foo` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprArrowFunction_Ref(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprArrowFunction{ + AmpersandTkn: &token.Token{}, + Expr: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `fn&() => $foo` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprArrowFunction_Params(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprArrowFunction{ + Params: []ast.Vertex{ + &ast.Parameter{ + Var: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$a"), + }, + }, + }, + &ast.Parameter{ + Var: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$b"), + }, + }, + }, + }, + Expr: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `fn($a, $b) => $foo` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprArrowFunction_ReturnType(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprArrowFunction{ + ReturnType: &ast.NameName{ + Parts: []ast.Vertex{ + &ast.NameNamePart{ + Value: []byte("foo"), + }, + }, + }, + Expr: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `fn(): foo => $bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprBitwiseNot(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprBitwiseNot{ + Expr: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `~$foo` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprBooleanNot(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprBooleanNot{ + Expr: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `!$foo` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprClassConstFetch(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprClassConstFetch{ + Class: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + ConstantName: &ast.Identifier{ + Value: []byte("bar"), + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo::bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprClone(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprClone{ + Expr: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `clone $foo` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprClosure(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprClosure{ + Stmts: []ast.Vertex{ + &ast.StmtNop{}, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `function() { + ; + }` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprClosure_Ref(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprClosure{ + AmpersandTkn: &token.Token{}, + Stmts: []ast.Vertex{ + &ast.StmtNop{}, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `function&() { + ; + }` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprClosure_Params(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprClosure{ + Params: []ast.Vertex{ + &ast.Parameter{ + Var: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$a"), + }, + }, + }, + &ast.Parameter{ + Var: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$b"), + }, + }, + }, + }, + Stmts: []ast.Vertex{ + &ast.StmtNop{}, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `function($a, $b) { + ; + }` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprClosure_ReturnType(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprClosure{ + ReturnType: &ast.NameName{ + Parts: []ast.Vertex{ + &ast.NameNamePart{ + Value: []byte("foo"), + }, + }, + }, + Stmts: []ast.Vertex{ + &ast.StmtNop{}, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `function(): foo { + ; + }` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprClosure_Use(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprClosure{ + ClosureUse: &ast.ExprClosureUse{ + Uses: []ast.Vertex{ + &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + }, + }, + Stmts: []ast.Vertex{ + &ast.StmtNop{}, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `function() use($foo) { + ; + }` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprClosureUse(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprClosureUse{ + Uses: []ast.Vertex{ + &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$a"), + }, + }, + &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$b"), + }, + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `use($a, $b)` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprConstFetch(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprConstFetch{ + Const: &ast.NameName{ + Parts: []ast.Vertex{ + &ast.NameNamePart{ + Value: []byte("FOO"), + }, + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `FOO` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprEmpty(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprEmpty{ + Expr: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `empty($foo)` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprErrorSuppress(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprErrorSuppress{ + Expr: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `@$foo` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprEval(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprEval{ + Expr: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `eval($foo)` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprExit(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprExit{} + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `exit` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprExit_Expr(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprExit{ + Expr: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `exit($foo)` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprFunctionCall(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprFunctionCall{ + Function: &ast.NameName{ + Parts: []ast.Vertex{ + &ast.NameNamePart{ + Value: []byte("foo"), + }, + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `foo()` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprFunctionCall_Arguments(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprFunctionCall{ + Function: &ast.NameName{ + Parts: []ast.Vertex{ + &ast.NameNamePart{ + Value: []byte("foo"), + }, + }, + }, + Arguments: []ast.Vertex{ + &ast.Argument{ + Expr: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `foo($bar)` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprInclude(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprInclude{ + Expr: &ast.ScalarString{ + Value: []byte("'foo.php'"), + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `include 'foo.php'` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprIncludeOnce(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprIncludeOnce{ + Expr: &ast.ScalarString{ + Value: []byte("'foo.php'"), + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `include_once 'foo.php'` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprInstanceOf(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprInstanceOf{ + Expr: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Class: &ast.NameName{ + Parts: []ast.Vertex{ + &ast.NameNamePart{ + Value: []byte("bar"), + }, + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo instanceof bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprIsset(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprIsset{ + Vars: []ast.Vertex{ + &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$a"), + }, + }, + &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$b"), + }, + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `isset($a, $b)` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprList(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprList{ + Items: []ast.Vertex{ + &ast.ExprArrayItem{ + Val: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$a"), + }, + }, + }, + &ast.ExprArrayItem{ + Val: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$b"), + }, + }, + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `list($a, $b)` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprMethodCall(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprMethodCall{ + Var: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Method: &ast.Identifier{ + Value: []byte("bar"), + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo->bar()` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprMethodCall_Arguments(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprMethodCall{ + Var: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Method: &ast.Identifier{ + Value: []byte("bar"), + }, + Arguments: []ast.Vertex{ + &ast.Argument{ + Expr: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$a"), + }, + }, + }, + &ast.Argument{ + Expr: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$b"), + }, + }, + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo->bar($a, $b)` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprNew(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprNew{ + Class: &ast.NameName{ + Parts: []ast.Vertex{ + &ast.NameNamePart{ + Value: []byte("foo"), + }, + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `new foo` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprNew_Arguments(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprNew{ + Class: &ast.NameName{ + Parts: []ast.Vertex{ + &ast.NameNamePart{ + Value: []byte("foo"), + }, + }, + }, + Arguments: []ast.Vertex{ + &ast.Argument{ + Expr: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$a"), + }, + }, + }, + &ast.Argument{ + Expr: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$b"), + }, + }, + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `new foo($a, $b)` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprPreDec(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprPreDec{ + Var: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `--$foo` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprPreInc(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprPreInc{ + Var: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `++$foo` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprPostDec(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprPostDec{ + Var: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo--` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprPostInc(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprPostInc{ + Var: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo++` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprPrint(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprPrint{ + Expr: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `print $foo` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprPropertyFetch(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprPropertyFetch{ + Var: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Property: &ast.Identifier{ + Value: []byte("bar"), + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo->bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprReference(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprReference{ + Var: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `&$foo` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprRequire(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprRequire{ + Expr: &ast.ScalarString{ + Value: []byte("'foo.php'"), + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `require 'foo.php'` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprRequireOnce(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprRequireOnce{ + Expr: &ast.ScalarString{ + Value: []byte("'foo.php'"), + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `require_once 'foo.php'` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprShellExec(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprShellExec{} + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := "``" + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprShellExec_Part(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprShellExec{ + Parts: []ast.Vertex{ + &ast.ScalarEncapsedStringPart{ + Value: []byte("foo"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := "`foo`" + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprShellExec_Parts(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprShellExec{ + Parts: []ast.Vertex{ + &ast.ScalarEncapsedStringPart{ + Value: []byte("foo "), + }, + &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + &ast.ScalarEncapsedStringPart{ + Value: []byte(" baz"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := "`foo $bar baz`" + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprStaticCall(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprStaticCall{ + Class: &ast.NameName{ + Parts: []ast.Vertex{ + &ast.NameNamePart{ + Value: []byte("foo"), + }, + }, + }, + Call: &ast.Identifier{ + Value: []byte("bar"), + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `foo::bar()` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprStaticCall_Arguments(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprStaticCall{ + Class: &ast.NameName{ + Parts: []ast.Vertex{ + &ast.NameNamePart{ + Value: []byte("foo"), + }, + }, + }, + Call: &ast.Identifier{ + Value: []byte("bar"), + }, + Arguments: []ast.Vertex{ + &ast.Argument{ + Expr: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$a"), + }, + }, + }, + &ast.Argument{ + Expr: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$b"), + }, + }, + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `foo::bar($a, $b)` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprStaticPropertyFetch(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprStaticPropertyFetch{ + Class: &ast.NameName{ + Parts: []ast.Vertex{ + &ast.NameNamePart{ + Value: []byte("foo"), + }, + }, + }, + Property: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `foo::$bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprTernary(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprTernary{ + Condition: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + IfTrue: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + IfFalse: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$baz"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo ? $bar : $baz` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprTernary_short(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprTernary{ + Condition: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + IfFalse: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo ?: $bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprUnaryMinus(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprUnaryMinus{ + Expr: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `-$foo` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprUnaryPlus(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprUnaryPlus{ + Expr: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `+$foo` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprYield(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprYield{ + Value: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `yield $foo` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprYield_Key(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprYield{ + Key: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Value: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `yield $foo => $bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprYieldFrom(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprYieldFrom{ + Expr: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `yield from $foo` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprAssign(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprAssign{ + Var: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Expr: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo = $bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprAssignReference(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprAssignReference{ + Var: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Expr: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo =& $bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprAssignBitwiseAnd(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprAssignBitwiseAnd{ + Var: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Expr: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo &= $bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprAssignBitwiseOr(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprAssignBitwiseOr{ + Var: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Expr: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo |= $bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprAssignBitwiseXor(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprAssignBitwiseXor{ + Var: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Expr: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo ^= $bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprAssignCoalesce(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprAssignCoalesce{ + Var: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Expr: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo ??= $bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprAssignConcat(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprAssignConcat{ + Var: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Expr: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo .= $bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprAssignDiv(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprAssignDiv{ + Var: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Expr: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo /= $bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprAssignMinus(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprAssignMinus{ + Var: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Expr: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo -= $bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprAssignMod(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprAssignMod{ + Var: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Expr: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo %= $bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprAssignMul(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprAssignMul{ + Var: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Expr: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo *= $bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprAssignPlus(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprAssignPlus{ + Var: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Expr: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo += $bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprAssignPow(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprAssignPow{ + Var: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Expr: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo **= $bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprAssignShiftLeft(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprAssignShiftLeft{ + Var: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Expr: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo <<= $bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprAssignShiftRight(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprAssignShiftRight{ + Var: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Expr: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo >>= $bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprBinaryBitwiseAnd(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprBinaryBitwiseAnd{ + Left: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Right: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo & $bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprBinaryBitwiseOr(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprBinaryBitwiseOr{ + Left: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Right: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo | $bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprBinaryBitwiseXor(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprBinaryBitwiseXor{ + Left: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Right: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo ^ $bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprBinaryBooleanAnd(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprBinaryBooleanAnd{ + Left: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Right: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo && $bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprBinaryBooleanOr(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprBinaryBooleanOr{ + Left: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Right: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo || $bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprBinaryCoalesce(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprBinaryCoalesce{ + Left: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Right: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo ?? $bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprBinaryConcat(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprBinaryConcat{ + Left: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Right: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo . $bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprBinaryDiv(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprBinaryDiv{ + Left: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Right: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo / $bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprBinaryEqual(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprBinaryEqual{ + Left: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Right: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo == $bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprBinaryGreater(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprBinaryGreater{ + Left: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Right: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo > $bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprBinaryGreaterOrEqual(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprBinaryGreaterOrEqual{ + Left: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Right: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo >= $bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprBinaryIdentical(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprBinaryIdentical{ + Left: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Right: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo === $bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprBinaryLogicalAnd(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprBinaryLogicalAnd{ + Left: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Right: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo and $bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprBinaryLogicalOr(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprBinaryLogicalOr{ + Left: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Right: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo or $bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprBinaryLogicalXor(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprBinaryLogicalXor{ + Left: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Right: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo xor $bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprBinaryMinus(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprBinaryMinus{ + Left: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Right: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo - $bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprBinaryMod(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprBinaryMod{ + Left: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Right: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo % $bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprBinaryMul(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprBinaryMul{ + Left: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Right: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo * $bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprBinaryNotEqual(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprBinaryNotEqual{ + Left: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Right: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo != $bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprBinaryNotIdentical(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprBinaryNotIdentical{ + Left: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Right: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo !== $bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprBinaryPlus(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprBinaryPlus{ + Left: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Right: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo + $bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprBinaryPow(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprBinaryPow{ + Left: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Right: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo ** $bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprBinaryShiftLeft(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprBinaryShiftLeft{ + Left: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Right: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo << $bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprBinaryShiftRight(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprBinaryShiftRight{ + Left: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Right: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo >> $bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprBinarySmaller(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprBinarySmaller{ + Left: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Right: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo < $bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprBinarySmallerOrEqual(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprBinarySmallerOrEqual{ + Left: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Right: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo <= $bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprBinarySpaceship(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprBinarySpaceship{ + Left: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + Right: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `$foo <=> $bar` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprCastArray(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprCastArray{ + Expr: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `(array)$foo` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprCastBool(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprCastBool{ + Expr: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `(bool)$foo` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprCastDouble(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprCastDouble{ + Expr: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `(float)$foo` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprCastInt(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprCastInt{ + Expr: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `(integer)$foo` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprCastObject(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprCastObject{ + Expr: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `(object)$foo` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprCastString(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprCastString{ + Expr: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `(string)$foo` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ExprCastUnset(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ExprCastUnset{ + Expr: &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$foo"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `(unset)$foo` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ScalarDnumber(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ScalarDnumber{ + Value: []byte("1234"), + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `1234` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ScalarEncapsed(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ScalarEncapsed{} + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `""` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ScalarEncapsed_Part(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ScalarEncapsed{ + Parts: []ast.Vertex{ + &ast.ScalarEncapsedStringPart{ + Value: []byte("foo"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `"foo"` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ScalarEncapsed_Parts(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ScalarEncapsed{ + Parts: []ast.Vertex{ + &ast.ScalarEncapsedStringPart{ + Value: []byte("foo "), + }, + &ast.ExprVariable{ + VarName: &ast.Identifier{ + Value: []byte("$bar"), + }, + }, + &ast.ScalarEncapsedStringPart{ + Value: []byte(" baz"), + }, + }, + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `"foo $bar baz"` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ScalarEncapsedStringPart(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ScalarEncapsedStringPart{ + Value: []byte("foo"), + } + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `foo` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestFormatter_ScalarHeredoc(t *testing.T) { + o := bytes.NewBufferString("") + + n := &ast.ScalarHeredoc{} + + f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1) + n.Accept(f) + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n.Accept(p) + + expected := `<< 0 { - io.WriteString(p.w, glue) - } - - p.Print(n) - } -} - -func (p *PrettyPrinter) printNodes(nn []ast.Vertex) { - p.indentDepth++ - l := len(nn) - 1 - for k, n := range nn { - p.printIndent() - p.Print(n) - if k < l { - io.WriteString(p.w, "\n") - } - } - p.indentDepth-- -} - -func (p *PrettyPrinter) printIndent() { - for i := 0; i < p.indentDepth; i++ { - io.WriteString(p.w, p.indentStr) - } -} - -func (p *PrettyPrinter) printNode(n ast.Vertex) { - switch n.(type) { - - // node - - case *ast.Root: - p.printNodeRoot(n) - case *ast.Identifier: - p.printNodeIdentifier(n) - case *ast.Parameter: - p.printNodeParameter(n) - case *ast.Nullable: - p.printNodeNullable(n) - case *ast.Argument: - p.printNodeArgument(n) - - // name - - case *ast.NameNamePart: - p.printNameNamePart(n) - case *ast.NameName: - p.printNameName(n) - case *ast.NameFullyQualified: - p.printNameFullyQualified(n) - case *ast.NameRelative: - p.printNameRelative(n) - - // scalar - - case *ast.ScalarLnumber: - p.printScalarLNumber(n) - case *ast.ScalarDnumber: - p.printScalarDNumber(n) - case *ast.ScalarString: - p.printScalarString(n) - case *ast.ScalarEncapsedStringPart: - p.printScalarEncapsedStringPart(n) - case *ast.ScalarEncapsed: - p.printScalarEncapsed(n) - case *ast.ScalarHeredoc: - p.printScalarHeredoc(n) - case *ast.ScalarMagicConstant: - p.printScalarMagicConstant(n) - - // assign - - case *ast.ExprAssign: - p.printAssignAssign(n) - case *ast.ExprAssignReference: - p.printAssignReference(n) - case *ast.ExprAssignBitwiseAnd: - p.printAssignBitwiseAnd(n) - case *ast.ExprAssignBitwiseOr: - p.printAssignBitwiseOr(n) - case *ast.ExprAssignBitwiseXor: - p.printAssignBitwiseXor(n) - case *ast.ExprAssignConcat: - p.printAssignConcat(n) - case *ast.ExprAssignDiv: - p.printAssignDiv(n) - case *ast.ExprAssignMinus: - p.printAssignMinus(n) - case *ast.ExprAssignMod: - p.printAssignMod(n) - case *ast.ExprAssignMul: - p.printAssignMul(n) - case *ast.ExprAssignPlus: - p.printAssignPlus(n) - case *ast.ExprAssignPow: - p.printAssignPow(n) - case *ast.ExprAssignShiftLeft: - p.printAssignShiftLeft(n) - case *ast.ExprAssignShiftRight: - p.printAssignShiftRight(n) - - // binary - - case *ast.ExprBinaryBitwiseAnd: - p.printBinaryBitwiseAnd(n) - case *ast.ExprBinaryBitwiseOr: - p.printBinaryBitwiseOr(n) - case *ast.ExprBinaryBitwiseXor: - p.printBinaryBitwiseXor(n) - case *ast.ExprBinaryBooleanAnd: - p.printBinaryBooleanAnd(n) - case *ast.ExprBinaryBooleanOr: - p.printBinaryBooleanOr(n) - case *ast.ExprBinaryCoalesce: - p.printBinaryCoalesce(n) - case *ast.ExprBinaryConcat: - p.printBinaryConcat(n) - case *ast.ExprBinaryDiv: - p.printBinaryDiv(n) - case *ast.ExprBinaryEqual: - p.printBinaryEqual(n) - case *ast.ExprBinaryGreaterOrEqual: - p.printBinaryGreaterOrEqual(n) - case *ast.ExprBinaryGreater: - p.printBinaryGreater(n) - case *ast.ExprBinaryIdentical: - p.printBinaryIdentical(n) - case *ast.ExprBinaryLogicalAnd: - p.printBinaryLogicalAnd(n) - case *ast.ExprBinaryLogicalOr: - p.printBinaryLogicalOr(n) - case *ast.ExprBinaryLogicalXor: - p.printBinaryLogicalXor(n) - case *ast.ExprBinaryMinus: - p.printBinaryMinus(n) - case *ast.ExprBinaryMod: - p.printBinaryMod(n) - case *ast.ExprBinaryMul: - p.printBinaryMul(n) - case *ast.ExprBinaryNotEqual: - p.printBinaryNotEqual(n) - case *ast.ExprBinaryNotIdentical: - p.printBinaryNotIdentical(n) - case *ast.ExprBinaryPlus: - p.printBinaryPlus(n) - case *ast.ExprBinaryPow: - p.printBinaryPow(n) - case *ast.ExprBinaryShiftLeft: - p.printBinaryShiftLeft(n) - case *ast.ExprBinaryShiftRight: - p.printBinaryShiftRight(n) - case *ast.ExprBinarySmallerOrEqual: - p.printBinarySmallerOrEqual(n) - case *ast.ExprBinarySmaller: - p.printBinarySmaller(n) - case *ast.ExprBinarySpaceship: - p.printBinarySpaceship(n) - - // cast - - case *ast.ExprCastArray: - p.printArray(n) - case *ast.ExprCastBool: - p.printBool(n) - case *ast.ExprCastDouble: - p.printDouble(n) - case *ast.ExprCastInt: - p.printInt(n) - case *ast.ExprCastObject: - p.printObject(n) - case *ast.ExprCastString: - p.printString(n) - case *ast.ExprCastUnset: - p.printUnset(n) - - // expr - - case *ast.ExprArrayDimFetch: - p.printExprArrayDimFetch(n) - case *ast.ExprArrayItem: - p.printExprArrayItem(n) - case *ast.ExprArray: - p.printExprArray(n) - case *ast.ExprBitwiseNot: - p.printExprBitwiseNot(n) - case *ast.ExprBooleanNot: - p.printExprBooleanNot(n) - case *ast.ExprClassConstFetch: - p.printExprClassConstFetch(n) - case *ast.ExprClone: - p.printExprClone(n) - case *ast.ExprClosureUse: - p.printExprClosureUse(n) - case *ast.ExprClosure: - p.printExprClosure(n) - case *ast.ExprConstFetch: - p.printExprConstFetch(n) - case *ast.ExprEmpty: - p.printExprEmpty(n) - case *ast.ExprErrorSuppress: - p.printExprErrorSuppress(n) - case *ast.ExprEval: - p.printExprEval(n) - case *ast.ExprExit: - p.printExprExit(n) - case *ast.ExprFunctionCall: - p.printExprFunctionCall(n) - case *ast.ExprInclude: - p.printExprInclude(n) - case *ast.ExprIncludeOnce: - p.printExprIncludeOnce(n) - case *ast.ExprInstanceOf: - p.printExprInstanceOf(n) - case *ast.ExprIsset: - p.printExprIsset(n) - case *ast.ExprList: - p.printExprList(n) - case *ast.ExprMethodCall: - p.printExprMethodCall(n) - case *ast.ExprNew: - p.printExprNew(n) - case *ast.ExprPostDec: - p.printExprPostDec(n) - case *ast.ExprPostInc: - p.printExprPostInc(n) - case *ast.ExprPreDec: - p.printExprPreDec(n) - case *ast.ExprPreInc: - p.printExprPreInc(n) - case *ast.ExprPrint: - p.printExprPrint(n) - case *ast.ExprPropertyFetch: - p.printExprPropertyFetch(n) - case *ast.ExprReference: - p.printExprReference(n) - case *ast.ExprRequire: - p.printExprRequire(n) - case *ast.ExprRequireOnce: - p.printExprRequireOnce(n) - case *ast.ExprShellExec: - p.printExprShellExec(n) - case *ast.ExprStaticCall: - p.printExprStaticCall(n) - case *ast.ExprStaticPropertyFetch: - p.printExprStaticPropertyFetch(n) - case *ast.ExprTernary: - p.printExprTernary(n) - case *ast.ExprUnaryMinus: - p.printExprUnaryMinus(n) - case *ast.ExprUnaryPlus: - p.printExprUnaryPlus(n) - case *ast.ExprVariable: - p.printExprVariable(n) - case *ast.ExprYieldFrom: - p.printExprYieldFrom(n) - case *ast.ExprYield: - p.printExprYield(n) - - // stmt - - case *ast.StmtBreak: - p.printStmtBreak(n) - case *ast.StmtCase: - p.printStmtCase(n) - case *ast.StmtCatch: - p.printStmtCatch(n) - case *ast.StmtClassMethod: - p.printStmtClassMethod(n) - case *ast.StmtClass: - p.printStmtClass(n) - case *ast.StmtClassConstList: - p.printStmtClassConstList(n) - case *ast.StmtConstant: - p.printStmtConstant(n) - case *ast.StmtContinue: - p.printStmtContinue(n) - case *ast.StmtDeclare: - p.printStmtDeclare(n) - case *ast.StmtDefault: - p.printStmtDefault(n) - case *ast.StmtDo: - p.printStmtDo(n) - case *ast.StmtEcho: - p.printStmtEcho(n) - case *ast.StmtElseIf: - p.printStmtElseif(n) - case *ast.StmtElse: - p.printStmtElse(n) - case *ast.StmtExpression: - p.printStmtExpression(n) - case *ast.StmtFinally: - p.printStmtFinally(n) - case *ast.StmtFor: - p.printStmtFor(n) - case *ast.StmtForeach: - p.printStmtForeach(n) - case *ast.StmtFunction: - p.printStmtFunction(n) - case *ast.StmtGlobal: - p.printStmtGlobal(n) - case *ast.StmtGoto: - p.printStmtGoto(n) - case *ast.StmtHaltCompiler: - p.printStmtHaltCompiler(n) - case *ast.StmtIf: - p.printStmtIf(n) - case *ast.StmtInlineHtml: - p.printStmtInlineHTML(n) - case *ast.StmtInterface: - p.printStmtInterface(n) - case *ast.StmtLabel: - p.printStmtLabel(n) - case *ast.StmtNamespace: - p.printStmtNamespace(n) - case *ast.StmtNop: - p.printStmtNop(n) - case *ast.StmtPropertyList: - p.printStmtPropertyList(n) - case *ast.StmtProperty: - p.printStmtProperty(n) - case *ast.StmtReturn: - p.printStmtReturn(n) - case *ast.StmtStaticVar: - p.printStmtStaticVar(n) - case *ast.StmtStatic: - p.printStmtStatic(n) - case *ast.StmtStmtList: - p.printStmtStmtList(n) - case *ast.StmtSwitch: - p.printStmtSwitch(n) - case *ast.StmtThrow: - p.printStmtThrow(n) - case *ast.StmtTraitMethodRef: - p.printStmtTraitMethodRef(n) - case *ast.StmtTraitUseAlias: - p.printStmtTraitUseAlias(n) - case *ast.StmtTraitUsePrecedence: - p.printStmtTraitUsePrecedence(n) - case *ast.StmtTraitUse: - p.printStmtTraitUse(n) - case *ast.StmtTrait: - p.printStmtTrait(n) - case *ast.StmtTry: - p.printStmtTry(n) - case *ast.StmtUnset: - p.printStmtUnset(n) - case *ast.StmtUse: - p.printStmtUse(n) - case *ast.StmtGroupUse: - p.printStmtGroupUse(n) - case *ast.StmtUseDeclaration: - p.printStmtUseDeclaration(n) - case *ast.StmtWhile: - p.printStmtWhile(n) - } -} - -// node - -func (p *PrettyPrinter) printNodeRoot(n ast.Vertex) { - var stmts []ast.Vertex - v := n.(*ast.Root) - - if len(v.Stmts) > 0 { - firstStmt := v.Stmts[0] - stmts = v.Stmts[1:] - - switch fs := firstStmt.(type) { - case *ast.StmtInlineHtml: - io.WriteString(p.w, string(fs.Value)) - io.WriteString(p.w, " 0 { - io.WriteString(p.w, "\\") - } - - p.Print(part) - } -} - -func (p *PrettyPrinter) printNameFullyQualified(n ast.Vertex) { - nn := n.(*ast.NameFullyQualified) - - for _, part := range nn.Parts { - io.WriteString(p.w, "\\") - p.Print(part) - } -} - -func (p *PrettyPrinter) printNameRelative(n ast.Vertex) { - nn := n.(*ast.NameRelative) - - io.WriteString(p.w, "namespace") - for _, part := range nn.Parts { - io.WriteString(p.w, "\\") - p.Print(part) - } -} - -// scalar - -func (p *PrettyPrinter) printScalarLNumber(n ast.Vertex) { - v := string(n.(*ast.ScalarLnumber).Value) - io.WriteString(p.w, v) -} - -func (p *PrettyPrinter) printScalarDNumber(n ast.Vertex) { - v := string(n.(*ast.ScalarDnumber).Value) - io.WriteString(p.w, v) -} - -func (p *PrettyPrinter) printScalarString(n ast.Vertex) { - v := string(n.(*ast.ScalarString).Value) - - io.WriteString(p.w, v) -} - -func (p *PrettyPrinter) printScalarEncapsedStringPart(n ast.Vertex) { - v := string(n.(*ast.ScalarEncapsedStringPart).Value) - io.WriteString(p.w, v) -} - -func (p *PrettyPrinter) printScalarEncapsed(n ast.Vertex) { - nn := n.(*ast.ScalarEncapsed) - io.WriteString(p.w, "\"") - - for _, part := range nn.Parts { - switch part.(type) { - case *ast.ScalarEncapsedStringPart: - p.Print(part) - default: - io.WriteString(p.w, "{") - p.Print(part) - io.WriteString(p.w, "}") - } - } - - io.WriteString(p.w, "\"") -} - -func (p *PrettyPrinter) printScalarHeredoc(n ast.Vertex) { - nn := n.(*ast.ScalarHeredoc) - - io.WriteString(p.w, string(nn.OpenHeredocTkn.Value)) - - for _, part := range nn.Parts { - switch part.(type) { - case *ast.ScalarEncapsedStringPart: - p.Print(part) - default: - io.WriteString(p.w, "{") - p.Print(part) - io.WriteString(p.w, "}") - } - } - - io.WriteString(p.w, strings.Trim(string(nn.OpenHeredocTkn.Value), "<\"'\n")) -} - -func (p *PrettyPrinter) printScalarMagicConstant(n ast.Vertex) { - v := string(n.(*ast.ScalarMagicConstant).Value) - io.WriteString(p.w, v) -} - -// Assign - -func (p *PrettyPrinter) printAssignAssign(n ast.Vertex) { - nn := n.(*ast.ExprAssign) - p.Print(nn.Var) - io.WriteString(p.w, " = ") - p.Print(nn.Expr) -} - -func (p *PrettyPrinter) printAssignReference(n ast.Vertex) { - nn := n.(*ast.ExprAssignReference) - p.Print(nn.Var) - io.WriteString(p.w, " =& ") - p.Print(nn.Expr) -} - -func (p *PrettyPrinter) printAssignBitwiseAnd(n ast.Vertex) { - nn := n.(*ast.ExprAssignBitwiseAnd) - p.Print(nn.Var) - io.WriteString(p.w, " &= ") - p.Print(nn.Expr) -} - -func (p *PrettyPrinter) printAssignBitwiseOr(n ast.Vertex) { - nn := n.(*ast.ExprAssignBitwiseOr) - p.Print(nn.Var) - io.WriteString(p.w, " |= ") - p.Print(nn.Expr) -} - -func (p *PrettyPrinter) printAssignBitwiseXor(n ast.Vertex) { - nn := n.(*ast.ExprAssignBitwiseXor) - p.Print(nn.Var) - io.WriteString(p.w, " ^= ") - p.Print(nn.Expr) -} - -func (p *PrettyPrinter) printAssignConcat(n ast.Vertex) { - nn := n.(*ast.ExprAssignConcat) - p.Print(nn.Var) - io.WriteString(p.w, " .= ") - p.Print(nn.Expr) -} - -func (p *PrettyPrinter) printAssignDiv(n ast.Vertex) { - nn := n.(*ast.ExprAssignDiv) - p.Print(nn.Var) - io.WriteString(p.w, " /= ") - p.Print(nn.Expr) -} - -func (p *PrettyPrinter) printAssignMinus(n ast.Vertex) { - nn := n.(*ast.ExprAssignMinus) - p.Print(nn.Var) - io.WriteString(p.w, " -= ") - p.Print(nn.Expr) -} - -func (p *PrettyPrinter) printAssignMod(n ast.Vertex) { - nn := n.(*ast.ExprAssignMod) - p.Print(nn.Var) - io.WriteString(p.w, " %= ") - p.Print(nn.Expr) -} - -func (p *PrettyPrinter) printAssignMul(n ast.Vertex) { - nn := n.(*ast.ExprAssignMul) - p.Print(nn.Var) - io.WriteString(p.w, " *= ") - p.Print(nn.Expr) -} - -func (p *PrettyPrinter) printAssignPlus(n ast.Vertex) { - nn := n.(*ast.ExprAssignPlus) - p.Print(nn.Var) - io.WriteString(p.w, " += ") - p.Print(nn.Expr) -} - -func (p *PrettyPrinter) printAssignPow(n ast.Vertex) { - nn := n.(*ast.ExprAssignPow) - p.Print(nn.Var) - io.WriteString(p.w, " **= ") - p.Print(nn.Expr) -} - -func (p *PrettyPrinter) printAssignShiftLeft(n ast.Vertex) { - nn := n.(*ast.ExprAssignShiftLeft) - p.Print(nn.Var) - io.WriteString(p.w, " <<= ") - p.Print(nn.Expr) -} - -func (p *PrettyPrinter) printAssignShiftRight(n ast.Vertex) { - nn := n.(*ast.ExprAssignShiftRight) - p.Print(nn.Var) - io.WriteString(p.w, " >>= ") - p.Print(nn.Expr) -} - -// binary - -func (p *PrettyPrinter) printBinaryBitwiseAnd(n ast.Vertex) { - nn := n.(*ast.ExprBinaryBitwiseAnd) - - p.Print(nn.Left) - io.WriteString(p.w, " & ") - p.Print(nn.Right) -} - -func (p *PrettyPrinter) printBinaryBitwiseOr(n ast.Vertex) { - nn := n.(*ast.ExprBinaryBitwiseOr) - - p.Print(nn.Left) - io.WriteString(p.w, " | ") - p.Print(nn.Right) -} - -func (p *PrettyPrinter) printBinaryBitwiseXor(n ast.Vertex) { - nn := n.(*ast.ExprBinaryBitwiseXor) - - p.Print(nn.Left) - io.WriteString(p.w, " ^ ") - p.Print(nn.Right) -} - -func (p *PrettyPrinter) printBinaryBooleanAnd(n ast.Vertex) { - nn := n.(*ast.ExprBinaryBooleanAnd) - - p.Print(nn.Left) - io.WriteString(p.w, " && ") - p.Print(nn.Right) -} - -func (p *PrettyPrinter) printBinaryBooleanOr(n ast.Vertex) { - nn := n.(*ast.ExprBinaryBooleanOr) - - p.Print(nn.Left) - io.WriteString(p.w, " || ") - p.Print(nn.Right) -} - -func (p *PrettyPrinter) printBinaryCoalesce(n ast.Vertex) { - nn := n.(*ast.ExprBinaryCoalesce) - - p.Print(nn.Left) - io.WriteString(p.w, " ?? ") - p.Print(nn.Right) -} - -func (p *PrettyPrinter) printBinaryConcat(n ast.Vertex) { - nn := n.(*ast.ExprBinaryConcat) - - p.Print(nn.Left) - io.WriteString(p.w, " . ") - p.Print(nn.Right) -} - -func (p *PrettyPrinter) printBinaryDiv(n ast.Vertex) { - nn := n.(*ast.ExprBinaryDiv) - - p.Print(nn.Left) - io.WriteString(p.w, " / ") - p.Print(nn.Right) -} - -func (p *PrettyPrinter) printBinaryEqual(n ast.Vertex) { - nn := n.(*ast.ExprBinaryEqual) - - p.Print(nn.Left) - io.WriteString(p.w, " == ") - p.Print(nn.Right) -} - -func (p *PrettyPrinter) printBinaryGreaterOrEqual(n ast.Vertex) { - nn := n.(*ast.ExprBinaryGreaterOrEqual) - - p.Print(nn.Left) - io.WriteString(p.w, " >= ") - p.Print(nn.Right) -} - -func (p *PrettyPrinter) printBinaryGreater(n ast.Vertex) { - nn := n.(*ast.ExprBinaryGreater) - - p.Print(nn.Left) - io.WriteString(p.w, " > ") - p.Print(nn.Right) -} - -func (p *PrettyPrinter) printBinaryIdentical(n ast.Vertex) { - nn := n.(*ast.ExprBinaryIdentical) - - p.Print(nn.Left) - io.WriteString(p.w, " === ") - p.Print(nn.Right) -} - -func (p *PrettyPrinter) printBinaryLogicalAnd(n ast.Vertex) { - nn := n.(*ast.ExprBinaryLogicalAnd) - - p.Print(nn.Left) - io.WriteString(p.w, " and ") - p.Print(nn.Right) -} - -func (p *PrettyPrinter) printBinaryLogicalOr(n ast.Vertex) { - nn := n.(*ast.ExprBinaryLogicalOr) - - p.Print(nn.Left) - io.WriteString(p.w, " or ") - p.Print(nn.Right) -} - -func (p *PrettyPrinter) printBinaryLogicalXor(n ast.Vertex) { - nn := n.(*ast.ExprBinaryLogicalXor) - - p.Print(nn.Left) - io.WriteString(p.w, " xor ") - p.Print(nn.Right) -} - -func (p *PrettyPrinter) printBinaryMinus(n ast.Vertex) { - nn := n.(*ast.ExprBinaryMinus) - - p.Print(nn.Left) - io.WriteString(p.w, " - ") - p.Print(nn.Right) -} - -func (p *PrettyPrinter) printBinaryMod(n ast.Vertex) { - nn := n.(*ast.ExprBinaryMod) - - p.Print(nn.Left) - io.WriteString(p.w, " % ") - p.Print(nn.Right) -} - -func (p *PrettyPrinter) printBinaryMul(n ast.Vertex) { - nn := n.(*ast.ExprBinaryMul) - - p.Print(nn.Left) - io.WriteString(p.w, " * ") - p.Print(nn.Right) -} - -func (p *PrettyPrinter) printBinaryNotEqual(n ast.Vertex) { - nn := n.(*ast.ExprBinaryNotEqual) - - p.Print(nn.Left) - io.WriteString(p.w, " != ") - p.Print(nn.Right) -} - -func (p *PrettyPrinter) printBinaryNotIdentical(n ast.Vertex) { - nn := n.(*ast.ExprBinaryNotIdentical) - - p.Print(nn.Left) - io.WriteString(p.w, " !== ") - p.Print(nn.Right) -} - -func (p *PrettyPrinter) printBinaryPlus(n ast.Vertex) { - nn := n.(*ast.ExprBinaryPlus) - - p.Print(nn.Left) - io.WriteString(p.w, " + ") - p.Print(nn.Right) -} - -func (p *PrettyPrinter) printBinaryPow(n ast.Vertex) { - nn := n.(*ast.ExprBinaryPow) - - p.Print(nn.Left) - io.WriteString(p.w, " ** ") - p.Print(nn.Right) -} - -func (p *PrettyPrinter) printBinaryShiftLeft(n ast.Vertex) { - nn := n.(*ast.ExprBinaryShiftLeft) - - p.Print(nn.Left) - io.WriteString(p.w, " << ") - p.Print(nn.Right) -} - -func (p *PrettyPrinter) printBinaryShiftRight(n ast.Vertex) { - nn := n.(*ast.ExprBinaryShiftRight) - - p.Print(nn.Left) - io.WriteString(p.w, " >> ") - p.Print(nn.Right) -} - -func (p *PrettyPrinter) printBinarySmallerOrEqual(n ast.Vertex) { - nn := n.(*ast.ExprBinarySmallerOrEqual) - - p.Print(nn.Left) - io.WriteString(p.w, " <= ") - p.Print(nn.Right) -} - -func (p *PrettyPrinter) printBinarySmaller(n ast.Vertex) { - nn := n.(*ast.ExprBinarySmaller) - - p.Print(nn.Left) - io.WriteString(p.w, " < ") - p.Print(nn.Right) -} - -func (p *PrettyPrinter) printBinarySpaceship(n ast.Vertex) { - nn := n.(*ast.ExprBinarySpaceship) - - p.Print(nn.Left) - io.WriteString(p.w, " <=> ") - p.Print(nn.Right) -} - -// cast - -func (p *PrettyPrinter) printArray(n ast.Vertex) { - nn := n.(*ast.ExprCastArray) - - io.WriteString(p.w, "(array)") - p.Print(nn.Expr) -} - -func (p *PrettyPrinter) printBool(n ast.Vertex) { - nn := n.(*ast.ExprCastBool) - - io.WriteString(p.w, "(bool)") - p.Print(nn.Expr) -} - -func (p *PrettyPrinter) printDouble(n ast.Vertex) { - nn := n.(*ast.ExprCastDouble) - - io.WriteString(p.w, "(float)") - p.Print(nn.Expr) -} - -func (p *PrettyPrinter) printInt(n ast.Vertex) { - nn := n.(*ast.ExprCastInt) - - io.WriteString(p.w, "(int)") - p.Print(nn.Expr) -} - -func (p *PrettyPrinter) printObject(n ast.Vertex) { - nn := n.(*ast.ExprCastObject) - - io.WriteString(p.w, "(object)") - p.Print(nn.Expr) -} - -func (p *PrettyPrinter) printString(n ast.Vertex) { - nn := n.(*ast.ExprCastString) - - io.WriteString(p.w, "(string)") - p.Print(nn.Expr) -} - -func (p *PrettyPrinter) printUnset(n ast.Vertex) { - nn := n.(*ast.ExprCastUnset) - - io.WriteString(p.w, "(unset)") - p.Print(nn.Expr) -} - -// expr - -func (p *PrettyPrinter) printExprArrayDimFetch(n ast.Vertex) { - nn := n.(*ast.ExprArrayDimFetch) - p.Print(nn.Var) - io.WriteString(p.w, "[") - p.Print(nn.Dim) - io.WriteString(p.w, "]") -} - -func (p *PrettyPrinter) printExprArrayItem(n ast.Vertex) { - nn := n.(*ast.ExprArrayItem) - - if nn.Key != nil { - p.Print(nn.Key) - io.WriteString(p.w, " => ") - } - - p.Print(nn.Val) -} - -func (p *PrettyPrinter) printExprArray(n ast.Vertex) { - nn := n.(*ast.ExprArray) - - io.WriteString(p.w, "array(") - p.joinPrint(", ", nn.Items) - io.WriteString(p.w, ")") -} - -func (p *PrettyPrinter) printExprBitwiseNot(n ast.Vertex) { - nn := n.(*ast.ExprBitwiseNot) - io.WriteString(p.w, "~") - p.Print(nn.Expr) -} - -func (p *PrettyPrinter) printExprBooleanNot(n ast.Vertex) { - nn := n.(*ast.ExprBooleanNot) - io.WriteString(p.w, "!") - p.Print(nn.Expr) -} - -func (p *PrettyPrinter) printExprClassConstFetch(n ast.Vertex) { - nn := n.(*ast.ExprClassConstFetch) - - p.Print(nn.Class) - io.WriteString(p.w, "::") - io.WriteString(p.w, string(nn.ConstantName.(*ast.Identifier).Value)) -} - -func (p *PrettyPrinter) printExprClone(n ast.Vertex) { - nn := n.(*ast.ExprClone) - - io.WriteString(p.w, "clone ") - p.Print(nn.Expr) -} - -func (p *PrettyPrinter) printExprClosureUse(n ast.Vertex) { - nn := n.(*ast.ExprClosureUse) - - io.WriteString(p.w, "use (") - p.joinPrint(", ", nn.Uses) - io.WriteString(p.w, ")") -} - -func (p *PrettyPrinter) printExprClosure(n ast.Vertex) { - nn := n.(*ast.ExprClosure) - - if nn.StaticTkn != nil { - io.WriteString(p.w, "static ") - } - - io.WriteString(p.w, "function ") - - if nn.AmpersandTkn != nil { - io.WriteString(p.w, "&") - } - - io.WriteString(p.w, "(") - p.joinPrint(", ", nn.Params) - io.WriteString(p.w, ")") - - if nn.ClosureUse != nil { - io.WriteString(p.w, " ") - p.Print(nn.ClosureUse) - } - - if nn.ReturnType != nil { - io.WriteString(p.w, ": ") - p.Print(nn.ReturnType) - } - - io.WriteString(p.w, " {\n") - p.printNodes(nn.Stmts) - io.WriteString(p.w, "\n") - p.printIndent() - io.WriteString(p.w, "}") -} - -func (p *PrettyPrinter) printExprConstFetch(n ast.Vertex) { - nn := n.(*ast.ExprConstFetch) - - p.Print(nn.Const) -} - -func (p *PrettyPrinter) printExprEmpty(n ast.Vertex) { - nn := n.(*ast.ExprEmpty) - - io.WriteString(p.w, "empty(") - p.Print(nn.Expr) - io.WriteString(p.w, ")") -} - -func (p *PrettyPrinter) printExprErrorSuppress(n ast.Vertex) { - nn := n.(*ast.ExprErrorSuppress) - - io.WriteString(p.w, "@") - p.Print(nn.Expr) -} - -func (p *PrettyPrinter) printExprEval(n ast.Vertex) { - nn := n.(*ast.ExprEval) - - io.WriteString(p.w, "eval(") - p.Print(nn.Expr) - io.WriteString(p.w, ")") -} - -func (p *PrettyPrinter) printExprExit(n ast.Vertex) { - nn := n.(*ast.ExprExit) - - io.WriteString(p.w, "exit(") - p.Print(nn.Expr) - io.WriteString(p.w, ")") -} - -func (p *PrettyPrinter) printExprFunctionCall(n ast.Vertex) { - nn := n.(*ast.ExprFunctionCall) - - p.Print(nn.Function) - io.WriteString(p.w, "(") - p.joinPrint(", ", nn.Arguments) - io.WriteString(p.w, ")") -} - -func (p *PrettyPrinter) printExprInclude(n ast.Vertex) { - nn := n.(*ast.ExprInclude) - - io.WriteString(p.w, "include ") - p.Print(nn.Expr) -} - -func (p *PrettyPrinter) printExprIncludeOnce(n ast.Vertex) { - nn := n.(*ast.ExprIncludeOnce) - - io.WriteString(p.w, "include_once ") - p.Print(nn.Expr) -} - -func (p *PrettyPrinter) printExprInstanceOf(n ast.Vertex) { - nn := n.(*ast.ExprInstanceOf) - - p.Print(nn.Expr) - io.WriteString(p.w, " instanceof ") - p.Print(nn.Class) -} - -func (p *PrettyPrinter) printExprIsset(n ast.Vertex) { - nn := n.(*ast.ExprIsset) - - io.WriteString(p.w, "isset(") - p.joinPrint(", ", nn.Vars) - io.WriteString(p.w, ")") -} - -func (p *PrettyPrinter) printExprList(n ast.Vertex) { - nn := n.(*ast.ExprList) - - io.WriteString(p.w, "list(") - p.joinPrint(", ", nn.Items) - io.WriteString(p.w, ")") -} - -func (p *PrettyPrinter) printExprMethodCall(n ast.Vertex) { - nn := n.(*ast.ExprMethodCall) - - p.Print(nn.Var) - io.WriteString(p.w, "->") - p.Print(nn.Method) - io.WriteString(p.w, "(") - p.joinPrint(", ", nn.Arguments) - io.WriteString(p.w, ")") -} - -func (p *PrettyPrinter) printExprNew(n ast.Vertex) { - nn := n.(*ast.ExprNew) - - io.WriteString(p.w, "new ") - p.Print(nn.Class) - - if nn.Arguments != nil { - io.WriteString(p.w, "(") - p.joinPrint(", ", nn.Arguments) - io.WriteString(p.w, ")") - } -} - -func (p *PrettyPrinter) printExprPostDec(n ast.Vertex) { - nn := n.(*ast.ExprPostDec) - - p.Print(nn.Var) - io.WriteString(p.w, "--") -} - -func (p *PrettyPrinter) printExprPostInc(n ast.Vertex) { - nn := n.(*ast.ExprPostInc) - - p.Print(nn.Var) - io.WriteString(p.w, "++") -} - -func (p *PrettyPrinter) printExprPreDec(n ast.Vertex) { - nn := n.(*ast.ExprPreDec) - - io.WriteString(p.w, "--") - p.Print(nn.Var) -} - -func (p *PrettyPrinter) printExprPreInc(n ast.Vertex) { - nn := n.(*ast.ExprPreInc) - - io.WriteString(p.w, "++") - p.Print(nn.Var) -} - -func (p *PrettyPrinter) printExprPrint(n ast.Vertex) { - nn := n.(*ast.ExprPrint) - - io.WriteString(p.w, "print(") - p.Print(nn.Expr) - io.WriteString(p.w, ")") -} - -func (p *PrettyPrinter) printExprPropertyFetch(n ast.Vertex) { - nn := n.(*ast.ExprPropertyFetch) - - p.Print(nn.Var) - io.WriteString(p.w, "->") - p.Print(nn.Property) -} - -func (p *PrettyPrinter) printExprReference(n ast.Vertex) { - nn := n.(*ast.ExprReference) - - io.WriteString(p.w, "&") - p.Print(nn.Var) -} - -func (p *PrettyPrinter) printExprRequire(n ast.Vertex) { - nn := n.(*ast.ExprRequire) - - io.WriteString(p.w, "require ") - p.Print(nn.Expr) -} - -func (p *PrettyPrinter) printExprRequireOnce(n ast.Vertex) { - nn := n.(*ast.ExprRequireOnce) - - io.WriteString(p.w, "require_once ") - p.Print(nn.Expr) -} - -func (p *PrettyPrinter) printExprShellExec(n ast.Vertex) { - nn := n.(*ast.ExprShellExec) - - io.WriteString(p.w, "`") - for _, part := range nn.Parts { - switch part.(type) { - case *ast.ScalarEncapsedStringPart: - p.Print(part) - default: - io.WriteString(p.w, "{") - p.Print(part) - io.WriteString(p.w, "}") - } - } - io.WriteString(p.w, "`") -} - -func (p *PrettyPrinter) printExprStaticCall(n ast.Vertex) { - nn := n.(*ast.ExprStaticCall) - - p.Print(nn.Class) - io.WriteString(p.w, "::") - p.Print(nn.Call) - io.WriteString(p.w, "(") - p.joinPrint(", ", nn.Arguments) - io.WriteString(p.w, ")") -} - -func (p *PrettyPrinter) printExprStaticPropertyFetch(n ast.Vertex) { - nn := n.(*ast.ExprStaticPropertyFetch) - - p.Print(nn.Class) - io.WriteString(p.w, "::") - p.Print(nn.Property) -} - -func (p *PrettyPrinter) printExprTernary(n ast.Vertex) { - nn := n.(*ast.ExprTernary) - - p.Print(nn.Condition) - io.WriteString(p.w, " ?") - - if nn.IfTrue != nil { - io.WriteString(p.w, " ") - p.Print(nn.IfTrue) - io.WriteString(p.w, " ") - } - - io.WriteString(p.w, ": ") - p.Print(nn.IfFalse) -} - -func (p *PrettyPrinter) printExprUnaryMinus(n ast.Vertex) { - nn := n.(*ast.ExprUnaryMinus) - - io.WriteString(p.w, "-") - p.Print(nn.Expr) -} - -func (p *PrettyPrinter) printExprUnaryPlus(n ast.Vertex) { - nn := n.(*ast.ExprUnaryPlus) - - io.WriteString(p.w, "+") - p.Print(nn.Expr) -} - -func (p *PrettyPrinter) printExprVariable(n ast.Vertex) { - nn := n.(*ast.ExprVariable) - io.WriteString(p.w, "$") - p.Print(nn.VarName) -} - -func (p *PrettyPrinter) printExprYieldFrom(n ast.Vertex) { - nn := n.(*ast.ExprYieldFrom) - - io.WriteString(p.w, "yield from ") - p.Print(nn.Expr) -} - -func (p *PrettyPrinter) printExprYield(n ast.Vertex) { - nn := n.(*ast.ExprYield) - - io.WriteString(p.w, "yield ") - - if nn.Key != nil { - p.Print(nn.Key) - io.WriteString(p.w, " => ") - } - - p.Print(nn.Value) -} - -// smtm - -func (p *PrettyPrinter) printStmtAltElseIf(n ast.Vertex) { - nn := n.(*ast.StmtElseIf) - - io.WriteString(p.w, "elseif (") - p.Print(nn.Cond) - io.WriteString(p.w, ") :") - - if s := nn.Stmt.(*ast.StmtStmtList).Stmts; len(s) > 0 { - io.WriteString(p.w, "\n") - p.printNodes(s) - } -} - -func (p *PrettyPrinter) printStmtAltElse(n ast.Vertex) { - nn := n.(*ast.StmtElse) - - io.WriteString(p.w, "else :") - - if s := nn.Stmt.(*ast.StmtStmtList).Stmts; len(s) > 0 { - io.WriteString(p.w, "\n") - p.printNodes(s) - } -} - -func (p *PrettyPrinter) printStmtAltIf(n ast.Vertex) { - nn := n.(*ast.StmtIf) - - io.WriteString(p.w, "if (") - p.Print(nn.Cond) - io.WriteString(p.w, ") :\n") - - s := nn.Stmt.(*ast.StmtStmtList) - p.printNodes(s.Stmts) - - for _, elseif := range nn.ElseIf { - io.WriteString(p.w, "\n") - p.printIndent() - p.Print(elseif) - } - - if nn.Else != nil { - io.WriteString(p.w, "\n") - p.printIndent() - p.Print(nn.Else) - } - - io.WriteString(p.w, "\n") - p.printIndent() - io.WriteString(p.w, "endif;") -} - -func (p *PrettyPrinter) printStmtBreak(n ast.Vertex) { - nn := n.(*ast.StmtBreak) - - io.WriteString(p.w, "break") - if nn.Expr != nil { - io.WriteString(p.w, " ") - p.Print(nn.Expr) - } - - io.WriteString(p.w, ";") -} - -func (p *PrettyPrinter) printStmtCase(n ast.Vertex) { - nn := n.(*ast.StmtCase) - - io.WriteString(p.w, "case ") - p.Print(nn.Cond) - io.WriteString(p.w, ":") - - if len(nn.Stmts) > 0 { - io.WriteString(p.w, "\n") - p.printNodes(nn.Stmts) - } -} - -func (p *PrettyPrinter) printStmtCatch(n ast.Vertex) { - nn := n.(*ast.StmtCatch) - - io.WriteString(p.w, "catch (") - p.joinPrint(" | ", nn.Types) - io.WriteString(p.w, " ") - p.Print(nn.Var) - io.WriteString(p.w, ") {\n") - p.printNodes(nn.Stmts) - io.WriteString(p.w, "\n") - p.printIndent() - io.WriteString(p.w, "}") -} - -func (p *PrettyPrinter) printStmtClassMethod(n ast.Vertex) { - nn := n.(*ast.StmtClassMethod) - - if nn.Modifiers != nil { - p.joinPrint(" ", nn.Modifiers) - io.WriteString(p.w, " ") - } - io.WriteString(p.w, "function ") - - if nn.AmpersandTkn != nil { - io.WriteString(p.w, "&") - } - - p.Print(nn.MethodName) - io.WriteString(p.w, "(") - p.joinPrint(", ", nn.Params) - io.WriteString(p.w, ")") - - if nn.ReturnType != nil { - io.WriteString(p.w, ": ") - p.Print(nn.ReturnType) - } - - switch s := nn.Stmt.(type) { - case *ast.StmtStmtList: - io.WriteString(p.w, "\n") - p.printIndent() - io.WriteString(p.w, "{\n") - p.printNodes(s.Stmts) - io.WriteString(p.w, "\n") - p.printIndent() - io.WriteString(p.w, "}") - default: - p.Print(s) - } -} - -func (p *PrettyPrinter) printStmtClass(n ast.Vertex) { - nn := n.(*ast.StmtClass) - - if nn.Modifiers != nil { - p.joinPrint(" ", nn.Modifiers) - io.WriteString(p.w, " ") - } - io.WriteString(p.w, "class") - - if nn.ClassName != nil { - io.WriteString(p.w, " ") - p.Print(nn.ClassName) - } - - if nn.Arguments != nil { - io.WriteString(p.w, "(") - p.joinPrint(", ", nn.Arguments) - io.WriteString(p.w, ")") - } - - if nn.Extends != nil { - io.WriteString(p.w, " extends ") - p.Print(nn.Extends.(*ast.StmtClassExtends).ClassName) - } - - if nn.Implements != nil { - io.WriteString(p.w, " implements ") - p.joinPrint(", ", nn.Implements.(*ast.StmtClassImplements).InterfaceNames) - } - - io.WriteString(p.w, "\n") - p.printIndent() - io.WriteString(p.w, "{\n") - p.printNodes(nn.Stmts) - io.WriteString(p.w, "\n") - p.printIndent() - io.WriteString(p.w, "}") -} - -func (p *PrettyPrinter) printStmtClassConstList(n ast.Vertex) { - nn := n.(*ast.StmtClassConstList) - - if nn.Modifiers != nil { - p.joinPrint(" ", nn.Modifiers) - io.WriteString(p.w, " ") - } - io.WriteString(p.w, "const ") - - p.joinPrint(", ", nn.Consts) - - io.WriteString(p.w, ";") -} - -func (p *PrettyPrinter) printStmtConstant(n ast.Vertex) { - nn := n.(*ast.StmtConstant) - - p.Print(nn.Name) - io.WriteString(p.w, " = ") - p.Print(nn.Expr) -} - -func (p *PrettyPrinter) printStmtContinue(n ast.Vertex) { - nn := n.(*ast.StmtContinue) - - io.WriteString(p.w, "continue") - if nn.Expr != nil { - io.WriteString(p.w, " ") - p.Print(nn.Expr) - } - - io.WriteString(p.w, ";") -} - -func (p *PrettyPrinter) printStmtDeclare(n ast.Vertex) { - nn := n.(*ast.StmtDeclare) - - io.WriteString(p.w, "declare(") - p.joinPrint(", ", nn.Consts) - io.WriteString(p.w, ")") - - switch s := nn.Stmt.(type) { - case *ast.StmtNop: - p.Print(s) - break - case *ast.StmtStmtList: - io.WriteString(p.w, " ") - p.Print(s) - default: - io.WriteString(p.w, "\n") - p.indentDepth++ - p.printIndent() - p.Print(s) - p.indentDepth-- - } -} - -func (p *PrettyPrinter) printStmtDefault(n ast.Vertex) { - nn := n.(*ast.StmtDefault) - io.WriteString(p.w, "default:") - - if len(nn.Stmts) > 0 { - io.WriteString(p.w, "\n") - p.printNodes(nn.Stmts) - } -} - -func (p *PrettyPrinter) printStmtDo(n ast.Vertex) { - nn := n.(*ast.StmtDo) - io.WriteString(p.w, "do") - - switch s := nn.Stmt.(type) { - case *ast.StmtStmtList: - io.WriteString(p.w, " ") - p.Print(s) - io.WriteString(p.w, " ") - default: - io.WriteString(p.w, "\n") - p.indentDepth++ - p.printIndent() - p.Print(s) - p.indentDepth-- - io.WriteString(p.w, "\n") - p.printIndent() - } - - io.WriteString(p.w, "while (") - p.Print(nn.Cond) - io.WriteString(p.w, ");") -} - -func (p *PrettyPrinter) printStmtEcho(n ast.Vertex) { - nn := n.(*ast.StmtEcho) - io.WriteString(p.w, "echo ") - p.joinPrint(", ", nn.Exprs) - io.WriteString(p.w, ";") -} - -func (p *PrettyPrinter) printStmtElseif(n ast.Vertex) { - nn := n.(*ast.StmtElseIf) - - if nn.Alt { - p.printStmtAltElseIf(nn) - return - } - - io.WriteString(p.w, "elseif (") - p.Print(nn.Cond) - io.WriteString(p.w, ")") - - switch s := nn.Stmt.(type) { - case *ast.StmtNop: - p.Print(s) - break - case *ast.StmtStmtList: - io.WriteString(p.w, " ") - p.Print(s) - default: - io.WriteString(p.w, "\n") - p.indentDepth++ - p.printIndent() - p.Print(s) - p.indentDepth-- - } -} - -func (p *PrettyPrinter) printStmtElse(n ast.Vertex) { - nn := n.(*ast.StmtElse) - - if nn.Alt { - p.printStmtAltElse(nn) - return - } - - io.WriteString(p.w, "else") - - switch s := nn.Stmt.(type) { - case *ast.StmtNop: - p.Print(s) - break - case *ast.StmtStmtList: - io.WriteString(p.w, " ") - p.Print(s) - default: - io.WriteString(p.w, "\n") - p.indentDepth++ - p.printIndent() - p.Print(s) - p.indentDepth-- - } -} - -func (p *PrettyPrinter) printStmtExpression(n ast.Vertex) { - nn := n.(*ast.StmtExpression) - - p.Print(nn.Expr) - - io.WriteString(p.w, ";") -} - -func (p *PrettyPrinter) printStmtFinally(n ast.Vertex) { - nn := n.(*ast.StmtFinally) - - io.WriteString(p.w, "finally {\n") - p.printNodes(nn.Stmts) - io.WriteString(p.w, "\n") - p.printIndent() - io.WriteString(p.w, "}") -} - -func (p *PrettyPrinter) printStmtFor(n ast.Vertex) { - nn := n.(*ast.StmtFor) - - if nn.Alt { - p.printStmtAltFor(nn) - return - } - - io.WriteString(p.w, "for (") - p.joinPrint(", ", nn.Init) - io.WriteString(p.w, "; ") - p.joinPrint(", ", nn.Cond) - io.WriteString(p.w, "; ") - p.joinPrint(", ", nn.Loop) - io.WriteString(p.w, ")") - - switch s := nn.Stmt.(type) { - case *ast.StmtNop: - p.Print(s) - break - case *ast.StmtStmtList: - io.WriteString(p.w, " ") - p.Print(s) - default: - io.WriteString(p.w, "\n") - p.indentDepth++ - p.printIndent() - p.Print(s) - p.indentDepth-- - } -} - -func (p *PrettyPrinter) printStmtAltFor(n ast.Vertex) { - nn := n.(*ast.StmtFor) - - io.WriteString(p.w, "for (") - p.joinPrint(", ", nn.Init) - io.WriteString(p.w, "; ") - p.joinPrint(", ", nn.Cond) - io.WriteString(p.w, "; ") - p.joinPrint(", ", nn.Loop) - io.WriteString(p.w, ") :\n") - - s := nn.Stmt.(*ast.StmtStmtList) - p.printNodes(s.Stmts) - io.WriteString(p.w, "\n") - p.printIndent() - - io.WriteString(p.w, "endfor;") -} - -func (p *PrettyPrinter) printStmtForeach(n ast.Vertex) { - nn := n.(*ast.StmtForeach) - - if nn.Alt { - p.printStmtAltForeach(n) - return - } - - io.WriteString(p.w, "foreach (") - p.Print(nn.Expr) - io.WriteString(p.w, " as ") - - if nn.Key != nil { - p.Print(nn.Key) - io.WriteString(p.w, " => ") - } - - p.Print(nn.Var) - io.WriteString(p.w, ")") - - switch s := nn.Stmt.(type) { - case *ast.StmtNop: - p.Print(s) - break - case *ast.StmtStmtList: - io.WriteString(p.w, " ") - p.Print(s) - default: - io.WriteString(p.w, "\n") - p.indentDepth++ - p.printIndent() - p.Print(s) - p.indentDepth-- - } -} - -func (p *PrettyPrinter) printStmtAltForeach(n ast.Vertex) { - nn := n.(*ast.StmtForeach) - - io.WriteString(p.w, "foreach (") - p.Print(nn.Expr) - io.WriteString(p.w, " as ") - - if nn.Key != nil { - p.Print(nn.Key) - io.WriteString(p.w, " => ") - } - - p.Print(nn.Var) - - io.WriteString(p.w, ") :\n") - - s := nn.Stmt.(*ast.StmtStmtList) - p.printNodes(s.Stmts) - - io.WriteString(p.w, "\n") - p.printIndent() - io.WriteString(p.w, "endforeach;") -} - -func (p *PrettyPrinter) printStmtFunction(n ast.Vertex) { - nn := n.(*ast.StmtFunction) - - io.WriteString(p.w, "function ") - - if nn.AmpersandTkn != nil { - io.WriteString(p.w, "&") - } - - p.Print(nn.FunctionName) - - io.WriteString(p.w, "(") - p.joinPrint(", ", nn.Params) - io.WriteString(p.w, ")") - - if nn.ReturnType != nil { - io.WriteString(p.w, ": ") - p.Print(nn.ReturnType) - } - - io.WriteString(p.w, " {\n") - p.printNodes(nn.Stmts) - io.WriteString(p.w, "\n") - p.printIndent() - io.WriteString(p.w, "}") -} - -func (p *PrettyPrinter) printStmtGlobal(n ast.Vertex) { - nn := n.(*ast.StmtGlobal) - - io.WriteString(p.w, "global ") - p.joinPrint(", ", nn.Vars) - io.WriteString(p.w, ";") -} - -func (p *PrettyPrinter) printStmtGoto(n ast.Vertex) { - nn := n.(*ast.StmtGoto) - - io.WriteString(p.w, "goto ") - p.Print(nn.Label) - io.WriteString(p.w, ";") -} - -func (p *PrettyPrinter) printStmtHaltCompiler(n ast.Vertex) { - io.WriteString(p.w, "__halt_compiler();") -} - -func (p *PrettyPrinter) printStmtIf(n ast.Vertex) { - nn := n.(*ast.StmtIf) - - if nn.Alt { - p.printStmtAltIf(nn) - return - } - - io.WriteString(p.w, "if (") - p.Print(nn.Cond) - io.WriteString(p.w, ")") - - switch s := nn.Stmt.(type) { - case *ast.StmtNop: - p.Print(s) - break - case *ast.StmtStmtList: - io.WriteString(p.w, " ") - p.Print(s) - default: - io.WriteString(p.w, "\n") - p.indentDepth++ - p.printIndent() - p.Print(s) - p.indentDepth-- - } - - if nn.ElseIf != nil { - io.WriteString(p.w, "\n") - p.indentDepth-- - p.printNodes(nn.ElseIf) - p.indentDepth++ - } - - if nn.Else != nil { - io.WriteString(p.w, "\n") - p.printIndent() - p.Print(nn.Else) - } -} - -func (p *PrettyPrinter) printStmtInlineHTML(n ast.Vertex) { - nn := n.(*ast.StmtInlineHtml) - - io.WriteString(p.w, "?>") - io.WriteString(p.w, string(nn.Value)) - io.WriteString(p.w, "HTML")}, - &ast.StmtExpression{ - Expr: &ast.ScalarHeredoc{ - Label: []byte("<<<\"LBL\"\n"), - Parts: []ast.Vertex{ - &ast.ScalarEncapsedStringPart{Value: []byte("hello world\n")}, - }, - }, - }, - }, - }) - - expected := `
HTML
>= $b` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -// binary - -func TestPrintBinaryBitwiseAnd(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprBinaryBitwiseAnd{ - Left: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - Right: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}, - }) - - expected := `$a & $b` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintBinaryBitwiseOr(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprBinaryBitwiseOr{ - Left: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - Right: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}, - }) - - expected := `$a | $b` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintBinaryBitwiseXor(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprBinaryBitwiseXor{ - Left: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - Right: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}, - }) - - expected := `$a ^ $b` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintBinaryBooleanAnd(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprBinaryBooleanAnd{ - Left: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - Right: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}, - }) - - expected := `$a && $b` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintBinaryBooleanOr(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprBinaryBooleanOr{ - Left: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - Right: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}, - }) - - expected := `$a || $b` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintBinaryCoalesce(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprBinaryCoalesce{ - Left: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - Right: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}, - }) - - expected := `$a ?? $b` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintBinaryConcat(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprBinaryConcat{ - Left: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - Right: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}, - }) - - expected := `$a . $b` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintBinaryDiv(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprBinaryDiv{ - Left: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - Right: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}, - }) - - expected := `$a / $b` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintBinaryEqual(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprBinaryEqual{ - Left: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - Right: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}, - }) - - expected := `$a == $b` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintBinaryGreaterOrEqual(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprBinaryGreaterOrEqual{ - Left: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - Right: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}, - }) - - expected := `$a >= $b` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintBinaryGreater(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprBinaryGreater{ - Left: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - Right: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}, - }) - - expected := `$a > $b` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintBinaryIdentical(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprBinaryIdentical{ - Left: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - Right: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}, - }) - - expected := `$a === $b` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintBinaryLogicalAnd(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprBinaryLogicalAnd{ - Left: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - Right: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}, - }) - - expected := `$a and $b` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintBinaryLogicalOr(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprBinaryLogicalOr{ - Left: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - Right: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}, - }) - - expected := `$a or $b` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintBinaryLogicalXor(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprBinaryLogicalXor{ - Left: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - Right: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}, - }) - - expected := `$a xor $b` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintBinaryMinus(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprBinaryMinus{ - Left: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - Right: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}, - }) - - expected := `$a - $b` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintBinaryMod(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprBinaryMod{ - Left: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - Right: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}, - }) - - expected := `$a % $b` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintBinaryMul(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprBinaryMul{ - Left: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - Right: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}, - }) - - expected := `$a * $b` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintBinaryNotEqual(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprBinaryNotEqual{ - Left: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - Right: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}, - }) - - expected := `$a != $b` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintBinaryNotIdentical(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprBinaryNotIdentical{ - Left: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - Right: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}, - }) - - expected := `$a !== $b` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintBinaryPlus(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprBinaryPlus{ - Left: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - Right: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}, - }) - - expected := `$a + $b` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintBinaryPow(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprBinaryPow{ - Left: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - Right: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}, - }) - - expected := `$a ** $b` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintBinaryShiftLeft(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprBinaryShiftLeft{ - Left: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - Right: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}, - }) - - expected := `$a << $b` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintBinaryShiftRight(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprBinaryShiftRight{ - Left: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - Right: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}, - }) - - expected := `$a >> $b` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintBinarySmallerOrEqual(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprBinarySmallerOrEqual{ - Left: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - Right: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}, - }) - - expected := `$a <= $b` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintBinarySmaller(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprBinarySmaller{ - Left: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - Right: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}, - }) - - expected := `$a < $b` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintBinarySpaceship(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprBinarySpaceship{ - Left: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - Right: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}, - }) - - expected := `$a <=> $b` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -// cast - -func TestPrintArray(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprCastArray{ - Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("var")}}, - }) - - expected := `(array)$var` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintBool(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprCastBool{ - Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("var")}}, - }) - - expected := `(bool)$var` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintDouble(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprCastDouble{ - Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("var")}}, - }) - - expected := `(float)$var` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintInt(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprCastInt{ - Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("var")}}, - }) - - expected := `(int)$var` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintObject(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprCastObject{ - Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("var")}}, - }) - - expected := `(object)$var` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintString(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprCastString{ - Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("var")}}, - }) - - expected := `(string)$var` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintUnset(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprCastUnset{ - Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("var")}}, - }) - - expected := `(unset)$var` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -// expr - -func TestPrintExprArrayDimFetch(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprArrayDimFetch{ - Var: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("var")}}, - Dim: &ast.ScalarLnumber{Value: []byte("1")}, - }) - - expected := `$var[1]` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintExprArrayItemWithKey(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprArrayItem{ - Key: &ast.ScalarString{Value: []byte("'Hello'")}, - Val: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("world")}}, - }) - - expected := `'Hello' => $world` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintExprArrayItem(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprArrayItem{ - Val: &ast.ExprReference{Var: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("world")}}}, - }) - - expected := `&$world` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintExprArray(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprArray{ - Items: []ast.Vertex{ - &ast.ExprArrayItem{ - Key: &ast.ScalarString{Value: []byte("'Hello'")}, - Val: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("world")}}, - }, - &ast.ExprArrayItem{ - Key: &ast.ScalarLnumber{Value: []byte("2")}, - Val: &ast.ExprReference{Var: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("var")}}}, - }, - &ast.ExprArrayItem{ - Val: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("var")}}, - }, - }, - }) - - expected := `array('Hello' => $world, 2 => &$var, $var)` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintExprBitwiseNot(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprBitwiseNot{ - Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("var")}}, - }) - - expected := `~$var` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintExprBooleanNot(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprBooleanNot{ - Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("var")}}, - }) - - expected := `!$var` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintExprClassConstFetch(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprClassConstFetch{ - Class: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("var")}}, - ConstantName: &ast.Identifier{Value: []byte("CONST")}, - }) - - expected := `$var::CONST` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintExprClone(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprClone{ - Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("var")}}, - }) - - expected := `clone $var` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintExprClosureUse(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprClosureUse{ - Uses: []ast.Vertex{ - &ast.ExprReference{Var: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("foo")}}}, - &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("bar")}}, - }, - }) - - expected := `use (&$foo, $bar)` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintExprClosure(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtNamespace{ - Stmts: []ast.Vertex{ - &ast.ExprClosure{ - Static: true, - ReturnsRef: true, - Params: []ast.Vertex{ - &ast.Parameter{ - Var: &ast.Reference{Var: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("var")}}}, - }, - }, - ClosureUse: &ast.ExprClosureUse{ - Uses: []ast.Vertex{ - &ast.ExprReference{Var: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}}, - &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}, - }, - }, - ReturnType: &ast.NameFullyQualified{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}}, - Stmts: []ast.Vertex{ - &ast.StmtExpression{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}}, - }, - }, - }, - }) - - expected := `namespace { - static function &(&$var) use (&$a, $b): \Foo { - $a; - } -}` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintExprConstFetch(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprConstFetch{ - Const: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("null")}}}, - }) - - expected := "null" - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintEmpty(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprEmpty{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("var")}}}) - - expected := `empty($var)` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrettyPrinterrorSuppress(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprErrorSuppress{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("var")}}}) - - expected := `@$var` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintEval(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprEval{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("var")}}}) - - expected := `eval($var)` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintExit(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprExit{Die: false, Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("var")}}}) - - expected := `exit($var)` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintDie(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprExit{Die: true, Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("var")}}}) - - expected := `die($var)` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintFunctionCall(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprFunctionCall{ - Function: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("var")}}, - ArgumentList: &ast.ArgumentList{ - Arguments: []ast.Vertex{ - &ast.Argument{ - IsReference: true, - Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - }, - &ast.Argument{ - Variadic: true, - Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}, - }, - &ast.Argument{ - Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("c")}}, - }, - }, - }, - }) - - expected := `$var(&$a, ...$b, $c)` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintInclude(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprInclude{Expr: &ast.ScalarString{Value: []byte("'path'")}}) - - expected := `include 'path'` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintIncludeOnce(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprIncludeOnce{Expr: &ast.ScalarString{Value: []byte("'path'")}}) - - expected := `include_once 'path'` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintInstanceOf(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprInstanceOf{ - Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("var")}}, - Class: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}}, - }) - - expected := `$var instanceof Foo` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintIsset(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprIsset{ - Vars: []ast.Vertex{ - &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}, - }, - }) - - expected := `isset($a, $b)` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintList(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprList{ - Items: []ast.Vertex{ - &ast.ExprArrayItem{ - Val: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - }, - &ast.ExprArrayItem{ - Val: &ast.ExprList{ - Items: []ast.Vertex{ - &ast.ExprArrayItem{ - Val: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}, - }, - &ast.ExprArrayItem{ - Val: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("c")}}, - }, - }, - }, - }, - }, - }) - - expected := `list($a, list($b, $c))` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintMethodCall(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprMethodCall{ - Var: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("foo")}}, - Method: &ast.Identifier{Value: []byte("bar")}, - ArgumentList: &ast.ArgumentList{ - Arguments: []ast.Vertex{ - &ast.Argument{ - Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - }, - &ast.Argument{ - Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}, - }, - }, - }, - }) - - expected := `$foo->bar($a, $b)` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintNew(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprNew{ - Class: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}}, - ArgumentList: &ast.ArgumentList{ - Arguments: []ast.Vertex{ - &ast.Argument{ - Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - }, - &ast.Argument{ - Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}, - }, - }, - }, - }) - - expected := `new Foo($a, $b)` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintPostDec(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprPostDec{ - Var: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("var")}}, - }) - - expected := `$var--` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintPostInc(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprPostInc{ - Var: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("var")}}, - }) - - expected := `$var++` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintPreDec(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprPreDec{ - Var: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("var")}}, - }) - - expected := `--$var` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintPreInc(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprPreInc{ - Var: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("var")}}, - }) - - expected := `++$var` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintPrint(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprPrint{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("var")}}}) - - expected := `print($var)` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintPropertyFetch(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprPropertyFetch{ - Var: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("foo")}}, - Property: &ast.Identifier{Value: []byte("bar")}, - }) - - expected := `$foo->bar` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintExprReference(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprReference{ - Var: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("foo")}}, - }) - - expected := `&$foo` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintRequire(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprRequire{Expr: &ast.ScalarString{Value: []byte("'path'")}}) - - expected := `require 'path'` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintRequireOnce(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprRequireOnce{Expr: &ast.ScalarString{Value: []byte("'path'")}}) - - expected := `require_once 'path'` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintShellExec(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprShellExec{ - Parts: []ast.Vertex{ - &ast.ScalarEncapsedStringPart{Value: []byte("hello ")}, - &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("world")}}, - &ast.ScalarEncapsedStringPart{Value: []byte("!")}, - }, - }) - - expected := "`hello {$world}!`" - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintExprShortArray(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprShortArray{ - Items: []ast.Vertex{ - &ast.ExprArrayItem{ - Key: &ast.ScalarString{Value: []byte("'Hello'")}, - Val: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("world")}}, - }, - &ast.ExprArrayItem{ - Key: &ast.ScalarLnumber{Value: []byte("2")}, - Val: &ast.ExprReference{Var: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("var")}}}, - }, - &ast.ExprArrayItem{ - Val: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("var")}}, - }, - }, - }) - - expected := `['Hello' => $world, 2 => &$var, $var]` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintShortList(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprShortList{ - Items: []ast.Vertex{ - &ast.ExprArrayItem{ - Val: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - }, - &ast.ExprArrayItem{ - Val: &ast.ExprList{ - Items: []ast.Vertex{ - &ast.ExprArrayItem{ - Val: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}, - }, - &ast.ExprArrayItem{ - Val: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("c")}}, - }, - }, - }, - }, - }, - }) - - expected := `[$a, list($b, $c)]` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintStaticCall(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprStaticCall{ - Class: &ast.Identifier{Value: []byte("Foo")}, - Call: &ast.Identifier{Value: []byte("bar")}, - ArgumentList: &ast.ArgumentList{ - Arguments: []ast.Vertex{ - &ast.Argument{ - Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - }, - &ast.Argument{ - Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}, - }, - }, - }, - }) - - expected := `Foo::bar($a, $b)` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintStaticPropertyFetch(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprStaticPropertyFetch{ - Class: &ast.Identifier{Value: []byte("Foo")}, - Property: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("bar")}}, - }) - - expected := `Foo::$bar` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintTernary(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprTernary{ - Condition: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - IfFalse: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}, - }) - - expected := `$a ?: $b` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintTernaryFull(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprTernary{ - Condition: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - IfTrue: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}, - IfFalse: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("c")}}, - }) - - expected := `$a ? $b : $c` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintUnaryMinus(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprUnaryMinus{ - Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("var")}}, - }) - - expected := `-$var` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintUnaryPlus(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprUnaryPlus{ - Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("var")}}, - }) - - expected := `+$var` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintVariable(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprVariable{VarName: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("var")}}}) - - expected := `$$var` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintYieldFrom(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprYieldFrom{ - Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("var")}}, - }) - - expected := `yield from $var` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintYield(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprYield{ - Value: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("var")}}, - }) - - expected := `yield $var` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintYieldFull(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.ExprYield{ - Key: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("k")}}, - Value: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("var")}}, - }) - - expected := `yield $k => $var` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -// stmt - -func TestPrintAltElseIf(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtElseIf{ - Alt: true, - Cond: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - Stmt: &ast.StmtStmtList{ - Stmts: []ast.Vertex{ - &ast.StmtExpression{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}}, - }, - }, - }) - - expected := `elseif ($a) : - $b;` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintAltElseIfEmpty(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtElseIf{ - Alt: true, - Cond: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - Stmt: &ast.StmtStmtList{}, - }) - - expected := `elseif ($a) :` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintAltElse(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtElse{ - Alt: true, - Stmt: &ast.StmtStmtList{ - Stmts: []ast.Vertex{ - &ast.StmtExpression{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}}, - }, - }, - }) - - expected := `else : - $b;` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintAltElseEmpty(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtElse{ - Alt: true, - Stmt: &ast.StmtStmtList{}, - }) - - expected := `else :` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintAltFor(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtNamespace{ - Stmts: []ast.Vertex{ - &ast.StmtFor{ - Alt: true, - Init: []ast.Vertex{ - &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - }, - Cond: []ast.Vertex{ - &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}, - }, - Loop: []ast.Vertex{ - &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("c")}}, - }, - Stmt: &ast.StmtStmtList{ - Stmts: []ast.Vertex{ - &ast.StmtExpression{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("d")}}}, - }, - }, - }, - }, - }) - - expected := `namespace { - for ($a; $b; $c) : - $d; - endfor; -}` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintAltForeach(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtNamespace{ - Stmts: []ast.Vertex{ - &ast.StmtForeach{ - Alt: true, - Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("var")}}, - Key: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("key")}}, - Var: &ast.ExprReference{Var: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("val")}}}, - Stmt: &ast.StmtStmtList{ - Stmts: []ast.Vertex{ - &ast.StmtExpression{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("d")}}}, - }, - }, - }, - }, - }) - - expected := `namespace { - foreach ($var as $key => &$val) : - $d; - endforeach; -}` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintAltIf(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtNamespace{ - Stmts: []ast.Vertex{ - &ast.StmtIf{ - Alt: true, - Cond: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - Stmt: &ast.StmtStmtList{ - Stmts: []ast.Vertex{ - &ast.StmtExpression{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("d")}}}, - }, - }, - ElseIf: []ast.Vertex{ - &ast.StmtElseIf{ - Alt: true, - Cond: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}, - Stmt: &ast.StmtStmtList{ - Stmts: []ast.Vertex{ - &ast.StmtExpression{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}}, - }, - }, - }, - &ast.StmtElseIf{ - Alt: true, - Cond: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("c")}}, - Stmt: &ast.StmtStmtList{}, - }, - }, - Else: &ast.StmtElse{ - Alt: true, - Stmt: &ast.StmtStmtList{ - Stmts: []ast.Vertex{ - &ast.StmtExpression{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}}, - }, - }, - }, - }, - }, - }) - - expected := `namespace { - if ($a) : - $d; - elseif ($b) : - $b; - elseif ($c) : - else : - $b; - endif; -}` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintStmtAltSwitch(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtNamespace{ - Stmts: []ast.Vertex{ - &ast.StmtSwitch{ - Alt: true, - Cond: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("var")}}, - CaseList: []ast.Vertex{ - &ast.StmtCase{ - Cond: &ast.ScalarString{Value: []byte("'a'")}, - Stmts: []ast.Vertex{ - &ast.StmtExpression{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}}, - }, - }, - &ast.StmtCase{ - Cond: &ast.ScalarString{Value: []byte("'b'")}, - Stmts: []ast.Vertex{ - &ast.StmtExpression{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}}, - }, - }, - }, - }, - }, - }) - - expected := `namespace { - switch ($var) : - case 'a': - $a; - case 'b': - $b; - endswitch; -}` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintAltWhile(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtNamespace{ - Stmts: []ast.Vertex{ - &ast.StmtWhile{ - Alt: true, - Cond: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - Stmt: &ast.StmtStmtList{ - Stmts: []ast.Vertex{ - &ast.StmtExpression{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}}, - }, - }, - }, - }, - }) - - expected := `namespace { - while ($a) : - $b; - endwhile; -}` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintStmtBreak(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtBreak{ - Expr: &ast.ScalarLnumber{Value: []byte("1")}, - }) - - expected := "break 1;" - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintStmtCase(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtCase{ - Cond: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - Stmts: []ast.Vertex{ - &ast.StmtExpression{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}}, - }, - }) - - expected := `case $a: - $a;` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintStmtCaseEmpty(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtCase{ - Cond: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - Stmts: []ast.Vertex{}, - }) - - expected := "case $a:" - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintStmtCatch(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtNamespace{ - Stmts: []ast.Vertex{ - &ast.StmtCatch{ - Types: []ast.Vertex{ - &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Exception")}}}, - &ast.NameFullyQualified{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("RuntimeException")}}}, - }, - Var: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("e")}}, - Stmts: []ast.Vertex{ - &ast.StmtExpression{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}}, - }, - }, - }, - }) - - expected := `namespace { - catch (Exception | \RuntimeException $e) { - $a; - } -}` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintStmtClassMethod(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtClassMethod{ - Modifiers: []ast.Vertex{&ast.Identifier{Value: []byte("public")}}, - ReturnsRef: true, - MethodName: &ast.Identifier{Value: []byte("foo")}, - Params: []ast.Vertex{ - &ast.Parameter{ - Type: &ast.Nullable{Expr: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("int")}}}}, - Var: &ast.Reference{Var: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}}, - DefaultValue: &ast.ExprConstFetch{Const: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("null")}}}}, - }, - &ast.Parameter{ - Var: &ast.Variadic{Var: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}}, - }, - }, - ReturnType: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("void")}}}, - Stmt: &ast.StmtStmtList{ - Stmts: []ast.Vertex{ - &ast.StmtExpression{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}}, - }, - }, - }) - - expected := `public function &foo(?int &$a = null, ...$b): void -{ - $a; -}` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} -func TestPrintStmtAbstractClassMethod(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtClassMethod{ - Modifiers: []ast.Vertex{&ast.Identifier{Value: []byte("public")}}, - ReturnsRef: true, - MethodName: &ast.Identifier{Value: []byte("foo")}, - Params: []ast.Vertex{ - &ast.Parameter{ - Type: &ast.Nullable{Expr: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("int")}}}}, - Var: &ast.Reference{Var: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}}, - DefaultValue: &ast.ExprConstFetch{Const: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("null")}}}}, - }, - &ast.Parameter{ - Var: &ast.Variadic{Var: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}}, - }, - }, - ReturnType: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("void")}}}, - Stmt: &ast.StmtNop{}, - }) - - expected := `public function &foo(?int &$a = null, ...$b): void;` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintStmtClass(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtNamespace{ - Stmts: []ast.Vertex{ - &ast.StmtClass{ - Modifiers: []ast.Vertex{&ast.Identifier{Value: []byte("abstract")}}, - ClassName: &ast.Identifier{Value: []byte("Foo")}, - Extends: &ast.StmtClassExtends{ - ClassName: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Bar")}}}, - }, - Implements: &ast.StmtClassImplements{ - InterfaceNames: []ast.Vertex{ - &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Baz")}}}, - &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Quuz")}}}, - }, - }, - Stmts: []ast.Vertex{ - &ast.StmtClassConstList{ - Modifiers: []ast.Vertex{&ast.Identifier{Value: []byte("public")}}, - Consts: []ast.Vertex{ - &ast.StmtConstant{ - Name: &ast.Identifier{Value: []byte("FOO")}, - Expr: &ast.ScalarString{Value: []byte("'bar'")}, - }, - }, - }, - }, - }, - }, - }) - - expected := `namespace { - abstract class Foo extends Bar implements Baz, Quuz - { - public const FOO = 'bar'; - } -}` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintStmtAnonymousClass(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtNamespace{ - Stmts: []ast.Vertex{ - &ast.StmtClass{ - Modifiers: []ast.Vertex{&ast.Identifier{Value: []byte("abstract")}}, - ArgumentList: &ast.ArgumentList{ - Arguments: []ast.Vertex{ - &ast.Argument{ - Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - }, - &ast.Argument{ - Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}, - }, - }, - }, - Extends: &ast.StmtClassExtends{ - ClassName: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Bar")}}}, - }, - Implements: &ast.StmtClassImplements{ - InterfaceNames: []ast.Vertex{ - &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Baz")}}}, - &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Quuz")}}}, - }, - }, - Stmts: []ast.Vertex{ - &ast.StmtClassConstList{ - Modifiers: []ast.Vertex{&ast.Identifier{Value: []byte("public")}}, - Consts: []ast.Vertex{ - &ast.StmtConstant{ - Name: &ast.Identifier{Value: []byte("FOO")}, - Expr: &ast.ScalarString{Value: []byte("'bar'")}, - }, - }, - }, - }, - }, - }, - }) - - expected := `namespace { - abstract class($a, $b) extends Bar implements Baz, Quuz - { - public const FOO = 'bar'; - } -}` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintStmtClassConstList(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtClassConstList{ - Modifiers: []ast.Vertex{&ast.Identifier{Value: []byte("public")}}, - Consts: []ast.Vertex{ - &ast.StmtConstant{ - Name: &ast.Identifier{Value: []byte("FOO")}, - Expr: &ast.ScalarString{Value: []byte("'a'")}, - }, - &ast.StmtConstant{ - Name: &ast.Identifier{Value: []byte("BAR")}, - Expr: &ast.ScalarString{Value: []byte("'b'")}, - }, - }, - }) - - expected := `public const FOO = 'a', BAR = 'b';` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintStmtConstant(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtConstant{ - Name: &ast.Identifier{Value: []byte("FOO")}, - Expr: &ast.ScalarString{Value: []byte("'BAR'")}, - }) - - expected := "FOO = 'BAR'" - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintStmtContinue(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtContinue{ - Expr: &ast.ScalarLnumber{Value: []byte("1")}, - }) - - expected := `continue 1;` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintStmtDeclareStmts(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtStmtList{ - Stmts: []ast.Vertex{ - &ast.StmtDeclare{ - Consts: []ast.Vertex{ - &ast.StmtConstant{ - Name: &ast.Identifier{Value: []byte("FOO")}, - Expr: &ast.ScalarString{Value: []byte("'bar'")}, - }, - }, - Stmt: &ast.StmtStmtList{ - Stmts: []ast.Vertex{ - &ast.StmtNop{}, - }, - }, - }, - }, - }) - - expected := `{ - declare(FOO = 'bar') { - ; - } -}` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintStmtDeclareExpr(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtStmtList{ - Stmts: []ast.Vertex{ - &ast.StmtDeclare{ - Consts: []ast.Vertex{ - &ast.StmtConstant{ - Name: &ast.Identifier{Value: []byte("FOO")}, - Expr: &ast.ScalarString{Value: []byte("'bar'")}, - }, - }, - Stmt: &ast.StmtExpression{Expr: &ast.ScalarString{Value: []byte("'bar'")}}, - }, - }, - }) - - expected := `{ - declare(FOO = 'bar') - 'bar'; -}` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintStmtDeclareNop(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtDeclare{ - Consts: []ast.Vertex{ - &ast.StmtConstant{ - Name: &ast.Identifier{Value: []byte("FOO")}, - Expr: &ast.ScalarString{Value: []byte("'bar'")}, - }, - }, - Stmt: &ast.StmtNop{}, - }) - - expected := `declare(FOO = 'bar');` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintStmtDefalut(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtDefault{ - Stmts: []ast.Vertex{ - &ast.StmtExpression{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}}, - }, - }) - - expected := `default: - $a;` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintStmtDefalutEmpty(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtDefault{ - Stmts: []ast.Vertex{}, - }) - - expected := `default:` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintStmtDo_Expression(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtNamespace{ - Stmts: []ast.Vertex{ - &ast.StmtDo{ - Cond: &ast.ScalarLnumber{Value: []byte("1")}, - Stmt: &ast.StmtExpression{ - Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - }, - }, - }, - }) - - expected := `namespace { - do - $a; - while (1); -}` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintStmtDo_StmtList(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtNamespace{ - Stmts: []ast.Vertex{ - &ast.StmtDo{ - Cond: &ast.ScalarLnumber{Value: []byte("1")}, - Stmt: &ast.StmtStmtList{ - Stmts: []ast.Vertex{ - &ast.StmtExpression{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}}, - }, - }, - }, - }, - }) - - expected := `namespace { - do { - $a; - } while (1); -}` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintStmtEcho(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtEcho{ - Exprs: []ast.Vertex{ - &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}, - }, - }) - - expected := `echo $a, $b;` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintStmtElseIfStmts(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtElseIf{ - Cond: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - Stmt: &ast.StmtStmtList{ - Stmts: []ast.Vertex{ - &ast.StmtNop{}, - }, - }, - }) - - expected := `elseif ($a) { - ; -}` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintStmtElseIfExpr(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtElseIf{ - Cond: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - Stmt: &ast.StmtExpression{Expr: &ast.ScalarString{Value: []byte("'bar'")}}, - }) - - expected := `elseif ($a) - 'bar';` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintStmtElseIfNop(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtElseIf{ - Cond: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - Stmt: &ast.StmtNop{}, - }) - - expected := `elseif ($a);` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintStmtElseStmts(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtElse{ - Stmt: &ast.StmtStmtList{ - Stmts: []ast.Vertex{ - &ast.StmtNop{}, - }, - }, - }) - - expected := `else { - ; -}` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintStmtElseExpr(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtElse{ - Stmt: &ast.StmtExpression{Expr: &ast.ScalarString{Value: []byte("'bar'")}}, - }) - - expected := `else - 'bar';` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintStmtElseNop(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtElse{ - Stmt: &ast.StmtNop{}, - }) - - expected := `else;` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintExpression(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtExpression{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}}) - - expected := `$a;` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintStmtFinally(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtNamespace{ - Stmts: []ast.Vertex{ - &ast.StmtFinally{ - Stmts: []ast.Vertex{ - &ast.StmtNop{}, - }, - }, - }, - }) - - expected := `namespace { - finally { - ; - } -}` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintStmtForStmts(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtNamespace{ - Stmts: []ast.Vertex{ - &ast.StmtFor{ - Init: []ast.Vertex{ - &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}, - }, - Cond: []ast.Vertex{ - &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("c")}}, - &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("d")}}, - }, - Loop: []ast.Vertex{ - &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("e")}}, - &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("f")}}, - }, - Stmt: &ast.StmtStmtList{ - Stmts: []ast.Vertex{ - &ast.StmtNop{}, - }, - }, - }, - }, - }) - - expected := `namespace { - for ($a, $b; $c, $d; $e, $f) { - ; - } -}` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintStmtForExpr(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtNamespace{ - Stmts: []ast.Vertex{ - &ast.StmtFor{ - Init: []ast.Vertex{ - &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - }, - Cond: []ast.Vertex{ - &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}, - }, - Loop: []ast.Vertex{ - &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("c")}}, - }, - Stmt: &ast.StmtExpression{Expr: &ast.ScalarString{Value: []byte("'bar'")}}, - }, - }, - }) - - expected := `namespace { - for ($a; $b; $c) - 'bar'; -}` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintStmtForNop(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtFor{ - Init: []ast.Vertex{ - &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - }, - Cond: []ast.Vertex{ - &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}, - }, - Loop: []ast.Vertex{ - &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("c")}}, - }, - Stmt: &ast.StmtNop{}, - }) - - expected := `for ($a; $b; $c);` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintStmtForeachStmts(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtNamespace{ - Stmts: []ast.Vertex{ - &ast.StmtForeach{ - Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - Var: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}, - Stmt: &ast.StmtStmtList{ - Stmts: []ast.Vertex{ - &ast.StmtNop{}, - }, - }, - }, - }, - }) - - expected := `namespace { - foreach ($a as $b) { - ; - } -}` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintStmtForeachExpr(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtNamespace{ - Stmts: []ast.Vertex{ - &ast.StmtForeach{ - Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - Key: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("k")}}, - Var: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("v")}}, - Stmt: &ast.StmtExpression{Expr: &ast.ScalarString{Value: []byte("'bar'")}}, - }, - }, - }) - - expected := `namespace { - foreach ($a as $k => $v) - 'bar'; -}` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintStmtForeachNop(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtForeach{ - Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - Key: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("k")}}, - Var: &ast.ExprReference{Var: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("v")}}}, - Stmt: &ast.StmtNop{}, - }) - - expected := `foreach ($a as $k => &$v);` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintStmtFunction(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtNamespace{ - Stmts: []ast.Vertex{ - &ast.StmtFunction{ - ReturnsRef: true, - FunctionName: &ast.Identifier{Value: []byte("foo")}, - Params: []ast.Vertex{ - &ast.Parameter{ - Var: &ast.Reference{Var: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("var")}}}, - }, - }, - ReturnType: &ast.NameFullyQualified{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}}, - Stmts: []ast.Vertex{ - &ast.StmtNop{}, - }, - }, - }, - }) - - expected := `namespace { - function &foo(&$var): \Foo { - ; - } -}` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintStmtGlobal(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtGlobal{ - Vars: []ast.Vertex{ - &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}, - }, - }) - - expected := `global $a, $b;` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintStmtGoto(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtGoto{ - Label: &ast.Identifier{Value: []byte("FOO")}, - }) - - expected := `goto FOO;` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintHaltCompiler(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtHaltCompiler{}) - - expected := `__halt_compiler();` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintIfExpression(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtNamespace{ - Stmts: []ast.Vertex{ - &ast.StmtIf{ - Cond: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - Stmt: &ast.StmtExpression{ - Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}, - }, - ElseIf: []ast.Vertex{ - &ast.StmtElseIf{ - Cond: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("c")}}, - Stmt: &ast.StmtStmtList{ - Stmts: []ast.Vertex{ - &ast.StmtExpression{ - Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("d")}}, - }, - }, - }, - }, - &ast.StmtElseIf{ - Cond: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("e")}}, - Stmt: &ast.StmtNop{}, - }, - }, - Else: &ast.StmtElse{ - Stmt: &ast.StmtExpression{ - Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("f")}}, - }, - }, - }, - }, - }) - - expected := `namespace { - if ($a) - $b; - elseif ($c) { - $d; - } - elseif ($e); - else - $f; -}` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintIfStmtList(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtNamespace{ - Stmts: []ast.Vertex{ - &ast.StmtIf{ - Cond: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - Stmt: &ast.StmtStmtList{ - Stmts: []ast.Vertex{ - &ast.StmtExpression{ - Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}, - }, - }, - }, - }, - }, - }) - - expected := `namespace { - if ($a) { - $b; - } -}` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintIfNop(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtIf{ - Cond: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, - Stmt: &ast.StmtNop{}, - }) - - expected := `if ($a);` - actual := o.String() - - if expected != actual { - t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) - } -} - -func TestPrintInlineHtml(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, " ") - p.Print(&ast.StmtInlineHtml{ - Value: []byte("test"), - }) - - expected := `?>test