Improve shown errors

This commit is contained in:
ZyX 2013-04-13 14:47:39 +04:00
parent fee328666f
commit ae691b7cd8
3 changed files with 66 additions and 7 deletions

View File

@ -894,10 +894,12 @@ def check_args(get_segment_variants, args, data, context, echoerr):
if not count:
hadproblem = True
new_echoerr.echo_all()
echoerr(context='Error while checking segment arguments (key {key})'.format(key=context_key(context)),
context_mark=context[-2][1].mark,
problem='no suitable segments found')
if new_echoerr:
new_echoerr.echo_all()
else:
echoerr(context='Error while checking segment arguments (key {key})'.format(key=context_key(context)),
context_mark=context[-2][1].mark,
problem='no suitable segments found')
return True, False, hadproblem
@ -912,7 +914,7 @@ def get_one_segment_variant(data, context, echoerr):
def get_all_possible_segments(data, context, echoerr):
name = context[-2][0]
module, name = (gen_marked_value(value, name.mark) for value in name.rpartition('.')[::2])
module, name = name.rpartition('.')[::2]
if module:
func = import_segment(name, data, context, echoerr, module=module)
if func:

View File

@ -29,6 +29,9 @@ class Mark:
self.buffer = buffer
self.pointer = pointer
def copy(self):
return Mark(self.name, self.line, self.column, self.buffer, self.pointer)
def get_snippet(self, indent=4, max_length=75):
if self.buffer is None:
return None

View File

@ -1,17 +1,71 @@
__all__ = ['gen_marked_value', 'MarkedValue']
try:
from __builtin__ import unicode
except ImportError:
unicode = str
def gen_new(cls):
def __new__(arg_cls, value, mark):
r = super(arg_cls, arg_cls).__new__(arg_cls, value)
r.mark = mark
r.value = value
return r
return __new__
class MarkedUnicode(unicode):
__new__ = gen_new(unicode)
def _proc_partition(self, part_result):
pointdiff = 1
r = []
for s in part_result:
mark = self.mark.copy()
# XXX Does not work properly with escaped strings, but this requires
# saving much more information in mark.
mark.column += pointdiff
mark.pointer += pointdiff
r.append(MarkedUnicode(s, mark))
pointdiff += len(s)
return tuple(r)
def rpartition(self, sep):
return self._proc_partition(super(MarkedUnicode, self).rpartition(sep))
def partition(self, sep):
return self._proc_partition(super(MarkedUnicode, self).partition(sep))
class MarkedInt(int):
__new__ = gen_new(int)
class MarkedFloat(float):
__new__ = gen_new(float)
class MarkedValue:
def __init__(self, value, mark):
self.mark = mark
self.value = value
specialclasses = {
unicode: MarkedUnicode,
int: MarkedInt,
float: MarkedFloat,
}
classcache = {}
def gen_marked_value(value, mark):
if value.__class__ in classcache:
def gen_marked_value(value, mark, use_special_classes=True):
if use_special_classes and value.__class__ in specialclasses:
Marked = specialclasses[value.__class__]
elif value.__class__ in classcache:
Marked = classcache[value.__class__]
else:
class Marked(MarkedValue):