-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregexp.go
409 lines (369 loc) · 8.17 KB
/
regexp.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
package contraption
import (
"fmt"
"log"
"reflect"
"strings"
"time"
"github.com/go-gl/glfw/v3.3/glfw"
"github.com/neputevshina/geom"
)
//go:generate go run github.com/pointlander/peg ./regexp.peg
const (
rchar = iota
rmatch
rjmp
rsplit
rsave
rjoker
rnotchar
)
type rinst struct {
opcode int
e any
typeonly bool
where pegRule
begin, end bool
x, y int
}
func (i *rinst) String() string {
var in, begin, end string
switch i.where {
case ruleAny:
in = " any"
case ruleIn:
in = " in"
case ruleOut:
in = " out"
}
if i.begin {
begin = " begin"
}
if i.end {
end = " end"
}
return fmt.Sprint("{", map[int]string{
rchar: "rchar",
rjmp: "rjmp",
rjoker: "rjoker",
rmatch: "rmatch",
rsplit: "rsplit",
rnotchar: "rnotchar",
}[i.opcode], " ", i.x, " ", i.y, in, begin, end, " ", reflect.TypeOf(i.e).Name(), " (", i.e, ")}")
}
func rcompile(pattern string) ([]rinst, error) {
var err error
peg := &rpeg{}
err = peg.Init(func(r *rpeg) error { r.Buffer = pattern; return nil })
if err != nil {
log.Panicln(err)
}
err = peg.Parse()
if err != nil {
return nil, err
}
//rprint(0, peg.AST(), pattern)
//peg.AST().PrettyPrint(os.Stdout, pattern)
v := rvisit(peg.AST().up, nil, peg.buffer)
v = append(v, rinst{opcode: rmatch})
return v, nil
}
type rlist struct {
rinst
next *rinst
}
func rprint(tab int, n *node32, pattern string) {
if n == nil {
return
}
fmt.Print(strings.Repeat("\t", tab))
fmt.Println(pattern[n.begin:n.end], "("+n.token32.String()+")")
rprint(tab+1, n.up, pattern)
rprint(tab, n.next, pattern)
}
func rvisit(n, p *node32, pattern []rune) []rinst {
if n == nil {
return nil
}
adjust := func(r []rinst, n int) {
for i := range r {
r[i].x += n
r[i].y += n
}
}
switch n.token32.pegRule {
case ruleAlter:
left := rvisit(n.up, n, pattern)
right := rvisit(n.up.next, n, pattern)
v := []rinst{
{opcode: rsplit,
x: 1,
y: 2 + len(left), // one for this, and one for next rjmp
},
}
adjust(left, 1) // for previous rsplit
v = append(v, left...)
v = append(v, rinst{
opcode: rjmp,
x: 1 + len(left) + 1 + len(right),
})
adjust(right, 2+len(left)) // rsplit+left+rjmp
v = append(v, right...)
return v
case ruleCat:
left := rvisit(n.up, n, pattern)
right := rvisit(n.up.next, n, pattern)
adjust(right, len(left))
return append(left, right...)
case rulePoint:
pt := rinst{}
left := n.up.up // Concrete -> {} here
typ := ""
val := ""
for left != nil {
switch left.pegRule {
case ruleRect:
pt.where = left.up.pegRule
case ruleTime:
switch left.up.pegRule {
case ruleBegin:
pt.begin = true
case ruleEnd:
pt.end = true
}
case ruleType:
typ = string(pattern[left.begin:left.end])
case ruleValue:
val = string(pattern[left.begin:left.end])
}
left = left.next
}
if typ == "." {
pt.opcode = rjoker
} else {
pt.opcode = rchar
if typ[0] == '!' { // TODO make an operator
typ = typ[1:]
pt.opcode = rnotchar
}
pt.e = nameevent(typ, val)
if val == "" {
pt.typeonly = true
}
}
left = n.up // Concrete
if left.next != nil {
switch left.next.pegRule {
case ruleMaybe:
v := []rinst{
{opcode: rsplit,
x: 1,
y: 2,
},
pt,
}
return v
case ruleAny:
// TODO groups
v := []rinst{
{opcode: rsplit,
x: 1,
y: 3},
pt,
{opcode: rjmp,
x: 0},
}
return v
case ruleLazyAny:
v := []rinst{
{opcode: rsplit,
x: 3,
y: 1},
pt,
{opcode: rjmp,
x: 0},
}
return v
case ruleSeveral:
panic("unimplemented, currently not needed")
default:
panic("check parser for errors")
}
}
return []rinst{pt}
}
return []rinst{}
}
// rinterp is the threaded regular expression bytecode vm taken from https://swtch.com/~rsc/regexp/regexp2.html#thompsonvm.
func rinterp(program []rinst, trace []EventPoint, rect geom.Rectangle, dur time.Duration, deadline time.Time, z int, alwaysin bool) (bool, EventTraceLast) {
// println(`rinterp`)
type rthread = int
cs := make([]rthread, 0, len(program))
ns := make([]rthread, 0, len(program))
var left, right time.Time
box := geom.Rectangle{}
cs = append(cs, 0)
var prev EventPoint
choked := false
for j, sv := range trace {
sv := sv
for i := 0; i < len(cs); i++ {
// j := j
pc := cs[i]
v := &program[pc]
joker := func() {
defer func() {
if z <= 0 {
return
}
trace[j].zc = z
}()
// This comparison is dependent on the Cond evaluation order in (*World).Develop.
where := v.where
if alwaysin && where == 0 {
where = ruleIn
}
if where != ruleAnywhere {
if where == ruleIn && !sv.Pt.In(rect) {
return
}
if where == ruleOut && sv.Pt.In(rect) {
return
}
}
// If left (ending) time is not specified first match will be it.
if left == (time.Time{}) {
left = sv.T
}
if z > 0 && trace[j].z > z {
choked = true
return
}
if v.begin {
left = sv.T
} else if v.end {
right = sv.T
}
box.Min.X = min(box.Min.X, sv.Pt.X)
box.Min.Y = min(box.Min.Y, sv.Pt.Y)
box.Max.X = min(box.Max.X, sv.Pt.X)
box.Max.Y = min(box.Max.Y, sv.Pt.Y)
ns = append(ns, cs[i]+1)
prev = sv
}
switch v.opcode {
case rnotchar:
if requals(sv, v) {
break
}
joker()
case rchar:
if requals(sv, v) {
joker()
}
case rjoker:
joker()
case rmatch:
// If right (starting time) is not specified end of the match will be on its end, duh.
if right == (time.Time{}) {
right = prev.T
}
e := EventTraceLast{
StartedAt: right,
Duration: left.Sub(right),
Box: box,
FirstTouch: sv.Pt,
}
// Set z candidates to z because we matched.
defer func() {
for j := range trace {
if trace[j].zc == z {
trace[j].z = trace[j].zc
}
}
}()
// Deadline is one who is changing there.
return left.Sub(right) <= dur && deadline.Before(left), e
case rjmp:
cs = append(cs, v.x)
case rsplit:
cs = append(cs, v.x)
cs = append(cs, v.y)
}
}
cs, ns = ns, cs
ns = ns[:0]
}
return false, EventTraceLast{Choked: choked}
}
type keyer interface {
key() glfw.Key
}
// Moved to nanovgo+glfw.go
// func requals(p EventPoint, inst *rinst) bool {
// teq := reflect.TypeOf(p.E) == reflect.TypeOf(inst.e)
// if inst.typeonly {
// return teq
// }
// pk, ok1 := p.E.(keyer) // Key in event
// ik, ok2 := inst.e.(keyer) // Key in regexp rule
// // If p.E or inst.e is not keyer, they both will fail on default condition.
// if ok1 && ok2 && teq {
// switch ik.key() {
// case anyShift:
// return pk.key() == glfw.KeyLeftShift ||
// pk.key() == glfw.KeyRightShift
// case anyCtrl:
// return pk.key() == glfw.KeyLeftControl ||
// pk.key() == glfw.KeyRightControl
// }
// }
// return p.E == inst.e
// }
/*
// pike's version of above, supports submatches https://swtch.com/~rsc/regexp/regexp2.html#pike
func rinterp(program []rinst, trace []EventPoint, rect image.Rectangle, dur time.Duration) (match bool, submatches [10][]EventPoint) {
type rthread struct {
pc *rinst
saved [10][]EventPoint
}
clist := make([]rthread, 0, len(program))
nlist := make([]rthread, 0, len(program))
var begin, end time.Time
in := true
clist = append(clist, rthread{&program[0], submatches})
for si, sv := range trace {
for i, t := range clist {
switch t.pc.opcode {
case rchar:
if t.pc.begin {
begin = sv.T
} else if t.pc.end {
end = sv.T
}
if t.pc.in {
in = in && sv.Pt.In(rect)
}
if !requals(sv, t.pc) {
break
}
clist = append(clist, rthread{&program[i+1], t.saved})
case rmatch:
submatches = t.saved
return in && end.Sub(begin) <= dur, submatches
case rjmp:
clist = append(clist, rthread{t.pc.x, t.saved})
case rsplit:
clist = append(clist, rthread{t.pc.x, t.saved})
clist = append(clist, rthread{t.pc.y, t.saved})
case rsave:
t.saved[t.pc.i] = trace[si:]
clist = append(clist, rthread{t.pc.x, t.saved})
}
}
clist, nlist = nlist, clist
nlist = nlist[:0]
}
return false, submatches
}
*/