/
dst.go
126 lines (118 loc) · 3.06 KB
/
dst.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"bytes"
"go/ast"
"go/format"
"go/parser"
"path/filepath"
"regexp"
"strings"
"github.com/dave/dst/gendst/data"
. "github.com/dave/jennifer/jen"
"golang.org/x/tools/go/loader"
)
func generateDst(names []string) error {
f := NewFile("dst")
for _, name := range names {
if name == "Package" {
f.Comment("Decorations is nil for Package nodes.")
} else {
f.Comment("Decorations returns the decorations that are common to all nodes (Before, Start, End, After).")
}
f.Func().Params(Id("n").Op("*").Id(name)).Id("Decorations").Params().Op("*").Id("NodeDecs").BlockFunc(func(g *Group) {
if name == "Package" {
g.Return(Nil())
} else {
g.Return(Op("&").Id("n").Dot("Decs").Dot("NodeDecs"))
}
})
}
return f.Save("./decorations-node-generated.go")
}
func generateDstDecs(names []string) error {
path := "github.com/dave/dst/gendst/data"
conf := loader.Config{ParserMode: parser.ParseComments}
conf.Import(path)
prog, err := conf.Load()
if err != nil {
panic(err)
}
var astFile *ast.File
for _, v := range prog.Package(path).Files {
_, name := filepath.Split(prog.Fset.File(v.Pos()).Name())
if name == "positions.go" {
astFile = v
break
}
}
buf := &bytes.Buffer{}
if err := format.Node(buf, prog.Fset, astFile); err != nil {
panic(err)
}
source := buf.String()
reg := regexp.MustCompile(`// ([a-zA-Z]+)`)
type part struct {
name string
start, end int
}
var parts []part
for _, cg := range astFile.Comments {
for _, c := range cg.List {
if strings.HasPrefix(c.Text, "// --") {
if len(parts) > 0 && parts[len(parts)-1].end == -1 {
parts[len(parts)-1].end = int(c.Pos() - 1)
}
continue
}
if matches := reg.FindStringSubmatch(c.Text); matches != nil {
name := matches[1]
pos := c.End()
prev := c.Pos() - 1
if len(parts) > 0 && parts[len(parts)-1].end == -1 {
parts[len(parts)-1].end = int(prev)
}
parts = append(parts, part{name, int(pos), -1})
}
}
}
if len(parts) > 0 && parts[len(parts)-1].end == -1 {
parts[len(parts)-1].end = int(astFile.End())
}
f := NewFile("dst")
for _, name := range names {
// type <name>Decorations struct {
// //...
// }
f.Line()
f.Commentf("%sDecorations holds decorations for %s:", name, name)
f.Comment("")
for _, part := range parts {
if part.name != name {
continue
}
text := source[part.start:part.end]
indented := text[0] == '\t'
text = strings.TrimSpace(text)
var indent string
if !indented || name == "LabeledStmt" { // LabeledStmt special case because comment is in wrong position
indent = "\t"
}
text = "// \t" + strings.Replace(text, "\n", "\n// "+indent, -1)
f.Comment(text)
f.Comment("")
}
f.Type().Id(name + "Decorations").StructFunc(func(g *Group) {
g.Id("NodeDecs")
for _, frag := range data.Info[name] {
switch frag := frag.(type) {
case data.Decoration:
if frag.Name == "Start" || frag.Name == "End" {
continue
}
g.Id(frag.Name).Id("Decorations")
}
}
})
}
return f.Save("./decorations-types-generated.go")
}