Skip to content

Commit 411967e

Browse files
committed
update dumper: show resolved names
1 parent c046bfe commit 411967e

File tree

3 files changed

+82
-42
lines changed

3 files changed

+82
-42
lines changed

main.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ func main() {
4343
nodes.Walk(nsResolver)
4444

4545
dumper := visitor.Dumper{
46-
Indent: " | ",
47-
Comments: comments,
48-
Positions: positions,
46+
Indent: " | ",
47+
Comments: comments,
48+
Positions: positions,
49+
NsResolver: nsResolver,
4950
}
5051
nodes.Walk(dumper)
5152
}

visitor/dumper.go

+27-15
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,42 @@ import (
1515
// Dumper prints ast hierarchy to stdout
1616
// Also prints comments and positions attached to nodes
1717
type Dumper struct {
18-
Indent string
19-
Comments comment.Comments
20-
Positions position.Positions
18+
Indent string
19+
Comments comment.Comments
20+
Positions position.Positions
21+
NsResolver *NamespaceResolver
2122
}
2223

2324
// EnterNode is invoked at every node in heirerchy
2425
func (d Dumper) EnterNode(w walker.Walkable) bool {
2526
n := w.(node.Node)
2627

27-
fmt.Printf("%v%v", d.Indent, reflect.TypeOf(n))
28-
if p := d.Positions[n]; p != nil {
29-
fmt.Printf(" %v", *p)
28+
fmt.Printf("%v[%v]\n", d.Indent, reflect.TypeOf(n))
29+
30+
if d.Positions != nil {
31+
if p := d.Positions[n]; p != nil {
32+
fmt.Printf("%v\"Position\": %s;\n", d.Indent+" ", *p)
33+
}
3034
}
31-
if a := n.Attributes(); len(a) > 0 {
32-
for key, attr := range a {
33-
fmt.Printf("\n%v\"%v\": %v;", d.Indent+" ", key, attr)
35+
36+
if d.NsResolver != nil {
37+
if namespacedName, ok := d.NsResolver.ResolvedNames[n]; ok {
38+
fmt.Printf("%v\"NamespacedName\": %s;\n", d.Indent+" ", namespacedName)
3439
}
3540
}
36-
fmt.Println()
3741

38-
if c := d.Comments[n]; len(c) > 0 {
39-
fmt.Printf("%v\"Comments\":\n", d.Indent+" ")
40-
for _, cc := range c {
41-
fmt.Printf("%v%q\n", d.Indent+" ", cc)
42+
if d.Comments != nil {
43+
if c := d.Comments[n]; len(c) > 0 {
44+
fmt.Printf("%v\"Comments\":\n", d.Indent+" ")
45+
for _, cc := range c {
46+
fmt.Printf("%v%q\n", d.Indent+" ", cc)
47+
}
48+
}
49+
}
50+
51+
if a := n.Attributes(); len(a) > 0 {
52+
for key, attr := range a {
53+
fmt.Printf("%v\"%v\": %v;\n", d.Indent+" ", key, attr)
4254
}
4355
}
4456

@@ -48,7 +60,7 @@ func (d Dumper) EnterNode(w walker.Walkable) bool {
4860
// GetChildrenVisitor is invoked at every node parameter that contains children nodes
4961
func (d Dumper) GetChildrenVisitor(key string) walker.Visitor {
5062
fmt.Printf("%v%q:\n", d.Indent+" ", key)
51-
return Dumper{d.Indent + " ", d.Comments, d.Positions}
63+
return Dumper{d.Indent + " ", d.Comments, d.Positions, d.NsResolver}
5264
}
5365

5466
// LeaveNode is invoked after node process

visitor/dumper_test.go

+51-24
Original file line numberDiff line numberDiff line change
@@ -23,69 +23,96 @@ func ExampleDumper() {
2323

2424
nodes, comments, positions := php7.Parse(bytes.NewBufferString(src), "test.php")
2525

26+
nsResolver := visitor.NewNamespaceResolver()
27+
nodes.Walk(nsResolver)
28+
2629
dumper := visitor.Dumper{
27-
Indent: "| ",
28-
Comments: comments,
29-
Positions: positions,
30+
Indent: "| ",
31+
Comments: comments,
32+
Positions: positions,
33+
NsResolver: nsResolver,
3034
}
3135
nodes.Walk(dumper)
3236

3337
// Unordered output:
34-
//| *stmt.StmtList Pos{Line: 3-11 Pos: 10-143}
38+
//| [*stmt.StmtList]
39+
//| "Position": Pos{Line: 3-11 Pos: 10-143};
3540
//| "Stmts":
36-
//| *stmt.Namespace Pos{Line: 3-11 Pos: 10-143}
41+
//| [*stmt.Namespace]
42+
//| "Position": Pos{Line: 3-11 Pos: 10-143};
3743
//| "NamespaceName":
38-
//| *name.Name Pos{Line: 3-3 Pos: 20-22}
44+
//| [*name.Name]
45+
//| "Position": Pos{Line: 3-3 Pos: 20-22};
3946
//| "Parts":
40-
//| *name.NamePart Pos{Line: 3-3 Pos: 20-22}
47+
//| [*name.NamePart]
48+
//| "Position": Pos{Line: 3-3 Pos: 20-22};
4149
//| "Value": Foo;
4250
//| "Stmts":
43-
//| *stmt.Class Pos{Line: 4-10 Pos: 29-139}
51+
//| [*stmt.Class]
52+
//| "Position": Pos{Line: 4-10 Pos: 29-139};
53+
//| "NamespacedName": Foo\Bar;
4454
//| "PhpDocComment": ;
4555
//| "ClassName":
46-
//| *node.Identifier Pos{Line: 4-4 Pos: 35-37}
56+
//| [*node.Identifier]
57+
//| "Position": Pos{Line: 4-4 Pos: 35-37};
4758
//| "Value": Bar;
4859
//| "Stmts":
49-
//| *stmt.ClassMethod Pos{Line: 5-9 Pos: 45-134}
50-
//| "ReturnsRef": false;
60+
//| [*stmt.ClassMethod]
61+
//| "Position": Pos{Line: 5-9 Pos: 45-134};
5162
//| "PhpDocComment": ;
63+
//| "ReturnsRef": false;
5264
//| "MethodName":
53-
//| *node.Identifier Pos{Line: 5-5 Pos: 61-72}
65+
//| [*node.Identifier]
66+
//| "Position": Pos{Line: 5-5 Pos: 61-72};
5467
//| "Value": FunctionName;
5568
//| "Modifiers":
56-
//| *node.Identifier Pos{Line: 5-5 Pos: 45-50}
69+
//| [*node.Identifier]
70+
//| "Position": Pos{Line: 5-5 Pos: 45-50};
5771
//| "Value": public;
5872
//| "Params":
59-
//| *node.Parameter Pos{Line: 5-5 Pos: 74-89}
73+
//| [*node.Parameter]
74+
//| "Position": Pos{Line: 5-5 Pos: 74-89};
6075
//| "ByRef": false;
6176
//| "Variadic": false;
6277
//| "VariableType":
63-
//| *name.Name Pos{Line: 5-5 Pos: 74-77}
78+
//| [*name.Name]
79+
//| "Position": Pos{Line: 5-5 Pos: 74-77};
80+
//| "NamespacedName": Foo\Type;
6481
//| "Parts":
65-
//| *name.NamePart Pos{Line: 5-5 Pos: 74-77}
82+
//| [*name.NamePart]
83+
//| "Position": Pos{Line: 5-5 Pos: 74-77};
6684
//| "Value": Type;
6785
//| "Variable":
68-
//| *expr.Variable Pos{Line: 5-5 Pos: 79-82}
86+
//| [*expr.Variable]
87+
//| "Position": Pos{Line: 5-5 Pos: 79-82};
6988
//| "VarName":
70-
//| *node.Identifier Pos{Line: 5-5 Pos: 79-82}
89+
//| [*node.Identifier]
90+
//| "Position": Pos{Line: 5-5 Pos: 79-82};
7191
//| "Value": $var;
7292
//| "DefaultValue":
73-
//| *expr.ConstFetch Pos{Line: 5-5 Pos: 86-89}
93+
//| [*expr.ConstFetch]
94+
//| "Position": Pos{Line: 5-5 Pos: 86-89};
7495
//| "Constant":
75-
//| *name.Name Pos{Line: 5-5 Pos: 86-89}
96+
//| [*name.Name]
97+
//| "Position": Pos{Line: 5-5 Pos: 86-89};
98+
//| "NamespacedName": Foo\null;
7699
//| "Parts":
77-
//| *name.NamePart Pos{Line: 5-5 Pos: 86-89}
100+
//| [*name.NamePart]
101+
//| "Position": Pos{Line: 5-5 Pos: 86-89};
78102
//| "Value": null;
79103
//| "Stmts":
80-
//| *stmt.Expression Pos{Line: 8-8 Pos: 124-128}
104+
//| [*stmt.Expression]
105+
//| "Position": Pos{Line: 8-8 Pos: 124-128};
81106
//| "Comments":
82107
//| "// some comment\n"
83108
//| "Expr":
84-
//| *expr.Variable Pos{Line: 8-8 Pos: 124-127}
109+
//| [*expr.Variable]
110+
//| "Position": Pos{Line: 8-8 Pos: 124-127};
85111
//| "Comments":
86112
//| "// some comment\n"
87113
//| "VarName":
88-
//| *node.Identifier Pos{Line: 8-8 Pos: 124-127}
114+
//| [*node.Identifier]
115+
//| "Position": Pos{Line: 8-8 Pos: 124-127};
89116
//| "Value": $var;
90117
//| "Comments":
91118
//| "// some comment\n"

0 commit comments

Comments
 (0)