| 1 |
""" |
| 2 |
This is not used in a PyPy translation, but it can be used |
| 3 |
in RPython code. It exports the same interface as the |
| 4 |
Python 're' module. You can call the functions at the start |
| 5 |
of the module (expect the ones with @not_rpython for now). |
| 6 |
They must be called with a *constant* pattern string. |
| 7 |
""" |
| 8 |
import re, sys |
| 9 |
from rpython.rlib.rsre import rsre_core, rsre_char |
| 10 |
from rpython.rlib.rsre.rpy import get_code as _get_code |
| 11 |
from rpython.rlib.unicodedata import unicodedb |
| 12 |
from rpython.rlib.objectmodel import specialize, we_are_translated |
| 13 |
from rpython.rlib.objectmodel import not_rpython |
| 14 |
rsre_char.set_unicode_db(unicodedb) |
| 15 |
|
| 16 |
|
| 17 |
I = IGNORECASE = re.I |
| 18 |
L = LOCALE = re.L |
| 19 |
U = UNICODE = re.U |
| 20 |
M = MULTILINE = re.M |
| 21 |
S = DOTALL = re.S |
| 22 |
X = VERBOSE = re.X |
| 23 |
|
| 24 |
|
| 25 |
@specialize.call_location() |
| 26 |
def match(pattern, string, flags=0): |
| 27 |
return compile(pattern, flags).match(string) |
| 28 |
|
| 29 |
@specialize.call_location() |
| 30 |
def search(pattern, string, flags=0): |
| 31 |
return compile(pattern, flags).search(string) |
| 32 |
|
| 33 |
@specialize.call_location() |
| 34 |
def findall(pattern, string, flags=0): |
| 35 |
return compile(pattern, flags).findall(string) |
| 36 |
|
| 37 |
@specialize.call_location() |
| 38 |
def finditer(pattern, string, flags=0): |
| 39 |
return compile(pattern, flags).finditer(string) |
| 40 |
|
| 41 |
@not_rpython |
| 42 |
def sub(pattern, repl, string, count=0): |
| 43 |
return compile(pattern).sub(repl, string, count) |
| 44 |
|
| 45 |
@not_rpython |
| 46 |
def subn(pattern, repl, string, count=0): |
| 47 |
return compile(pattern).subn(repl, string, count) |
| 48 |
|
| 49 |
@specialize.call_location() |
| 50 |
def split(pattern, string, maxsplit=0): |
| 51 |
return compile(pattern).split(string, maxsplit) |
| 52 |
|
| 53 |
@specialize.memo() |
| 54 |
def compile(pattern, flags=0): |
| 55 |
code, flags, args = _get_code(pattern, flags, allargs=True) |
| 56 |
return RSREPattern(pattern, code, flags, *args) |
| 57 |
|
| 58 |
escape = re.escape |
| 59 |
error = re.error |
| 60 |
|
| 61 |
|
| 62 |
class RSREPattern(object): |
| 63 |
|
| 64 |
def __init__(self, pattern, code, flags, |
| 65 |
num_groups, groupindex, indexgroup): |
| 66 |
self._code = code |
| 67 |
self.pattern = pattern |
| 68 |
self.flags = flags |
| 69 |
self.groups = num_groups |
| 70 |
self.groupindex = groupindex |
| 71 |
self._indexgroup = indexgroup |
| 72 |
|
| 73 |
def match(self, string, pos=0, endpos=sys.maxint): |
| 74 |
return self._make_match(rsre_core.match(self._code, string, |
| 75 |
pos, endpos)) |
| 76 |
|
| 77 |
def search(self, string, pos=0, endpos=sys.maxint): |
| 78 |
return self._make_match(rsre_core.search(self._code, string, |
| 79 |
pos, endpos)) |
| 80 |
|
| 81 |
def findall(self, string, pos=0, endpos=sys.maxint): |
| 82 |
matchlist = [] |
| 83 |
scanner = self.scanner(string, pos, endpos) |
| 84 |
while True: |
| 85 |
match = scanner.search() |
| 86 |
if match is None: |
| 87 |
break |
| 88 |
if self.groups == 0 or self.groups == 1: |
| 89 |
item = match.group(self.groups) |
| 90 |
else: |
| 91 |
assert False, ("findall() not supported if there is more " |
| 92 |
"than one group: not valid RPython") |
| 93 |
item = match.groups("") |
| 94 |
matchlist.append(item) |
| 95 |
return matchlist |
| 96 |
|
| 97 |
def finditer(self, string, pos=0, endpos=sys.maxint): |
| 98 |
scanner = self.scanner(string, pos, endpos) |
| 99 |
while True: |
| 100 |
match = scanner.search() |
| 101 |
if match is None: |
| 102 |
break |
| 103 |
yield match |
| 104 |
|
| 105 |
@not_rpython |
| 106 |
def subn(self, repl, string, count=0): |
| 107 |
filter = repl |
| 108 |
if not callable(repl) and "\\" in repl: |
| 109 |
|
| 110 |
filter = re._subx(self, repl) |
| 111 |
start = 0 |
| 112 |
sublist = [] |
| 113 |
force_unicode = (isinstance(string, unicode) or |
| 114 |
isinstance(repl, unicode)) |
| 115 |
n = last_pos = 0 |
| 116 |
while not count or n < count: |
| 117 |
match = rsre_core.search(self._code, string, start) |
| 118 |
if match is None: |
| 119 |
break |
| 120 |
if last_pos < match.match_start: |
| 121 |
sublist.append(string[last_pos:match.match_start]) |
| 122 |
if not (last_pos == match.match_start |
| 123 |
== match.match_end and n > 0): |
| 124 |
|
| 125 |
if callable(filter): |
| 126 |
piece = filter(self._make_match(match)) |
| 127 |
else: |
| 128 |
piece = filter |
| 129 |
sublist.append(piece) |
| 130 |
last_pos = match.match_end |
| 131 |
n += 1 |
| 132 |
elif last_pos >= len(string): |
| 133 |
break |
| 134 |
|
| 135 |
start = match.match_end |
| 136 |
if start == match.match_start: |
| 137 |
start += 1 |
| 138 |
|
| 139 |
if last_pos < len(string): |
| 140 |
sublist.append(string[last_pos:]) |
| 141 |
|
| 142 |
if n == 0: |
| 143 |
|
| 144 |
return string, n |
| 145 |
|
| 146 |
if force_unicode: |
| 147 |
item = u"".join(sublist) |
| 148 |
else: |
| 149 |
item = "".join(sublist) |
| 150 |
return item, n |
| 151 |
|
| 152 |
@not_rpython |
| 153 |
def sub(self, repl, string, count=0): |
| 154 |
item, n = self.subn(repl, string, count) |
| 155 |
return item |
| 156 |
|
| 157 |
def split(self, string, maxsplit=0): |
| 158 |
splitlist = [] |
| 159 |
start = 0 |
| 160 |
n = 0 |
| 161 |
last = 0 |
| 162 |
while not maxsplit or n < maxsplit: |
| 163 |
match = rsre_core.search(self._code, string, start) |
| 164 |
if match is None: |
| 165 |
break |
| 166 |
if match.match_start == match.match_end: |
| 167 |
if match.match_start == len(string): |
| 168 |
break |
| 169 |
start = match.match_end + 1 |
| 170 |
continue |
| 171 |
splitlist.append(string[last:match.match_start]) |
| 172 |
|
| 173 |
if self.groups: |
| 174 |
match1 = self._make_match(match) |
| 175 |
splitlist.extend(match1.groups(None)) |
| 176 |
n += 1 |
| 177 |
last = start = match.match_end |
| 178 |
splitlist.append(string[last:]) |
| 179 |
return splitlist |
| 180 |
|
| 181 |
def scanner(self, string, start=0, end=sys.maxint): |
| 182 |
return SREScanner(self, string, start, end) |
| 183 |
|
| 184 |
def _make_match(self, res): |
| 185 |
if res is None: |
| 186 |
return None |
| 187 |
return RSREMatch(self, res) |
| 188 |
|
| 189 |
|
| 190 |
class RSREMatch(object): |
| 191 |
|
| 192 |
def __init__(self, pattern, ctx): |
| 193 |
self.re = pattern |
| 194 |
self._ctx = ctx |
| 195 |
|
| 196 |
def span(self, groupnum=0): |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
return self._ctx.span(groupnum) |
| 201 |
|
| 202 |
def start(self, groupnum=0): |
| 203 |
return self.span(groupnum)[0] |
| 204 |
|
| 205 |
def end(self, groupnum=0): |
| 206 |
return self.span(groupnum)[1] |
| 207 |
|
| 208 |
def group(self, group=0): |
| 209 |
frm, to = self.span(group) |
| 210 |
if 0 <= frm <= to: |
| 211 |
return self._ctx._string[frm:to] |
| 212 |
else: |
| 213 |
return None |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
def groups(self, default=None): |
| 229 |
fmarks = self._ctx.flatten_marks() |
| 230 |
grps = [] |
| 231 |
for i in range(1, self.re.groups+1): |
| 232 |
grp = self.group(i) |
| 233 |
if grp is None: grp = default |
| 234 |
grps.append(grp) |
| 235 |
if not we_are_translated(): |
| 236 |
grps = tuple(grps) |
| 237 |
return grps |
| 238 |
|
| 239 |
def groupdict(self, default=None): |
| 240 |
d = {} |
| 241 |
for key, value in self.re.groupindex.iteritems(): |
| 242 |
grp = self.group(value) |
| 243 |
if grp is None: grp = default |
| 244 |
d[key] = grp |
| 245 |
return d |
| 246 |
|
| 247 |
def expand(self, template): |
| 248 |
return re._expand(self.re, self, template) |
| 249 |
|
| 250 |
@property |
| 251 |
def regs(self): |
| 252 |
fmarks = self._ctx.flatten_marks() |
| 253 |
return tuple([(fmarks[i], fmarks[i+1]) |
| 254 |
for i in range(0, len(fmarks), 2)]) |
| 255 |
|
| 256 |
@property |
| 257 |
def lastindex(self): |
| 258 |
self._ctx.flatten_marks() |
| 259 |
if self._ctx.match_lastindex < 0: |
| 260 |
return None |
| 261 |
return self._ctx.match_lastindex // 2 + 1 |
| 262 |
|
| 263 |
@property |
| 264 |
def lastgroup(self): |
| 265 |
lastindex = self.lastindex |
| 266 |
if lastindex < 0 or lastindex >= len(self.re._indexgroup): |
| 267 |
return None |
| 268 |
return self.re._indexgroup[lastindex] |
| 269 |
|
| 270 |
@property |
| 271 |
def string(self): |
| 272 |
return self._ctx._string |
| 273 |
|
| 274 |
@property |
| 275 |
def pos(self): |
| 276 |
return self._ctx.match_start |
| 277 |
|
| 278 |
@property |
| 279 |
def endpos(self): |
| 280 |
return self._ctx.end |
| 281 |
|
| 282 |
|
| 283 |
class SREScanner(object): |
| 284 |
def __init__(self, pattern, string, start, end): |
| 285 |
self.pattern = pattern |
| 286 |
self._string = string |
| 287 |
self._start = start |
| 288 |
self._end = end |
| 289 |
|
| 290 |
def _match_search(self, matcher): |
| 291 |
if self._start > len(self._string): |
| 292 |
return None |
| 293 |
match = matcher(self._string, self._start, self._end) |
| 294 |
if match is None: |
| 295 |
self._start += 1 |
| 296 |
else: |
| 297 |
self._start = match.end() |
| 298 |
if match.start() == self._start: |
| 299 |
self._start += 1 |
| 300 |
return match |
| 301 |
|
| 302 |
def match(self): |
| 303 |
return self._match_search(self.pattern.match) |
| 304 |
|
| 305 |
def search(self): |
| 306 |
return self._match_search(self.pattern.search) |
| 307 |
|
| 308 |
class Scanner: |
| 309 |
|
| 310 |
def __init__(self, lexicon, flags=0): |
| 311 |
from rpython.rlib.rsre.rpy.sre_constants import BRANCH, SUBPATTERN |
| 312 |
from rpython.rlib.rsre.rpy import sre_parse |
| 313 |
self.lexicon = lexicon |
| 314 |
|
| 315 |
p = [] |
| 316 |
s = sre_parse.Pattern() |
| 317 |
s.flags = flags |
| 318 |
for phrase, action in lexicon: |
| 319 |
p.append(sre_parse.SubPattern(s, [ |
| 320 |
(SUBPATTERN, (len(p)+1, sre_parse.parse(phrase, flags))), |
| 321 |
])) |
| 322 |
s.groups = len(p)+1 |
| 323 |
p = sre_parse.SubPattern(s, [(BRANCH, (None, p))]) |
| 324 |
self.scanner = compile(p) |
| 325 |
def scan(self, string): |
| 326 |
result = [] |
| 327 |
append = result.append |
| 328 |
match = self.scanner.scanner(string).match |
| 329 |
i = 0 |
| 330 |
while 1: |
| 331 |
m = match() |
| 332 |
if not m: |
| 333 |
break |
| 334 |
j = m.end() |
| 335 |
if i == j: |
| 336 |
break |
| 337 |
action = self.lexicon[m.lastindex-1][1] |
| 338 |
if callable(action): |
| 339 |
self.match = m |
| 340 |
action = action(self, m.group()) |
| 341 |
if action is not None: |
| 342 |
append(action) |
| 343 |
i = j |
| 344 |
return result, string[i:] |