forked from quasilyte/gogrep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_operations.go
366 lines (307 loc) · 14.2 KB
/
gen_operations.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
//go:build main
// +build main
package main
import (
"bytes"
"fmt"
"go/format"
"io/ioutil"
"log"
"strings"
"text/template"
)
var opPrototypes = []operationProto{
{name: "Node", tag: "Node"},
{name: "NamedNode", tag: "Node", valueIndex: "strings | wildcard name"},
{name: "NodeSeq"},
{name: "NamedNodeSeq", valueIndex: "strings | wildcard name"},
{name: "OptNode"},
{name: "NamedOptNode", valueIndex: "strings | wildcard name"},
{name: "FieldNode", tag: "Node"},
{name: "NamedFieldNode", tag: "Node", valueIndex: "strings | wildcard name"},
{name: "MultiStmt", tag: "StmtList", args: "stmts...", example: "f(); g()"},
{name: "MultiExpr", tag: "ExprList", args: "exprs...", example: "f(), g()"},
{name: "MultiDecl", tag: "DeclList", args: "exprs...", example: "f(), g()"},
{name: "End"},
{name: "BasicLit", tag: "BasicLit", valueIndex: "ifaces | parsed literal value"},
{name: "StrictIntLit", tag: "BasicLit", valueIndex: "strings | raw literal value"},
{name: "StrictFloatLit", tag: "BasicLit", valueIndex: "strings | raw literal value"},
{name: "StrictCharLit", tag: "BasicLit", valueIndex: "strings | raw literal value"},
{name: "StrictStringLit", tag: "BasicLit", valueIndex: "strings | raw literal value"},
{name: "StrictComplexLit", tag: "BasicLit", valueIndex: "strings | raw literal value"},
{name: "Ident", tag: "Ident", valueIndex: "strings | ident name"},
{name: "Pkg", tag: "Ident", valueIndex: "strings | package path"},
{name: "IndexExpr", tag: "IndexExpr", args: "x expr"},
{name: "IndexListExpr", tag: "IndexListExpr", args: "x exprs..."},
{name: "SliceExpr", tag: "SliceExpr", args: "x"},
{name: "SliceFromExpr", tag: "SliceExpr", args: "x from", example: "x[from:]"},
{name: "SliceToExpr", tag: "SliceExpr", args: "x to", example: "x[:to]"},
{name: "SliceFromToExpr", tag: "SliceExpr", args: "x from to", example: "x[from:to]"},
{name: "SliceToCapExpr", tag: "SliceExpr", args: "x from cap", example: "x[:from:cap]"},
{name: "SliceFromToCapExpr", tag: "SliceExpr", args: "x from to cap", example: "x[from:to:cap]"},
{name: "FuncLit", tag: "FuncLit", args: "type block"},
{name: "CompositeLit", tag: "CompositeLit", args: "elts...", example: "{elts...}"},
{name: "TypedCompositeLit", tag: "CompositeLit", args: "typ elts...", example: "typ{elts...}"},
{name: "SimpleSelectorExpr", tag: "SelectorExpr", args: "x", valueIndex: "strings | selector name"},
{name: "SelectorExpr", tag: "SelectorExpr", args: "x sel"},
{name: "TypeAssertExpr", tag: "TypeAssertExpr", args: "x typ"},
{name: "TypeSwitchAssertExpr", tag: "TypeAssertExpr", args: "x"},
{name: "StructType", tag: "StructType", args: "fields"},
{name: "InterfaceType", tag: "InterfaceType", args: "fields"},
{name: "EfaceType", tag: "InterfaceType"},
{name: "VoidFuncType", tag: "FuncType", args: "params"},
{name: "GenericVoidFuncType", tag: "FuncType", args: "typeparams params"},
{name: "FuncType", tag: "FuncType", args: "params results"},
{name: "GenericFuncType", tag: "FuncType", args: "typeparams params results"},
{name: "ArrayType", tag: "ArrayType", args: "length elem"},
{name: "SliceType", tag: "ArrayType", args: "elem"},
{name: "MapType", tag: "MapType", args: "key value"},
{name: "ChanType", tag: "ChanType", args: "value", value: "ast.ChanDir | channel direction"},
{name: "KeyValueExpr", tag: "KeyValueExpr", args: "key value"},
{name: "Ellipsis", tag: "Ellipsis"},
{name: "TypedEllipsis", tag: "Ellipsis", args: "type"},
{name: "StarExpr", tag: "StarExpr", args: "x"},
{name: "UnaryExpr", tag: "UnaryExpr", args: "x", value: "token.Token | unary operator"},
{name: "BinaryExpr", tag: "BinaryExpr", args: "x y", value: "token.Token | binary operator"},
{name: "ParenExpr", tag: "ParenExpr", args: "x"},
{
name: "ArgList",
args: "exprs...",
example: "1, 2, 3",
},
{
name: "SimpleArgList",
note: "Like ArgList, but pattern contains no $*",
args: "exprs[]",
value: "int | slice len",
example: "1, 2, 3",
},
{name: "VariadicCallExpr", tag: "CallExpr", args: "fn args", example: "f(1, xs...)"},
{name: "NonVariadicCallExpr", tag: "CallExpr", args: "fn args", example: "f(1, xs)"},
{name: "MaybeVariadicCallExpr", tag: "CallExpr", args: "fn args", value: "int | can be variadic if len(args)>value", example: "f(1, xs) or f(1, xs...)"},
{name: "CallExpr", tag: "CallExpr", args: "fn args", example: "f(1, xs) or f(1, xs...)"},
{name: "AssignStmt", tag: "AssignStmt", args: "lhs rhs", value: "token.Token | ':=' or '='", example: "lhs := rhs()"},
{name: "MultiAssignStmt", tag: "AssignStmt", args: "lhs... rhs...", value: "token.Token | ':=' or '='", example: "lhs1, lhs2 := rhs()"},
{name: "BranchStmt", tag: "BranchStmt", args: "x", value: "token.Token | branch kind"},
{name: "SimpleLabeledBranchStmt", tag: "BranchStmt", args: "x", valueIndex: "strings | label name", value: "token.Token | branch kind"},
{name: "LabeledBranchStmt", tag: "BranchStmt", args: "label x", value: "token.Token | branch kind"},
{name: "SimpleLabeledStmt", tag: "LabeledStmt", args: "x", valueIndex: "strings | label name"},
{name: "LabeledStmt", tag: "LabeledStmt", args: "label x"},
{name: "BlockStmt", tag: "BlockStmt", args: "body..."},
{name: "ExprStmt", tag: "ExprStmt", args: "x"},
{name: "GoStmt", tag: "GoStmt", args: "x"},
{name: "DeferStmt", tag: "DeferStmt", args: "x"},
{name: "SendStmt", tag: "SendStmt", args: "ch value"},
{name: "EmptyStmt", tag: "EmptyStmt"},
{name: "IncDecStmt", tag: "IncDecStmt", args: "x", value: "token.Token | '++' or '--'"},
{name: "ReturnStmt", tag: "ReturnStmt", args: "results..."},
{name: "IfStmt", tag: "IfStmt", args: "cond block", example: "if cond {}"},
{name: "IfInitStmt", tag: "IfStmt", args: "init cond block", example: "if init; cond {}"},
{name: "IfElseStmt", tag: "IfStmt", args: "cond block else", example: "if cond {} else ..."},
{name: "IfInitElseStmt", tag: "IfStmt", args: "init cond block else", example: "if init; cond {} else ..."},
{name: "IfNamedOptStmt", tag: "IfStmt", args: "block", valueIndex: "strings | wildcard name", example: "if $*x {}"},
{name: "IfNamedOptElseStmt", tag: "IfStmt", args: "block else", valueIndex: "strings | wildcard name", example: "if $*x {} else ..."},
{name: "SwitchStmt", tag: "SwitchStmt", args: "body...", example: "switch {}"},
{name: "SwitchTagStmt", tag: "SwitchStmt", args: "tag body...", example: "switch tag {}"},
{name: "SwitchInitStmt", tag: "SwitchStmt", args: "init body...", example: "switch init; {}"},
{name: "SwitchInitTagStmt", tag: "SwitchStmt", args: "init tag body...", example: "switch init; tag {}"},
{name: "SelectStmt", tag: "SelectStmt", args: "body..."},
{name: "TypeSwitchStmt", tag: "TypeSwitchStmt", args: "x block", example: "switch x.(type) {}"},
{name: "TypeSwitchInitStmt", tag: "TypeSwitchStmt", args: "init x block", example: "switch init; x.(type) {}"},
{name: "CaseClause", tag: "CaseClause", args: "values... body..."},
{name: "DefaultCaseClause", tag: "CaseClause", args: "body..."},
{name: "CommClause", tag: "CommClause", args: "comm body..."},
{name: "DefaultCommClause", tag: "CommClause", args: "body..."},
{name: "ForStmt", tag: "ForStmt", args: "blocl", example: "for {}"},
{name: "ForPostStmt", tag: "ForStmt", args: "post block", example: "for ; ; post {}"},
{name: "ForCondStmt", tag: "ForStmt", args: "cond block", example: "for ; cond; {}"},
{name: "ForCondPostStmt", tag: "ForStmt", args: "cond post block", example: "for ; cond; post {}"},
{name: "ForInitStmt", tag: "ForStmt", args: "init block", example: "for init; ; {}"},
{name: "ForInitPostStmt", tag: "ForStmt", args: "init post block", example: "for init; ; post {}"},
{name: "ForInitCondStmt", tag: "ForStmt", args: "init cond block", example: "for init; cond; {}"},
{name: "ForInitCondPostStmt", tag: "ForStmt", args: "init cond post block", example: "for init; cond; post {}"},
{name: "RangeStmt", tag: "RangeStmt", args: "x block", example: "for range x {}"},
{name: "RangeKeyStmt", tag: "RangeStmt", args: "key x block", value: "token.Token | ':=' or '='", example: "for key := range x {}"},
{name: "RangeKeyValueStmt", tag: "RangeStmt", args: "key value x block", value: "token.Token | ':=' or '='", example: "for key, value := range x {}"},
{name: "RangeClause", tag: "RangeStmt", args: "x", example: "range x"},
{name: "RangeHeader", tag: "RangeStmt", args: "x", example: "for range x"},
{name: "RangeKeyHeader", tag: "RangeStmt", args: "key x", value: "token.Token | ':=' or '='", example: "for key := range x"},
{name: "RangeKeyValueHeader", tag: "RangeStmt", args: "key value x", value: "token.Token | ':=' or '='", example: "for key, value := range x"},
{name: "FieldList", args: "fields..."},
{name: "UnnamedField", args: "typ", example: "type"},
{name: "SimpleField", args: "typ", valueIndex: "strings | field name", example: "name type"},
{name: "Field", args: "name typ", example: "$name type"},
{name: "MultiField", args: "names... typ", example: "name1, name2 type"},
{name: "ValueSpec", tag: "ValueSpec", args: "value"},
{name: "ValueInitSpec", tag: "ValueSpec", args: "lhs... rhs...", example: "lhs = rhs"},
{name: "TypedValueInitSpec", tag: "ValueSpec", args: "lhs... type rhs...", example: "lhs typ = rhs"},
{name: "TypedValueSpec", tag: "ValueSpec", args: "lhs... type", example: "lhs typ"},
{name: "SimpleTypeSpec", tag: "TypeSpec", args: "type", valueIndex: "strings | type name", example: "name type"},
{name: "TypeSpec", tag: "TypeSpec", args: "name type", example: "name type"},
{name: "GenericTypeSpec", tag: "TypeSpec", args: "name typeparasm type", example: "name[typeparams] type"},
{name: "TypeAliasSpec", tag: "TypeSpec", args: "name type", example: "name = type"},
{name: "SimpleFuncDecl", tag: "FuncDecl", args: "type block", valueIndex: "strings | field name"},
{name: "FuncDecl", tag: "FuncDecl", args: "name type block"},
{name: "MethodDecl", tag: "FuncDecl", args: "recv name type block"},
{name: "FuncProtoDecl", tag: "FuncDecl", args: "name type"},
{name: "MethodProtoDecl", tag: "FuncDecl", args: "recv name type"},
{name: "DeclStmt", tag: "DeclStmt", args: "decl"},
{name: "ConstDecl", tag: "GenDecl", args: "valuespecs..."},
{name: "VarDecl", tag: "GenDecl", args: "valuespecs..."},
{name: "TypeDecl", tag: "GenDecl", args: "typespecs..."},
{name: "AnyImportDecl", tag: "GenDecl"},
{name: "ImportDecl", tag: "GenDecl", args: "importspecs..."},
{name: "EmptyPackage", tag: "File", args: "name"},
}
type operationProto struct {
name string
value string
valueIndex string
tag string
example string
args string
note string
}
type operationInfo struct {
Example string
Note string
Args string
Enum uint8
TagName string
Name string
ValueDoc string
ValueIndexDoc string
ExtraValueKindName string
ValueKindName string
VariadicMap uint64
NumArgs int
SliceIndex int
}
const stackUnchanged = ""
var fileTemplate = template.Must(template.New("operations.go").Parse(`// Code generated "gen_operations.go"; DO NOT EDIT.
package gogrep
import (
"github.com/quasilyte/gogrep/nodetag"
)
//go:generate stringer -type=operation -trimprefix=op
type operation uint8
const (
opInvalid operation = 0
{{ range .Operations }}
// Tag: {{.TagName}}
{{- if .Note}}{{print "\n"}}// {{.Note}}{{end}}
{{- if .Args}}{{print "\n"}}// Args: {{.Args}}{{end}}
{{- if .Example}}{{print "\n"}}// Example: {{.Example}}{{end}}
{{- if .ValueDoc}}{{print "\n"}}// Value: {{.ValueDoc}}{{end}}
{{- if .ValueIndexDoc}}{{print "\n"}}// ValueIndex: {{.ValueIndexDoc}}{{end}}
op{{ .Name }} operation = {{.Enum}}
{{ end -}}
)
type operationInfo struct {
Tag nodetag.Value
NumArgs int
ValueKind valueKind
ExtraValueKind valueKind
VariadicMap bitmap64
SliceIndex int
}
var operationInfoTable = [256]operationInfo{
opInvalid: {},
{{ range .Operations -}}
op{{.Name}}: {
Tag: nodetag.{{.TagName}},
NumArgs: {{.NumArgs}},
ValueKind: {{.ValueKindName}},
ExtraValueKind: {{.ExtraValueKindName}},
VariadicMap: {{.VariadicMap}}, // {{printf "%b" .VariadicMap}}
SliceIndex: {{.SliceIndex}},
},
{{ end }}
}
`))
func main() {
operations := make([]operationInfo, len(opPrototypes))
for i, proto := range opPrototypes {
enum := uint8(i + 1)
tagName := proto.tag
if tagName == "" {
tagName = "Unknown"
}
variadicMap := uint64(0)
numArgs := 0
sliceLenIndex := -1
if proto.args != "" {
args := strings.Split(proto.args, " ")
numArgs = len(args)
for i, arg := range args {
isVariadic := strings.HasSuffix(arg, "...")
if isVariadic {
variadicMap |= 1 << i
}
if strings.HasSuffix(arg, "[]") {
sliceLenIndex = i
}
}
}
extraValueKindName := "emptyValue"
if proto.valueIndex != "" {
parts := strings.Split(proto.valueIndex, " | ")
typ := parts[0]
switch typ {
case "strings":
extraValueKindName = "stringValue"
case "ifaces":
extraValueKindName = "ifaceValue"
default:
panic(fmt.Sprintf("%s: unexpected %s type", proto.name, typ))
}
}
valueKindName := "emptyValue"
if proto.value != "" {
parts := strings.Split(proto.value, " | ")
typ := parts[0]
switch typ {
case "token.Token":
valueKindName = "tokenValue"
case "ast.ChanDir":
valueKindName = "chandirValue"
case "int":
valueKindName = "intValue"
default:
panic(fmt.Sprintf("%s: unexpected %s type", proto.name, typ))
}
}
operations[i] = operationInfo{
Example: proto.example,
Note: proto.note,
Args: proto.args,
Enum: enum,
TagName: tagName,
Name: proto.name,
ValueDoc: proto.value,
ValueIndexDoc: proto.valueIndex,
NumArgs: numArgs,
VariadicMap: variadicMap,
ExtraValueKindName: extraValueKindName,
ValueKindName: valueKindName,
SliceIndex: sliceLenIndex,
}
}
var buf bytes.Buffer
err := fileTemplate.Execute(&buf, map[string]interface{}{
"Operations": operations,
})
if err != nil {
log.Panicf("execute template: %v", err)
}
writeFile("operations.gen.go", buf.Bytes())
}
func writeFile(filename string, data []byte) {
pretty, err := format.Source(data)
if err != nil {
log.Panicf("gofmt: %v", err)
}
if err := ioutil.WriteFile(filename, pretty, 0666); err != nil {
log.Panicf("write %s: %v", filename, err)
}
}