Use ._syntax_error in place of HyTypeError.

And standardize the indentation of these calls.
This commit is contained in:
Kodi Arfer 2018-12-08 15:36:13 -05:00
parent 62638b44a3
commit 902926c543

View File

@ -436,6 +436,9 @@ class HyASTCompiler(object):
except Exception as e:
raise_empty(HyCompileError, e, sys.exc_info()[2])
def _syntax_error(self, expr, message):
return HyTypeError(expr, message)
def _compile_collect(self, exprs, with_kwargs=False, dict_display=False,
oldpy_unpack=False):
"""Collect the expression contexts from a list of compiled expression.
@ -455,8 +458,8 @@ class HyASTCompiler(object):
if not PY35 and oldpy_unpack and is_unpack("iterable", expr):
if oldpy_starargs:
raise HyTypeError(expr, "Pythons < 3.5 allow only one "
"`unpack-iterable` per call")
raise self._syntax_error(expr,
"Pythons < 3.5 allow only one `unpack-iterable` per call")
oldpy_starargs = self.compile(expr[1])
ret += oldpy_starargs
oldpy_starargs = oldpy_starargs.force_expr
@ -472,21 +475,20 @@ class HyASTCompiler(object):
expr, arg=None, value=ret.force_expr))
elif oldpy_unpack:
if oldpy_kwargs:
raise HyTypeError(expr, "Pythons < 3.5 allow only one "
"`unpack-mapping` per call")
raise self._syntax_error(expr,
"Pythons < 3.5 allow only one `unpack-mapping` per call")
oldpy_kwargs = ret.force_expr
elif with_kwargs and isinstance(expr, HyKeyword):
try:
value = next(exprs_iter)
except StopIteration:
raise HyTypeError(expr,
"Keyword argument {kw} needs "
"a value.".format(kw=expr))
raise self._syntax_error(expr,
"Keyword argument {kw} needs a value.".format(kw=expr))
if not expr:
raise HyTypeError(expr, "Can't call a function with the "
"empty keyword")
raise self._syntax_error(expr,
"Can't call a function with the empty keyword")
compiled_value = self.compile(value)
ret += compiled_value
@ -527,8 +529,8 @@ class HyASTCompiler(object):
if isinstance(name, Result):
if not name.is_expr():
raise HyTypeError(expr,
"Can't assign or delete a non-expression")
raise self._syntax_error(expr,
"Can't assign or delete a non-expression")
name = name.expr
if isinstance(name, (ast.Tuple, ast.List)):
@ -547,9 +549,8 @@ class HyASTCompiler(object):
new_name = ast.Starred(
value=self._storeize(expr, name.value, func))
else:
raise HyTypeError(expr,
"Can't assign or delete a %s" %
type(expr).__name__)
raise self._syntax_error(expr,
"Can't assign or delete a %s" % type(expr).__name__)
new_name.ctx = func()
ast.copy_location(new_name, name)
@ -575,9 +576,8 @@ class HyASTCompiler(object):
op = unmangle(ast_str(form[0]))
if level == 0 and op in ("unquote", "unquote-splice"):
if len(form) != 2:
raise HyTypeError(form,
("`%s' needs 1 argument, got %s" %
op, len(form) - 1))
raise HyTypeError("`%s' needs 1 argument, got %s" % op, len(form) - 1,
self.filename, form, self.source)
return set(), form[1], op == "unquote-splice"
elif op == "quasiquote":
level += 1
@ -629,7 +629,8 @@ class HyASTCompiler(object):
@special("unpack-iterable", [FORM])
def compile_unpack_iterable(self, expr, root, arg):
if not PY3:
raise HyTypeError(expr, "`unpack-iterable` isn't allowed here")
raise self._syntax_error(expr,
"`unpack-iterable` isn't allowed here")
ret = self.compile(arg)
ret += asty.Starred(expr, value=ret.force_expr, ctx=ast.Load())
return ret
@ -659,7 +660,8 @@ class HyASTCompiler(object):
if cause is not None:
if not PY3:
raise HyTypeError(expr, "raise from only supported in python 3")
raise self._syntax_error(expr,
"raise from only supported in python 3")
cause = self.compile(cause)
ret += cause
cause = cause.force_expr
@ -706,13 +708,11 @@ class HyASTCompiler(object):
# Using (else) without (except) is verboten!
if orelse and not handlers:
raise HyTypeError(
expr,
raise self._syntax_error(expr,
"`try' cannot have `else' without `except'")
# Likewise a bare (try) or (try BODY).
if not (handlers or finalbody):
raise HyTypeError(
expr,
raise self._syntax_error(expr,
"`try' must have an `except' or `finally' clause")
returnable = Result(
@ -963,7 +963,8 @@ class HyASTCompiler(object):
def compile_decorate_expression(self, expr, name, args):
decs, fn = args[:-1], self.compile(args[-1])
if not fn.stmts or not isinstance(fn.stmts[-1], _decoratables):
raise HyTypeError(args[-1], "Decorated a non-function")
raise self._syntax_error(args[-1],
"Decorated a non-function")
decs, ret, _ = self._compile_collect(decs)
fn.stmts[-1].decorator_list = decs + fn.stmts[-1].decorator_list
return ret + fn
@ -1194,8 +1195,8 @@ class HyASTCompiler(object):
if (HySymbol('*'), None) in kids:
if len(kids) != 1:
star = kids[kids.index((HySymbol('*'), None))][0]
raise HyTypeError(star, "* in an import name list "
"must be on its own")
raise self._syntax_error(star,
"* in an import name list must be on its own")
else:
assignments = [(k, v or k) for k, v in kids]
@ -1390,15 +1391,15 @@ class HyASTCompiler(object):
if str_name in (["None"] + (["True", "False"] if PY3 else [])):
# Python 2 allows assigning to True and False, although
# this is rarely wise.
raise HyTypeError(name,
"Can't assign to `%s'" % str_name)
raise self._syntax_error(name,
"Can't assign to `%s'" % str_name)
result = self.compile(result)
ld_name = self.compile(name)
if isinstance(ld_name.expr, ast.Call):
raise HyTypeError(name,
"Can't assign to a callable: `%s'" % str_name)
raise self._syntax_error(name,
"Can't assign to a callable: `%s'" % str_name)
if (result.temp_variables
and isinstance(name, HySymbol)
@ -1474,7 +1475,8 @@ class HyASTCompiler(object):
mandatory, optional, rest, kwonly, kwargs = params
optional, defaults, ret = self._parse_optional_args(optional)
if kwonly is not None and not PY3:
raise HyTypeError(params, "&kwonly parameters require Python 3")
raise self._syntax_error(params,
"&kwonly parameters require Python 3")
kwonly, kw_defaults, ret2 = self._parse_optional_args(kwonly, True)
ret += ret2
main_args = mandatory + optional
@ -1627,8 +1629,8 @@ class HyASTCompiler(object):
return self.compile(expr)
if not expr:
raise HyTypeError(
expr, "empty expressions are not allowed at top level")
raise self._syntax_error(expr,
"empty expressions are not allowed at top level")
args = list(expr)
root = args.pop(0)
@ -1646,8 +1648,7 @@ class HyASTCompiler(object):
sroot in (mangle(","), mangle(".")) or
not any(is_unpack("iterable", x) for x in args)):
if sroot in _bad_roots:
raise HyTypeError(
expr,
raise self._syntax_error(expr,
"The special form '{}' is not allowed here".format(root))
# `sroot` is a special operator. Get the build method and
# pattern-match the arguments.
@ -1655,11 +1656,10 @@ class HyASTCompiler(object):
try:
parse_tree = pattern.parse(args)
except NoParseError as e:
raise HyTypeError(
raise self._syntax_error(
expr[min(e.state.pos + 1, len(expr) - 1)],
"parse error for special form '{}': {}".format(
root,
e.msg.replace("<EOF>", "end of form")))
root, e.msg.replace("<EOF>", "end of form")))
return Result() + build_method(
self, expr, unmangle(sroot), *parse_tree)
@ -1681,13 +1681,13 @@ class HyASTCompiler(object):
FORM +
many(FORM)).parse(args)
except NoParseError:
raise HyTypeError(
expr, "attribute access requires object")
raise self._syntax_error(expr,
"attribute access requires object")
# Reconstruct `args` to exclude `obj`.
args = [x for p in kws for x in p] + list(rest)
if is_unpack("iterable", obj):
raise HyTypeError(
obj, "can't call a method on an unpacking form")
raise self._syntax_error(obj,
"can't call a method on an unpacking form")
func = self.compile(HyExpression(
[HySymbol(".").replace(root), obj] +
attrs))
@ -1725,16 +1725,12 @@ class HyASTCompiler(object):
glob, local = symbol.rsplit(".", 1)
if not glob:
raise HyTypeError(symbol, 'cannot access attribute on '
'anything other than a name '
'(in order to get attributes of '
'expressions, use '
'`(. <expression> {attr})` or '
'`(.{attr} <expression>)`)'.format(
attr=local))
raise self._syntax_error(symbol,
'cannot access attribute on anything other than a name (in order to get attributes of expressions, use `(. <expression> {attr})` or `(.{attr} <expression>)`)'.format(attr=local))
if not local:
raise HyTypeError(symbol, 'cannot access empty attribute')
raise self._syntax_error(symbol,
'cannot access empty attribute')
glob = HySymbol(glob).replace(symbol)
ret = self.compile_symbol(glob)