php-parser/visitor/dumper_test.go

120 lines
4.2 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"
"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;
}
}
}`
nodes, comments, positions := php7.Parse(bytes.NewBufferString(src), "test.php")
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-01 20:31:36 +00:00
Indent: "| ",
Comments: comments,
Positions: positions,
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};
//| "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};
//| "Comments":
2018-02-27 21:38:05 +00:00
//| "// some comment\n"
//| "Expr":
2018-03-01 20:31:36 +00:00
//| [*expr.Variable]
//| "Position": Pos{Line: 8-8 Pos: 124-127};
//| "Comments":
2018-02-27 21:38:05 +00:00
//| "// some comment\n"
//| "VarName":
2018-03-01 20:31:36 +00:00
//| [*node.Identifier]
//| "Position": Pos{Line: 8-8 Pos: 124-127};
//| "Value": $var;
//| "Comments":
2018-02-27 21:38:05 +00:00
//| "// some comment\n"
}