2018-06-18 20:29:52 +00:00
|
|
|
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")
|
2018-06-29 21:51:11 +00:00
|
|
|
php7parser.WithMeta()
|
2018-06-18 20:29:52 +00:00
|
|
|
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-29 21:51:11 +00:00
|
|
|
// Meta: []meta.Meta{
|
|
|
|
// &meta.WhiteSpace{
|
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 1,
|
|
|
|
// EndLine: 3,
|
|
|
|
// StartPos: 6,
|
|
|
|
// EndPos: 9,
|
|
|
|
// },
|
|
|
|
// Value: "\n\n\t\t",
|
|
|
|
// TokenName: 67,
|
|
|
|
// },
|
|
|
|
// &meta.WhiteSpace{
|
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 3,
|
|
|
|
// EndLine: 3,
|
|
|
|
// StartPos: 23,
|
|
|
|
// EndPos: 23,
|
|
|
|
// },
|
|
|
|
// Value: " ",
|
|
|
|
// TokenName: 133,
|
|
|
|
// },
|
|
|
|
// &meta.WhiteSpace{
|
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 10,
|
|
|
|
// EndLine: 11,
|
|
|
|
// StartPos: 139,
|
|
|
|
// EndPos: 141,
|
|
|
|
// },
|
|
|
|
// Value: "\n\t\t",
|
|
|
|
// TokenName: 134,
|
|
|
|
// },
|
|
|
|
// },
|
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-29 21:51:11 +00:00
|
|
|
// Meta: []meta.Meta{
|
|
|
|
// &meta.WhiteSpace{
|
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 3,
|
|
|
|
// EndLine: 3,
|
|
|
|
// StartPos: 19,
|
|
|
|
// EndPos: 19,
|
|
|
|
// },
|
|
|
|
// Value: " ",
|
|
|
|
// TokenName: 7,
|
|
|
|
// },
|
|
|
|
// },
|
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-29 21:51:11 +00:00
|
|
|
// Meta: []meta.Meta{
|
|
|
|
// &meta.WhiteSpace{
|
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 3,
|
|
|
|
// EndLine: 4,
|
|
|
|
// StartPos: 25,
|
|
|
|
// EndPos: 28,
|
|
|
|
// },
|
|
|
|
// Value: "\n\t\t\t",
|
|
|
|
// TokenName: 48,
|
|
|
|
// },
|
|
|
|
// &meta.WhiteSpace{
|
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 4,
|
|
|
|
// EndLine: 4,
|
|
|
|
// StartPos: 38,
|
|
|
|
// EndPos: 38,
|
|
|
|
// },
|
|
|
|
// Value: " ",
|
|
|
|
// TokenName: 133,
|
|
|
|
// },
|
|
|
|
// &meta.WhiteSpace{
|
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 9,
|
|
|
|
// EndLine: 10,
|
|
|
|
// StartPos: 134,
|
|
|
|
// EndPos: 137,
|
|
|
|
// },
|
|
|
|
// Value: "\n\t\t\t",
|
|
|
|
// TokenName: 134,
|
|
|
|
// },
|
|
|
|
// },
|
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-29 21:51:11 +00:00
|
|
|
// Meta: []meta.Meta{
|
|
|
|
// &meta.WhiteSpace{
|
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 4,
|
|
|
|
// EndLine: 4,
|
|
|
|
// StartPos: 34,
|
|
|
|
// EndPos: 34,
|
|
|
|
// },
|
|
|
|
// Value: " ",
|
|
|
|
// TokenName: 7,
|
|
|
|
// },
|
|
|
|
// },
|
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-29 21:51:11 +00:00
|
|
|
// Meta: []meta.Meta{
|
|
|
|
// &meta.WhiteSpace{
|
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 5,
|
|
|
|
// EndLine: 5,
|
|
|
|
// StartPos: 51,
|
|
|
|
// EndPos: 51,
|
|
|
|
// },
|
|
|
|
// Value: " ",
|
|
|
|
// TokenName: 34,
|
|
|
|
// },
|
|
|
|
// },
|
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-29 21:51:11 +00:00
|
|
|
// Meta: []meta.Meta{
|
|
|
|
// &meta.WhiteSpace{
|
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 5,
|
|
|
|
// EndLine: 5,
|
|
|
|
// StartPos: 60,
|
|
|
|
// EndPos: 60,
|
|
|
|
// },
|
|
|
|
// Value: " ",
|
|
|
|
// TokenName: 129,
|
|
|
|
// },
|
|
|
|
// },
|
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-29 21:51:11 +00:00
|
|
|
// Meta: []meta.Meta{
|
|
|
|
// &meta.WhiteSpace{
|
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 4,
|
|
|
|
// EndLine: 5,
|
|
|
|
// StartPos: 40,
|
|
|
|
// EndPos: 44,
|
|
|
|
// },
|
|
|
|
// Value: "\n\t\t\t\t",
|
|
|
|
// TokenName: 91,
|
|
|
|
// },
|
|
|
|
// },
|
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-29 21:51:11 +00:00
|
|
|
// Meta: []meta.Meta{
|
|
|
|
// &meta.WhiteSpace{
|
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 5,
|
|
|
|
// EndLine: 5,
|
|
|
|
// StartPos: 83,
|
|
|
|
// EndPos: 83,
|
|
|
|
// },
|
|
|
|
// Value: " ",
|
|
|
|
// TokenName: 151,
|
|
|
|
// },
|
|
|
|
// },
|
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-29 21:51:11 +00:00
|
|
|
// Meta: []meta.Meta{
|
|
|
|
// &meta.WhiteSpace{
|
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 5,
|
|
|
|
// EndLine: 5,
|
|
|
|
// StartPos: 78,
|
|
|
|
// EndPos: 78,
|
|
|
|
// },
|
|
|
|
// Value: " ",
|
|
|
|
// TokenName: 9,
|
|
|
|
// },
|
|
|
|
// },
|
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-29 21:51:11 +00:00
|
|
|
// Meta: []meta.Meta{
|
|
|
|
// &meta.WhiteSpace{
|
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 5,
|
|
|
|
// EndLine: 5,
|
|
|
|
// StartPos: 85,
|
|
|
|
// EndPos: 85,
|
|
|
|
// },
|
|
|
|
// Value: " ",
|
|
|
|
// TokenName: 7,
|
|
|
|
// },
|
|
|
|
// },
|
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-29 21:51:11 +00:00
|
|
|
// Meta: []meta.Meta{
|
|
|
|
// &meta.WhiteSpace{
|
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 5,
|
|
|
|
// EndLine: 6,
|
|
|
|
// StartPos: 91,
|
|
|
|
// EndPos: 95,
|
|
|
|
// },
|
|
|
|
// Value: "\n\t\t\t\t",
|
|
|
|
// TokenName: 133,
|
|
|
|
// },
|
|
|
|
// &meta.WhiteSpace{
|
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 8,
|
|
|
|
// EndLine: 9,
|
|
|
|
// StartPos: 128,
|
|
|
|
// EndPos: 132,
|
|
|
|
// },
|
|
|
|
// Value: "\n\t\t\t\t",
|
|
|
|
// TokenName: 134,
|
|
|
|
// },
|
|
|
|
// },
|
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-29 21:51:11 +00:00
|
|
|
// Meta: []meta.Meta{
|
|
|
|
// &meta.WhiteSpace{
|
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 6,
|
|
|
|
// EndLine: 7,
|
|
|
|
// StartPos: 97,
|
|
|
|
// EndPos: 102,
|
|
|
|
// },
|
|
|
|
// Value: "\n\t\t\t\t\t",
|
|
|
|
// TokenName: 9,
|
|
|
|
// },
|
|
|
|
// &meta.Comment{
|
2018-06-25 12:38:31 +00:00
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 7,
|
|
|
|
// EndLine: 7,
|
|
|
|
// StartPos: 103,
|
|
|
|
// EndPos: 117,
|
|
|
|
// },
|
|
|
|
// Value: "//some comment\n",
|
2018-06-29 21:51:11 +00:00
|
|
|
// TokenName: 9,
|
|
|
|
// },
|
|
|
|
// &meta.WhiteSpace{
|
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 7,
|
|
|
|
// EndLine: 8,
|
|
|
|
// StartPos: 117,
|
|
|
|
// EndPos: 122,
|
|
|
|
// },
|
|
|
|
// Value: "\n\t\t\t\t\t",
|
|
|
|
// TokenName: 9,
|
2018-06-25 12:38:31 +00:00
|
|
|
// },
|
|
|
|
// },
|
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",
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
//}
|
|
|
|
}
|