2018-06-18 20:29:52 +00:00
|
|
|
//Package visitor contains walker.visitor implementations
|
|
|
|
package visitor_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/z7zmey/php-parser/php7"
|
|
|
|
"github.com/z7zmey/php-parser/visitor"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ExampleGoDumper() {
|
|
|
|
src := `<?php
|
|
|
|
|
|
|
|
namespace Foo {
|
|
|
|
class Bar {
|
|
|
|
public function FunctionName(Type $var = null)
|
|
|
|
{
|
|
|
|
//some comment
|
|
|
|
$var;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
|
|
|
|
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
|
|
|
|
php7parser.Parse()
|
|
|
|
nodes := php7parser.GetRootNode()
|
|
|
|
|
|
|
|
nsResolver := visitor.NewNamespaceResolver()
|
|
|
|
nodes.Walk(nsResolver)
|
|
|
|
|
|
|
|
dumper := &visitor.GoDumper{
|
|
|
|
Writer: os.Stdout,
|
|
|
|
}
|
|
|
|
nodes.Walk(dumper)
|
|
|
|
|
|
|
|
// Unordered output:
|
|
|
|
//&node.Root{
|
2018-06-24 07:19:44 +00:00
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 3,
|
|
|
|
// EndLine: 11,
|
|
|
|
// StartPos: 10,
|
|
|
|
// EndPos: 142,
|
|
|
|
// },
|
2018-06-18 20:29:52 +00:00
|
|
|
// Stmts: []node.Node{
|
|
|
|
// &stmt.Namespace{
|
2018-06-24 07:19:44 +00:00
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 3,
|
|
|
|
// EndLine: 11,
|
|
|
|
// StartPos: 10,
|
|
|
|
// EndPos: 142,
|
|
|
|
// },
|
2018-06-18 20:29:52 +00:00
|
|
|
// NamespaceName: &name.Name{
|
2018-06-24 07:19:44 +00:00
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 3,
|
|
|
|
// EndLine: 3,
|
|
|
|
// StartPos: 20,
|
|
|
|
// EndPos: 22,
|
|
|
|
// },
|
2018-06-18 20:29:52 +00:00
|
|
|
// Parts: []node.Node{
|
|
|
|
// &name.NamePart{
|
2018-06-24 07:19:44 +00:00
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 3,
|
|
|
|
// EndLine: 3,
|
|
|
|
// StartPos: 20,
|
|
|
|
// EndPos: 22,
|
|
|
|
// },
|
2018-06-18 20:29:52 +00:00
|
|
|
// Value: "Foo",
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// Stmts: []node.Node{
|
|
|
|
// &stmt.Class{
|
2018-06-24 07:19:44 +00:00
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 4,
|
|
|
|
// EndLine: 10,
|
|
|
|
// StartPos: 29,
|
|
|
|
// EndPos: 138,
|
|
|
|
// },
|
2018-06-18 20:29:52 +00:00
|
|
|
// PhpDocComment: "",
|
|
|
|
// ClassName: &node.Identifier{
|
2018-06-24 07:19:44 +00:00
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 4,
|
|
|
|
// EndLine: 4,
|
|
|
|
// StartPos: 35,
|
|
|
|
// EndPos: 37,
|
|
|
|
// },
|
2018-06-18 20:29:52 +00:00
|
|
|
// Value: "Bar",
|
|
|
|
// },
|
|
|
|
// Stmts: []node.Node{
|
|
|
|
// &stmt.ClassMethod{
|
2018-06-24 07:19:44 +00:00
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 5,
|
|
|
|
// EndLine: 9,
|
|
|
|
// StartPos: 45,
|
|
|
|
// EndPos: 133,
|
|
|
|
// },
|
2018-06-18 20:29:52 +00:00
|
|
|
// ReturnsRef: false,
|
|
|
|
// PhpDocComment: "",
|
|
|
|
// MethodName: &node.Identifier{
|
2018-06-24 07:19:44 +00:00
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 5,
|
|
|
|
// EndLine: 5,
|
|
|
|
// StartPos: 61,
|
|
|
|
// EndPos: 72,
|
|
|
|
// },
|
2018-06-18 20:29:52 +00:00
|
|
|
// Value: "FunctionName",
|
|
|
|
// },
|
|
|
|
// Modifiers: []node.Node{
|
|
|
|
// &node.Identifier{
|
2018-06-24 07:19:44 +00:00
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 5,
|
|
|
|
// EndLine: 5,
|
|
|
|
// StartPos: 45,
|
|
|
|
// EndPos: 50,
|
|
|
|
// },
|
2018-06-18 20:29:52 +00:00
|
|
|
// Value: "public",
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// Params: []node.Node{
|
|
|
|
// &node.Parameter{
|
2018-06-24 07:19:44 +00:00
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 5,
|
|
|
|
// EndLine: 5,
|
|
|
|
// StartPos: 74,
|
|
|
|
// EndPos: 89,
|
|
|
|
// },
|
2018-06-18 20:29:52 +00:00
|
|
|
// Variadic: false,
|
2018-06-24 07:19:44 +00:00
|
|
|
// ByRef: false,
|
2018-06-18 20:29:52 +00:00
|
|
|
// VariableType: &name.Name{
|
2018-06-24 07:19:44 +00:00
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 5,
|
|
|
|
// EndLine: 5,
|
|
|
|
// StartPos: 74,
|
|
|
|
// EndPos: 77,
|
|
|
|
// },
|
2018-06-18 20:29:52 +00:00
|
|
|
// Parts: []node.Node{
|
|
|
|
// &name.NamePart{
|
2018-06-24 07:19:44 +00:00
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 5,
|
|
|
|
// EndLine: 5,
|
|
|
|
// StartPos: 74,
|
|
|
|
// EndPos: 77,
|
|
|
|
// },
|
2018-06-18 20:29:52 +00:00
|
|
|
// Value: "Type",
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// Variable: &expr.Variable{
|
2018-06-24 07:19:44 +00:00
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 5,
|
|
|
|
// EndLine: 5,
|
|
|
|
// StartPos: 79,
|
|
|
|
// EndPos: 82,
|
|
|
|
// },
|
2018-06-18 20:29:52 +00:00
|
|
|
// VarName: &node.Identifier{
|
2018-06-24 07:19:44 +00:00
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 5,
|
|
|
|
// EndLine: 5,
|
|
|
|
// StartPos: 79,
|
|
|
|
// EndPos: 82,
|
|
|
|
// },
|
2018-06-18 20:29:52 +00:00
|
|
|
// Value: "var",
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// DefaultValue: &expr.ConstFetch{
|
2018-06-24 07:19:44 +00:00
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 5,
|
|
|
|
// EndLine: 5,
|
|
|
|
// StartPos: 86,
|
|
|
|
// EndPos: 89,
|
|
|
|
// },
|
2018-06-18 20:29:52 +00:00
|
|
|
// Constant: &name.Name{
|
2018-06-24 07:19:44 +00:00
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 5,
|
|
|
|
// EndLine: 5,
|
|
|
|
// StartPos: 86,
|
|
|
|
// EndPos: 89,
|
|
|
|
// },
|
2018-06-18 20:29:52 +00:00
|
|
|
// Parts: []node.Node{
|
|
|
|
// &name.NamePart{
|
2018-06-24 07:19:44 +00:00
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 5,
|
|
|
|
// EndLine: 5,
|
|
|
|
// StartPos: 86,
|
|
|
|
// EndPos: 89,
|
|
|
|
// },
|
2018-06-18 20:29:52 +00:00
|
|
|
// Value: "null",
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// Stmt: &stmt.StmtList{
|
2018-06-24 07:19:44 +00:00
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 6,
|
|
|
|
// EndLine: 9,
|
|
|
|
// StartPos: 96,
|
|
|
|
// EndPos: 133,
|
|
|
|
// },
|
2018-06-18 20:29:52 +00:00
|
|
|
// Stmts: []node.Node{
|
|
|
|
// &stmt.Expression{
|
2018-06-24 07:19:44 +00:00
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 8,
|
|
|
|
// EndLine: 8,
|
|
|
|
// StartPos: 123,
|
|
|
|
// EndPos: 127,
|
|
|
|
// },
|
2018-06-18 20:29:52 +00:00
|
|
|
// Expr: &expr.Variable{
|
2018-06-24 07:19:44 +00:00
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 8,
|
|
|
|
// EndLine: 8,
|
|
|
|
// StartPos: 123,
|
|
|
|
// EndPos: 126,
|
|
|
|
// },
|
2018-06-25 12:38:31 +00:00
|
|
|
// Comments: []*comment.Comment{
|
|
|
|
// &comment.Comment{
|
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 7,
|
|
|
|
// EndLine: 7,
|
|
|
|
// StartPos: 103,
|
|
|
|
// EndPos: 117,
|
|
|
|
// },
|
|
|
|
// Value: "//some comment\n",
|
|
|
|
// TokenName: '\t',
|
|
|
|
// },
|
|
|
|
// },
|
2018-06-18 20:29:52 +00:00
|
|
|
// VarName: &node.Identifier{
|
2018-06-24 07:19:44 +00:00
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 8,
|
|
|
|
// EndLine: 8,
|
|
|
|
// StartPos: 123,
|
|
|
|
// EndPos: 126,
|
|
|
|
// },
|
2018-06-18 20:29:52 +00:00
|
|
|
// Value: "var",
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
//}
|
|
|
|
}
|