Ejemplo n.º 1
0
def test_macroexpand_source_data():
    # https://github.com/hylang/hy/issues/1944
    ast = HyString('a')
    ast.start_line = 3
    ast.start_column = 5
    bad = tag_macroexpand(mangle("@"), ast, "hy.core.macros")
    assert bad.start_line == 3
    assert bad.start_column == 5
Ejemplo n.º 2
0
def test_lex_bracket_strings():

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

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

    assert type(obj) == HyList
    assert type(obj[0]) == HyExpression

    assert obj[0] == HyExpression(
        [HySymbol("test"), HyString("one"),
         HyString("two")])

    obj = HyList([HyString("one"), HyString("two")])
    obj = tokenize('(shill ["one" "two"])')[0][1]
    assert obj == macroexpand(obj, HyASTCompiler(""))
Ejemplo n.º 5
0
Archivo: parser.py Proyecto: willkg/hy
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 HyFString(values, brackets=delim)
    return HyString(content, brackets=delim)
Ejemplo n.º 6
0
Archivo: cmdline.py Proyecto: jams2/hy
def ideas_macro(ETname):
    return HyExpression([
        HySymbol('print'),
        HyString(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"]))

""")
    ])
Ejemplo n.º 7
0
def t_bracket_string(state, p):
    m = bracket_string_re.match(p[0].value)
    delim, content = m.groups()
    return HyString(
        content,
        is_format = delim == 'f' or delim.startswith('f-'),
        brackets = delim)
Ejemplo n.º 8
0
def test_macroexpand_source_data():
    # https://github.com/hylang/hy/issues/1944
    ast = HyExpression([HySymbol('#@'), HyString('a')])
    ast.start_line = 3
    ast.start_column = 5
    bad = macroexpand(ast, "hy.core.macros")
    assert bad.start_line == 3
    assert bad.start_column == 5
Ejemplo n.º 9
0
Archivo: parser.py Proyecto: willkg/hy
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(
            "Can't convert {} to a HyString".format(p[0].value), state, p[0])
    return (HyString(s) if isinstance(s, str) else HyBytes(s))
Ejemplo n.º 10
0
def koan_macro(ETname):
    return HyExpression([HySymbol('print'),
                         HyString("""
  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."
""")])
Ejemplo n.º 11
0
def t_bracket_string(p):
    m = bracket_string_re.match(p[0].value)
    delim, content = m.groups()
    return HyString(content, brackets=delim)
Ejemplo n.º 12
0
def hash_other(p):
    # p == [(Token('HASHOTHER', '#foo'), bar)]
    st = p[0].getstr()[1:]
    str_object = HyString(st)
    expr = p[1]
    return HyExpression([HySymbol("dispatch-tag-macro"), str_object, expr])
Ejemplo n.º 13
0
Archivo: parser.py Proyecto: willkg/hy
def _format_string(state, p, rest, allow_recursion=True):
    """
    Produces a list of elements
    where each element is either a HyString or a HyFComponent.
    """
    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(HyString(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, item = parse_one_thing(item)
        except LexException:
            raise
        except ValueError as e:
            raise LexException.from_lexer("f-string: " + str(e), state, p[0])
        subnodes = [model]

        # Look for a conversion character.
        item = item.lstrip()
        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(HyString(item[1:]))
        elif item:
            raise LexException.from_lexer("f-string: trailing junk in field",
                                          state, p[0])

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

    return values
Ejemplo n.º 14
0
def test_lex_expression_strings():
    """ Test that expressions can produce strings """
    objs = tokenize("(foo \"bar\")")
    assert objs == [HyExpression([HySymbol("foo"), HyString("bar")])]
Ejemplo n.º 15
0
def test_tag_macro():
    """Ensure tag macros are handled properly"""
    entry = tokenize("#^()")
    assert entry[0][0] == HySymbol("dispatch_tag_macro")
    assert entry[0][1] == HyString("^")
    assert len(entry[0]) == 3
Ejemplo n.º 16
0
def test_replace_string_type():
    """Test replacing python string"""
    replaced = replace_hy_obj("foo", HyString("bar"))
    assert replaced == HyString("foo")
Ejemplo n.º 17
0
def hash_other(p):
    st = p[0].getstr()[1]
    str_object = HyString(st)
    expr = p[1]
    return HyExpression([HySymbol("dispatch_sharp_macro"), str_object, expr])
Ejemplo n.º 18
0
 def uni_hystring(s):
     return HyString(literal_eval('u' + s))