Beispiel #1
0
    def apply_on(self, text, aliases=None):
        initial_aliases = dict(aliases) if aliases is not None else dict()

        temp_match = SequenceMatch(
            self.content,
            BetweenMatch(),
            EndAnchorMatch(),
        )
        initial_context = MatchContext(
            text=text,
            position=0,
            aliases=initial_aliases,
            matched_text=None,
            anchored=True,
        )

        for _ in range(MAX_ITERATIONS):
            solution = next(temp_match.execute(initial_context), None)
            if solution is None:
                return ApplyRuleResult(text=text, aliases=initial_aliases)

            if solution.aliases != initial_context.aliases:
                initial_context = initial_context._replace(aliases=solution.aliases)
                continue

            return ApplyRuleResult(text=solution.matched_text, aliases=solution.aliases)

        raise AliasDependenciesTooComplexError()
Beispiel #2
0
    def _execute_immaterial_match_impl(self, context):
        temp_match = SequenceMatch(
            RepeatMatch(
                AlternativesMatch(
                    self.term,
                    BetweenMatch(),
                ),
                0,
                None,
            ),
            BetweenMatch(),
            EndAnchorMatch(),
        )
        temp_context = context._replace(anchored=True)

        solution = next(temp_match.execute(temp_context), None)

        if solution is not None:
            yield context._replace(
                text=context.text[:context.position]+solution.matched_text,
                matched_text='',
                aliases=solution.aliases,
            )
        else:
            yield context._replace(matched_text='')
Beispiel #3
0
def p_sequence_match_empty(p):
    """sequence_match : """
    p[0] = SequenceMatch()

    try:
        p[0].source_span = SourceSpan.right_after(p[-1].source_span)
    except AttributeError:
        p[0].source_span = SourceSpan.at_beginning()