-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathast-match.go
205 lines (193 loc) · 4.52 KB
/
ast-match.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
package yarex
import (
"strings"
"unicode/utf8"
"unsafe"
)
func astMatch(re Ast, s string) bool {
stack := make([]stackFrame, initialStackSize, initialStackSize) // We need to make stack here, instead of inside makeContext, since matchContext only contains uintptr to stack. GC does not count it as a valid reference.
getStack := func() []stackFrame {
return stack
}
setStack := func(s []stackFrame) {
stack = s
}
c0 := makeContext(&s, &getStack, &setStack)
if re.match(c0.push(0, 0), 0, func(c matchContext, _ int) *matchContext { return &c }) != nil {
return true
}
if canOnlyMatchAtBegining(re) {
return false
}
for i := 1; i < len(s); i++ {
if re.match(c0.push(0, i), i, func(c matchContext, _ int) *matchContext { return &c }) != nil {
return true
}
}
return false
}
func (re AstLit) match(c matchContext, p int, k Continuation) *matchContext {
str := *(*string)(unsafe.Pointer(c.str))
lit := string(re)
if !strings.HasPrefix(str[p:], lit) {
return nil
}
return k(c, p+len(lit))
}
func (re AstNotNewline) match(c matchContext, p int, k Continuation) *matchContext {
str := *(*string)(unsafe.Pointer(c.str))
if !(p < len(str)) || str[0] == '\n' {
return nil
}
return k(c, p+1)
}
func (r *AstSeq) match(c matchContext, p int, k Continuation) *matchContext {
seq := r.seq
var loop func(int) Continuation
loop = func(i int) func(c matchContext, p int) *matchContext {
return func(c matchContext, p int) *matchContext {
if i < len(seq) {
return seq[i].match(c, p, loop(i+1))
}
return k(c, p)
}
}
return loop(0)(c, p)
}
func (r *AstAlt) match(c matchContext, p int, k Continuation) *matchContext {
for _, re := range r.opts {
if c1 := re.match(c, p, k); c1 != nil {
return c1
}
}
return nil
}
func (r *AstRepeat) match(c matchContext, p int, k Continuation) *matchContext {
str := *(*string)(unsafe.Pointer(c.str))
switch re := r.re.(type) {
case AstLit:
s := string(re)
width := len(s)
if width == 0 {
return k(c, p)
}
p1 := p
i := 0
if r.max < 0 {
for strings.HasPrefix(str[p1:], s) {
i++
p1 += width
}
} else {
for i < r.max && strings.HasPrefix(str[p1:], s) {
i++
p1 += width
}
}
for i >= r.min { // Try backtrack
if ret := k(c, p1); ret != nil {
return ret
}
p1 -= width
i--
}
return nil
case AstCharClass:
cc := re.CharClass
stack := make([]int, 0, 64)
stack = append(stack, p)
p1 := p
i := 0
if r.max < 0 {
for p1 < len(str) {
r, size := utf8.DecodeRuneInString(str[p1:])
if cc.Contains(r) {
p1 += size
i++
stack = append(stack, p1)
} else {
break
}
}
} else {
for i < r.max && p1 < len(str) {
r, size := utf8.DecodeRuneInString(str[p1:])
if cc.Contains(r) {
p1 += size
i++
stack = append(stack, p1)
} else {
break
}
}
}
for i >= r.min { // Try backtrack
if ret := k(c, stack[i]); ret != nil {
return ret
}
i--
}
return nil
default:
prev := -1 // initial value must be a number which never equal to any position (i.e. positive integer)
var loop func(count int) Continuation
loop = func(count int) Continuation {
return func(c matchContext, p int) *matchContext {
if prev == p { // Matched zero-length assertion. So, move ahead the next pattern.
return k(c, p)
}
prev = p
if count < r.min {
return re.match(c, p, loop(count+1))
}
if count == r.max {
return k(c, p)
}
c1 := re.match(c, p, loop(count+1))
if c1 != nil {
return c1
}
return k(c, p)
}
}
return loop(0)(c, p)
}
}
func (r *AstCap) match(c matchContext, p int, k Continuation) *matchContext {
c = c.push(r.index, p)
return r.re.match(c, p, func(c matchContext, p1 int) *matchContext {
c = c.push(r.index, p1)
return k(c, p1)
})
}
func (r AstBackRef) match(c matchContext, p int, k Continuation) *matchContext {
cap, ok := c.GetCaptured(uint(r))
if !ok {
return nil
}
return AstLit(cap).match(c, p, k)
}
func (re AstAssertBegin) match(c matchContext, p int, k Continuation) *matchContext {
if p != 0 {
return nil
}
return k(c, p)
}
func (re AstAssertEnd) match(c matchContext, p int, k Continuation) *matchContext {
str := *(*string)(unsafe.Pointer(c.str))
if p != len(str) {
return nil
}
return k(c, p)
}
func (re AstCharClass) match(c matchContext, p int, k Continuation) *matchContext {
str := *(*string)(unsafe.Pointer(c.str))
if len(str) < p+1 {
return nil
}
r, size := utf8.DecodeRuneInString(str[p:])
if !re.Contains(r) {
return nil
}
return k(c, p+size)
}