-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnfa.go
344 lines (288 loc) · 7.92 KB
/
nfa.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
package rgx
import (
"fmt"
)
type group struct {
names []string
start bool
end bool
}
type backreference struct {
name string
target *State
}
type State struct {
start bool
terminal bool
endOfText bool
startOfText bool
transitions map[uint8][]*State
groups []*group
backreference *backreference
}
const (
epsilonChar = 0
startOfText = 1
endOfText = 2
anyChar = 3
newline = 10
)
func toNfa(parseContext *parsingContext) (*State, *RegexError) {
token := parseContext.tokens[0]
startState, endState, err := tokenToNfa(token, parseContext, &State{
transitions: map[uint8][]*State{},
})
if err != nil {
return nil, err
}
for i := 1; i < len(parseContext.tokens); i++ {
_, endNext, err := tokenToNfa(parseContext.tokens[i], parseContext, endState)
if err != nil {
return nil, err
}
endState = endNext
}
start := &State{
start: true,
transitions: map[uint8][]*State{
epsilonChar: {startState},
},
groups: []*group{{
names: []string{"0"},
start: true,
end: false,
}},
}
end := &State{
transitions: map[uint8][]*State{},
terminal: true,
groups: []*group{
{
names: []string{"0"},
start: false,
end: true,
},
},
}
endState.transitions[epsilonChar] = append(endState.transitions[epsilonChar], end)
return start, nil
}
func tokenToNfa(token regexToken, parseContext *parsingContext, startFrom *State) (*State, *State, *RegexError) {
switch token.tokenType {
case literal:
value := token.value.(uint8)
to := &State{
transitions: map[uint8][]*State{},
}
startFrom.transitions[value] = []*State{to}
return startFrom, to, nil
case quantifier:
return handleQuantifierToToken(token, parseContext, startFrom)
case wildcard:
to := &State{
transitions: map[uint8][]*State{},
}
startFrom.transitions[anyChar] = []*State{to}
return startFrom, to, nil
case or:
values := token.value.([]regexToken)
_, end1, err := tokenToNfa(values[0], parseContext, startFrom)
if err != nil {
return nil, nil, err
}
_, end2, err := tokenToNfa(values[1], parseContext, startFrom)
if err != nil {
return nil, nil, err
}
to := &State{
transitions: map[uint8][]*State{},
}
end1.transitions[epsilonChar] = append(end1.transitions[epsilonChar], to)
end2.transitions[epsilonChar] = append(end2.transitions[epsilonChar], to)
return startFrom, to, nil
case groupCaptured:
v := token.value.(groupTokenPayload)
// concatenate all the elements in the group
start, end, err := tokenToNfa(v.tokens[0], parseContext, &State{
transitions: map[uint8][]*State{},
})
if err != nil {
return nil, nil, err
}
for i := 1; i < len(v.tokens); i++ {
_, endNext, err := tokenToNfa(v.tokens[i], parseContext, end)
if err != nil {
return nil, nil, err
}
end = endNext
}
// concatenation ends
groupNameNumeric := fmt.Sprintf("%d", parseContext.nextGroup())
groupNameUserSet := v.name
groupNames := []string{groupNameNumeric}
parseContext.capturedGroups[groupNameNumeric] = true
if groupNameUserSet != "" {
groupNames = append(groupNames, groupNameUserSet)
parseContext.capturedGroups[groupNameUserSet] = true
}
if startFrom.groups != nil {
startFrom.groups = append(startFrom.groups, &group{
names: groupNames,
start: true,
})
} else {
startFrom.groups = []*group{{
names: groupNames,
start: true,
}}
}
if end.groups != nil {
end.groups = append(end.groups, &group{
names: groupNames,
end: true,
})
} else {
end.groups = []*group{{
names: groupNames,
end: true,
}}
}
startFrom.transitions[epsilonChar] = append(startFrom.transitions[epsilonChar], start)
return startFrom, end, nil
case groupUncaptured:
values := token.value.([]regexToken)
if len(values) == 0 {
end := &State{
transitions: map[uint8][]*State{},
}
startFrom.transitions[epsilonChar] = append(startFrom.transitions[epsilonChar], end)
return startFrom, end, nil
}
start, end, err := tokenToNfa(values[0], parseContext, &State{
transitions: map[uint8][]*State{},
})
if err != nil {
return nil, nil, err
}
for i := 1; i < len(values); i++ {
_, endNext, err := tokenToNfa(values[i], parseContext, end)
if err != nil {
return nil, nil, err
}
end = endNext
}
startFrom.transitions[epsilonChar] = append(startFrom.transitions[epsilonChar], start)
return startFrom, end, nil
case bracket:
constructTokens := token.value.(map[uint8]bool)
to := &State{
transitions: map[uint8][]*State{},
}
for ch := range constructTokens {
startFrom.transitions[ch] = []*State{to}
}
return startFrom, to, nil
case bracketNot:
constructTokens := token.value.(map[uint8]bool)
to := &State{
transitions: map[uint8][]*State{},
}
deadEnd := &State{
transitions: map[uint8][]*State{},
}
for ch := range constructTokens {
startFrom.transitions[ch] = []*State{deadEnd}
}
startFrom.transitions[anyChar] = []*State{to}
return startFrom, to, nil
case textBeginning:
to := &State{
transitions: map[uint8][]*State{},
}
startFrom.startOfText = true
startFrom.transitions[epsilonChar] = append(startFrom.transitions[epsilonChar], to)
return startFrom, to, nil
case textEnd:
startFrom.endOfText = true
return startFrom, startFrom, nil
case backReference:
groupName := token.value.(string)
if _, ok := parseContext.capturedGroups[groupName]; !ok {
return nil, nil, &RegexError{
Code: CompilationError,
Message: fmt.Sprintf("Group (%s) does not exist", groupName),
}
}
to := &State{
transitions: map[uint8][]*State{},
}
startFrom.backreference = &backreference{
name: groupName,
target: to,
}
return startFrom, to, nil
default:
return nil, nil, &RegexError{
Code: CompilationError,
Message: fmt.Sprintf("unrecognized token: %+v", token),
}
}
}
func handleQuantifierToToken(token regexToken, parseContext *parsingContext, startFrom *State) (*State, *State, *RegexError) {
payload := token.value.(quantifierPayload)
// the minimum amount of time the NFA needs to repeat
min := payload.min
// the maximum amount of time the NFA needs to repeat
max := payload.max
to := &State{
transitions: map[uint8][]*State{},
}
if min == 0 {
startFrom.transitions[epsilonChar] = append(startFrom.transitions[epsilonChar], to)
}
// how many times should the NFA be generated in the bigger state machine
var total int
if max != quantifierInfinity {
total = max
} else {
if min == 0 {
total = 1 // we need to at least create this NFA once, even if we require it 0 times
} else {
total = min
}
}
var value = payload.value
previousStart, previousEnd, err := tokenToNfa(value, parseContext, &State{
transitions: map[uint8][]*State{},
})
if err != nil {
return nil, nil, err
}
startFrom.transitions[epsilonChar] = append(startFrom.transitions[epsilonChar], previousStart)
// starting from 2, because the one above is the first one
for i := 2; i <= total; i++ {
// the same NFA needs to be generated 'total' times
start, end, err := tokenToNfa(value, parseContext, &State{
transitions: map[uint8][]*State{},
})
if err != nil {
return nil, nil, err
}
// connect the end of the previous one to the start of this one
previousEnd.transitions[epsilonChar] = append(previousEnd.transitions[epsilonChar], start)
// keep track of the previous NFA's entry and exit states
previousStart = start
previousEnd = end
// after the minimum required amount of repetitions
// the rest must be optional, thus we add an epsilon transition
// to the start of each NFA so that we can skip them if needed
if i > min {
start.transitions[epsilonChar] = append(start.transitions[epsilonChar], to)
}
}
previousEnd.transitions[epsilonChar] = append(previousEnd.transitions[epsilonChar], to)
if max == quantifierInfinity {
to.transitions[epsilonChar] = append(to.transitions[epsilonChar], previousStart)
}
return startFrom, to, nil
}