-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
319 lines (282 loc) · 8.23 KB
/
main.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
package main
import (
"flag"
"fmt"
"go/ast"
"go/parser"
"go/token"
"io"
"io/fs"
"log"
"os"
"path/filepath"
"slices"
"strconv"
"github.com/pkg/errors"
"github.com/dlclark/regexp2/syntax"
)
// single regex options - if expr is set then use that, opt, and package
var expr = flag.String("expr", "", "the regexp to convert to Go code to output file")
var opt = flag.Int("opt", 0, "bitwise options to use when compiling the regexp")
var pkg = flag.String("package", "regexp2codegen", "package to use when converting a single regexp")
// if not single regex then scan the path and convert all regex's we find, optionally including test files
var path = flag.String("path", ".", "file path to scan and generate regexp's for")
var tests = flag.Bool("test", false, "true if go tests should be scanned as well")
// universal options
var out = flag.String("o", "", "output file to write generated regexp code into, if the file exists overwrites it. defaults to stdout")
func main() {
flag.Parse()
if expr != nil && len(*expr) > 0 {
options := syntax.RegexOptions(0)
if opt != nil {
options = syntax.RegexOptions(*opt)
}
convertSingle(*expr, options, *pkg)
return
}
convPath, _ := os.Getwd()
if path != nil && len(*path) > 0 {
convPath, _ = filepath.Abs(*path)
}
convertPath(convPath, *tests)
}
func getOutStream() (io.Writer, string) {
outPath := ""
if out == nil || len(*out) == 0 {
return os.Stdout, ""
}
outPath, _ = filepath.Abs(*out)
file, err := os.Create(outPath)
if err != nil {
log.Fatalf("error creating out file: %v", err)
}
return file, outPath
}
func convertSingle(expr string, opts syntax.RegexOptions, pkg string) {
stream, _ := getOutStream()
if stream == nil {
log.Fatalf("unable to open output")
}
c, err := newConverter(stream, pkg)
if err != nil {
log.Fatal(errors.Wrap(err, "code generation error"))
}
if err := c.addRegexp("command line", "MyPattern", expr, opts); err != nil {
log.Fatal(errors.Wrap(err, "code generation error"))
}
if err := c.addFooter(); err != nil {
log.Fatal(errors.Wrap(err, "code generation error"))
}
}
func convertPath(path string, includeTest bool) {
log.Printf("Create regexp for path %s, include tests=%v", path, includeTest)
//Create a FileSet to work with
fset := token.NewFileSet()
pkgs, err := parser.ParseDir(fset, path, func(fi fs.FileInfo) bool {
// if we disallow tests and match the test pattern then skip
if !includeTest {
if ok, _ := filepath.Match("*_test.go", fi.Name()); ok {
return false
}
}
return true
}, parser.ParseComments|parser.SkipObjectResolution)
if err != nil {
log.Fatalf("unable to parse go file: %v", err)
}
var alias string
var c *converter
var stream io.Writer
var outFile string
for p, pkg := range pkgs {
for f, file := range pkg.Files {
if !importsRegexp2(file, &alias) {
// we don't import the regexp engine in this file
// so we won't find anything here
continue
}
if alias == "" {
alias = "regexp2"
}
log.Printf("file %v imports regexp2", f)
ast.Inspect(file, func(n ast.Node) bool {
// Find asignment statements
// a := regexp2.MustCompile("pattern", 0)
// var a = regexp2....
if varDec, ok := n.(*ast.ValueSpec); ok {
// var dec
for i, val := range varDec.Values {
ok, pat, opt, pos := isStaticCompileCall(val, alias)
if ok {
fset.Position(pos).String()
log.Printf("%s: adding pattern %#v options %v", fset.Position(pos), pat, opt)
// first find inits a converter
if c == nil {
stream, outFile = getOutStream()
if stream == nil {
log.Fatalf("unable to open output")
}
c, err = newConverter(stream, p)
if err != nil {
log.Fatal(errors.Wrap(err, "code generation error"))
}
}
if err := c.addRegexp(getLocation(fset, pos, outFile), getName(varDec.Names[i]), pat, syntax.RegexOptions(opt)); err != nil {
log.Fatal(errors.Wrap(err, "code generation error"))
}
}
}
} else if assign, ok := n.(*ast.AssignStmt); ok {
for i, exp := range assign.Rhs {
ok, pat, opt, pos := isStaticCompileCall(exp, alias)
if ok {
log.Printf("%s: adding pattern %#v options %v", fset.Position(pos), pat, opt)
// first find inits a converter
if c == nil {
stream, outFile = getOutStream()
if stream == nil {
log.Fatalf("unable to open output")
}
c, err = newConverter(stream, p)
if err != nil {
log.Fatal(errors.Wrap(err, "code generation error"))
}
}
if err := c.addRegexp(getLocation(fset, pos, outFile), getName(assign.Lhs[i]), pat, syntax.RegexOptions(opt)); err != nil {
log.Fatal(errors.Wrap(err, "code generation error"))
}
}
}
}
return true
})
}
}
if c != nil {
if err := c.addFooter(); err != nil {
log.Fatal(errors.Wrap(err, "code generation error"))
}
}
}
// returns a location in the fileset relative to the output path given
// or pwd if output path is blank
func getLocation(fset *token.FileSet, pos token.Pos, outPath string) string {
fullPos := fset.Position(pos)
//make filename relative to our pwd
if outPath == "" {
outPath, _ = os.Getwd()
} else {
outPath = filepath.Dir(outPath)
}
file, _ := filepath.Rel(outPath, fullPos.Filename)
return fmt.Sprint(file, ":", fullPos.Line, ":", fullPos.Column)
}
func getName(lhs ast.Node) string {
// should be an ident
if ident, ok := lhs.(*ast.Ident); ok {
return ident.Name
}
return ""
}
func isStaticCompileCall(n ast.Node, importAlias string) (ok bool, pattern string, opts int, patternPos token.Pos) {
if funcCall, ok := n.(*ast.CallExpr); ok {
//wrong number of args, can't be us
if len(funcCall.Args) != 2 {
return false, "", 0, 0
}
if ok, _ := isSelector(funcCall.Fun, importAlias, "MustCompile", "Compile"); ok {
// get our pattern, the options, if they're both literals (or known constants)
// then we're good
pat, ok := funcCall.Args[0].(*ast.BasicLit)
if !ok {
return false, "", 0, 0
}
// our pattern
pattern, _ = strconv.Unquote(pat.Value)
// handle options as int constant
opts, ok = getOpts(funcCall.Args[1], importAlias)
if !ok {
return false, "", 0, 0
}
// it parsed!
return true, pattern, opts, funcCall.Args[0].Pos()
}
}
return false, "", 0, 0
}
func getOpts(node ast.Node, importAlias string) (int, bool) {
if op, ok := node.(*ast.BasicLit); ok {
// string version of an int, convert to int
opts, err := strconv.Atoi(op.Value)
if err != nil {
log.Printf("unknown constant for options: %s", op.Value)
return 0, false
}
return opts, true
} else if ok, name := isSelector(node, importAlias, optNames...); ok {
//selector, convert to int
return convertOptsNameToInt(name)
} else if bin, ok := node.(*ast.BinaryExpr); ok {
// binary expression, gonna need to split and then do the operator
// converting to a constant
x, ok := getOpts(bin.X, importAlias)
if !ok {
return 0, false
}
y, ok := getOpts(bin.Y, importAlias)
if !ok {
return 0, false
}
switch bin.Op {
case token.ADD:
return x + y, true
case token.AND:
return x & y, true
case token.OR:
return x | y, true
case token.XOR:
return x ^ y, true
case token.SHL:
return x << y, true
case token.SHR:
return x >> y, true
case token.AND_NOT:
return x &^ y, true
default:
log.Printf("unknown operator for options: %s", bin.Op.String())
}
} else {
log.Printf("unknown ast node type for options: %T %+[1]v", node)
}
return 0, false
}
func convertOptsNameToInt(name string) (int, bool) {
idx := slices.Index(optNames, name)
if idx == -1 {
log.Printf("unknown pattern option: %s", name)
return 0, false
}
return 1 << idx, true
}
func isSelector(node ast.Node, pkg string, name ...string) (bool, string) {
if sel, ok := node.(*ast.SelectorExpr); ok {
if nm, ok := sel.X.(*ast.Ident); ok && nm.Name == pkg {
if slices.Contains(name, sel.Sel.Name) {
return true, sel.Sel.Name
}
}
}
return false, ""
}
func importsRegexp2(file *ast.File, alias *string) bool {
*alias = ""
for _, i := range file.Imports {
if i.Path.Value == "\"github.com/dlclark/regexp2\"" {
if i.Name != nil {
*alias = i.Name.Name
}
return true
}
}
return false
}