-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregex.py
373 lines (307 loc) · 9.86 KB
/
regex.py
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
# encoding: utf-8
# UNDER THE MIT LICENSE
#
# Contact: kyle@lahnakoski.com
from collections import OrderedDict
from string import whitespace
from parsing_future import unichr, is_text
from imports import export
from parsing.core import add_reset_action
from parsing.enhancement import (
Char,
NotAny,
ZeroOrMore,
OneOrMore,
Optional,
Many,
Combine,
Group,
Forward,
LookAhead,
ParseEnhancement,
LookBehind,
)
from parsing.expressions import MatchFirst, And
from parsing.infix import delimited_list
from parsing.results import ParseResults, Annotation
from parsing.tokens import (
Literal,
AnyChar,
LineStart,
LineEnd,
Word,
SingleCharLiteral,
)
from parsing.utils import (
printables,
alphas,
alphanums,
nums,
hexnums,
Log,
listwrap,
regex_compile,
ParseException,
)
from parsing.whitespaces import Whitespace, NO_WHITESPACE
__all__ = ["Regex"]
def hex_to_char(t):
return Literal(unichr(int(t.value().lower().split("x")[1], 16)))
def to_range(tokens):
min_ = tokens["min"].parser_config.match
max_ = tokens["max"].parser_config.match
return Char("".join(unichr(i) for i in range(ord(min_), ord(max_) + 1)))
def to_bracket(tokens):
acc = []
for e in listwrap(tokens["body"].value()):
if isinstance(e, SingleCharLiteral):
acc.append(e.parser_config.match)
elif isinstance(e, Char):
acc.extend(e.parser_config.include)
else:
Log.error("programmer error")
if tokens["negate"]:
return Char(exclude=acc)
else:
return Char(acc)
num_captures = 0
def _reset():
global num_captures
num_captures = 0
add_reset_action(_reset)
def INC():
global num_captures
num_captures += 1
def DEC():
global num_captures
num_captures -= 1
def name_token(tokens):
with NO_WHITESPACE:
n = tokens["name"]
v = tokens["value"]
if not n:
n = str(num_captures)
return Combine(v).set_token_name(n)
def repeat(tokens):
if tokens.length() == 1:
return tokens.value()
try:
operand, operator = tokens
except Exception as cause:
Log.error("not expected", cause=cause)
mode = operator["mode"]
if not mode:
if operator["exact"]:
return Many(operand, NO_WHITESPACE, exact=int(operator["exact"]))
else:
return Many(
operand,
NO_WHITESPACE,
min_match=int(operator["min"]),
max_match=int(operator["max"]),
)
elif mode in "*?":
return ZeroOrMore(operand, NO_WHITESPACE)
elif mode in "+?":
return OneOrMore(operand, NO_WHITESPACE)
elif mode == "?":
return Optional(operand, NO_WHITESPACE)
else:
Log.error("not expected")
NO_WHITESPACE.use()
#########################################################################################
# SQUARE BRACKETS
any_whitechar = Literal("\\s") / (lambda: Char(whitespace))
not_whitechar = Literal("\\S") / (lambda: Char(exclude=whitespace))
any_wordchar = Literal("\\w") / (lambda: Char(alphanums + "_"))
not_wordchar = Literal("\\W") / (lambda: Char(exclude=alphanums + "_"))
any_digitchar = Literal("\\d") / (lambda: Char(nums))
not_digitchar = Literal("\\D") / (lambda: Char(exclude=nums))
bs_char = Literal("\\\\") / (lambda: Literal("\\"))
tab_char = Literal("\\t") / (lambda: Literal("\t"))
CR = Literal("\\n") / (lambda: Literal("\n"))
LF = Literal("\\r") / (lambda: Literal("\r"))
any_char = Literal(".") / (lambda: AnyChar())
macro = (
any_whitechar
| any_wordchar
| any_digitchar
| not_digitchar
| not_wordchar
| not_whitechar
| CR
| LF
| any_char
| bs_char
| tab_char
)
escaped_char = (~macro + Combine("\\" + AnyChar())) / (lambda t: Literal(t.value()[1]))
plain_char = Char(exclude=r"\]") / (lambda t: Literal(t.value()))
escaped_hex = (
Combine(
(Literal("\\0x") | Literal("\\x") | Literal("\\X")) # lookup literals is faster
+ OneOrMore(Char(hexnums), NO_WHITESPACE)
)
/ hex_to_char
)
escaped_oct = Combine(
Literal("\\0") + OneOrMore(Char("01234567"), NO_WHITESPACE)
) / (lambda t: Literal(unichr(int(t.value()[2:], 8))))
single_char = escaped_hex | escaped_oct | escaped_char | plain_char
range_char = Group(single_char("min") + "-" + single_char("max")) / to_range
brackets = (
"["
+ Optional("^", NO_WHITESPACE)("negate")
+ OneOrMore(Group(range_char | single_char | macro)("body"), NO_WHITESPACE)
+ "]"
) / to_bracket
#########################################################################################
# REGEX
regex = Forward()
line_start = Literal("^") / (lambda: LineStart())
line_end = Literal("$") / (lambda: LineEnd())
word_edge = Literal("\\b") / (lambda: NotAny(any_wordchar))
simple_char = Word(
printables, exclude=r".^$*+{}[]\|()"
) / (lambda t: Literal(t.value()))
esc_char = ("\\" + AnyChar()) / (lambda t: Literal(t.value()[1]))
with Whitespace():
# ALLOW SPACES IN THE RANGE
repetition = (
Word(nums)("exact") + "}"
| Word(nums)("min") + "," + Word(nums)("max") + "}"
| Word(nums)("min") + "," + "}"
| "," + Word(nums)("max") + "}"
)
repetition = Group(
"{" + repetition | (Literal("*?") | Literal("+?") | Char("*+?"))("mode")
)
LB = Char("(")
ahead = ("(?=" + regex + ")") / (lambda t: LookAhead(t["value"]))
not_ahead = ("(?!" + regex + ")") / (lambda t: NotAny(t["value"]))
behind = ("(?<=" + regex + ")") / (lambda t: LookBehind(t["value"]))
not_behind = ("(?<!" + regex + ")") / (lambda t: Log.error("not supported"))
non_capture = ("(?:" + regex + ")") / (lambda t: t["value"])
# conditional = ("(?" + try_match + "|" + else_match + ")")
# recursive = ("(?R)")
# TODO: match previous capture (3)
named = (
(Literal("(?P<") / INC + Word(alphanums + "_")("name") + ">" + regex + ")")
/ name_token
/ DEC
)
group = ((LB / INC) + regex + ")") / name_token / DEC
term = (
macro
| simple_char
| esc_char
| word_edge
| brackets
| ahead
| not_ahead
| behind
| not_behind
| non_capture
| named
| group
)
more = (term + Optional(repetition, NO_WHITESPACE)) / repeat
sequence = OneOrMore(more, NO_WHITESPACE) / (lambda t: And(t, NO_WHITESPACE))
regex << (
delimited_list(sequence, separator="|").set_token_name("value")
/ (lambda t: MatchFirst(listwrap(t.value())).streamline())
).streamline()
regex = regex.finalize()
parameters = (
"\\" + Char(alphanums)("name") | "\\g<" + Word(alphas, alphanums)("name") + ">"
) / (lambda t: t["name"])
NO_WHITESPACE.release()
class Regex(ParseEnhancement):
"""
Converter to concatenate all matching tokens to a single string.
"""
__slots__ = ["regex"]
def __init__(self, pattern):
"""
:param pattern: THE REGEX PATTERN
:param asGroupList: RETURN A LIST OF CAPTURED GROUPS /1, /2, /3, ...
"""
parsed = regex.parse_string(pattern)
ParseEnhancement.__init__(self, parsed.value().streamline())
# WE ASSUME IT IS SAFE TO ASSIGN regex (NO SERIOUS BACKTRACKING PROBLEMS)
self.streamlined = True
self.regex = regex_compile(pattern)
def copy(self):
output = ParseEnhancement.copy(self)
output.regex = self.regex
return output
def capture_groups(self):
"""
ADD A SPECIAL PARSE ACTION TO PROVIDE THE NUMBERED (eg "1") AND
NAMED GROUPS (eg (?P<name>...)
"""
def group_list(tokens):
start, end = tokens.start, tokens.end
# RE-MATCH :(
sub_string = tokens.tokens[0]
found = tokens.type.regex.match(sub_string)
lookup = dict(reversed(p) for p in found.re.groupindex.items())
ann = []
pe = 0
for i, (s, e) in enumerate(found.regs[1:], start=1):
if s == -1:
continue # NOT FOUND
g = sub_string[s:e]
if pe <= s:
ann.append(g)
pe = e
ii = chr(i + ord("0"))
ann.append(Annotation(ii, s + start, e + start, [g]))
n = lookup.get(i)
if n:
ann.append(Annotation(n, s + start, e + start, [g]))
return ParseResults(_plain_group, start, end, ann, [])
return self / group_list
def sub(self, replacement):
# MIMIC re.sub
if is_text(replacement):
# USE PYTHON REGEX
def pa(tokens):
return tokens.type.regex.sub(replacement, tokens[0])
output = self / pa
return output
else:
# A FUNCTION
def pf(tokens):
regex_result = tokens.type.regex.match(tokens.tokens[0])
return replacement(regex_result)
return self / pf
def parse_impl(self, string, start, do_actions=True):
found = self.regex.match(string, start)
if found:
return ParseResults(self, start, found.end(), [found[0]], [])
else:
raise ParseException(self, start, string)
def streamline(self):
# WE RUN THE DANGER OF MAKING PATHELOGICAL REGEX, SO WE DO NOT TRY
if self.streamlined:
return self
expr = self.expr.streamline()
if expr is self:
self.streamlined = True
return self
output = self.copy()
output.expr = expr
output.streamlined = True
return output
def expecting(self):
return OrderedDict((k, [self]) for k in self.expr.expecting().keys())
def min_length(self):
return self.expr.min_length()
def __regex__(self):
if self.regex:
return "|", self.regex.pattern
else:
return self.expr.__regex__()
_plain_group = Group(None)
export("parsing.core", "regex_parameters", parameters)