pub fn module_source() -> &'static str {
r#""""`re.Scanner` for pythonrs's native `re` (see `src/stdlib/pyre.rs`)."""
def _wrapper_groups(lexicon, flags):
"""The group number of each phrase's wrapper group in `Scanner`'s combined
pattern, by lexicon index: every wrapper is followed by its phrase's own
groups, so each one sits that many numbers past the last."""
import re
numbers = {}
gid = 1
for index, (phrase, action) in enumerate(lexicon):
numbers[gid] = index
gid += 1 + re.compile(phrase, flags).groups
return numbers
def _text(s, like):
"""`s` as the same kind of string as the phrase `like` (a bytes lexicon
builds a bytes pattern)."""
return s.encode('ascii') if isinstance(like, bytes) else s
class Scanner:
__module__ = 're'
def __init__(self, lexicon, flags=0):
import re
self.lexicon = lexicon
if not lexicon:
# CPython's compiled code for an empty BRANCH fails `_sre`'s
# validator, which reports it this way.
raise RuntimeError('invalid SRE code')
# combine phrases into a compound pattern; under VERBOSE a phrase may
# end in a `#` comment, which must not swallow the wrapper's `)`
close = '\n)' if flags & re.VERBOSE else ')'
p = [_text('(', phrase) + phrase + _text(close, phrase) for phrase, action in lexicon]
self.scanner = re.compile(_text('|', p[0]).join(p), flags)
def scan(self, string):
import re
result = []
append = result.append
match = self.scanner.scanner(string).match
phrase_of = _wrapper_groups(self.lexicon, self.scanner.flags & re.VERBOSE)
i = 0
while True:
m = match()
if not m:
break
j = m.end()
if i == j:
break
action = self.lexicon[phrase_of[m.lastindex]][1]
if callable(action):
self.match = m
action = action(self, m.group())
if action is not None:
append(action)
i = j
return result, string[i:]
"#
}