diff --git a/dumper.go b/dumper.go new file mode 100644 index 0000000..3e2ef65 --- /dev/null +++ b/dumper.go @@ -0,0 +1,26 @@ +package main + +import ( + "fmt" + + "github.com/z7zmey/php-parser/node" +) + +type dumper struct { + indent string +} + +func (d dumper) Visit(n node.Node) bool { + fmt.Printf("%v[%v]:\n", d.indent, n.Name()) + + return true +} + +func (d dumper) Children(key string) node.Visitor { + fmt.Printf("%v%v:\n", d.indent+". ", key) + return dumper{d.indent + ". . "} +} + +func (d dumper) Scalar(key string, value interface{}) { + fmt.Printf("%v%v: %v\n", d.indent+". ", key, value) +} diff --git a/main.go b/main.go index d3a1c72..959f6e6 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,6 @@ package main import ( - "bytes" "flag" "fmt" "log" @@ -17,14 +16,12 @@ func main() { for _, path := range flag.Args() { real, err := realpath.Realpath(path) checkErr(err) - fmt.Printf("\n==> %s", real) + fmt.Printf("==> %s\n", real) src, _ := os.Open(string(real)) rootnode := parser.Parse(src, real) - buf := new(bytes.Buffer) - rootnode.Print(buf, "") - fmt.Println(buf.String()) + rootnode.Walk(dumper{" | "}) } } diff --git a/node/argument.go b/node/argument.go index 6102d97..e50dc82 100644 --- a/node/argument.go +++ b/node/argument.go @@ -1,10 +1,5 @@ package node -import ( - "fmt" - "io" -) - type Argument struct { name string expr Node @@ -23,12 +18,13 @@ func NewArgument(expression Node, variadic bool) Node { } } -func (n Argument) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) - fmt.Fprintf(out, "\n%vvariadic: %t", indent+" ", n.variadic) +func (n Argument) Walk(v Visitor) { + if v.Visit(n) == false { + return + } if n.expr != nil { - fmt.Fprintf(out, "\n%vexpr:", indent+" ") - n.expr.Print(out, indent+" ") + vv := v.Children("expr") + n.expr.Walk(vv) } } diff --git a/node/expr/array.go b/node/expr/array.go index 883f487..773de1d 100644 --- a/node/expr/array.go +++ b/node/expr/array.go @@ -1,9 +1,6 @@ package expr import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) @@ -28,13 +25,15 @@ func NewArray(opentToken token.Token, closeToken token.Token, items []node.Node) } } -func (n Array) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d]", indent, n.name, n.opentToken.StartLine, n.closeToken.EndLine) +func (n Array) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.items != nil { - fmt.Fprintf(out, "\n%vitems:", indent+" ") + vv := v.Children("items") for _, nn := range n.items { - nn.Print(out, indent+" ") + nn.Walk(vv) } } } diff --git a/node/expr/array_dim_fetch.go b/node/expr/array_dim_fetch.go index e79f0cc..34d9dfc 100644 --- a/node/expr/array_dim_fetch.go +++ b/node/expr/array_dim_fetch.go @@ -1,9 +1,6 @@ package expr import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" ) @@ -25,16 +22,18 @@ func NewArrayDimFetch(variable node.Node, dim node.Node) node.Node { } } -func (n ArrayDimFetch) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.Name) +func (n ArrayDimFetch) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.variable != nil { - fmt.Fprintf(out, "\n%vvariable:", indent+" ") - n.variable.Print(out, indent+" ") + vv := v.Children("variable") + n.variable.Walk(vv) } if n.dim != nil { - fmt.Fprintf(out, "\n%vdim:", indent+" ") - n.dim.Print(out, indent+" ") + vv := v.Children("dim") + n.dim.Walk(vv) } } diff --git a/node/expr/array_item.go b/node/expr/array_item.go index 9b20b7e..9073be9 100644 --- a/node/expr/array_item.go +++ b/node/expr/array_item.go @@ -1,9 +1,6 @@ package expr import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" ) @@ -27,17 +24,18 @@ func NewArrayItem(key node.Node, val node.Node, byRef bool) node.Node { } } -func (n ArrayItem) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) - fmt.Fprintf(out, "\n%vbyRef: %t", indent+" ", n.byRef) +func (n ArrayItem) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.key != nil { - fmt.Fprintf(out, "\n%vkey:", indent+" ") - n.key.Print(out, indent+" ") + vv := v.Children("key") + n.key.Walk(vv) } if n.val != nil { - fmt.Fprintf(out, "\n%vval:", indent+" ") - n.val.Print(out, indent+" ") + vv := v.Children("val") + n.val.Walk(vv) } } diff --git a/node/expr/assign_op/assign.go b/node/expr/assign_op/assign.go index 977a8d1..b8da9f1 100644 --- a/node/expr/assign_op/assign.go +++ b/node/expr/assign_op/assign.go @@ -21,3 +21,19 @@ func NewAssign(variable node.Node, expression node.Node) node.Node { }, } } + +func (n Assign) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + if n.variable != nil { + vv := v.Children("variable") + n.variable.Walk(vv) + } + + if n.expression != nil { + vv := v.Children("expression") + n.expression.Walk(vv) + } +} diff --git a/node/expr/assign_op/assign_op.go b/node/expr/assign_op/assign_op.go index 55d9629..a0da3f7 100644 --- a/node/expr/assign_op/assign_op.go +++ b/node/expr/assign_op/assign_op.go @@ -1,9 +1,6 @@ package assign_op import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" ) @@ -12,17 +9,3 @@ type AssignOp struct { variable node.Node expression node.Node } - -func (n AssignOp) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) - - if n.variable != nil { - fmt.Fprintf(out, "\n%vvariable:", indent+" ") - n.variable.Print(out, indent+" ") - } - - if n.expression != nil { - fmt.Fprintf(out, "\n%vexpression:", indent+" ") - n.expression.Print(out, indent+" ") - } -} diff --git a/node/expr/assign_op/assign_ref.go b/node/expr/assign_op/assign_ref.go index b719616..b1e7810 100644 --- a/node/expr/assign_op/assign_ref.go +++ b/node/expr/assign_op/assign_ref.go @@ -21,3 +21,19 @@ func NewAssignRef(variable node.Node, expression node.Node) node.Node { }, } } + +func (n AssignRef) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + if n.variable != nil { + vv := v.Children("variable") + n.variable.Walk(vv) + } + + if n.expression != nil { + vv := v.Children("expression") + n.expression.Walk(vv) + } +} diff --git a/node/expr/assign_op/bitwise_and.go b/node/expr/assign_op/bitwise_and.go index 93ce1ca..3878e58 100644 --- a/node/expr/assign_op/bitwise_and.go +++ b/node/expr/assign_op/bitwise_and.go @@ -21,3 +21,19 @@ func NewBitwiseAnd(variable node.Node, expression node.Node) node.Node { }, } } + +func (n BitwiseAnd) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + if n.variable != nil { + vv := v.Children("variable") + n.variable.Walk(vv) + } + + if n.expression != nil { + vv := v.Children("expression") + n.expression.Walk(vv) + } +} diff --git a/node/expr/assign_op/bitwise_or.go b/node/expr/assign_op/bitwise_or.go index 1901349..55f3e59 100644 --- a/node/expr/assign_op/bitwise_or.go +++ b/node/expr/assign_op/bitwise_or.go @@ -21,3 +21,19 @@ func NewBitwiseOr(variable node.Node, expression node.Node) node.Node { }, } } + +func (n BitwiseOr) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + if n.variable != nil { + vv := v.Children("variable") + n.variable.Walk(vv) + } + + if n.expression != nil { + vv := v.Children("expression") + n.expression.Walk(vv) + } +} diff --git a/node/expr/assign_op/bitwise_xor.go b/node/expr/assign_op/bitwise_xor.go index 71e48b5..d0e1927 100644 --- a/node/expr/assign_op/bitwise_xor.go +++ b/node/expr/assign_op/bitwise_xor.go @@ -21,3 +21,19 @@ func NewBitwiseXor(variable node.Node, expression node.Node) node.Node { func (n BitwiseXor) Name() string { return "BitwiseXor" } + +func (n BitwiseXor) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + if n.variable != nil { + vv := v.Children("variable") + n.variable.Walk(vv) + } + + if n.expression != nil { + vv := v.Children("expression") + n.expression.Walk(vv) + } +} diff --git a/node/expr/assign_op/concat.go b/node/expr/assign_op/concat.go index 7b5c652..bd9621e 100644 --- a/node/expr/assign_op/concat.go +++ b/node/expr/assign_op/concat.go @@ -21,3 +21,19 @@ func NewConcat(variable node.Node, expression node.Node) node.Node { }, } } + +func (n Concat) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + if n.variable != nil { + vv := v.Children("variable") + n.variable.Walk(vv) + } + + if n.expression != nil { + vv := v.Children("expression") + n.expression.Walk(vv) + } +} diff --git a/node/expr/assign_op/div.go b/node/expr/assign_op/div.go index 8504fa3..b9b6716 100644 --- a/node/expr/assign_op/div.go +++ b/node/expr/assign_op/div.go @@ -21,3 +21,19 @@ func NewDiv(variable node.Node, expression node.Node) node.Node { }, } } + +func (n Div) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + if n.variable != nil { + vv := v.Children("variable") + n.variable.Walk(vv) + } + + if n.expression != nil { + vv := v.Children("expression") + n.expression.Walk(vv) + } +} diff --git a/node/expr/assign_op/minus.go b/node/expr/assign_op/minus.go index fadbbc5..47b9ef8 100644 --- a/node/expr/assign_op/minus.go +++ b/node/expr/assign_op/minus.go @@ -21,3 +21,19 @@ func NewMinus(variable node.Node, expression node.Node) node.Node { }, } } + +func (n Minus) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + if n.variable != nil { + vv := v.Children("variable") + n.variable.Walk(vv) + } + + if n.expression != nil { + vv := v.Children("expression") + n.expression.Walk(vv) + } +} diff --git a/node/expr/assign_op/mod.go b/node/expr/assign_op/mod.go index a068144..ab2163e 100644 --- a/node/expr/assign_op/mod.go +++ b/node/expr/assign_op/mod.go @@ -21,3 +21,19 @@ func NewMod(variable node.Node, expression node.Node) node.Node { }, } } + +func (n Mod) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + if n.variable != nil { + vv := v.Children("variable") + n.variable.Walk(vv) + } + + if n.expression != nil { + vv := v.Children("expression") + n.expression.Walk(vv) + } +} diff --git a/node/expr/assign_op/mul.go b/node/expr/assign_op/mul.go index 6d400fa..cd0b5d8 100644 --- a/node/expr/assign_op/mul.go +++ b/node/expr/assign_op/mul.go @@ -21,3 +21,19 @@ func NewMul(variable node.Node, expression node.Node) node.Node { }, } } + +func (n Mul) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + if n.variable != nil { + vv := v.Children("variable") + n.variable.Walk(vv) + } + + if n.expression != nil { + vv := v.Children("expression") + n.expression.Walk(vv) + } +} diff --git a/node/expr/assign_op/plus.go b/node/expr/assign_op/plus.go index 7ab0e01..ca3375f 100644 --- a/node/expr/assign_op/plus.go +++ b/node/expr/assign_op/plus.go @@ -21,3 +21,19 @@ func NewPlus(variable node.Node, expression node.Node) node.Node { }, } } + +func (n Plus) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + if n.variable != nil { + vv := v.Children("variable") + n.variable.Walk(vv) + } + + if n.expression != nil { + vv := v.Children("expression") + n.expression.Walk(vv) + } +} diff --git a/node/expr/assign_op/pow.go b/node/expr/assign_op/pow.go index 47d606d..790f359 100644 --- a/node/expr/assign_op/pow.go +++ b/node/expr/assign_op/pow.go @@ -21,3 +21,19 @@ func NewPow(variable node.Node, expression node.Node) node.Node { }, } } + +func (n Pow) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + if n.variable != nil { + vv := v.Children("variable") + n.variable.Walk(vv) + } + + if n.expression != nil { + vv := v.Children("expression") + n.expression.Walk(vv) + } +} diff --git a/node/expr/assign_op/shift_left.go b/node/expr/assign_op/shift_left.go index 1a25f58..13a1aeb 100644 --- a/node/expr/assign_op/shift_left.go +++ b/node/expr/assign_op/shift_left.go @@ -21,3 +21,19 @@ func NewShiftLeft(variable node.Node, expression node.Node) node.Node { }, } } + +func (n ShiftLeft) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + if n.variable != nil { + vv := v.Children("variable") + n.variable.Walk(vv) + } + + if n.expression != nil { + vv := v.Children("expression") + n.expression.Walk(vv) + } +} diff --git a/node/expr/assign_op/shift_right.go b/node/expr/assign_op/shift_right.go index 16b2e4f..03b153b 100644 --- a/node/expr/assign_op/shift_right.go +++ b/node/expr/assign_op/shift_right.go @@ -21,3 +21,19 @@ func NewShiftRight(variable node.Node, expression node.Node) node.Node { }, } } + +func (n ShiftRight) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + if n.variable != nil { + vv := v.Children("variable") + n.variable.Walk(vv) + } + + if n.expression != nil { + vv := v.Children("expression") + n.expression.Walk(vv) + } +} diff --git a/node/expr/binary_op/binary_op.go b/node/expr/binary_op/binary_op.go index 7f2a9b0..167c819 100644 --- a/node/expr/binary_op/binary_op.go +++ b/node/expr/binary_op/binary_op.go @@ -1,9 +1,6 @@ package binary_op import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" ) @@ -12,17 +9,3 @@ type BinaryOp struct { left node.Node right node.Node } - -func (n BinaryOp) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) - - if n.left != nil { - fmt.Fprintf(out, "\n%vleft:", indent+" ") - n.left.Print(out, indent+" ") - } - - if n.right != nil { - fmt.Fprintf(out, "\n%vright:", indent+" ") - n.right.Print(out, indent+" ") - } -} diff --git a/node/expr/binary_op/bitwise_and.go b/node/expr/binary_op/bitwise_and.go index 194ac80..bbeb817 100644 --- a/node/expr/binary_op/bitwise_and.go +++ b/node/expr/binary_op/bitwise_and.go @@ -21,3 +21,19 @@ func NewBitwiseAnd(variable node.Node, expression node.Node) node.Node { }, } } + +func (n BitwiseAnd) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + if n.left != nil { + vv := v.Children("left") + n.left.Walk(vv) + } + + if n.right != nil { + vv := v.Children("right") + n.right.Walk(vv) + } +} diff --git a/node/expr/binary_op/bitwise_or.go b/node/expr/binary_op/bitwise_or.go index 00affb6..93650bf 100644 --- a/node/expr/binary_op/bitwise_or.go +++ b/node/expr/binary_op/bitwise_or.go @@ -21,3 +21,19 @@ func NewBitwiseOr(variable node.Node, expression node.Node) node.Node { func (n BitwiseOr) Name() string { return "BitwiseOr" } + +func (n BitwiseOr) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + if n.left != nil { + vv := v.Children("left") + n.left.Walk(vv) + } + + if n.right != nil { + vv := v.Children("right") + n.right.Walk(vv) + } +} diff --git a/node/expr/binary_op/bitwise_xor.go b/node/expr/binary_op/bitwise_xor.go index d5bc094..2d68ed7 100644 --- a/node/expr/binary_op/bitwise_xor.go +++ b/node/expr/binary_op/bitwise_xor.go @@ -21,3 +21,19 @@ func NewBitwiseXor(variable node.Node, expression node.Node) node.Node { func (n BitwiseXor) Name() string { return "BitwiseXor" } + +func (n BitwiseXor) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + if n.left != nil { + vv := v.Children("left") + n.left.Walk(vv) + } + + if n.right != nil { + vv := v.Children("right") + n.right.Walk(vv) + } +} diff --git a/node/expr/binary_op/boolean_and.go b/node/expr/binary_op/boolean_and.go index b7eff32..422be6a 100644 --- a/node/expr/binary_op/boolean_and.go +++ b/node/expr/binary_op/boolean_and.go @@ -21,3 +21,19 @@ func NewBooleanAnd(variable node.Node, expression node.Node) node.Node { }, } } + +func (n BooleanAnd) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + if n.left != nil { + vv := v.Children("left") + n.left.Walk(vv) + } + + if n.right != nil { + vv := v.Children("right") + n.right.Walk(vv) + } +} diff --git a/node/expr/binary_op/boolean_or.go b/node/expr/binary_op/boolean_or.go index 0e5885c..35c56d6 100644 --- a/node/expr/binary_op/boolean_or.go +++ b/node/expr/binary_op/boolean_or.go @@ -21,3 +21,19 @@ func NewBooleanOr(variable node.Node, expression node.Node) node.Node { }, } } + +func (n BooleanOr) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + if n.left != nil { + vv := v.Children("left") + n.left.Walk(vv) + } + + if n.right != nil { + vv := v.Children("right") + n.right.Walk(vv) + } +} diff --git a/node/expr/binary_op/coalesce.go b/node/expr/binary_op/coalesce.go index 180f9d1..f0a2b4a 100644 --- a/node/expr/binary_op/coalesce.go +++ b/node/expr/binary_op/coalesce.go @@ -21,3 +21,19 @@ func NewCoalesce(variable node.Node, expression node.Node) node.Node { }, } } + +func (n Coalesce) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + if n.left != nil { + vv := v.Children("left") + n.left.Walk(vv) + } + + if n.right != nil { + vv := v.Children("right") + n.right.Walk(vv) + } +} diff --git a/node/expr/binary_op/concat.go b/node/expr/binary_op/concat.go index 4349778..ac1183d 100644 --- a/node/expr/binary_op/concat.go +++ b/node/expr/binary_op/concat.go @@ -21,3 +21,19 @@ func NewConcat(variable node.Node, expression node.Node) node.Node { }, } } + +func (n Concat) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + if n.left != nil { + vv := v.Children("left") + n.left.Walk(vv) + } + + if n.right != nil { + vv := v.Children("right") + n.right.Walk(vv) + } +} diff --git a/node/expr/binary_op/div.go b/node/expr/binary_op/div.go index a9785fc..bec6627 100644 --- a/node/expr/binary_op/div.go +++ b/node/expr/binary_op/div.go @@ -21,3 +21,19 @@ func NewDiv(variable node.Node, expression node.Node) node.Node { }, } } + +func (n Div) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + if n.left != nil { + vv := v.Children("left") + n.left.Walk(vv) + } + + if n.right != nil { + vv := v.Children("right") + n.right.Walk(vv) + } +} diff --git a/node/expr/binary_op/equal.go b/node/expr/binary_op/equal.go index d9346f6..cef0055 100644 --- a/node/expr/binary_op/equal.go +++ b/node/expr/binary_op/equal.go @@ -21,3 +21,19 @@ func NewEqual(variable node.Node, expression node.Node) node.Node { }, } } + +func (n Equal) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + if n.left != nil { + vv := v.Children("left") + n.left.Walk(vv) + } + + if n.right != nil { + vv := v.Children("right") + n.right.Walk(vv) + } +} diff --git a/node/expr/binary_op/greater.go b/node/expr/binary_op/greater.go index 2e4276f..cd9ec50 100644 --- a/node/expr/binary_op/greater.go +++ b/node/expr/binary_op/greater.go @@ -21,3 +21,19 @@ func NewGreater(variable node.Node, expression node.Node) node.Node { }, } } + +func (n Greater) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + if n.left != nil { + vv := v.Children("left") + n.left.Walk(vv) + } + + if n.right != nil { + vv := v.Children("right") + n.right.Walk(vv) + } +} diff --git a/node/expr/binary_op/greater_or_equal.go b/node/expr/binary_op/greater_or_equal.go index 689ebdf..8e7f52f 100644 --- a/node/expr/binary_op/greater_or_equal.go +++ b/node/expr/binary_op/greater_or_equal.go @@ -21,3 +21,19 @@ func NewGreaterOrEqual(variable node.Node, expression node.Node) node.Node { }, } } + +func (n GreaterOrEqual) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + if n.left != nil { + vv := v.Children("left") + n.left.Walk(vv) + } + + if n.right != nil { + vv := v.Children("right") + n.right.Walk(vv) + } +} diff --git a/node/expr/binary_op/identical.go b/node/expr/binary_op/identical.go index f566423..c5e5094 100644 --- a/node/expr/binary_op/identical.go +++ b/node/expr/binary_op/identical.go @@ -21,3 +21,19 @@ func NewIdentical(variable node.Node, expression node.Node) node.Node { }, } } + +func (n Identical) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + if n.left != nil { + vv := v.Children("left") + n.left.Walk(vv) + } + + if n.right != nil { + vv := v.Children("right") + n.right.Walk(vv) + } +} diff --git a/node/expr/binary_op/logical_and.go b/node/expr/binary_op/logical_and.go index 59341d7..31edd78 100644 --- a/node/expr/binary_op/logical_and.go +++ b/node/expr/binary_op/logical_and.go @@ -21,3 +21,19 @@ func NewLogicalAnd(variable node.Node, expression node.Node) node.Node { }, } } + +func (n LogicalAnd) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + if n.left != nil { + vv := v.Children("left") + n.left.Walk(vv) + } + + if n.right != nil { + vv := v.Children("right") + n.right.Walk(vv) + } +} diff --git a/node/expr/binary_op/logical_or.go b/node/expr/binary_op/logical_or.go index e9c4a0a..7be1e49 100644 --- a/node/expr/binary_op/logical_or.go +++ b/node/expr/binary_op/logical_or.go @@ -21,3 +21,19 @@ func NewLogicalOr(variable node.Node, expression node.Node) node.Node { }, } } + +func (n LogicalOr) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + if n.left != nil { + vv := v.Children("left") + n.left.Walk(vv) + } + + if n.right != nil { + vv := v.Children("right") + n.right.Walk(vv) + } +} diff --git a/node/expr/binary_op/logical_xor.go b/node/expr/binary_op/logical_xor.go index e0213d6..c1960b6 100644 --- a/node/expr/binary_op/logical_xor.go +++ b/node/expr/binary_op/logical_xor.go @@ -21,3 +21,19 @@ func NewLogicalXor(variable node.Node, expression node.Node) node.Node { }, } } + +func (n LogicalXor) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + if n.left != nil { + vv := v.Children("left") + n.left.Walk(vv) + } + + if n.right != nil { + vv := v.Children("right") + n.right.Walk(vv) + } +} diff --git a/node/expr/binary_op/minus.go b/node/expr/binary_op/minus.go index 00cfedc..e7924b3 100644 --- a/node/expr/binary_op/minus.go +++ b/node/expr/binary_op/minus.go @@ -21,3 +21,19 @@ func NewMinus(variable node.Node, expression node.Node) node.Node { }, } } + +func (n Minus) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + if n.left != nil { + vv := v.Children("left") + n.left.Walk(vv) + } + + if n.right != nil { + vv := v.Children("right") + n.right.Walk(vv) + } +} diff --git a/node/expr/binary_op/mod.go b/node/expr/binary_op/mod.go index a99409a..da9da7c 100644 --- a/node/expr/binary_op/mod.go +++ b/node/expr/binary_op/mod.go @@ -4,7 +4,7 @@ import ( "github.com/z7zmey/php-parser/node" ) -func(n Mod) Name() string { +func (n Mod) Name() string { return "Mod" } @@ -21,3 +21,19 @@ func NewMod(variable node.Node, expression node.Node) node.Node { }, } } + +func (n Mod) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + if n.left != nil { + vv := v.Children("left") + n.left.Walk(vv) + } + + if n.right != nil { + vv := v.Children("right") + n.right.Walk(vv) + } +} diff --git a/node/expr/binary_op/mul.go b/node/expr/binary_op/mul.go index 8b6dff0..e5ef893 100644 --- a/node/expr/binary_op/mul.go +++ b/node/expr/binary_op/mul.go @@ -4,7 +4,7 @@ import ( "github.com/z7zmey/php-parser/node" ) -func(n Mul) Name() string { +func (n Mul) Name() string { return "Mul" } @@ -21,3 +21,19 @@ func NewMul(variable node.Node, expression node.Node) node.Node { }, } } + +func (n Mul) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + if n.left != nil { + vv := v.Children("left") + n.left.Walk(vv) + } + + if n.right != nil { + vv := v.Children("right") + n.right.Walk(vv) + } +} diff --git a/node/expr/binary_op/not_equal.go b/node/expr/binary_op/not_equal.go index a5c3271..fdee617 100644 --- a/node/expr/binary_op/not_equal.go +++ b/node/expr/binary_op/not_equal.go @@ -4,7 +4,7 @@ import ( "github.com/z7zmey/php-parser/node" ) -func(n NotEqual) Name() string { +func (n NotEqual) Name() string { return "NotEqual" } @@ -21,3 +21,19 @@ func NewNotEqual(variable node.Node, expression node.Node) node.Node { }, } } + +func (n NotEqual) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + if n.left != nil { + vv := v.Children("left") + n.left.Walk(vv) + } + + if n.right != nil { + vv := v.Children("right") + n.right.Walk(vv) + } +} diff --git a/node/expr/binary_op/not_identical.go b/node/expr/binary_op/not_identical.go index f9ee000..6a2deb8 100644 --- a/node/expr/binary_op/not_identical.go +++ b/node/expr/binary_op/not_identical.go @@ -4,7 +4,7 @@ import ( "github.com/z7zmey/php-parser/node" ) -func(n NotIdentical) Name() string { +func (n NotIdentical) Name() string { return "NotIdentical" } @@ -21,3 +21,19 @@ func NewNotIdentical(variable node.Node, expression node.Node) node.Node { }, } } + +func (n NotIdentical) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + if n.left != nil { + vv := v.Children("left") + n.left.Walk(vv) + } + + if n.right != nil { + vv := v.Children("right") + n.right.Walk(vv) + } +} diff --git a/node/expr/binary_op/plus.go b/node/expr/binary_op/plus.go index ab9a967..c7a805f 100644 --- a/node/expr/binary_op/plus.go +++ b/node/expr/binary_op/plus.go @@ -4,7 +4,7 @@ import ( "github.com/z7zmey/php-parser/node" ) -func(n Plus) Name() string { +func (n Plus) Name() string { return "Plus" } @@ -21,3 +21,19 @@ func NewPlus(variable node.Node, expression node.Node) node.Node { }, } } + +func (n Plus) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + if n.left != nil { + vv := v.Children("left") + n.left.Walk(vv) + } + + if n.right != nil { + vv := v.Children("right") + n.right.Walk(vv) + } +} diff --git a/node/expr/binary_op/pow.go b/node/expr/binary_op/pow.go index 9a6fdbb..5f28094 100644 --- a/node/expr/binary_op/pow.go +++ b/node/expr/binary_op/pow.go @@ -4,7 +4,7 @@ import ( "github.com/z7zmey/php-parser/node" ) -func(n Pow) Name() string { +func (n Pow) Name() string { return "Pow" } @@ -21,3 +21,19 @@ func NewPow(variable node.Node, expression node.Node) node.Node { }, } } + +func (n Pow) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + if n.left != nil { + vv := v.Children("left") + n.left.Walk(vv) + } + + if n.right != nil { + vv := v.Children("right") + n.right.Walk(vv) + } +} diff --git a/node/expr/binary_op/shift_left.go b/node/expr/binary_op/shift_left.go index 4638f0f..7be8148 100644 --- a/node/expr/binary_op/shift_left.go +++ b/node/expr/binary_op/shift_left.go @@ -4,7 +4,7 @@ import ( "github.com/z7zmey/php-parser/node" ) -func(n ShiftLeft) Name() string { +func (n ShiftLeft) Name() string { return "ShiftLeft" } @@ -21,3 +21,19 @@ func NewShiftLeft(variable node.Node, expression node.Node) node.Node { }, } } + +func (n ShiftLeft) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + if n.left != nil { + vv := v.Children("left") + n.left.Walk(vv) + } + + if n.right != nil { + vv := v.Children("right") + n.right.Walk(vv) + } +} diff --git a/node/expr/binary_op/shift_right.go b/node/expr/binary_op/shift_right.go index d078d41..6110ddd 100644 --- a/node/expr/binary_op/shift_right.go +++ b/node/expr/binary_op/shift_right.go @@ -4,7 +4,7 @@ import ( "github.com/z7zmey/php-parser/node" ) -func(n ShiftRight) Name() string { +func (n ShiftRight) Name() string { return "ShiftRight" } @@ -21,3 +21,19 @@ func NewShiftRight(variable node.Node, expression node.Node) node.Node { }, } } + +func (n ShiftRight) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + if n.left != nil { + vv := v.Children("left") + n.left.Walk(vv) + } + + if n.right != nil { + vv := v.Children("right") + n.right.Walk(vv) + } +} diff --git a/node/expr/binary_op/smaller.go b/node/expr/binary_op/smaller.go index 8557506..ee98050 100644 --- a/node/expr/binary_op/smaller.go +++ b/node/expr/binary_op/smaller.go @@ -4,7 +4,7 @@ import ( "github.com/z7zmey/php-parser/node" ) -func(n Smaller) Name() string { +func (n Smaller) Name() string { return "Smaller" } @@ -21,3 +21,19 @@ func NewSmaller(variable node.Node, expression node.Node) node.Node { }, } } + +func (n Smaller) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + if n.left != nil { + vv := v.Children("left") + n.left.Walk(vv) + } + + if n.right != nil { + vv := v.Children("right") + n.right.Walk(vv) + } +} diff --git a/node/expr/binary_op/smaller_or_equal.go b/node/expr/binary_op/smaller_or_equal.go index f390416..934d0e8 100644 --- a/node/expr/binary_op/smaller_or_equal.go +++ b/node/expr/binary_op/smaller_or_equal.go @@ -4,7 +4,7 @@ import ( "github.com/z7zmey/php-parser/node" ) -func(n SmallerOrEqual) Name() string { +func (n SmallerOrEqual) Name() string { return "SmallerOrEqual" } @@ -21,3 +21,19 @@ func NewSmallerOrEqual(variable node.Node, expression node.Node) node.Node { }, } } + +func (n SmallerOrEqual) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + if n.left != nil { + vv := v.Children("left") + n.left.Walk(vv) + } + + if n.right != nil { + vv := v.Children("right") + n.right.Walk(vv) + } +} diff --git a/node/expr/binary_op/spaceship.go b/node/expr/binary_op/spaceship.go index 0799d7c..a2ff825 100644 --- a/node/expr/binary_op/spaceship.go +++ b/node/expr/binary_op/spaceship.go @@ -4,7 +4,7 @@ import ( "github.com/z7zmey/php-parser/node" ) -func(n Spaceship) Name() string { +func (n Spaceship) Name() string { return "Spaceship" } @@ -21,3 +21,19 @@ func NewSpaceship(variable node.Node, expression node.Node) node.Node { }, } } + +func (n Spaceship) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + if n.left != nil { + vv := v.Children("left") + n.left.Walk(vv) + } + + if n.right != nil { + vv := v.Children("right") + n.right.Walk(vv) + } +} diff --git a/node/expr/bitwise_not.go b/node/expr/bitwise_not.go index a1ac956..ea7dcf5 100644 --- a/node/expr/bitwise_not.go +++ b/node/expr/bitwise_not.go @@ -1,9 +1,6 @@ package expr import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" ) @@ -23,11 +20,13 @@ func NewBitwiseNot(expression node.Node) node.Node { } } -func (n BitwiseNot) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) +func (n BitwiseNot) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.expr != nil { - fmt.Fprintf(out, "\n%vexpr:", indent+" ") - n.expr.Print(out, indent+" ") + vv := v.Children("expr") + n.expr.Walk(vv) } } diff --git a/node/expr/boolean_not.go b/node/expr/boolean_not.go index 1de96dd..f6d2210 100644 --- a/node/expr/boolean_not.go +++ b/node/expr/boolean_not.go @@ -1,9 +1,6 @@ package expr import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" ) @@ -23,11 +20,13 @@ func NewBooleanNot(expression node.Node) node.Node { } } -func (n BooleanNot) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) +func (n BooleanNot) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.expr != nil { - fmt.Fprintf(out, "\n%vexpr:", indent+" ") - n.expr.Print(out, indent+" ") + vv := v.Children("expr") + n.expr.Walk(vv) } } diff --git a/node/expr/cast/cast.go b/node/expr/cast/cast.go index 75853f7..abaeb94 100644 --- a/node/expr/cast/cast.go +++ b/node/expr/cast/cast.go @@ -1,9 +1,6 @@ package cast import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" ) @@ -11,12 +8,3 @@ type Cast struct { name string expr node.Node } - -func (n Cast) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) - - if n.expr != nil { - fmt.Fprintf(out, "\n%vexpr:", indent+" ") - n.expr.Print(out, indent+" ") - } -} diff --git a/node/expr/cast/cast_array.go b/node/expr/cast/cast_array.go index 01d6375..2ad6a65 100644 --- a/node/expr/cast/cast_array.go +++ b/node/expr/cast/cast_array.go @@ -20,3 +20,14 @@ func NewCastArray(expr node.Node) node.Node { }, } } + +func (n CastArray) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + if n.expr != nil { + vv := v.Children("expr") + n.expr.Walk(vv) + } +} diff --git a/node/expr/cast/cast_bool.go b/node/expr/cast/cast_bool.go index 48468d1..c5b47a3 100644 --- a/node/expr/cast/cast_bool.go +++ b/node/expr/cast/cast_bool.go @@ -20,3 +20,14 @@ func NewCastBool(expr node.Node) node.Node { }, } } + +func (n CastBool) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + if n.expr != nil { + vv := v.Children("expr") + n.expr.Walk(vv) + } +} diff --git a/node/expr/cast/cast_double.go b/node/expr/cast/cast_double.go index 044dbd8..2ec2a8e 100644 --- a/node/expr/cast/cast_double.go +++ b/node/expr/cast/cast_double.go @@ -20,3 +20,14 @@ func NewCastDouble(expr node.Node) node.Node { }, } } + +func (n CastDouble) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + if n.expr != nil { + vv := v.Children("expr") + n.expr.Walk(vv) + } +} diff --git a/node/expr/cast/cast_int.go b/node/expr/cast/cast_int.go index 7690e00..28495f0 100644 --- a/node/expr/cast/cast_int.go +++ b/node/expr/cast/cast_int.go @@ -20,3 +20,14 @@ func NewCastInt(expr node.Node) node.Node { }, } } + +func (n CastInt) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + if n.expr != nil { + vv := v.Children("expr") + n.expr.Walk(vv) + } +} diff --git a/node/expr/cast/cast_object.go b/node/expr/cast/cast_object.go index dad301f..2985726 100644 --- a/node/expr/cast/cast_object.go +++ b/node/expr/cast/cast_object.go @@ -20,3 +20,14 @@ func NewCastObject(expr node.Node) node.Node { }, } } + +func (n CastObject) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + if n.expr != nil { + vv := v.Children("expr") + n.expr.Walk(vv) + } +} diff --git a/node/expr/cast/cast_string.go b/node/expr/cast/cast_string.go index 915357c..993aa4d 100644 --- a/node/expr/cast/cast_string.go +++ b/node/expr/cast/cast_string.go @@ -20,3 +20,14 @@ func NewCastString(expr node.Node) node.Node { }, } } + +func (n CastString) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + if n.expr != nil { + vv := v.Children("expr") + n.expr.Walk(vv) + } +} diff --git a/node/expr/cast/cast_unset.go b/node/expr/cast/cast_unset.go index a1f8e01..b9359c7 100644 --- a/node/expr/cast/cast_unset.go +++ b/node/expr/cast/cast_unset.go @@ -20,3 +20,14 @@ func NewCastUnset(expr node.Node) node.Node { }, } } + +func (n CastUnset) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + if n.expr != nil { + vv := v.Children("expr") + n.expr.Walk(vv) + } +} diff --git a/node/expr/class_const_fetch.go b/node/expr/class_const_fetch.go index ad7e778..8926c37 100644 --- a/node/expr/class_const_fetch.go +++ b/node/expr/class_const_fetch.go @@ -1,9 +1,6 @@ package expr import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) @@ -18,6 +15,7 @@ type ClassConstFetch struct { constant token.Token } +// TODO: constant must be identifier func NewClassConstFetch(class node.Node, constant token.Token) node.Node { return ClassConstFetch{ "ClassConstFetch", @@ -26,12 +24,15 @@ func NewClassConstFetch(class node.Node, constant token.Token) node.Node { } } -func (n ClassConstFetch) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) - fmt.Fprintf(out, "\n%vname: %q", indent+" ", n.constant.Value) +func (n ClassConstFetch) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + v.Scalar("constant", n.constant.Value) if n.class != nil { - fmt.Fprintf(out, "\n%vclass:", indent+" ") - n.class.Print(out, indent+" ") + vv := v.Children("class") + n.class.Walk(vv) } } diff --git a/node/expr/clone.go b/node/expr/clone.go index 027892d..21ec935 100644 --- a/node/expr/clone.go +++ b/node/expr/clone.go @@ -1,9 +1,6 @@ package expr import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" ) @@ -23,11 +20,13 @@ func NewClone(expression node.Node) node.Node { } } -func (n Clone) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) +func (n Clone) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.expr != nil { - fmt.Fprintf(out, "\n%vexpr:", indent+" ") - n.expr.Print(out, indent+" ") + vv := v.Children("expr") + n.expr.Walk(vv) } } diff --git a/node/expr/closure.go b/node/expr/closure.go index fd31671..6d47ebe 100644 --- a/node/expr/closure.go +++ b/node/expr/closure.go @@ -1,9 +1,6 @@ package expr import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" ) @@ -33,35 +30,37 @@ func NewClosure(params []node.Node, uses []node.Node, returnType node.Node, stmt } } -func (n Closure) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) +func (n Closure) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } - fmt.Fprintf(out, "\n%vis static: %t", indent+" ", n.isStatic) - fmt.Fprintf(out, "\n%vis return ref: %t", indent+" ", n.isReturnRef) + v.Scalar("isStatic", n.isStatic) + v.Scalar("isReturnRef", n.isReturnRef) if n.params != nil { - fmt.Fprintf(out, "\n%vparams:", indent+" ") + vv := v.Children("params") for _, nn := range n.params { - nn.Print(out, indent+" ") + nn.Walk(vv) } } if n.uses != nil { - fmt.Fprintf(out, "\n%vuses:", indent+" ") + vv := v.Children("uses") for _, nn := range n.uses { - nn.Print(out, indent+" ") + nn.Walk(vv) } } if n.returnType != nil { - fmt.Fprintf(out, "\n%vreturn type:", indent+" ") - n.returnType.Print(out, indent+" ") + vv := v.Children("returnType") + n.returnType.Walk(vv) } if n.stmts != nil { - fmt.Fprintf(out, "\n%vstmts:", indent+" ") + vv := v.Children("stmts") for _, nn := range n.stmts { - nn.Print(out, indent+" ") + nn.Walk(vv) } } } diff --git a/node/expr/closure_use.go b/node/expr/closure_use.go index b05ed2f..4e48da6 100644 --- a/node/expr/closure_use.go +++ b/node/expr/closure_use.go @@ -1,9 +1,6 @@ package expr import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" ) @@ -25,12 +22,15 @@ func NewClusureUse(variable node.Node, byRef bool) node.Node { } } -func (n ClusureUse) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) - fmt.Fprintf(out, "\n%vby ref: %t", indent+" ", n.byRef) +func (n ClusureUse) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + v.Scalar("byRef", n.byRef) if n.variable != nil { - fmt.Fprintf(out, "\n%vvariable:", indent+" ") - n.variable.Print(out, indent+" ") + vv := v.Children("variable") + n.variable.Walk(vv) } } diff --git a/node/expr/const_fetch.go b/node/expr/const_fetch.go index 1b2b7ec..3607ba8 100644 --- a/node/expr/const_fetch.go +++ b/node/expr/const_fetch.go @@ -1,9 +1,6 @@ package expr import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" ) @@ -23,11 +20,13 @@ func NewConstFetch(constant node.Node) node.Node { } } -func (n ConstFetch) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) +func (n ConstFetch) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.constant != nil { - fmt.Fprintf(out, "\n%vconstant:", indent+" ") - n.constant.Print(out, indent+" ") + vv := v.Children("constant") + n.constant.Walk(vv) } } diff --git a/node/expr/empty.go b/node/expr/empty.go index f07ac29..3546c99 100644 --- a/node/expr/empty.go +++ b/node/expr/empty.go @@ -1,9 +1,6 @@ package expr import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" ) @@ -23,11 +20,13 @@ func NewEmpty(expression node.Node) node.Node { } } -func (n Empty) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) +func (n Empty) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.expr != nil { - fmt.Fprintf(out, "\n%vexpr:", indent+" ") - n.expr.Print(out, indent+" ") + vv := v.Children("expr") + n.expr.Walk(vv) } } diff --git a/node/expr/error_suppress.go b/node/expr/error_suppress.go index 0484eec..4bbd76f 100644 --- a/node/expr/error_suppress.go +++ b/node/expr/error_suppress.go @@ -1,9 +1,6 @@ package expr import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" ) @@ -23,11 +20,13 @@ func NewErrorSuppress(expression node.Node) node.Node { } } -func (n ErrorSuppress) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) +func (n ErrorSuppress) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.expr != nil { - fmt.Fprintf(out, "\n%vexpr:", indent+" ") - n.expr.Print(out, indent+" ") + vv := v.Children("expr") + n.expr.Walk(vv) } } diff --git a/node/expr/eval.go b/node/expr/eval.go index a7c50d1..08dcca3 100644 --- a/node/expr/eval.go +++ b/node/expr/eval.go @@ -1,9 +1,6 @@ package expr import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" ) @@ -23,11 +20,13 @@ func NewEval(expression node.Node) node.Node { } } -func (n Eval) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) +func (n Eval) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.expr != nil { - fmt.Fprintf(out, "\n%vexpr:", indent+" ") - n.expr.Print(out, indent+" ") + vv := v.Children("expr") + n.expr.Walk(vv) } } diff --git a/node/expr/exit.go b/node/expr/exit.go index 238770c..50248df 100644 --- a/node/expr/exit.go +++ b/node/expr/exit.go @@ -1,9 +1,6 @@ package expr import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" ) @@ -25,12 +22,13 @@ func NewExit(expr node.Node, isDie bool) node.Node { } } -func (n Exit) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) - fmt.Fprintf(out, "\n%vis die: %t", indent+" ", n.isDie) +func (n Exit) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.expr != nil { - fmt.Fprintf(out, "\n%vexpr:", indent+" ") - n.expr.Print(out, indent+" ") + vv := v.Children("expr") + n.expr.Walk(vv) } } diff --git a/node/expr/function_call.go b/node/expr/function_call.go index 77878f4..90a8fe1 100644 --- a/node/expr/function_call.go +++ b/node/expr/function_call.go @@ -1,9 +1,6 @@ package expr import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" ) @@ -25,18 +22,20 @@ func NewFunctionCall(function node.Node, arguments []node.Node) node.Node { } } -func (n FunctionCall) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) +func (n FunctionCall) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.function != nil { - fmt.Fprintf(out, "\n%vfunction:", indent+" ") - n.function.Print(out, indent+" ") + vv := v.Children("function") + n.function.Walk(vv) } if n.arguments != nil { - fmt.Fprintf(out, "\n%varguments:", indent+" ") + vv := v.Children("arguments") for _, nn := range n.arguments { - nn.Print(out, indent+" ") + nn.Walk(vv) } } } diff --git a/node/expr/include.go b/node/expr/include.go index 9987af7..634e797 100644 --- a/node/expr/include.go +++ b/node/expr/include.go @@ -1,9 +1,6 @@ package expr import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" ) @@ -23,11 +20,13 @@ func NewInclude(expression node.Node) node.Node { } } -func (n Include) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) +func (n Include) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.expr != nil { - fmt.Fprintf(out, "\n%vexpr:", indent+" ") - n.expr.Print(out, indent+" ") + vv := v.Children("expr") + n.expr.Walk(vv) } } diff --git a/node/expr/include_once.go b/node/expr/include_once.go index 47962b2..8c564bb 100644 --- a/node/expr/include_once.go +++ b/node/expr/include_once.go @@ -1,9 +1,6 @@ package expr import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" ) @@ -23,11 +20,13 @@ func NewIncludeOnce(expression node.Node) node.Node { } } -func (n IncludeOnce) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) +func (n IncludeOnce) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.expr != nil { - fmt.Fprintf(out, "\n%vexpr:", indent+" ") - n.expr.Print(out, indent+" ") + vv := v.Children("expr") + n.expr.Walk(vv) } } diff --git a/node/expr/instance_of.go b/node/expr/instance_of.go index 324ddb3..94a1e27 100644 --- a/node/expr/instance_of.go +++ b/node/expr/instance_of.go @@ -1,9 +1,6 @@ package expr import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" ) @@ -25,16 +22,18 @@ func NewInstanceOf(expr node.Node, class node.Node) node.Node { } } -func (n InstanceOf) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) +func (n InstanceOf) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.expr != nil { - fmt.Fprintf(out, "\n%vexpr:", indent+" ") - n.expr.Print(out, indent+" ") + vv := v.Children("expr") + n.expr.Walk(vv) } if n.class != nil { - fmt.Fprintf(out, "\n%vclass:", indent+" ") - n.class.Print(out, indent+" ") + vv := v.Children("class") + n.class.Walk(vv) } } diff --git a/node/expr/isset.go b/node/expr/isset.go index 5df82d4..f17c74e 100644 --- a/node/expr/isset.go +++ b/node/expr/isset.go @@ -1,9 +1,6 @@ package expr import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" ) @@ -23,13 +20,15 @@ func NewIsset(variables []node.Node) node.Node { } } -func (n Isset) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) +func (n Isset) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.variables != nil { - fmt.Fprintf(out, "\n%vvariables:", indent+" ") + vv := v.Children("variables") for _, nn := range n.variables { - nn.Print(out, indent+" ") + nn.Walk(vv) } } } diff --git a/node/expr/list.go b/node/expr/list.go index 733e6bc..d9c9f34 100644 --- a/node/expr/list.go +++ b/node/expr/list.go @@ -1,9 +1,6 @@ package expr import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" ) @@ -23,13 +20,15 @@ func NewList(items []node.Node) node.Node { } } -func (n List) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) +func (n List) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.items != nil { - fmt.Fprintf(out, "\n%vitems:", indent+" ") + vv := v.Children("items") for _, nn := range n.items { - nn.Print(out, indent+" ") + nn.Walk(vv) } } } diff --git a/node/expr/method_call.go b/node/expr/method_call.go index cb3ff6d..1cbb154 100644 --- a/node/expr/method_call.go +++ b/node/expr/method_call.go @@ -1,9 +1,6 @@ package expr import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" ) @@ -27,23 +24,25 @@ func NewMethodCall(variable node.Node, method node.Node, arguments []node.Node) } } -func (n MethodCall) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) +func (n MethodCall) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.variable != nil { - fmt.Fprintf(out, "\n%vvariable:", indent+" ") - n.variable.Print(out, indent+" ") + vv := v.Children("variable") + n.variable.Walk(vv) } if n.method != nil { - fmt.Fprintf(out, "\n%vmethod:", indent+" ") - n.method.Print(out, indent+" ") + vv := v.Children("method") + n.method.Walk(vv) } if n.arguments != nil { - fmt.Fprintf(out, "\n%varguments:", indent+" ") + vv := v.Children("arguments") for _, nn := range n.arguments { - nn.Print(out, indent+" ") + nn.Walk(vv) } } } diff --git a/node/expr/new.go b/node/expr/new.go index ecd1b56..311cc17 100644 --- a/node/expr/new.go +++ b/node/expr/new.go @@ -1,9 +1,6 @@ package expr import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" ) @@ -25,18 +22,20 @@ func NewNew(class node.Node, arguments []node.Node) node.Node { } } -func (n New) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) +func (n New) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.class != nil { - fmt.Fprintf(out, "\n%vclass:", indent+" ") - n.class.Print(out, indent+" ") + vv := v.Children("class") + n.class.Walk(vv) } if n.arguments != nil { - fmt.Fprintf(out, "\n%varguments:", indent+" ") + vv := v.Children("arguments") for _, nn := range n.arguments { - nn.Print(out, indent+" ") + nn.Walk(vv) } } } diff --git a/node/expr/post_dec.go b/node/expr/post_dec.go index deb27d1..25d9dc6 100644 --- a/node/expr/post_dec.go +++ b/node/expr/post_dec.go @@ -1,9 +1,6 @@ package expr import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" ) @@ -23,11 +20,13 @@ func NewPostDec(variableession node.Node) node.Node { } } -func (n PostDec) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) +func (n PostDec) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.variable != nil { - fmt.Fprintf(out, "\n%vvariable:", indent+" ") - n.variable.Print(out, indent+" ") + vv := v.Children("variable") + n.variable.Walk(vv) } } diff --git a/node/expr/post_inc.go b/node/expr/post_inc.go index d036f4e..be9cccc 100644 --- a/node/expr/post_inc.go +++ b/node/expr/post_inc.go @@ -1,9 +1,6 @@ package expr import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" ) @@ -23,11 +20,13 @@ func NewPostInc(variableession node.Node) node.Node { } } -func (n PostInc) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) +func (n PostInc) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.variable != nil { - fmt.Fprintf(out, "\n%vvariable:", indent+" ") - n.variable.Print(out, indent+" ") + vv := v.Children("variable") + n.variable.Walk(vv) } } diff --git a/node/expr/pre_dec.go b/node/expr/pre_dec.go index 0b3d89d..2036ae6 100644 --- a/node/expr/pre_dec.go +++ b/node/expr/pre_dec.go @@ -1,9 +1,6 @@ package expr import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" ) @@ -23,11 +20,13 @@ func NewPreDec(variableession node.Node) node.Node { } } -func (n PreDec) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) +func (n PreDec) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.variable != nil { - fmt.Fprintf(out, "\n%vvariable:", indent+" ") - n.variable.Print(out, indent+" ") + vv := v.Children("variable") + n.variable.Walk(vv) } } diff --git a/node/expr/pre_inc.go b/node/expr/pre_inc.go index ca048da..3ddc59e 100644 --- a/node/expr/pre_inc.go +++ b/node/expr/pre_inc.go @@ -1,9 +1,6 @@ package expr import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" ) @@ -23,11 +20,13 @@ func NewPreInc(variableession node.Node) node.Node { } } -func (n PreInc) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) +func (n PreInc) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.variable != nil { - fmt.Fprintf(out, "\n%vvariable:", indent+" ") - n.variable.Print(out, indent+" ") + vv := v.Children("variable") + n.variable.Walk(vv) } } diff --git a/node/expr/print.go b/node/expr/print.go index ab2438f..6adcd5f 100644 --- a/node/expr/print.go +++ b/node/expr/print.go @@ -1,9 +1,6 @@ package expr import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" ) @@ -23,11 +20,13 @@ func NewPrint(expression node.Node) node.Node { } } -func (n Print) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) +func (n Print) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.expr != nil { - fmt.Fprintf(out, "\n%vexpr:", indent+" ") - n.expr.Print(out, indent+" ") + vv := v.Children("expr") + n.expr.Walk(vv) } } diff --git a/node/expr/property_fetch.go b/node/expr/property_fetch.go index 774823d..6469ba4 100644 --- a/node/expr/property_fetch.go +++ b/node/expr/property_fetch.go @@ -1,9 +1,6 @@ package expr import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" ) @@ -25,16 +22,18 @@ func NewPropertyFetch(variable node.Node, property node.Node) node.Node { } } -func (n PropertyFetch) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) +func (n PropertyFetch) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.variable != nil { - fmt.Fprintf(out, "\n%vvariable:", indent+" ") - n.variable.Print(out, indent+" ") + vv := v.Children("variable") + n.variable.Walk(vv) } if n.property != nil { - fmt.Fprintf(out, "\n%vproperty:", indent+" ") - n.property.Print(out, indent+" ") + vv := v.Children("property") + n.property.Walk(vv) } } diff --git a/node/expr/require.go b/node/expr/require.go index 2d13a17..0e6d7be 100644 --- a/node/expr/require.go +++ b/node/expr/require.go @@ -1,13 +1,10 @@ package expr import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" ) -func(n Require) Name() string { +func (n Require) Name() string { return "Require" } @@ -23,11 +20,13 @@ func NewRequire(expression node.Node) node.Node { } } -func (n Require) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) +func (n Require) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.expr != nil { - fmt.Fprintf(out, "\n%vexpr:", indent+" ") - n.expr.Print(out, indent+" ") + vv := v.Children("expr") + n.expr.Walk(vv) } } diff --git a/node/expr/require_once.go b/node/expr/require_once.go index af2bf4d..491fa54 100644 --- a/node/expr/require_once.go +++ b/node/expr/require_once.go @@ -1,9 +1,6 @@ package expr import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" ) @@ -23,11 +20,13 @@ func NewRequireOnce(expression node.Node) node.Node { } } -func (n RequireOnce) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) +func (n RequireOnce) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.expr != nil { - fmt.Fprintf(out, "\n%vexpr:", indent+" ") - n.expr.Print(out, indent+" ") + vv := v.Children("expr") + n.expr.Walk(vv) } } diff --git a/node/expr/shell_exec.go b/node/expr/shell_exec.go index 69b31a2..4b7bb6a 100644 --- a/node/expr/shell_exec.go +++ b/node/expr/shell_exec.go @@ -1,9 +1,6 @@ package expr import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" ) @@ -23,13 +20,15 @@ func NewShellExec(parts []node.Node) node.Node { } } -func (n ShellExec) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) +func (n ShellExec) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.parts != nil { - fmt.Fprintf(out, "\n%vparts:", indent+" ") + vv := v.Children("parts") for _, nn := range n.parts { - nn.Print(out, indent+" ") + nn.Walk(vv) } } } diff --git a/node/expr/short_array.go b/node/expr/short_array.go index 2d73efe..0f4b2e2 100644 --- a/node/expr/short_array.go +++ b/node/expr/short_array.go @@ -1,9 +1,6 @@ package expr import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) @@ -28,13 +25,15 @@ func NewShortArray(opentToken token.Token, closeToken token.Token, items []node. } } -func (n ShortArray) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d]", indent, n.name, n.opentToken.StartLine, n.closeToken.EndLine) +func (n ShortArray) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.items != nil { - fmt.Fprintf(out, "\n%vitems:", indent+" ") + vv := v.Children("items") for _, nn := range n.items { - nn.Print(out, indent+" ") + nn.Walk(vv) } } } diff --git a/node/expr/short_list.go b/node/expr/short_list.go index 3efbe46..d7c3ac3 100644 --- a/node/expr/short_list.go +++ b/node/expr/short_list.go @@ -1,9 +1,6 @@ package expr import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" ) @@ -23,13 +20,15 @@ func NewShortList(items []node.Node) node.Node { } } -func (n ShortList) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) +func (n ShortList) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.items != nil { - fmt.Fprintf(out, "\n%vitems:", indent+" ") + vv := v.Children("items") for _, nn := range n.items { - nn.Print(out, indent+" ") + nn.Walk(vv) } } } diff --git a/node/expr/static_call.go b/node/expr/static_call.go index b21f657..41ddce1 100644 --- a/node/expr/static_call.go +++ b/node/expr/static_call.go @@ -1,9 +1,6 @@ package expr import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" ) @@ -27,23 +24,25 @@ func NewStaticCall(class node.Node, call node.Node, arguments []node.Node) node. } } -func (n StaticCall) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) +func (n StaticCall) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.class != nil { - fmt.Fprintf(out, "\n%vclass:", indent+" ") - n.class.Print(out, indent+" ") + vv := v.Children("class") + n.class.Walk(vv) } if n.call != nil { - fmt.Fprintf(out, "\n%vcall:", indent+" ") - n.call.Print(out, indent+" ") + vv := v.Children("call") + n.call.Walk(vv) } if n.arguments != nil { - fmt.Fprintf(out, "\n%varguments:", indent+" ") + vv := v.Children("arguments") for _, nn := range n.arguments { - nn.Print(out, indent+" ") + nn.Walk(vv) } } } diff --git a/node/expr/static_property_fetch.go b/node/expr/static_property_fetch.go index 7163c3e..4a67fd4 100644 --- a/node/expr/static_property_fetch.go +++ b/node/expr/static_property_fetch.go @@ -1,9 +1,6 @@ package expr import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" ) @@ -25,16 +22,18 @@ func NewStaticPropertyFetch(class node.Node, property node.Node) node.Node { } } -func (n StaticPropertyFetch) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) +func (n StaticPropertyFetch) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.class != nil { - fmt.Fprintf(out, "\n%vclass:", indent+" ") - n.class.Print(out, indent+" ") + vv := v.Children("class") + n.class.Walk(vv) } if n.property != nil { - fmt.Fprintf(out, "\n%vproperty:", indent+" ") - n.property.Print(out, indent+" ") + vv := v.Children("property") + n.property.Walk(vv) } } diff --git a/node/expr/ternary.go b/node/expr/ternary.go index 8b6768e..9296c14 100644 --- a/node/expr/ternary.go +++ b/node/expr/ternary.go @@ -1,9 +1,6 @@ package expr import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" ) @@ -27,21 +24,23 @@ func NewTernary(condition node.Node, ifTrue node.Node, ifFalse node.Node) node.N } } -func (n Ternary) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) +func (n Ternary) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.condition != nil { - fmt.Fprintf(out, "\n%vcondition:", indent+" ") - n.condition.Print(out, indent+" ") + vv := v.Children("condition") + n.condition.Walk(vv) } if n.ifTrue != nil { - fmt.Fprintf(out, "\n%vifTrue:", indent+" ") - n.ifTrue.Print(out, indent+" ") + vv := v.Children("ifTrue") + n.ifTrue.Walk(vv) } if n.ifFalse != nil { - fmt.Fprintf(out, "\n%vifFalse:", indent+" ") - n.ifFalse.Print(out, indent+" ") + vv := v.Children("ifFalse") + n.ifFalse.Walk(vv) } } diff --git a/node/expr/unary_minus.go b/node/expr/unary_minus.go index f0cdc7c..4ecb90c 100644 --- a/node/expr/unary_minus.go +++ b/node/expr/unary_minus.go @@ -1,9 +1,6 @@ package expr import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" ) @@ -23,11 +20,13 @@ func NewUnaryMinus(expression node.Node) node.Node { } } -func (n UnaryMinus) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) +func (n UnaryMinus) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.expr != nil { - fmt.Fprintf(out, "\n%vexpr:", indent+" ") - n.expr.Print(out, indent+" ") + vv := v.Children("expr") + n.expr.Walk(vv) } } diff --git a/node/expr/unary_plus.go b/node/expr/unary_plus.go index aff43d3..c0bb60d 100644 --- a/node/expr/unary_plus.go +++ b/node/expr/unary_plus.go @@ -1,9 +1,6 @@ package expr import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" ) @@ -23,11 +20,13 @@ func NewUnaryPlus(expression node.Node) node.Node { } } -func (n UnaryPlus) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) +func (n UnaryPlus) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.expr != nil { - fmt.Fprintf(out, "\n%vexpr:", indent+" ") - n.expr.Print(out, indent+" ") + vv := v.Children("expr") + n.expr.Walk(vv) } } diff --git a/node/expr/variable.go b/node/expr/variable.go index 3213d05..3d0e787 100644 --- a/node/expr/variable.go +++ b/node/expr/variable.go @@ -1,9 +1,6 @@ package expr import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" ) @@ -12,22 +9,24 @@ func (n Variable) Name() string { } type Variable struct { - name string - variable node.Node + name string + varName node.Node } -func NewVariable(variable node.Node) node.Node { +func NewVariable(varName node.Node) node.Node { return Variable{ "Variable", - variable, + varName, } } -func (n Variable) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) +func (n Variable) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } - if n.variable != nil { - fmt.Fprintf(out, "\n%vvariable:", indent+" ") - n.variable.Print(out, indent+" ") + if n.varName != nil { + vv := v.Children("varName") + n.varName.Walk(vv) } } diff --git a/node/expr/yield.go b/node/expr/yield.go index 4de1c01..470aceb 100644 --- a/node/expr/yield.go +++ b/node/expr/yield.go @@ -1,9 +1,6 @@ package expr import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" ) @@ -25,16 +22,18 @@ func NewYield(key node.Node, value node.Node) node.Node { } } -func (n Yield) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) +func (n Yield) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.key != nil { - fmt.Fprintf(out, "\n%vkey:", indent+" ") - n.key.Print(out, indent+" ") + vv := v.Children("key") + n.key.Walk(vv) } if n.value != nil { - fmt.Fprintf(out, "\n%vvalue:", indent+" ") - n.value.Print(out, indent+" ") + vv := v.Children("value") + n.value.Walk(vv) } } diff --git a/node/expr/yield_from.go b/node/expr/yield_from.go index 95006a0..78f6a7f 100644 --- a/node/expr/yield_from.go +++ b/node/expr/yield_from.go @@ -1,9 +1,6 @@ package expr import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" ) @@ -23,11 +20,13 @@ func NewYieldFrom(expression node.Node) node.Node { } } -func (n YieldFrom) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) +func (n YieldFrom) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.expr != nil { - fmt.Fprintf(out, "\n%vexpr:", indent+" ") - n.expr.Print(out, indent+" ") + vv := v.Children("expr") + n.expr.Walk(vv) } } diff --git a/node/identifier.go b/node/identifier.go index 45b6b15..4d38366 100644 --- a/node/identifier.go +++ b/node/identifier.go @@ -1,9 +1,6 @@ package node import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/token" ) @@ -23,7 +20,10 @@ func NewIdentifier(token token.Token) Node { } } -func (n Identifier) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) - fmt.Fprintf(out, "\n%vname: %q", indent+" ", n.token.Value) +func (n Identifier) Walk(v Visitor) { + if v.Visit(n) == false { + return + } + + v.Scalar("token", n.token.Value) } diff --git a/node/name/name.go b/node/name/name.go index bd2dcbd..30c5eb6 100644 --- a/node/name/name.go +++ b/node/name/name.go @@ -1,9 +1,6 @@ package name import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" ) @@ -23,13 +20,15 @@ func NewName(parts []node.Node) node.Node { } } -func (n NameNode) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v", indent, n.name) +func (n NameNode) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.parts != nil { - fmt.Fprintf(out, "\n%vparts:", indent+" ") + vv := v.Children("parts") for _, nn := range n.parts { - nn.Print(out, indent+" ") + nn.Walk(vv) } } } diff --git a/node/name/name_part.go b/node/name/name_part.go index e5c59c5..56f35d8 100644 --- a/node/name/name_part.go +++ b/node/name/name_part.go @@ -1,14 +1,11 @@ package name import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n NamePart) Name() string { +func (n NamePart) Name() string { return "NamePart" } @@ -24,6 +21,10 @@ func NewNamePart(token token.Token) node.Node { } } -func (n NamePart) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.StartLine, n.token.EndLine, n.token.Value) +func (n NamePart) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + v.Scalar("token", n.token.Value) } diff --git a/node/node.go b/node/node.go index 6be4d59..0dca0ac 100644 --- a/node/node.go +++ b/node/node.go @@ -1,10 +1,6 @@ package node -import ( - "io" -) - type Node interface { Name() string - Print(out io.Writer, indent string) + Walk(v Visitor) } diff --git a/node/nullable.go b/node/nullable.go index e08e787..da751c3 100644 --- a/node/nullable.go +++ b/node/nullable.go @@ -1,10 +1,5 @@ package node -import ( - "fmt" - "io" -) - type Nullable struct { name string expr Node @@ -21,11 +16,13 @@ func NewNullable(expression Node) Node { } } -func (n Nullable) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) +func (n Nullable) Walk(v Visitor) { + if v.Visit(n) == false { + return + } if n.expr != nil { - fmt.Fprintf(out, "\n%vexpr:", indent+" ") - n.expr.Print(out, indent+" ") + vv := v.Children("expr") + n.expr.Walk(vv) } } diff --git a/node/parameter.go b/node/parameter.go index 2c0514f..90e35e1 100644 --- a/node/parameter.go +++ b/node/parameter.go @@ -1,10 +1,5 @@ package node -import ( - "fmt" - "io" -) - type Parameter struct { name string variableType Node @@ -29,24 +24,26 @@ func NewParameter(variableType Node, variable Node, defaultValue Node, byRef boo } } -func (n Parameter) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) - fmt.Fprintf(out, "\n%vbyRef: %t", indent+" ", n.byRef) - fmt.Fprintf(out, "\n%vvariadic: %t", indent+" ", n.variadic) +func (n Parameter) Walk(v Visitor) { + if v.Visit(n) == false { + return + } + + v.Scalar("byRef", n.byRef) + v.Scalar("variadic", n.variadic) if n.variableType != nil { - fmt.Fprintf(out, "\n%vvariableType:", indent+" ") - n.variableType.Print(out, indent+" ") + vv := v.Children("variableType") + n.variableType.Walk(vv) } if n.variable != nil { - fmt.Fprintf(out, "\n%vvariable:", indent+" ") - n.variable.Print(out, indent+" ") + vv := v.Children("variable") + n.variable.Walk(vv) } if n.defaultValue != nil { - fmt.Fprintf(out, "\n%vdefaultValue:", indent+" ") - n.defaultValue.Print(out, indent+" ") + vv := v.Children("defaultValue") + n.defaultValue.Walk(vv) } - } diff --git a/node/scalar/dnumber.go b/node/scalar/dnumber.go index 1169782..5c8bd96 100644 --- a/node/scalar/dnumber.go +++ b/node/scalar/dnumber.go @@ -1,14 +1,11 @@ package scalar import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n Dnumber) Name() string { +func (n Dnumber) Name() string { return "Dnumber" } @@ -24,6 +21,10 @@ func NewDnumber(token token.Token) node.Node { } } -func (n Dnumber) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.StartLine, n.token.EndLine, n.token.Value) +func (n Dnumber) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + v.Scalar("token", n.token.Value) } diff --git a/node/scalar/encapsed.go b/node/scalar/encapsed.go index 26c3361..6ebeaf8 100644 --- a/node/scalar/encapsed.go +++ b/node/scalar/encapsed.go @@ -1,14 +1,11 @@ package scalar import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n Encapsed) Name() string { +func (n Encapsed) Name() string { return "Encapsed" } @@ -28,13 +25,15 @@ func NewEncapsed(startToken token.Token, parts []node.Node, endToken token.Token } } -func (n Encapsed) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d]", indent, n.name, n.startToken.StartLine, n.endToken.EndLine) +func (n Encapsed) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.parts != nil { - fmt.Fprintf(out, "\n%vparts:", indent+" ") + vv := v.Children("parts") for _, nn := range n.parts { - nn.Print(out, indent+" ") + nn.Walk(vv) } } } diff --git a/node/scalar/encapsed_string_part.go b/node/scalar/encapsed_string_part.go index 0c8f77b..41ea6cf 100644 --- a/node/scalar/encapsed_string_part.go +++ b/node/scalar/encapsed_string_part.go @@ -1,14 +1,11 @@ package scalar import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n EncapsedStringPart) Name() string { +func (n EncapsedStringPart) Name() string { return "EncapsedStringPart" } @@ -24,6 +21,10 @@ func NewEncapsedStringPart(t token.Token) node.Node { } } -func (n EncapsedStringPart) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.StartLine, n.token.EndLine, n.token.Value) +func (n EncapsedStringPart) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + v.Scalar("token", n.token.Value) } diff --git a/node/scalar/lnumber.go b/node/scalar/lnumber.go index 47c65df..68cb21d 100644 --- a/node/scalar/lnumber.go +++ b/node/scalar/lnumber.go @@ -1,14 +1,11 @@ package scalar import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n Lnumber) Name() string { +func (n Lnumber) Name() string { return "Lnumber" } @@ -24,6 +21,10 @@ func NewLnumber(token token.Token) node.Node { } } -func (n Lnumber) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.StartLine, n.token.EndLine, n.token.Value) +func (n Lnumber) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + v.Scalar("token", n.token.Value) } diff --git a/node/scalar/magic_constant.go b/node/scalar/magic_constant.go index 6dfaf9c..0993927 100644 --- a/node/scalar/magic_constant.go +++ b/node/scalar/magic_constant.go @@ -1,14 +1,11 @@ package scalar import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n MagicConstant) Name() string { +func (n MagicConstant) Name() string { return "MagicConstant" } @@ -24,6 +21,10 @@ func NewMagicConstant(token token.Token) node.Node { } } -func (n MagicConstant) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.StartLine, n.token.EndLine, n.token.Value) +func (n MagicConstant) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + v.Scalar("token", n.token.Value) } diff --git a/node/scalar/string.go b/node/scalar/string.go index 400825a..82784e0 100644 --- a/node/scalar/string.go +++ b/node/scalar/string.go @@ -1,14 +1,11 @@ package scalar import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n String) Name() string { +func (n String) Name() string { return "String" } @@ -24,6 +21,10 @@ func NewString(token token.Token) node.Node { } } -func (n String) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.StartLine, n.token.EndLine, n.token.Value) +func (n String) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + v.Scalar("token", n.token.Value) } diff --git a/node/stmt/alt_else.go b/node/stmt/alt_else.go index 65bce03..4e5d768 100644 --- a/node/stmt/alt_else.go +++ b/node/stmt/alt_else.go @@ -1,14 +1,11 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n AltElse) Name() string { +func (n AltElse) Name() string { return "AltElse" } @@ -26,11 +23,13 @@ func NewAltElse(token token.Token, stmt node.Node) node.Node { } } -func (n AltElse) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.StartLine, n.token.EndLine, n.token.Value) +func (n AltElse) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.stmt != nil { - fmt.Fprintf(out, "\n%vstmt:", indent+" ") - n.stmt.Print(out, indent+" ") + vv := v.Children("stmt") + n.stmt.Walk(vv) } } diff --git a/node/stmt/alt_else_if.go b/node/stmt/alt_else_if.go index 9826698..c88cbfa 100644 --- a/node/stmt/alt_else_if.go +++ b/node/stmt/alt_else_if.go @@ -1,14 +1,11 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n AltElseIf) Name() string { +func (n AltElseIf) Name() string { return "AltElseIf" } @@ -28,16 +25,18 @@ func NewAltElseIf(token token.Token, cond node.Node, stmt node.Node) node.Node { } } -func (n AltElseIf) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.StartLine, n.token.EndLine, n.token.Value) +func (n AltElseIf) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.cond != nil { - fmt.Fprintf(out, "\n%vcond:", indent+" ") - n.cond.Print(out, indent+" ") + vv := v.Children("cond") + n.cond.Walk(vv) } if n.stmt != nil { - fmt.Fprintf(out, "\n%vstmt:", indent+" ") - n.stmt.Print(out, indent+" ") + vv := v.Children("stmt") + n.stmt.Walk(vv) } } diff --git a/node/stmt/alt_if.go b/node/stmt/alt_if.go index ab58dd5..7deca96 100644 --- a/node/stmt/alt_if.go +++ b/node/stmt/alt_if.go @@ -1,24 +1,21 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n AltIf) Name() string { +func (n AltIf) Name() string { return "AltIf" } type AltIf struct { - name string - token token.Token - cond node.Node - stmt node.Node - elseAltIf []node.Node - _else node.Node + name string + token token.Token + cond node.Node + stmt node.Node + elseIf []node.Node + _else node.Node } func NewAltIf(token token.Token, cond node.Node, stmt node.Node) node.Node { @@ -32,12 +29,12 @@ func NewAltIf(token token.Token, cond node.Node, stmt node.Node) node.Node { } } -func (n AltIf) AddElseIf(elseAltIf node.Node) node.Node { - if n.elseAltIf == nil { - n.elseAltIf = make([]node.Node, 0) +func (n AltIf) AddElseIf(elseIf node.Node) node.Node { + if n.elseIf == nil { + n.elseIf = make([]node.Node, 0) } - n.elseAltIf = append(n.elseAltIf, elseAltIf) + n.elseIf = append(n.elseIf, elseIf) return n } @@ -48,27 +45,30 @@ func (n AltIf) SetElse(_else node.Node) node.Node { return n } -func (n AltIf) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.StartLine, n.token.EndLine, n.token.Value) +func (n AltIf) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.cond != nil { - fmt.Fprintf(out, "\n%vcond:", indent+" ") - n.cond.Print(out, indent+" ") + vv := v.Children("cond") + n.cond.Walk(vv) } if n.stmt != nil { - fmt.Fprintf(out, "\n%vstmt:", indent+" ") - n.stmt.Print(out, indent+" ") + vv := v.Children("stmt") + n.stmt.Walk(vv) } - if n.elseAltIf != nil { - fmt.Fprintf(out, "\n%velseAltIfs:", indent+" ") - for _, nn := range n.elseAltIf { - nn.Print(out, indent+" ") + if n.elseIf != nil { + vv := v.Children("elseIf") + for _, nn := range n.elseIf { + nn.Walk(vv) } } + if n._else != nil { - fmt.Fprintf(out, "\n%velse:", indent+" ") - n._else.Print(out, indent+" ") + vv := v.Children("else") + n._else.Walk(vv) } } diff --git a/node/stmt/break.go b/node/stmt/break.go index 1a72f55..9749b15 100644 --- a/node/stmt/break.go +++ b/node/stmt/break.go @@ -1,14 +1,11 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n Break) Name() string { +func (n Break) Name() string { return "Break" } @@ -26,11 +23,13 @@ func NewBreak(token token.Token, expr node.Node) node.Node { } } -func (n Break) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.StartLine, n.token.EndLine, n.token.Value) +func (n Break) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.expr != nil { - fmt.Fprintf(out, "\n%vexpr:", indent+" ") - n.expr.Print(out, indent+" ") + vv := v.Children("expr") + n.expr.Walk(vv) } } diff --git a/node/stmt/case.go b/node/stmt/case.go index ca1069e..05f9326 100644 --- a/node/stmt/case.go +++ b/node/stmt/case.go @@ -1,14 +1,11 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n Case) Name() string { +func (n Case) Name() string { return "Case" } @@ -28,16 +25,20 @@ func NewCase(token token.Token, cond node.Node, stmts []node.Node) node.Node { } } -func (n Case) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.StartLine, n.token.EndLine, n.token.Value) +func (n Case) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } - fmt.Fprintf(out, "\n%vcond:", indent+" ") - n.cond.Print(out, indent+" ") + if n.cond != nil { + vv := v.Children("cond") + n.cond.Walk(vv) + } if n.stmts != nil { - fmt.Fprintf(out, "\n%vstmts:", indent+" ") + vv := v.Children("stmts") for _, nn := range n.stmts { - nn.Print(out, indent+" ") + nn.Walk(vv) } } } diff --git a/node/stmt/catch.go b/node/stmt/catch.go index d4d541f..1444786 100644 --- a/node/stmt/catch.go +++ b/node/stmt/catch.go @@ -1,14 +1,11 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n Catch) Name() string { +func (n Catch) Name() string { return "Catch" } @@ -30,21 +27,22 @@ func NewCatch(token token.Token, types []node.Node, variable node.Node, stmts [] } } -func (n Catch) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.StartLine, n.token.EndLine, n.token.Value) - - fmt.Fprintf(out, "\n%vtypes:", indent+" ") - for _, nn := range n.types { - nn.Print(out, indent+" ") +func (n Catch) Walk(v node.Visitor) { + if v.Visit(n) == false { + return } - fmt.Fprintf(out, "\n%vvariable:", indent+" ") - n.variable.Print(out, indent+" ") + if n.types != nil { + vv := v.Children("types") + for _, nn := range n.types { + nn.Walk(vv) + } + } if n.stmts != nil { - fmt.Fprintf(out, "\n%vstmts:", indent+" ") + vv := v.Children("stmts") for _, nn := range n.stmts { - nn.Print(out, indent+" ") + nn.Walk(vv) } } } diff --git a/node/stmt/class.go b/node/stmt/class.go index 4f4ba53..cf32ea8 100644 --- a/node/stmt/class.go +++ b/node/stmt/class.go @@ -1,14 +1,11 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n Class) Name() string { +func (n Class) Name() string { return "Class" } @@ -34,39 +31,43 @@ func NewClass(token token.Token, modifiers []node.Node, args []node.Node, extend } } -func (n Class) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.StartLine, n.token.EndLine, n.token.Value) +func (n Class) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + v.Scalar("token", n.token.Value) if n.modifiers != nil { - fmt.Fprintf(out, "\n%vmotifiers:", indent+" ") + vv := v.Children("modifiers") for _, nn := range n.modifiers { - nn.Print(out, indent+" ") + nn.Walk(vv) } } if n.args != nil { - fmt.Fprintf(out, "\n%vargs:", indent+" ") + vv := v.Children("args") for _, nn := range n.args { - nn.Print(out, indent+" ") + nn.Walk(vv) } } if n.extends != nil { - fmt.Fprintf(out, "\n%vextends:", indent+" ") - n.extends.Print(out, indent+" ") + vv := v.Children("extends") + n.extends.Walk(vv) } if n.implements != nil { - fmt.Fprintf(out, "\n%vimplements:", indent+" ") + vv := v.Children("implements") for _, nn := range n.implements { - nn.Print(out, indent+" ") + nn.Walk(vv) } } if n.stmts != nil { - fmt.Fprintf(out, "\n%vstmts:", indent+" ") + vv := v.Children("stmts") for _, nn := range n.stmts { - nn.Print(out, indent+" ") + nn.Walk(vv) } } } diff --git a/node/stmt/class_const_list.go b/node/stmt/class_const_list.go index 91e7153..20e08f6 100644 --- a/node/stmt/class_const_list.go +++ b/node/stmt/class_const_list.go @@ -1,14 +1,11 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n ClassConstList) Name() string { +func (n ClassConstList) Name() string { return "ClassConstList" } @@ -28,20 +25,22 @@ func NewClassConstList(token token.Token, modifiers []node.Node, consts []node.N } } -func (n ClassConstList) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.StartLine, n.token.EndLine, n.token.Value) +func (n ClassConstList) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.modifiers != nil { - fmt.Fprintf(out, "\n%vmotifiers:", indent+" ") + vv := v.Children("modifiers") for _, nn := range n.modifiers { - nn.Print(out, indent+" ") + nn.Walk(vv) } } if n.consts != nil { - fmt.Fprintf(out, "\n%vconsts:", indent+" ") + vv := v.Children("consts") for _, nn := range n.consts { - nn.Print(out, indent+" ") + nn.Walk(vv) } } } diff --git a/node/stmt/class_method.go b/node/stmt/class_method.go index 3f1dca3..c3a429c 100644 --- a/node/stmt/class_method.go +++ b/node/stmt/class_method.go @@ -1,14 +1,11 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n ClassMethod) Name() string { +func (n ClassMethod) Name() string { return "ClassMethod" } @@ -34,34 +31,37 @@ func NewClassMethod(token token.Token, modifiers []node.Node, isReturnRef bool, } } -func (n ClassMethod) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.StartLine, n.token.EndLine, n.token.Value) +func (n ClassMethod) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + v.Scalar("token", n.token.Value) + v.Scalar("isReturnRef", n.isReturnRef) if n.modifiers != nil { - fmt.Fprintf(out, "\n%vmodifiers:", indent+" ") + vv := v.Children("modifiers") for _, nn := range n.modifiers { - nn.Print(out, indent+" ") + nn.Walk(vv) } } - fmt.Fprintf(out, "\n%vreturn ref: %t", indent+" ", n.isReturnRef) - if n.params != nil { - fmt.Fprintf(out, "\n%vparams:", indent+" ") + vv := v.Children("params") for _, nn := range n.params { - nn.Print(out, indent+" ") + nn.Walk(vv) } } if n.returnType != nil { - fmt.Fprintf(out, "\n%vreturn type:", indent+" ") - n.returnType.Print(out, indent+" ") + vv := v.Children("returnType") + n.returnType.Walk(vv) } if n.stmts != nil { - fmt.Fprintf(out, "\n%vstmts:", indent+" ") + vv := v.Children("stmts") for _, nn := range n.stmts { - nn.Print(out, indent+" ") + nn.Walk(vv) } } } diff --git a/node/stmt/const_list.go b/node/stmt/const_list.go index bf6cec3..f27daff 100644 --- a/node/stmt/const_list.go +++ b/node/stmt/const_list.go @@ -1,14 +1,11 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n ConstList) Name() string { +func (n ConstList) Name() string { return "ConstList" } @@ -26,13 +23,15 @@ func NewConstList(token token.Token, consts []node.Node) node.Node { } } -func (n ConstList) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.StartLine, n.token.EndLine, n.token.Value) +func (n ConstList) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.consts != nil { - fmt.Fprintf(out, "\n%vconsts:", indent+" ") + vv := v.Children("consts") for _, nn := range n.consts { - nn.Print(out, indent+" ") + nn.Walk(vv) } } } diff --git a/node/stmt/constant.go b/node/stmt/constant.go index 9567040..05bcb84 100644 --- a/node/stmt/constant.go +++ b/node/stmt/constant.go @@ -1,14 +1,11 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n Constant) Name() string { +func (n Constant) Name() string { return "Constant" } @@ -26,9 +23,15 @@ func NewConstant(token token.Token, expr node.Node) node.Node { } } -func (n Constant) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.StartLine, n.token.EndLine, n.token.Value) +func (n Constant) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } - fmt.Fprintf(out, "\n%vexpr:", indent+" ") - n.expr.Print(out, indent+" ") + v.Scalar("token", n.token.Value) + + if n.expr != nil { + vv := v.Children("expr") + n.expr.Walk(vv) + } } diff --git a/node/stmt/continue.go b/node/stmt/continue.go index e5a02fc..d240bee 100644 --- a/node/stmt/continue.go +++ b/node/stmt/continue.go @@ -1,14 +1,11 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n Continue) Name() string { +func (n Continue) Name() string { return "Continue" } @@ -26,11 +23,13 @@ func NewContinue(token token.Token, expr node.Node) node.Node { } } -func (n Continue) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.StartLine, n.token.EndLine, n.token.Value) +func (n Continue) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.expr != nil { - fmt.Fprintf(out, "\n%vexpr:", indent+" ") - n.expr.Print(out, indent+" ") + vv := v.Children("expr") + n.expr.Walk(vv) } } diff --git a/node/stmt/declare.go b/node/stmt/declare.go index 9d453e4..12c0a68 100644 --- a/node/stmt/declare.go +++ b/node/stmt/declare.go @@ -1,14 +1,11 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n Declare) Name() string { +func (n Declare) Name() string { return "Declare" } @@ -28,18 +25,20 @@ func NewDeclare(token token.Token, consts []node.Node, stmt node.Node) node.Node } } -func (n Declare) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.StartLine, n.token.EndLine, n.token.Value) +func (n Declare) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.consts != nil { - fmt.Fprintf(out, "\n%vconsts:", indent+" ") + vv := v.Children("consts") for _, nn := range n.consts { - nn.Print(out, indent+" ") + nn.Walk(vv) } } if n.stmt != nil { - fmt.Fprintf(out, "\n%vstmt:", indent+" ") - n.stmt.Print(out, indent+" ") + vv := v.Children("stmt") + n.stmt.Walk(vv) } } diff --git a/node/stmt/default.go b/node/stmt/default.go index 6e3fb47..f54bcae 100644 --- a/node/stmt/default.go +++ b/node/stmt/default.go @@ -1,14 +1,11 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n Default) Name() string { +func (n Default) Name() string { return "Default" } @@ -26,13 +23,15 @@ func NewDefault(token token.Token, stmts []node.Node) node.Node { } } -func (n Default) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.StartLine, n.token.EndLine, n.token.Value) +func (n Default) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.stmts != nil { - fmt.Fprintf(out, "\n%vstmts:", indent+" ") + vv := v.Children("stmts") for _, nn := range n.stmts { - nn.Print(out, indent+" ") + nn.Walk(vv) } } } diff --git a/node/stmt/do.go b/node/stmt/do.go index 3fd920b..f9b6776 100644 --- a/node/stmt/do.go +++ b/node/stmt/do.go @@ -1,14 +1,11 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n Do) Name() string { +func (n Do) Name() string { return "Do" } @@ -28,16 +25,18 @@ func NewDo(token token.Token, stmt node.Node, cond node.Node) node.Node { } } -func (n Do) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.StartLine, n.token.EndLine, n.token.Value) +func (n Do) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.cond != nil { - fmt.Fprintf(out, "\n%vcond:", indent+" ") - n.cond.Print(out, indent+" ") + vv := v.Children("cond") + n.cond.Walk(vv) } if n.stmt != nil { - fmt.Fprintf(out, "\n%vstmt:", indent+" ") - n.stmt.Print(out, indent+" ") + vv := v.Children("stmt") + n.stmt.Walk(vv) } } diff --git a/node/stmt/echo.go b/node/stmt/echo.go index 9c8b097..0912614 100644 --- a/node/stmt/echo.go +++ b/node/stmt/echo.go @@ -1,14 +1,11 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n Echo) Name() string { +func (n Echo) Name() string { return "Echo" } @@ -26,13 +23,15 @@ func NewEcho(token token.Token, exprs []node.Node) node.Node { } } -func (n Echo) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.StartLine, n.token.EndLine, n.token.Value) +func (n Echo) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.exprs != nil { - fmt.Fprintf(out, "\n%vexprs:", indent+" ") + vv := v.Children("exprs") for _, nn := range n.exprs { - nn.Print(out, indent+" ") + nn.Walk(vv) } } } diff --git a/node/stmt/else.go b/node/stmt/else.go index 71d5a49..ab3f233 100644 --- a/node/stmt/else.go +++ b/node/stmt/else.go @@ -1,14 +1,11 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n Else) Name() string { +func (n Else) Name() string { return "Else" } @@ -26,11 +23,13 @@ func NewElse(token token.Token, stmt node.Node) node.Node { } } -func (n Else) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.StartLine, n.token.EndLine, n.token.Value) +func (n Else) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.stmt != nil { - fmt.Fprintf(out, "\n%vstmt:", indent+" ") - n.stmt.Print(out, indent+" ") + vv := v.Children("stmt") + n.stmt.Walk(vv) } } diff --git a/node/stmt/else_if.go b/node/stmt/else_if.go index 56b12a4..3a1f87a 100644 --- a/node/stmt/else_if.go +++ b/node/stmt/else_if.go @@ -1,14 +1,11 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n ElseIf) Name() string { +func (n ElseIf) Name() string { return "ElseIf" } @@ -28,16 +25,18 @@ func NewElseIf(token token.Token, cond node.Node, stmt node.Node) node.Node { } } -func (n ElseIf) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.StartLine, n.token.EndLine, n.token.Value) +func (n ElseIf) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.cond != nil { - fmt.Fprintf(out, "\n%vcond:", indent+" ") - n.cond.Print(out, indent+" ") + vv := v.Children("cond") + n.cond.Walk(vv) } if n.stmt != nil { - fmt.Fprintf(out, "\n%vstmt:", indent+" ") - n.stmt.Print(out, indent+" ") + vv := v.Children("stmt") + n.stmt.Walk(vv) } } diff --git a/node/stmt/expression.go b/node/stmt/expression.go index 1876636..410f8df 100644 --- a/node/stmt/expression.go +++ b/node/stmt/expression.go @@ -1,13 +1,10 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" ) -func(n Expression) Name() string { +func (n Expression) Name() string { return "Expression" } @@ -23,11 +20,13 @@ func NewExpression(expr node.Node) node.Node { } } -func (n Expression) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) +func (n Expression) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.expr != nil { - fmt.Fprintf(out, "\n%vexpr:", indent+" ") - n.expr.Print(out, indent+" ") + vv := v.Children("expr") + n.expr.Walk(vv) } } diff --git a/node/stmt/finally.go b/node/stmt/finally.go index 7823205..918fa99 100644 --- a/node/stmt/finally.go +++ b/node/stmt/finally.go @@ -1,14 +1,11 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n Finally) Name() string { +func (n Finally) Name() string { return "Finally" } @@ -26,13 +23,15 @@ func NewFinally(token token.Token, stmts []node.Node) node.Node { } } -func (n Finally) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.StartLine, n.token.EndLine, n.token.Value) +func (n Finally) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.stmts != nil { - fmt.Fprintf(out, "\n%vstmts:", indent+" ") + vv := v.Children("stmts") for _, nn := range n.stmts { - nn.Print(out, indent+" ") + nn.Walk(vv) } } } diff --git a/node/stmt/for.go b/node/stmt/for.go index f572931..7458418 100644 --- a/node/stmt/for.go +++ b/node/stmt/for.go @@ -1,14 +1,11 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n For) Name() string { +func (n For) Name() string { return "For" } @@ -32,32 +29,34 @@ func NewFor(token token.Token, init []node.Node, cond []node.Node, loop []node.N } } -func (n For) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.StartLine, n.token.EndLine, n.token.Value) +func (n For) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.init != nil { - fmt.Fprintf(out, "\n%vinit:", indent+" ") + vv := v.Children("init") for _, nn := range n.init { - nn.Print(out, indent+" ") + nn.Walk(vv) } } if n.cond != nil { - fmt.Fprintf(out, "\n%vcond:", indent+" ") + vv := v.Children("cond") for _, nn := range n.cond { - nn.Print(out, indent+" ") + nn.Walk(vv) } } if n.loop != nil { - fmt.Fprintf(out, "\n%vloop:", indent+" ") + vv := v.Children("loop") for _, nn := range n.loop { - nn.Print(out, indent+" ") + nn.Walk(vv) } } if n.stmt != nil { - fmt.Fprintf(out, "\n%vstmt:", indent+" ") - n.stmt.Print(out, indent+" ") + vv := v.Children("stmt") + n.stmt.Walk(vv) } } diff --git a/node/stmt/foreach.go b/node/stmt/foreach.go index a1ef5c9..aed5fc2 100644 --- a/node/stmt/foreach.go +++ b/node/stmt/foreach.go @@ -1,14 +1,11 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n Foreach) Name() string { +func (n Foreach) Name() string { return "Foreach" } @@ -34,26 +31,28 @@ func NewForeach(token token.Token, expr node.Node, key node.Node, variable node. } } -func (n Foreach) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.StartLine, n.token.EndLine, n.token.Value) +func (n Foreach) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.expr != nil { - fmt.Fprintf(out, "\n%vexpr:", indent+" ") - n.expr.Print(out, indent+" ") + vv := v.Children("expr") + n.expr.Walk(vv) } if n.key != nil { - fmt.Fprintf(out, "\n%vkey:", indent+" ") - n.key.Print(out, indent+" ") + vv := v.Children("key") + n.key.Walk(vv) } if n.variable != nil { - fmt.Fprintf(out, "\n%vvariable[byRef: %t]:", indent+" ", n.byRef) - n.variable.Print(out, indent+" ") + vv := v.Children("variable") + n.variable.Walk(vv) } if n.stmt != nil { - fmt.Fprintf(out, "\n%vstmt:", indent+" ") - n.stmt.Print(out, indent+" ") + vv := v.Children("stmt") + n.stmt.Walk(vv) } } diff --git a/node/stmt/function.go b/node/stmt/function.go index 83b1a7f..9400d86 100644 --- a/node/stmt/function.go +++ b/node/stmt/function.go @@ -1,14 +1,11 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n Function) Name() string { +func (n Function) Name() string { return "Function" } @@ -32,27 +29,30 @@ func NewFunction(token token.Token, isReturnRef bool, params []node.Node, return } } -func (n Function) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.StartLine, n.token.EndLine, n.token.Value) +func (n Function) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } - fmt.Fprintf(out, "\n%vreturn ref: %t", indent+" ", n.isReturnRef) + v.Scalar("token", n.token.Value) + v.Scalar("isReturnRef", n.isReturnRef) if n.params != nil { - fmt.Fprintf(out, "\n%vparams:", indent+" ") + vv := v.Children("params") for _, nn := range n.params { - nn.Print(out, indent+" ") + nn.Walk(vv) } } if n.returnType != nil { - fmt.Fprintf(out, "\n%vreturn type:", indent+" ") - n.returnType.Print(out, indent+" ") + vv := v.Children("returnType") + n.returnType.Walk(vv) } if n.stmts != nil { - fmt.Fprintf(out, "\n%vstmts:", indent+" ") + vv := v.Children("stmts") for _, nn := range n.stmts { - nn.Print(out, indent+" ") + nn.Walk(vv) } } } diff --git a/node/stmt/global.go b/node/stmt/global.go index 84c25bb..50f11e8 100644 --- a/node/stmt/global.go +++ b/node/stmt/global.go @@ -1,14 +1,11 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n Global) Name() string { +func (n Global) Name() string { return "Global" } @@ -26,13 +23,15 @@ func NewGlobal(token token.Token, vars []node.Node) node.Node { } } -func (n Global) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.StartLine, n.token.EndLine, n.token.Value) +func (n Global) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.vars != nil { - fmt.Fprintf(out, "\n%vvars:", indent+" ") + vv := v.Children("vars") for _, nn := range n.vars { - nn.Print(out, indent+" ") + nn.Walk(vv) } } } diff --git a/node/stmt/goto.go b/node/stmt/goto.go index c3311f2..f66e84d 100644 --- a/node/stmt/goto.go +++ b/node/stmt/goto.go @@ -1,14 +1,11 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n Goto) Name() string { +func (n Goto) Name() string { return "Goto" } @@ -18,14 +15,19 @@ type Goto struct { label token.Token } -func NewGoto(token token.Token, name token.Token) node.Node { +// todl label must be identifier +func NewGoto(token token.Token, label token.Token) node.Node { return Goto{ "Goto", token, - name, + label, } } -func (n Goto) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.StartLine, n.label.EndLine, n.label.Value) +func (n Goto) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + v.Scalar("label", n.label.Value) } diff --git a/node/stmt/group_use.go b/node/stmt/group_use.go index 745cb85..cc3918b 100644 --- a/node/stmt/group_use.go +++ b/node/stmt/group_use.go @@ -1,14 +1,11 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n GroupUse) Name() string { +func (n GroupUse) Name() string { return "GroupUse" } @@ -41,23 +38,25 @@ func (n GroupUse) SetUseType(useType node.Node) node.Node { return n } -func (n GroupUse) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.GetStartLine(), n.token.GetEndLine(), n.token.GetValue()) +func (n GroupUse) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.useType != nil { - fmt.Fprintf(out, "\n%vuse type:", indent+" ") - n.useType.Print(out, indent+" ") + vv := v.Children("useType") + n.useType.Walk(vv) } if n.prefix != nil { - fmt.Fprintf(out, "\n%vprefix:", indent+" ") - n.prefix.Print(out, indent+" ") + vv := v.Children("prefix") + n.prefix.Walk(vv) } if n.useList != nil { - fmt.Fprintf(out, "\n%vuse list:", indent+" ") + vv := v.Children("useList") for _, nn := range n.useList { - nn.Print(out, indent+" ") + nn.Walk(vv) } } } diff --git a/node/stmt/halt_compiler.go b/node/stmt/halt_compiler.go index 7636ead..9e0f66a 100644 --- a/node/stmt/halt_compiler.go +++ b/node/stmt/halt_compiler.go @@ -1,14 +1,11 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n HaltCompiler) Name() string { +func (n HaltCompiler) Name() string { return "HaltCompiler" } @@ -24,6 +21,8 @@ func NewHaltCompiler(token token.Token) node.Node { } } -func (n HaltCompiler) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.StartLine, n.token.EndLine, n.token.Value) +func (n HaltCompiler) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } } diff --git a/node/stmt/if.go b/node/stmt/if.go index aeca4ff..4ae2293 100644 --- a/node/stmt/if.go +++ b/node/stmt/if.go @@ -1,14 +1,11 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n If) Name() string { +func (n If) Name() string { return "If" } @@ -48,27 +45,30 @@ func (n If) SetElse(_else node.Node) node.Node { return n } -func (n If) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.StartLine, n.token.EndLine, n.token.Value) +func (n If) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.cond != nil { - fmt.Fprintf(out, "\n%vcond:", indent+" ") - n.cond.Print(out, indent+" ") + vv := v.Children("cond") + n.cond.Walk(vv) } if n.stmt != nil { - fmt.Fprintf(out, "\n%vstmt:", indent+" ") - n.stmt.Print(out, indent+" ") + vv := v.Children("stmt") + n.stmt.Walk(vv) } if n.elseIf != nil { - fmt.Fprintf(out, "\n%velseIfs:", indent+" ") + vv := v.Children("elseIf") for _, nn := range n.elseIf { - nn.Print(out, indent+" ") + nn.Walk(vv) } } + if n._else != nil { - fmt.Fprintf(out, "\n%velse:", indent+" ") - n._else.Print(out, indent+" ") + vv := v.Children("else") + n._else.Walk(vv) } } diff --git a/node/stmt/inline_html.go b/node/stmt/inline_html.go index 55e25aa..2dd05c2 100644 --- a/node/stmt/inline_html.go +++ b/node/stmt/inline_html.go @@ -1,14 +1,11 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n InlineHtml) Name() string { +func (n InlineHtml) Name() string { return "InlineHtml" } @@ -24,6 +21,10 @@ func NewInlineHtml(token token.Token) node.Node { } } -func (n InlineHtml) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.StartLine, n.token.EndLine, n.token.Value) +func (n InlineHtml) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + v.Scalar("token", n.token.Value) } diff --git a/node/stmt/interface.go b/node/stmt/interface.go index 79e253d..e70b3d3 100644 --- a/node/stmt/interface.go +++ b/node/stmt/interface.go @@ -1,23 +1,20 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n Interface) Name() string { +func (n Interface) Name() string { return "Interface" } type Interface struct { - name string - token token.Token - iName token.Token - extends []node.Node - stmts []node.Node + name string + token token.Token + interfaceName token.Token + extends []node.Node + stmts []node.Node } func NewInterface(token token.Token, name token.Token, extends []node.Node, stmts []node.Node) node.Node { @@ -30,20 +27,24 @@ func NewInterface(token token.Token, name token.Token, extends []node.Node, stmt } } -func (n Interface) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.StartLine, n.token.EndLine, n.iName.Value) +func (n Interface) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + v.Scalar("token", n.interfaceName.Value) if n.extends != nil { - fmt.Fprintf(out, "\n%vextends:", indent+" ") + vv := v.Children("extends") for _, nn := range n.extends { - nn.Print(out, indent+" ") + nn.Walk(vv) } } if n.stmts != nil { - fmt.Fprintf(out, "\n%vstmts:", indent+" ") + vv := v.Children("stmts") for _, nn := range n.stmts { - nn.Print(out, indent+" ") + nn.Walk(vv) } } } diff --git a/node/stmt/label.go b/node/stmt/label.go index a51741a..08e966e 100644 --- a/node/stmt/label.go +++ b/node/stmt/label.go @@ -1,14 +1,11 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n Label) Name() string { +func (n Label) Name() string { return "Label" } @@ -24,6 +21,10 @@ func NewLabel(token token.Token) node.Node { } } -func (n Label) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.StartLine, n.token.EndLine, n.token.Value) +func (n Label) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + v.Scalar("token", n.token.Value) } diff --git a/node/stmt/namespace.go b/node/stmt/namespace.go index 017454f..17b55ec 100644 --- a/node/stmt/namespace.go +++ b/node/stmt/namespace.go @@ -1,45 +1,44 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n Namespace) Name() string { +func (n Namespace) Name() string { return "Namespace" } type Namespace struct { - name string - token token.Token - nName node.Node - stmts []node.Node + name string + token token.Token + namespaceName node.Node + stmts []node.Node } -func NewNamespace(token token.Token, name node.Node, stmts []node.Node) node.Node { +func NewNamespace(token token.Token, namespaceName node.Node, stmts []node.Node) node.Node { return Namespace{ "Namespace", token, - name, + namespaceName, stmts, } } -func (n Namespace) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.StartLine, n.token.EndLine, n.token.Value) +func (n Namespace) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } - if n.nName != nil { - fmt.Fprintf(out, "\n%vname:", indent+" ") - n.nName.Print(out, indent+" ") + if n.namespaceName != nil { + vv := v.Children("namespaceName") + n.namespaceName.Walk(vv) } if n.stmts != nil { - fmt.Fprintf(out, "\n%vstmts:", indent+" ") + vv := v.Children("stmts") for _, nn := range n.stmts { - nn.Print(out, indent+" ") + nn.Walk(vv) } } } diff --git a/node/stmt/nop.go b/node/stmt/nop.go index c6f3210..78bef7b 100644 --- a/node/stmt/nop.go +++ b/node/stmt/nop.go @@ -8,7 +8,7 @@ import ( "github.com/z7zmey/php-parser/token" ) -func(n Nop) Name() string { +func (n Nop) Name() string { return "Nop" } @@ -27,3 +27,11 @@ func NewNop(token token.Token) node.Node { func (n Nop) Print(out io.Writer, indent string) { fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.StartLine, n.token.EndLine, n.token.Value) } + +func (n Nop) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + v.Scalar("token", n.token.Value) +} diff --git a/node/stmt/property.go b/node/stmt/property.go index 30529c5..577624a 100644 --- a/node/stmt/property.go +++ b/node/stmt/property.go @@ -1,14 +1,11 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n Property) Name() string { +func (n Property) Name() string { return "Property" } @@ -26,11 +23,15 @@ func NewProperty(token token.Token, expr node.Node) node.Node { } } -func (n Property) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.StartLine, n.token.EndLine, n.token.Value) +func (n Property) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + v.Scalar("token", n.token.Value) if n.expr != nil { - fmt.Fprintf(out, "\n%vexpr:", indent+" ") - n.expr.Print(out, indent+" ") + vv := v.Children("expr") + n.expr.Walk(vv) } } diff --git a/node/stmt/property_list.go b/node/stmt/property_list.go index 2864c54..8c3d04f 100644 --- a/node/stmt/property_list.go +++ b/node/stmt/property_list.go @@ -1,13 +1,10 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" ) -func(n PropertyList) Name() string { +func (n PropertyList) Name() string { return "PropertyList" } @@ -25,20 +22,22 @@ func NewPropertyList(modifiers []node.Node, properties []node.Node) node.Node { } } -func (n PropertyList) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) +func (n PropertyList) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.modifiers != nil { - fmt.Fprintf(out, "\n%vmodifiers:", indent+" ") + vv := v.Children("modifiers") for _, nn := range n.modifiers { - nn.Print(out, indent+" ") + nn.Walk(vv) } } if n.properties != nil { - fmt.Fprintf(out, "\n%vproperties:", indent+" ") + vv := v.Children("properties") for _, nn := range n.properties { - nn.Print(out, indent+" ") + nn.Walk(vv) } } } diff --git a/node/stmt/return.go b/node/stmt/return.go index aed2d3e..79fd167 100644 --- a/node/stmt/return.go +++ b/node/stmt/return.go @@ -1,14 +1,11 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n Return) Name() string { +func (n Return) Name() string { return "Return" } @@ -26,11 +23,13 @@ func NewReturn(token token.Token, expr node.Node) node.Node { } } -func (n Return) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.StartLine, n.token.EndLine, n.token.Value) +func (n Return) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.expr != nil { - fmt.Fprintf(out, "\n%vexpr:", indent+" ") - n.expr.Print(out, indent+" ") + vv := v.Children("expr") + n.expr.Walk(vv) } } diff --git a/node/stmt/static.go b/node/stmt/static.go index 37a3207..a33984d 100644 --- a/node/stmt/static.go +++ b/node/stmt/static.go @@ -1,14 +1,11 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n Static) Name() string { +func (n Static) Name() string { return "Static" } @@ -26,13 +23,15 @@ func NewStatic(token token.Token, vars []node.Node) node.Node { } } -func (n Static) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.StartLine, n.token.EndLine, n.token.Value) +func (n Static) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.vars != nil { - fmt.Fprintf(out, "\n%vvars:", indent+" ") + vv := v.Children("vars") for _, nn := range n.vars { - nn.Print(out, indent+" ") + nn.Walk(vv) } } } diff --git a/node/stmt/static_var.go b/node/stmt/static_var.go index 17ab3ba..15aa864 100644 --- a/node/stmt/static_var.go +++ b/node/stmt/static_var.go @@ -1,14 +1,11 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n StaticVar) Name() string { +func (n StaticVar) Name() string { return "StaticVar" } @@ -26,11 +23,13 @@ func NewStaticVar(token token.Token, expr node.Node) node.Node { } } -func (n StaticVar) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.StartLine, n.token.EndLine, n.token.Value) +func (n StaticVar) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.expr != nil { - fmt.Fprintf(out, "\n%vexpr:", indent+" ") - n.expr.Print(out, indent+" ") + vv := v.Children("expr") + n.expr.Walk(vv) } } diff --git a/node/stmt/stmt_list.go b/node/stmt/stmt_list.go index d2d0dcb..ccbf237 100644 --- a/node/stmt/stmt_list.go +++ b/node/stmt/stmt_list.go @@ -1,13 +1,10 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" ) -func(n StmtList) Name() string { +func (n StmtList) Name() string { return "StmtList" } @@ -23,13 +20,15 @@ func NewStmtList(stmts []node.Node) node.Node { } } -func (n StmtList) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) +func (n StmtList) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.stmts != nil { - fmt.Fprintf(out, "\n%vstmts:", indent+" ") + vv := v.Children("stmts") for _, nn := range n.stmts { - nn.Print(out, indent+" ") + nn.Walk(vv) } } } diff --git a/node/stmt/switch.go b/node/stmt/switch.go index 7e82755..278d74f 100644 --- a/node/stmt/switch.go +++ b/node/stmt/switch.go @@ -1,14 +1,11 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n Switch) Name() string { +func (n Switch) Name() string { return "Switch" } @@ -28,18 +25,20 @@ func NewSwitch(token token.Token, cond node.Node, cases []node.Node) node.Node { } } -func (n Switch) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.StartLine, n.token.EndLine, n.token.Value) +func (n Switch) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.cond != nil { - fmt.Fprintf(out, "\n%vcond:", indent+" ") - n.cond.Print(out, indent+" ") + vv := v.Children("cond") + n.cond.Walk(vv) } if n.cases != nil { - fmt.Fprintf(out, "\n%vcases:", indent+" ") + vv := v.Children("cases") for _, nn := range n.cases { - nn.Print(out, indent+" ") + nn.Walk(vv) } } } diff --git a/node/stmt/throw.go b/node/stmt/throw.go index 1da2d8d..05b8bd3 100644 --- a/node/stmt/throw.go +++ b/node/stmt/throw.go @@ -1,14 +1,11 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n Throw) Name() string { +func (n Throw) Name() string { return "Throw" } @@ -26,11 +23,13 @@ func NewThrow(token token.Token, expr node.Node) node.Node { } } -func (n Throw) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.StartLine, n.token.EndLine, n.token.Value) +func (n Throw) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.expr != nil { - fmt.Fprintf(out, "\n%vexpr:", indent+" ") - n.expr.Print(out, indent+" ") + vv := v.Children("expr") + n.expr.Walk(vv) } } diff --git a/node/stmt/trait.go b/node/stmt/trait.go index 25e036f..fa96fc5 100644 --- a/node/stmt/trait.go +++ b/node/stmt/trait.go @@ -1,14 +1,11 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n Trait) Name() string { +func (n Trait) Name() string { return "Trait" } @@ -27,13 +24,17 @@ func NewTrait(token token.Token, stmts []node.Node) node.Node { } } -func (n Trait) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.StartLine, n.token.EndLine, n.token.Value) +func (n Trait) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + v.Scalar("token", n.token.Value) if n.stmts != nil { - fmt.Fprintf(out, "\n%vstmts:", indent+" ") + vv := v.Children("stmts") for _, nn := range n.stmts { - nn.Print(out, indent+" ") + nn.Walk(vv) } } } diff --git a/node/stmt/trait_method_ref.go b/node/stmt/trait_method_ref.go index d694410..8fc2efd 100644 --- a/node/stmt/trait_method_ref.go +++ b/node/stmt/trait_method_ref.go @@ -1,14 +1,11 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n TraitMethodRef) Name() string { +func (n TraitMethodRef) Name() string { return "TraitMethodRef" } @@ -18,6 +15,7 @@ type TraitMethodRef struct { method token.Token } +// TODO: method must be identifier func NewTraitMethodRef(trait node.Node, method token.Token) node.Node { return TraitMethodRef{ "TraitMethodRef", @@ -26,11 +24,15 @@ func NewTraitMethodRef(trait node.Node, method token.Token) node.Node { } } -func (n TraitMethodRef) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -] %q", indent, n.name, n.method.Value) +func (n TraitMethodRef) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } + + v.Scalar("method", n.method.Value) if n.trait != nil { - fmt.Fprintf(out, "\n%vtrait", indent+" ") - n.trait.Print(out, indent+" ") + vv := v.Children("trait") + n.trait.Walk(vv) } } diff --git a/node/stmt/trait_use.go b/node/stmt/trait_use.go index fcad08e..d4ca5c6 100644 --- a/node/stmt/trait_use.go +++ b/node/stmt/trait_use.go @@ -1,14 +1,11 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n TraitUse) Name() string { +func (n TraitUse) Name() string { return "TraitUse" } @@ -29,20 +26,22 @@ func NewTraitUse(token token.Token, traits []node.Node, adaptations []node.Node) } } -func (n TraitUse) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.StartLine, n.token.EndLine, n.token.Value) +func (n TraitUse) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.traits != nil { - fmt.Fprintf(out, "\n%vtraits:", indent+" ") + vv := v.Children("traits") for _, nn := range n.traits { - nn.Print(out, indent+" ") + nn.Walk(vv) } } if n.adaptations != nil { - fmt.Fprintf(out, "\n%vadaptations:", indent+" ") + vv := v.Children("adaptations") for _, nn := range n.adaptations { - nn.Print(out, indent+" ") + nn.Walk(vv) } } } diff --git a/node/stmt/trait_use_alias.go b/node/stmt/trait_use_alias.go index 4c67e37..137f848 100644 --- a/node/stmt/trait_use_alias.go +++ b/node/stmt/trait_use_alias.go @@ -1,14 +1,11 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n TraitUseAlias) Name() string { +func (n TraitUseAlias) Name() string { return "TraitUseAlias" } @@ -28,20 +25,18 @@ func NewTraitUseAlias(ref node.Node, modifier node.Node, alias token.TokenInterf } } -func (n TraitUseAlias) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) - - if n.alias != nil { - fmt.Fprintf(out, "\n%valias: %q", indent+" ", n.alias.GetValue()) +func (n TraitUseAlias) Walk(v node.Visitor) { + if v.Visit(n) == false { + return } if n.ref != nil { - fmt.Fprintf(out, "\n%vmethod", indent+" ") - n.ref.Print(out, indent+" ") + vv := v.Children("ref") + n.ref.Walk(vv) } if n.modifier != nil { - fmt.Fprintf(out, "\n%vmodifier:", indent+" ") - n.modifier.Print(out, indent+" ") + vv := v.Children("modifier") + n.modifier.Walk(vv) } } diff --git a/node/stmt/trait_use_precedence.go b/node/stmt/trait_use_precedence.go index a711192..5d1bf9e 100644 --- a/node/stmt/trait_use_precedence.go +++ b/node/stmt/trait_use_precedence.go @@ -1,13 +1,10 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" ) -func(n TraitUsePrecedence) Name() string { +func (n TraitUsePrecedence) Name() string { return "TraitUsePrecedence" } @@ -25,16 +22,18 @@ func NewTraitUsePrecedence(ref node.Node, insteadof node.Node) node.Node { } } -func (n TraitUsePrecedence) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) +func (n TraitUsePrecedence) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.ref != nil { - fmt.Fprintf(out, "\n%vmethod", indent+" ") - n.ref.Print(out, indent+" ") + vv := v.Children("ref") + n.ref.Walk(vv) } if n.insteadof != nil { - fmt.Fprintf(out, "\n%vinsteadof:", indent+" ") - n.insteadof.Print(out, indent+" ") + vv := v.Children("insteadof") + n.insteadof.Walk(vv) } } diff --git a/node/stmt/try.go b/node/stmt/try.go index 34cb02f..b0c4c11 100644 --- a/node/stmt/try.go +++ b/node/stmt/try.go @@ -1,14 +1,11 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n Try) Name() string { +func (n Try) Name() string { return "Try" } @@ -30,25 +27,27 @@ func NewTry(token token.Token, stmts []node.Node, catches []node.Node, finally n } } -func (n Try) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.StartLine, n.token.EndLine, n.token.Value) +func (n Try) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.stmts != nil { - fmt.Fprintf(out, "\n%vstmts:", indent+" ") + vv := v.Children("stmts") for _, nn := range n.stmts { - nn.Print(out, indent+" ") + nn.Walk(vv) } } if n.catches != nil { - fmt.Fprintf(out, "\n%vcatches:", indent+" ") + vv := v.Children("catches") for _, nn := range n.catches { - nn.Print(out, indent+" ") + nn.Walk(vv) } } if n.finally != nil { - fmt.Fprintf(out, "\n%vfinally:", indent+" ") - n.finally.Print(out, indent+" ") + vv := v.Children("finally") + n.finally.Walk(vv) } } diff --git a/node/stmt/unset.go b/node/stmt/unset.go index 54e878c..0035884 100644 --- a/node/stmt/unset.go +++ b/node/stmt/unset.go @@ -1,14 +1,11 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n Unset) Name() string { +func (n Unset) Name() string { return "Unset" } @@ -26,13 +23,15 @@ func NewUnset(token token.Token, vars []node.Node) node.Node { } } -func (n Unset) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.StartLine, n.token.EndLine, n.token.Value) +func (n Unset) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.vars != nil { - fmt.Fprintf(out, "\n%vvars:", indent+" ") + vv := v.Children("vars") for _, nn := range n.vars { - nn.Print(out, indent+" ") + nn.Walk(vv) } } } diff --git a/node/stmt/use.go b/node/stmt/use.go index c571aef..479eea5 100644 --- a/node/stmt/use.go +++ b/node/stmt/use.go @@ -1,17 +1,10 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n Use) Name() string { - return "Use" -} - type Use struct { name string useType node.Node @@ -28,25 +21,27 @@ func NewUse(useType node.Node, use node.Node, alias token.TokenInterface) node.N } } +func (n Use) Name() string { + return "Use" +} + func (n Use) SetType(useType node.Node) node.Node { n.useType = useType return n } -func (n Use) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [- -]", indent, n.name) +func (n Use) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.useType != nil { - fmt.Fprintf(out, "\n%vtype:", indent+" ") - n.useType.Print(out, indent+" ") + vv := v.Children("useType") + n.useType.Walk(vv) } if n.use != nil { - fmt.Fprintf(out, "\n%vuse:", indent+" ") - n.use.Print(out, indent+" ") - } - - if n.alias != nil { - fmt.Fprintf(out, "\n%valias: %q", indent+" ", n.alias.GetValue()) + vv := v.Children("use") + n.use.Walk(vv) } } diff --git a/node/stmt/use_list.go b/node/stmt/use_list.go index 7a5adc9..3621a62 100644 --- a/node/stmt/use_list.go +++ b/node/stmt/use_list.go @@ -1,14 +1,11 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n UseList) Name() string { +func (n UseList) Name() string { return "UseList" } @@ -28,18 +25,20 @@ func NewUseList(token token.Token, useType node.Node, uses []node.Node) node.Nod } } -func (n UseList) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.StartLine, n.token.EndLine, n.token.Value) +func (n UseList) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.useType != nil { - fmt.Fprintf(out, "\n%vtype:", indent+" ") - n.useType.Print(out, indent+" ") + vv := v.Children("useType") + n.useType.Walk(vv) } if n.uses != nil { - fmt.Fprintf(out, "\n%vuses:", indent+" ") + vv := v.Children("uses") for _, nn := range n.uses { - nn.Print(out, indent+" ") + nn.Walk(vv) } } } diff --git a/node/stmt/while.go b/node/stmt/while.go index 259df64..c708b1d 100644 --- a/node/stmt/while.go +++ b/node/stmt/while.go @@ -1,17 +1,10 @@ package stmt import ( - "fmt" - "io" - "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/token" ) -func(n While) Name() string { - return "While" -} - type While struct { name string token token.Token @@ -28,16 +21,22 @@ func NewWhile(token token.Token, cond node.Node, stmt node.Node) node.Node { } } -func (n While) Print(out io.Writer, indent string) { - fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.name, n.token.StartLine, n.token.EndLine, n.token.Value) +func (n While) Name() string { + return "While" +} + +func (n While) Walk(v node.Visitor) { + if v.Visit(n) == false { + return + } if n.cond != nil { - fmt.Fprintf(out, "\n%vcond:", indent+" ") - n.cond.Print(out, indent+" ") + vv := v.Children("cond") + n.cond.Walk(vv) } if n.stmt != nil { - fmt.Fprintf(out, "\n%vstmt:", indent+" ") - n.stmt.Print(out, indent+" ") + vv := v.Children("stmt") + n.stmt.Walk(vv) } } diff --git a/node/visitor.go b/node/visitor.go new file mode 100644 index 0000000..3659f03 --- /dev/null +++ b/node/visitor.go @@ -0,0 +1,7 @@ +package node + +type Visitor interface { + Visit(node Node) bool + Children(key string) Visitor + Scalar(key string, value interface{}) +}