php-parser/visitor/dumper_test.go

154 lines
5.7 KiB
Go
Raw Normal View History

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 {
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.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)
// Unordered output:
2018-06-19 20:55:12 +00:00
// | [*node.Root]
// | "Position": Pos{Line: 3-11 Pos: 10-143}
// | "Stmts":
// | [*stmt.Namespace]
// | "Position": Pos{Line: 3-11 Pos: 10-143}
// | "Meta":
2018-10-25 10:32:18 +00:00
// | "<?php" before "NodeStart"
// | "\n\n\t\t" before "NodeStart"
// | " " before "OpenCurlyBracesToken"
// | "\n\t\t" before "CloseCurlyBracesToken"
2018-06-19 20:55:12 +00:00
// | "NamespaceName":
// | [*name.Name]
// | "Position": Pos{Line: 3-3 Pos: 20-22}
// | "Parts":
// | [*name.NamePart]
// | "Position": Pos{Line: 3-3 Pos: 20-22}
// | "Meta":
// | " " before "NodeStart"
2018-06-19 20:55:12 +00:00
// | "Value": "Foo"
// | "Stmts":
// | [*stmt.Class]
// | "Position": Pos{Line: 4-10 Pos: 29-139}
// | "NamespacedName": "Foo\\Bar"
// | "Meta":
// | "\n\t\t\t" before "NodeStart"
// | " " before "OpenCurlyBracesToken"
// | "\n\t\t\t" before "CloseCurlyBracesToken"
2018-06-19 20:55:12 +00:00
// | "PhpDocComment": ""
// | "ClassName":
// | [*node.Identifier]
// | "Position": Pos{Line: 4-4 Pos: 35-37}
// | "Meta":
2018-10-25 09:31:17 +00:00
// | " " before "NodeStart"
2018-06-19 20:55:12 +00:00
// | "Value": "Bar"
// | "Stmts":
// | [*stmt.ClassMethod]
// | "Position": Pos{Line: 5-9 Pos: 45-134}
// | "Meta":
// | "\n\t\t\t\t" before "NodeStart"
// | " " before "FunctionToken"
2018-06-19 20:55:12 +00:00
// | "ReturnsRef": false
// | "PhpDocComment": ""
// | "MethodName":
// | [*node.Identifier]
// | "Position": Pos{Line: 5-5 Pos: 61-72}
// | "Meta":
2018-10-25 09:31:17 +00:00
// | " " before "NodeStart"
2018-06-19 20:55:12 +00:00
// | "Value": "FunctionName"
// | "Modifiers":
// | [*node.Identifier]
// | "Position": Pos{Line: 5-5 Pos: 45-50}
// | "Value": "public"
// | "Params":
// | [*node.Parameter]
// | "Position": Pos{Line: 5-5 Pos: 74-89}
// | "Meta":
// | " " before "EqualToken"
2018-06-19 20:55:12 +00:00
// | "ByRef": false
// | "Variadic": false
2018-06-19 20:55:12 +00:00
// | "VariableType":
// | [*name.Name]
// | "Position": Pos{Line: 5-5 Pos: 74-77}
// | "NamespacedName": "Foo\\Type"
// | "Parts":
// | [*name.NamePart]
// | "Position": Pos{Line: 5-5 Pos: 74-77}
// | "Value": "Type"
// | "Variable":
// | [*expr.Variable]
// | "Position": Pos{Line: 5-5 Pos: 79-82}
// | "Meta":
// | " " before "NodeStart"
// | "$" before "NodeStart"
2018-06-19 20:55:12 +00:00
// | "VarName":
// | [*node.Identifier]
// | "Position": Pos{Line: 5-5 Pos: 79-82}
// | "Value": "var"
// | "DefaultValue":
// | [*expr.ConstFetch]
// | "Position": Pos{Line: 5-5 Pos: 86-89}
// | "Meta":
// | " " before "NodeStart"
2018-06-19 20:55:12 +00:00
// | "Constant":
// | [*name.Name]
// | "Position": Pos{Line: 5-5 Pos: 86-89}
// | "NamespacedName": "null"
// | "Parts":
// | [*name.NamePart]
// | "Position": Pos{Line: 5-5 Pos: 86-89}
// | "Value": "null"
// | "Stmt":
// | [*stmt.StmtList]
// | "Position": Pos{Line: 6-9 Pos: 96-134}
// | "Meta":
// | "\n\t\t\t\t" before "NodeStart"
// | "\n\t\t\t\t" before "CloseCurlyBracesToken"
2018-06-19 20:55:12 +00:00
// | "Stmts":
// | [*stmt.Expression]
// | "Position": Pos{Line: 8-8 Pos: 124-128}
// | "Meta":
// | "\n\t\t\t\t\t" before "NodeStart"
// | "// some comment\n" before "NodeStart"
// | "\t\t\t\t\t" before "NodeStart"
// | ";" before "SemiColonToken"
2018-06-19 20:55:12 +00:00
// | "Expr":
// | [*expr.Variable]
// | "Position": Pos{Line: 8-8 Pos: 124-127}
// | "Meta":
// | "$" before "NodeStart"
2018-06-19 20:55:12 +00:00
// | "VarName":
// | [*node.Identifier]
// | "Position": Pos{Line: 8-8 Pos: 124-127}
// | "Value": "var"
2018-02-27 21:38:05 +00:00
}