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