error message voodoo

This commit is contained in:
Paul Tagliamonte 2012-12-22 23:34:00 -05:00
parent 472da09f51
commit 8a601221fb
3 changed files with 45 additions and 17 deletions

View File

@ -293,6 +293,11 @@ class AST27Converter(object):
t = type(tree)
handler = self.table[t]
ret = handler(tree)
for node in ast.walk(ret):
node.lineno = tree.line
node.col_offset = tree.column
return ret
@ -304,10 +309,15 @@ def forge_ast(name, forest):
for tree in forest:
ret = conv.render(tree)
if not isinstance(ret, ast.stmt):
ret = ast.Expr(value=ret)
ret = ast.Expr(
value=ret,
lineno=ret.lineno,
col_offset=ret.col_offset
)
statements.append(ret)
return ast.fix_missing_locations(ast.Module(body=statements))
return ast.Module(body=statements)
#return ast.fix_missing_locations(ast.Module(body=statements))
def forge_module(name, fpath, forest):

View File

@ -4,10 +4,23 @@ class Machine(object):
self.nodes = []
self.state = state(self)
self.state.enter()
self.line = 1
self.column = 1
def add_node(self, node):
node.line = self.line
node.column = self.column
self.nodes.append(node)
def process(self, buf):
for i in range(0, len(buf)):
char = buf[i]
self.column += 1
if char == "\n":
self.line += 1
self.column = 0
nx = self.state.process(char)
if nx:
# print "New state: %s" % (nx)

View File

@ -12,18 +12,23 @@ from hy.lang.map import HYMap
WHITESPACE = [" ", "\t", "\n", "\r"]
def _resolve_atom(value):
def _resolve_atom(value, self):
def _mangle(obj):
obj.line = self.machine.line
obj.column = self.machine.column
return obj
if value == "true":
return HYBool(True)
return _mangle(HYBool(True))
elif value == "false":
return HYBool(False)
return _mangle(HYBool(False))
try:
return HYNumber(value)
return _mangle(HYNumber(value))
except ValueError:
pass
return HYSymbol(value)
return _mangle(HYSymbol(value))
class State(object):
@ -74,13 +79,13 @@ class Expression(State):
def exit(self):
if self.bulk:
self.nodes.append(_resolve_atom(self.bulk))
self.nodes.append(_resolve_atom(self.bulk, self))
self.machine.nodes.append(self.nodes)
self.machine.add_node(self.nodes)
def commit(self):
if self.bulk.strip() != "":
self.nodes.append(_resolve_atom(self.bulk))
self.nodes.append(_resolve_atom(self.bulk, self))
self.bulk = ""
def p(self, x):
@ -101,12 +106,12 @@ class List(State):
def exit(self):
if self.bulk:
self.nodes.append(_resolve_atom(self.bulk))
self.machine.nodes.append(self.nodes)
self.nodes.append(_resolve_atom(self.bulk, self))
self.machine.add_node(self.nodes)
def commit(self):
if self.bulk.strip() != "":
self.nodes.append(_resolve_atom(self.bulk))
self.nodes.append(_resolve_atom(self.bulk, self))
self.bulk = ""
def p(self, x):
@ -127,7 +132,7 @@ class Map(State):
def exit(self):
if self.bulk:
self.nodes.append(_resolve_atom(self.bulk))
self.nodes.append(_resolve_atom(self.bulk, self))
if (len(self.nodes) % 2) != 0:
raise Exception("Hash map is fucked")
@ -137,11 +142,11 @@ class Map(State):
hmap = zip(i, i)
for key, val in hmap:
ret[key] = val
self.machine.nodes.append(ret)
self.machine.add_node(ret)
def commit(self):
if self.bulk.strip() != "":
self.nodes.append(_resolve_atom(self.bulk))
self.nodes.append(_resolve_atom(self.bulk, self))
self.bulk = ""
def p(self, x):
@ -163,7 +168,7 @@ class String(State):
self.esc = False
def exit(self):
self.machine.nodes.append(HYString(self.buf))
self.machine.add_node(HYString(self.buf))
def p(self, x):
if x == "\\":