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 * |
Serhiy Storchaka | 70ca021 | 2013-02-16 16:47:47 +0200 | [diff] [blame] | 16 | from _sre import MAXREPEAT |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 17 | |
Fredrik Lundh | b35ffc0 | 2001-01-15 12:46:09 +0000 | [diff] [blame] | 18 | assert _sre.MAGIC == MAGIC, "SRE module mismatch" |
| 19 | |
Martin v. Löwis | 78e2f06 | 2003-04-19 12:56:08 +0000 | [diff] [blame] | 20 | if _sre.CODESIZE == 2: |
| 21 | MAXCODE = 65535 |
| 22 | else: |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 23 | MAXCODE = 0xFFFFFFFF |
Fredrik Lundh | 3562f11 | 2000-07-02 12:00:07 +0000 | [diff] [blame] | 24 | |
Raymond Hettinger | 01c9f8c | 2004-03-26 11:16:55 +0000 | [diff] [blame] | 25 | def _identityfunction(x): |
| 26 | return x |
| 27 | |
Raymond Hettinger | 049ade2 | 2005-02-28 19:27:52 +0000 | [diff] [blame] | 28 | _LITERAL_CODES = set([LITERAL, NOT_LITERAL]) |
| 29 | _REPEATING_CODES = set([REPEAT, MIN_REPEAT, MAX_REPEAT]) |
| 30 | _SUCCESS_CODES = set([SUCCESS, FAILURE]) |
| 31 | _ASSERT_CODES = set([ASSERT, ASSERT_NOT]) |
| 32 | |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 33 | def _compile(code, pattern, flags): |
| 34 | # internal: compile a (sub)pattern |
| 35 | emit = code.append |
Raymond Hettinger | 01c9f8c | 2004-03-26 11:16:55 +0000 | [diff] [blame] | 36 | _len = len |
Raymond Hettinger | 049ade2 | 2005-02-28 19:27:52 +0000 | [diff] [blame] | 37 | LITERAL_CODES = _LITERAL_CODES |
| 38 | REPEATING_CODES = _REPEATING_CODES |
| 39 | SUCCESS_CODES = _SUCCESS_CODES |
| 40 | ASSERT_CODES = _ASSERT_CODES |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 41 | for op, av in pattern: |
Raymond Hettinger | 01c9f8c | 2004-03-26 11:16:55 +0000 | [diff] [blame] | 42 | if op in LITERAL_CODES: |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 43 | if flags & SRE_FLAG_IGNORECASE: |
| 44 | emit(OPCODES[OP_IGNORE[op]]) |
Fredrik Lundh | 2e24044 | 2001-01-15 18:28:14 +0000 | [diff] [blame] | 45 | emit(_sre.getlower(av, flags)) |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 46 | else: |
| 47 | emit(OPCODES[op]) |
Fredrik Lundh | 2e24044 | 2001-01-15 18:28:14 +0000 | [diff] [blame] | 48 | emit(av) |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 49 | elif op is IN: |
| 50 | if flags & SRE_FLAG_IGNORECASE: |
| 51 | emit(OPCODES[OP_IGNORE[op]]) |
| 52 | def fixup(literal, flags=flags): |
| 53 | return _sre.getlower(literal, flags) |
| 54 | else: |
| 55 | emit(OPCODES[op]) |
Raymond Hettinger | 01c9f8c | 2004-03-26 11:16:55 +0000 | [diff] [blame] | 56 | fixup = _identityfunction |
| 57 | skip = _len(code); emit(0) |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 58 | _compile_charset(av, flags, code, fixup) |
Raymond Hettinger | 01c9f8c | 2004-03-26 11:16:55 +0000 | [diff] [blame] | 59 | code[skip] = _len(code) - skip |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 60 | elif op is ANY: |
| 61 | if flags & SRE_FLAG_DOTALL: |
| 62 | emit(OPCODES[ANY_ALL]) |
| 63 | else: |
| 64 | emit(OPCODES[ANY]) |
Raymond Hettinger | 01c9f8c | 2004-03-26 11:16:55 +0000 | [diff] [blame] | 65 | elif op in REPEATING_CODES: |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 66 | if flags & SRE_FLAG_TEMPLATE: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 67 | raise error("internal: unsupported template operator") |
Raymond Hettinger | 01c9f8c | 2004-03-26 11:16:55 +0000 | [diff] [blame] | 68 | elif _simple(av) and op is not REPEAT: |
| 69 | if op is MAX_REPEAT: |
Guido van Rossum | 41c99e7 | 2003-04-14 17:59:34 +0000 | [diff] [blame] | 70 | emit(OPCODES[REPEAT_ONE]) |
| 71 | else: |
| 72 | emit(OPCODES[MIN_REPEAT_ONE]) |
Raymond Hettinger | 01c9f8c | 2004-03-26 11:16:55 +0000 | [diff] [blame] | 73 | skip = _len(code); emit(0) |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 74 | emit(av[0]) |
| 75 | emit(av[1]) |
| 76 | _compile(code, av[2], flags) |
| 77 | emit(OPCODES[SUCCESS]) |
Raymond Hettinger | 01c9f8c | 2004-03-26 11:16:55 +0000 | [diff] [blame] | 78 | code[skip] = _len(code) - skip |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 79 | else: |
| 80 | emit(OPCODES[REPEAT]) |
Raymond Hettinger | 01c9f8c | 2004-03-26 11:16:55 +0000 | [diff] [blame] | 81 | skip = _len(code); emit(0) |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 82 | emit(av[0]) |
| 83 | emit(av[1]) |
| 84 | _compile(code, av[2], flags) |
Raymond Hettinger | 01c9f8c | 2004-03-26 11:16:55 +0000 | [diff] [blame] | 85 | code[skip] = _len(code) - skip |
| 86 | if op is MAX_REPEAT: |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 87 | emit(OPCODES[MAX_UNTIL]) |
| 88 | else: |
| 89 | emit(OPCODES[MIN_UNTIL]) |
| 90 | elif op is SUBPATTERN: |
| 91 | if av[0]: |
| 92 | emit(OPCODES[MARK]) |
| 93 | emit((av[0]-1)*2) |
| 94 | # _compile_info(code, av[1], flags) |
| 95 | _compile(code, av[1], flags) |
| 96 | if av[0]: |
| 97 | emit(OPCODES[MARK]) |
| 98 | emit((av[0]-1)*2+1) |
Raymond Hettinger | 01c9f8c | 2004-03-26 11:16:55 +0000 | [diff] [blame] | 99 | elif op in SUCCESS_CODES: |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 100 | emit(OPCODES[op]) |
Raymond Hettinger | 01c9f8c | 2004-03-26 11:16:55 +0000 | [diff] [blame] | 101 | elif op in ASSERT_CODES: |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 102 | emit(OPCODES[op]) |
Raymond Hettinger | 01c9f8c | 2004-03-26 11:16:55 +0000 | [diff] [blame] | 103 | skip = _len(code); emit(0) |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 104 | if av[0] >= 0: |
| 105 | emit(0) # look ahead |
| 106 | else: |
| 107 | lo, hi = av[1].getwidth() |
| 108 | if lo != hi: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 109 | raise error("look-behind requires fixed-width pattern") |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 110 | emit(lo) # look behind |
| 111 | _compile(code, av[1], flags) |
| 112 | emit(OPCODES[SUCCESS]) |
Raymond Hettinger | 01c9f8c | 2004-03-26 11:16:55 +0000 | [diff] [blame] | 113 | code[skip] = _len(code) - skip |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 114 | elif op is CALL: |
| 115 | emit(OPCODES[op]) |
Raymond Hettinger | 01c9f8c | 2004-03-26 11:16:55 +0000 | [diff] [blame] | 116 | skip = _len(code); emit(0) |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 117 | _compile(code, av, flags) |
| 118 | emit(OPCODES[SUCCESS]) |
Raymond Hettinger | 01c9f8c | 2004-03-26 11:16:55 +0000 | [diff] [blame] | 119 | code[skip] = _len(code) - skip |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 120 | elif op is AT: |
| 121 | emit(OPCODES[op]) |
| 122 | if flags & SRE_FLAG_MULTILINE: |
Fredrik Lundh | b25e1ad | 2001-03-22 15:50:10 +0000 | [diff] [blame] | 123 | av = AT_MULTILINE.get(av, av) |
| 124 | if flags & SRE_FLAG_LOCALE: |
| 125 | av = AT_LOCALE.get(av, av) |
| 126 | elif flags & SRE_FLAG_UNICODE: |
| 127 | av = AT_UNICODE.get(av, av) |
| 128 | emit(ATCODES[av]) |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 129 | elif op is BRANCH: |
| 130 | emit(OPCODES[op]) |
| 131 | tail = [] |
Raymond Hettinger | 01c9f8c | 2004-03-26 11:16:55 +0000 | [diff] [blame] | 132 | tailappend = tail.append |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 133 | for av in av[1]: |
Raymond Hettinger | 01c9f8c | 2004-03-26 11:16:55 +0000 | [diff] [blame] | 134 | skip = _len(code); emit(0) |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 135 | # _compile_info(code, av, flags) |
| 136 | _compile(code, av, flags) |
| 137 | emit(OPCODES[JUMP]) |
Raymond Hettinger | 01c9f8c | 2004-03-26 11:16:55 +0000 | [diff] [blame] | 138 | tailappend(_len(code)); emit(0) |
| 139 | code[skip] = _len(code) - skip |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 140 | emit(0) # end of branch |
| 141 | for tail in tail: |
Raymond Hettinger | 01c9f8c | 2004-03-26 11:16:55 +0000 | [diff] [blame] | 142 | code[tail] = _len(code) - tail |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 143 | elif op is CATEGORY: |
| 144 | emit(OPCODES[op]) |
| 145 | if flags & SRE_FLAG_LOCALE: |
Fredrik Lundh | b25e1ad | 2001-03-22 15:50:10 +0000 | [diff] [blame] | 146 | av = CH_LOCALE[av] |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 147 | elif flags & SRE_FLAG_UNICODE: |
Fredrik Lundh | b25e1ad | 2001-03-22 15:50:10 +0000 | [diff] [blame] | 148 | av = CH_UNICODE[av] |
| 149 | emit(CHCODES[av]) |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 150 | elif op is GROUPREF: |
| 151 | if flags & SRE_FLAG_IGNORECASE: |
| 152 | emit(OPCODES[OP_IGNORE[op]]) |
| 153 | else: |
| 154 | emit(OPCODES[op]) |
| 155 | emit(av-1) |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 156 | elif op is GROUPREF_EXISTS: |
| 157 | emit(OPCODES[op]) |
Andrew M. Kuchling | c30faa8 | 2005-06-02 13:35:52 +0000 | [diff] [blame] | 158 | emit(av[0]-1) |
Raymond Hettinger | 01c9f8c | 2004-03-26 11:16:55 +0000 | [diff] [blame] | 159 | skipyes = _len(code); emit(0) |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 160 | _compile(code, av[1], flags) |
| 161 | if av[2]: |
| 162 | emit(OPCODES[JUMP]) |
Raymond Hettinger | 01c9f8c | 2004-03-26 11:16:55 +0000 | [diff] [blame] | 163 | skipno = _len(code); emit(0) |
| 164 | code[skipyes] = _len(code) - skipyes + 1 |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 165 | _compile(code, av[2], flags) |
Raymond Hettinger | 01c9f8c | 2004-03-26 11:16:55 +0000 | [diff] [blame] | 166 | code[skipno] = _len(code) - skipno |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 167 | else: |
Raymond Hettinger | 01c9f8c | 2004-03-26 11:16:55 +0000 | [diff] [blame] | 168 | code[skipyes] = _len(code) - skipyes + 1 |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 169 | else: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 170 | raise ValueError("unsupported operand type", op) |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 171 | |
| 172 | def _compile_charset(charset, flags, code, fixup=None): |
| 173 | # compile charset subprogram |
| 174 | emit = code.append |
Raymond Hettinger | f13eb55 | 2002-06-02 00:40:05 +0000 | [diff] [blame] | 175 | if fixup is None: |
Raymond Hettinger | 01c9f8c | 2004-03-26 11:16:55 +0000 | [diff] [blame] | 176 | fixup = _identityfunction |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 177 | for op, av in _optimize_charset(charset, fixup): |
| 178 | emit(OPCODES[op]) |
| 179 | if op is NEGATE: |
| 180 | pass |
| 181 | elif op is LITERAL: |
| 182 | emit(fixup(av)) |
| 183 | elif op is RANGE: |
| 184 | emit(fixup(av[0])) |
| 185 | emit(fixup(av[1])) |
| 186 | elif op is CHARSET: |
| 187 | code.extend(av) |
Fredrik Lundh | 19af43d | 2001-07-02 16:58:38 +0000 | [diff] [blame] | 188 | elif op is BIGCHARSET: |
| 189 | code.extend(av) |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 190 | elif op is CATEGORY: |
| 191 | if flags & SRE_FLAG_LOCALE: |
| 192 | emit(CHCODES[CH_LOCALE[av]]) |
| 193 | elif flags & SRE_FLAG_UNICODE: |
| 194 | emit(CHCODES[CH_UNICODE[av]]) |
| 195 | else: |
| 196 | emit(CHCODES[av]) |
| 197 | else: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 198 | raise error("internal: unsupported set operator") |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 199 | emit(OPCODES[FAILURE]) |
| 200 | |
| 201 | def _optimize_charset(charset, fixup): |
| 202 | # internal: optimize character set |
Fredrik Lundh | 3562f11 | 2000-07-02 12:00:07 +0000 | [diff] [blame] | 203 | out = [] |
Serhiy Storchaka | 68457be | 2013-10-27 08:20:29 +0200 | [diff] [blame] | 204 | tail = [] |
| 205 | charmap = bytearray(256) |
| 206 | for op, av in charset: |
| 207 | while True: |
| 208 | try: |
| 209 | if op is LITERAL: |
| 210 | charmap[fixup(av)] = 1 |
| 211 | elif op is RANGE: |
| 212 | for i in range(fixup(av[0]), fixup(av[1])+1): |
| 213 | charmap[i] = 1 |
| 214 | elif op is NEGATE: |
| 215 | out.append((op, av)) |
| 216 | else: |
| 217 | tail.append((op, av)) |
| 218 | except IndexError: |
| 219 | if len(charmap) == 256: |
| 220 | # character set contains non-UCS1 character codes |
| 221 | charmap += b'\0' * 0xff00 |
| 222 | continue |
| 223 | # character set contains non-BMP character codes |
| 224 | tail.append((op, av)) |
| 225 | break |
| 226 | |
Fredrik Lundh | 3562f11 | 2000-07-02 12:00:07 +0000 | [diff] [blame] | 227 | # compress character map |
Fredrik Lundh | 3562f11 | 2000-07-02 12:00:07 +0000 | [diff] [blame] | 228 | runs = [] |
Serhiy Storchaka | 68457be | 2013-10-27 08:20:29 +0200 | [diff] [blame] | 229 | q = 0 |
| 230 | while True: |
| 231 | p = charmap.find(1, q) |
| 232 | if p < 0: |
| 233 | break |
| 234 | if len(runs) >= 2: |
| 235 | runs = None |
| 236 | break |
| 237 | q = charmap.find(0, p) |
| 238 | if q < 0: |
| 239 | runs.append((p, len(charmap))) |
| 240 | break |
| 241 | runs.append((p, q)) |
| 242 | if runs is not None: |
Fredrik Lundh | 3562f11 | 2000-07-02 12:00:07 +0000 | [diff] [blame] | 243 | # use literal/range |
Serhiy Storchaka | 68457be | 2013-10-27 08:20:29 +0200 | [diff] [blame] | 244 | for p, q in runs: |
| 245 | if q - p == 1: |
| 246 | out.append((LITERAL, p)) |
Fredrik Lundh | 3562f11 | 2000-07-02 12:00:07 +0000 | [diff] [blame] | 247 | else: |
Serhiy Storchaka | 68457be | 2013-10-27 08:20:29 +0200 | [diff] [blame] | 248 | out.append((RANGE, (p, q - 1))) |
| 249 | out += tail |
Fredrik Lundh | 3562f11 | 2000-07-02 12:00:07 +0000 | [diff] [blame] | 250 | if len(out) < len(charset): |
| 251 | return out |
Serhiy Storchaka | 68457be | 2013-10-27 08:20:29 +0200 | [diff] [blame] | 252 | return charset |
| 253 | |
| 254 | # use bitmap |
| 255 | if len(charmap) == 256: |
Fredrik Lundh | 19af43d | 2001-07-02 16:58:38 +0000 | [diff] [blame] | 256 | data = _mk_bitmap(charmap) |
Serhiy Storchaka | 68457be | 2013-10-27 08:20:29 +0200 | [diff] [blame] | 257 | out.append((CHARSET, data)) |
| 258 | out += tail |
Fredrik Lundh | 3562f11 | 2000-07-02 12:00:07 +0000 | [diff] [blame] | 259 | return out |
Fredrik Lundh | 3562f11 | 2000-07-02 12:00:07 +0000 | [diff] [blame] | 260 | |
Serhiy Storchaka | 68457be | 2013-10-27 08:20:29 +0200 | [diff] [blame] | 261 | # To represent a big charset, first a bitmap of all characters in the |
| 262 | # set is constructed. Then, this bitmap is sliced into chunks of 256 |
| 263 | # characters, duplicate chunks are eliminated, and each chunk is |
| 264 | # given a number. In the compiled expression, the charset is |
| 265 | # represented by a 32-bit word sequence, consisting of one word for |
| 266 | # the number of different chunks, a sequence of 256 bytes (64 words) |
| 267 | # of chunk numbers indexed by their original chunk position, and a |
| 268 | # sequence of 256-bit chunks (8 words each). |
Fredrik Lundh | 19af43d | 2001-07-02 16:58:38 +0000 | [diff] [blame] | 269 | |
Serhiy Storchaka | 68457be | 2013-10-27 08:20:29 +0200 | [diff] [blame] | 270 | # Compression is normally good: in a typical charset, large ranges of |
| 271 | # Unicode will be either completely excluded (e.g. if only cyrillic |
| 272 | # letters are to be matched), or completely included (e.g. if large |
| 273 | # subranges of Kanji match). These ranges will be represented by |
| 274 | # chunks of all one-bits or all zero-bits. |
Fredrik Lundh | 19af43d | 2001-07-02 16:58:38 +0000 | [diff] [blame] | 275 | |
Serhiy Storchaka | 68457be | 2013-10-27 08:20:29 +0200 | [diff] [blame] | 276 | # Matching can be also done efficiently: the more significant byte of |
| 277 | # the Unicode character is an index into the chunk number, and the |
| 278 | # less significant byte is a bit index in the chunk (just like the |
| 279 | # CHARSET matching). |
Fredrik Lundh | 19af43d | 2001-07-02 16:58:38 +0000 | [diff] [blame] | 280 | |
Serhiy Storchaka | 68457be | 2013-10-27 08:20:29 +0200 | [diff] [blame] | 281 | charmap = bytes(charmap) # should be hashable |
Fredrik Lundh | 19af43d | 2001-07-02 16:58:38 +0000 | [diff] [blame] | 282 | comps = {} |
Serhiy Storchaka | 68457be | 2013-10-27 08:20:29 +0200 | [diff] [blame] | 283 | mapping = bytearray(256) |
Fredrik Lundh | 19af43d | 2001-07-02 16:58:38 +0000 | [diff] [blame] | 284 | block = 0 |
Serhiy Storchaka | 68457be | 2013-10-27 08:20:29 +0200 | [diff] [blame] | 285 | data = bytearray() |
| 286 | for i in range(0, 65536, 256): |
| 287 | chunk = charmap[i: i + 256] |
| 288 | if chunk in comps: |
| 289 | mapping[i // 256] = comps[chunk] |
| 290 | else: |
| 291 | mapping[i // 256] = comps[chunk] = block |
| 292 | block += 1 |
| 293 | data += chunk |
| 294 | data = _mk_bitmap(data) |
| 295 | data[0:0] = [block] + _bytes_to_codes(mapping) |
| 296 | out.append((BIGCHARSET, data)) |
| 297 | out += tail |
| 298 | return out |
| 299 | |
| 300 | _CODEBITS = _sre.CODESIZE * 8 |
| 301 | _BITS_TRANS = b'0' + b'1' * 255 |
| 302 | def _mk_bitmap(bits, _CODEBITS=_CODEBITS, _int=int): |
| 303 | s = bits.translate(_BITS_TRANS)[::-1] |
| 304 | return [_int(s[i - _CODEBITS: i], 2) |
| 305 | for i in range(len(s), 0, -_CODEBITS)] |
| 306 | |
| 307 | def _bytes_to_codes(b): |
| 308 | # Convert block indices to word array |
| 309 | import array |
| 310 | a = array.array('I', b) |
| 311 | assert a.itemsize == _sre.CODESIZE |
| 312 | assert len(a) * a.itemsize == len(b) |
| 313 | return a.tolist() |
Fredrik Lundh | 19af43d | 2001-07-02 16:58:38 +0000 | [diff] [blame] | 314 | |
Fredrik Lundh | e186983 | 2000-08-01 22:47:49 +0000 | [diff] [blame] | 315 | def _simple(av): |
| 316 | # check if av is a "simple" operator |
| 317 | lo, hi = av[2].getwidth() |
Fredrik Lundh | e186983 | 2000-08-01 22:47:49 +0000 | [diff] [blame] | 318 | return lo == hi == 1 and av[2][0][0] != SUBPATTERN |
| 319 | |
Antoine Pitrou | 79aa68d | 2013-10-25 21:36:10 +0200 | [diff] [blame] | 320 | def _generate_overlap_table(prefix): |
| 321 | """ |
| 322 | Generate an overlap table for the following prefix. |
| 323 | An overlap table is a table of the same size as the prefix which |
| 324 | informs about the potential self-overlap for each index in the prefix: |
| 325 | - if overlap[i] == 0, prefix[i:] can't overlap prefix[0:...] |
| 326 | - if overlap[i] == k with 0 < k <= i, prefix[i-k+1:i+1] overlaps with |
| 327 | prefix[0:k] |
| 328 | """ |
| 329 | table = [0] * len(prefix) |
| 330 | for i in range(1, len(prefix)): |
| 331 | idx = table[i - 1] |
| 332 | while prefix[i] != prefix[idx]: |
| 333 | if idx == 0: |
| 334 | table[i] = 0 |
| 335 | break |
| 336 | idx = table[idx - 1] |
| 337 | else: |
| 338 | table[i] = idx + 1 |
| 339 | return table |
| 340 | |
Fredrik Lundh | 29c08be | 2000-06-29 23:33:12 +0000 | [diff] [blame] | 341 | def _compile_info(code, pattern, flags): |
| 342 | # internal: compile an info block. in the current version, |
Fredrik Lundh | 3562f11 | 2000-07-02 12:00:07 +0000 | [diff] [blame] | 343 | # this contains min/max pattern width, and an optional literal |
| 344 | # prefix or a character map |
Fredrik Lundh | 29c08be | 2000-06-29 23:33:12 +0000 | [diff] [blame] | 345 | lo, hi = pattern.getwidth() |
| 346 | if lo == 0: |
Fredrik Lundh | 90a0791 | 2000-06-30 07:50:59 +0000 | [diff] [blame] | 347 | return # not worth it |
Fredrik Lundh | 29c08be | 2000-06-29 23:33:12 +0000 | [diff] [blame] | 348 | # look for a literal prefix |
| 349 | prefix = [] |
Raymond Hettinger | 01c9f8c | 2004-03-26 11:16:55 +0000 | [diff] [blame] | 350 | prefixappend = prefix.append |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 351 | prefix_skip = 0 |
Fredrik Lundh | 3562f11 | 2000-07-02 12:00:07 +0000 | [diff] [blame] | 352 | charset = [] # not used |
Raymond Hettinger | 01c9f8c | 2004-03-26 11:16:55 +0000 | [diff] [blame] | 353 | charsetappend = charset.append |
Fredrik Lundh | 29c08be | 2000-06-29 23:33:12 +0000 | [diff] [blame] | 354 | if not (flags & SRE_FLAG_IGNORECASE): |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 355 | # look for literal prefix |
Fredrik Lundh | 90a0791 | 2000-06-30 07:50:59 +0000 | [diff] [blame] | 356 | for op, av in pattern.data: |
| 357 | if op is LITERAL: |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 358 | if len(prefix) == prefix_skip: |
| 359 | prefix_skip = prefix_skip + 1 |
Raymond Hettinger | 01c9f8c | 2004-03-26 11:16:55 +0000 | [diff] [blame] | 360 | prefixappend(av) |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 361 | elif op is SUBPATTERN and len(av[1]) == 1: |
| 362 | op, av = av[1][0] |
| 363 | if op is LITERAL: |
Raymond Hettinger | 01c9f8c | 2004-03-26 11:16:55 +0000 | [diff] [blame] | 364 | prefixappend(av) |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 365 | else: |
| 366 | break |
Fredrik Lundh | 90a0791 | 2000-06-30 07:50:59 +0000 | [diff] [blame] | 367 | else: |
| 368 | break |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 369 | # if no prefix, look for charset prefix |
| 370 | if not prefix and pattern.data: |
| 371 | op, av = pattern.data[0] |
| 372 | if op is SUBPATTERN and av[1]: |
| 373 | op, av = av[1][0] |
| 374 | if op is LITERAL: |
Raymond Hettinger | 01c9f8c | 2004-03-26 11:16:55 +0000 | [diff] [blame] | 375 | charsetappend((op, av)) |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 376 | elif op is BRANCH: |
| 377 | c = [] |
Raymond Hettinger | 01c9f8c | 2004-03-26 11:16:55 +0000 | [diff] [blame] | 378 | cappend = c.append |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 379 | for p in av[1]: |
| 380 | if not p: |
| 381 | break |
| 382 | op, av = p[0] |
| 383 | if op is LITERAL: |
Raymond Hettinger | 01c9f8c | 2004-03-26 11:16:55 +0000 | [diff] [blame] | 384 | cappend((op, av)) |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 385 | else: |
| 386 | break |
| 387 | else: |
| 388 | charset = c |
| 389 | elif op is BRANCH: |
| 390 | c = [] |
Raymond Hettinger | 01c9f8c | 2004-03-26 11:16:55 +0000 | [diff] [blame] | 391 | cappend = c.append |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 392 | for p in av[1]: |
| 393 | if not p: |
| 394 | break |
| 395 | op, av = p[0] |
| 396 | if op is LITERAL: |
Raymond Hettinger | 01c9f8c | 2004-03-26 11:16:55 +0000 | [diff] [blame] | 397 | cappend((op, av)) |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 398 | else: |
| 399 | break |
| 400 | else: |
| 401 | charset = c |
| 402 | elif op is IN: |
| 403 | charset = av |
| 404 | ## if prefix: |
| 405 | ## print "*** PREFIX", prefix, prefix_skip |
| 406 | ## if charset: |
| 407 | ## print "*** CHARSET", charset |
Fredrik Lundh | 29c08be | 2000-06-29 23:33:12 +0000 | [diff] [blame] | 408 | # add an info block |
| 409 | emit = code.append |
| 410 | emit(OPCODES[INFO]) |
| 411 | skip = len(code); emit(0) |
| 412 | # literal flag |
| 413 | mask = 0 |
Fredrik Lundh | 3562f11 | 2000-07-02 12:00:07 +0000 | [diff] [blame] | 414 | if prefix: |
| 415 | mask = SRE_INFO_PREFIX |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 416 | if len(prefix) == prefix_skip == len(pattern.data): |
Fredrik Lundh | 3562f11 | 2000-07-02 12:00:07 +0000 | [diff] [blame] | 417 | mask = mask + SRE_INFO_LITERAL |
| 418 | elif charset: |
| 419 | mask = mask + SRE_INFO_CHARSET |
Fredrik Lundh | 29c08be | 2000-06-29 23:33:12 +0000 | [diff] [blame] | 420 | emit(mask) |
| 421 | # pattern length |
Fredrik Lundh | 3562f11 | 2000-07-02 12:00:07 +0000 | [diff] [blame] | 422 | if lo < MAXCODE: |
| 423 | emit(lo) |
| 424 | else: |
| 425 | emit(MAXCODE) |
| 426 | prefix = prefix[:MAXCODE] |
| 427 | if hi < MAXCODE: |
Fredrik Lundh | 90a0791 | 2000-06-30 07:50:59 +0000 | [diff] [blame] | 428 | emit(hi) |
Fredrik Lundh | 29c08be | 2000-06-29 23:33:12 +0000 | [diff] [blame] | 429 | else: |
Fredrik Lundh | 90a0791 | 2000-06-30 07:50:59 +0000 | [diff] [blame] | 430 | emit(0) |
Fredrik Lundh | 29c08be | 2000-06-29 23:33:12 +0000 | [diff] [blame] | 431 | # add literal prefix |
Fredrik Lundh | 29c08be | 2000-06-29 23:33:12 +0000 | [diff] [blame] | 432 | if prefix: |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 433 | emit(len(prefix)) # length |
| 434 | emit(prefix_skip) # skip |
| 435 | code.extend(prefix) |
| 436 | # generate overlap table |
Antoine Pitrou | 79aa68d | 2013-10-25 21:36:10 +0200 | [diff] [blame] | 437 | code.extend(_generate_overlap_table(prefix)) |
Fredrik Lundh | 3562f11 | 2000-07-02 12:00:07 +0000 | [diff] [blame] | 438 | elif charset: |
Guido van Rossum | 577fb5a | 2003-02-24 01:18:35 +0000 | [diff] [blame] | 439 | _compile_charset(charset, flags, code) |
Fredrik Lundh | 29c08be | 2000-06-29 23:33:12 +0000 | [diff] [blame] | 440 | code[skip] = len(code) - skip |
| 441 | |
Just van Rossum | 7490250 | 2003-07-02 21:37:16 +0000 | [diff] [blame] | 442 | def isstring(obj): |
Thomas Wouters | 40a088d | 2008-03-18 20:19:54 +0000 | [diff] [blame] | 443 | return isinstance(obj, (str, bytes)) |
Just van Rossum | 7490250 | 2003-07-02 21:37:16 +0000 | [diff] [blame] | 444 | |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 445 | def _code(p, flags): |
Fredrik Lundh | 29c08be | 2000-06-29 23:33:12 +0000 | [diff] [blame] | 446 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 447 | flags = p.pattern.flags | flags |
Fredrik Lundh | be2211e | 2000-06-29 16:57:40 +0000 | [diff] [blame] | 448 | code = [] |
Fredrik Lundh | 29c08be | 2000-06-29 23:33:12 +0000 | [diff] [blame] | 449 | |
| 450 | # compile info block |
| 451 | _compile_info(code, p, flags) |
| 452 | |
| 453 | # compile the pattern |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 454 | _compile(code, p.data, flags) |
Fredrik Lundh | 29c08be | 2000-06-29 23:33:12 +0000 | [diff] [blame] | 455 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 456 | code.append(OPCODES[SUCCESS]) |
Fredrik Lundh | 29c08be | 2000-06-29 23:33:12 +0000 | [diff] [blame] | 457 | |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 458 | return code |
| 459 | |
| 460 | def compile(p, flags=0): |
| 461 | # internal: convert pattern list to internal format |
| 462 | |
Just van Rossum | 7490250 | 2003-07-02 21:37:16 +0000 | [diff] [blame] | 463 | if isstring(p): |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 464 | pattern = p |
| 465 | p = sre_parse.parse(p, flags) |
| 466 | else: |
| 467 | pattern = None |
| 468 | |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 469 | code = _code(p, flags) |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 470 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 471 | # print code |
| 472 | |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 473 | # XXX: <fl> get rid of this limitation! |
Fredrik Lundh | 5e7d51b | 2004-10-15 06:15:08 +0000 | [diff] [blame] | 474 | if p.pattern.groups > 100: |
| 475 | raise AssertionError( |
| 476 | "sorry, but this version only supports 100 named groups" |
| 477 | ) |
Fredrik Lundh | 29c08be | 2000-06-29 23:33:12 +0000 | [diff] [blame] | 478 | |
Fredrik Lundh | c230173 | 2000-07-02 22:25:39 +0000 | [diff] [blame] | 479 | # map in either direction |
| 480 | groupindex = p.pattern.groupdict |
| 481 | indexgroup = [None] * p.pattern.groups |
| 482 | for k, i in groupindex.items(): |
| 483 | indexgroup[i] = k |
| 484 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 485 | return _sre.compile( |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 486 | pattern, flags | p.pattern.flags, code, |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 487 | p.pattern.groups-1, |
| 488 | groupindex, indexgroup |
Fredrik Lundh | 90a0791 | 2000-06-30 07:50:59 +0000 | [diff] [blame] | 489 | ) |