/
parse.go
346 lines (318 loc) · 8.49 KB
/
parse.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package macro
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"log"
"reflect"
"regexp"
"strconv"
"strings"
)
// Marco Grammar:
//
// ```
// Macro := '#[' CallSequence ']'
// CallSequence := CallExpr (',' CallExpr)*
// CallExpr := CallWithoutArg | CallWithArgs
// CallWithoutArg := GoIdent
// CallWithArgs := GoIdent '(' Args? ')'
// Args := Arg (',' Arg)*
// Arg := GoIdent | GoBasicLit | True | False | GoSelectorExpr
// GoBasicLit := GoInt | GoFloat | GoString
// ```
//
// The permitted positions to put the macro are:
// - the last comment on top of the struct definition
// - immediately follow the field of struct and/or enum
//
// ```go
// // #[visitor]
// type BinExpr struct {
// lhs Node // #[visitor]
// rhs Node // #[visitor], some other comments
// }
//
// const (
// N_PROG // #[visitor]
// )
// ```
type ProcCtx struct {
Path string // the path to start the entire process
Fset *token.FileSet // files been processed by `go/parser/ParseDir`
Pkgs map[string]*ast.Package // processed results, produced by `go/parser/ParseDir`
}
type MacroCtx struct {
ProcCtx *ProcCtx
File string // file being walked through to find out the macros
Node ast.Node // the node being attached with macro
Name string // the name of this macro
Args []interface{} // the args defined in the macro expr
}
func (c *MacroCtx) HasStringArg(name string) bool {
for _, arg := range c.Args {
if s, ok := arg.(string); ok {
return s == name
}
}
return false
}
type MacroImpl = func(MacroCtx)
func HasMacroLike(cmt string) (string, bool) {
reg := regexp.MustCompile(`^//\s*#\[([^]]*?)\]`)
matched := reg.FindStringSubmatch(cmt)
if len(matched) != 2 {
return "", false
}
return matched[1], true
}
func typ(t interface{}) reflect.Type {
return reflect.TypeOf(t).Elem()
}
func IsTyp(o interface{}, t interface{}) bool {
return reflect.TypeOf(o).Elem() == reflect.TypeOf(t).Elem()
}
func ParseMacro(file, macro string, targe ast.Node, procCtx *ProcCtx) ([]*MacroCtx, error) {
// construct a valid call expr in go, otherwise the returned `expr` will be approximate
expr, err := parser.ParseExpr("_(" + macro + ")")
if err != nil {
return nil, err
}
ctxs := make([]*MacroCtx, 0, 1)
for _, m := range expr.(*ast.CallExpr).Args {
if IsTyp(m, (*ast.Ident)(nil)) {
mc := &MacroCtx{procCtx, file, targe, m.(*ast.Ident).Name, nil}
ctxs = append(ctxs, mc)
} else if IsTyp(m, (*ast.CallExpr)(nil)) {
c := m.(*ast.CallExpr)
name, err := nameOfCallExpr(c)
if err != nil {
return nil, err
}
args, err := argsOfCallExpr(c)
if err != nil {
return nil, err
}
mc := &MacroCtx{procCtx, file, targe, name, args}
ctxs = append(ctxs, mc)
} else {
return nil, fmt.Errorf("deformed macro: %v", m)
}
}
return ctxs, nil
}
func nameOfCallExpr(c *ast.CallExpr) (string, error) {
id := c.Fun
if typ(id) != typ((*ast.Ident)(nil)) {
return "", fmt.Errorf("deformed macro name: %v", id)
}
return id.(*ast.Ident).Name, nil
}
func argsOfCallExpr(c *ast.CallExpr) ([]interface{}, error) {
args := make([]interface{}, 0, 1)
for _, arg := range c.Args {
a, err := parseArg(arg)
if err != nil {
return nil, err
}
args = append(args, a)
}
return args, nil
}
func parseArg(node ast.Node) (interface{}, error) {
switch v := node.(type) {
case *ast.Ident:
if v.Name == "true" {
return true, nil
}
if v.Name == "false" {
return false, nil
}
return v.Name, nil
case *ast.BasicLit:
if v.Kind == token.INT || v.Kind == token.FLOAT {
return v.Value, nil
}
if v.Kind == token.STRING {
s, err := strconv.Unquote(v.Value)
if err != nil {
return nil, err
}
return s, nil
}
case *ast.SelectorExpr:
return nameOfSelectorExpr(v)
}
return nil, fmt.Errorf("deformed macro arg: %v", node)
}
func nameOfSelectorExpr(s *ast.SelectorExpr) (string, error) {
ns := make([]string, 0, 1)
for {
ns = append([]string{s.Sel.Name}, ns...)
if IsTyp(s.X, (*ast.SelectorExpr)(nil)) {
s = s.X.(*ast.SelectorExpr)
} else if IsTyp(s.X, (*ast.Ident)(nil)) {
ns = append([]string{s.X.(*ast.Ident).Name}, ns...)
break
} else {
return "", fmt.Errorf("deformed macro arg in SelectorExpr.X: %v", s.X)
}
}
return strings.Join(ns, "."), nil
}
func IsStructDec(n ast.Node) (string, *ast.StructType, bool) {
if !IsTyp(n, (*ast.GenDecl)(nil)) || n.(*ast.GenDecl).Tok != token.TYPE {
return "", nil, false
}
d := n.(*ast.GenDecl)
if len(d.Specs) != 1 || !IsTyp(d.Specs[0], (*ast.TypeSpec)(nil)) {
return "", nil, false
}
t := d.Specs[0].(*ast.TypeSpec).Type
if !IsTyp(t, (*ast.StructType)(nil)) {
return "", nil, false
}
ts := d.Specs[0].(*ast.TypeSpec)
return ts.Name.Name, ts.Type.(*ast.StructType), true
}
func IsEnum(n ast.Node) ([]ast.Spec, bool) {
if !IsTyp(n, (*ast.GenDecl)(nil)) || n.(*ast.GenDecl).Tok != token.CONST {
return nil, false
}
return n.(*ast.GenDecl).Specs, true
}
func IsFnDec(n ast.Node) (*ast.FuncDecl, bool) {
if !IsTyp(n, (*ast.FuncDecl)(nil)) {
return nil, false
}
return n.(*ast.FuncDecl), true
}
func RecvName(fn *ast.FuncDecl) string {
if fn.Recv == nil {
return ""
}
if v, ok := fn.Recv.List[0].Type.(*ast.StarExpr); ok {
return v.X.(*ast.Ident).Name
}
return ""
}
func macroCtxsOfEnum(file *ast.File, filename string, specs []ast.Spec, procCtx *ProcCtx) ([]*MacroCtx, error) {
ctxs := make([]*MacroCtx, 0, 1)
for _, spec := range specs {
if v, ok := spec.(*ast.ValueSpec); ok {
if v.Comment == nil {
continue
}
cmt := v.Comment.List[0].Text
if c, ok := HasMacroLike(cmt); ok {
m, err := ParseMacro(filename, c, spec, procCtx)
if err != nil {
return nil, err
}
ctxs = append(ctxs, m...)
}
}
}
return ctxs, nil
}
func macroCtxsInsideStruct(filename string, s *ast.StructType, procCtx *ProcCtx) ([]*MacroCtx, error) {
ctxs := make([]*MacroCtx, 0, 1)
for _, f := range s.Fields.List {
if f.Comment == nil {
continue
}
cmt := f.Comment.List[0].Text
if c, ok := HasMacroLike(cmt); ok {
m, err := ParseMacro(filename, c, f, procCtx)
if err != nil {
return nil, err
}
ctxs = append(ctxs, m...)
}
}
return ctxs, nil
}
// the comments on the top of the struct definition are attached to the GenDecl which is the parent Node of the
// struct definition, so the comment needs to be passed by the callee
func macroCtxsOfDec(filename string, comment string, s ast.Node, procCtx *ProcCtx) ([]*MacroCtx, error) {
cmt, ok := HasMacroLike(comment)
if !ok {
return nil, nil
}
return ParseMacro(filename, cmt, s, procCtx)
}
func MacroCtxsOfFile(file *ast.File, filename string, procCtx *ProcCtx) ([]*MacroCtx, error) {
ctxs := make([]*MacroCtx, 0, 1)
for _, dec := range file.Decls {
if n, ok := IsEnum(dec); ok {
cs, err := macroCtxsOfEnum(file, filename, n, procCtx)
if err != nil {
return nil, err
}
ctxs = append(ctxs, cs...)
} else if _, n, ok := IsStructDec(dec); ok {
// process the struct itself
if dec.(*ast.GenDecl).Doc != nil {
cmts := dec.(*ast.GenDecl).Doc.List
cmt := cmts[len(cmts)-1].Text
cs, err := macroCtxsOfDec(filename, cmt, dec, procCtx)
if err != nil {
return nil, err
}
ctxs = append(ctxs, cs...)
}
// process the fields of the struct
cs, err := macroCtxsInsideStruct(filename, n, procCtx)
if err != nil {
return nil, err
}
ctxs = append(ctxs, cs...)
} else if v, ok := IsFnDec(dec); ok {
if dec.(*ast.FuncDecl).Doc != nil {
cmts := v.Doc.List
cmt := cmts[len(cmts)-1].Text
cs, err := macroCtxsOfDec(filename, cmt, dec, procCtx)
if err != nil {
return nil, err
}
ctxs = append(ctxs, cs...)
}
}
}
return ctxs, nil
}
type WalkPkgHandle = func(*ast.File, string, *ProcCtx) error
func WalkPkgs(pkgs map[string]*ast.Package, handle WalkPkgHandle, pc *ProcCtx) error {
for _, pkg := range pkgs {
for filename, file := range pkg.Files {
err := handle(file, filename, pc)
if err != nil {
return err
}
}
}
return nil
}
func MacroCtxsOfWorkingDir(wd string, defDir string) ([]*MacroCtx, *ProcCtx, error) {
fset := token.NewFileSet()
pkgs, err := parser.ParseDir(fset, defDir, nil, parser.ParseComments)
if err != nil {
log.Fatal(err)
}
procCtx := &ProcCtx{Path: wd, Fset: fset, Pkgs: pkgs}
ctxs := make([]*MacroCtx, 0, 1)
err = WalkPkgs(pkgs, func(f *ast.File, s string, pc *ProcCtx) error {
var cs []*MacroCtx
cs, err := MacroCtxsOfFile(f, s, procCtx)
if err != nil {
return err
}
ctxs = append(ctxs, cs...)
return nil
}, procCtx)
if err != nil {
return nil, nil, err
}
return ctxs, procCtx, nil
}