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
|
// Copyright (c) 2017 Couchbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package regexp
import (
"regexp/syntax"
"unicode"
unicode_utf8 "unicode/utf8"
"github.com/couchbase/vellum/utf8"
)
type compiler struct {
sizeLimit uint
insts prog
instsPool []inst
sequences utf8.Sequences
rangeStack utf8.RangeStack
startBytes []byte
endBytes []byte
}
func newCompiler(sizeLimit uint) *compiler {
return &compiler{
sizeLimit: sizeLimit,
startBytes: make([]byte, unicode_utf8.UTFMax),
endBytes: make([]byte, unicode_utf8.UTFMax),
}
}
func (c *compiler) compile(ast *syntax.Regexp) (prog, error) {
err := c.c(ast)
if err != nil {
return nil, err
}
inst := c.allocInst()
inst.op = OpMatch
c.insts = append(c.insts, inst)
return c.insts, nil
}
func (c *compiler) c(ast *syntax.Regexp) (err error) {
if ast.Flags&syntax.NonGreedy > 1 {
return ErrNoLazy
}
switch ast.Op {
case syntax.OpEndLine, syntax.OpBeginLine,
syntax.OpBeginText, syntax.OpEndText:
return ErrNoEmpty
case syntax.OpWordBoundary, syntax.OpNoWordBoundary:
return ErrNoWordBoundary
case syntax.OpEmptyMatch:
return nil
case syntax.OpLiteral:
for _, r := range ast.Rune {
if ast.Flags&syntax.FoldCase > 0 {
next := syntax.Regexp{
Op: syntax.OpCharClass,
Flags: ast.Flags & syntax.FoldCase,
Rune0: [2]rune{r, r},
}
next.Rune = next.Rune0[0:2]
// try to find more folded runes
for r1 := unicode.SimpleFold(r); r1 != r; r1 = unicode.SimpleFold(r1) {
next.Rune = append(next.Rune, r1, r1)
}
err = c.c(&next)
if err != nil {
return err
}
} else {
c.sequences, c.rangeStack, err = utf8.NewSequencesPrealloc(
r, r, c.sequences, c.rangeStack, c.startBytes, c.endBytes)
if err != nil {
return err
}
for _, seq := range c.sequences {
c.compileUtf8Ranges(seq)
}
}
}
case syntax.OpAnyChar:
next := syntax.Regexp{
Op: syntax.OpCharClass,
Flags: ast.Flags & syntax.FoldCase,
Rune0: [2]rune{0, unicode.MaxRune},
}
next.Rune = next.Rune0[:2]
return c.c(&next)
case syntax.OpAnyCharNotNL:
next := syntax.Regexp{
Op: syntax.OpCharClass,
Flags: ast.Flags & syntax.FoldCase,
Rune: []rune{0, 0x09, 0x0B, unicode.MaxRune},
}
return c.c(&next)
case syntax.OpCharClass:
return c.compileClass(ast)
case syntax.OpCapture:
return c.c(ast.Sub[0])
case syntax.OpConcat:
for _, sub := range ast.Sub {
err := c.c(sub)
if err != nil {
return err
}
}
return nil
case syntax.OpAlternate:
if len(ast.Sub) == 0 {
return nil
}
jmpsToEnd := make([]uint, 0, len(ast.Sub)-1)
// does not handle last entry
for i := 0; i < len(ast.Sub)-1; i++ {
sub := ast.Sub[i]
split := c.emptySplit()
j1 := c.top()
err := c.c(sub)
if err != nil {
return err
}
jmpsToEnd = append(jmpsToEnd, c.emptyJump())
j2 := c.top()
c.setSplit(split, j1, j2)
}
// handle last entry
err := c.c(ast.Sub[len(ast.Sub)-1])
if err != nil {
return err
}
end := uint(len(c.insts))
for _, jmpToEnd := range jmpsToEnd {
c.setJump(jmpToEnd, end)
}
case syntax.OpQuest:
split := c.emptySplit()
j1 := c.top()
err := c.c(ast.Sub[0])
if err != nil {
return err
}
j2 := c.top()
c.setSplit(split, j1, j2)
case syntax.OpStar:
j1 := c.top()
split := c.emptySplit()
j2 := c.top()
err := c.c(ast.Sub[0])
if err != nil {
return err
}
jmp := c.emptyJump()
j3 := uint(len(c.insts))
c.setJump(jmp, j1)
c.setSplit(split, j2, j3)
case syntax.OpPlus:
j1 := c.top()
err := c.c(ast.Sub[0])
if err != nil {
return err
}
split := c.emptySplit()
j2 := c.top()
c.setSplit(split, j1, j2)
case syntax.OpRepeat:
if ast.Max == -1 {
for i := 0; i < ast.Min; i++ {
err := c.c(ast.Sub[0])
if err != nil {
return err
}
}
next := syntax.Regexp{
Op: syntax.OpStar,
Flags: ast.Flags,
Sub: ast.Sub,
Sub0: ast.Sub0,
Rune: ast.Rune,
Rune0: ast.Rune0,
}
return c.c(&next)
}
for i := 0; i < ast.Min; i++ {
err := c.c(ast.Sub[0])
if err != nil {
return err
}
}
splits := make([]uint, 0, ast.Max-ast.Min)
starts := make([]uint, 0, ast.Max-ast.Min)
for i := ast.Min; i < ast.Max; i++ {
splits = append(splits, c.emptySplit())
starts = append(starts, uint(len(c.insts)))
err := c.c(ast.Sub[0])
if err != nil {
return err
}
}
end := uint(len(c.insts))
for i := 0; i < len(splits); i++ {
c.setSplit(splits[i], starts[i], end)
}
}
return c.checkSize()
}
func (c *compiler) checkSize() error {
if uint(len(c.insts)*instSize) > c.sizeLimit {
return ErrCompiledTooBig
}
return nil
}
func (c *compiler) compileClass(ast *syntax.Regexp) error {
if len(ast.Rune) == 0 {
return nil
}
jmps := make([]uint, 0, len(ast.Rune)-2)
// does not do last pair
for i := 0; i < len(ast.Rune)-2; i += 2 {
rstart := ast.Rune[i]
rend := ast.Rune[i+1]
split := c.emptySplit()
j1 := c.top()
err := c.compileClassRange(rstart, rend)
if err != nil {
return err
}
jmps = append(jmps, c.emptyJump())
j2 := c.top()
c.setSplit(split, j1, j2)
}
// handle last pair
rstart := ast.Rune[len(ast.Rune)-2]
rend := ast.Rune[len(ast.Rune)-1]
err := c.compileClassRange(rstart, rend)
if err != nil {
return err
}
end := c.top()
for _, jmp := range jmps {
c.setJump(jmp, end)
}
return nil
}
func (c *compiler) compileClassRange(startR, endR rune) (err error) {
c.sequences, c.rangeStack, err = utf8.NewSequencesPrealloc(
startR, endR, c.sequences, c.rangeStack, c.startBytes, c.endBytes)
if err != nil {
return err
}
jmps := make([]uint, 0, len(c.sequences)-1)
// does not do last entry
for i := 0; i < len(c.sequences)-1; i++ {
seq := c.sequences[i]
split := c.emptySplit()
j1 := c.top()
c.compileUtf8Ranges(seq)
jmps = append(jmps, c.emptyJump())
j2 := c.top()
c.setSplit(split, j1, j2)
}
// handle last entry
c.compileUtf8Ranges(c.sequences[len(c.sequences)-1])
end := c.top()
for _, jmp := range jmps {
c.setJump(jmp, end)
}
return nil
}
func (c *compiler) compileUtf8Ranges(seq utf8.Sequence) {
for _, r := range seq {
inst := c.allocInst()
inst.op = OpRange
inst.rangeStart = r.Start
inst.rangeEnd = r.End
c.insts = append(c.insts, inst)
}
}
func (c *compiler) emptySplit() uint {
inst := c.allocInst()
inst.op = OpSplit
c.insts = append(c.insts, inst)
return c.top() - 1
}
func (c *compiler) emptyJump() uint {
inst := c.allocInst()
inst.op = OpJmp
c.insts = append(c.insts, inst)
return c.top() - 1
}
func (c *compiler) setSplit(i, pc1, pc2 uint) {
split := c.insts[i]
split.splitA = pc1
split.splitB = pc2
}
func (c *compiler) setJump(i, pc uint) {
jmp := c.insts[i]
jmp.to = pc
}
func (c *compiler) top() uint {
return uint(len(c.insts))
}
func (c *compiler) allocInst() *inst {
if len(c.instsPool) <= 0 {
c.instsPool = make([]inst, 16)
}
inst := &c.instsPool[0]
c.instsPool = c.instsPool[1:]
return inst
}
|