2013-12-26 17:36:45 +01:00
|
|
|
# -*- encoding: utf-8 -*-
|
2017-04-27 23:16:57 +02:00
|
|
|
# Copyright 2017 the authors.
|
|
|
|
# This file is part of Hy, which is free software licensed under the Expat
|
|
|
|
# license. See the LICENSE.
|
2013-03-01 04:27:20 +01:00
|
|
|
|
2013-12-26 04:02:20 +01:00
|
|
|
import traceback
|
|
|
|
|
2014-12-06 20:15:59 +01:00
|
|
|
from clint.textui import colored
|
|
|
|
|
2013-03-01 04:27:20 +01:00
|
|
|
|
|
|
|
class HyError(Exception):
|
2013-03-03 02:38:18 +01:00
|
|
|
"""
|
2014-05-02 17:58:42 +02:00
|
|
|
Generic Hy error. All internal Exceptions will be subclassed from this
|
2013-03-03 02:38:18 +01:00
|
|
|
Exception.
|
|
|
|
"""
|
2013-03-01 04:27:20 +01:00
|
|
|
pass
|
Much better version of new error messages.
This version is much simpler.
At the point that the exception is raised, we don't have access to
the actual source, just the current expression. but as the
exception percolates up, we can intercept it, add the source and
the re-raise it.
Then at the final point, in the cmdline handler, we can choose to
let the entire traceback print, or just the simpler, direct error
message.
And even with the full traceback, the last bit is nicely formatted
just like the shorter, simpler message.
The error message is colored if clint is installed, but to avoid
yet another dependency, you get monochrome without clint.
I'm sure there is a better way to do the markup, the current method
is kludgy but works.
I wish there was more shared code between HyTypeError and LexException
but they are kind of different in some fundamental ways.
This doesn't work (yet) with runtime errors generated from Python,
like NameError, but I have a method that can catch NameError and turn it
into a more pleasing output.
Finally, there is no obvious way to raise HyTypeError from pure Hy code,
so methods in core/language.hy throw ugly TypeError/ValueError.
2013-12-22 20:56:03 +01:00
|
|
|
|
|
|
|
|
2013-12-26 04:02:20 +01:00
|
|
|
class HyCompileError(HyError):
|
|
|
|
def __init__(self, exception, traceback=None):
|
|
|
|
self.exception = exception
|
|
|
|
self.traceback = traceback
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
if isinstance(self.exception, HyTypeError):
|
|
|
|
return str(self.exception)
|
|
|
|
if self.traceback:
|
|
|
|
tb = "".join(traceback.format_tb(self.traceback)).strip()
|
|
|
|
else:
|
|
|
|
tb = "No traceback available. 😟"
|
|
|
|
return("Internal Compiler Bug 😱\n⤷ %s: %s\nCompilation traceback:\n%s"
|
|
|
|
% (self.exception.__class__.__name__,
|
|
|
|
self.exception, tb))
|
|
|
|
|
|
|
|
|
|
|
|
class HyTypeError(TypeError):
|
|
|
|
def __init__(self, expression, message):
|
|
|
|
super(HyTypeError, self).__init__(message)
|
|
|
|
self.expression = expression
|
|
|
|
self.message = message
|
|
|
|
self.source = None
|
|
|
|
self.filename = None
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
|
|
|
|
line = self.expression.start_line
|
|
|
|
start = self.expression.start_column
|
|
|
|
end = self.expression.end_column
|
|
|
|
|
|
|
|
source = []
|
|
|
|
if self.source is not None:
|
|
|
|
source = self.source.split("\n")[line-1:self.expression.end_line]
|
|
|
|
|
|
|
|
if line == self.expression.end_line:
|
|
|
|
length = end - start
|
|
|
|
else:
|
|
|
|
length = len(source[0]) - start
|
|
|
|
|
|
|
|
result = ""
|
|
|
|
|
|
|
|
result += ' File "%s", line %d, column %d\n\n' % (self.filename,
|
|
|
|
line,
|
|
|
|
start)
|
|
|
|
|
|
|
|
if len(source) == 1:
|
|
|
|
result += ' %s\n' % colored.red(source[0])
|
|
|
|
result += ' %s%s\n' % (' '*(start-1),
|
|
|
|
colored.green('^' + '-'*(length-1) + '^'))
|
|
|
|
if len(source) > 1:
|
|
|
|
result += ' %s\n' % colored.red(source[0])
|
|
|
|
result += ' %s%s\n' % (' '*(start-1),
|
|
|
|
colored.green('^' + '-'*length))
|
|
|
|
if len(source) > 2: # write the middle lines
|
|
|
|
for line in source[1:-1]:
|
|
|
|
result += ' %s\n' % colored.red("".join(line))
|
|
|
|
result += ' %s\n' % colored.green("-"*len(line))
|
|
|
|
|
|
|
|
# write the last line
|
|
|
|
result += ' %s\n' % colored.red("".join(source[-1]))
|
|
|
|
result += ' %s\n' % colored.green('-'*(end-1) + '^')
|
|
|
|
|
2013-12-26 17:36:45 +01:00
|
|
|
result += colored.yellow("%s: %s\n\n" %
|
|
|
|
(self.__class__.__name__,
|
2015-12-14 21:52:42 +01:00
|
|
|
self.message.encode('utf-8')))
|
2013-12-26 04:02:20 +01:00
|
|
|
|
2015-12-14 21:52:42 +01:00
|
|
|
return result
|
2013-12-26 04:02:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
class HyMacroExpansionError(HyTypeError):
|
|
|
|
pass
|
2015-02-16 23:27:18 +01:00
|
|
|
|
|
|
|
|
|
|
|
class HyIOError(HyError, IOError):
|
|
|
|
"""
|
|
|
|
Trivial subclass of IOError and HyError, to distinguish between
|
2015-08-09 06:04:02 +02:00
|
|
|
IOErrors raised by Hy itself as opposed to Hy programs.
|
2015-02-16 23:27:18 +01:00
|
|
|
"""
|
|
|
|
pass
|