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