2018-02-27 21:38:05 +00:00
|
|
|
package visitor_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-03-18 14:05:06 +00:00
|
|
|
"os"
|
2018-02-27 21:38:05 +00:00
|
|
|
|
|
|
|
"github.com/z7zmey/php-parser/php7"
|
|
|
|
"github.com/z7zmey/php-parser/visitor"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ExampleDumper() {
|
|
|
|
src := `<?php
|
|
|
|
|
|
|
|
namespace Foo {
|
2019-02-13 20:18:07 +00:00
|
|
|
class Bar {
|
2018-02-27 21:38:05 +00:00
|
|
|
public function FunctionName(Type $var = null)
|
|
|
|
{
|
|
|
|
// some comment
|
|
|
|
$var;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
|
2018-04-10 12:23:13 +00:00
|
|
|
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
|
2018-06-29 21:51:11 +00:00
|
|
|
php7parser.WithMeta()
|
2018-04-10 12:23:13 +00:00
|
|
|
php7parser.Parse()
|
|
|
|
nodes := php7parser.GetRootNode()
|
2018-02-27 21:38:05 +00:00
|
|
|
|
2018-03-01 20:31:36 +00:00
|
|
|
nsResolver := visitor.NewNamespaceResolver()
|
|
|
|
nodes.Walk(nsResolver)
|
|
|
|
|
2018-06-18 20:29:52 +00:00
|
|
|
dumper := &visitor.Dumper{
|
2018-03-18 14:05:06 +00:00
|
|
|
Writer: os.Stdout,
|
2018-03-01 20:31:36 +00:00
|
|
|
Indent: "| ",
|
|
|
|
NsResolver: nsResolver,
|
2018-02-27 21:38:05 +00:00
|
|
|
}
|
|
|
|
nodes.Walk(dumper)
|
|
|
|
|
2018-02-27 21:57:33 +00:00
|
|
|
// Unordered output:
|
2018-06-19 20:55:12 +00:00
|
|
|
// | [*node.Root]
|
2019-02-13 20:18:07 +00:00
|
|
|
// | "Position": Pos{Line: 3-11 Pos: 10-144}
|
2018-06-19 20:55:12 +00:00
|
|
|
// | "Stmts":
|
|
|
|
// | [*stmt.Namespace]
|
2019-02-13 20:18:07 +00:00
|
|
|
// | "Position": Pos{Line: 3-11 Pos: 10-144}
|
|
|
|
// | "freefloating":
|
|
|
|
// | "Start": "<?php"
|
|
|
|
// | "Start": "\n\n\t\t"
|
|
|
|
// | "Stmts": "\n\t\t"
|
2018-06-19 20:55:12 +00:00
|
|
|
// | "NamespaceName":
|
|
|
|
// | [*name.Name]
|
|
|
|
// | "Position": Pos{Line: 3-3 Pos: 20-22}
|
2019-02-13 20:18:07 +00:00
|
|
|
// | "freefloating":
|
|
|
|
// | "Start": " "
|
|
|
|
// | "End": " "
|
2018-06-19 20:55:12 +00:00
|
|
|
// | "Parts":
|
|
|
|
// | [*name.NamePart]
|
|
|
|
// | "Position": Pos{Line: 3-3 Pos: 20-22}
|
|
|
|
// | "Value": "Foo"
|
|
|
|
// | "Stmts":
|
|
|
|
// | [*stmt.Class]
|
2019-02-13 20:18:07 +00:00
|
|
|
// | "Position": Pos{Line: 4-10 Pos: 29-140}
|
2018-06-19 20:55:12 +00:00
|
|
|
// | "NamespacedName": "Foo\\Bar"
|
2019-02-13 20:18:07 +00:00
|
|
|
// | "freefloating":
|
|
|
|
// | "Start": "\n\t\t\t"
|
|
|
|
// | "Name": " "
|
|
|
|
// | "Stmts": "\n\t\t\t"
|
2018-06-19 20:55:12 +00:00
|
|
|
// | "PhpDocComment": ""
|
|
|
|
// | "ClassName":
|
|
|
|
// | [*node.Identifier]
|
|
|
|
// | "Position": Pos{Line: 4-4 Pos: 35-37}
|
2019-02-13 20:18:07 +00:00
|
|
|
// | "freefloating":
|
|
|
|
// | "Start": " "
|
2018-06-19 20:55:12 +00:00
|
|
|
// | "Value": "Bar"
|
|
|
|
// | "Stmts":
|
|
|
|
// | [*stmt.ClassMethod]
|
2019-02-13 20:18:07 +00:00
|
|
|
// | "Position": Pos{Line: 5-9 Pos: 46-135}
|
|
|
|
// | "freefloating":
|
|
|
|
// | "Start": " \n\t\t\t\t"
|
|
|
|
// | "ModifierList": " "
|
|
|
|
// | "Function": " "
|
2018-06-19 20:55:12 +00:00
|
|
|
// | "ReturnsRef": false
|
|
|
|
// | "PhpDocComment": ""
|
|
|
|
// | "MethodName":
|
|
|
|
// | [*node.Identifier]
|
2019-02-13 20:18:07 +00:00
|
|
|
// | "Position": Pos{Line: 5-5 Pos: 62-73}
|
2018-06-19 20:55:12 +00:00
|
|
|
// | "Value": "FunctionName"
|
|
|
|
// | "Modifiers":
|
|
|
|
// | [*node.Identifier]
|
2019-02-13 20:18:07 +00:00
|
|
|
// | "Position": Pos{Line: 5-5 Pos: 46-51}
|
2018-06-19 20:55:12 +00:00
|
|
|
// | "Value": "public"
|
|
|
|
// | "Params":
|
|
|
|
// | [*node.Parameter]
|
2019-02-13 20:18:07 +00:00
|
|
|
// | "Position": Pos{Line: 5-5 Pos: 75-90}
|
|
|
|
// | "freefloating":
|
|
|
|
// | "OptionalType": " "
|
|
|
|
// | "Var": " "
|
2018-07-02 17:48:55 +00:00
|
|
|
// | "Variadic": false
|
2019-02-13 20:18:07 +00:00
|
|
|
// | "ByRef": false
|
2018-06-19 20:55:12 +00:00
|
|
|
// | "VariableType":
|
|
|
|
// | [*name.Name]
|
2019-02-13 20:18:07 +00:00
|
|
|
// | "Position": Pos{Line: 5-5 Pos: 75-78}
|
2018-06-19 20:55:12 +00:00
|
|
|
// | "NamespacedName": "Foo\\Type"
|
|
|
|
// | "Parts":
|
|
|
|
// | [*name.NamePart]
|
2019-02-13 20:18:07 +00:00
|
|
|
// | "Position": Pos{Line: 5-5 Pos: 75-78}
|
2018-06-19 20:55:12 +00:00
|
|
|
// | "Value": "Type"
|
|
|
|
// | "Variable":
|
|
|
|
// | [*expr.Variable]
|
2019-02-13 20:18:07 +00:00
|
|
|
// | "Position": Pos{Line: 5-5 Pos: 80-83}
|
|
|
|
// | "freefloating":
|
|
|
|
// | "Dollar": "$"
|
2018-06-19 20:55:12 +00:00
|
|
|
// | "VarName":
|
|
|
|
// | [*node.Identifier]
|
2019-02-13 20:18:07 +00:00
|
|
|
// | "Position": Pos{Line: 5-5 Pos: 80-83}
|
2018-06-19 20:55:12 +00:00
|
|
|
// | "Value": "var"
|
|
|
|
// | "DefaultValue":
|
|
|
|
// | [*expr.ConstFetch]
|
2019-02-13 20:18:07 +00:00
|
|
|
// | "Position": Pos{Line: 5-5 Pos: 87-90}
|
|
|
|
// | "freefloating":
|
|
|
|
// | "Start": " "
|
2018-06-19 20:55:12 +00:00
|
|
|
// | "Constant":
|
|
|
|
// | [*name.Name]
|
2019-02-13 20:18:07 +00:00
|
|
|
// | "Position": Pos{Line: 5-5 Pos: 87-90}
|
2018-06-19 20:55:12 +00:00
|
|
|
// | "NamespacedName": "null"
|
|
|
|
// | "Parts":
|
|
|
|
// | [*name.NamePart]
|
2019-02-13 20:18:07 +00:00
|
|
|
// | "Position": Pos{Line: 5-5 Pos: 87-90}
|
2018-06-19 20:55:12 +00:00
|
|
|
// | "Value": "null"
|
|
|
|
// | "Stmt":
|
|
|
|
// | [*stmt.StmtList]
|
2019-02-13 20:18:07 +00:00
|
|
|
// | "Position": Pos{Line: 6-9 Pos: 97-135}
|
|
|
|
// | "freefloating":
|
|
|
|
// | "Start": "\n\t\t\t\t"
|
|
|
|
// | "Stmts": "\n\t\t\t\t"
|
2018-06-19 20:55:12 +00:00
|
|
|
// | "Stmts":
|
|
|
|
// | [*stmt.Expression]
|
2019-02-13 20:18:07 +00:00
|
|
|
// | "Position": Pos{Line: 8-8 Pos: 125-129}
|
|
|
|
// | "freefloating":
|
|
|
|
// | "SemiColon": ";"
|
|
|
|
// | "Start": "\n\t\t\t\t\t"
|
|
|
|
// | "Start": "// some comment\n"
|
|
|
|
// | "Start": "\t\t\t\t\t"
|
2018-06-19 20:55:12 +00:00
|
|
|
// | "Expr":
|
|
|
|
// | [*expr.Variable]
|
2019-02-13 20:18:07 +00:00
|
|
|
// | "Position": Pos{Line: 8-8 Pos: 125-128}
|
|
|
|
// | "freefloating":
|
|
|
|
// | "Dollar": "$"
|
2018-06-19 20:55:12 +00:00
|
|
|
// | "VarName":
|
|
|
|
// | [*node.Identifier]
|
2019-02-13 20:18:07 +00:00
|
|
|
// | "Position": Pos{Line: 8-8 Pos: 125-128}
|
2018-06-19 20:55:12 +00:00
|
|
|
// | "Value": "var"
|
2018-02-27 21:38:05 +00:00
|
|
|
}
|