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) nodes.Walk(nsResolver)
dumper := visitor.Dumper{ dumper := visitor.Dumper{
Indent: " | ", Indent: " | ",
Comments: comments, Comments: comments,
Positions: positions, Positions: positions,
NsResolver: nsResolver,
} }
nodes.Walk(dumper) nodes.Walk(dumper)
} }

View File

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

View File

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