Remove HyLambdaListKeyword from the parser

It's not a syntactic element and doesn't belong in the parser. Parsing
lambda lists is now handled by the compiler alone.
This commit is contained in:
James King 2014-06-03 21:36:49 -04:00
parent b5a058a3bc
commit 277028cdd5
7 changed files with 10 additions and 65 deletions

View File

@ -23,7 +23,6 @@ from hy.version import __version__, __appname__ # NOQA
from hy.models.expression import HyExpression # NOQA
from hy.models.lambdalist import HyLambdaListKeyword # NOQA
from hy.models.integer import HyInteger # NOQA
from hy.models.keyword import HyKeyword # NOQA
from hy.models.complex import HyComplex # NOQA

View File

@ -24,7 +24,6 @@
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
from hy.models.lambdalist import HyLambdaListKeyword
from hy.models.expression import HyExpression
from hy.models.keyword import HyKeyword
from hy.models.integer import HyInteger
@ -430,6 +429,7 @@ class HyASTCompiler(object):
def _parse_lambda_list(self, exprs):
""" Return FunctionDef parameter values from lambda list."""
ll_keywords = ("&rest", "&optional", "&key", "&kwargs")
ret = Result()
args = []
defaults = []
@ -439,10 +439,7 @@ class HyASTCompiler(object):
for expr in exprs:
if isinstance(expr, HyLambdaListKeyword):
if expr not in expr._valid_types:
raise HyTypeError(expr, "{0} is not a valid "
"lambda-keyword.".format(repr(expr)))
if expr in ll_keywords:
if expr == "&rest" and lambda_keyword is None:
lambda_keyword = expr
elif expr == "&optional":
@ -616,7 +613,7 @@ class HyASTCompiler(object):
return imports, ret.replace(form), False
elif isinstance(form, (HySymbol, HyLambdaListKeyword)):
elif isinstance(form, HySymbol):
return imports, HyExpression([HySymbol(name),
HyString(form)]).replace(form), False

View File

@ -30,7 +30,6 @@ from hy.models.expression import HyExpression
from hy.models.float import HyFloat
from hy.models.integer import HyInteger
from hy.models.keyword import HyKeyword
from hy.models.lambdalist import HyLambdaListKeyword
from hy.models.list import HyList
from hy.models.string import HyString
from hy.models.symbol import HySymbol
@ -271,9 +270,6 @@ def t_identifier(p):
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()

View File

@ -1,39 +0,0 @@
# Copyright (c) 2013 James King <james@agentultra.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
from hy.models.string import HyString
class HyLambdaListKeyword(HyString):
"""
Hy LambdaListKeyword. Demarcates arguments in an argument list.
(defun my-fun (x &rest xs &optional (foo "default string")))
becomes:
def my_fun(x, *xs, foo="default string"):
pass
"""
_valid_types = ["&rest", "&optional", "&key", "&kwargs"]
def __init__(self, string):
self += string

View File

@ -21,7 +21,6 @@
from hy.models.expression import HyExpression
from hy.models.integer import HyInteger
from hy.models.lambdalist import HyLambdaListKeyword
from hy.models.float import HyFloat
from hy.models.complex import HyComplex
from hy.models.symbol import HySymbol
@ -85,14 +84,6 @@ def test_lex_expression_integer():
assert objs == [HyExpression([HySymbol("foo"), HyInteger(2)])]
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")])]
def test_lex_symbols():
""" Make sure that symbols are valid expressions"""
objs = tokenize("foo ")

View File

@ -975,3 +975,10 @@
"NATIVE: test keyword quoting magic"
(assert (= :foo "\ufdd0:foo"))
(assert (= `:foo "\ufdd0:foo")))
(defn test-only-parse-lambda-list-in-defn []
"NATIVE: test lambda lists are only parsed in defn"
(try
(foo [&rest spam] 1)
(catch [NameError] True)
(else (raise AssertionError))))

View File

@ -77,12 +77,6 @@
(assert (= q qq)))
(defn test-quote-lambdalistkeyword []
"NATIVE: test quoting lambda list keywords"
(setv opt (quote &optional))
(assert (isinstance opt hy.HyLambdaListKeyword))
(assert (= (str opt) "&optional")))
(defmacro doodle [&rest body]
`(do ~@body))