#25: save position within node
This commit is contained in:
@@ -20,7 +20,6 @@ type Dumper struct {
|
||||
Writer io.Writer
|
||||
Indent string
|
||||
Comments parser.Comments
|
||||
Positions parser.Positions
|
||||
NsResolver *NamespaceResolver
|
||||
}
|
||||
|
||||
@@ -30,10 +29,8 @@ func (d *Dumper) EnterNode(w walker.Walkable) bool {
|
||||
|
||||
fmt.Fprintf(d.Writer, "%v[%v]\n", d.Indent, reflect.TypeOf(n))
|
||||
|
||||
if d.Positions != nil {
|
||||
if p := d.Positions[n]; p != nil {
|
||||
fmt.Fprintf(d.Writer, "%v\"Position\": %s\n", d.Indent+" ", *p)
|
||||
}
|
||||
if n.GetPosition() != nil {
|
||||
fmt.Fprintf(d.Writer, "%v\"Position\": %s\n", d.Indent+" ", n.GetPosition())
|
||||
}
|
||||
|
||||
if d.NsResolver != nil {
|
||||
|
||||
@@ -33,7 +33,6 @@ func ExampleDumper() {
|
||||
Writer: os.Stdout,
|
||||
Indent: "| ",
|
||||
Comments: php7parser.GetComments(),
|
||||
Positions: php7parser.GetPositions(),
|
||||
NsResolver: nsResolver,
|
||||
}
|
||||
nodes.Walk(dumper)
|
||||
|
||||
@@ -42,6 +42,23 @@ func (d *GoDumper) EnterNode(w walker.Walkable) bool {
|
||||
|
||||
d.depth++
|
||||
|
||||
if p := n.GetPosition(); p != nil {
|
||||
printIndent(d.Writer, d.depth)
|
||||
fmt.Fprint(d.Writer, "Position: &position.Position{\n")
|
||||
d.depth++
|
||||
printIndent(d.Writer, d.depth)
|
||||
fmt.Fprintf(d.Writer, "StartLine: %d,\n", p.StartLine)
|
||||
printIndent(d.Writer, d.depth)
|
||||
fmt.Fprintf(d.Writer, "EndLine: %d,\n", p.EndLine)
|
||||
printIndent(d.Writer, d.depth)
|
||||
fmt.Fprintf(d.Writer, "StartPos: %d,\n", p.StartPos)
|
||||
printIndent(d.Writer, d.depth)
|
||||
fmt.Fprintf(d.Writer, "EndPos: %d,\n", p.EndPos)
|
||||
d.depth--
|
||||
printIndent(d.Writer, d.depth)
|
||||
fmt.Fprint(d.Writer, "},\n")
|
||||
}
|
||||
|
||||
if a := n.Attributes(); len(a) > 0 {
|
||||
for key, attr := range a {
|
||||
printIndent(d.Writer, d.depth)
|
||||
|
||||
@@ -36,53 +36,155 @@ func ExampleGoDumper() {
|
||||
|
||||
// Unordered output:
|
||||
//&node.Root{
|
||||
// Position: &position.Position{
|
||||
// StartLine: 3,
|
||||
// EndLine: 11,
|
||||
// StartPos: 10,
|
||||
// EndPos: 142,
|
||||
// },
|
||||
// Stmts: []node.Node{
|
||||
// &stmt.Namespace{
|
||||
// Position: &position.Position{
|
||||
// StartLine: 3,
|
||||
// EndLine: 11,
|
||||
// StartPos: 10,
|
||||
// EndPos: 142,
|
||||
// },
|
||||
// NamespaceName: &name.Name{
|
||||
// Position: &position.Position{
|
||||
// StartLine: 3,
|
||||
// EndLine: 3,
|
||||
// StartPos: 20,
|
||||
// EndPos: 22,
|
||||
// },
|
||||
// Parts: []node.Node{
|
||||
// &name.NamePart{
|
||||
// Position: &position.Position{
|
||||
// StartLine: 3,
|
||||
// EndLine: 3,
|
||||
// StartPos: 20,
|
||||
// EndPos: 22,
|
||||
// },
|
||||
// Value: "Foo",
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// Stmts: []node.Node{
|
||||
// &stmt.Class{
|
||||
// Position: &position.Position{
|
||||
// StartLine: 4,
|
||||
// EndLine: 10,
|
||||
// StartPos: 29,
|
||||
// EndPos: 138,
|
||||
// },
|
||||
// PhpDocComment: "",
|
||||
// ClassName: &node.Identifier{
|
||||
// Position: &position.Position{
|
||||
// StartLine: 4,
|
||||
// EndLine: 4,
|
||||
// StartPos: 35,
|
||||
// EndPos: 37,
|
||||
// },
|
||||
// Value: "Bar",
|
||||
// },
|
||||
// Stmts: []node.Node{
|
||||
// &stmt.ClassMethod{
|
||||
// Position: &position.Position{
|
||||
// StartLine: 5,
|
||||
// EndLine: 9,
|
||||
// StartPos: 45,
|
||||
// EndPos: 133,
|
||||
// },
|
||||
// ReturnsRef: false,
|
||||
// PhpDocComment: "",
|
||||
// MethodName: &node.Identifier{
|
||||
// Position: &position.Position{
|
||||
// StartLine: 5,
|
||||
// EndLine: 5,
|
||||
// StartPos: 61,
|
||||
// EndPos: 72,
|
||||
// },
|
||||
// Value: "FunctionName",
|
||||
// },
|
||||
// Modifiers: []node.Node{
|
||||
// &node.Identifier{
|
||||
// Position: &position.Position{
|
||||
// StartLine: 5,
|
||||
// EndLine: 5,
|
||||
// StartPos: 45,
|
||||
// EndPos: 50,
|
||||
// },
|
||||
// Value: "public",
|
||||
// },
|
||||
// },
|
||||
// Params: []node.Node{
|
||||
// &node.Parameter{
|
||||
// ByRef: false,
|
||||
// Position: &position.Position{
|
||||
// StartLine: 5,
|
||||
// EndLine: 5,
|
||||
// StartPos: 74,
|
||||
// EndPos: 89,
|
||||
// },
|
||||
// Variadic: false,
|
||||
// ByRef: false,
|
||||
// VariableType: &name.Name{
|
||||
// Position: &position.Position{
|
||||
// StartLine: 5,
|
||||
// EndLine: 5,
|
||||
// StartPos: 74,
|
||||
// EndPos: 77,
|
||||
// },
|
||||
// Parts: []node.Node{
|
||||
// &name.NamePart{
|
||||
// Position: &position.Position{
|
||||
// StartLine: 5,
|
||||
// EndLine: 5,
|
||||
// StartPos: 74,
|
||||
// EndPos: 77,
|
||||
// },
|
||||
// Value: "Type",
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// Variable: &expr.Variable{
|
||||
// Position: &position.Position{
|
||||
// StartLine: 5,
|
||||
// EndLine: 5,
|
||||
// StartPos: 79,
|
||||
// EndPos: 82,
|
||||
// },
|
||||
// VarName: &node.Identifier{
|
||||
// Position: &position.Position{
|
||||
// StartLine: 5,
|
||||
// EndLine: 5,
|
||||
// StartPos: 79,
|
||||
// EndPos: 82,
|
||||
// },
|
||||
// Value: "var",
|
||||
// },
|
||||
// },
|
||||
// DefaultValue: &expr.ConstFetch{
|
||||
// Position: &position.Position{
|
||||
// StartLine: 5,
|
||||
// EndLine: 5,
|
||||
// StartPos: 86,
|
||||
// EndPos: 89,
|
||||
// },
|
||||
// Constant: &name.Name{
|
||||
// Position: &position.Position{
|
||||
// StartLine: 5,
|
||||
// EndLine: 5,
|
||||
// StartPos: 86,
|
||||
// EndPos: 89,
|
||||
// },
|
||||
// Parts: []node.Node{
|
||||
// &name.NamePart{
|
||||
// Position: &position.Position{
|
||||
// StartLine: 5,
|
||||
// EndLine: 5,
|
||||
// StartPos: 86,
|
||||
// EndPos: 89,
|
||||
// },
|
||||
// Value: "null",
|
||||
// },
|
||||
// },
|
||||
@@ -91,10 +193,34 @@ func ExampleGoDumper() {
|
||||
// },
|
||||
// },
|
||||
// Stmt: &stmt.StmtList{
|
||||
// Position: &position.Position{
|
||||
// StartLine: 6,
|
||||
// EndLine: 9,
|
||||
// StartPos: 96,
|
||||
// EndPos: 133,
|
||||
// },
|
||||
// Stmts: []node.Node{
|
||||
// &stmt.Expression{
|
||||
// Position: &position.Position{
|
||||
// StartLine: 8,
|
||||
// EndLine: 8,
|
||||
// StartPos: 123,
|
||||
// EndPos: 127,
|
||||
// },
|
||||
// Expr: &expr.Variable{
|
||||
// Position: &position.Position{
|
||||
// StartLine: 8,
|
||||
// EndLine: 8,
|
||||
// StartPos: 123,
|
||||
// EndPos: 126,
|
||||
// },
|
||||
// VarName: &node.Identifier{
|
||||
// Position: &position.Position{
|
||||
// StartLine: 8,
|
||||
// EndLine: 8,
|
||||
// StartPos: 123,
|
||||
// EndPos: 126,
|
||||
// },
|
||||
// Value: "var",
|
||||
// },
|
||||
// },
|
||||
|
||||
@@ -15,7 +15,6 @@ import (
|
||||
type JsonDumper struct {
|
||||
Writer io.Writer
|
||||
Comments parser.Comments
|
||||
Positions parser.Positions
|
||||
NsResolver *NamespaceResolver
|
||||
}
|
||||
|
||||
@@ -27,15 +26,14 @@ func (d *JsonDumper) EnterNode(w walker.Walkable) bool {
|
||||
|
||||
fmt.Fprintf(d.Writer, "{%q:%q", "type", nodeType)
|
||||
|
||||
if d.Positions != nil {
|
||||
if p := d.Positions[n]; p != nil {
|
||||
fmt.Fprintf(d.Writer, ",%q:{%q:%d,%q:%d,%q:%d,%q:%d}",
|
||||
"position",
|
||||
"startPos", p.StartPos,
|
||||
"endPos", p.EndPos,
|
||||
"startLine", p.StartLine,
|
||||
"endLine", p.EndLine)
|
||||
}
|
||||
if p := n.GetPosition(); p != nil {
|
||||
p := n.GetPosition()
|
||||
fmt.Fprintf(d.Writer, ",%q:{%q:%d,%q:%d,%q:%d,%q:%d}",
|
||||
"position",
|
||||
"startPos", p.StartPos,
|
||||
"endPos", p.EndPos,
|
||||
"startLine", p.StartLine,
|
||||
"endLine", p.EndLine)
|
||||
}
|
||||
|
||||
if d.NsResolver != nil {
|
||||
|
||||
@@ -33,7 +33,6 @@ func ExampleJsonDumper() {
|
||||
dumper := &visitor.JsonDumper{
|
||||
Writer: os.Stdout,
|
||||
Comments: php7parser.GetComments(),
|
||||
Positions: php7parser.GetPositions(),
|
||||
NsResolver: nsResolver,
|
||||
}
|
||||
nodes.Walk(dumper)
|
||||
|
||||
@@ -15,7 +15,6 @@ import (
|
||||
type PrettyJsonDumper struct {
|
||||
Writer io.Writer
|
||||
Comments parser.Comments
|
||||
Positions parser.Positions
|
||||
NsResolver *NamespaceResolver
|
||||
depth int
|
||||
isChildNode bool
|
||||
@@ -44,24 +43,22 @@ func (d *PrettyJsonDumper) EnterNode(w walker.Walkable) bool {
|
||||
d.printIndent(d.Writer)
|
||||
fmt.Fprintf(d.Writer, "%q: %q", "type", nodeType)
|
||||
|
||||
if d.Positions != nil {
|
||||
if p := d.Positions[n]; p != nil {
|
||||
fmt.Fprint(d.Writer, ",\n")
|
||||
d.printIndent(d.Writer)
|
||||
fmt.Fprintf(d.Writer, "%q: {\n", "position")
|
||||
d.depth++
|
||||
d.printIndent(d.Writer)
|
||||
fmt.Fprintf(d.Writer, "%q: %d,\n", "startPos", p.StartPos)
|
||||
d.printIndent(d.Writer)
|
||||
fmt.Fprintf(d.Writer, "%q: %d,\n", "endPos", p.EndPos)
|
||||
d.printIndent(d.Writer)
|
||||
fmt.Fprintf(d.Writer, "%q: %d,\n", "startLine", p.StartLine)
|
||||
d.printIndent(d.Writer)
|
||||
fmt.Fprintf(d.Writer, "%q: %d\n", "endLine", p.EndLine)
|
||||
d.depth--
|
||||
d.printIndent(d.Writer)
|
||||
fmt.Fprint(d.Writer, "}")
|
||||
}
|
||||
if p := n.GetPosition(); p != nil {
|
||||
fmt.Fprint(d.Writer, ",\n")
|
||||
d.printIndent(d.Writer)
|
||||
fmt.Fprintf(d.Writer, "%q: {\n", "position")
|
||||
d.depth++
|
||||
d.printIndent(d.Writer)
|
||||
fmt.Fprintf(d.Writer, "%q: %d,\n", "startPos", p.StartPos)
|
||||
d.printIndent(d.Writer)
|
||||
fmt.Fprintf(d.Writer, "%q: %d,\n", "endPos", p.EndPos)
|
||||
d.printIndent(d.Writer)
|
||||
fmt.Fprintf(d.Writer, "%q: %d,\n", "startLine", p.StartLine)
|
||||
d.printIndent(d.Writer)
|
||||
fmt.Fprintf(d.Writer, "%q: %d\n", "endLine", p.EndLine)
|
||||
d.depth--
|
||||
d.printIndent(d.Writer)
|
||||
fmt.Fprint(d.Writer, "}")
|
||||
}
|
||||
|
||||
if d.NsResolver != nil {
|
||||
|
||||
@@ -33,7 +33,6 @@ func ExamplePrettyJsonDumper() {
|
||||
dumper := &visitor.PrettyJsonDumper{
|
||||
Writer: os.Stdout,
|
||||
Comments: php7parser.GetComments(),
|
||||
Positions: php7parser.GetPositions(),
|
||||
NsResolver: nsResolver,
|
||||
}
|
||||
nodes.Walk(dumper)
|
||||
|
||||
Reference in New Issue
Block a user