php-parser/visitor/dumper_test.go

120 lines
4.1 KiB
Go
Raw Normal View History

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)
// Unordered output:
2018-03-01 20:31:36 +00:00
//| [*stmt.StmtList]
//| "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};
//| "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;
//| "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};
//| "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};
//| "PhpDocComment": ;
2018-03-01 20:31:36 +00:00
//| "ReturnsRef": false;
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};
//| "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};
//| "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};
//| "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};
//| "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};
//| "NamespacedName": Foo\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};
//| "Value": null;
2018-02-27 21:38:05 +00:00
//| "Stmts":
2018-03-01 20:31:36 +00:00
//| [*stmt.Expression]
//| "Position": Pos{Line: 8-8 Pos: 124-128};
2018-02-27 21:38:05 +00:00
//| "Expr":
2018-03-01 20:31:36 +00:00
//| [*expr.Variable]
//| "Position": Pos{Line: 8-8 Pos: 124-127};
//| "Comments":
2018-05-27 19:37:22 +00:00
//| "// some comment\n" before "VariableToken"
2018-02-27 21:38:05 +00:00
//| "VarName":
2018-03-01 20:31:36 +00:00
//| [*node.Identifier]
//| "Position": Pos{Line: 8-8 Pos: 124-127};
2018-03-18 14:50:19 +00:00
//| "Value": var;
2018-02-27 21:38:05 +00:00
}