-
Notifications
You must be signed in to change notification settings - Fork 3
/
opcompile.go
206 lines (197 loc) · 4.4 KB
/
opcompile.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
package yarex
func newOpAlt(left, right OpTree) *OpAlt {
min := left.minimumReq()
if right.minimumReq() < min {
min = right.minimumReq()
}
return &OpAlt{
OpBase: OpBase{
minReq: min,
follower: left,
},
alt: right,
}
}
func opCompile(re Ast) OpTree {
return (&opCompiler{}).compile(re, OpSuccess{})
}
type opCompiler struct {
repeatCount uint
}
func (oc *opCompiler) compile(re Ast, follower OpTree) OpTree {
switch r := re.(type) {
case AstLit:
str := string(r)
return &OpStr{
OpBase: OpBase{
minReq: follower.minimumReq() + len(str),
follower: follower,
},
str: str,
}
case *AstSeq:
return oc.compileSeq(r.seq, follower)
case *AstAlt:
return oc.compileAlt(r.opts, follower)
case AstNotNewline:
return &OpNotNewLine{
OpBase: OpBase{
minReq: follower.minimumReq() + 1,
follower: follower,
},
}
case *AstRepeat:
return oc.compileRepeat(r.re, r.min, r.max, follower)
case *AstCap:
return oc.compileCapture(r.re, r.index, follower)
case AstBackRef:
return &OpBackRef{
OpBase: OpBase{
minReq: follower.minimumReq(),
follower: follower,
},
key: ContextKey{'c', uint(r)},
}
case AstAssertBegin:
return &OpAssertBegin{
OpBase: OpBase{
minReq: follower.minimumReq(),
follower: follower,
},
}
case AstAssertEnd:
return &OpAssertEnd{
OpBase: OpBase{
minReq: follower.minimumReq(),
follower: follower,
},
}
case AstCharClass:
return &OpClass{
OpBase: OpBase{
minReq: follower.minimumReq() + 1,
follower: follower,
},
cls: (CharClass)(r),
}
}
panic("EXECUTION SHOULD NOT REACH HERE")
}
func (oc *opCompiler) compileSeq(seq []Ast, follower OpTree) OpTree {
if len(seq) == 0 {
return follower
}
follower = oc.compileSeq(seq[1:], follower)
return oc.compile(seq[0], follower)
}
func (oc *opCompiler) compileAlt(opts []Ast, follower OpTree) OpTree {
if len(opts) == 0 {
panic("THIS SHOULD NOT HAPPEN")
}
left := oc.compile(opts[0], follower)
if len(opts) == 1 {
return left
}
right := oc.compileAlt(opts[1:], follower)
return newOpAlt(left, right)
}
func (oc *opCompiler) compileRepeat(re Ast, min, max int, follower OpTree) OpTree {
if min > 0 {
return oc.compile(re, oc.compileRepeat(re, min-1, max-1, follower))
}
if max == 0 {
return follower
}
switch r := re.(type) {
// Optimization for repeating fixed-length patterns
case AstLit:
return &OpRepeatLit{
OpBase: OpBase{
follower: follower,
minReq: follower.minimumReq(),
},
lit: string(r),
max: max,
}
case AstCharClass:
return &OpRepeatClass{
OpBase: OpBase{
follower: follower,
minReq: follower.minimumReq(),
},
CharClass: r.CharClass,
max: max,
}
}
if max > 0 {
left := oc.compile(re, oc.compileRepeat(re, 0, max-1, follower))
return newOpAlt(left, follower)
}
// If you are here max < 0, which means infinite repeat
if !canMatchZeroWidth(re) { // If re does not match zero-width string, we can optimize by skipping zero-width check
self := &OpAlt{
OpBase: OpBase{
minReq: follower.minimumReq(),
},
alt: follower,
}
self.follower = oc.compile(re, self) // self-reference makes infinite loop
return self
}
oc.repeatCount++
self := &OpRepeat{
OpBase: OpBase{
minReq: follower.minimumReq(),
},
key: ContextKey{'r', oc.repeatCount},
alt: follower,
}
self.follower = oc.compile(re, self) // self-reference makes infinite loop
return self
}
func canMatchZeroWidth(re Ast) bool {
switch r := re.(type) {
case AstBackRef, AstAssertBegin, AstAssertEnd:
return true
case AstNotNewline, AstCharClass:
return false
case AstLit:
return len(string(r)) == 0
case *AstSeq:
for _, s := range r.seq {
if !canMatchZeroWidth(s) {
return false
}
}
return true
case *AstAlt:
for _, o := range r.opts {
if canMatchZeroWidth(o) {
return true
}
}
return false
case *AstRepeat:
return r.min == 0 || canMatchZeroWidth(r.re)
case *AstCap:
return canMatchZeroWidth(r.re)
}
panic("EXECUTION SHOULD NOT REACH HERE")
}
func (oc *opCompiler) compileCapture(re Ast, index uint, follower OpTree) OpTree {
follower = &OpCaptureEnd{
OpBase: OpBase{
minReq: follower.minimumReq(),
follower: follower,
},
key: ContextKey{'c', index},
}
follower = oc.compile(re, follower)
return &OpCaptureStart{
OpBase: OpBase{
minReq: follower.minimumReq(),
follower: follower,
},
key: ContextKey{'c', index},
}
}