From 411967e87efaf1e761afc28381166a4d77a4237f Mon Sep 17 00:00:00 2001 From: z7zmey Date: Thu, 1 Mar 2018 22:31:36 +0200 Subject: [PATCH] update dumper: show resolved names --- main.go | 7 ++-- visitor/dumper.go | 44 ++++++++++++++++--------- visitor/dumper_test.go | 75 ++++++++++++++++++++++++++++-------------- 3 files changed, 83 insertions(+), 43 deletions(-) diff --git a/main.go b/main.go index e3cb8e3..7b599a7 100644 --- a/main.go +++ b/main.go @@ -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) } diff --git a/visitor/dumper.go b/visitor/dumper.go index 242981e..f8e2a6b 100644 --- a/visitor/dumper.go +++ b/visitor/dumper.go @@ -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 diff --git a/visitor/dumper_test.go b/visitor/dumper_test.go index e0c2fcf..8016cbb 100644 --- a/visitor/dumper_test.go +++ b/visitor/dumper_test.go @@ -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"