2018-02-27 21:38:05 +00:00
|
|
|
// Package visitor contains walker.visitor implementations
|
|
|
|
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 {
|
|
|
|
class Bar {
|
|
|
|
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")
|
|
|
|
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-02-27 21:38:05 +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: "| ",
|
2018-04-10 12:23:13 +00:00
|
|
|
Comments: php7parser.GetComments(),
|
|
|
|
Positions: php7parser.GetPositions(),
|
2018-03-01 20:31:36 +00:00
|
|
|
NsResolver: nsResolver,
|
2018-02-27 21:38:05 +00:00
|
|
|
}
|
|
|
|
nodes.Walk(dumper)
|
|
|
|
|
2018-02-27 21:57:33 +00:00
|
|
|
// Unordered output:
|
2018-05-02 09:14:24 +00:00
|
|
|
//| [*node.Root]
|
2018-03-01 20:31:36 +00:00
|
|
|
//| "Position": Pos{Line: 3-11 Pos: 10-143};
|
2018-02-27 21:38:05 +00:00
|
|
|
//| "Stmts":
|
2018-03-01 20:31:36 +00:00
|
|
|
//| [*stmt.Namespace]
|
|
|
|
//| "Position": Pos{Line: 3-11 Pos: 10-143};
|
2018-02-27 21:38:05 +00:00
|
|
|
//| "NamespaceName":
|
2018-03-01 20:31:36 +00:00
|
|
|
//| [*name.Name]
|
|
|
|
//| "Position": Pos{Line: 3-3 Pos: 20-22};
|
2018-02-27 21:38:05 +00:00
|
|
|
//| "Parts":
|
2018-03-01 20:31:36 +00:00
|
|
|
//| [*name.NamePart]
|
|
|
|
//| "Position": Pos{Line: 3-3 Pos: 20-22};
|
2018-02-27 21:57:33 +00:00
|
|
|
//| "Value": Foo;
|
2018-02-27 21:38:05 +00:00
|
|
|
//| "Stmts":
|
2018-03-01 20:31:36 +00:00
|
|
|
//| [*stmt.Class]
|
|
|
|
//| "Position": Pos{Line: 4-10 Pos: 29-139};
|
|
|
|
//| "NamespacedName": Foo\Bar;
|
2018-02-27 21:57:33 +00:00
|
|
|
//| "PhpDocComment": ;
|
2018-02-27 21:38:05 +00:00
|
|
|
//| "ClassName":
|
2018-03-01 20:31:36 +00:00
|
|
|
//| [*node.Identifier]
|
|
|
|
//| "Position": Pos{Line: 4-4 Pos: 35-37};
|
2018-02-27 21:57:33 +00:00
|
|
|
//| "Value": Bar;
|
2018-02-27 21:38:05 +00:00
|
|
|
//| "Stmts":
|
2018-03-01 20:31:36 +00:00
|
|
|
//| [*stmt.ClassMethod]
|
|
|
|
//| "Position": Pos{Line: 5-9 Pos: 45-134};
|
|
|
|
//| "ReturnsRef": false;
|
2018-06-03 06:35:44 +00:00
|
|
|
//| "PhpDocComment": ;
|
2018-02-27 21:38:05 +00:00
|
|
|
//| "MethodName":
|
2018-03-01 20:31:36 +00:00
|
|
|
//| [*node.Identifier]
|
|
|
|
//| "Position": Pos{Line: 5-5 Pos: 61-72};
|
2018-02-27 21:57:33 +00:00
|
|
|
//| "Value": FunctionName;
|
2018-02-27 21:38:05 +00:00
|
|
|
//| "Modifiers":
|
2018-03-01 20:31:36 +00:00
|
|
|
//| [*node.Identifier]
|
|
|
|
//| "Position": Pos{Line: 5-5 Pos: 45-50};
|
2018-02-27 21:57:33 +00:00
|
|
|
//| "Value": public;
|
2018-02-27 21:38:05 +00:00
|
|
|
//| "Params":
|
2018-03-01 20:31:36 +00:00
|
|
|
//| [*node.Parameter]
|
|
|
|
//| "Position": Pos{Line: 5-5 Pos: 74-89};
|
2018-02-27 21:57:33 +00:00
|
|
|
//| "ByRef": false;
|
|
|
|
//| "Variadic": false;
|
2018-02-27 21:38:05 +00:00
|
|
|
//| "VariableType":
|
2018-03-01 20:31:36 +00:00
|
|
|
//| [*name.Name]
|
|
|
|
//| "Position": Pos{Line: 5-5 Pos: 74-77};
|
|
|
|
//| "NamespacedName": Foo\Type;
|
2018-02-27 21:38:05 +00:00
|
|
|
//| "Parts":
|
2018-03-01 20:31:36 +00:00
|
|
|
//| [*name.NamePart]
|
|
|
|
//| "Position": Pos{Line: 5-5 Pos: 74-77};
|
2018-02-27 21:57:33 +00:00
|
|
|
//| "Value": Type;
|
2018-02-27 21:38:05 +00:00
|
|
|
//| "Variable":
|
2018-03-01 20:31:36 +00:00
|
|
|
//| [*expr.Variable]
|
|
|
|
//| "Position": Pos{Line: 5-5 Pos: 79-82};
|
2018-02-27 21:38:05 +00:00
|
|
|
//| "VarName":
|
2018-03-01 20:31:36 +00:00
|
|
|
//| [*node.Identifier]
|
|
|
|
//| "Position": Pos{Line: 5-5 Pos: 79-82};
|
2018-03-18 14:50:19 +00:00
|
|
|
//| "Value": var;
|
2018-02-27 21:38:05 +00:00
|
|
|
//| "DefaultValue":
|
2018-03-01 20:31:36 +00:00
|
|
|
//| [*expr.ConstFetch]
|
|
|
|
//| "Position": Pos{Line: 5-5 Pos: 86-89};
|
2018-02-27 21:38:05 +00:00
|
|
|
//| "Constant":
|
2018-03-01 20:31:36 +00:00
|
|
|
//| [*name.Name]
|
|
|
|
//| "Position": Pos{Line: 5-5 Pos: 86-89};
|
2018-06-17 21:15:41 +00:00
|
|
|
//| "NamespacedName": null;
|
2018-02-27 21:38:05 +00:00
|
|
|
//| "Parts":
|
2018-03-01 20:31:36 +00:00
|
|
|
//| [*name.NamePart]
|
|
|
|
//| "Position": Pos{Line: 5-5 Pos: 86-89};
|
2018-02-27 21:57:33 +00:00
|
|
|
//| "Value": null;
|
2018-06-03 06:35:44 +00:00
|
|
|
//| "Stmt":
|
|
|
|
//| [*stmt.StmtList]
|
|
|
|
//| "Position": Pos{Line: 6-9 Pos: 96-134};
|
|
|
|
//| "Stmts":
|
|
|
|
//| [*stmt.Expression]
|
|
|
|
//| "Position": Pos{Line: 8-8 Pos: 124-128};
|
|
|
|
//| "Expr":
|
|
|
|
//| [*expr.Variable]
|
2018-03-01 20:31:36 +00:00
|
|
|
//| "Position": Pos{Line: 8-8 Pos: 124-127};
|
2018-06-03 06:35:44 +00:00
|
|
|
//| "Comments":
|
|
|
|
//| "// some comment\n" before "VariableToken"
|
|
|
|
//| "VarName":
|
|
|
|
//| [*node.Identifier]
|
|
|
|
//| "Position": Pos{Line: 8-8 Pos: 124-127};
|
|
|
|
//| "Value": var;
|
2018-02-27 21:38:05 +00:00
|
|
|
}
|