update dumper: show resolved names

This commit is contained in:
z7zmey 2018-03-01 22:31:36 +02:00
parent c046bfe1b7
commit 411967e87e
3 changed files with 83 additions and 43 deletions

View File

@ -43,9 +43,10 @@ func main() {
nodes.Walk(nsResolver)
dumper := visitor.Dumper{
Indent: " | ",
Comments: comments,
Positions: positions,
Indent: " | ",
Comments: comments,
Positions: positions,
NsResolver: nsResolver,
}
nodes.Walk(dumper)
}

View File

@ -15,30 +15,42 @@ import (
// Dumper prints ast hierarchy to stdout
// Also prints comments and positions attached to nodes
type Dumper struct {
Indent string
Comments comment.Comments
Positions position.Positions
Indent string
Comments comment.Comments
Positions position.Positions
NsResolver *NamespaceResolver
}
// EnterNode is invoked at every node in heirerchy
func (d Dumper) EnterNode(w walker.Walkable) bool {
n := w.(node.Node)
fmt.Printf("%v%v", d.Indent, reflect.TypeOf(n))
if p := d.Positions[n]; p != nil {
fmt.Printf(" %v", *p)
}
if a := n.Attributes(); len(a) > 0 {
for key, attr := range a {
fmt.Printf("\n%v\"%v\": %v;", d.Indent+" ", key, attr)
fmt.Printf("%v[%v]\n", d.Indent, reflect.TypeOf(n))
if d.Positions != nil {
if p := d.Positions[n]; p != nil {
fmt.Printf("%v\"Position\": %s;\n", d.Indent+" ", *p)
}
}
fmt.Println()
if c := d.Comments[n]; len(c) > 0 {
fmt.Printf("%v\"Comments\":\n", d.Indent+" ")
for _, cc := range c {
fmt.Printf("%v%q\n", d.Indent+" ", cc)
if d.NsResolver != nil {
if namespacedName, ok := d.NsResolver.ResolvedNames[n]; ok {
fmt.Printf("%v\"NamespacedName\": %s;\n", d.Indent+" ", namespacedName)
}
}
if d.Comments != nil {
if c := d.Comments[n]; len(c) > 0 {
fmt.Printf("%v\"Comments\":\n", d.Indent+" ")
for _, cc := range c {
fmt.Printf("%v%q\n", d.Indent+" ", cc)
}
}
}
if a := n.Attributes(); len(a) > 0 {
for key, attr := range a {
fmt.Printf("%v\"%v\": %v;\n", d.Indent+" ", key, attr)
}
}
@ -48,7 +60,7 @@ func (d Dumper) EnterNode(w walker.Walkable) bool {
// GetChildrenVisitor is invoked at every node parameter that contains children nodes
func (d Dumper) GetChildrenVisitor(key string) walker.Visitor {
fmt.Printf("%v%q:\n", d.Indent+" ", key)
return Dumper{d.Indent + " ", d.Comments, d.Positions}
return Dumper{d.Indent + " ", d.Comments, d.Positions, d.NsResolver}
}
// LeaveNode is invoked after node process

View File

@ -23,69 +23,96 @@ func ExampleDumper() {
nodes, comments, positions := php7.Parse(bytes.NewBufferString(src), "test.php")
nsResolver := visitor.NewNamespaceResolver()
nodes.Walk(nsResolver)
dumper := visitor.Dumper{
Indent: "| ",
Comments: comments,
Positions: positions,
Indent: "| ",
Comments: comments,
Positions: positions,
NsResolver: nsResolver,
}
nodes.Walk(dumper)
// Unordered output:
//| *stmt.StmtList Pos{Line: 3-11 Pos: 10-143}
//| [*stmt.StmtList]
//| "Position": Pos{Line: 3-11 Pos: 10-143};
//| "Stmts":
//| *stmt.Namespace Pos{Line: 3-11 Pos: 10-143}
//| [*stmt.Namespace]
//| "Position": Pos{Line: 3-11 Pos: 10-143};
//| "NamespaceName":
//| *name.Name Pos{Line: 3-3 Pos: 20-22}
//| [*name.Name]
//| "Position": Pos{Line: 3-3 Pos: 20-22};
//| "Parts":
//| *name.NamePart Pos{Line: 3-3 Pos: 20-22}
//| [*name.NamePart]
//| "Position": Pos{Line: 3-3 Pos: 20-22};
//| "Value": Foo;
//| "Stmts":
//| *stmt.Class Pos{Line: 4-10 Pos: 29-139}
//| [*stmt.Class]
//| "Position": Pos{Line: 4-10 Pos: 29-139};
//| "NamespacedName": Foo\Bar;
//| "PhpDocComment": ;
//| "ClassName":
//| *node.Identifier Pos{Line: 4-4 Pos: 35-37}
//| [*node.Identifier]
//| "Position": Pos{Line: 4-4 Pos: 35-37};
//| "Value": Bar;
//| "Stmts":
//| *stmt.ClassMethod Pos{Line: 5-9 Pos: 45-134}
//| "ReturnsRef": false;
//| [*stmt.ClassMethod]
//| "Position": Pos{Line: 5-9 Pos: 45-134};
//| "PhpDocComment": ;
//| "ReturnsRef": false;
//| "MethodName":
//| *node.Identifier Pos{Line: 5-5 Pos: 61-72}
//| [*node.Identifier]
//| "Position": Pos{Line: 5-5 Pos: 61-72};
//| "Value": FunctionName;
//| "Modifiers":
//| *node.Identifier Pos{Line: 5-5 Pos: 45-50}
//| [*node.Identifier]
//| "Position": Pos{Line: 5-5 Pos: 45-50};
//| "Value": public;
//| "Params":
//| *node.Parameter Pos{Line: 5-5 Pos: 74-89}
//| [*node.Parameter]
//| "Position": Pos{Line: 5-5 Pos: 74-89};
//| "ByRef": false;
//| "Variadic": false;
//| "VariableType":
//| *name.Name Pos{Line: 5-5 Pos: 74-77}
//| [*name.Name]
//| "Position": Pos{Line: 5-5 Pos: 74-77};
//| "NamespacedName": Foo\Type;
//| "Parts":
//| *name.NamePart Pos{Line: 5-5 Pos: 74-77}
//| [*name.NamePart]
//| "Position": Pos{Line: 5-5 Pos: 74-77};
//| "Value": Type;
//| "Variable":
//| *expr.Variable Pos{Line: 5-5 Pos: 79-82}
//| [*expr.Variable]
//| "Position": Pos{Line: 5-5 Pos: 79-82};
//| "VarName":
//| *node.Identifier Pos{Line: 5-5 Pos: 79-82}
//| [*node.Identifier]
//| "Position": Pos{Line: 5-5 Pos: 79-82};
//| "Value": $var;
//| "DefaultValue":
//| *expr.ConstFetch Pos{Line: 5-5 Pos: 86-89}
//| [*expr.ConstFetch]
//| "Position": Pos{Line: 5-5 Pos: 86-89};
//| "Constant":
//| *name.Name Pos{Line: 5-5 Pos: 86-89}
//| [*name.Name]
//| "Position": Pos{Line: 5-5 Pos: 86-89};
//| "NamespacedName": Foo\null;
//| "Parts":
//| *name.NamePart Pos{Line: 5-5 Pos: 86-89}
//| [*name.NamePart]
//| "Position": Pos{Line: 5-5 Pos: 86-89};
//| "Value": null;
//| "Stmts":
//| *stmt.Expression Pos{Line: 8-8 Pos: 124-128}
//| [*stmt.Expression]
//| "Position": Pos{Line: 8-8 Pos: 124-128};
//| "Comments":
//| "// some comment\n"
//| "Expr":
//| *expr.Variable Pos{Line: 8-8 Pos: 124-127}
//| [*expr.Variable]
//| "Position": Pos{Line: 8-8 Pos: 124-127};
//| "Comments":
//| "// some comment\n"
//| "VarName":
//| *node.Identifier Pos{Line: 8-8 Pos: 124-127}
//| [*node.Identifier]
//| "Position": Pos{Line: 8-8 Pos: 124-127};
//| "Value": $var;
//| "Comments":
//| "// some comment\n"