-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregish.py
More file actions
389 lines (300 loc) · 9.97 KB
/
regish.py
File metadata and controls
389 lines (300 loc) · 9.97 KB
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
import collections
import contextlib
import re
import traceback
ANY_CHR = " "
class RegEx(object):
pass
class Empty(RegEx):
def match(self, string, index):
del string
yield index, ''
def __repr__(self):
return "Empty()"
RegEx.empty = Empty()
class Statement(RegEx):
def __init__(self, regex):
self._regex = regex
def match(self, string):
for m, word in self._regex.match(string, 0):
del word
if m == len(string):
return True
return False
def __repr__(self):
return "Statement( {!r} )".format(self._regex)
class Choice(RegEx):
def __init__(self, left, right):
self.left = left
self.right = right
def match(self, string, index):
yield from self.left.match(string, index)
yield from self.right.match(string, index)
def __repr__(self):
return "Choice( {!r}, {!r} )".format(self.left, self.right)
class Sequence(RegEx):
def __init__(self, left, right):
self.left = left
self.right = right
def match(self, string, index):
for i, one in self.left.match(string, index):
for j, two in self.right.match(string, i):
yield j, one + two
def __repr__(self):
return "Sequence( {!r}, {!r} )".format(self.left, self.right)
class Repetition(RegEx):
def __init__(self, internal, at_least_one):
self._internal = internal
self._at_least_one = at_least_one
def match(self, string, index, *, skip_num_check=False):
zero_width = False
for i, start in self._internal.match(string, index):
if i != index:
for j, more in self.match(string, i, skip_num_check=True):
yield j, start + more
else:
zero_width = True
if skip_num_check or not self._at_least_one or zero_width:
yield index, ''
def __repr__(self):
return "Repetition{}( {!r} )".format(
'+' if self._at_least_one else '*',
self._internal,
)
class Symbol(RegEx):
def __init__(self, symbol):
self._symbol = symbol
def __repr__(self):
return "Symbol( {!r} )".format(self._symbol)
def match(self, string, index):
for i, expected in enumerate(self._symbol):
try:
char = string[index + i]
except IndexError:
return
if expected == '.':
continue
if char not in {ANY_CHR, expected}:
return
yield index + len(self._symbol), string[index:index + len(self._symbol)]
class Bracket(RegEx):
def __init__(self, chars):
self._chars = chars
def __repr__(self):
return "Bracket( {!r} )".format(self._chars)
def match(self, string, index):
# shut up
try:
if string[index] == ANY_CHR or re.match(r"[{}]".format(self._chars), string[index]):
yield index + 1, string[index]
except IndexError:
pass
class Backref(RegEx):
def __init__(self, capture):
self._capture = capture
def __repr__(self):
return "Backref()"
def match(self, string, index):
yield from self._capture.ref_match(string, index)
class Capture(RegEx):
def __init__(self, regex):
self._regex = regex
self._shapes = collections.deque()
def __repr__(self):
return "Capture( {!r} )".format(self._regex)
@staticmethod
def _combine(str1, str2):
"""
Combine two strings, respecting ANY_CHR.
If the strings are incompatible, return None
"""
if len(str1) != len(str2):
return
new_word = collections.deque()
for a, b in zip(str1, str2):
if a == b or b == ANY_CHR:
new_word.append(a)
elif a == ANY_CHR:
new_word.append(b)
else:
return
return ''.join(new_word)
def match(self, string, index):
yield from self._match(string, index)
def _match(self, string, index, offset=0):
for i, word in self._regex.match(string, index):
with self._push(word):
yield i + offset, word
def ref_match(self, string, index):
shape = self._shapes[-1]
value = string[index: index + len(shape)]
string = self._combine(shape, value)
if string is None:
return
yield from self._match(string, 0, index)
@contextlib.contextmanager
def _push(self, value):
self._shapes.append(value)
yield
self._shapes.pop()
class Optional(RegEx):
def __init__(self, regex):
self._regex = regex
def __repr__(self):
return "Optional( {!r} )".format(self._regex)
def match(self, string, index):
yield from self._regex.match(string, index)
yield index, ''
# ---
class RegExParser:
def __init__(self, input_):
self._input = input_
self._captures = []
def parse(self):
self._ptr = 0
return self._statement()
# Recursive descent parsing internals.
def _peek(self):
return self._input[self._ptr]
def _eat(self, character):
if self._peek() == character:
self._ptr += 1
else:
raise Exception("Expected: {}; got: {}".format(character, self._peek()))
def _next(self):
c = self._peek()
self._eat(c)
return c
def _more(self):
return self._ptr < len(self._input)
# Regular expression term types.
def _statement(self):
return Statement(self._regex())
def _regex(self):
term = self._term()
if self._more() and self._peek() == '|':
self._eat('|')
regex = self._regex()
return Choice(term, regex)
return term
def _term(self):
factor = RegEx.empty
while self._more() and self._peek() != ')' and self._peek() != '|':
nextFactor = self._factor()
factor = Sequence(factor, nextFactor)
return factor
def _factor(self):
base = self._base()
while self._more() and self._peek() == '*':
self._eat('*')
base = Repetition(base, False)
while self._more() and self._peek() == '+':
self._eat('+')
base = Repetition(base, True)
while self._more() and self._peek() == '?':
self._eat('?')
base = Optional(base)
return base
def _bracket(self):
agg = []
while self._more() and self._peek() != ']':
agg.append(self._next())
return Bracket(''.join(agg))
def _base(self):
peek = self._peek()
if peek == '(':
self._eat('(')
r = self._regex()
self._eat(')')
capture = Capture(r)
self._captures.append(capture)
return capture
if peek == '[':
self._eat('[')
r = self._bracket()
self._eat(']')
return r
if peek == '\\':
self._eat('\\')
num = int(self._next())
if num > len(self._captures) or num < 1:
raise Exception("invalid group reference {}".format(num))
return Backref(self._captures[num - 1])
return Symbol(self._next())
def compile_re(regex):
return RegExParser(regex).parse()
# ===
if __name__ == '__main__':
af = '\x1b[38;5;{}m'.format
clear = '\x1b[0m'
def match(regex, string, expected=True):
msg = "{{}}{!r} {}~ {!r}: {{}}!".format(regex, "=" if expected else "≠", string).format
try:
compiled_regex = compile_re(regex)
if compiled_regex.match(string) == expected:
print(msg(af(2), "Success"), end='')
print(clear)
else:
print(msg(af(3), "Failure"), end='')
print(clear)
except Exception:
print(msg(af(1), "Error"))
traceback.print_exc()
print(clear)
def run_tests():
match(r"abc", "abc")
match(r"abc", "def", False)
match(r"abc", " ")
match(r"abc", "a ")
match(r"abc", " b ")
match(r"abc", " c")
match(r"abc", " e ", False)
match(r"abcd", "abc", False)
match(r"(abc)", "abc")
match(r"a.c", "ab ")
match(r"a.c", "a c")
match(r"abc|def", "abc")
match(r"abc|def", "def")
match(r"abc|def", " c")
match(r"abc|def", "d ")
match(r"abc|def", "abf", False)
match(r"abc|def", "a f", False)
match(r"a[nuts]c", "a c")
match(r"a[nuts]c", "at ")
match(r"a[n-s]c", "a c")
match(r"a[n-s]c", " pc")
match(r"a*c", "aac")
match(r"a*c", "a c")
match(r"a*c", "a ")
match(r"a*c", " ac")
match(r"a*c", " c")
match(r"a*c", " ")
match(r"()*abc", " ")
match(r"()+abc", " ")
match(r"[nuts]*", " ")
match(r"[nuts]*", "stu")
match(r"[nuts]+", "tun")
match(r"[abc]*[nuts]+yz", "tyz")
match(r"[abc]*[nuts]+yz", "ayz", False)
match(r"[^nuts]*", "abc")
match(r"([^mc]|mm|cc)*", " f")
match(r"([^mc]|mm|cc)*", " fm", False)
match(r"([^mc]|mm)*", "xyz")
match(r"([ab])\1*", "aaaaa")
match(r"([ab])\1*", "bbbbb")
match(r"([ab])\1*", "babab", False)
match(r"([ab])\1*", "b b b")
match(r"([ab])\1*", "b a b", False)
match(r"([ab])\1*", " a b ", False)
match(r"([ab])\1*", " c ", False)
match(r"([ab]+)q\1", " qab", False)
match(r"([ab]+)q\1", " b qa a")
match(r"([ab]+)\1", " ba ")
match(r"([ab]+)\1", " ", False)
match(r"(ab|cd)\1", "a d", False)
match(r"(ab|cd)\1+", "a d ", False)
match(r"a?b?", "a")
match(r"a?b?", "b")
match(r"a?b?", "c", False)
match(r"a?", " ")
run_tests()