#25: save comments within node

This commit is contained in:
z7zmey
2018-06-25 15:38:31 +03:00
parent 1ebb0c6fad
commit 3cd45ecac5
183 changed files with 16743 additions and 14671 deletions

View File

@@ -39,12 +39,10 @@ func (d *Dumper) EnterNode(w walker.Walkable) bool {
}
}
if d.Comments != nil {
if c := d.Comments[n]; len(c) > 0 {
fmt.Fprintf(d.Writer, "%v\"Comments\":\n", d.Indent+" ")
for _, cc := range c {
fmt.Fprintf(d.Writer, "%v%q before %q\n", d.Indent+" ", cc, comment.TokenNames[cc.TokenName()])
}
if c := n.GetComments(); len(c) > 0 {
fmt.Fprintf(d.Writer, "%v\"Comments\":\n", d.Indent+" ")
for _, cc := range c {
fmt.Fprintf(d.Writer, "%v%q before %q\n", d.Indent+" ", cc, comment.TokenNames[cc.TokenName])
}
}

View File

@@ -59,6 +59,44 @@ func (d *GoDumper) EnterNode(w walker.Walkable) bool {
fmt.Fprint(d.Writer, "},\n")
}
if cc := n.GetComments(); len(cc) > 0 {
printIndent(d.Writer, d.depth)
fmt.Fprint(d.Writer, "Comments: []*comment.Comment{\n")
d.depth++
for _, c := range cc {
printIndent(d.Writer, d.depth)
fmt.Fprint(d.Writer, "&comment.Comment{\n")
d.depth++
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", c.Position.StartLine)
printIndent(d.Writer, d.depth)
fmt.Fprintf(d.Writer, "EndLine: %d,\n", c.Position.EndLine)
printIndent(d.Writer, d.depth)
fmt.Fprintf(d.Writer, "StartPos: %d,\n", c.Position.StartPos)
printIndent(d.Writer, d.depth)
fmt.Fprintf(d.Writer, "EndPos: %d,\n", c.Position.EndPos)
d.depth--
printIndent(d.Writer, d.depth)
fmt.Fprint(d.Writer, "},\n")
printIndent(d.Writer, d.depth)
fmt.Fprintf(d.Writer, "Value: %q,\n", c.Value)
printIndent(d.Writer, d.depth)
fmt.Fprintf(d.Writer, "TokenName: %q,\n", c.TokenName)
d.depth--
printIndent(d.Writer, d.depth)
fmt.Fprint(d.Writer, "},\n")
}
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)

View File

@@ -214,6 +214,18 @@ func ExampleGoDumper() {
// StartPos: 123,
// EndPos: 126,
// },
// Comments: []*comment.Comment{
// &comment.Comment{
// Position: &position.Position{
// StartLine: 7,
// EndLine: 7,
// StartPos: 103,
// EndPos: 117,
// },
// Value: "//some comment\n",
// TokenName: '\t',
// },
// },
// VarName: &node.Identifier{
// Position: &position.Position{
// StartLine: 8,

View File

@@ -42,20 +42,18 @@ func (d *JsonDumper) EnterNode(w walker.Walkable) bool {
}
}
if d.Comments != nil {
if c := d.Comments[n]; len(c) > 0 {
fmt.Fprintf(d.Writer, ",%q:[", "comments")
if c := n.GetComments(); len(c) > 0 {
fmt.Fprintf(d.Writer, ",%q:[", "comments")
for k, cc := range c {
if k == 0 {
fmt.Fprintf(d.Writer, "%q", cc)
} else {
fmt.Fprintf(d.Writer, ",%q", cc)
}
for k, cc := range c {
if k == 0 {
fmt.Fprintf(d.Writer, "%q", cc)
} else {
fmt.Fprintf(d.Writer, ",%q", cc)
}
fmt.Fprint(d.Writer, "]")
}
fmt.Fprint(d.Writer, "]")
}
if a := n.Attributes(); len(a) > 0 {

View File

@@ -69,27 +69,25 @@ func (d *PrettyJsonDumper) EnterNode(w walker.Walkable) bool {
}
}
if d.Comments != nil {
if c := d.Comments[n]; len(c) > 0 {
fmt.Fprint(d.Writer, ",\n")
d.printIndent(d.Writer)
fmt.Fprint(d.Writer, "\"comments\": [\n")
d.depth++
for k, cc := range c {
if k == 0 {
d.printIndent(d.Writer)
fmt.Fprintf(d.Writer, "%q", cc)
} else {
fmt.Fprint(d.Writer, ",\n")
d.printIndent(d.Writer)
fmt.Fprintf(d.Writer, "%q", cc)
}
if c := n.GetComments(); len(c) > 0 {
fmt.Fprint(d.Writer, ",\n")
d.printIndent(d.Writer)
fmt.Fprint(d.Writer, "\"comments\": [\n")
d.depth++
for k, cc := range c {
if k == 0 {
d.printIndent(d.Writer)
fmt.Fprintf(d.Writer, "%q", cc)
} else {
fmt.Fprint(d.Writer, ",\n")
d.printIndent(d.Writer)
fmt.Fprintf(d.Writer, "%q", cc)
}
d.depth--
fmt.Fprint(d.Writer, "\n")
d.printIndent(d.Writer)
fmt.Fprint(d.Writer, "]")
}
d.depth--
fmt.Fprint(d.Writer, "\n")
d.printIndent(d.Writer)
fmt.Fprint(d.Writer, "]")
}
if a := n.Attributes(); len(a) > 0 {