-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoptreematch.go
243 lines (239 loc) · 5.25 KB
/
optreematch.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
package yarex
import (
"strings"
"unicode/utf8"
"unsafe"
)
type opExecer struct {
op OpTree
}
func (oe opExecer) exec(str string, pos int, onSuccess func(MatchContext)) bool {
op := oe.op
_, headOnly := op.(*OpAssertBegin)
if headOnly && pos != 0 {
return false
}
minReq := op.minimumReq()
if minReq > len(str)-pos {
return false
}
stack := *(opStackPool.Get().(*[]opStackFrame))
defer func() { opStackPool.Put(&stack) }()
getter := func() []opStackFrame { return stack }
setter := func(s []opStackFrame) { stack = s }
ctx0 := makeOpMatchContext(&str, &getter, &setter)
if opTreeExec(op, ctx0.Push(ContextKey{'c', 0}, 0), pos, onSuccess) {
return true
}
if headOnly {
return false
}
for i := pos + 1; minReq <= len(str)-i; i++ {
if opTreeExec(op, ctx0.Push(ContextKey{'c', 0}, i), i, onSuccess) {
return true
}
}
return false
}
func opTreeExec(next OpTree, ctx MatchContext, p int, onSuccess func(MatchContext)) bool {
str := *(*string)(unsafe.Pointer(ctx.Str))
var (
localStack [16]int
heapStack *[]int
)
for {
switch op := next.(type) {
case OpSuccess:
ctx = ctx.Push(ContextKey{'c', 0}, p)
onSuccess(ctx)
return true
case *OpStr:
if len(str)-p < op.minReq {
return false
}
for i := 0; i < len(op.str); i++ {
if str[p+i] != op.str[i] {
return false
}
}
next = op.follower
p += len(op.str)
case *OpAlt:
if opTreeExec(op.follower, ctx, p, onSuccess) {
return true
}
next = op.alt
case *OpRepeat:
prev := ctx.FindVal(op.key)
if prev == p { // This means zero-width matching occurs.
next = op.alt // So, terminate repeating.
continue
}
ctx2 := ctx.Push(op.key, p)
if opTreeExec(op.follower, ctx2, p, onSuccess) {
return true
}
next = op.alt
case *OpRepeatLit:
endPos := len(str) - op.minReq - len(op.lit)
n := 0
for (op.max < 0 || n < op.max) && p <= endPos {
if str[p:p+len(op.lit)] != op.lit {
break
}
if len(localStack) == n {
goto OpRepeatLit_HEAP_STACK
}
localStack[n] = p
n++
p += len(op.lit)
}
for n > 0 { // try backtrack
if opTreeExec(op.follower, ctx, p, onSuccess) {
return true
}
n--
p = localStack[n]
}
next = op.follower
break
OpRepeatLit_HEAP_STACK:
heapStack = (IntStackPool.Get().(*[]int))
copy(*heapStack, localStack[:])
(*heapStack)[n] = p
n++
p += len(op.lit)
for (op.max < 0 || n < op.max) && p <= endPos {
if str[p:p+len(op.lit)] != op.lit {
break
}
if len(*heapStack) == n {
*heapStack = append(*heapStack, p)
*heapStack = (*heapStack)[:cap(*heapStack)]
} else {
(*heapStack)[n] = p
}
n++
p += len(op.lit)
}
for n > 0 { // try backtrack
if opTreeExec(op.follower, ctx, p, onSuccess) {
IntStackPool.Put(heapStack)
return true
}
n--
p = (*heapStack)[n]
}
IntStackPool.Put(heapStack)
next = op.follower
case *OpRepeatClass:
endPos := len(str) - op.minReq - 1
size := 0
n := 0
for (op.max < 0 || n < op.max) && p <= endPos {
r, size := utf8.DecodeRuneInString(str[p:])
if size == 0 || r == utf8.RuneError {
break
}
if !op.CharClass.Contains(r) {
break
}
if len(localStack) == n {
goto OpRepeatClass_HEAP_STACK
}
localStack[n] = p
n++
p += size
}
for n > 0 { // try backtrack
if opTreeExec(op.follower, ctx, p, onSuccess) {
return true
}
n--
p = localStack[n]
}
next = op.follower
break
OpRepeatClass_HEAP_STACK:
heapStack = (IntStackPool.Get().(*[]int))
copy(*heapStack, localStack[:])
(*heapStack)[n] = p
n++
p += size
for (op.max < 0 || n < op.max) && p <= endPos {
r, size := utf8.DecodeRuneInString(str[p:])
if size == 0 || r == utf8.RuneError {
break
}
if !op.CharClass.Contains(r) {
break
}
if len(*heapStack) == n {
*heapStack = append(*heapStack, p)
*heapStack = (*heapStack)[:cap(*heapStack)]
} else {
(*heapStack)[n] = p
}
n++
p += size
}
for n > 0 { // try backtrack
if opTreeExec(op.follower, ctx, p, onSuccess) {
IntStackPool.Put(heapStack)
return true
}
n--
p = (*heapStack)[n]
}
IntStackPool.Put(heapStack)
next = op.follower
case *OpClass:
if len(str)-p < op.minReq {
return false
}
r, size := utf8.DecodeRuneInString(str[p:])
if size == 0 || r == utf8.RuneError {
return false
}
if !op.cls.Contains(r) {
return false
}
next = op.follower
p += size
case *OpNotNewLine:
if len(str)-p < op.minReq {
return false
}
r, size := utf8.DecodeRuneInString(str[p:])
if size == 0 || r == utf8.RuneError {
return false
}
if r == '\n' {
return false
}
next = op.follower
p += size
case *OpCaptureStart:
return opTreeExec(op.follower, ctx.Push(op.key, p), p, onSuccess)
case *OpCaptureEnd:
return opTreeExec(op.follower, ctx.Push(op.key, p), p, onSuccess)
case *OpBackRef:
s, ok := ctx.GetCaptured(op.key)
if !ok || !strings.HasPrefix(str[p:], s) {
return false
}
next = op.follower
p += len(s)
case *OpAssertBegin:
if p != 0 {
return false
}
next = op.follower
case *OpAssertEnd:
if p != len(str) {
return false
}
next = op.follower
}
}
}