예제 #1
0
def t_identifier(p):
    obj = p[0].value

    try:
        return HyInteger(obj)
    except ValueError:
        pass

    try:
        return HyFloat(obj)
    except ValueError:
        pass

    if obj != 'j':
        try:
            return HyComplex(obj)
        except ValueError:
            pass

    table = {
        "true": "True",
        "false": "False",
        "nil": "None",
        "null": "None",
    }

    if obj in table:
        return HySymbol(table[obj])

    if obj.startswith(":"):
        return HyKeyword(obj)

    if obj.startswith("&"):
        return HyLambdaListKeyword(obj)

    def mangle(p):
        if p.startswith("*") and p.endswith("*") and p not in ("*", "**"):
            p = p[1:-1].upper()

        if "-" in p and p != "-":
            p = p.replace("-", "_")

        if p.endswith("?") and p != "?":
            p = "is_%s" % (p[:-1])

        return p

    obj = ".".join([mangle(part) for part in obj.split(".")])

    return HySymbol(obj)
예제 #2
0
def _resolve_atom(obj):
    """
    Resolve a bare atom into one of the following (in order):

        - Integer
        - LambdaListKeyword
        - Float
        - Complex
        - Symbol
    """
    try:
        return HyInteger(obj)
    except ValueError:
        pass

    if obj.startswith("&"):
        return HyLambdaListKeyword(obj)

    try:
        return HyFloat(obj)
    except ValueError:
        pass

    if obj != "j":
        try:
            return HyComplex(obj)
        except ValueError:
            pass

    table = {
        "true": "True",
        "false": "False",
        "null": "None",
    }

    if obj in table:
        return HySymbol(table[obj])

    if obj.startswith(":"):
        return HyKeyword(obj)

    if obj.startswith("*") and obj.endswith("*") and obj not in ("*", "**"):
        obj = obj[1:-1].upper()

    if "-" in obj and obj != "-":
        obj = obj.replace("-", "_")

    return HySymbol(obj)
예제 #3
0
def t_identifier(p):
    obj = p[0].value

    try:
        return HyInteger(obj)
    except ValueError:
        pass

    try:
        return HyFloat(obj)
    except ValueError:
        pass

    if obj != 'j':
        try:
            return HyComplex(obj)
        except ValueError:
            pass

    table = {
        "true": "True",
        "false": "False",
        "null": "None",
    }

    if obj in table:
        return HySymbol(table[obj])

    if obj.startswith(":"):
        return HyKeyword(obj)

    if obj.startswith("&"):
        return HyLambdaListKeyword(obj)

    if obj.startswith("*") and obj.endswith("*") and obj not in ("*", "**"):
        obj = obj[1:-1].upper()

    if "-" in obj and obj != "-":
        obj = obj.replace("-", "_")

    return HySymbol(obj)
예제 #4
0
파일: test_lex.py 프로젝트: pyos/hy
def test_lex_lambda_list_keyword():
    """ Make sure expressions can produce lambda list keywords """
    objs = tokenize("(x &rest xs)")
    assert objs == [HyExpression([HySymbol("x"),
                                  HyLambdaListKeyword("&rest"),
                                  HySymbol("xs")])]