Пример #1
0
def test_escapes():
    """ Ensure we can escape things """
    entry = tokenize(r"""(foo "foo\n")""")[0]
    assert entry[1] == String("foo\n")

    entry = tokenize(r"""(foo r"foo\s")""")[0]
    assert entry[1] == String(r"foo\s")
Пример #2
0
def test_lex_bracket_strings():

    objs = tokenize("#[my delim[hello world]my delim]")
    assert objs == [String("hello world")]
    assert objs[0].brackets == "my delim"

    objs = tokenize("#[[squid]]")
    assert objs == [String("squid")]
    assert objs[0].brackets == ""
Пример #3
0
def test_lex_strings():
    """ Make sure that strings are valid expressions"""
    objs = tokenize('"foo"')
    assert objs == [String("foo")]
    # Make sure backslash-escaped newlines work (see issue #831)
    objs = tokenize(r"""
"a\
bc"
""")
    assert objs == [String("abc")]
Пример #4
0
def test_preprocessor_expression():
    """Test that macro expansion doesn't recurse"""
    obj = macroexpand(
        tokenize('(test (test "one" "two"))')[0], __name__,
        HyASTCompiler(__name__))

    assert type(obj) == List
    assert type(obj[0]) == Expression

    assert obj[0] == Expression([Symbol("test"), String("one"), String("two")])

    obj = List([String("one"), String("two")])
    obj = tokenize('(shill ["one" "two"])')[0][1]
    assert obj == macroexpand(obj, __name__, HyASTCompiler(__name__))
Пример #5
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)
Пример #6
0
def ideas_macro(ETname):
    return Expression([
        Symbol('print'),
        String(r"""

    => (import [sh [figlet]])
    => (figlet "Hi, Hy!")
     _   _ _     _   _       _
    | | | (_)   | | | |_   _| |
    | |_| | |   | |_| | | | | |
    |  _  | |_  |  _  | |_| |_|
    |_| |_|_( ) |_| |_|\__, (_)
            |/         |___/


;;; string things
(.join ", " ["what" "the" "heck"])


;;; this one plays with command line bits
(import [sh [cat grep]])
(-> (cat "/usr/share/dict/words") (grep "-E" "bro$"))


;;; filtering a list w/ a lambda
(filter (fn [x] (= (% x 2) 0)) (range 0 10))


;;; swaggin' functional bits (Python rulez)
(max (map (fn [x] (len x)) ["hi" "my" "name" "is" "paul"]))

""")
    ])
Пример #7
0
def test_macroexpand_source_data():
    # https://github.com/hylang/hy/issues/1944
    ast = Expression([Symbol("#@"), String("a")])
    ast.start_line = 3
    ast.start_column = 5
    bad = macroexpand_1(ast, "hy.core.macros")
    assert bad.start_line == 3
    assert bad.start_column == 5
Пример #8
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)
Пример #9
0
def koan_macro(ETname):
    return Expression([Symbol('print'),
                       String("""
  Ummon asked the head monk, "What sutra are you lecturing on?"
  "The Nirvana Sutra."
  "The Nirvana Sutra has the Four Virtues, hasn't it?"
  "It has."
  Ummon asked, picking up a cup, "How many virtues has this?"
  "None at all," said the monk.
  "But ancient people said it had, didn't they?" said Ummon.
  "What do you think of what they said?"
  Ummon struck the cup and asked, "You understand?"
  "No," said the monk.
  "Then," said Ummon, "You'd better go on with your lectures on the sutra."
""")])
Пример #10
0
def t_string(state, p):
    s = p[0].value
    # Detect any "f" prefix.
    if s.startswith('f') or s.startswith('rf'):
        return t_fstring(state, p)
    # 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.String",
            state,
            p[0],
        )
    return (String(s) if isinstance(s, str) else Bytes(s))
Пример #11
0
def test_lex_expression_strings():
    """ Test that expressions can produce strings """
    objs = tokenize("(foo \"bar\")")
    assert objs == [Expression([Symbol("foo"), String("bar")])]
Пример #12
0
def _format_string(state, p, rest, allow_recursion=True):
    """
    Produces a list of elements
    where each element is either a hy.models.String or a hy.models.FComponent.
    """
    values = []

    while True:
        # Look for the next replacement field, and get the
        # plain text before it.
        match = re.search(r'\{\{?|\}\}?', rest)
        if match:
            literal_chars = rest[:match.start()]
            if match.group() == '}':
                raise LexException.from_lexer(
                    "f-string: single '}' is not allowed", state, p[0])
            if match.group() in ('{{', '}}'):
                # Doubled braces just add a single brace to the text.
                literal_chars += match.group()[0]
            rest = rest[match.end():]
        else:
            literal_chars = rest
            rest = ""
        if literal_chars:
            values.append(String(literal_chars))
        if not rest:
            break
        if match.group() != '{':
            continue

        # Look for the end of the replacement field, allowing
        # one more level of matched braces, but no deeper, and only
        # if we can recurse.
        match = re.match(
            r'(?: \{ [^{}]* \} | [^{}]+ )* \}'
            if allow_recursion else r'[^{}]* \}', rest, re.VERBOSE)
        if not match:
            raise LexException.from_lexer('f-string: mismatched braces', state,
                                          p[0])
        item = rest[:match.end() - 1]
        rest = rest[match.end():]

        # Parse the first form.
        try:
            from . import parse_one_thing
            model, remainder = parse_one_thing(item)
            f_expression = item[:-len(remainder)]
            item = remainder
        except LexException:
            raise
        except ValueError as e:
            raise LexException.from_lexer("f-string: " + str(e), state, p[0])
        subnodes = [model]

        # Check for '=' debugging syntax, reproduce whitespace in output
        eq_sign_match = re.match(r'\s*=\s*', item)
        if eq_sign_match:
            values.append(String(f_expression + eq_sign_match.group()))
            item = item[eq_sign_match.end():]
        else:
            item = item.lstrip()

        # Look for a conversion character.
        conversion = None
        if item.startswith('!'):
            conversion = item[1]
            item = item[2:].lstrip()

        # Look for a format specifier.
        if item.startswith(':'):
            if allow_recursion:
                format_spec = _format_string(state,
                                             p,
                                             item[1:],
                                             allow_recursion=False)
                subnodes.extend(format_spec)
            else:
                subnodes.append(String(item[1:]))
        elif item:
            raise LexException.from_lexer("f-string: trailing junk in field",
                                          state, p[0])
        elif eq_sign_match and not conversion:
            # Python has a special default conversion in this case.
            conversion = "r"

        values.append(FComponent(subnodes, conversion=conversion))

    return values
Пример #13
0
def test_replace_string_type():
    """Test replacing python string"""
    replaced = replace_hy_obj("foo", String("bar"))
    assert replaced == String("foo")
Пример #14
0
def test_preprocessor_simple():
    """Test basic macro expansion"""
    obj = macroexpand(
        tokenize('(test "one" "two")')[0], __name__, HyASTCompiler(__name__))
    assert obj == List([String("one"), String("two")])
    assert type(obj) == List
Пример #15
0
def test_replace_str():
    replaced = replace_hy_obj("foo", String("bar"))
    assert replaced == String("foo")