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