Show file name in error messages from AutogenParser.

This commit is contained in:
Colin Watson 2013-11-26 17:42:56 +00:00
parent ab4f15013a
commit 7e90f5ad25

View file

@ -119,14 +119,15 @@ class AutogenState:
need_idx, need_rbracket, indx_name, have_value, done) = range(12) need_idx, need_rbracket, indx_name, have_value, done) = range(12)
class AutogenParseError(Exception): class AutogenParseError(Exception):
def __init__(self, message, line): def __init__(self, message, path, line):
super(AutogenParseError, self).__init__(message) super(AutogenParseError, self).__init__(message)
self.path = path
self.line = line self.line = line
def __str__(self): def __str__(self):
return ( return (
super(AutogenParseError, self).__str__() + super(AutogenParseError, self).__str__() +
" at line %d" % self.line) " at file %s line %d" % (self.path, self.line))
class AutogenDefinition(list): class AutogenDefinition(list):
def __getitem__(self, key): def __getitem__(self, key):
@ -161,6 +162,7 @@ class AutogenParser:
self.def_stack = [("", self.definitions)] self.def_stack = [("", self.definitions)]
self.curdef = None self.curdef = None
self.new_name = None self.new_name = None
self.cur_path = None
self.cur_line = 0 self.cur_line = 0
@staticmethod @staticmethod
@ -172,6 +174,9 @@ class AutogenParser:
def is_value_name_char(c): def is_value_name_char(c):
return c in ":^-_" or c.isalnum() return c in ":^-_" or c.isalnum()
def error(self, message):
raise AutogenParseError(message, self.cur_file, self.cur_line)
def read_tokens(self, f): def read_tokens(self, f):
data = f.read() data = f.read()
end = len(data) end = len(data)
@ -210,15 +215,13 @@ class AutogenParser:
while True: while True:
offset += 1 offset += 1
if offset >= end: if offset >= end:
raise AutogenParseError( self.error("EOF in quoted string")
"EOF in quoted string", self.cur_line)
if data[offset] == "\n": if data[offset] == "\n":
self.cur_line += 1 self.cur_line += 1
if data[offset] == "\\": if data[offset] == "\\":
offset += 1 offset += 1
if offset >= end: if offset >= end:
raise AutogenParseError( self.error("EOF in quoted string")
"EOF in quoted string", self.cur_line)
if data[offset] == "\n": if data[offset] == "\n":
self.cur_line += 1 self.cur_line += 1
# Proper escaping unimplemented; this can be filled # Proper escaping unimplemented; this can be filled
@ -260,21 +263,19 @@ class AutogenParser:
yield AutogenToken.var_name, s yield AutogenToken.var_name, s
offset = end_name offset = end_name
else: else:
raise AutogenParseError( self.error("Invalid input character '%s'" % c)
"Invalid input character '%s'" % c, self.cur_line)
yield AutogenToken.eof, None yield AutogenToken.eof, None
def do_need_name_end(self, token): def do_need_name_end(self, token):
if len(self.def_stack) > 1: if len(self.def_stack) > 1:
raise AutogenParseError( self.error("Definition blocks were left open")
"Definition blocks were left open", self.cur_line)
def do_need_name_var_name(self, token): def do_need_name_var_name(self, token):
self.new_name = token self.new_name = token
def do_end_block(self, token): def do_end_block(self, token):
if len(self.def_stack) <= 1: if len(self.def_stack) <= 1:
raise AutogenParseError("Too many close braces", self.cur_line) self.error("Too many close braces")
new_name, parent_def = self.def_stack.pop() new_name, parent_def = self.def_stack.pop()
parent_def.append((new_name, self.curdef)) parent_def.append((new_name, self.curdef))
self.curdef = parent_def self.curdef = parent_def
@ -292,7 +293,7 @@ class AutogenParser:
def do_indexed_name(self, token): def do_indexed_name(self, token):
self.new_name = token self.new_name = token
def read_definitions(self, f): def read_definitions_file(self, f):
self.curdef = self.definitions self.curdef = self.definitions
self.cur_line = 0 self.cur_line = 0
state = AutogenState.init state = AutogenState.init
@ -367,12 +368,17 @@ class AutogenParser:
if handler is not None: if handler is not None:
handler(token) handler(token)
else: else:
raise AutogenParseError( self.error(
"Parse error in state %s: unexpected token '%s'" % ( "Parse error in state %s: unexpected token '%s'" % (
state, token), self.cur_line) state, token))
if state == AutogenState.done: if state == AutogenState.done:
break break
def read_definitions(self, path):
self.cur_file = path
with open(path) as f:
self.read_definitions_file(f)
defparser = AutogenParser() defparser = AutogenParser()
# #
@ -791,8 +797,7 @@ parser = OptionParser(usage="%prog DEFINITION-FILES")
_, args = parser.parse_args() _, args = parser.parse_args()
for arg in args: for arg in args:
with open(arg) as f: defparser.read_definitions(arg)
defparser.read_definitions(f)
rules("module", module) rules("module", module)
rules("kernel", kernel) rules("kernel", kernel)