コード例 #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
コード例 #2
0
ファイル: test_lex.py プロジェクト: shubhampachori12110095/hy
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 == ""
コード例 #3
0
ファイル: test_lex.py プロジェクト: shubhampachori12110095/hy
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")]
コード例 #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(""))
コード例 #5
0
ファイル: parser.py プロジェクト: 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)
コード例 #6
0
ファイル: cmdline.py プロジェクト: 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"]))

""")
    ])
コード例 #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)
コード例 #8
0
ファイル: test_macro_processor.py プロジェクト: yongfengz/hy
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
コード例 #9
0
ファイル: parser.py プロジェクト: 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))
コード例 #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."
""")])
コード例 #11
0
ファイル: parser.py プロジェクト: vulogov-pulsepoint/hy
def t_bracket_string(p):
    m = bracket_string_re.match(p[0].value)
    delim, content = m.groups()
    return HyString(content, brackets=delim)
コード例 #12
0
ファイル: parser.py プロジェクト: vulogov-pulsepoint/hy
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])
コード例 #13
0
ファイル: parser.py プロジェクト: 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
コード例 #14
0
ファイル: test_lex.py プロジェクト: shubhampachori12110095/hy
def test_lex_expression_strings():
    """ Test that expressions can produce strings """
    objs = tokenize("(foo \"bar\")")
    assert objs == [HyExpression([HySymbol("foo"), HyString("bar")])]
コード例 #15
0
ファイル: test_lex.py プロジェクト: shubhampachori12110095/hy
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
コード例 #16
0
ファイル: test_models.py プロジェクト: zequequiel/hy
def test_replace_string_type():
    """Test replacing python string"""
    replaced = replace_hy_obj("foo", HyString("bar"))
    assert replaced == HyString("foo")
コード例 #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])
コード例 #18
0
 def uni_hystring(s):
     return HyString(literal_eval('u' + s))