Ejemplo n.º 1
0
def t_bracket_string(state, p):
    m = bracket_string_re.match(p[0].value)
    delim, content = m.groups()
    if delim == 'f' or delim.startswith('f-'):
        values = _format_string(state, p, content)
        return FString(values, brackets=delim)
    return String(content, brackets=delim)
Ejemplo n.º 2
0
def test_invalid_bracket_strings():
    for string, brackets in [("]foo]", "foo"), ("something ]f] else", "f")]:
        with pytest.raises(ValueError):
            String(string, brackets)
    for nodes, brackets in [
        ([String("hello"), String("world ]foo]")], "foo"),
        ([String("something"), FComponent([String("world")]), String("]f]")], "f"),
        ([String("something"), FComponent([Integer(1), String("]f]")])], "f"),
    ]:
        with pytest.raises(ValueError):
            FString(nodes, brackets=brackets)
Ejemplo n.º 3
0
def t_fstring(state, p):
    s = p[0].value
    assert s.startswith('f') or s.startswith('rf')
    assert isinstance(s, str)
    s = s.replace('f', '', 1)
    # Replace the single double quotes with triple double quotes to allow
    # embedded newlines.
    try:
        s = eval(s.replace('"', '"""', 1)[:-1] + '"""')
    except SyntaxError:
        raise LexException.from_lexer(
            f"Can't convert {p[0].value} to a hy.models.FString",
            state,
            p[0],
        )
    # internal parser
    values = _format_string(state, p, s)
    return FString(values)