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