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