2018-02-27 21:38:05 +00:00
|
|
|
// Package visitor contains walker.visitor implementations
|
|
|
|
package visitor_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
|
|
|
|
"github.com/z7zmey/php-parser/php7"
|
|
|
|
"github.com/z7zmey/php-parser/visitor"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ExampleDumper() {
|
|
|
|
src := `<?php
|
|
|
|
|
|
|
|
namespace Foo {
|
|
|
|
class Bar {
|
|
|
|
public function FunctionName(Type $var = null)
|
|
|
|
{
|
|
|
|
// some comment
|
|
|
|
$var;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
|
|
|
|
nodes, comments, positions := php7.Parse(bytes.NewBufferString(src), "test.php")
|
|
|
|
|
|
|
|
dumper := visitor.Dumper{
|
|
|
|
Indent: "| ",
|
|
|
|
Comments: comments,
|
|
|
|
Positions: positions,
|
|
|
|
}
|
|
|
|
nodes.Walk(dumper)
|
|
|
|
|
2018-02-27 21:57:33 +00:00
|
|
|
// Unordered output:
|
2018-02-27 21:38:05 +00:00
|
|
|
//| *stmt.StmtList Pos{Line: 3-11 Pos: 10-143}
|
|
|
|
//| "Stmts":
|
|
|
|
//| *stmt.Namespace Pos{Line: 3-11 Pos: 10-143}
|
|
|
|
//| "NamespaceName":
|
|
|
|
//| *name.Name Pos{Line: 3-3 Pos: 20-22}
|
|
|
|
//| "Parts":
|
2018-02-27 21:57:33 +00:00
|
|
|
//| *name.NamePart Pos{Line: 3-3 Pos: 20-22}
|
|
|
|
//| "Value": Foo;
|
2018-02-27 21:38:05 +00:00
|
|
|
//| "Stmts":
|
2018-02-27 21:57:33 +00:00
|
|
|
//| *stmt.Class Pos{Line: 4-10 Pos: 29-139}
|
|
|
|
//| "PhpDocComment": ;
|
2018-02-27 21:38:05 +00:00
|
|
|
//| "ClassName":
|
2018-02-27 21:57:33 +00:00
|
|
|
//| *node.Identifier Pos{Line: 4-4 Pos: 35-37}
|
|
|
|
//| "Value": Bar;
|
2018-02-27 21:38:05 +00:00
|
|
|
//| "Stmts":
|
2018-02-27 21:57:33 +00:00
|
|
|
//| *stmt.ClassMethod Pos{Line: 5-9 Pos: 45-134}
|
|
|
|
//| "ReturnsRef": false;
|
|
|
|
//| "PhpDocComment": ;
|
2018-02-27 21:38:05 +00:00
|
|
|
//| "MethodName":
|
2018-02-27 21:57:33 +00:00
|
|
|
//| *node.Identifier Pos{Line: 5-5 Pos: 61-72}
|
|
|
|
//| "Value": FunctionName;
|
2018-02-27 21:38:05 +00:00
|
|
|
//| "Modifiers":
|
2018-02-27 21:57:33 +00:00
|
|
|
//| *node.Identifier Pos{Line: 5-5 Pos: 45-50}
|
|
|
|
//| "Value": public;
|
2018-02-27 21:38:05 +00:00
|
|
|
//| "Params":
|
2018-02-27 21:57:33 +00:00
|
|
|
//| *node.Parameter Pos{Line: 5-5 Pos: 74-89}
|
|
|
|
//| "ByRef": false;
|
|
|
|
//| "Variadic": false;
|
2018-02-27 21:38:05 +00:00
|
|
|
//| "VariableType":
|
|
|
|
//| *name.Name Pos{Line: 5-5 Pos: 74-77}
|
|
|
|
//| "Parts":
|
2018-02-27 21:57:33 +00:00
|
|
|
//| *name.NamePart Pos{Line: 5-5 Pos: 74-77}
|
|
|
|
//| "Value": Type;
|
2018-02-27 21:38:05 +00:00
|
|
|
//| "Variable":
|
|
|
|
//| *expr.Variable Pos{Line: 5-5 Pos: 79-82}
|
|
|
|
//| "VarName":
|
2018-02-27 21:57:33 +00:00
|
|
|
//| *node.Identifier Pos{Line: 5-5 Pos: 79-82}
|
|
|
|
//| "Value": $var;
|
2018-02-27 21:38:05 +00:00
|
|
|
//| "DefaultValue":
|
|
|
|
//| *expr.ConstFetch Pos{Line: 5-5 Pos: 86-89}
|
|
|
|
//| "Constant":
|
|
|
|
//| *name.Name Pos{Line: 5-5 Pos: 86-89}
|
|
|
|
//| "Parts":
|
2018-02-27 21:57:33 +00:00
|
|
|
//| *name.NamePart Pos{Line: 5-5 Pos: 86-89}
|
|
|
|
//| "Value": null;
|
2018-02-27 21:38:05 +00:00
|
|
|
//| "Stmts":
|
|
|
|
//| *stmt.Expression Pos{Line: 8-8 Pos: 124-128}
|
2018-02-27 21:57:33 +00:00
|
|
|
//| "Comments":
|
2018-02-27 21:38:05 +00:00
|
|
|
//| "// some comment\n"
|
|
|
|
//| "Expr":
|
|
|
|
//| *expr.Variable Pos{Line: 8-8 Pos: 124-127}
|
2018-02-27 21:57:33 +00:00
|
|
|
//| "Comments":
|
2018-02-27 21:38:05 +00:00
|
|
|
//| "// some comment\n"
|
|
|
|
//| "VarName":
|
2018-02-27 21:57:33 +00:00
|
|
|
//| *node.Identifier Pos{Line: 8-8 Pos: 124-127}
|
|
|
|
//| "Value": $var;
|
|
|
|
//| "Comments":
|
2018-02-27 21:38:05 +00:00
|
|
|
//| "// some comment\n"
|
|
|
|
}
|