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)
|
|
|
|
{
|
2018-07-29 08:44:38 +00:00
|
|
|
// some comment
|
2018-06-18 20:29:52 +00:00
|
|
|
$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:
|
2018-07-02 17:48:55 +00:00
|
|
|
// &node.Root{
|
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 3,
|
|
|
|
// EndLine: 11,
|
|
|
|
// StartPos: 10,
|
2018-07-29 08:44:38 +00:00
|
|
|
// EndPos: 143,
|
2018-07-02 17:48:55 +00:00
|
|
|
// },
|
|
|
|
// Stmts: []node.Node{
|
|
|
|
// &stmt.Namespace{
|
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 3,
|
|
|
|
// EndLine: 11,
|
|
|
|
// StartPos: 10,
|
2018-07-29 08:44:38 +00:00
|
|
|
// EndPos: 143,
|
2018-07-02 17:48:55 +00:00
|
|
|
// },
|
|
|
|
// Meta: []meta.Meta{
|
2018-07-29 08:44:38 +00:00
|
|
|
// &meta.TokenType{
|
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 1,
|
|
|
|
// EndLine: 1,
|
|
|
|
// StartPos: 1,
|
|
|
|
// EndPos: 5,
|
|
|
|
// },
|
|
|
|
// Value: "<?php",
|
|
|
|
// TokenName: 65,
|
|
|
|
// },
|
|
|
|
// &meta.WhiteSpaceType{
|
2018-07-02 17:48:55 +00:00
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 1,
|
|
|
|
// EndLine: 3,
|
|
|
|
// StartPos: 6,
|
|
|
|
// EndPos: 9,
|
|
|
|
// },
|
|
|
|
// Value: "\n\n\t\t",
|
|
|
|
// TokenName: 65,
|
|
|
|
// },
|
2018-07-29 08:44:38 +00:00
|
|
|
// &meta.WhiteSpaceType{
|
2018-07-02 17:48:55 +00:00
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 3,
|
|
|
|
// EndLine: 3,
|
|
|
|
// StartPos: 23,
|
|
|
|
// EndPos: 23,
|
|
|
|
// },
|
|
|
|
// Value: " ",
|
2018-07-29 08:44:38 +00:00
|
|
|
// TokenName: 130,
|
2018-07-02 17:48:55 +00:00
|
|
|
// },
|
2018-07-29 08:44:38 +00:00
|
|
|
// &meta.WhiteSpaceType{
|
2018-07-02 17:48:55 +00:00
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 10,
|
|
|
|
// EndLine: 11,
|
2018-07-29 08:44:38 +00:00
|
|
|
// StartPos: 140,
|
|
|
|
// EndPos: 142,
|
2018-07-02 17:48:55 +00:00
|
|
|
// },
|
|
|
|
// Value: "\n\t\t",
|
2018-07-29 08:44:38 +00:00
|
|
|
// TokenName: 131,
|
2018-07-02 17:48:55 +00:00
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// NamespaceName: &name.Name{
|
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 3,
|
|
|
|
// EndLine: 3,
|
|
|
|
// StartPos: 20,
|
|
|
|
// EndPos: 22,
|
|
|
|
// },
|
|
|
|
// Parts: []node.Node{
|
|
|
|
// &name.NamePart{
|
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 3,
|
|
|
|
// EndLine: 3,
|
|
|
|
// StartPos: 20,
|
|
|
|
// EndPos: 22,
|
|
|
|
// },
|
|
|
|
// Meta: []meta.Meta{
|
2018-07-29 08:44:38 +00:00
|
|
|
// &meta.WhiteSpaceType{
|
2018-07-02 17:48:55 +00:00
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 3,
|
|
|
|
// EndLine: 3,
|
|
|
|
// StartPos: 19,
|
|
|
|
// EndPos: 19,
|
|
|
|
// },
|
|
|
|
// Value: " ",
|
|
|
|
// TokenName: 10,
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// Value: "Foo",
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// Stmts: []node.Node{
|
|
|
|
// &stmt.Class{
|
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 4,
|
|
|
|
// EndLine: 10,
|
|
|
|
// StartPos: 29,
|
2018-07-29 08:44:38 +00:00
|
|
|
// EndPos: 139,
|
2018-07-02 17:48:55 +00:00
|
|
|
// },
|
|
|
|
// Meta: []meta.Meta{
|
2018-07-29 08:44:38 +00:00
|
|
|
// &meta.WhiteSpaceType{
|
2018-07-02 17:48:55 +00:00
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 3,
|
|
|
|
// EndLine: 4,
|
|
|
|
// StartPos: 25,
|
|
|
|
// EndPos: 28,
|
|
|
|
// },
|
|
|
|
// Value: "\n\t\t\t",
|
2018-07-29 08:44:38 +00:00
|
|
|
// TokenName: 51,
|
2018-07-02 17:48:55 +00:00
|
|
|
// },
|
2018-07-29 08:44:38 +00:00
|
|
|
// &meta.WhiteSpaceType{
|
2018-07-02 17:48:55 +00:00
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 4,
|
|
|
|
// EndLine: 4,
|
|
|
|
// StartPos: 38,
|
|
|
|
// EndPos: 38,
|
|
|
|
// },
|
|
|
|
// Value: " ",
|
2018-07-29 08:44:38 +00:00
|
|
|
// TokenName: 130,
|
2018-07-02 17:48:55 +00:00
|
|
|
// },
|
2018-07-29 08:44:38 +00:00
|
|
|
// &meta.WhiteSpaceType{
|
2018-07-02 17:48:55 +00:00
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 9,
|
|
|
|
// EndLine: 10,
|
2018-07-29 08:44:38 +00:00
|
|
|
// StartPos: 135,
|
|
|
|
// EndPos: 138,
|
2018-07-02 17:48:55 +00:00
|
|
|
// },
|
|
|
|
// Value: "\n\t\t\t",
|
2018-07-29 08:44:38 +00:00
|
|
|
// TokenName: 131,
|
2018-07-02 17:48:55 +00:00
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// PhpDocComment: "",
|
|
|
|
// ClassName: &node.Identifier{
|
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 4,
|
|
|
|
// EndLine: 4,
|
|
|
|
// StartPos: 35,
|
|
|
|
// EndPos: 37,
|
|
|
|
// },
|
|
|
|
// Meta: []meta.Meta{
|
2018-07-29 08:44:38 +00:00
|
|
|
// &meta.WhiteSpaceType{
|
2018-07-02 17:48:55 +00:00
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 4,
|
|
|
|
// EndLine: 4,
|
|
|
|
// StartPos: 34,
|
|
|
|
// EndPos: 34,
|
|
|
|
// },
|
|
|
|
// Value: " ",
|
2018-07-29 08:44:38 +00:00
|
|
|
// TokenName: 126,
|
2018-07-02 17:48:55 +00:00
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// Value: "Bar",
|
|
|
|
// },
|
|
|
|
// Stmts: []node.Node{
|
|
|
|
// &stmt.ClassMethod{
|
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 5,
|
|
|
|
// EndLine: 9,
|
|
|
|
// StartPos: 45,
|
2018-07-29 08:44:38 +00:00
|
|
|
// EndPos: 134,
|
2018-07-02 17:48:55 +00:00
|
|
|
// },
|
|
|
|
// Meta: []meta.Meta{
|
2018-07-29 08:44:38 +00:00
|
|
|
// &meta.WhiteSpaceType{
|
2018-07-02 17:48:55 +00:00
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 5,
|
|
|
|
// EndLine: 5,
|
|
|
|
// StartPos: 51,
|
|
|
|
// EndPos: 51,
|
|
|
|
// },
|
|
|
|
// Value: " ",
|
2018-07-29 08:44:38 +00:00
|
|
|
// TokenName: 37,
|
2018-07-02 17:48:55 +00:00
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// ReturnsRef: false,
|
|
|
|
// PhpDocComment: "",
|
|
|
|
// MethodName: &node.Identifier{
|
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 5,
|
|
|
|
// EndLine: 5,
|
|
|
|
// StartPos: 61,
|
|
|
|
// EndPos: 72,
|
|
|
|
// },
|
|
|
|
// Meta: []meta.Meta{
|
2018-07-29 08:44:38 +00:00
|
|
|
// &meta.WhiteSpaceType{
|
2018-07-02 17:48:55 +00:00
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 5,
|
|
|
|
// EndLine: 5,
|
|
|
|
// StartPos: 60,
|
|
|
|
// EndPos: 60,
|
|
|
|
// },
|
|
|
|
// Value: " ",
|
2018-07-29 08:44:38 +00:00
|
|
|
// TokenName: 126,
|
2018-07-02 17:48:55 +00:00
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// Value: "FunctionName",
|
|
|
|
// },
|
|
|
|
// Modifiers: []node.Node{
|
|
|
|
// &node.Identifier{
|
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 5,
|
|
|
|
// EndLine: 5,
|
|
|
|
// StartPos: 45,
|
|
|
|
// EndPos: 50,
|
|
|
|
// },
|
|
|
|
// Meta: []meta.Meta{
|
2018-07-29 08:44:38 +00:00
|
|
|
// &meta.WhiteSpaceType{
|
2018-07-02 17:48:55 +00:00
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 4,
|
|
|
|
// EndLine: 5,
|
|
|
|
// StartPos: 40,
|
|
|
|
// EndPos: 44,
|
|
|
|
// },
|
|
|
|
// Value: "\n\t\t\t\t",
|
2018-07-29 08:44:38 +00:00
|
|
|
// TokenName: 126,
|
2018-07-02 17:48:55 +00:00
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// Value: "public",
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// Params: []node.Node{
|
|
|
|
// &node.Parameter{
|
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 5,
|
|
|
|
// EndLine: 5,
|
|
|
|
// StartPos: 74,
|
|
|
|
// EndPos: 89,
|
|
|
|
// },
|
|
|
|
// Meta: []meta.Meta{
|
2018-07-29 08:44:38 +00:00
|
|
|
// &meta.WhiteSpaceType{
|
2018-07-02 17:48:55 +00:00
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 5,
|
|
|
|
// EndLine: 5,
|
|
|
|
// StartPos: 83,
|
|
|
|
// EndPos: 83,
|
|
|
|
// },
|
|
|
|
// Value: " ",
|
2018-07-29 08:44:38 +00:00
|
|
|
// TokenName: 148,
|
2018-07-02 17:48:55 +00:00
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// ByRef: false,
|
|
|
|
// Variadic: false,
|
|
|
|
// VariableType: &name.Name{
|
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 5,
|
|
|
|
// EndLine: 5,
|
|
|
|
// StartPos: 74,
|
|
|
|
// EndPos: 77,
|
|
|
|
// },
|
|
|
|
// Parts: []node.Node{
|
|
|
|
// &name.NamePart{
|
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 5,
|
|
|
|
// EndLine: 5,
|
|
|
|
// StartPos: 74,
|
|
|
|
// EndPos: 77,
|
|
|
|
// },
|
|
|
|
// Value: "Type",
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// Variable: &expr.Variable{
|
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 5,
|
|
|
|
// EndLine: 5,
|
|
|
|
// StartPos: 79,
|
|
|
|
// EndPos: 82,
|
|
|
|
// },
|
|
|
|
// Meta: []meta.Meta{
|
2018-07-29 08:44:38 +00:00
|
|
|
// &meta.WhiteSpaceType{
|
2018-07-02 17:48:55 +00:00
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 5,
|
|
|
|
// EndLine: 5,
|
|
|
|
// StartPos: 78,
|
|
|
|
// EndPos: 78,
|
|
|
|
// },
|
|
|
|
// Value: " ",
|
|
|
|
// TokenName: 12,
|
|
|
|
// },
|
|
|
|
// },
|
2018-07-29 08:44:38 +00:00
|
|
|
// StringVar: false,
|
2018-07-02 17:48:55 +00:00
|
|
|
// VarName: &node.Identifier{
|
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 5,
|
|
|
|
// EndLine: 5,
|
|
|
|
// StartPos: 79,
|
|
|
|
// EndPos: 82,
|
|
|
|
// },
|
|
|
|
// Value: "var",
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// DefaultValue: &expr.ConstFetch{
|
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 5,
|
|
|
|
// EndLine: 5,
|
|
|
|
// StartPos: 86,
|
|
|
|
// EndPos: 89,
|
|
|
|
// },
|
|
|
|
// Constant: &name.Name{
|
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 5,
|
|
|
|
// EndLine: 5,
|
|
|
|
// StartPos: 86,
|
|
|
|
// EndPos: 89,
|
|
|
|
// },
|
|
|
|
// Parts: []node.Node{
|
|
|
|
// &name.NamePart{
|
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 5,
|
|
|
|
// EndLine: 5,
|
|
|
|
// StartPos: 86,
|
|
|
|
// EndPos: 89,
|
|
|
|
// },
|
|
|
|
// Meta: []meta.Meta{
|
2018-07-29 08:44:38 +00:00
|
|
|
// &meta.WhiteSpaceType{
|
2018-07-02 17:48:55 +00:00
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 5,
|
|
|
|
// EndLine: 5,
|
|
|
|
// StartPos: 85,
|
|
|
|
// EndPos: 85,
|
|
|
|
// },
|
|
|
|
// Value: " ",
|
|
|
|
// TokenName: 10,
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// Value: "null",
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// Stmt: &stmt.StmtList{
|
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 6,
|
|
|
|
// EndLine: 9,
|
|
|
|
// StartPos: 96,
|
2018-07-29 08:44:38 +00:00
|
|
|
// EndPos: 134,
|
2018-07-02 17:48:55 +00:00
|
|
|
// },
|
|
|
|
// Meta: []meta.Meta{
|
2018-07-29 08:44:38 +00:00
|
|
|
// &meta.WhiteSpaceType{
|
2018-07-02 17:48:55 +00:00
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 5,
|
|
|
|
// EndLine: 6,
|
|
|
|
// StartPos: 91,
|
|
|
|
// EndPos: 95,
|
|
|
|
// },
|
|
|
|
// Value: "\n\t\t\t\t",
|
2018-07-29 08:44:38 +00:00
|
|
|
// TokenName: 130,
|
2018-07-02 17:48:55 +00:00
|
|
|
// },
|
2018-07-29 08:44:38 +00:00
|
|
|
// &meta.WhiteSpaceType{
|
2018-07-02 17:48:55 +00:00
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 8,
|
|
|
|
// EndLine: 9,
|
2018-07-29 08:44:38 +00:00
|
|
|
// StartPos: 129,
|
|
|
|
// EndPos: 133,
|
2018-07-02 17:48:55 +00:00
|
|
|
// },
|
|
|
|
// Value: "\n\t\t\t\t",
|
2018-07-29 08:44:38 +00:00
|
|
|
// TokenName: 131,
|
2018-07-02 17:48:55 +00:00
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// Stmts: []node.Node{
|
|
|
|
// &stmt.Expression{
|
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 8,
|
|
|
|
// EndLine: 8,
|
2018-07-29 08:44:38 +00:00
|
|
|
// StartPos: 124,
|
|
|
|
// EndPos: 128,
|
|
|
|
// },
|
|
|
|
// Meta: []meta.Meta{
|
|
|
|
// &meta.TokenType{
|
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 8,
|
|
|
|
// EndLine: 8,
|
|
|
|
// StartPos: 128,
|
|
|
|
// EndPos: 128,
|
|
|
|
// },
|
|
|
|
// Value: ";",
|
|
|
|
// TokenName: 132,
|
|
|
|
// },
|
2018-07-02 17:48:55 +00:00
|
|
|
// },
|
|
|
|
// Expr: &expr.Variable{
|
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 8,
|
|
|
|
// EndLine: 8,
|
2018-07-29 08:44:38 +00:00
|
|
|
// StartPos: 124,
|
|
|
|
// EndPos: 127,
|
2018-07-02 17:48:55 +00:00
|
|
|
// },
|
|
|
|
// Meta: []meta.Meta{
|
2018-07-29 08:44:38 +00:00
|
|
|
// &meta.WhiteSpaceType{
|
2018-07-02 17:48:55 +00:00
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 6,
|
|
|
|
// EndLine: 7,
|
|
|
|
// StartPos: 97,
|
|
|
|
// EndPos: 102,
|
|
|
|
// },
|
|
|
|
// Value: "\n\t\t\t\t\t",
|
|
|
|
// TokenName: 12,
|
|
|
|
// },
|
2018-07-29 08:44:38 +00:00
|
|
|
// &meta.CommentType{
|
2018-07-02 17:48:55 +00:00
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 7,
|
|
|
|
// EndLine: 7,
|
|
|
|
// StartPos: 103,
|
2018-07-29 08:44:38 +00:00
|
|
|
// EndPos: 118,
|
2018-07-02 17:48:55 +00:00
|
|
|
// },
|
2018-07-29 08:44:38 +00:00
|
|
|
// Value: "// some comment\n",
|
2018-07-02 17:48:55 +00:00
|
|
|
// TokenName: 12,
|
|
|
|
// },
|
2018-07-29 08:44:38 +00:00
|
|
|
// &meta.WhiteSpaceType{
|
2018-07-02 17:48:55 +00:00
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 8,
|
|
|
|
// EndLine: 8,
|
2018-07-29 08:44:38 +00:00
|
|
|
// StartPos: 119,
|
|
|
|
// EndPos: 123,
|
2018-07-02 17:48:55 +00:00
|
|
|
// },
|
|
|
|
// Value: "\t\t\t\t\t",
|
|
|
|
// TokenName: 12,
|
|
|
|
// },
|
|
|
|
// },
|
2018-07-29 08:44:38 +00:00
|
|
|
// StringVar: false,
|
2018-07-02 17:48:55 +00:00
|
|
|
// VarName: &node.Identifier{
|
|
|
|
// Position: &position.Position{
|
|
|
|
// StartLine: 8,
|
|
|
|
// EndLine: 8,
|
2018-07-29 08:44:38 +00:00
|
|
|
// StartPos: 124,
|
|
|
|
// EndPos: 127,
|
2018-07-02 17:48:55 +00:00
|
|
|
// },
|
|
|
|
// Value: "var",
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// }
|
2018-06-18 20:29:52 +00:00
|
|
|
}
|