package printer import ( "io" "strings" "github.com/z7zmey/php-parser/pkg/ast" "github.com/z7zmey/php-parser/pkg/token" ) type printerState int const ( PhpState printerState = iota HtmlState ) type Printer struct { w io.Writer s printerState bufStart string } // NewPrinter - Constructor for Printer func NewPrinter(w io.Writer) *Printer { return &Printer{ w: w, } } func (p *Printer) SetState(s printerState) { p.s = s } func (p *Printer) Print(n ast.Vertex) { _, isRoot := n.(*ast.Root) _, isInlineHtml := n.(*ast.StmtInlineHtml) if p.s == HtmlState && !isInlineHtml && !isRoot { if n.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " 0 { io.WriteString(p.w, glue) } p.Print(n) } } func (p *Printer) printNodes(nn []ast.Vertex) { for _, n := range nn { p.Print(n) } } func (p *Printer) printFreeFloatingOrDefault(n ast.Vertex, pos token.Position, def string) { if n == nil { return } if len(n.GetNode().Tokens[pos]) == 0 { io.WriteString(p.w, def) return } for _, m := range n.GetNode().Tokens[pos] { io.WriteString(p.w, string(m.Value)) } } func (p *Printer) printFreeFloating(n ast.Vertex, pos token.Position) { if n == nil { return } for _, m := range n.GetNode().Tokens[pos] { io.WriteString(p.w, string(m.Value)) } } func (p *Printer) printNode(n ast.Vertex) { switch n.(type) { // node case *ast.Root: p.printNodeRoot(n) case *ast.Identifier: p.printNodeIdentifier(n) case *ast.Reference: p.printNodeReference(n) case *ast.Variadic: p.printNodeVariadic(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.printAssign(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.ExprAssignCoalesce: p.printAssignCoalesce(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.ExprArrowFunction: p.printExprArrowFunction(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.ExprShortArray: p.printExprShortArray(n) case *ast.ExprShortList: p.printExprShortList(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.StmtAltElseIf: p.printStmtAltElseIf(n) case *ast.StmtAltElse: p.printStmtAltElse(n) case *ast.StmtAltFor: p.printStmtAltFor(n) case *ast.StmtAltForeach: p.printStmtAltForeach(n) case *ast.StmtAltIf: p.printStmtAltIf(n) case *ast.StmtAltSwitch: p.printStmtAltSwitch(n) case *ast.StmtAltWhile: p.printStmtAltWhile(n) 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.StmtConstList: p.printStmtConstList(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.StmtTraitAdaptationList: p.printStmtTraitAdaptationList(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.StmtGroupUseList: p.printStmtGroupUseList(n) case *ast.StmtUseList: p.printStmtUseList(n) case *ast.StmtUseDeclaration: p.printStmtUseDeclaration(n) case *ast.StmtUseType: p.printStmtUseType(n) case *ast.StmtWhile: p.printStmtWhile(n) case *ast.ParserAs: p.printParserAs(n) case *ast.ParserNsSeparator: p.printParserNsSeparator(n) case *ast.ParserBrackets: p.printParserBrackets(n) } } // node func (p *Printer) printNodeRoot(n ast.Vertex) { nn := n.(*ast.Root) p.SetState(HtmlState) p.printFreeFloating(nn, token.Start) p.printNodes(nn.Stmts) p.printFreeFloating(nn, token.End) } func (p *Printer) printNodeIdentifier(n ast.Vertex) { nn := n.(*ast.Identifier) p.printFreeFloatingOrDefault(nn, token.Start, p.bufStart) p.bufStart = "" io.WriteString(p.w, string(nn.Value)) p.printFreeFloating(nn, token.End) } func (p *Printer) printNodeReference(n ast.Vertex) { nn := n.(*ast.Reference) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "&") p.Print(nn.Var) p.printFreeFloating(nn, token.End) } func (p *Printer) printNodeVariadic(n ast.Vertex) { nn := n.(*ast.Variadic) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "...") p.Print(nn.Var) p.printFreeFloating(nn, token.End) } func (p *Printer) printNodeParameter(n ast.Vertex) { nn := n.(*ast.Parameter) p.printFreeFloating(nn, token.Start) if nn.Type != nil { p.Print(nn.Type) } p.Print(nn.Var) if nn.DefaultValue != nil { io.WriteString(p.w, "=") p.Print(nn.DefaultValue) } p.printFreeFloating(nn, token.End) } func (p *Printer) printNodeNullable(n ast.Vertex) { nn := n.(*ast.Nullable) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "?") p.Print(nn.Expr) p.printFreeFloating(nn, token.End) } func (p *Printer) printNodeArgument(n ast.Vertex) { nn := n.(*ast.Argument) p.printFreeFloating(nn, token.Start) if nn.IsReference { io.WriteString(p.w, "&") } p.printFreeFloating(nn, token.Ampersand) if nn.Variadic { io.WriteString(p.w, "...") } p.Print(nn.Expr) p.printFreeFloating(nn, token.End) } // name func (p *Printer) printNameNamePart(n ast.Vertex) { nn := n.(*ast.NameNamePart) p.printFreeFloatingOrDefault(nn, token.Start, p.bufStart) p.bufStart = "" io.WriteString(p.w, string(nn.Value)) p.printFreeFloating(nn, token.End) } func (p *Printer) printNameName(n ast.Vertex) { nn := n.(*ast.NameName) p.printFreeFloating(nn, token.Start) p.joinPrint("\\", nn.Parts) p.printFreeFloating(nn, token.End) } func (p *Printer) printNameFullyQualified(n ast.Vertex) { nn := n.(*ast.NameFullyQualified) p.printFreeFloatingOrDefault(nn, token.Start, p.bufStart) p.bufStart = "" io.WriteString(p.w, "\\") p.joinPrint("\\", nn.Parts) p.printFreeFloating(nn, token.End) } func (p *Printer) printNameRelative(n ast.Vertex) { nn := n.(*ast.NameRelative) p.printFreeFloatingOrDefault(nn, token.Start, p.bufStart) p.bufStart = "" io.WriteString(p.w, "namespace") p.printFreeFloating(nn, token.Namespace) for _, part := range nn.Parts { io.WriteString(p.w, "\\") p.Print(part) } p.printFreeFloating(nn, token.End) } // scalar func (p *Printer) printScalarLNumber(n ast.Vertex) { nn := n.(*ast.ScalarLnumber) p.printFreeFloatingOrDefault(nn, token.Start, p.bufStart) p.bufStart = "" io.WriteString(p.w, string(nn.Value)) p.printFreeFloating(nn, token.End) } func (p *Printer) printScalarDNumber(n ast.Vertex) { nn := n.(*ast.ScalarDnumber) p.printFreeFloatingOrDefault(nn, token.Start, p.bufStart) p.bufStart = "" io.WriteString(p.w, string(nn.Value)) p.printFreeFloating(nn, token.End) } func (p *Printer) printScalarString(n ast.Vertex) { nn := n.(*ast.ScalarString) p.printFreeFloatingOrDefault(nn, token.Start, p.bufStart) p.bufStart = "" io.WriteString(p.w, string(nn.Value)) p.printFreeFloating(nn, token.End) } func (p *Printer) printScalarEncapsedStringPart(n ast.Vertex) { nn := n.(*ast.ScalarEncapsedStringPart) p.printFreeFloatingOrDefault(nn, token.Start, p.bufStart) p.bufStart = "" io.WriteString(p.w, string(nn.Value)) p.printFreeFloating(nn, token.End) } func (p *Printer) printScalarEncapsed(n ast.Vertex) { nn := n.(*ast.ScalarEncapsed) p.printFreeFloatingOrDefault(nn, token.Start, p.bufStart) p.bufStart = "" io.WriteString(p.w, "\"") for _, part := range nn.Parts { switch part.(type) { case *ast.ExprArrayDimFetch: s := part.GetNode().Tokens[token.Start] if len(s) > 0 && string(s[0].Value) == "${" { p.printExprArrayDimFetchWithoutLeadingDollar(part) } else { p.Print(part) } case *ast.ExprVariable: s := part.GetNode().Tokens[token.Start] if len(s) > 0 && string(s[0].Value) == "${" { p.printExprVariableWithoutLeadingDollar(part) } else { p.Print(part) } default: p.Print(part) } } io.WriteString(p.w, "\"") p.printFreeFloating(nn, token.End) } func (p *Printer) printScalarHeredoc(n ast.Vertex) { nn := n.(*ast.ScalarHeredoc) p.printFreeFloatingOrDefault(nn, token.Start, p.bufStart) p.bufStart = "" io.WriteString(p.w, string(nn.Label)) for _, part := range nn.Parts { switch part.(type) { case *ast.ExprArrayDimFetch: s := part.GetNode().Tokens[token.Start] if len(s) > 0 && string(s[0].Value) == "${" { p.printExprArrayDimFetchWithoutLeadingDollar(part) } else { p.Print(part) } case *ast.ExprVariable: s := part.GetNode().Tokens[token.Start] if len(s) > 0 && string(s[0].Value) == "${" { p.printExprVariableWithoutLeadingDollar(part) } else { p.Print(part) } default: p.Print(part) } } io.WriteString(p.w, strings.Trim(string(nn.Label), "<\"'\n")) p.printFreeFloating(nn, token.End) } func (p *Printer) printScalarMagicConstant(n ast.Vertex) { nn := n.(*ast.ScalarMagicConstant) p.printFreeFloatingOrDefault(nn, token.Start, p.bufStart) p.bufStart = "" io.WriteString(p.w, string(nn.Value)) p.printFreeFloating(nn, token.End) } // Assign func (p *Printer) printAssign(n ast.Vertex) { nn := n.(*ast.ExprAssign) p.printFreeFloating(nn, token.Start) p.Print(nn.Var) p.printFreeFloating(nn, token.Var) io.WriteString(p.w, "=") p.Print(nn.Expr) p.printFreeFloating(nn, token.End) } func (p *Printer) printAssignReference(n ast.Vertex) { nn := n.(*ast.ExprAssignReference) p.printFreeFloating(nn, token.Start) p.Print(nn.Var) p.printFreeFloating(nn, token.Var) io.WriteString(p.w, "=") p.printFreeFloating(nn, token.Equal) io.WriteString(p.w, "&") p.Print(nn.Expr) p.printFreeFloating(nn, token.End) } func (p *Printer) printAssignBitwiseAnd(n ast.Vertex) { nn := n.(*ast.ExprAssignBitwiseAnd) p.printFreeFloating(nn, token.Start) p.Print(nn.Var) p.printFreeFloating(nn, token.Var) io.WriteString(p.w, "&") io.WriteString(p.w, "=") p.Print(nn.Expr) p.printFreeFloating(nn, token.End) } func (p *Printer) printAssignBitwiseOr(n ast.Vertex) { nn := n.(*ast.ExprAssignBitwiseOr) p.printFreeFloating(nn, token.Start) p.Print(nn.Var) p.printFreeFloating(nn, token.Var) io.WriteString(p.w, "|=") p.Print(nn.Expr) p.printFreeFloating(nn, token.End) } func (p *Printer) printAssignBitwiseXor(n ast.Vertex) { nn := n.(*ast.ExprAssignBitwiseXor) p.printFreeFloating(nn, token.Start) p.Print(nn.Var) p.printFreeFloating(nn, token.Var) io.WriteString(p.w, "^=") p.Print(nn.Expr) p.printFreeFloating(nn, token.End) } func (p *Printer) printAssignCoalesce(n ast.Vertex) { nn := n.(*ast.ExprAssignCoalesce) p.printFreeFloating(nn, token.Start) p.Print(nn.Var) p.printFreeFloating(nn, token.Var) io.WriteString(p.w, "??=") p.Print(nn.Expr) p.printFreeFloating(nn, token.End) } func (p *Printer) printAssignConcat(n ast.Vertex) { nn := n.(*ast.ExprAssignConcat) p.printFreeFloating(nn, token.Start) p.Print(nn.Var) p.printFreeFloating(nn, token.Var) io.WriteString(p.w, ".=") p.Print(nn.Expr) p.printFreeFloating(nn, token.End) } func (p *Printer) printAssignDiv(n ast.Vertex) { nn := n.(*ast.ExprAssignDiv) p.printFreeFloating(nn, token.Start) p.Print(nn.Var) p.printFreeFloating(nn, token.Var) io.WriteString(p.w, "/=") p.Print(nn.Expr) p.printFreeFloating(nn, token.End) } func (p *Printer) printAssignMinus(n ast.Vertex) { nn := n.(*ast.ExprAssignMinus) p.printFreeFloating(nn, token.Start) p.Print(nn.Var) p.printFreeFloating(nn, token.Var) io.WriteString(p.w, "-=") p.Print(nn.Expr) p.printFreeFloating(nn, token.End) } func (p *Printer) printAssignMod(n ast.Vertex) { nn := n.(*ast.ExprAssignMod) p.printFreeFloating(nn, token.Start) p.Print(nn.Var) p.printFreeFloating(nn, token.Var) io.WriteString(p.w, "%=") p.Print(nn.Expr) p.printFreeFloating(nn, token.End) } func (p *Printer) printAssignMul(n ast.Vertex) { nn := n.(*ast.ExprAssignMul) p.printFreeFloating(nn, token.Start) p.Print(nn.Var) p.printFreeFloating(nn, token.Var) io.WriteString(p.w, "*=") p.Print(nn.Expr) p.printFreeFloating(nn, token.End) } func (p *Printer) printAssignPlus(n ast.Vertex) { nn := n.(*ast.ExprAssignPlus) p.printFreeFloating(nn, token.Start) p.Print(nn.Var) p.printFreeFloating(nn, token.Var) io.WriteString(p.w, "+=") p.Print(nn.Expr) p.printFreeFloating(nn, token.End) } func (p *Printer) printAssignPow(n ast.Vertex) { nn := n.(*ast.ExprAssignPow) p.printFreeFloating(nn, token.Start) p.Print(nn.Var) p.printFreeFloating(nn, token.Var) io.WriteString(p.w, "**=") p.Print(nn.Expr) p.printFreeFloating(nn, token.End) } func (p *Printer) printAssignShiftLeft(n ast.Vertex) { nn := n.(*ast.ExprAssignShiftLeft) p.printFreeFloating(nn, token.Start) p.Print(nn.Var) p.printFreeFloating(nn, token.Var) io.WriteString(p.w, "<<=") p.Print(nn.Expr) p.printFreeFloating(nn, token.End) } func (p *Printer) printAssignShiftRight(n ast.Vertex) { nn := n.(*ast.ExprAssignShiftRight) p.printFreeFloating(nn, token.Start) p.Print(nn.Var) p.printFreeFloating(nn, token.Var) io.WriteString(p.w, ">>=") p.Print(nn.Expr) p.printFreeFloating(nn, token.End) } // binary func (p *Printer) printBinaryBitwiseAnd(n ast.Vertex) { nn := n.(*ast.ExprBinaryBitwiseAnd) p.printFreeFloating(nn, token.Start) p.Print(nn.Left) p.printFreeFloating(nn, token.Expr) io.WriteString(p.w, "&") p.Print(nn.Right) p.printFreeFloating(nn, token.End) } func (p *Printer) printBinaryBitwiseOr(n ast.Vertex) { nn := n.(*ast.ExprBinaryBitwiseOr) p.printFreeFloating(nn, token.Start) p.Print(nn.Left) p.printFreeFloating(nn, token.Expr) io.WriteString(p.w, "|") p.Print(nn.Right) p.printFreeFloating(nn, token.End) } func (p *Printer) printBinaryBitwiseXor(n ast.Vertex) { nn := n.(*ast.ExprBinaryBitwiseXor) p.printFreeFloating(nn, token.Start) p.Print(nn.Left) p.printFreeFloating(nn, token.Expr) io.WriteString(p.w, "^") p.Print(nn.Right) p.printFreeFloating(nn, token.End) } func (p *Printer) printBinaryBooleanAnd(n ast.Vertex) { nn := n.(*ast.ExprBinaryBooleanAnd) p.printFreeFloating(nn, token.Start) p.Print(nn.Left) p.printFreeFloating(nn, token.Expr) io.WriteString(p.w, "&&") p.Print(nn.Right) p.printFreeFloating(nn, token.End) } func (p *Printer) printBinaryBooleanOr(n ast.Vertex) { nn := n.(*ast.ExprBinaryBooleanOr) p.printFreeFloating(nn, token.Start) p.Print(nn.Left) p.printFreeFloating(nn, token.Expr) io.WriteString(p.w, "||") p.Print(nn.Right) p.printFreeFloating(nn, token.End) } func (p *Printer) printBinaryCoalesce(n ast.Vertex) { nn := n.(*ast.ExprBinaryCoalesce) p.printFreeFloating(nn, token.Start) p.Print(nn.Left) p.printFreeFloating(nn, token.Expr) io.WriteString(p.w, "??") p.Print(nn.Right) p.printFreeFloating(nn, token.End) } func (p *Printer) printBinaryConcat(n ast.Vertex) { nn := n.(*ast.ExprBinaryConcat) p.printFreeFloating(nn, token.Start) p.Print(nn.Left) p.printFreeFloating(nn, token.Expr) io.WriteString(p.w, ".") p.Print(nn.Right) p.printFreeFloating(nn, token.End) } func (p *Printer) printBinaryDiv(n ast.Vertex) { nn := n.(*ast.ExprBinaryDiv) p.printFreeFloating(nn, token.Start) p.Print(nn.Left) p.printFreeFloating(nn, token.Expr) io.WriteString(p.w, "/") p.Print(nn.Right) p.printFreeFloating(nn, token.End) } func (p *Printer) printBinaryEqual(n ast.Vertex) { nn := n.(*ast.ExprBinaryEqual) p.printFreeFloating(nn, token.Start) p.Print(nn.Left) p.printFreeFloating(nn, token.Expr) io.WriteString(p.w, "==") p.Print(nn.Right) p.printFreeFloating(nn, token.End) } func (p *Printer) printBinaryGreaterOrEqual(n ast.Vertex) { nn := n.(*ast.ExprBinaryGreaterOrEqual) p.printFreeFloating(nn, token.Start) p.Print(nn.Left) p.printFreeFloating(nn, token.Expr) io.WriteString(p.w, ">=") p.Print(nn.Right) p.printFreeFloating(nn, token.End) } func (p *Printer) printBinaryGreater(n ast.Vertex) { nn := n.(*ast.ExprBinaryGreater) p.printFreeFloating(nn, token.Start) p.Print(nn.Left) p.printFreeFloating(nn, token.Expr) io.WriteString(p.w, ">") p.Print(nn.Right) p.printFreeFloating(nn, token.End) } func (p *Printer) printBinaryIdentical(n ast.Vertex) { nn := n.(*ast.ExprBinaryIdentical) p.printFreeFloating(nn, token.Start) p.Print(nn.Left) p.printFreeFloating(nn, token.Expr) io.WriteString(p.w, "===") p.Print(nn.Right) p.printFreeFloating(nn, token.End) } func (p *Printer) printBinaryLogicalAnd(n ast.Vertex) { nn := n.(*ast.ExprBinaryLogicalAnd) p.printFreeFloating(nn, token.Start) p.Print(nn.Left) p.printFreeFloating(nn, token.Expr) if nn.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } io.WriteString(p.w, "and") if nn.Right.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } p.Print(nn.Right) p.printFreeFloating(nn, token.End) } func (p *Printer) printBinaryLogicalOr(n ast.Vertex) { nn := n.(*ast.ExprBinaryLogicalOr) p.printFreeFloating(nn, token.Start) p.Print(nn.Left) p.printFreeFloating(nn, token.Expr) if nn.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } io.WriteString(p.w, "or") if nn.Right.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } p.Print(nn.Right) p.printFreeFloating(nn, token.End) } func (p *Printer) printBinaryLogicalXor(n ast.Vertex) { nn := n.(*ast.ExprBinaryLogicalXor) p.printFreeFloating(nn, token.Start) p.Print(nn.Left) p.printFreeFloating(nn, token.Expr) if nn.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } io.WriteString(p.w, "xor") if nn.Right.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } p.Print(nn.Right) p.printFreeFloating(nn, token.End) } func (p *Printer) printBinaryMinus(n ast.Vertex) { nn := n.(*ast.ExprBinaryMinus) p.printFreeFloating(nn, token.Start) p.Print(nn.Left) p.printFreeFloating(nn, token.Expr) io.WriteString(p.w, "-") p.Print(nn.Right) p.printFreeFloating(nn, token.End) } func (p *Printer) printBinaryMod(n ast.Vertex) { nn := n.(*ast.ExprBinaryMod) p.printFreeFloating(nn, token.Start) p.Print(nn.Left) p.printFreeFloating(nn, token.Expr) io.WriteString(p.w, "%") p.Print(nn.Right) p.printFreeFloating(nn, token.End) } func (p *Printer) printBinaryMul(n ast.Vertex) { nn := n.(*ast.ExprBinaryMul) p.printFreeFloating(nn, token.Start) p.Print(nn.Left) p.printFreeFloating(nn, token.Expr) io.WriteString(p.w, "*") p.Print(nn.Right) p.printFreeFloating(nn, token.End) } func (p *Printer) printBinaryNotEqual(n ast.Vertex) { nn := n.(*ast.ExprBinaryNotEqual) p.printFreeFloating(nn, token.Start) p.Print(nn.Left) p.printFreeFloating(nn, token.Expr) p.printFreeFloating(nn, token.Equal) if nn.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, "!=") } p.Print(nn.Right) p.printFreeFloating(nn, token.End) } func (p *Printer) printBinaryNotIdentical(n ast.Vertex) { nn := n.(*ast.ExprBinaryNotIdentical) p.printFreeFloating(nn, token.Start) p.Print(nn.Left) p.printFreeFloating(nn, token.Expr) io.WriteString(p.w, "!==") p.Print(nn.Right) p.printFreeFloating(nn, token.End) } func (p *Printer) printBinaryPlus(n ast.Vertex) { nn := n.(*ast.ExprBinaryPlus) p.printFreeFloating(nn, token.Start) p.Print(nn.Left) p.printFreeFloating(nn, token.Expr) io.WriteString(p.w, "+") p.Print(nn.Right) p.printFreeFloating(nn, token.End) } func (p *Printer) printBinaryPow(n ast.Vertex) { nn := n.(*ast.ExprBinaryPow) p.printFreeFloating(nn, token.Start) p.Print(nn.Left) p.printFreeFloating(nn, token.Expr) io.WriteString(p.w, "**") p.Print(nn.Right) p.printFreeFloating(nn, token.End) } func (p *Printer) printBinaryShiftLeft(n ast.Vertex) { nn := n.(*ast.ExprBinaryShiftLeft) p.printFreeFloating(nn, token.Start) p.Print(nn.Left) p.printFreeFloating(nn, token.Expr) io.WriteString(p.w, "<<") p.Print(nn.Right) p.printFreeFloating(nn, token.End) } func (p *Printer) printBinaryShiftRight(n ast.Vertex) { nn := n.(*ast.ExprBinaryShiftRight) p.printFreeFloating(nn, token.Start) p.Print(nn.Left) p.printFreeFloating(nn, token.Expr) io.WriteString(p.w, ">>") p.Print(nn.Right) p.printFreeFloating(nn, token.End) } func (p *Printer) printBinarySmallerOrEqual(n ast.Vertex) { nn := n.(*ast.ExprBinarySmallerOrEqual) p.printFreeFloating(nn, token.Start) p.Print(nn.Left) p.printFreeFloating(nn, token.Expr) io.WriteString(p.w, "<=") p.Print(nn.Right) p.printFreeFloating(nn, token.End) } func (p *Printer) printBinarySmaller(n ast.Vertex) { nn := n.(*ast.ExprBinarySmaller) p.printFreeFloating(nn, token.Start) p.Print(nn.Left) p.printFreeFloating(nn, token.Expr) io.WriteString(p.w, "<") p.Print(nn.Right) p.printFreeFloating(nn, token.End) } func (p *Printer) printBinarySpaceship(n ast.Vertex) { nn := n.(*ast.ExprBinarySpaceship) p.printFreeFloating(nn, token.Start) p.Print(nn.Left) p.printFreeFloating(nn, token.Expr) io.WriteString(p.w, "<=>") p.Print(nn.Right) p.printFreeFloating(nn, token.End) } // cast func (p *Printer) printArray(n ast.Vertex) { nn := n.(*ast.ExprCastArray) p.printFreeFloating(nn, token.Start) p.printFreeFloating(nn, token.Cast) if nn.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, "(array)") } p.Print(nn.Expr) p.printFreeFloating(nn, token.End) } func (p *Printer) printBool(n ast.Vertex) { nn := n.(*ast.ExprCastBool) p.printFreeFloating(nn, token.Start) p.printFreeFloating(nn, token.Cast) if nn.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, "(boolean)") } p.Print(nn.Expr) p.printFreeFloating(nn, token.End) } func (p *Printer) printDouble(n ast.Vertex) { nn := n.(*ast.ExprCastDouble) p.printFreeFloating(nn, token.Start) p.printFreeFloating(nn, token.Cast) if nn.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, "(float)") } p.Print(nn.Expr) p.printFreeFloating(nn, token.End) } func (p *Printer) printInt(n ast.Vertex) { nn := n.(*ast.ExprCastInt) p.printFreeFloating(nn, token.Start) p.printFreeFloating(nn, token.Cast) if nn.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, "(integer)") } p.Print(nn.Expr) p.printFreeFloating(nn, token.End) } func (p *Printer) printObject(n ast.Vertex) { nn := n.(*ast.ExprCastObject) p.printFreeFloating(nn, token.Start) p.printFreeFloating(nn, token.Cast) if nn.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, "(object)") } p.Print(nn.Expr) p.printFreeFloating(nn, token.End) } func (p *Printer) printString(n ast.Vertex) { nn := n.(*ast.ExprCastString) p.printFreeFloating(nn, token.Start) p.printFreeFloating(nn, token.Cast) if nn.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, "(string)") } p.Print(nn.Expr) p.printFreeFloating(nn, token.End) } func (p *Printer) printUnset(n ast.Vertex) { nn := n.(*ast.ExprCastUnset) p.printFreeFloating(nn, token.Start) p.printFreeFloating(nn, token.Cast) if nn.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, "(unset)") } p.Print(nn.Expr) p.printFreeFloating(nn, token.End) } // expr func (p *Printer) printExprArrayDimFetch(n ast.Vertex) { nn := n.(*ast.ExprArrayDimFetch) p.printFreeFloating(nn, token.Start) p.Print(nn.Var) p.printFreeFloating(nn, token.Var) if nn.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, "[") } p.Print(nn.Dim) p.printFreeFloating(nn, token.Expr) if nn.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, "]") } p.printFreeFloating(nn, token.End) } func (p *Printer) printExprArrayDimFetchWithoutLeadingDollar(n ast.Vertex) { nn := n.(*ast.ExprArrayDimFetch) p.printFreeFloating(nn, token.Start) p.printExprVariableWithoutLeadingDollar(nn.Var) p.printFreeFloating(nn, token.Var) if nn.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, "[") } p.Print(nn.Dim) p.printFreeFloating(nn, token.Expr) if nn.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, "]") } p.printFreeFloating(nn, token.End) } func (p *Printer) printExprArrayItem(n ast.Vertex) { nn := n.(*ast.ExprArrayItem) p.printFreeFloating(nn, token.Start) if nn.Unpack { io.WriteString(p.w, "...") } if nn.Key != nil { p.Print(nn.Key) p.printFreeFloating(nn, token.Expr) io.WriteString(p.w, "=>") } p.Print(nn.Val) p.printFreeFloating(nn, token.End) } func (p *Printer) printExprArray(n ast.Vertex) { nn := n.(*ast.ExprArray) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "array") p.printFreeFloating(nn, token.Array) io.WriteString(p.w, "(") p.joinPrint(",", nn.Items) p.printFreeFloating(nn, token.ArrayPairList) io.WriteString(p.w, ")") p.printFreeFloating(nn, token.End) } func (p *Printer) printExprArrowFunction(n ast.Vertex) { nn := n.(*ast.ExprArrowFunction) p.printFreeFloating(nn, token.Start) if nn.Static { io.WriteString(p.w, "static") } p.printFreeFloating(nn, token.Static) if nn.Static && n.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } io.WriteString(p.w, "fn") p.printFreeFloating(nn, token.Function) if nn.ReturnsRef { io.WriteString(p.w, "&") } p.printFreeFloating(nn, token.Ampersand) io.WriteString(p.w, "(") p.joinPrint(",", nn.Params) p.printFreeFloating(nn, token.ParameterList) io.WriteString(p.w, ")") p.printFreeFloating(nn, token.Params) if nn.ReturnType != nil { p.bufStart = ":" p.Print(nn.ReturnType) } p.printFreeFloating(nn, token.ReturnType) io.WriteString(p.w, "=>") p.printNode(nn.Expr) p.printFreeFloating(nn, token.End) } func (p *Printer) printExprBitwiseNot(n ast.Vertex) { nn := n.(*ast.ExprBitwiseNot) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "~") p.Print(nn.Expr) p.printFreeFloating(nn, token.End) } func (p *Printer) printExprBooleanNot(n ast.Vertex) { nn := n.(*ast.ExprBooleanNot) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "!") p.Print(nn.Expr) p.printFreeFloating(nn, token.End) } func (p *Printer) printExprClassConstFetch(n ast.Vertex) { nn := n.(*ast.ExprClassConstFetch) p.printFreeFloating(nn, token.Start) p.Print(nn.Class) p.printFreeFloating(nn, token.Name) io.WriteString(p.w, "::") p.Print(nn.ConstantName) p.printFreeFloating(nn, token.End) } func (p *Printer) printExprClone(n ast.Vertex) { nn := n.(*ast.ExprClone) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "clone") if nn.Expr.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } p.Print(nn.Expr) p.printFreeFloating(nn, token.End) } func (p *Printer) printExprClosureUse(n ast.Vertex) { nn := n.(*ast.ExprClosureUse) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "use") p.printFreeFloating(nn, token.Use) io.WriteString(p.w, "(") p.joinPrint(",", nn.Uses) p.printFreeFloating(nn, token.LexicalVarList) io.WriteString(p.w, ")") p.printFreeFloating(nn, token.End) } func (p *Printer) printExprClosure(n ast.Vertex) { nn := n.(*ast.ExprClosure) p.printFreeFloating(nn, token.Start) if nn.Static { io.WriteString(p.w, "static") } p.printFreeFloating(nn, token.Static) if nn.Static && n.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } io.WriteString(p.w, "function") p.printFreeFloating(nn, token.Function) if nn.ReturnsRef { io.WriteString(p.w, "&") } p.printFreeFloating(nn, token.Ampersand) io.WriteString(p.w, "(") p.joinPrint(",", nn.Params) p.printFreeFloating(nn, token.ParameterList) io.WriteString(p.w, ")") p.printFreeFloating(nn, token.Params) if nn.ClosureUse != nil { p.Print(nn.ClosureUse) } p.printFreeFloating(nn, token.LexicalVars) if nn.ReturnType != nil { p.bufStart = ":" p.Print(nn.ReturnType) } p.printFreeFloating(nn, token.ReturnType) io.WriteString(p.w, "{") p.printNodes(nn.Stmts) p.printFreeFloating(nn, token.Stmts) io.WriteString(p.w, "}") p.printFreeFloating(nn, token.End) } func (p *Printer) printExprConstFetch(n ast.Vertex) { nn := n.(*ast.ExprConstFetch) p.printFreeFloating(nn, token.Start) p.Print(nn.Const) p.printFreeFloating(nn, token.End) } func (p *Printer) printExprEmpty(n ast.Vertex) { nn := n.(*ast.ExprEmpty) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "empty") if _, ok := nn.Expr.(*ast.ParserBrackets); !ok { io.WriteString(p.w, "(") } p.Print(nn.Expr) if _, ok := nn.Expr.(*ast.ParserBrackets); !ok { io.WriteString(p.w, ")") } p.printFreeFloating(nn, token.End) } func (p *Printer) printExprErrorSuppress(n ast.Vertex) { nn := n.(*ast.ExprErrorSuppress) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "@") p.Print(nn.Expr) p.printFreeFloating(nn, token.End) } func (p *Printer) printExprEval(n ast.Vertex) { nn := n.(*ast.ExprEval) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "eval") if _, ok := nn.Expr.(*ast.ParserBrackets); !ok { io.WriteString(p.w, "(") } p.Print(nn.Expr) if _, ok := nn.Expr.(*ast.ParserBrackets); !ok { io.WriteString(p.w, ")") } p.printFreeFloating(nn, token.End) } func (p *Printer) printExprExit(n ast.Vertex) { nn := n.(*ast.ExprExit) p.printFreeFloating(nn, token.Start) if nn.Die { io.WriteString(p.w, "die") } else { io.WriteString(p.w, "exit") } if nn.Expr != nil && nn.Expr.GetNode().Tokens.IsEmpty() && nn.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } p.Print(nn.Expr) p.printFreeFloating(nn, token.Expr) p.printFreeFloating(nn, token.End) } func (p *Printer) printExprFunctionCall(n ast.Vertex) { nn := n.(*ast.ExprFunctionCall) p.printFreeFloating(nn, token.Start) p.Print(nn.Function) p.printFreeFloatingOrDefault(nn.ArgumentList, token.Start, "(") p.joinPrint(",", nn.ArgumentList.Arguments) p.printFreeFloatingOrDefault(nn.ArgumentList, token.End, ")") p.printFreeFloating(nn, token.End) } func (p *Printer) printExprInclude(n ast.Vertex) { nn := n.(*ast.ExprInclude) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "include") if nn.Expr.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } p.Print(nn.Expr) p.printFreeFloating(nn, token.End) } func (p *Printer) printExprIncludeOnce(n ast.Vertex) { nn := n.(*ast.ExprIncludeOnce) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "include_once") if nn.Expr.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } p.Print(nn.Expr) p.printFreeFloating(nn, token.End) } func (p *Printer) printExprInstanceOf(n ast.Vertex) { nn := n.(*ast.ExprInstanceOf) p.printFreeFloating(nn, token.Start) p.Print(nn.Expr) p.printFreeFloating(nn, token.Expr) if nn.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } io.WriteString(p.w, "instanceof") p.bufStart = " " p.Print(nn.Class) p.printFreeFloating(nn, token.End) } func (p *Printer) printExprIsset(n ast.Vertex) { nn := n.(*ast.ExprIsset) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "isset") p.printFreeFloating(nn, token.Isset) io.WriteString(p.w, "(") p.joinPrint(",", nn.Vars) p.printFreeFloating(nn, token.VarList) io.WriteString(p.w, ")") p.printFreeFloating(nn, token.End) } func (p *Printer) printExprList(n ast.Vertex) { nn := n.(*ast.ExprList) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "list") p.printFreeFloating(nn, token.List) io.WriteString(p.w, "(") p.joinPrint(",", nn.Items) p.printFreeFloating(nn, token.ArrayPairList) io.WriteString(p.w, ")") p.printFreeFloating(nn, token.End) } func (p *Printer) printExprMethodCall(n ast.Vertex) { nn := n.(*ast.ExprMethodCall) p.printFreeFloating(nn, token.Start) p.Print(nn.Var) p.printFreeFloating(nn, token.Var) io.WriteString(p.w, "->") p.Print(nn.Method) p.printFreeFloatingOrDefault(nn.ArgumentList, token.Start, "(") p.joinPrint(",", nn.ArgumentList.Arguments) p.printFreeFloatingOrDefault(nn.ArgumentList, token.End, ")") p.printFreeFloating(nn, token.End) } func (p *Printer) printExprNew(n ast.Vertex) { nn := n.(*ast.ExprNew) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "new") p.bufStart = " " p.Print(nn.Class) if nn.ArgumentList != nil { p.printFreeFloatingOrDefault(nn.ArgumentList, token.Start, "(") p.joinPrint(",", nn.ArgumentList.Arguments) p.printFreeFloatingOrDefault(nn.ArgumentList, token.End, ")") } p.printFreeFloating(nn, token.End) } func (p *Printer) printExprPostDec(n ast.Vertex) { nn := n.(*ast.ExprPostDec) p.printFreeFloating(nn, token.Start) p.Print(nn.Var) p.printFreeFloating(nn, token.Var) io.WriteString(p.w, "--") p.printFreeFloating(nn, token.End) } func (p *Printer) printExprPostInc(n ast.Vertex) { nn := n.(*ast.ExprPostInc) p.printFreeFloating(nn, token.Start) p.Print(nn.Var) p.printFreeFloating(nn, token.Var) io.WriteString(p.w, "++") p.printFreeFloating(nn, token.End) } func (p *Printer) printExprPreDec(n ast.Vertex) { nn := n.(*ast.ExprPreDec) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "--") p.Print(nn.Var) p.printFreeFloating(nn, token.End) } func (p *Printer) printExprPreInc(n ast.Vertex) { nn := n.(*ast.ExprPreInc) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "++") p.Print(nn.Var) p.printFreeFloating(nn, token.End) } func (p *Printer) printExprPrint(n ast.Vertex) { nn := n.(*ast.ExprPrint) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "print") if nn.Expr.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } p.Print(nn.Expr) p.printFreeFloating(nn, token.End) } func (p *Printer) printExprPropertyFetch(n ast.Vertex) { nn := n.(*ast.ExprPropertyFetch) p.printFreeFloating(nn, token.Start) p.Print(nn.Var) p.printFreeFloating(nn, token.Var) io.WriteString(p.w, "->") p.Print(nn.Property) p.printFreeFloating(nn, token.End) } func (p *Printer) printExprReference(n ast.Vertex) { nn := n.(*ast.ExprReference) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "&") p.Print(nn.Var) p.printFreeFloating(nn, token.End) } func (p *Printer) printExprRequire(n ast.Vertex) { nn := n.(*ast.ExprRequire) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "require") if nn.Expr.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } p.Print(nn.Expr) p.printFreeFloating(nn, token.End) } func (p *Printer) printExprRequireOnce(n ast.Vertex) { nn := n.(*ast.ExprRequireOnce) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "require_once") if nn.Expr.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } p.Print(nn.Expr) p.printFreeFloating(nn, token.End) } func (p *Printer) printExprShellExec(n ast.Vertex) { nn := n.(*ast.ExprShellExec) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "`") p.joinPrint("", nn.Parts) io.WriteString(p.w, "`") p.printFreeFloating(nn, token.End) } func (p *Printer) printExprShortArray(n ast.Vertex) { nn := n.(*ast.ExprShortArray) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "[") p.joinPrint(",", nn.Items) p.printFreeFloating(nn, token.ArrayPairList) io.WriteString(p.w, "]") p.printFreeFloating(nn, token.End) } func (p *Printer) printExprShortList(n ast.Vertex) { nn := n.(*ast.ExprShortList) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "[") p.joinPrint(",", nn.Items) p.printFreeFloating(nn, token.ArrayPairList) io.WriteString(p.w, "]") p.printFreeFloating(nn, token.End) } func (p *Printer) printExprStaticCall(n ast.Vertex) { nn := n.(*ast.ExprStaticCall) p.printFreeFloating(nn, token.Start) p.Print(nn.Class) p.printFreeFloating(nn, token.Name) io.WriteString(p.w, "::") p.Print(nn.Call) p.printFreeFloatingOrDefault(nn.ArgumentList, token.Start, "(") p.joinPrint(",", nn.ArgumentList.Arguments) p.printFreeFloatingOrDefault(nn.ArgumentList, token.End, ")") p.printFreeFloating(nn, token.End) } func (p *Printer) printExprStaticPropertyFetch(n ast.Vertex) { nn := n.(*ast.ExprStaticPropertyFetch) p.printFreeFloating(nn, token.Start) p.Print(nn.Class) p.printFreeFloating(nn, token.Name) io.WriteString(p.w, "::") p.Print(nn.Property) p.printFreeFloating(nn, token.End) } func (p *Printer) printExprTernary(n ast.Vertex) { nn := n.(*ast.ExprTernary) p.printFreeFloating(nn, token.Start) p.Print(nn.Condition) p.printFreeFloating(nn, token.Cond) io.WriteString(p.w, "?") if nn.IfTrue != nil { p.Print(nn.IfTrue) } p.printFreeFloating(nn, token.True) io.WriteString(p.w, ":") p.Print(nn.IfFalse) p.printFreeFloating(nn, token.End) } func (p *Printer) printExprUnaryMinus(n ast.Vertex) { nn := n.(*ast.ExprUnaryMinus) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "-") p.Print(nn.Expr) p.printFreeFloating(nn, token.End) } func (p *Printer) printExprUnaryPlus(n ast.Vertex) { nn := n.(*ast.ExprUnaryPlus) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "+") p.Print(nn.Expr) p.printFreeFloating(nn, token.End) } func (p *Printer) printExprVariable(n ast.Vertex) { nn := n.(*ast.ExprVariable) p.printFreeFloatingOrDefault(nn, token.Start, p.bufStart) p.bufStart = "" if _, ok := nn.VarName.(*ast.Identifier); !ok { io.WriteString(p.w, "$") } p.Print(nn.VarName) p.printFreeFloating(nn, token.End) } func (p *Printer) printExprVariableWithoutLeadingDollar(n ast.Vertex) { nn := n.(*ast.ExprVariable) p.printFreeFloating(nn, token.Start) p.Print(nn.VarName) p.printFreeFloating(nn, token.End) } func (p *Printer) printExprYieldFrom(n ast.Vertex) { nn := n.(*ast.ExprYieldFrom) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "yield from") if nn.Expr.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } p.Print(nn.Expr) p.printFreeFloating(nn, token.End) } func (p *Printer) printExprYield(n ast.Vertex) { nn := n.(*ast.ExprYield) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "yield") if nn.Key != nil { if nn.Key.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } p.Print(nn.Key) p.printFreeFloating(nn, token.Expr) io.WriteString(p.w, "=>") } else { if nn.Value.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } } p.Print(nn.Value) p.printFreeFloating(nn, token.End) } // smtm func (p *Printer) printStmtAltElseIf(n ast.Vertex) { nn := n.(*ast.StmtAltElseIf) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "elseif") if _, ok := nn.Cond.(*ast.ParserBrackets); !ok { io.WriteString(p.w, "(") } p.Print(nn.Cond) if _, ok := nn.Cond.(*ast.ParserBrackets); !ok { io.WriteString(p.w, ")") } stmtList, _ := nn.Stmt.(*ast.StmtStmtList) brackets, ok := nn.Stmt.(*ast.ParserBrackets) if ok { p.printFreeFloating(brackets, token.Start) stmtList = brackets.Child.(*ast.StmtStmtList) } else { io.WriteString(p.w, ":") } p.printFreeFloating(stmtList, token.Stmts) p.printNodes(stmtList.Stmts) p.printFreeFloating(stmtList, token.End) if ok { p.printFreeFloating(brackets, token.End) } p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtAltElse(n ast.Vertex) { nn := n.(*ast.StmtAltElse) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "else") stmtList, _ := nn.Stmt.(*ast.StmtStmtList) brackets, ok := nn.Stmt.(*ast.ParserBrackets) if ok { p.printFreeFloating(brackets, token.Start) stmtList = brackets.Child.(*ast.StmtStmtList) } else { io.WriteString(p.w, ":") } p.printFreeFloating(stmtList, token.Stmts) p.printNodes(stmtList.Stmts) p.printFreeFloating(stmtList, token.End) if ok { p.printFreeFloating(brackets, token.End) } p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtAltFor(n ast.Vertex) { nn := n.(*ast.StmtAltFor) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "for") p.printFreeFloating(nn, token.For) io.WriteString(p.w, "(") p.joinPrint(",", nn.Init) p.printFreeFloating(nn, token.InitExpr) io.WriteString(p.w, ";") p.joinPrint(",", nn.Cond) p.printFreeFloating(nn, token.CondExpr) io.WriteString(p.w, ";") p.joinPrint(",", nn.Loop) p.printFreeFloating(nn, token.IncExpr) io.WriteString(p.w, ")") p.printFreeFloating(nn, token.Cond) io.WriteString(p.w, ":") s := nn.Stmt.(*ast.StmtStmtList) p.printNodes(s.Stmts) p.printFreeFloating(nn, token.Stmts) io.WriteString(p.w, "endfor") p.printFreeFloating(nn, token.AltEnd) p.printFreeFloating(nn, token.SemiColon) if nn.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, ";") } p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtAltForeach(n ast.Vertex) { nn := n.(*ast.StmtAltForeach) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "foreach") p.printFreeFloating(nn, token.Foreach) io.WriteString(p.w, "(") p.Print(nn.Expr) p.printFreeFloating(nn, token.Expr) if nn.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } io.WriteString(p.w, "as") if nn.Key != nil { if nn.Key.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } p.Print(nn.Key) p.printFreeFloating(nn, token.Key) io.WriteString(p.w, "=>") } else { if nn.Var.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } } p.Print(nn.Var) p.printFreeFloating(nn, token.Var) io.WriteString(p.w, ")") p.printFreeFloating(nn, token.Cond) io.WriteString(p.w, ":") s := nn.Stmt.(*ast.StmtStmtList) p.printNodes(s.Stmts) p.printFreeFloating(nn, token.Stmts) io.WriteString(p.w, "endforeach") p.printFreeFloating(nn, token.AltEnd) p.printFreeFloating(nn, token.SemiColon) if nn.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, ";") } p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtAltIf(n ast.Vertex) { nn := n.(*ast.StmtAltIf) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "if") if _, ok := nn.Cond.(*ast.ParserBrackets); !ok { io.WriteString(p.w, "(") } p.Print(nn.Cond) if _, ok := nn.Cond.(*ast.ParserBrackets); !ok { io.WriteString(p.w, ")") } stmtList, _ := nn.Stmt.(*ast.StmtStmtList) brackets, ok := nn.Stmt.(*ast.ParserBrackets) if ok { p.printFreeFloating(brackets, token.Start) stmtList = brackets.Child.(*ast.StmtStmtList) } else { io.WriteString(p.w, ":") } p.printFreeFloating(stmtList, token.Stmts) p.printNodes(stmtList.Stmts) p.printFreeFloating(stmtList, token.End) if ok { p.printFreeFloating(brackets, token.End) } for _, elseif := range nn.ElseIf { p.Print(elseif) } if nn.Else != nil { p.Print(nn.Else) } if !ok { io.WriteString(p.w, "endif") } p.printFreeFloating(nn, token.SemiColon) if nn.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, ";") } p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtAltSwitch(n ast.Vertex) { nn := n.(*ast.StmtAltSwitch) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "switch") if _, ok := nn.Cond.(*ast.ParserBrackets); !ok { io.WriteString(p.w, "(") } p.Print(nn.Cond) if _, ok := nn.Cond.(*ast.ParserBrackets); !ok { io.WriteString(p.w, ")") } p.printFreeFloating(nn, token.Cond) io.WriteString(p.w, ":") p.printFreeFloating(nn.CaseList, token.Start) p.printFreeFloating(nn.CaseList, token.CaseListStart) p.printNodes(nn.CaseList.Cases) p.printFreeFloating(nn.CaseList, token.CaseListEnd) p.printFreeFloating(nn.CaseList, token.End) io.WriteString(p.w, "endswitch") p.printFreeFloating(nn, token.AltEnd) p.printFreeFloating(nn, token.SemiColon) if nn.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, ";") } p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtAltWhile(n ast.Vertex) { nn := n.(*ast.StmtAltWhile) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "while") if _, ok := nn.Cond.(*ast.ParserBrackets); !ok { io.WriteString(p.w, "(") } p.Print(nn.Cond) if _, ok := nn.Cond.(*ast.ParserBrackets); !ok { io.WriteString(p.w, ")") } p.printFreeFloating(nn, token.Cond) io.WriteString(p.w, ":") s := nn.Stmt.(*ast.StmtStmtList) p.printNodes(s.Stmts) p.printFreeFloating(nn, token.Stmts) io.WriteString(p.w, "endwhile") p.printFreeFloating(nn, token.AltEnd) p.printFreeFloating(nn, token.SemiColon) if nn.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, ";") } p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtBreak(n ast.Vertex) { nn := n.(*ast.StmtBreak) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "break") if nn.Expr != nil { if nn.Expr.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } p.Print(nn.Expr) } p.printFreeFloating(nn, token.Expr) p.printFreeFloating(nn, token.SemiColon) if nn.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, ";") } p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtCase(n ast.Vertex) { nn := n.(*ast.StmtCase) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "case") if nn.Cond.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } p.Print(nn.Cond) p.printFreeFloating(nn, token.Expr) p.printFreeFloating(nn, token.CaseSeparator) if nn.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, ":") } if len(nn.Stmts) > 0 { p.printNodes(nn.Stmts) } p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtCatch(n ast.Vertex) { nn := n.(*ast.StmtCatch) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "catch") p.printFreeFloating(nn, token.Catch) io.WriteString(p.w, "(") p.joinPrint("|", nn.Types) p.Print(nn.Var) p.printFreeFloating(nn, token.Var) io.WriteString(p.w, ")") p.printFreeFloating(nn, token.Cond) io.WriteString(p.w, "{") p.printNodes(nn.Stmts) p.printFreeFloating(nn, token.Stmts) io.WriteString(p.w, "}") p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtClassMethod(n ast.Vertex) { nn := n.(*ast.StmtClassMethod) p.printFreeFloating(nn, token.Start) if nn.Modifiers != nil { for k, m := range nn.Modifiers { if k > 0 && m.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } p.Print(m) } if nn.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } } p.printFreeFloating(nn, token.ModifierList) io.WriteString(p.w, "function") p.printFreeFloating(nn, token.Function) if nn.ReturnsRef { if nn.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } io.WriteString(p.w, "&") p.printFreeFloating(nn, token.Ampersand) } else { if nn.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } } p.Print(nn.MethodName) p.printFreeFloating(nn, token.Name) io.WriteString(p.w, "(") p.joinPrint(",", nn.Params) p.printFreeFloating(nn, token.ParameterList) io.WriteString(p.w, ")") p.printFreeFloating(nn, token.Params) if nn.ReturnType != nil { p.bufStart = ":" p.Print(nn.ReturnType) } p.Print(nn.Stmt) p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtClass(n ast.Vertex) { nn := n.(*ast.StmtClass) p.printFreeFloatingOrDefault(nn, token.Start, p.bufStart) p.bufStart = "" if nn.Modifiers != nil { for k, m := range nn.Modifiers { if k > 0 && m.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } p.Print(m) } if nn.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } } p.printFreeFloating(nn, token.ModifierList) io.WriteString(p.w, "class") if nn.ClassName != nil { p.bufStart = " " p.Print(nn.ClassName) } if nn.ArgumentList != nil { p.printFreeFloatingOrDefault(nn.ArgumentList, token.Start, "(") p.joinPrint(",", nn.ArgumentList.Arguments) p.printFreeFloatingOrDefault(nn.ArgumentList, token.End, ")") } if nn.Extends != nil { p.printFreeFloating(nn.Extends, token.Start) if nn.Extends.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } io.WriteString(p.w, "extends") p.bufStart = " " p.Print(nn.Extends.ClassName) } if nn.Implements != nil { p.printFreeFloating(nn.Implements, token.Start) if nn.Implements.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } io.WriteString(p.w, "implements") if nn.Implements.InterfaceNames[0].GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } p.joinPrint(",", nn.Implements.InterfaceNames) } p.printFreeFloating(nn, token.Name) io.WriteString(p.w, "{") p.printNodes(nn.Stmts) p.printFreeFloating(nn, token.Stmts) io.WriteString(p.w, "}") p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtClassConstList(n ast.Vertex) { nn := n.(*ast.StmtClassConstList) p.printFreeFloating(nn, token.Start) if nn.Modifiers != nil { for k, m := range nn.Modifiers { if k > 0 && m.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } p.Print(m) } if nn.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } } p.printFreeFloating(nn, token.ModifierList) io.WriteString(p.w, "const") if nn.Consts[0].GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } p.joinPrint(",", nn.Consts) p.printFreeFloating(nn, token.ConstList) p.printFreeFloating(nn, token.SemiColon) if nn.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, ";") } p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtConstList(n ast.Vertex) { nn := n.(*ast.StmtConstList) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "const") if nn.Consts[0].GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } p.joinPrint(",", nn.Consts) p.printFreeFloating(nn, token.Stmts) p.printFreeFloating(nn, token.SemiColon) if nn.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, ";") } p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtConstant(n ast.Vertex) { nn := n.(*ast.StmtConstant) p.printFreeFloating(nn, token.Start) p.Print(nn.ConstantName) p.printFreeFloating(nn, token.Name) io.WriteString(p.w, "=") p.Print(nn.Expr) p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtContinue(n ast.Vertex) { nn := n.(*ast.StmtContinue) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "continue") if nn.Expr != nil { if nn.Expr.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } p.Print(nn.Expr) } p.printFreeFloating(nn, token.Expr) p.printFreeFloating(nn, token.SemiColon) if nn.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, ";") } p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtDeclare(n ast.Vertex) { nn := n.(*ast.StmtDeclare) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "declare") p.printFreeFloating(nn, token.Declare) io.WriteString(p.w, "(") p.joinPrint(",", nn.Consts) p.printFreeFloating(nn, token.ConstList) io.WriteString(p.w, ")") if nn.Alt { p.printFreeFloating(nn, token.Cond) io.WriteString(p.w, ":") s := nn.Stmt.(*ast.StmtStmtList) p.printNodes(s.Stmts) p.printFreeFloating(nn, token.Stmts) io.WriteString(p.w, "enddeclare") p.printFreeFloating(nn, token.AltEnd) p.printFreeFloating(nn, token.SemiColon) if nn.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, ";") } } else { p.Print(nn.Stmt) } p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtDefault(n ast.Vertex) { nn := n.(*ast.StmtDefault) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "default") p.printFreeFloating(nn, token.Default) p.printFreeFloating(nn, token.CaseSeparator) if nn.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, ":") } if len(nn.Stmts) > 0 { p.printNodes(nn.Stmts) } p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtDo(n ast.Vertex) { nn := n.(*ast.StmtDo) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "do") if _, ok := nn.Stmt.(*ast.StmtStmtList); !ok { if nn.Stmt.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } } p.Print(nn.Stmt) p.printFreeFloating(nn, token.Stmts) io.WriteString(p.w, "while") if _, ok := nn.Cond.(*ast.ParserBrackets); !ok { io.WriteString(p.w, "(") } p.Print(nn.Cond) if _, ok := nn.Cond.(*ast.ParserBrackets); !ok { io.WriteString(p.w, ")") } p.printFreeFloating(nn, token.Cond) p.printFreeFloating(nn, token.SemiColon) if nn.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, ";") } p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtEcho(n ast.Vertex) { nn := n.(*ast.StmtEcho) if nn.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, "echo") } if nn.Exprs[0].GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } p.printFreeFloating(nn, token.Start) p.printFreeFloating(nn, token.Echo) p.joinPrint(",", nn.Exprs) p.printFreeFloating(nn, token.Expr) p.printFreeFloating(nn, token.SemiColon) if nn.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, ";") } p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtElseif(n ast.Vertex) { nn := n.(*ast.StmtElseIf) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "elseif") if _, ok := nn.Cond.(*ast.ParserBrackets); !ok { io.WriteString(p.w, "(") } p.Print(nn.Cond) if _, ok := nn.Cond.(*ast.ParserBrackets); !ok { io.WriteString(p.w, ")") } p.Print(nn.Stmt) p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtElse(n ast.Vertex) { nn := n.(*ast.StmtElse) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "else") if _, ok := nn.Stmt.(*ast.StmtStmtList); !ok { if nn.Stmt.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } } p.Print(nn.Stmt) p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtExpression(n ast.Vertex) { nn := n.(*ast.StmtExpression) p.printFreeFloating(nn, token.Start) p.Print(nn.Expr) p.printFreeFloating(nn, token.Expr) p.printFreeFloating(nn, token.SemiColon) if nn.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, ";") } p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtFinally(n ast.Vertex) { nn := n.(*ast.StmtFinally) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "finally") p.printFreeFloating(nn, token.Finally) io.WriteString(p.w, "{") p.printNodes(nn.Stmts) p.printFreeFloating(nn, token.Stmts) io.WriteString(p.w, "}") p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtFor(n ast.Vertex) { nn := n.(*ast.StmtFor) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "for") p.printFreeFloating(nn, token.For) io.WriteString(p.w, "(") p.joinPrint(",", nn.Init) p.printFreeFloating(nn, token.InitExpr) io.WriteString(p.w, ";") p.joinPrint(",", nn.Cond) p.printFreeFloating(nn, token.CondExpr) io.WriteString(p.w, ";") p.joinPrint(",", nn.Loop) p.printFreeFloating(nn, token.IncExpr) io.WriteString(p.w, ")") p.Print(nn.Stmt) p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtForeach(n ast.Vertex) { nn := n.(*ast.StmtForeach) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "foreach") p.printFreeFloating(nn, token.Foreach) io.WriteString(p.w, "(") p.Print(nn.Expr) p.printFreeFloating(nn, token.Expr) if nn.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } io.WriteString(p.w, "as") if nn.Key != nil { if nn.Key.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } p.Print(nn.Key) p.printFreeFloating(nn, token.Key) io.WriteString(p.w, "=>") } else { if nn.Var.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } } p.Print(nn.Var) p.printFreeFloating(nn, token.Var) io.WriteString(p.w, ")") p.Print(nn.Stmt) p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtFunction(n ast.Vertex) { nn := n.(*ast.StmtFunction) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "function") p.printFreeFloating(nn, token.Function) if nn.ReturnsRef { if nn.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } io.WriteString(p.w, "&") } else { if nn.FunctionName.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } } p.Print(nn.FunctionName) p.printFreeFloating(nn, token.Name) io.WriteString(p.w, "(") p.joinPrint(",", nn.Params) p.printFreeFloating(nn, token.ParamList) io.WriteString(p.w, ")") p.printFreeFloating(nn, token.Params) if nn.ReturnType != nil { p.bufStart = ":" p.Print(nn.ReturnType) } p.printFreeFloating(nn, token.ReturnType) io.WriteString(p.w, "{") p.printNodes(nn.Stmts) p.printFreeFloating(nn, token.Stmts) io.WriteString(p.w, "}") p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtGlobal(n ast.Vertex) { nn := n.(*ast.StmtGlobal) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "global") p.joinPrint(",", nn.Vars) p.printFreeFloating(nn, token.VarList) p.printFreeFloating(nn, token.SemiColon) if nn.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, ";") } p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtGoto(n ast.Vertex) { nn := n.(*ast.StmtGoto) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "goto") if nn.Label.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } p.Print(nn.Label) p.printFreeFloating(nn, token.Label) p.printFreeFloating(nn, token.SemiColon) if nn.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, ";") } p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtHaltCompiler(n ast.Vertex) { nn := n.(*ast.StmtHaltCompiler) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "__halt_compiler") p.printFreeFloatingOrDefault(nn, token.End, "();") } func (p *Printer) printStmtIf(n ast.Vertex) { nn := n.(*ast.StmtIf) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "if") if _, ok := nn.Cond.(*ast.ParserBrackets); !ok { io.WriteString(p.w, "(") } p.Print(nn.Cond) if _, ok := nn.Cond.(*ast.ParserBrackets); !ok { io.WriteString(p.w, ")") } p.Print(nn.Stmt) if nn.ElseIf != nil { p.printNodes(nn.ElseIf) } if nn.Else != nil { p.Print(nn.Else) } p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtInlineHTML(n ast.Vertex) { nn := n.(*ast.StmtInlineHtml) p.printFreeFloating(nn, token.Start) if p.s == PhpState && nn.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, "?>") } p.SetState(HtmlState) io.WriteString(p.w, string(nn.Value)) p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtInterface(n ast.Vertex) { nn := n.(*ast.StmtInterface) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "interface") if nn.InterfaceName.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } p.Print(nn.InterfaceName) if nn.Extends != nil { p.printFreeFloating(nn.Extends, token.Start) if nn.Extends.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } io.WriteString(p.w, "extends") if nn.Extends.InterfaceNames[0].GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } p.joinPrint(",", nn.Extends.InterfaceNames) } p.printFreeFloating(nn, token.Name) io.WriteString(p.w, "{") p.printNodes(nn.Stmts) p.printFreeFloating(nn, token.Stmts) io.WriteString(p.w, "}") p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtLabel(n ast.Vertex) { nn := n.(*ast.StmtLabel) p.printFreeFloating(nn, token.Start) p.Print(nn.LabelName) p.printFreeFloating(nn, token.Label) io.WriteString(p.w, ":") p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtNamespace(n ast.Vertex) { nn := n.(*ast.StmtNamespace) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "namespace") if nn.NamespaceName != nil { if nn.NamespaceName.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } p.Print(nn.NamespaceName) } if nn.Stmts != nil { p.printFreeFloating(nn, token.Namespace) io.WriteString(p.w, "{") p.printNodes(nn.Stmts) p.printFreeFloating(nn, token.Stmts) io.WriteString(p.w, "}") } else { p.printFreeFloating(nn, token.SemiColon) if nn.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, ";") } } p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtNop(n ast.Vertex) { p.printFreeFloating(n, token.Start) p.printFreeFloating(n, token.SemiColon) if n.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, ";") } p.printFreeFloating(n, token.End) } func (p *Printer) printStmtPropertyList(n ast.Vertex) { nn := n.(*ast.StmtPropertyList) p.printFreeFloating(nn, token.Start) for k, m := range nn.Modifiers { if k > 0 && m.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } p.Print(m) } if nn.Type != nil { p.bufStart = " " p.Print(nn.Type) } if nn.Properties[0].GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } p.joinPrint(",", nn.Properties) p.printFreeFloating(n, token.PropertyList) p.printFreeFloating(n, token.SemiColon) if n.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, ";") } p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtProperty(n ast.Vertex) { nn := n.(*ast.StmtProperty) p.printFreeFloating(nn, token.Start) p.Print(nn.Var) if nn.Expr != nil { p.printFreeFloating(nn, token.Var) io.WriteString(p.w, "=") p.Print(nn.Expr) } p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtReturn(n ast.Vertex) { nn := n.(*ast.StmtReturn) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "return") p.bufStart = " " p.Print(nn.Expr) p.printFreeFloating(nn, token.Expr) p.printFreeFloating(nn, token.SemiColon) if n.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, ";") } p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtStaticVar(n ast.Vertex) { nn := n.(*ast.StmtStaticVar) p.printFreeFloating(nn, token.Start) p.Print(nn.Var) if nn.Expr != nil { p.printFreeFloating(nn, token.Var) io.WriteString(p.w, "=") p.Print(nn.Expr) } p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtStatic(n ast.Vertex) { nn := n.(*ast.StmtStatic) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "static") p.joinPrint(",", nn.Vars) p.printFreeFloating(nn, token.VarList) p.printFreeFloating(nn, token.SemiColon) if n.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, ";") } p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtStmtList(n ast.Vertex) { nn := n.(*ast.StmtStmtList) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "{") p.printNodes(nn.Stmts) p.printFreeFloating(nn, token.Stmts) io.WriteString(p.w, "}") p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtSwitch(n ast.Vertex) { nn := n.(*ast.StmtSwitch) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "switch") if _, ok := nn.Cond.(*ast.ParserBrackets); !ok { io.WriteString(p.w, "(") } p.Print(nn.Cond) if _, ok := nn.Cond.(*ast.ParserBrackets); !ok { io.WriteString(p.w, ")") } p.printFreeFloating(nn.CaseList, token.Start) io.WriteString(p.w, "{") p.printFreeFloating(nn.CaseList, token.CaseListStart) p.printNodes(nn.CaseList.Cases) p.printFreeFloating(nn.CaseList, token.CaseListEnd) io.WriteString(p.w, "}") p.printFreeFloating(nn.CaseList, token.End) p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtThrow(n ast.Vertex) { nn := n.(*ast.StmtThrow) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "throw") if nn.Expr.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } p.Print(nn.Expr) p.printFreeFloating(nn, token.Expr) p.printFreeFloating(nn, token.SemiColon) if n.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, ";") } p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtTraitAdaptationList(n ast.Vertex) { nn := n.(*ast.StmtTraitAdaptationList) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "{") p.printNodes(nn.Adaptations) p.printFreeFloating(nn, token.AdaptationList) io.WriteString(p.w, "}") p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtTraitMethodRef(n ast.Vertex) { nn := n.(*ast.StmtTraitMethodRef) p.printFreeFloating(nn, token.Start) if nn.Trait != nil { p.Print(nn.Trait) p.printFreeFloating(nn, token.Name) io.WriteString(p.w, "::") } p.Print(nn.Method) p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtTraitUseAlias(n ast.Vertex) { nn := n.(*ast.StmtTraitUseAlias) p.printFreeFloating(nn, token.Start) p.Print(nn.Ref) p.printFreeFloating(nn, token.Ref) if nn.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } io.WriteString(p.w, "as") if nn.Modifier != nil { if nn.Modifier.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } p.Print(nn.Modifier) } if nn.Alias != nil { if nn.Alias.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } p.Print(nn.Alias) } p.printFreeFloating(nn, token.Alias) p.printFreeFloating(nn, token.SemiColon) if n.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, ";") } p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtTraitUsePrecedence(n ast.Vertex) { nn := n.(*ast.StmtTraitUsePrecedence) p.printFreeFloating(nn, token.Start) p.Print(nn.Ref) p.printFreeFloating(nn, token.Ref) if nn.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } io.WriteString(p.w, "insteadof") p.bufStart = " " p.joinPrint(",", nn.Insteadof) p.printFreeFloating(nn, token.NameList) p.printFreeFloating(nn, token.SemiColon) if n.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, ";") } p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtTraitUse(n ast.Vertex) { nn := n.(*ast.StmtTraitUse) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "use") if nn.Traits[0].GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } p.joinPrint(",", nn.Traits) p.Print(nn.TraitAdaptationList) p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtTrait(n ast.Vertex) { nn := n.(*ast.StmtTrait) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "trait") if nn.TraitName.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") } p.Print(nn.TraitName) p.printFreeFloating(nn, token.Name) io.WriteString(p.w, "{") p.printNodes(nn.Stmts) p.printFreeFloating(nn, token.Stmts) io.WriteString(p.w, "}") p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtTry(n ast.Vertex) { nn := n.(*ast.StmtTry) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "try") p.printFreeFloating(nn, token.Try) io.WriteString(p.w, "{") p.printNodes(nn.Stmts) p.printFreeFloating(nn, token.Stmts) io.WriteString(p.w, "}") if nn.Catches != nil { p.printNodes(nn.Catches) } if nn.Finally != nil { p.Print(nn.Finally) } p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtUnset(n ast.Vertex) { nn := n.(*ast.StmtUnset) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "unset") p.printFreeFloating(nn, token.Unset) io.WriteString(p.w, "(") p.joinPrint(",", nn.Vars) p.printFreeFloating(nn, token.VarList) io.WriteString(p.w, ")") p.printFreeFloating(nn, token.CloseParenthesisToken) p.printFreeFloating(nn, token.SemiColon) if n.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, ";") } p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtUse(n ast.Vertex) { nn := n.(*ast.StmtUse) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "use") p.bufStart = " " p.Print(nn.UseList) p.printFreeFloatingOrDefault(nn, token.End, ";") } func (p *Printer) printStmtGroupUseList(n ast.Vertex) { nn := n.(*ast.StmtGroupUseList) p.printFreeFloating(nn, token.Start) p.Print(nn.Prefix) if _, ok := nn.UseList.(*ast.ParserNsSeparator); !ok { io.WriteString(p.w, "\\{") } p.Print(nn.UseList) if _, ok := nn.UseList.(*ast.ParserNsSeparator); !ok { io.WriteString(p.w, "}") } p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtUseList(n ast.Vertex) { nn := n.(*ast.StmtUseList) p.printFreeFloating(nn, token.Start) p.joinPrint(",", nn.UseDeclarations) p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtUseDeclaration(n ast.Vertex) { nn := n.(*ast.StmtUseDeclaration) p.printFreeFloating(nn, token.Start) p.Print(nn.Use) if nn.Alias != nil { if _, ok := nn.Alias.(*ast.ParserAs); !ok { io.WriteString(p.w, " as") } p.bufStart = " " p.Print(nn.Alias) } p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtUseType(n ast.Vertex) { nn := n.(*ast.StmtUseType) p.printFreeFloating(nn, token.Start) p.Print(nn.Type) p.bufStart = " " p.Print(nn.Use) p.printFreeFloating(nn, token.End) } func (p *Printer) printStmtWhile(n ast.Vertex) { nn := n.(*ast.StmtWhile) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "while") if _, ok := nn.Cond.(*ast.ParserBrackets); !ok { io.WriteString(p.w, "(") } p.Print(nn.Cond) if _, ok := nn.Cond.(*ast.ParserBrackets); !ok { io.WriteString(p.w, ")") } p.Print(nn.Stmt) p.printFreeFloating(nn, token.End) } func (p *Printer) printParserAs(n ast.Vertex) { nn := n.(*ast.ParserAs) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "as") p.Print(nn.Child) p.printFreeFloating(nn, token.End) } func (p *Printer) printParserNsSeparator(n ast.Vertex) { nn := n.(*ast.ParserNsSeparator) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "\\") p.Print(nn.Child) p.printFreeFloating(nn, token.End) } func (p *Printer) printParserBrackets(n ast.Vertex) { nn := n.(*ast.ParserBrackets) p.printFreeFloating(nn, token.Start) p.Print(nn.Child) p.printFreeFloating(nn, token.End) }