Ejemplo n.º 1
0
        def make_pattern(rules, flags=0):
            """Compile a rules to single branch with groups."""
            pattern = Pattern()
            pattern.flags = flags
            pattern.subpatterns = [None] * (len(rules) + 1)

            return sre_compile(SubPattern(pattern, [
                (BRANCH, (None, [SubPattern(pattern, [
                    (SUBPATTERN, (group, parse(regex, flags, pattern))),
                ]) for group, (regex, _) in enumerate(rules, 1)]))
            ]))
Ejemplo n.º 2
0
    def __init__(self, rules, flags=0):
        pattern = Pattern()
        pattern.flags = flags
        pattern.groups = len(rules) + 1

        self.rules = [name for name, _ in rules]
        self._scanner = sre_compile(SubPattern(pattern, [
            (BRANCH, (None, [SubPattern(pattern, [
                (SUBPATTERN, (group, parse(regex, flags, pattern))),
            ]) for group, (_, regex) in enumerate(rules, 1)]))
        ])).scanner
Ejemplo n.º 3
0
    def __init__(self, rules, flags=0):
        pattern = Pattern()
        pattern.flags = flags
        pattern.groups = len(rules) + 1

        self.rules = [name for name, _ in rules]
        self._scanner = sre_compile(
            SubPattern(pattern, [(BRANCH, (None, [
                SubPattern(pattern, [
                    (SUBPATTERN, (group, parse(regex, flags, pattern))),
                ]) for group, (_, regex) in enumerate(rules, 1)
            ]))])).scanner
        self.at = []  # array dei token
        self.idx = 0  # indice su at del token corrente
Ejemplo n.º 4
0
    def __init__(self, rules, flags=0):
        # Convert the flags to an int if they are not already
        # https://github.com/python/cpython/commit/c1c47c166b1012d34f2c6e111ee9ccb5c4d12de7
        if isinstance(flags, RegexFlag):
            flags = flags.value

        pattern = Pattern()
        pattern.flags = flags
        pattern.groups = len(rules) + 1

        _og = pattern.opengroup
        pattern.opengroup = lambda n: _og('\x00'.join((name, n)) if n else n)

        self.rules = []
        subpatterns = []
        for group, (name, regex) in enumerate(rules, 1):
            last_group = pattern.groups - 1
            subpatterns.append(SubPattern(pattern, [
                (SUBPATTERN, (group, *append_subpattern_extra_flags,
                    parse(regex, flags, pattern))),
            ]))
            self.rules.append((name, last_group, pattern.groups - 1))

        self._scanner = sre_compile(SubPattern(
            pattern, [(BRANCH, (None, subpatterns))])).scanner
Ejemplo n.º 5
0
    def __init__(self, flags=0, with_whitespaces=False):
        self.lexicon = get_lexicon(PYTHON_VERSION_LESS_THAN_3)
        self.groups = Groups

        p = []
        s = Pattern()
        s.flags = flags
        if PYTHON_VERSION_LESS_THAN_3:
            s.groups = len(self.lexicon) + 1
            for group, (phrase, name) in enumerate(self.lexicon, 1):
                p.append(
                    SubPattern(s, [
                        (SUBPATTERN, (group, parse(phrase, flags))),
                    ]))  # append subpattern
                self.lexicon.append((group - 1, name))
        else:
            for phrase, name in self.lexicon:
                gid = s.opengroup()
                p.append(
                    SubPattern(s, [
                        (SUBPATTERN, (gid, 0, 0, parse(phrase, flags))),
                    ]))
                s.closegroup(gid, p[-1])

        p = SubPattern(s, [(BRANCH, (None, p))])
        self.scanner = sre_compile(p).scanner
        self.with_whitespaces = with_whitespaces
Ejemplo n.º 6
0
    def __init__(self, rules, flags=0):
        pattern = Pattern()
        pattern.flags = flags
        if sys.version_info < (3, 0):
            pattern.groups = len(rules) + 1
        _og = pattern.opengroup
        pattern.opengroup = lambda n: _og(n and '%s\x00%s' % (name, n) or n)

        self.rules = []
        subpatterns = []
        for group, (name, regex) in enumerate(rules, 1):
            last_group = pattern.groups - 1
            subpatterns.append(
                SubPattern(pattern, [(SUBPATTERN, (group, parse(regex, flags,
                                                                pattern))), ]))
            self.rules.append((name, last_group, pattern.groups - 1))
        self._scanner = sre_compile(
            SubPattern(pattern, [(BRANCH, (None, subpatterns))])).scanner
Ejemplo n.º 7
0
def create_subpattern(seq=None):
    state = Pattern()
    return SubPattern(state, seq)