def _scan_init(self): p = [] try: state = sre_parse.State() except AttributeError: # python < 3.8.0 state = sre_parse.Pattern() for name, regx in self.LEXICON: group = state.opengroup(name) re = sre_parse.parse(regx) data = [(SUBPATTERN, (group, 0, 0, re))] p.append(sre_parse.SubPattern(state, data)) state.closegroup(group, p[-1]) data = [(BRANCH, (None, p))] return state, data
def __init__(self, lexicon, flags=0): from sre_constants import BRANCH, SUBPATTERN if isinstance(flags, RegexFlag): flags = flags.value self.lexicon = lexicon # combine phrases into a compound pattern p = [] s = sre_parse.State() s.flags = flags for phrase, action in lexicon: gid = s.opengroup() p.append(sre_parse.SubPattern(s, [ (SUBPATTERN, (gid, 0, 0, sre_parse.parse(phrase, flags))), ])) s.closegroup(gid, p[-1]) p = sre_parse.SubPattern(s, [(BRANCH, (None, p))]) self.scanner = sre_compile.compile(p)
# -------------------------------------------------------------------- # experimental stuff (see python-dev discussions for details) class Scanner: def __init__(self, lexicon, flags=0): from sre_constants import BRANCH, SUBPATTERN if isinstance(flags, RegexFlag): flags = flags.value self.lexicon = lexicon # combine phrases into a compound pattern p = [] <<<<<<< HEAD s = sre_parse.Pattern() ======= s = sre_parse.State() >>>>>>> 716b15a33aed978ded8a6bde17855cb6c6aa7f78 s.flags = flags for phrase, action in lexicon: gid = s.opengroup() p.append(sre_parse.SubPattern(s, [ (SUBPATTERN, (gid, 0, 0, sre_parse.parse(phrase, flags))), ])) s.closegroup(gid, p[-1]) p = sre_parse.SubPattern(s, [(BRANCH, (None, p))]) self.scanner = sre_compile.compile(p) def scan(self, string): result = [] append = result.append match = self.scanner.scanner(string).match i = 0