-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgogenerate.go
562 lines (530 loc) · 13.2 KB
/
gogenerate.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
package yarex
import (
"fmt"
"io"
"regexp"
"strings"
)
var reNotWord = regexp.MustCompile(`\W`)
type charClassResult struct {
id string
code *codeFragments
}
type GoGenerator struct {
pkgname string
useUtf8 bool
stateCount uint
idPrefix string
idCount uint
repeatCount uint
funcs map[string]*codeFragments
charClasses map[string]charClassResult
useCharClass bool
useSmallLoop bool
}
func NewGoGenerator(file string, pkg string) *GoGenerator {
gg := &GoGenerator{}
gg.pkgname = pkg
gg.idPrefix = fmt.Sprintf("yarexGen_%s", reNotWord.ReplaceAllString(file, "_"))
gg.funcs = map[string]*codeFragments{}
gg.charClasses = map[string]charClassResult{}
return gg
}
func (gg *GoGenerator) Add(rs ...string) error {
for _, r := range rs {
if _, ok := gg.funcs[r]; ok {
continue
}
ast, err := parse(r)
if err != nil {
return err
}
ast = optimizeAst(ast)
code := gg.generateFunc(r, ast)
gg.funcs[r] = code
}
return nil
}
func (gg *GoGenerator) WriteTo(w io.Writer) (int64, error) {
var acc int64
importUtf8 := ""
if gg.useUtf8 {
importUtf8 = `"unicode/utf8"`
}
n, err := fmt.Fprintf(w, `package %s
import (
"strconv"
"unsafe"
%s
"github.com/Maki-Daisuke/go-yarex"
)
`, gg.pkgname, importUtf8)
acc += int64(n)
if err != nil {
return acc, err
}
for _, cr := range gg.charClasses {
n, err := fmt.Fprintf(w, "var %s = ", cr.id)
acc += int64(n)
if err != nil {
return acc, err
}
m, err := cr.code.WriteTo(w)
acc += m
if err != nil {
return acc, err
}
n, err = fmt.Fprintf(w, "\n")
acc += int64(n)
if err != nil {
return acc, err
}
}
for _, f := range gg.funcs {
n, err := f.WriteTo(w)
acc += n
if err != nil {
return acc, err
}
}
return acc, nil
}
func (gg *GoGenerator) newState() uint {
gg.stateCount++
return gg.stateCount
}
func (gg *GoGenerator) newId() string {
gg.idCount++
return fmt.Sprintf("%s%d", gg.idPrefix, gg.idCount)
}
func (gg *GoGenerator) newRepeatID() uint {
gg.repeatCount++
return gg.repeatCount
}
func (gg *GoGenerator) generateFunc(re string, ast Ast) *codeFragments {
funcID := gg.newId()
gg.stateCount = 0
gg.useCharClass = false
gg.useSmallLoop = false
follower := gg.generateAst(funcID, ast, &codeFragments{0, fmt.Sprintf(`
onSuccess(ctx.Push(yarex.ContextKey{'c', 0}, p))
return true
default:
// This should not happen.
panic("state" + strconv.Itoa(state) + "is not defined")
}
}
}
var _ = yarex.RegisterCompiledRegexp(%q, %t, %d, %s)
`, re, canOnlyMatchAtBegining(ast), minRequiredLengthOfAst(ast), funcID), nil})
varDecl := ""
if gg.useCharClass {
varDecl = `
var (
r rune
size int
)
`
}
if gg.useSmallLoop {
varDecl += `
var (
localStack [16]int
heapStack *[]int
endPos int
n int
)
`
}
return follower.prepend(fmt.Sprintf(`
func %s (state int, ctx yarex.MatchContext, p int, onSuccess func(yarex.MatchContext)) bool {
%s
str := *(*string)(unsafe.Pointer(ctx.Str))
for{
switch state {
case 0:
`, funcID, varDecl))
}
func (gg *GoGenerator) generateAst(funcID string, re Ast, follower *codeFragments) *codeFragments {
switch r := re.(type) {
case AstLit:
return gg.generateLit(string(r), follower)
case AstNotNewline:
gg.useUtf8 = true
return &codeFragments{follower.minReq + 1, fmt.Sprintf(`
if len(str)-p < %d {
return false
}
r, size := utf8.DecodeRuneInString(str[p:])
if size == 0 || r == utf8.RuneError {
return false
}
if r == '\n' {
return false
}
p += size
`, follower.minReq+1), follower}
case *AstSeq:
return gg.generateSeq(funcID, r.seq, follower)
case *AstAlt:
return gg.generateAlt(funcID, r.opts, follower)
case *AstRepeat:
return gg.generateRepeat(funcID, r.re, r.min, r.max, follower)
case *AstCap:
return gg.compileCapture(funcID, r.re, r.index, follower)
case AstBackRef:
return gg.compileBackRef(uint(r), follower)
case AstAssertBegin:
return follower.prepend(`
if p != 0 {
return false
}
`)
case AstAssertEnd:
return follower.prepend(`
if p != len(str) {
return false
}
`)
case AstCharClass:
return gg.generateCharClass(r.str, r.CharClass, follower)
default:
panic(fmt.Errorf("Please implement compiler for %T", re))
}
}
func (gg *GoGenerator) generateLit(str string, follower *codeFragments) *codeFragments {
if len(str) == 0 {
return follower
}
minReq := follower.minReq + len(str)
var buf strings.Builder
fmt.Fprintf(&buf, `if len(str)-p < %d {
return false
}
`, minReq)
fmt.Fprintf(&buf, "if !(str[p] == %d", str[0])
for i := 1; i < len(str); i++ {
fmt.Fprintf(&buf, "&& str[p+%d] == %d", i, str[i])
}
fmt.Fprintf(&buf, `) {
return false
}
p += %d
`, len(str))
return &codeFragments{minReq, buf.String(), follower}
}
func (gg *GoGenerator) generateSeq(funcID string, seq []Ast, follower *codeFragments) *codeFragments {
if len(seq) == 0 {
return follower
}
follower = gg.generateSeq(funcID, seq[1:], follower)
return gg.generateAst(funcID, seq[0], follower)
}
func (gg *GoGenerator) generateAlt(funcID string, opts []Ast, follower *codeFragments) *codeFragments {
switch len(opts) {
case 0:
return follower
case 1:
return gg.generateSeq(funcID, opts, follower)
}
origMinReq := follower.minReq
followerState := gg.newState()
follower = follower.prepend(fmt.Sprintf(`
fallthrough
case %d:
`, followerState))
follower = gg.generateAst(funcID, opts[len(opts)-1], follower)
minReq := follower.minReq
stateLastOpt := gg.newState()
follower = follower.prepend(fmt.Sprintf("case %d:\n", stateLastOpt))
states := make([]uint, len(opts)-1)
for i := len(opts) - 2; i >= 0; i-- {
follower = follower.prepend(fmt.Sprintf("state = %d\n", followerState))
follower.minReq = origMinReq
follower = gg.generateAst(funcID, opts[i], follower)
if follower.minReq < minReq {
minReq = follower.minReq
}
s := gg.newState()
follower = follower.prepend(fmt.Sprintf("case %d:\n", s))
states[i] = s
}
tries := make([]string, len(states))
for i, s := range states {
tries[i] = fmt.Sprintf(`%s(%d, ctx, p, onSuccess)`, funcID, s)
}
follower = follower.prepend(fmt.Sprintf(`
if %s {
return true
}
state = %d
`, strings.Join(tries, " || "), stateLastOpt))
follower.minReq = minReq
return follower
}
func (gg *GoGenerator) generateRepeat(funcID string, re Ast, min, max int, follower *codeFragments) *codeFragments {
switch r := re.(type) {
case AstLit:
return gg.generateRepeatLit(funcID, string(r), min, max, follower)
case AstCharClass:
return gg.generateRepeatCharClass(funcID, r, min, max, follower)
}
if min > 0 {
return gg.generateAst(funcID, re, gg.generateRepeat(funcID, re, min-1, max-1, follower))
}
if max == 0 {
return follower
}
if max > 0 {
follower = gg.generateRepeat(funcID, re, 0, max-1, follower)
followerState := gg.newState()
follower = follower.prepend(fmt.Sprintf(`
fallthrough
case %d:
`, followerState))
minReq := follower.minReq
follower = gg.generateAst(funcID, re, follower)
altState := gg.newState()
follower = follower.prepend(fmt.Sprintf(`
if %s(%d, ctx, p, onSuccess) {
return true
}
state = %d
case %d:
`, funcID, altState, followerState, altState))
follower.minReq = minReq
return follower
}
// Here, we need to compile infinite-loop regexp
startState := gg.newState()
repeatState := gg.newState()
followerState := gg.newState()
follower = follower.prepend(fmt.Sprintf(`
state = %d
case %d:
`, startState, followerState))
minReq := follower.minReq
follower = gg.generateAst(funcID, re, follower)
if canMatchZeroWidth(re) { // If re can matches zero-width string, we need zero-width check
repeatID := gg.newRepeatID()
follower = follower.prepend(fmt.Sprintf(`
prev := ctx.FindVal(yarex.ContextKey{'r', %d})
if prev == p { // This means zero-width matching occurs.
state = %d // So, terminate repeating.
continue
}
ctx2 := ctx.Push(yarex.ContextKey{'r', %d}, p)
if %s(%d, ctx2, p, onSuccess) {
return true
}
state = %d
case %d:
`, repeatID, followerState, repeatID, funcID, repeatState, followerState, repeatState))
} else { // We can skip zero-width check for optimization
follower = follower.prepend(fmt.Sprintf(`
if %s(%d, ctx, p, onSuccess) {
return true
}
state = %d
case %d:
`, funcID, repeatState, followerState, repeatState))
}
follower.minReq = minReq
return follower.prepend(fmt.Sprintf(`
fallthrough
case %d:
`, startState))
}
func (gg *GoGenerator) generateRepeatLit(funcID string, lit string, min, max int, follower *codeFragments) *codeFragments {
gg.useSmallLoop = true
followerState := gg.newState()
minReq := follower.minReq + len(lit)
maxCond := ""
if max >= 0 {
maxCond = fmt.Sprintf(`n < %d && `, max)
}
conds := []string{}
for i := 0; i < len(lit); i++ {
conds = append(conds, fmt.Sprintf(`str[p+%d] != %d`, i, lit[i]))
}
condition := strings.Join(conds, " || ")
return follower.prepend(fmt.Sprintf(`
endPos = len(str) - %d
n = 0
for %s p <= endPos {
if %s {
break
}
if len(localStack) == n {
goto LABEL_HEAP_STACK%d
}
localStack[n] = p
n++
p += %d
}
for n > %d { // try backtrack
if %s(%d, ctx, p, onSuccess) {
return true
}
n--
p = localStack[n]
}
goto LABEL_END%d
LABEL_HEAP_STACK%d:
heapStack = (yarex.IntStackPool.Get().(*[]int))
copy(*heapStack, localStack[:])
(*heapStack)[n] = p
n++
p += %d
for %s p <= endPos {
if %s {
break
}
if len(*heapStack) == n {
*heapStack = append(*heapStack, p)
*heapStack = (*heapStack)[:cap(*heapStack)]
} else {
(*heapStack)[n] = p
}
n++
p += %d
}
for n > %d { // try backtrack
if %s(%d, ctx, p, onSuccess) {
yarex.IntStackPool.Put(heapStack)
return true
}
n--
p = (*heapStack)[n]
}
yarex.IntStackPool.Put(heapStack)
LABEL_END%d:
fallthrough
case %d:
`, minReq, maxCond, condition, followerState, len(lit), min, funcID, followerState, followerState, followerState, len(lit), maxCond, condition, len(lit), min, funcID, followerState, followerState, followerState))
}
func (gg *GoGenerator) generateRepeatCharClass(funcID string, re AstCharClass, min, max int, follower *codeFragments) *codeFragments {
gg.generateCharClass(re.str, re.CharClass, follower) // Compile and register CharClass
ccId := gg.charClasses[re.str].id // Get CharClass's identifier
followerState := gg.newState()
minReq := follower.minReq + 1
maxCond := ""
if max >= 0 {
maxCond = fmt.Sprintf(`n < %d && `, max)
}
return follower.prepend(fmt.Sprintf(`
endPos = len(str) - %d
n = 0
for %s p <= endPos {
r, size = utf8.DecodeRuneInString(str[p:])
if size == 0 || r == utf8.RuneError {
break
}
if !%s.Contains(r) {
break
}
if len(localStack) == n {
goto LABEL_HEAP_STACK%d
}
localStack[n] = p
n++
p += size
}
for n > %d { // try backtrack
if %s(%d, ctx, p, onSuccess) {
return true
}
n--
p = localStack[n]
}
goto LABEL_END%d
LABEL_HEAP_STACK%d:
heapStack = (yarex.IntStackPool.Get().(*[]int))
copy(*heapStack, localStack[:])
(*heapStack)[n] = p
n++
p += size
for %s p <= endPos {
r, size = utf8.DecodeRuneInString(str[p:])
if size == 0 || r == utf8.RuneError {
break
}
if !%s.Contains(r) {
break
}
if len(*heapStack) == n {
*heapStack = append(*heapStack, p)
*heapStack = (*heapStack)[:cap(*heapStack)]
} else {
(*heapStack)[n] = p
}
n++
p += size
}
for n > %d { // try backtrack
if %s(%d, ctx, p, onSuccess) {
yarex.IntStackPool.Put(heapStack)
return true
}
n--
p = (*heapStack)[n]
}
yarex.IntStackPool.Put(heapStack)
LABEL_END%d:
fallthrough
case %d:
`, minReq, maxCond, ccId, followerState, min, funcID, followerState, followerState, followerState, maxCond, ccId, min, funcID, followerState, followerState, followerState))
}
func (gg *GoGenerator) compileCapture(funcID string, re Ast, index uint, follower *codeFragments) *codeFragments {
follower = follower.prepend(fmt.Sprintf(`
ctx = ctx.Push(yarex.ContextKey{'c', %d}, p)
`, index))
follower = gg.generateAst(funcID, re, follower)
return follower.prepend(fmt.Sprintf(`
ctx = ctx.Push(yarex.ContextKey{'c', %d}, p)
`, index))
}
func (gg *GoGenerator) compileBackRef(index uint, follower *codeFragments) *codeFragments {
return follower.prepend(fmt.Sprintf(`
s, ok := ctx.GetCaptured(yarex.ContextKey{'c', %d})
if !ok { // There is no captured string with the index. So, failed matching.
return false
}
l := len(s)
if len(str)-p < l {
return false
}
for i := 0; i < l; i++ {
if str[p+i] != s[i] {
return false
}
}
p += l
`, index))
}
func (gg *GoGenerator) generateCharClass(ptn string, c CharClass, follower *codeFragments) *codeFragments {
var id string
if r, ok := gg.charClasses[ptn]; ok {
id = r.id
} else {
id = gg.newId()
gg.charClasses[ptn] = charClassResult{
id: id,
code: gg.generateCharClassAux(c, nil),
}
}
gg.useCharClass = true
return &codeFragments{follower.minReq + 1, fmt.Sprintf(`
if len(str)-p < %d {
return false
}
r, size = utf8.DecodeRuneInString(str[p:])
if size == 0 || r == utf8.RuneError {
return false
}
if !%s.Contains(r) {
return false
}
p += size
`, follower.minReq+1, id), follower}
}