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 | # re-compatible interface for the sre matching engine |
| 5 | # |
| 6 | # Copyright (c) 1998-2000 by Secret Labs AB. All rights reserved. |
| 7 | # |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 8 | # This version of the SRE library can be redistributed under CNRI's |
| 9 | # Python 1.6 license. For any other use, please contact Secret Labs |
| 10 | # AB (info@pythonware.com). |
| 11 | # |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 12 | # Portions of this engine have been developed in cooperation with |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 13 | # CNRI. Hewlett-Packard provided funding for 1.6 integration and |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 14 | # other compatibility work. |
| 15 | # |
| 16 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 17 | # FIXME: change all FIXME's to XXX ;-) |
| 18 | |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 19 | import sre_compile |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 20 | import sre_parse |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 21 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 22 | import string |
| 23 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 24 | # flags |
| 25 | I = IGNORECASE = sre_compile.SRE_FLAG_IGNORECASE |
| 26 | L = LOCALE = sre_compile.SRE_FLAG_LOCALE |
| 27 | M = MULTILINE = sre_compile.SRE_FLAG_MULTILINE |
| 28 | S = DOTALL = sre_compile.SRE_FLAG_DOTALL |
| 29 | X = VERBOSE = sre_compile.SRE_FLAG_VERBOSE |
| 30 | |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 31 | # sre extensions (may or may not be in 1.6/2.0 final) |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 32 | T = TEMPLATE = sre_compile.SRE_FLAG_TEMPLATE |
| 33 | U = UNICODE = sre_compile.SRE_FLAG_UNICODE |
| 34 | |
| 35 | # sre exception |
Fredrik Lundh | be2211e | 2000-06-29 16:57:40 +0000 | [diff] [blame] | 36 | error = sre_compile.error |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 37 | |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 38 | # -------------------------------------------------------------------- |
| 39 | # public interface |
| 40 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 41 | # FIXME: add docstrings |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 42 | |
| 43 | def match(pattern, string, flags=0): |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 44 | return _compile(pattern, flags).match(string) |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 45 | |
| 46 | def search(pattern, string, flags=0): |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 47 | return _compile(pattern, flags).search(string) |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 48 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 49 | def sub(pattern, repl, string, count=0): |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 50 | return _compile(pattern, 0).sub(repl, string, count) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 51 | |
| 52 | def subn(pattern, repl, string, count=0): |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 53 | return _compile(pattern, 0).subn(repl, string, count) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 54 | |
| 55 | def split(pattern, string, maxsplit=0): |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 56 | return _compile(pattern, 0).split(string, maxsplit) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 57 | |
| 58 | def findall(pattern, string, maxsplit=0): |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 59 | return _compile(pattern, 0).findall(string, maxsplit) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 60 | |
| 61 | def compile(pattern, flags=0): |
| 62 | return _compile(pattern, flags) |
| 63 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 64 | def purge(): |
| 65 | _cache.clear() |
| 66 | |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 67 | def template(pattern, flags=0): |
| 68 | return _compile(pattern, flags|T) |
| 69 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 70 | def escape(pattern): |
| 71 | s = list(pattern) |
| 72 | for i in range(len(pattern)): |
| 73 | c = pattern[i] |
| 74 | if not ("a" <= c <= "z" or "A" <= c <= "Z" or "0" <= c <= "9"): |
| 75 | if c == "\000": |
| 76 | s[i] = "\\000" |
| 77 | else: |
| 78 | s[i] = "\\" + c |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 79 | return _join(s, pattern) |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 80 | |
| 81 | # -------------------------------------------------------------------- |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 82 | # internals |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 83 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 84 | _cache = {} |
| 85 | _MAXCACHE = 100 |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 86 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 87 | def _join(seq, sep): |
| 88 | # internal: join into string having the same type as sep |
| 89 | return string.join(seq, sep[:0]) |
| 90 | |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 91 | def _compile(*key): |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 92 | # internal: compile pattern |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 93 | p = _cache.get(key) |
| 94 | if p is not None: |
| 95 | return p |
| 96 | pattern, flags = key |
| 97 | if type(pattern) not in sre_compile.STRING_TYPES: |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 98 | return pattern |
Fredrik Lundh | e186983 | 2000-08-01 22:47:49 +0000 | [diff] [blame] | 99 | try: |
| 100 | p = sre_compile.compile(pattern, flags) |
| 101 | except error, v: |
| 102 | raise error, v # invalid expression |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 103 | if len(_cache) >= _MAXCACHE: |
| 104 | _cache.clear() |
| 105 | _cache[key] = p |
| 106 | return p |
| 107 | |
| 108 | def _sub(pattern, template, string, count=0): |
| 109 | # internal: pattern.sub implementation hook |
| 110 | return _subn(pattern, template, string, count)[0] |
| 111 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 112 | def _subn(pattern, template, string, count=0): |
| 113 | # internal: pattern.subn implementation hook |
| 114 | if callable(template): |
Andrew M. Kuchling | e8d52af | 2000-06-18 20:27:10 +0000 | [diff] [blame] | 115 | filter = template |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 116 | else: |
Fredrik Lundh | 90a0791 | 2000-06-30 07:50:59 +0000 | [diff] [blame] | 117 | template = sre_parse.parse_template(template, pattern) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 118 | def filter(match, template=template): |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 119 | return sre_parse.expand_template(template, match) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 120 | n = i = 0 |
| 121 | s = [] |
| 122 | append = s.append |
Fredrik Lundh | be2211e | 2000-06-29 16:57:40 +0000 | [diff] [blame] | 123 | c = pattern.scanner(string) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 124 | while not count or n < count: |
| 125 | m = c.search() |
| 126 | if not m: |
| 127 | break |
Fredrik Lundh | 90a0791 | 2000-06-30 07:50:59 +0000 | [diff] [blame] | 128 | b, e = m.span() |
Fredrik Lundh | 01016fe | 2000-06-30 00:27:46 +0000 | [diff] [blame] | 129 | if i < b: |
| 130 | append(string[i:b]) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 131 | append(filter(m)) |
Fredrik Lundh | 90a0791 | 2000-06-30 07:50:59 +0000 | [diff] [blame] | 132 | i = e |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 133 | n = n + 1 |
Fredrik Lundh | 01016fe | 2000-06-30 00:27:46 +0000 | [diff] [blame] | 134 | append(string[i:]) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 135 | return _join(s, string[:0]), n |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 136 | |
| 137 | def _split(pattern, string, maxsplit=0): |
| 138 | # internal: pattern.split implementation hook |
| 139 | n = i = 0 |
| 140 | s = [] |
| 141 | append = s.append |
Fredrik Lundh | be2211e | 2000-06-29 16:57:40 +0000 | [diff] [blame] | 142 | extend = s.extend |
| 143 | c = pattern.scanner(string) |
Fredrik Lundh | 01016fe | 2000-06-30 00:27:46 +0000 | [diff] [blame] | 144 | g = pattern.groups |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 145 | while not maxsplit or n < maxsplit: |
| 146 | m = c.search() |
| 147 | if not m: |
| 148 | break |
Fredrik Lundh | 90a0791 | 2000-06-30 07:50:59 +0000 | [diff] [blame] | 149 | b, e = m.span() |
| 150 | if b == e: |
| 151 | if i >= len(string): |
| 152 | break |
| 153 | continue |
Fredrik Lundh | be2211e | 2000-06-29 16:57:40 +0000 | [diff] [blame] | 154 | append(string[i:b]) |
Fredrik Lundh | 90a0791 | 2000-06-30 07:50:59 +0000 | [diff] [blame] | 155 | if g and b != e: |
| 156 | extend(m.groups()) |
| 157 | i = e |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 158 | n = n + 1 |
Fredrik Lundh | 8094611 | 2000-06-29 18:03:25 +0000 | [diff] [blame] | 159 | append(string[i:]) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 160 | return s |
Fredrik Lundh | 0640e11 | 2000-06-30 13:55:15 +0000 | [diff] [blame] | 161 | |
| 162 | # register myself for pickling |
| 163 | |
| 164 | import copy_reg |
| 165 | |
| 166 | def _pickle(p): |
| 167 | return _compile, (p.pattern, p.flags) |
| 168 | |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 169 | copy_reg.pickle(type(_compile("", 0)), _pickle, _compile) |
Fredrik Lundh | 7cafe4d | 2000-07-02 17:33:27 +0000 | [diff] [blame] | 170 | |
| 171 | # -------------------------------------------------------------------- |
| 172 | # experimental stuff (see python-dev discussions for details) |
| 173 | |
| 174 | class Scanner: |
| 175 | def __init__(self, lexicon): |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 176 | from sre_constants import BRANCH, SUBPATTERN |
Fredrik Lundh | 7cafe4d | 2000-07-02 17:33:27 +0000 | [diff] [blame] | 177 | self.lexicon = lexicon |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 178 | # combine phrases into a compound pattern |
Fredrik Lundh | 7cafe4d | 2000-07-02 17:33:27 +0000 | [diff] [blame] | 179 | p = [] |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 180 | s = sre_parse.Pattern() |
Fredrik Lundh | 7cafe4d | 2000-07-02 17:33:27 +0000 | [diff] [blame] | 181 | for phrase, action in lexicon: |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 182 | p.append(sre_parse.SubPattern(s, [ |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 183 | (SUBPATTERN, (len(p), sre_parse.parse(phrase))), |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 184 | ])) |
| 185 | p = sre_parse.SubPattern(s, [(BRANCH, (None, p))]) |
| 186 | s.groups = len(p) |
| 187 | self.scanner = sre_compile.compile(p) |
Fredrik Lundh | 7cafe4d | 2000-07-02 17:33:27 +0000 | [diff] [blame] | 188 | def scan(self, string): |
| 189 | result = [] |
| 190 | append = result.append |
| 191 | match = self.scanner.match |
| 192 | i = 0 |
| 193 | while 1: |
| 194 | m = match(string, i) |
| 195 | if not m: |
| 196 | break |
| 197 | j = m.end() |
| 198 | if i == j: |
| 199 | break |
Fredrik Lundh | 019bcb5 | 2000-07-02 22:59:57 +0000 | [diff] [blame] | 200 | action = self.lexicon[m.lastindex][1] |
Fredrik Lundh | 7cafe4d | 2000-07-02 17:33:27 +0000 | [diff] [blame] | 201 | if callable(action): |
| 202 | self.match = match |
| 203 | action = action(self, m.group()) |
| 204 | if action is not None: |
| 205 | append(action) |
| 206 | i = j |
| 207 | return result, string[i:] |