1 | n/a | # |
---|
2 | n/a | # Secret Labs' Regular Expression Engine |
---|
3 | n/a | # |
---|
4 | n/a | # convert template to internal format |
---|
5 | n/a | # |
---|
6 | n/a | # Copyright (c) 1997-2001 by Secret Labs AB. All rights reserved. |
---|
7 | n/a | # |
---|
8 | n/a | # See the sre.py file for information on usage and redistribution. |
---|
9 | n/a | # |
---|
10 | n/a | |
---|
11 | n/a | """Internal support module for sre""" |
---|
12 | n/a | |
---|
13 | n/a | import _sre |
---|
14 | n/a | import sre_parse |
---|
15 | n/a | from sre_constants import * |
---|
16 | n/a | |
---|
17 | n/a | assert _sre.MAGIC == MAGIC, "SRE module mismatch" |
---|
18 | n/a | |
---|
19 | n/a | _LITERAL_CODES = {LITERAL, NOT_LITERAL} |
---|
20 | n/a | _REPEATING_CODES = {REPEAT, MIN_REPEAT, MAX_REPEAT} |
---|
21 | n/a | _SUCCESS_CODES = {SUCCESS, FAILURE} |
---|
22 | n/a | _ASSERT_CODES = {ASSERT, ASSERT_NOT} |
---|
23 | n/a | |
---|
24 | n/a | # Sets of lowercase characters which have the same uppercase. |
---|
25 | n/a | _equivalences = ( |
---|
26 | n/a | # LATIN SMALL LETTER I, LATIN SMALL LETTER DOTLESS I |
---|
27 | n/a | (0x69, 0x131), # iı |
---|
28 | n/a | # LATIN SMALL LETTER S, LATIN SMALL LETTER LONG S |
---|
29 | n/a | (0x73, 0x17f), # sſ |
---|
30 | n/a | # MICRO SIGN, GREEK SMALL LETTER MU |
---|
31 | n/a | (0xb5, 0x3bc), # µμ |
---|
32 | n/a | # COMBINING GREEK YPOGEGRAMMENI, GREEK SMALL LETTER IOTA, GREEK PROSGEGRAMMENI |
---|
33 | n/a | (0x345, 0x3b9, 0x1fbe), # \u0345ιι |
---|
34 | n/a | # GREEK SMALL LETTER IOTA WITH DIALYTIKA AND TONOS, GREEK SMALL LETTER IOTA WITH DIALYTIKA AND OXIA |
---|
35 | n/a | (0x390, 0x1fd3), # Îá¿ |
---|
36 | n/a | # GREEK SMALL LETTER UPSILON WITH DIALYTIKA AND TONOS, GREEK SMALL LETTER UPSILON WITH DIALYTIKA AND OXIA |
---|
37 | n/a | (0x3b0, 0x1fe3), # ΰΰ |
---|
38 | n/a | # GREEK SMALL LETTER BETA, GREEK BETA SYMBOL |
---|
39 | n/a | (0x3b2, 0x3d0), # Î²Ï |
---|
40 | n/a | # GREEK SMALL LETTER EPSILON, GREEK LUNATE EPSILON SYMBOL |
---|
41 | n/a | (0x3b5, 0x3f5), # εϵ |
---|
42 | n/a | # GREEK SMALL LETTER THETA, GREEK THETA SYMBOL |
---|
43 | n/a | (0x3b8, 0x3d1), # Î¸Ï |
---|
44 | n/a | # GREEK SMALL LETTER KAPPA, GREEK KAPPA SYMBOL |
---|
45 | n/a | (0x3ba, 0x3f0), # κϰ |
---|
46 | n/a | # GREEK SMALL LETTER PI, GREEK PI SYMBOL |
---|
47 | n/a | (0x3c0, 0x3d6), # ÏÏ |
---|
48 | n/a | # GREEK SMALL LETTER RHO, GREEK RHO SYMBOL |
---|
49 | n/a | (0x3c1, 0x3f1), # Ïϱ |
---|
50 | n/a | # GREEK SMALL LETTER FINAL SIGMA, GREEK SMALL LETTER SIGMA |
---|
51 | n/a | (0x3c2, 0x3c3), # ÏÏ |
---|
52 | n/a | # GREEK SMALL LETTER PHI, GREEK PHI SYMBOL |
---|
53 | n/a | (0x3c6, 0x3d5), # ÏÏ |
---|
54 | n/a | # LATIN SMALL LETTER S WITH DOT ABOVE, LATIN SMALL LETTER LONG S WITH DOT ABOVE |
---|
55 | n/a | (0x1e61, 0x1e9b), # ṡẠ|
---|
56 | n/a | # LATIN SMALL LIGATURE LONG S T, LATIN SMALL LIGATURE ST |
---|
57 | n/a | (0xfb05, 0xfb06), # ï¬
ï¬ |
---|
58 | n/a | ) |
---|
59 | n/a | |
---|
60 | n/a | # Maps the lowercase code to lowercase codes which have the same uppercase. |
---|
61 | n/a | _ignorecase_fixes = {i: tuple(j for j in t if i != j) |
---|
62 | n/a | for t in _equivalences for i in t} |
---|
63 | n/a | |
---|
64 | n/a | def _compile(code, pattern, flags): |
---|
65 | n/a | # internal: compile a (sub)pattern |
---|
66 | n/a | emit = code.append |
---|
67 | n/a | _len = len |
---|
68 | n/a | LITERAL_CODES = _LITERAL_CODES |
---|
69 | n/a | REPEATING_CODES = _REPEATING_CODES |
---|
70 | n/a | SUCCESS_CODES = _SUCCESS_CODES |
---|
71 | n/a | ASSERT_CODES = _ASSERT_CODES |
---|
72 | n/a | if (flags & SRE_FLAG_IGNORECASE and |
---|
73 | n/a | not (flags & SRE_FLAG_LOCALE) and |
---|
74 | n/a | flags & SRE_FLAG_UNICODE and |
---|
75 | n/a | not (flags & SRE_FLAG_ASCII)): |
---|
76 | n/a | fixes = _ignorecase_fixes |
---|
77 | n/a | else: |
---|
78 | n/a | fixes = None |
---|
79 | n/a | for op, av in pattern: |
---|
80 | n/a | if op in LITERAL_CODES: |
---|
81 | n/a | if flags & SRE_FLAG_IGNORECASE: |
---|
82 | n/a | lo = _sre.getlower(av, flags) |
---|
83 | n/a | if fixes and lo in fixes: |
---|
84 | n/a | emit(IN_IGNORE) |
---|
85 | n/a | skip = _len(code); emit(0) |
---|
86 | n/a | if op is NOT_LITERAL: |
---|
87 | n/a | emit(NEGATE) |
---|
88 | n/a | for k in (lo,) + fixes[lo]: |
---|
89 | n/a | emit(LITERAL) |
---|
90 | n/a | emit(k) |
---|
91 | n/a | emit(FAILURE) |
---|
92 | n/a | code[skip] = _len(code) - skip |
---|
93 | n/a | else: |
---|
94 | n/a | emit(OP_IGNORE[op]) |
---|
95 | n/a | emit(lo) |
---|
96 | n/a | else: |
---|
97 | n/a | emit(op) |
---|
98 | n/a | emit(av) |
---|
99 | n/a | elif op is IN: |
---|
100 | n/a | if flags & SRE_FLAG_IGNORECASE: |
---|
101 | n/a | emit(OP_IGNORE[op]) |
---|
102 | n/a | def fixup(literal, flags=flags): |
---|
103 | n/a | return _sre.getlower(literal, flags) |
---|
104 | n/a | else: |
---|
105 | n/a | emit(op) |
---|
106 | n/a | fixup = None |
---|
107 | n/a | skip = _len(code); emit(0) |
---|
108 | n/a | _compile_charset(av, flags, code, fixup, fixes) |
---|
109 | n/a | code[skip] = _len(code) - skip |
---|
110 | n/a | elif op is ANY: |
---|
111 | n/a | if flags & SRE_FLAG_DOTALL: |
---|
112 | n/a | emit(ANY_ALL) |
---|
113 | n/a | else: |
---|
114 | n/a | emit(ANY) |
---|
115 | n/a | elif op in REPEATING_CODES: |
---|
116 | n/a | if flags & SRE_FLAG_TEMPLATE: |
---|
117 | n/a | raise error("internal: unsupported template operator %r" % (op,)) |
---|
118 | n/a | elif _simple(av) and op is not REPEAT: |
---|
119 | n/a | if op is MAX_REPEAT: |
---|
120 | n/a | emit(REPEAT_ONE) |
---|
121 | n/a | else: |
---|
122 | n/a | emit(MIN_REPEAT_ONE) |
---|
123 | n/a | skip = _len(code); emit(0) |
---|
124 | n/a | emit(av[0]) |
---|
125 | n/a | emit(av[1]) |
---|
126 | n/a | _compile(code, av[2], flags) |
---|
127 | n/a | emit(SUCCESS) |
---|
128 | n/a | code[skip] = _len(code) - skip |
---|
129 | n/a | else: |
---|
130 | n/a | emit(REPEAT) |
---|
131 | n/a | skip = _len(code); emit(0) |
---|
132 | n/a | emit(av[0]) |
---|
133 | n/a | emit(av[1]) |
---|
134 | n/a | _compile(code, av[2], flags) |
---|
135 | n/a | code[skip] = _len(code) - skip |
---|
136 | n/a | if op is MAX_REPEAT: |
---|
137 | n/a | emit(MAX_UNTIL) |
---|
138 | n/a | else: |
---|
139 | n/a | emit(MIN_UNTIL) |
---|
140 | n/a | elif op is SUBPATTERN: |
---|
141 | n/a | group, add_flags, del_flags, p = av |
---|
142 | n/a | if group: |
---|
143 | n/a | emit(MARK) |
---|
144 | n/a | emit((group-1)*2) |
---|
145 | n/a | # _compile_info(code, p, (flags | add_flags) & ~del_flags) |
---|
146 | n/a | _compile(code, p, (flags | add_flags) & ~del_flags) |
---|
147 | n/a | if group: |
---|
148 | n/a | emit(MARK) |
---|
149 | n/a | emit((group-1)*2+1) |
---|
150 | n/a | elif op in SUCCESS_CODES: |
---|
151 | n/a | emit(op) |
---|
152 | n/a | elif op in ASSERT_CODES: |
---|
153 | n/a | emit(op) |
---|
154 | n/a | skip = _len(code); emit(0) |
---|
155 | n/a | if av[0] >= 0: |
---|
156 | n/a | emit(0) # look ahead |
---|
157 | n/a | else: |
---|
158 | n/a | lo, hi = av[1].getwidth() |
---|
159 | n/a | if lo != hi: |
---|
160 | n/a | raise error("look-behind requires fixed-width pattern") |
---|
161 | n/a | emit(lo) # look behind |
---|
162 | n/a | _compile(code, av[1], flags) |
---|
163 | n/a | emit(SUCCESS) |
---|
164 | n/a | code[skip] = _len(code) - skip |
---|
165 | n/a | elif op is CALL: |
---|
166 | n/a | emit(op) |
---|
167 | n/a | skip = _len(code); emit(0) |
---|
168 | n/a | _compile(code, av, flags) |
---|
169 | n/a | emit(SUCCESS) |
---|
170 | n/a | code[skip] = _len(code) - skip |
---|
171 | n/a | elif op is AT: |
---|
172 | n/a | emit(op) |
---|
173 | n/a | if flags & SRE_FLAG_MULTILINE: |
---|
174 | n/a | av = AT_MULTILINE.get(av, av) |
---|
175 | n/a | if flags & SRE_FLAG_LOCALE: |
---|
176 | n/a | av = AT_LOCALE.get(av, av) |
---|
177 | n/a | elif (flags & SRE_FLAG_UNICODE) and not (flags & SRE_FLAG_ASCII): |
---|
178 | n/a | av = AT_UNICODE.get(av, av) |
---|
179 | n/a | emit(av) |
---|
180 | n/a | elif op is BRANCH: |
---|
181 | n/a | emit(op) |
---|
182 | n/a | tail = [] |
---|
183 | n/a | tailappend = tail.append |
---|
184 | n/a | for av in av[1]: |
---|
185 | n/a | skip = _len(code); emit(0) |
---|
186 | n/a | # _compile_info(code, av, flags) |
---|
187 | n/a | _compile(code, av, flags) |
---|
188 | n/a | emit(JUMP) |
---|
189 | n/a | tailappend(_len(code)); emit(0) |
---|
190 | n/a | code[skip] = _len(code) - skip |
---|
191 | n/a | emit(FAILURE) # end of branch |
---|
192 | n/a | for tail in tail: |
---|
193 | n/a | code[tail] = _len(code) - tail |
---|
194 | n/a | elif op is CATEGORY: |
---|
195 | n/a | emit(op) |
---|
196 | n/a | if flags & SRE_FLAG_LOCALE: |
---|
197 | n/a | av = CH_LOCALE[av] |
---|
198 | n/a | elif (flags & SRE_FLAG_UNICODE) and not (flags & SRE_FLAG_ASCII): |
---|
199 | n/a | av = CH_UNICODE[av] |
---|
200 | n/a | emit(av) |
---|
201 | n/a | elif op is GROUPREF: |
---|
202 | n/a | if flags & SRE_FLAG_IGNORECASE: |
---|
203 | n/a | emit(OP_IGNORE[op]) |
---|
204 | n/a | else: |
---|
205 | n/a | emit(op) |
---|
206 | n/a | emit(av-1) |
---|
207 | n/a | elif op is GROUPREF_EXISTS: |
---|
208 | n/a | emit(op) |
---|
209 | n/a | emit(av[0]-1) |
---|
210 | n/a | skipyes = _len(code); emit(0) |
---|
211 | n/a | _compile(code, av[1], flags) |
---|
212 | n/a | if av[2]: |
---|
213 | n/a | emit(JUMP) |
---|
214 | n/a | skipno = _len(code); emit(0) |
---|
215 | n/a | code[skipyes] = _len(code) - skipyes + 1 |
---|
216 | n/a | _compile(code, av[2], flags) |
---|
217 | n/a | code[skipno] = _len(code) - skipno |
---|
218 | n/a | else: |
---|
219 | n/a | code[skipyes] = _len(code) - skipyes + 1 |
---|
220 | n/a | else: |
---|
221 | n/a | raise error("internal: unsupported operand type %r" % (op,)) |
---|
222 | n/a | |
---|
223 | n/a | def _compile_charset(charset, flags, code, fixup=None, fixes=None): |
---|
224 | n/a | # compile charset subprogram |
---|
225 | n/a | emit = code.append |
---|
226 | n/a | for op, av in _optimize_charset(charset, fixup, fixes): |
---|
227 | n/a | emit(op) |
---|
228 | n/a | if op is NEGATE: |
---|
229 | n/a | pass |
---|
230 | n/a | elif op is LITERAL: |
---|
231 | n/a | emit(av) |
---|
232 | n/a | elif op is RANGE or op is RANGE_IGNORE: |
---|
233 | n/a | emit(av[0]) |
---|
234 | n/a | emit(av[1]) |
---|
235 | n/a | elif op is CHARSET: |
---|
236 | n/a | code.extend(av) |
---|
237 | n/a | elif op is BIGCHARSET: |
---|
238 | n/a | code.extend(av) |
---|
239 | n/a | elif op is CATEGORY: |
---|
240 | n/a | if flags & SRE_FLAG_LOCALE: |
---|
241 | n/a | emit(CH_LOCALE[av]) |
---|
242 | n/a | elif (flags & SRE_FLAG_UNICODE) and not (flags & SRE_FLAG_ASCII): |
---|
243 | n/a | emit(CH_UNICODE[av]) |
---|
244 | n/a | else: |
---|
245 | n/a | emit(av) |
---|
246 | n/a | else: |
---|
247 | n/a | raise error("internal: unsupported set operator %r" % (op,)) |
---|
248 | n/a | emit(FAILURE) |
---|
249 | n/a | |
---|
250 | n/a | def _optimize_charset(charset, fixup, fixes): |
---|
251 | n/a | # internal: optimize character set |
---|
252 | n/a | out = [] |
---|
253 | n/a | tail = [] |
---|
254 | n/a | charmap = bytearray(256) |
---|
255 | n/a | for op, av in charset: |
---|
256 | n/a | while True: |
---|
257 | n/a | try: |
---|
258 | n/a | if op is LITERAL: |
---|
259 | n/a | if fixup: |
---|
260 | n/a | lo = fixup(av) |
---|
261 | n/a | charmap[lo] = 1 |
---|
262 | n/a | if fixes and lo in fixes: |
---|
263 | n/a | for k in fixes[lo]: |
---|
264 | n/a | charmap[k] = 1 |
---|
265 | n/a | else: |
---|
266 | n/a | charmap[av] = 1 |
---|
267 | n/a | elif op is RANGE: |
---|
268 | n/a | r = range(av[0], av[1]+1) |
---|
269 | n/a | if fixup: |
---|
270 | n/a | r = map(fixup, r) |
---|
271 | n/a | if fixup and fixes: |
---|
272 | n/a | for i in r: |
---|
273 | n/a | charmap[i] = 1 |
---|
274 | n/a | if i in fixes: |
---|
275 | n/a | for k in fixes[i]: |
---|
276 | n/a | charmap[k] = 1 |
---|
277 | n/a | else: |
---|
278 | n/a | for i in r: |
---|
279 | n/a | charmap[i] = 1 |
---|
280 | n/a | elif op is NEGATE: |
---|
281 | n/a | out.append((op, av)) |
---|
282 | n/a | else: |
---|
283 | n/a | tail.append((op, av)) |
---|
284 | n/a | except IndexError: |
---|
285 | n/a | if len(charmap) == 256: |
---|
286 | n/a | # character set contains non-UCS1 character codes |
---|
287 | n/a | charmap += b'\0' * 0xff00 |
---|
288 | n/a | continue |
---|
289 | n/a | # Character set contains non-BMP character codes. |
---|
290 | n/a | # There are only two ranges of cased non-BMP characters: |
---|
291 | n/a | # 10400-1044F (Deseret) and 118A0-118DF (Warang Citi), |
---|
292 | n/a | # and for both ranges RANGE_IGNORE works. |
---|
293 | n/a | if fixup and op is RANGE: |
---|
294 | n/a | op = RANGE_IGNORE |
---|
295 | n/a | tail.append((op, av)) |
---|
296 | n/a | break |
---|
297 | n/a | |
---|
298 | n/a | # compress character map |
---|
299 | n/a | runs = [] |
---|
300 | n/a | q = 0 |
---|
301 | n/a | while True: |
---|
302 | n/a | p = charmap.find(1, q) |
---|
303 | n/a | if p < 0: |
---|
304 | n/a | break |
---|
305 | n/a | if len(runs) >= 2: |
---|
306 | n/a | runs = None |
---|
307 | n/a | break |
---|
308 | n/a | q = charmap.find(0, p) |
---|
309 | n/a | if q < 0: |
---|
310 | n/a | runs.append((p, len(charmap))) |
---|
311 | n/a | break |
---|
312 | n/a | runs.append((p, q)) |
---|
313 | n/a | if runs is not None: |
---|
314 | n/a | # use literal/range |
---|
315 | n/a | for p, q in runs: |
---|
316 | n/a | if q - p == 1: |
---|
317 | n/a | out.append((LITERAL, p)) |
---|
318 | n/a | else: |
---|
319 | n/a | out.append((RANGE, (p, q - 1))) |
---|
320 | n/a | out += tail |
---|
321 | n/a | # if the case was changed or new representation is more compact |
---|
322 | n/a | if fixup or len(out) < len(charset): |
---|
323 | n/a | return out |
---|
324 | n/a | # else original character set is good enough |
---|
325 | n/a | return charset |
---|
326 | n/a | |
---|
327 | n/a | # use bitmap |
---|
328 | n/a | if len(charmap) == 256: |
---|
329 | n/a | data = _mk_bitmap(charmap) |
---|
330 | n/a | out.append((CHARSET, data)) |
---|
331 | n/a | out += tail |
---|
332 | n/a | return out |
---|
333 | n/a | |
---|
334 | n/a | # To represent a big charset, first a bitmap of all characters in the |
---|
335 | n/a | # set is constructed. Then, this bitmap is sliced into chunks of 256 |
---|
336 | n/a | # characters, duplicate chunks are eliminated, and each chunk is |
---|
337 | n/a | # given a number. In the compiled expression, the charset is |
---|
338 | n/a | # represented by a 32-bit word sequence, consisting of one word for |
---|
339 | n/a | # the number of different chunks, a sequence of 256 bytes (64 words) |
---|
340 | n/a | # of chunk numbers indexed by their original chunk position, and a |
---|
341 | n/a | # sequence of 256-bit chunks (8 words each). |
---|
342 | n/a | |
---|
343 | n/a | # Compression is normally good: in a typical charset, large ranges of |
---|
344 | n/a | # Unicode will be either completely excluded (e.g. if only cyrillic |
---|
345 | n/a | # letters are to be matched), or completely included (e.g. if large |
---|
346 | n/a | # subranges of Kanji match). These ranges will be represented by |
---|
347 | n/a | # chunks of all one-bits or all zero-bits. |
---|
348 | n/a | |
---|
349 | n/a | # Matching can be also done efficiently: the more significant byte of |
---|
350 | n/a | # the Unicode character is an index into the chunk number, and the |
---|
351 | n/a | # less significant byte is a bit index in the chunk (just like the |
---|
352 | n/a | # CHARSET matching). |
---|
353 | n/a | |
---|
354 | n/a | charmap = bytes(charmap) # should be hashable |
---|
355 | n/a | comps = {} |
---|
356 | n/a | mapping = bytearray(256) |
---|
357 | n/a | block = 0 |
---|
358 | n/a | data = bytearray() |
---|
359 | n/a | for i in range(0, 65536, 256): |
---|
360 | n/a | chunk = charmap[i: i + 256] |
---|
361 | n/a | if chunk in comps: |
---|
362 | n/a | mapping[i // 256] = comps[chunk] |
---|
363 | n/a | else: |
---|
364 | n/a | mapping[i // 256] = comps[chunk] = block |
---|
365 | n/a | block += 1 |
---|
366 | n/a | data += chunk |
---|
367 | n/a | data = _mk_bitmap(data) |
---|
368 | n/a | data[0:0] = [block] + _bytes_to_codes(mapping) |
---|
369 | n/a | out.append((BIGCHARSET, data)) |
---|
370 | n/a | out += tail |
---|
371 | n/a | return out |
---|
372 | n/a | |
---|
373 | n/a | _CODEBITS = _sre.CODESIZE * 8 |
---|
374 | n/a | MAXCODE = (1 << _CODEBITS) - 1 |
---|
375 | n/a | _BITS_TRANS = b'0' + b'1' * 255 |
---|
376 | n/a | def _mk_bitmap(bits, _CODEBITS=_CODEBITS, _int=int): |
---|
377 | n/a | s = bits.translate(_BITS_TRANS)[::-1] |
---|
378 | n/a | return [_int(s[i - _CODEBITS: i], 2) |
---|
379 | n/a | for i in range(len(s), 0, -_CODEBITS)] |
---|
380 | n/a | |
---|
381 | n/a | def _bytes_to_codes(b): |
---|
382 | n/a | # Convert block indices to word array |
---|
383 | n/a | a = memoryview(b).cast('I') |
---|
384 | n/a | assert a.itemsize == _sre.CODESIZE |
---|
385 | n/a | assert len(a) * a.itemsize == len(b) |
---|
386 | n/a | return a.tolist() |
---|
387 | n/a | |
---|
388 | n/a | def _simple(av): |
---|
389 | n/a | # check if av is a "simple" operator |
---|
390 | n/a | lo, hi = av[2].getwidth() |
---|
391 | n/a | return lo == hi == 1 and av[2][0][0] != SUBPATTERN |
---|
392 | n/a | |
---|
393 | n/a | def _generate_overlap_table(prefix): |
---|
394 | n/a | """ |
---|
395 | n/a | Generate an overlap table for the following prefix. |
---|
396 | n/a | An overlap table is a table of the same size as the prefix which |
---|
397 | n/a | informs about the potential self-overlap for each index in the prefix: |
---|
398 | n/a | - if overlap[i] == 0, prefix[i:] can't overlap prefix[0:...] |
---|
399 | n/a | - if overlap[i] == k with 0 < k <= i, prefix[i-k+1:i+1] overlaps with |
---|
400 | n/a | prefix[0:k] |
---|
401 | n/a | """ |
---|
402 | n/a | table = [0] * len(prefix) |
---|
403 | n/a | for i in range(1, len(prefix)): |
---|
404 | n/a | idx = table[i - 1] |
---|
405 | n/a | while prefix[i] != prefix[idx]: |
---|
406 | n/a | if idx == 0: |
---|
407 | n/a | table[i] = 0 |
---|
408 | n/a | break |
---|
409 | n/a | idx = table[idx - 1] |
---|
410 | n/a | else: |
---|
411 | n/a | table[i] = idx + 1 |
---|
412 | n/a | return table |
---|
413 | n/a | |
---|
414 | n/a | def _get_literal_prefix(pattern): |
---|
415 | n/a | # look for literal prefix |
---|
416 | n/a | prefix = [] |
---|
417 | n/a | prefixappend = prefix.append |
---|
418 | n/a | prefix_skip = None |
---|
419 | n/a | for op, av in pattern.data: |
---|
420 | n/a | if op is LITERAL: |
---|
421 | n/a | prefixappend(av) |
---|
422 | n/a | elif op is SUBPATTERN: |
---|
423 | n/a | group, add_flags, del_flags, p = av |
---|
424 | n/a | if add_flags & SRE_FLAG_IGNORECASE: |
---|
425 | n/a | break |
---|
426 | n/a | prefix1, prefix_skip1, got_all = _get_literal_prefix(p) |
---|
427 | n/a | if prefix_skip is None: |
---|
428 | n/a | if group is not None: |
---|
429 | n/a | prefix_skip = len(prefix) |
---|
430 | n/a | elif prefix_skip1 is not None: |
---|
431 | n/a | prefix_skip = len(prefix) + prefix_skip1 |
---|
432 | n/a | prefix.extend(prefix1) |
---|
433 | n/a | if not got_all: |
---|
434 | n/a | break |
---|
435 | n/a | else: |
---|
436 | n/a | break |
---|
437 | n/a | else: |
---|
438 | n/a | return prefix, prefix_skip, True |
---|
439 | n/a | return prefix, prefix_skip, False |
---|
440 | n/a | |
---|
441 | n/a | def _get_charset_prefix(pattern): |
---|
442 | n/a | charset = [] # not used |
---|
443 | n/a | charsetappend = charset.append |
---|
444 | n/a | if pattern.data: |
---|
445 | n/a | op, av = pattern.data[0] |
---|
446 | n/a | if op is SUBPATTERN: |
---|
447 | n/a | group, add_flags, del_flags, p = av |
---|
448 | n/a | if p and not (add_flags & SRE_FLAG_IGNORECASE): |
---|
449 | n/a | op, av = p[0] |
---|
450 | n/a | if op is LITERAL: |
---|
451 | n/a | charsetappend((op, av)) |
---|
452 | n/a | elif op is BRANCH: |
---|
453 | n/a | c = [] |
---|
454 | n/a | cappend = c.append |
---|
455 | n/a | for p in av[1]: |
---|
456 | n/a | if not p: |
---|
457 | n/a | break |
---|
458 | n/a | op, av = p[0] |
---|
459 | n/a | if op is LITERAL: |
---|
460 | n/a | cappend((op, av)) |
---|
461 | n/a | else: |
---|
462 | n/a | break |
---|
463 | n/a | else: |
---|
464 | n/a | charset = c |
---|
465 | n/a | elif op is BRANCH: |
---|
466 | n/a | c = [] |
---|
467 | n/a | cappend = c.append |
---|
468 | n/a | for p in av[1]: |
---|
469 | n/a | if not p: |
---|
470 | n/a | break |
---|
471 | n/a | op, av = p[0] |
---|
472 | n/a | if op is LITERAL: |
---|
473 | n/a | cappend((op, av)) |
---|
474 | n/a | else: |
---|
475 | n/a | break |
---|
476 | n/a | else: |
---|
477 | n/a | charset = c |
---|
478 | n/a | elif op is IN: |
---|
479 | n/a | charset = av |
---|
480 | n/a | return charset |
---|
481 | n/a | |
---|
482 | n/a | def _compile_info(code, pattern, flags): |
---|
483 | n/a | # internal: compile an info block. in the current version, |
---|
484 | n/a | # this contains min/max pattern width, and an optional literal |
---|
485 | n/a | # prefix or a character map |
---|
486 | n/a | lo, hi = pattern.getwidth() |
---|
487 | n/a | if hi > MAXCODE: |
---|
488 | n/a | hi = MAXCODE |
---|
489 | n/a | if lo == 0: |
---|
490 | n/a | code.extend([INFO, 4, 0, lo, hi]) |
---|
491 | n/a | return |
---|
492 | n/a | # look for a literal prefix |
---|
493 | n/a | prefix = [] |
---|
494 | n/a | prefix_skip = 0 |
---|
495 | n/a | charset = [] # not used |
---|
496 | n/a | if not (flags & SRE_FLAG_IGNORECASE): |
---|
497 | n/a | # look for literal prefix |
---|
498 | n/a | prefix, prefix_skip, got_all = _get_literal_prefix(pattern) |
---|
499 | n/a | # if no prefix, look for charset prefix |
---|
500 | n/a | if not prefix: |
---|
501 | n/a | charset = _get_charset_prefix(pattern) |
---|
502 | n/a | ## if prefix: |
---|
503 | n/a | ## print("*** PREFIX", prefix, prefix_skip) |
---|
504 | n/a | ## if charset: |
---|
505 | n/a | ## print("*** CHARSET", charset) |
---|
506 | n/a | # add an info block |
---|
507 | n/a | emit = code.append |
---|
508 | n/a | emit(INFO) |
---|
509 | n/a | skip = len(code); emit(0) |
---|
510 | n/a | # literal flag |
---|
511 | n/a | mask = 0 |
---|
512 | n/a | if prefix: |
---|
513 | n/a | mask = SRE_INFO_PREFIX |
---|
514 | n/a | if prefix_skip is None and got_all: |
---|
515 | n/a | mask = mask | SRE_INFO_LITERAL |
---|
516 | n/a | elif charset: |
---|
517 | n/a | mask = mask | SRE_INFO_CHARSET |
---|
518 | n/a | emit(mask) |
---|
519 | n/a | # pattern length |
---|
520 | n/a | if lo < MAXCODE: |
---|
521 | n/a | emit(lo) |
---|
522 | n/a | else: |
---|
523 | n/a | emit(MAXCODE) |
---|
524 | n/a | prefix = prefix[:MAXCODE] |
---|
525 | n/a | emit(min(hi, MAXCODE)) |
---|
526 | n/a | # add literal prefix |
---|
527 | n/a | if prefix: |
---|
528 | n/a | emit(len(prefix)) # length |
---|
529 | n/a | if prefix_skip is None: |
---|
530 | n/a | prefix_skip = len(prefix) |
---|
531 | n/a | emit(prefix_skip) # skip |
---|
532 | n/a | code.extend(prefix) |
---|
533 | n/a | # generate overlap table |
---|
534 | n/a | code.extend(_generate_overlap_table(prefix)) |
---|
535 | n/a | elif charset: |
---|
536 | n/a | _compile_charset(charset, flags, code) |
---|
537 | n/a | code[skip] = len(code) - skip |
---|
538 | n/a | |
---|
539 | n/a | def isstring(obj): |
---|
540 | n/a | return isinstance(obj, (str, bytes)) |
---|
541 | n/a | |
---|
542 | n/a | def _code(p, flags): |
---|
543 | n/a | |
---|
544 | n/a | flags = p.pattern.flags | flags |
---|
545 | n/a | code = [] |
---|
546 | n/a | |
---|
547 | n/a | # compile info block |
---|
548 | n/a | _compile_info(code, p, flags) |
---|
549 | n/a | |
---|
550 | n/a | # compile the pattern |
---|
551 | n/a | _compile(code, p.data, flags) |
---|
552 | n/a | |
---|
553 | n/a | code.append(SUCCESS) |
---|
554 | n/a | |
---|
555 | n/a | return code |
---|
556 | n/a | |
---|
557 | n/a | def compile(p, flags=0): |
---|
558 | n/a | # internal: convert pattern list to internal format |
---|
559 | n/a | |
---|
560 | n/a | if isstring(p): |
---|
561 | n/a | pattern = p |
---|
562 | n/a | p = sre_parse.parse(p, flags) |
---|
563 | n/a | else: |
---|
564 | n/a | pattern = None |
---|
565 | n/a | |
---|
566 | n/a | code = _code(p, flags) |
---|
567 | n/a | |
---|
568 | n/a | # print(code) |
---|
569 | n/a | |
---|
570 | n/a | # map in either direction |
---|
571 | n/a | groupindex = p.pattern.groupdict |
---|
572 | n/a | indexgroup = [None] * p.pattern.groups |
---|
573 | n/a | for k, i in groupindex.items(): |
---|
574 | n/a | indexgroup[i] = k |
---|
575 | n/a | |
---|
576 | n/a | return _sre.compile( |
---|
577 | n/a | pattern, flags | p.pattern.flags, code, |
---|
578 | n/a | p.pattern.groups-1, |
---|
579 | n/a | groupindex, tuple(indexgroup) |
---|
580 | n/a | ) |
---|