-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathregexprs.nim
428 lines (391 loc) · 12.5 KB
/
regexprs.nim
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#
#
# Lexim - The Lexer Generator for Nim
# (c) Copyright 2015 Andreas Rumpf
#
# See the file "copying.txt", included in this
# distribution, for details about the copyright.
#
# This module implements a parser for regular expressions.
import strutils
type
RegexKind* = enum ## the regex AST's kind
reEps, ## epsilon node
reChar, ## character node
reStr, ## string node
reCClass, ## character class node
reStar, ## star node
rePlus, ## plus node
reOpt, ## option node
reCat, ## concatenation node
reAlt, ## alternatives node (|)
reCapture, ## (capture)
reCaptureEnd, ## not used by regex, but by NFA
reBackref, ## \\backref
reBegin, ## \\A
reEnd, ## \\Z
reWordBoundary, ## \\b
reWordBoundaryNot ## \\B
PRegExpr* = ref TRegExpr
TRegExpr* = object
kind*: RegexKind
a*, b*: PRegExpr # some nodes have two successors
c*: char
s*: string
cc*: ref set[char]
rule*: int # if >= 0 it is a final state;
# then it is the rule that was matched
RegexError* = object of ValueError
RegexFlag* = enum ## how regexes are parsed
reExtended, ## extended syntax support
reNoBackrefs, ## always process \\1 as a character literal,
## not as back reference
reNoCaptures ## () is the same as (?:)
MacroLookupProc* = proc (macroname: string): PRegExpr {.closure.} ## \
## lookup proc that expands {macros}.
ReCtx = object
pos: int
flags: set[RegexFlag]
captures: int # count the captures to give them an index
findMacro: MacroLookupProc
const
wordChars* = {'A'..'Z', 'a'..'z', '0'..'9', '_', '\128', '\255'}
whitespace* = {'\1'..'\32'}
digits* = {'0'..'9'}
proc newExpr(kind: RegexKind): PRegExpr =
new(result)
result.kind = kind
proc epsExpr*(): PRegExpr =
result = newExpr(reEps)
proc charExpr*(c: char): PRegExpr =
result = newExpr(reChar)
result.c = c
proc backrefExpr*(x: int): PRegExpr =
result = newExpr(reBackref)
result.c = char x
proc strExpr*(str: string): PRegExpr =
if len(str) == 1:
result = charExpr(str[0])
else:
result = newExpr(reStr)
result.s = str
proc cclassExpr*(charset: set[char]): PRegExpr =
result = newExpr(reCClass)
new(result.cc)
result.cc[] = charset
proc starExpr*(r: PRegExpr): PRegExpr =
if r.kind == reStar:
result = r
else:
result = newExpr(reStar)
result.a = r
proc plusExpr*(r: PRegExpr): PRegExpr =
result = newExpr(rePlus)
result.a = r
proc optExpr*(r: PRegExpr): PRegExpr =
result = newExpr(reOpt)
result.a = r
proc catExpr*(a, b: PRegExpr): PRegExpr =
result = newExpr(reCat)
result.a = a
result.b = b
proc altExpr*(a, b: PRegExpr): PRegExpr =
result = newExpr(reAlt)
result.a = a
result.b = b
proc altExpr*(a: varargs[PRegExpr]): PRegExpr =
result = altExpr(a[0], a[1])
for i in 2 ..< a.len:
result = result.altExpr(a[i])
proc mnExpr*(r: PRegExpr; m, n: int): PRegExpr =
var ri: PRegExpr
if m > n or n == 0:
result = epsExpr()
else:
# construct r^m:
if m == 0:
ri = epsExpr()
else:
ri = r
for i in countup(2, m): ri = catExpr(ri, r)
result = ri # r{m,n} := r^m
for i in countup(m + 1, n):
if ri.kind == reEps: ri = r
else: ri = catExpr(ri, r)
result = altExpr(result, ri) # r{m,n} := r{m,n} | r^i,
# i=m+1,...,n
proc newCapture*(a: PRegExpr): PRegExpr =
result = newExpr(reCapture)
result.a = a
proc getNext(buf: string; c: var ReCtx): char =
if reExtended in c.flags:
while c.pos < buf.len and buf[c.pos] in {' ', '\t'}: inc(c.pos)
result = if c.pos < buf.len: buf[c.pos] else: '\0'
proc error(msg: string) {.noinline.} =
raise newException(RegexError, msg)
proc getChar(buf: string; c: var ReCtx; inClass: bool): PRegExpr =
var val, i: int
if reExtended in c.flags and not inClass:
while c.pos < buf.len and buf[c.pos] in {' ', '\t'}: inc(c.pos)
if c.pos < buf.len and buf[c.pos] != '\\':
result = charExpr(buf[c.pos])
inc(c.pos)
else:
let ch = if c.pos+1 < buf.len: buf[c.pos+1] else: '\0'
case ch
of 'n':
result = altExpr(strExpr("\C\L"), charExpr('\L'), charExpr('\C'))
inc(c.pos, 2)
of 'r':
result = charExpr('\r')
inc(c.pos, 2)
of 'l', 'L':
result = charExpr('\L')
inc(c.pos, 2)
of 't':
result = charExpr('\t')
inc(c.pos, 2)
of 'b':
result = if inClass: charExpr('\b') else: newExpr(reWordBoundary)
inc(c.pos, 2)
of 'B':
result = if inClass: charExpr('\b') else: newExpr(reWordBoundaryNot)
inc(c.pos, 2)
of 'e':
result = charExpr('\e')
inc(c.pos, 2)
of 'a', 'A':
result = if inClass: charExpr('\a') else: newExpr(reBegin)
inc(c.pos, 2)
of 'v':
result = charExpr('\v')
inc(c.pos, 2)
of 'f':
result = charExpr('\f')
inc(c.pos, 2)
of 'z', 'Z':
if not inClass: result = newExpr(reEnd)
else: error("\\Z not supported in character class")
inc(c.pos, 2)
of 's':
result = cclassExpr(whitespace)
inc(c.pos, 2)
of 'S':
result = cclassExpr({'\1'..'\255'} - whitespace)
inc(c.pos, 2)
of 'd':
result = cclassExpr(digits)
inc(c.pos, 2)
of 'D':
result = cclassExpr({'\1'..'\255'} - digits)
inc(c.pos, 2)
of 'w':
result = cclassExpr(wordChars)
inc(c.pos, 2)
of 'W':
result = cclassExpr({'\1'..'\255'} - wordChars)
inc(c.pos, 2)
of '0'..'9':
let startsWithZero = ch == '0'
val = ord(ch) - ord('0')
inc(c.pos, 2)
i = 1
while (i <= 4) and c.pos < buf.len and (buf[c.pos] in {'0'..'9'}):
val = val * 10 + ord(buf[c.pos]) - ord('0')
inc(c.pos)
inc(i)
if startsWithZero or reNoBackrefs in c.flags:
result = charExpr(char val)
else:
result = backrefExpr(val)
else:
if ch in {'\0'..'\x1F'}:
error "invalid character #" & toHex(ch.ord, 2)
else:
result = charExpr(ch)
inc(c.pos, 2)
proc parseStr(buf: string; c: var ReCtx): PRegExpr =
var s = ""
inc(c.pos) # skip "
while c.pos < buf.len and buf[c.pos] != '\"':
if buf[c.pos] in {'\0', '\C', '\L'}:
error "\" expected"
let al = getChar(buf, c,false)
if al.kind == reChar: s.add al.c
else: error "invalid regular expression " & buf
inc(c.pos) # skip "
result = strExpr(s)
proc parseCClass(buf: string; c: var ReCtx): PRegExpr =
# scan a character class
var
caret: bool
cc: set[char]
inc(c.pos) # skip [
if c.pos < buf.len and buf[c.pos] == '^':
caret = true
inc(c.pos)
else:
caret = false
while c.pos < buf.len and buf[c.pos] != ']':
if buf[c.pos] in {'\0', '\C', '\L'}:
error "] expected"
let a = getChar(buf, c, true)
if a.kind == reChar:
incl(cc, a.c)
if c.pos < buf.len and buf[c.pos] == '-':
inc(c.pos)
if c.pos < buf.len and buf[c.pos] == ']':
incl(cc, '-')
break
let b = getChar(buf, c, true)
if b.kind == reChar:
cc = cc + {a.c .. b.c}
elif b.kind == reCClass:
incl(cc, '-')
cc = cc + b.cc[]
else:
error "invalid regular expression " & buf
elif a.kind == reCClass:
cc = cc + a.cc[]
else:
error "invalid regular expression " & buf
if c.pos < buf.len and buf[c.pos] == ']': inc(c.pos)
else: error "] expected"
if caret: result = cclassExpr({'\1'..'\xFF'} - cc)
else: result = cclassExpr(cc)
proc parseNum(buf: string; c: var ReCtx): int =
result = 0
if c.pos < buf.len and buf[c.pos] in {'0'..'9'}:
while true:
result = result * 10 + ord(buf[c.pos]) - ord('0')
inc(c.pos)
if c.pos >= buf.len or buf[c.pos] notin {'0'..'9'}: break
else:
error "number expected"
proc parseIdent(buf: string; c: var ReCtx): string =
result = ""
if c.pos < buf.len and buf[c.pos] in {'a'..'z', 'A'..'Z', '_'}:
while c.pos < buf.len:
case buf[c.pos]
of 'a'..'z', 'A'..'Z', '0'..'9':
result.add toUpperAscii(buf[c.pos])
inc(c.pos)
of '_':
inc(c.pos) # ignore _
else: break
else:
error "identifier expected"
proc parseMacroCall(buf: string; c: var ReCtx): PRegExpr =
let name = parseIdent(buf, c)
result = c.findMacro(name)
if result.isNil:
error "undefined macro: " & name
proc parseRegExpr*(buf: string; c: var ReCtx): PRegExpr
proc factor(buf: string; c: var ReCtx): PRegExpr =
case getNext(buf, c)
of '\"':
result = parseStr(buf, c)
of '[':
result = parseCClass(buf, c)
of '.':
inc(c.pos)
result = cclassExpr({'\1'..'\xFF'}) # - {'\L'})
of '(':
inc(c.pos) # skip (
var isCapture = reNoCaptures notin c.flags
if c.pos+1 < buf.len and buf[c.pos] == '?' and buf[c.pos+1] == ':':
inc c.pos, 2
isCapture = false
result = parseRegExpr(buf, c)
if getNext(buf, c) == ')': inc(c.pos)
else: error ") expected"
if isCapture:
inc c.captures
result = newCapture(result)
result.c = char c.captures
of '\\':
result = getChar(buf, c, false)
of '{':
inc(c.pos) # skip {
while c.pos < buf.len and buf[c.pos] in {' ', '\t'}: inc(c.pos)
result = parseMacroCall(buf, c)
if getNext(buf, c) == '}': inc(c.pos)
else: error "} expected"
of '*', '+', '?':
error "escape " & buf[c.pos] & " with \\"
of '$':
result = newExpr(reEnd)
inc(c.pos)
of '^':
result = newExpr(reBegin)
inc(c.pos)
else:
result = charExpr(if c.pos < buf.len: buf[c.pos] else: '\0')
inc(c.pos)
while true:
case getNext(buf, c)
of '*':
inc(c.pos)
result = starExpr(result)
of '+':
inc(c.pos)
result = plusExpr(result)
of '?':
inc(c.pos)
result = optExpr(result)
of '{':
inc(c.pos) # skip {
if getNext(buf, c) notin {'0'..'9'}:
# a macro, but do not parse it here, but later to
# keep the operator predecence:
while true: # back to {
# a single decrement might not do
# because of skipped whitespace
dec(c.pos)
if buf[c.pos] == '{': break
break
else:
var n: int
let m = parseNum(buf, c)
if getNext(buf, c) == ',':
inc(c.pos)
while c.pos < buf.len and buf[c.pos] in {' ', '\t'}: inc(c.pos)
n = parseNum(buf, c)
else:
n = m
result = mnExpr(result, m, n)
if getNext(buf, c) == '}': inc(c.pos)
else: error "} expected"
else: break
proc term(buf: string; c: var ReCtx): PRegExpr =
const
termDelim = {'\0', ':', '|', ')'} #,'/'
if getNext(buf, c) notin termDelim:
result = factor(buf, c)
while getNext(buf, c) notin termDelim:
result = catExpr(result, factor(buf, c))
else:
result = epsExpr()
proc parseRegExpr(buf: string; c: var ReCtx): PRegExpr =
result = term(buf, c)
while getNext(buf, c) == '|':
inc(c.pos)
result = altExpr(result, term(buf, c))
proc parseRegExpr*(reg: string; findMacro: MacroLookupProc;
flags: set[RegexFlag] = {}): PRegExpr =
var c: ReCtx
c.pos = 0
c.flags = flags
c.findMacro = findMacro
c.captures = 0
result = parseRegExpr(reg, c)
proc containsInvCap(r: PRegExpr; inAlt: bool): bool =
if r != nil:
result = containsInvCap(r.a, inAlt or r.kind == reAlt) or
containsInvCap(r.b, inAlt or r.kind == reAlt) or
r.kind == reCapture and inAlt
proc containsInvalidCapture*(r: PRegExpr): bool =
## When the implementation uses a DFA, captures can only be supported in
## quite a limited way: (abc)|(xyz) cannot be supported. This proc checks for
## that so a nice error can be generated.
result = containsInvCap(r, false)