Debian Codegraph @

lib-python/3/re/_compiler.py

raw symbols SCIP
1 #
2 # Secret Labs' Regular Expression Engine
3 #
4 # convert template to internal format
5 #
6 # Copyright (c) 1997-2001 by Secret Labs AB. All rights reserved.
7 #
8 # See the __init__.py file for information on usage and redistribution.
9 #
10
11 """Internal support module for sre"""
12
13 import _sre
14 from . import _parser
15 from ._constants import *
16 from ._casefix import _EXTRA_CASES
17
18 assert _sre.MAGIC == MAGIC, "SRE module mismatch"
19
20 _LITERAL_CODES = {LITERAL, NOT_LITERAL}
21 _SUCCESS_CODES = {SUCCESS, FAILURE}
22 _ASSERT_CODES = {ASSERT, ASSERT_NOT}
23 _UNIT_CODES = _LITERAL_CODES | {ANY, IN}
24
25 _REPEATING_CODES = {
26 MIN_REPEAT: (REPEAT, MIN_UNTIL, MIN_REPEAT_ONE),
27 MAX_REPEAT: (REPEAT, MAX_UNTIL, REPEAT_ONE),
28 POSSESSIVE_REPEAT: (POSSESSIVE_REPEAT, SUCCESS, POSSESSIVE_REPEAT_ONE),
29 }
30
31 def _combine_flags(flags, add_flags, del_flags,
32 TYPE_FLAGS=_parser.TYPE_FLAGS):
33 if add_flags & TYPE_FLAGS:
34 flags &= ~TYPE_FLAGS
35 return (flags | add_flags) & ~del_flags
36
37 def _compile(code, pattern, flags):
38 # internal: compile a (sub)pattern
39 emit = code.append
40 _len = len
41 LITERAL_CODES = _LITERAL_CODES
42 REPEATING_CODES = _REPEATING_CODES
43 SUCCESS_CODES = _SUCCESS_CODES
44 ASSERT_CODES = _ASSERT_CODES
45 iscased = None
46 tolower = None
47 fixes = None
48 if flags & SRE_FLAG_IGNORECASE and not flags & SRE_FLAG_LOCALE:
49 if flags & SRE_FLAG_UNICODE:
50 iscased = _sre.unicode_iscased
51 tolower = _sre.unicode_tolower
52 fixes = _EXTRA_CASES
53 else:
54 iscased = _sre.ascii_iscased
55 tolower = _sre.ascii_tolower
56 for op, av in pattern:
57 if op in LITERAL_CODES:
58 if not flags & SRE_FLAG_IGNORECASE:
59 emit(op)
60 emit(av)
61 elif flags & SRE_FLAG_LOCALE:
62 emit(OP_LOCALE_IGNORE[op])
63 emit(av)
64 elif not iscased(av):
65 emit(op)
66 emit(av)
67 else:
68 lo = tolower(av)
69 if not fixes: # ascii
70 emit(OP_IGNORE[op])
71 emit(lo)
72 elif lo not in fixes:
73 emit(OP_UNICODE_IGNORE[op])
74 emit(lo)
75 else:
76 emit(IN_UNI_IGNORE)
77 skip = _len(code); emit(0)
78 if op is NOT_LITERAL:
79 emit(NEGATE)
80 for k in (lo,) + fixes[lo]:
81 emit(LITERAL)
82 emit(k)
83 emit(FAILURE)
84 code[skip] = _len(code) - skip
85 elif op is IN:
86 charset, hascased = _optimize_charset(av, iscased, tolower, fixes)
87 if flags & SRE_FLAG_IGNORECASE and flags & SRE_FLAG_LOCALE:
88 emit(IN_LOC_IGNORE)
89 elif not hascased:
90 emit(IN)
91 elif not fixes: # ascii
92 emit(IN_IGNORE)
93 else:
94 emit(IN_UNI_IGNORE)
95 skip = _len(code); emit(0)
96 _compile_charset(charset, flags, code)
97 code[skip] = _len(code) - skip
98 elif op is ANY:
99 if flags & SRE_FLAG_DOTALL:
100 emit(ANY_ALL)
101 else:
102 emit(ANY)
103 elif op in REPEATING_CODES:
104 if flags & SRE_FLAG_TEMPLATE:
105 raise error("internal: unsupported template operator %r" % (op,))
106 if _simple(av[2]):
107 emit(REPEATING_CODES[op][2])
108 skip = _len(code); emit(0)
109 emit(av[0])
110 emit(av[1])
111 _compile(code, av[2], flags)
112 emit(SUCCESS)
113 code[skip] = _len(code) - skip
114 else:
115 emit(REPEATING_CODES[op][0])
116 skip = _len(code); emit(0)
117 emit(av[0])
118 emit(av[1])
119 _compile(code, av[2], flags)
120 code[skip] = _len(code) - skip
121 emit(REPEATING_CODES[op][1])
122 elif op is SUBPATTERN:
123 group, add_flags, del_flags, p = av
124 if group:
125 emit(MARK)
126 emit((group-1)*2)
127 # _compile_info(code, p, _combine_flags(flags, add_flags, del_flags))
128 _compile(code, p, _combine_flags(flags, add_flags, del_flags))
129 if group:
130 emit(MARK)
131 emit((group-1)*2+1)
132 elif op is ATOMIC_GROUP:
133 # Atomic Groups are handled by starting with an Atomic
134 # Group op code, then putting in the atomic group pattern
135 # and finally a success op code to tell any repeat
136 # operations within the Atomic Group to stop eating and
137 # pop their stack if they reach it
138 emit(ATOMIC_GROUP)
139 skip = _len(code); emit(0)
140 _compile(code, av, flags)
141 emit(SUCCESS)
142 code[skip] = _len(code) - skip
143 elif op in SUCCESS_CODES:
144 emit(op)
145 elif op in ASSERT_CODES:
146 emit(op)
147 skip = _len(code); emit(0)
148 if av[0] >= 0:
149 emit(0) # look ahead
150 else:
151 lo, hi = av[1].getwidth()
152 if lo > MAXCODE:
153 raise error("looks too much behind")
154 if lo != hi:
155 raise error("look-behind requires fixed-width pattern")
156 emit(lo) # look behind
157 _compile(code, av[1], flags)
158 emit(SUCCESS)
159 code[skip] = _len(code) - skip
160 elif op is AT:
161 emit(op)
162 if flags & SRE_FLAG_MULTILINE:
163 av = AT_MULTILINE.get(av, av)
164 if flags & SRE_FLAG_LOCALE:
165 av = AT_LOCALE.get(av, av)
166 elif flags & SRE_FLAG_UNICODE:
167 av = AT_UNICODE.get(av, av)
168 emit(av)
169 elif op is BRANCH:
170 emit(op)
171 tail = []
172 tailappend = tail.append
173 for av in av[1]:
174 skip = _len(code); emit(0)
175 # _compile_info(code, av, flags)
176 _compile(code, av, flags)
177 emit(JUMP)
178 tailappend(_len(code)); emit(0)
179 code[skip] = _len(code) - skip
180 emit(FAILURE) # end of branch
181 for tail in tail:
182 code[tail] = _len(code) - tail
183 elif op is CATEGORY:
184 emit(op)
185 if flags & SRE_FLAG_LOCALE:
186 av = CH_LOCALE[av]
187 elif flags & SRE_FLAG_UNICODE:
188 av = CH_UNICODE[av]
189 emit(av)
190 elif op is GROUPREF:
191 if not flags & SRE_FLAG_IGNORECASE:
192 emit(op)
193 elif flags & SRE_FLAG_LOCALE:
194 emit(GROUPREF_LOC_IGNORE)
195 elif not fixes: # ascii
196 emit(GROUPREF_IGNORE)
197 else:
198 emit(GROUPREF_UNI_IGNORE)
199 emit(av-1)
200 elif op is GROUPREF_EXISTS:
201 emit(op)
202 emit(av[0]-1)
203 skipyes = _len(code); emit(0)
204 _compile(code, av[1], flags)
205 if av[2]:
206 emit(JUMP)
207 skipno = _len(code); emit(0)
208 code[skipyes] = _len(code) - skipyes + 1
209 _compile(code, av[2], flags)
210 code[skipno] = _len(code) - skipno
211 else:
212 code[skipyes] = _len(code) - skipyes + 1
213 else:
214 raise error("internal: unsupported operand type %r" % (op,))
215
216 def _compile_charset(charset, flags, code):
217 # compile charset subprogram
218 emit = code.append
219 for op, av in charset:
220 emit(op)
221 if op is NEGATE:
222 pass
223 elif op is LITERAL:
224 emit(av)
225 elif op is RANGE or op is RANGE_UNI_IGNORE:
226 emit(av[0])
227 emit(av[1])
228 elif op is CHARSET:
229 code.extend(av)
230 elif op is BIGCHARSET:
231 code.extend(av)
232 elif op is CATEGORY:
233 if flags & SRE_FLAG_LOCALE:
234 emit(CH_LOCALE[av])
235 elif flags & SRE_FLAG_UNICODE:
236 emit(CH_UNICODE[av])
237 else:
238 emit(av)
239 else:
240 raise error("internal: unsupported set operator %r" % (op,))
241 emit(FAILURE)
242
243 def _optimize_charset(charset, iscased=None, fixup=None, fixes=None):
244 # internal: optimize character set
245 out = []
246 tail = []
247 charmap = bytearray(256)
248 hascased = False
249 for op, av in charset:
250 while True:
251 try:
252 if op is LITERAL:
253 if fixup:
254 lo = fixup(av)
255 charmap[lo] = 1
256 if fixes and lo in fixes:
257 for k in fixes[lo]:
258 charmap[k] = 1
259 if not hascased and iscased(av):
260 hascased = True
261 else:
262 charmap[av] = 1
263 elif op is RANGE:
264 r = range(av[0], av[1]+1)
265 if fixup:
266 if fixes:
267 for i in map(fixup, r):
268 charmap[i] = 1
269 if i in fixes:
270 for k in fixes[i]:
271 charmap[k] = 1
272 else:
273 for i in map(fixup, r):
274 charmap[i] = 1
275 if not hascased:
276 hascased = any(map(iscased, r))
277 else:
278 for i in r:
279 charmap[i] = 1
280 elif op is NEGATE:
281 out.append((op, av))
282 else:
283 tail.append((op, av))
284 except IndexError:
285 if len(charmap) == 256:
286 # character set contains non-UCS1 character codes
287 charmap += b'\0' * 0xff00
288 continue
289 # Character set contains non-BMP character codes.
290 # For range, all BMP characters in the range are already
291 # proceeded.
292 if fixup:
293 hascased = True
294 # For now, IN_UNI_IGNORE+LITERAL and
295 # IN_UNI_IGNORE+RANGE_UNI_IGNORE work for all non-BMP
296 # characters, because two characters (at least one of
297 # which is not in the BMP) match case-insensitively
298 # if and only if:
299 # 1) c1.lower() == c2.lower()
300 # 2) c1.lower() == c2 or c1.lower().upper() == c2
301 # Also, both c.lower() and c.lower().upper() are single
302 # characters for every non-BMP character.
303 if op is RANGE:
304 op = RANGE_UNI_IGNORE
305 tail.append((op, av))
306 break
307
308 # compress character map
309 runs = []
310 q = 0
311 while True:
312 p = charmap.find(1, q)
313 if p < 0:
314 break
315 if len(runs) >= 2:
316 runs = None
317 break
318 q = charmap.find(0, p)
319 if q < 0:
320 runs.append((p, len(charmap)))
321 break
322 runs.append((p, q))
323 if runs is not None:
324 # use literal/range
325 for p, q in runs:
326 if q - p == 1:
327 out.append((LITERAL, p))
328 else:
329 out.append((RANGE, (p, q - 1)))
330 out += tail
331 # if the case was changed or new representation is more compact
332 if hascased or len(out) < len(charset):
333 return out, hascased
334 # else original character set is good enough
335 return charset, hascased
336
337 # use bitmap
338 if len(charmap) == 256:
339 data = _mk_bitmap(charmap)
340 out.append((CHARSET, data))
341 out += tail
342 return out, hascased
343
344 # To represent a big charset, first a bitmap of all characters in the
345 # set is constructed. Then, this bitmap is sliced into chunks of 256
346 # characters, duplicate chunks are eliminated, and each chunk is
347 # given a number. In the compiled expression, the charset is
348 # represented by a 32-bit word sequence, consisting of one word for
349 # the number of different chunks, a sequence of 256 bytes (64 words)
350 # of chunk numbers indexed by their original chunk position, and a
351 # sequence of 256-bit chunks (8 words each).
352
353 # Compression is normally good: in a typical charset, large ranges of
354 # Unicode will be either completely excluded (e.g. if only cyrillic
355 # letters are to be matched), or completely included (e.g. if large
356 # subranges of Kanji match). These ranges will be represented by
357 # chunks of all one-bits or all zero-bits.
358
359 # Matching can be also done efficiently: the more significant byte of
360 # the Unicode character is an index into the chunk number, and the
361 # less significant byte is a bit index in the chunk (just like the
362 # CHARSET matching).
363
364 charmap = bytes(charmap) # should be hashable
365 comps = {}
366 mapping = bytearray(256)
367 block = 0
368 data = bytearray()
369 for i in range(0, 65536, 256):
370 chunk = charmap[i: i + 256]
371 if chunk in comps:
372 mapping[i // 256] = comps[chunk]
373 else:
374 mapping[i // 256] = comps[chunk] = block
375 block += 1
376 data += chunk
377 data = _mk_bitmap(data)
378 data[0:0] = [block] + _bytes_to_codes(mapping)
379 out.append((BIGCHARSET, data))
380 out += tail
381 return out, hascased
382
383 _CODEBITS = _sre.CODESIZE * 8
384 MAXCODE = (1 << _CODEBITS) - 1
385 _BITS_TRANS = b'0' + b'1' * 255
386 def _mk_bitmap(bits, _CODEBITS=_CODEBITS, _int=int):
387 s = bits.translate(_BITS_TRANS)[::-1]
388 return [_int(s[i - _CODEBITS: i], 2)
389 for i in range(len(s), 0, -_CODEBITS)]
390
391 def _bytes_to_codes(b):
392 # Convert block indices to word array
393 a = memoryview(b).cast('I')
394 assert a.itemsize == _sre.CODESIZE
395 assert len(a) * a.itemsize == len(b)
396 return a.tolist()
397
398 def _simple(p):
399 # check if this subpattern is a "simple" operator
400 if len(p) != 1:
401 return False
402 op, av = p[0]
403 if op is SUBPATTERN:
404 return av[0] is None and _simple(av[-1])
405 return op in _UNIT_CODES
406
407 def _generate_overlap_table(prefix):
408 """
409 Generate an overlap table for the following prefix.
410 An overlap table is a table of the same size as the prefix which
411 informs about the potential self-overlap for each index in the prefix:
412 - if overlap[i] == 0, prefix[i:] can't overlap prefix[0:...]
413 - if overlap[i] == k with 0 < k <= i, prefix[i-k+1:i+1] overlaps with
414 prefix[0:k]
415 """
416 table = [0] * len(prefix)
417 for i in range(1, len(prefix)):
418 idx = table[i - 1]
419 while prefix[i] != prefix[idx]:
420 if idx == 0:
421 table[i] = 0
422 break
423 idx = table[idx - 1]
424 else:
425 table[i] = idx + 1
426 return table
427
428 def _get_iscased(flags):
429 if not flags & SRE_FLAG_IGNORECASE:
430 return None
431 elif flags & SRE_FLAG_UNICODE:
432 return _sre.unicode_iscased
433 else:
434 return _sre.ascii_iscased
435
436 def _get_literal_prefix(pattern, flags):
437 # look for literal prefix
438 prefix = []
439 prefixappend = prefix.append
440 prefix_skip = None
441 iscased = _get_iscased(flags)
442 for op, av in pattern.data:
443 if op is LITERAL:
444 if iscased and iscased(av):
445 break
446 prefixappend(av)
447 elif op is SUBPATTERN:
448 group, add_flags, del_flags, p = av
449 flags1 = _combine_flags(flags, add_flags, del_flags)
450 if flags1 & SRE_FLAG_IGNORECASE and flags1 & SRE_FLAG_LOCALE:
451 break
452 prefix1, prefix_skip1, got_all = _get_literal_prefix(p, flags1)
453 if prefix_skip is None:
454 if group is not None:
455 prefix_skip = len(prefix)
456 elif prefix_skip1 is not None:
457 prefix_skip = len(prefix) + prefix_skip1
458 prefix.extend(prefix1)
459 if not got_all:
460 break
461 else:
462 break
463 else:
464 return prefix, prefix_skip, True
465 return prefix, prefix_skip, False
466
467 def _get_charset_prefix(pattern, flags):
468 while True:
469 if not pattern.data:
470 return None
471 op, av = pattern.data[0]
472 if op is not SUBPATTERN:
473 break
474 group, add_flags, del_flags, pattern = av
475 flags = _combine_flags(flags, add_flags, del_flags)
476 if flags & SRE_FLAG_IGNORECASE and flags & SRE_FLAG_LOCALE:
477 return None
478
479 iscased = _get_iscased(flags)
480 if op is LITERAL:
481 if iscased and iscased(av):
482 return None
483 return [(op, av)]
484 elif op is BRANCH:
485 charset = []
486 charsetappend = charset.append
487 for p in av[1]:
488 if not p:
489 return None
490 op, av = p[0]
491 if op is LITERAL and not (iscased and iscased(av)):
492 charsetappend((op, av))
493 else:
494 return None
495 return charset
496 elif op is IN:
497 charset = av
498 if iscased:
499 for op, av in charset:
500 if op is LITERAL:
501 if iscased(av):
502 return None
503 elif op is RANGE:
504 if av[1] > 0xffff:
505 return None
506 if any(map(iscased, range(av[0], av[1]+1))):
507 return None
508 return charset
509 return None
510
511 def _compile_info(code, pattern, flags):
512 # internal: compile an info block. in the current version,
513 # this contains min/max pattern width, and an optional literal
514 # prefix or a character map
515 lo, hi = pattern.getwidth()
516 if hi > MAXCODE:
517 hi = MAXCODE
518 if lo == 0:
519 code.extend([INFO, 4, 0, lo, hi])
520 return
521 # look for a literal prefix
522 prefix = []
523 prefix_skip = 0
524 charset = [] # not used
525 if not (flags & SRE_FLAG_IGNORECASE and flags & SRE_FLAG_LOCALE):
526 # look for literal prefix
527 prefix, prefix_skip, got_all = _get_literal_prefix(pattern, flags)
528 # if no prefix, look for charset prefix
529 if not prefix:
530 charset = _get_charset_prefix(pattern, flags)
531 ## if prefix:
532 ## print("*** PREFIX", prefix, prefix_skip)
533 ## if charset:
534 ## print("*** CHARSET", charset)
535 # add an info block
536 emit = code.append
537 emit(INFO)
538 skip = len(code); emit(0)
539 # literal flag
540 mask = 0
541 if prefix:
542 mask = SRE_INFO_PREFIX
543 if prefix_skip is None and got_all:
544 mask = mask | SRE_INFO_LITERAL
545 elif charset:
546 mask = mask | SRE_INFO_CHARSET
547 emit(mask)
548 # pattern length
549 if lo < MAXCODE:
550 emit(lo)
551 else:
552 emit(MAXCODE)
553 prefix = prefix[:MAXCODE]
554 emit(hi)
555 # add literal prefix
556 if prefix:
557 emit(len(prefix)) # length
558 if prefix_skip is None:
559 prefix_skip = len(prefix)
560 emit(prefix_skip) # skip
561 code.extend(prefix)
562 # generate overlap table
563 code.extend(_generate_overlap_table(prefix))
564 elif charset:
565 charset, hascased = _optimize_charset(charset)
566 assert not hascased
567 _compile_charset(charset, flags, code)
568 code[skip] = len(code) - skip
569
570 def isstring(obj):
571 return isinstance(obj, (str, bytes))
572
573 def _code(p, flags):
574
575 flags = p.state.flags | flags
576 code = []
577
578 # compile info block
579 _compile_info(code, p, flags)
580
581 # compile the pattern
582 _compile(code, p.data, flags)
583
584 code.append(SUCCESS)
585
586 return code
587
588 def _hex_code(code):
589 return '[%s]' % ', '.join('%#0*x' % (_sre.CODESIZE*2+2, x) for x in code)
590
591 def dis(code):
592 import sys
593
594 labels = set()
595 level = 0
596 offset_width = len(str(len(code) - 1))
597
598 def dis_(start, end):
599 def print_(*args, to=None):
600 if to is not None:
601 labels.add(to)
602 args += ('(to %d)' % (to,),)
603 print('%*d%s ' % (offset_width, start, ':' if start in labels else '.'),
604 end=' '*(level-1))
605 print(*args)
606
607 def print_2(*args):
608 print(end=' '*(offset_width + 2*level))
609 print(*args)
610
611 nonlocal level
612 level += 1
613 i = start
614 while i < end:
615 start = i
616 op = code[i]
617 i += 1
618 op = OPCODES[op]
619 if op in (SUCCESS, FAILURE, ANY, ANY_ALL,
620 MAX_UNTIL, MIN_UNTIL, NEGATE):
621 print_(op)
622 elif op in (LITERAL, NOT_LITERAL,
623 LITERAL_IGNORE, NOT_LITERAL_IGNORE,
624 LITERAL_UNI_IGNORE, NOT_LITERAL_UNI_IGNORE,
625 LITERAL_LOC_IGNORE, NOT_LITERAL_LOC_IGNORE):
626 arg = code[i]
627 i += 1
628 print_(op, '%#02x (%r)' % (arg, chr(arg)))
629 elif op is AT:
630 arg = code[i]
631 i += 1
632 arg = str(ATCODES[arg])
633 assert arg[:3] == 'AT_'
634 print_(op, arg[3:])
635 elif op is CATEGORY:
636 arg = code[i]
637 i += 1
638 arg = str(CHCODES[arg])
639 assert arg[:9] == 'CATEGORY_'
640 print_(op, arg[9:])
641 elif op in (IN, IN_IGNORE, IN_UNI_IGNORE, IN_LOC_IGNORE):
642 skip = code[i]
643 print_(op, skip, to=i+skip)
644 dis_(i+1, i+skip)
645 i += skip
646 elif op in (RANGE, RANGE_UNI_IGNORE):
647 lo, hi = code[i: i+2]
648 i += 2
649 print_(op, '%#02x %#02x (%r-%r)' % (lo, hi, chr(lo), chr(hi)))
650 elif op is CHARSET:
651 print_(op, _hex_code(code[i: i + 256//_CODEBITS]))
652 i += 256//_CODEBITS
653 elif op is BIGCHARSET:
654 arg = code[i]
655 i += 1
656 mapping = list(b''.join(x.to_bytes(_sre.CODESIZE, sys.byteorder)
657 for x in code[i: i + 256//_sre.CODESIZE]))
658 print_(op, arg, mapping)
659 i += 256//_sre.CODESIZE
660 level += 1
661 for j in range(arg):
662 print_2(_hex_code(code[i: i + 256//_CODEBITS]))
663 i += 256//_CODEBITS
664 level -= 1
665 elif op in (MARK, GROUPREF, GROUPREF_IGNORE, GROUPREF_UNI_IGNORE,
666 GROUPREF_LOC_IGNORE):
667 arg = code[i]
668 i += 1
669 print_(op, arg)
670 elif op is JUMP:
671 skip = code[i]
672 print_(op, skip, to=i+skip)
673 i += 1
674 elif op is BRANCH:
675 skip = code[i]
676 print_(op, skip, to=i+skip)
677 while skip:
678 dis_(i+1, i+skip)
679 i += skip
680 start = i
681 skip = code[i]
682 if skip:
683 print_('branch', skip, to=i+skip)
684 else:
685 print_(FAILURE)
686 i += 1
687 elif op in (REPEAT, REPEAT_ONE, MIN_REPEAT_ONE,
688 POSSESSIVE_REPEAT, POSSESSIVE_REPEAT_ONE):
689 skip, min, max = code[i: i+3]
690 if max == MAXREPEAT:
691 max = 'MAXREPEAT'
692 print_(op, skip, min, max, to=i+skip)
693 dis_(i+3, i+skip)
694 i += skip
695 elif op is GROUPREF_EXISTS:
696 arg, skip = code[i: i+2]
697 print_(op, arg, skip, to=i+skip)
698 i += 2
699 elif op in (ASSERT, ASSERT_NOT):
700 skip, arg = code[i: i+2]
701 print_(op, skip, arg, to=i+skip)
702 dis_(i+2, i+skip)
703 i += skip
704 elif op is ATOMIC_GROUP:
705 skip = code[i]
706 print_(op, skip, to=i+skip)
707 dis_(i+1, i+skip)
708 i += skip
709 elif op is INFO:
710 skip, flags, min, max = code[i: i+4]
711 if max == MAXREPEAT:
712 max = 'MAXREPEAT'
713 print_(op, skip, bin(flags), min, max, to=i+skip)
714 start = i+4
715 if flags & SRE_INFO_PREFIX:
716 prefix_len, prefix_skip = code[i+4: i+6]
717 print_2(' prefix_skip', prefix_skip)
718 start = i + 6
719 prefix = code[start: start+prefix_len]
720 print_2(' prefix',
721 '[%s]' % ', '.join('%#02x' % x for x in prefix),
722 '(%r)' % ''.join(map(chr, prefix)))
723 start += prefix_len
724 print_2(' overlap', code[start: start+prefix_len])
725 start += prefix_len
726 if flags & SRE_INFO_CHARSET:
727 level += 1
728 print_2('in')
729 dis_(start, i+skip)
730 level -= 1
731 i += skip
732 else:
733 raise ValueError(op)
734
735 level -= 1
736
737 dis_(0, len(code))
738
739
740 def compile(p, flags=0):
741 # internal: convert pattern list to internal format
742
743 if isstring(p):
744 pattern = p
745 p = _parser.parse(p, flags)
746 else:
747 pattern = None
748
749 code = _code(p, flags)
750
751 if flags & SRE_FLAG_DEBUG:
752 print()
753 dis(code)
754
755 # map in either direction
756 groupindex = p.state.groupdict
757 indexgroup = [None] * p.state.groups
758 for k, i in groupindex.items():
759 indexgroup[i] = k
760
761 return _sre.compile(
762 pattern, flags | p.state.flags, code,
763 p.state.groups-1,
764 groupindex, tuple(indexgroup)
765 )