From: Colin Watson <cjwatson@ubuntu.com>
To: grub-devel@gnu.org
Subject: [PATCH 2/3] Show file name in error messages from AutogenParser.
Date: Tue, 26 Nov 2013 20:25:23 +0000 [thread overview]
Message-ID: <20131126202523.GC1228@riva.ucam.org> (raw)
In-Reply-To: <20131126202446.GA1228@riva.ucam.org>
---
gentpl.py | 37 +++++++++++++++++++++----------------
1 file changed, 21 insertions(+), 16 deletions(-)
diff --git a/gentpl.py b/gentpl.py
index f0f4493..5bee43a 100644
--- a/gentpl.py
+++ b/gentpl.py
@@ -119,14 +119,15 @@ class AutogenState:
need_idx, need_rbracket, indx_name, have_value, done) = range(12)
class AutogenParseError(Exception):
- def __init__(self, message, line):
+ def __init__(self, message, path, line):
super(AutogenParseError, self).__init__(message)
+ self.path = path
self.line = line
def __str__(self):
return (
super(AutogenParseError, self).__str__() +
- " at line %d" % self.line)
+ " at file %s line %d" % (self.path, self.line))
class AutogenDefinition(list):
def __getitem__(self, key):
@@ -161,6 +162,7 @@ class AutogenParser:
self.def_stack = [("", self.definitions)]
self.curdef = None
self.new_name = None
+ self.cur_path = None
self.cur_line = 0
@staticmethod
@@ -172,6 +174,9 @@ class AutogenParser:
def is_value_name_char(c):
return c in ":^-_" or c.isalnum()
+ def error(self, message):
+ raise AutogenParseError(message, self.cur_file, self.cur_line)
+
def read_tokens(self, f):
data = f.read()
end = len(data)
@@ -210,15 +215,13 @@ class AutogenParser:
while True:
offset += 1
if offset >= end:
- raise AutogenParseError(
- "EOF in quoted string", self.cur_line)
+ self.error("EOF in quoted string")
if data[offset] == "\n":
self.cur_line += 1
if data[offset] == "\\":
offset += 1
if offset >= end:
- raise AutogenParseError(
- "EOF in quoted string", self.cur_line)
+ self.error("EOF in quoted string")
if data[offset] == "\n":
self.cur_line += 1
# Proper escaping unimplemented; this can be filled
@@ -260,21 +263,19 @@ class AutogenParser:
yield AutogenToken.var_name, s
offset = end_name
else:
- raise AutogenParseError(
- "Invalid input character '%s'" % c, self.cur_line)
+ self.error("Invalid input character '%s'" % c)
yield AutogenToken.eof, None
def do_need_name_end(self, token):
if len(self.def_stack) > 1:
- raise AutogenParseError(
- "Definition blocks were left open", self.cur_line)
+ self.error("Definition blocks were left open")
def do_need_name_var_name(self, token):
self.new_name = token
def do_end_block(self, token):
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()
parent_def.append((new_name, self.curdef))
self.curdef = parent_def
@@ -292,7 +293,7 @@ class AutogenParser:
def do_indexed_name(self, token):
self.new_name = token
- def read_definitions(self, f):
+ def read_definitions_file(self, f):
self.curdef = self.definitions
self.cur_line = 0
state = AutogenState.init
@@ -367,12 +368,17 @@ class AutogenParser:
if handler is not None:
handler(token)
else:
- raise AutogenParseError(
+ self.error(
"Parse error in state %s: unexpected token '%s'" % (
- state, token), self.cur_line)
+ state, token))
if state == AutogenState.done:
break
+ def read_definitions(self, path):
+ self.cur_file = path
+ with open(path) as f:
+ self.read_definitions_file(f)
+
defparser = AutogenParser()
#
@@ -791,8 +797,7 @@ parser = OptionParser(usage="%prog DEFINITION-FILES")
_, args = parser.parse_args()
for arg in args:
- with open(arg) as f:
- defparser.read_definitions(f)
+ defparser.read_definitions(arg)
rules("module", module)
rules("kernel", kernel)
--
1.8.4.3
next prev parent reply other threads:[~2013-11-26 20:25 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-11-26 20:24 [PATCH 0/3] Eliminate the use of Autogen Colin Watson
2013-11-26 20:25 ` [PATCH 1/3] Generate Makefile.*.am directly from gentpl.py, eliminating the use of Autogen. The Autogen definitions files remain intact as they offer a useful abstraction Colin Watson
2013-11-26 20:25 ` Colin Watson [this message]
2013-11-26 20:25 ` [PATCH 3/3] Handle #if/#endif and C-style comments in AutoGen definitions files Colin Watson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20131126202523.GC1228@riva.ucam.org \
--to=cjwatson@ubuntu.com \
--cc=grub-devel@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).