Ejemplo n.º 1
0
def merge_strings(exp):
    """merge successive strings and string-like terms into a single Str

    WARNING: has side-effects
    """
    # Merge all of the children first - might promote a
    # Str("A") + (Str("B") + Str("C")) into an Str("A") + Str("BC")
    # before we do the merge into Str("ABC")

    if hasattr(exp, "expression"):
        exp.expression = merge_strings(exp.expression)
    elif hasattr(exp, "expressions"):
        subexps = []
        for subexp in exp.expressions:
            subexps.append(merge_strings(subexp))
        exp.expressions = subexps

    if isinstance(exp, Expression.Seq):
        all_strings = 1
        subexps = []
        for subexp in exp.expressions:
            if not subexps:
                subexps.append(subexp)
            elif not is_mergeable(subexps[-1]):
                all_strings = 0
                subexps.append(subexp)
            elif not is_mergeable(subexp):
                all_strings = 0
                subexps.append(subexp)
            else:
                # Previous and current are mergeable
                subexps[-1] = Expression.Str(get_merge_text(subexps[-1]) + \
                                             get_merge_text(subexp))
        if all_strings and subexps:
            assert len(subexps) == 1
            return subexps[0]

    return exp
Ejemplo n.º 2
0
def Str1(s):
    """(s) -> match the literal string"""
    return Expression.Str(s)