From patchwork Sat Jul 27 15:41:56 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Markus Armbruster X-Patchwork-Id: 262532 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 8ADA12C0119 for ; Sun, 28 Jul 2013 07:11:46 +1000 (EST) Received: from localhost ([::1]:48340 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V3BmB-0006Tf-Io for incoming@patchwork.ozlabs.org; Sat, 27 Jul 2013 17:11:43 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38687) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V3Bls-0006SO-NN for qemu-devel@nongnu.org; Sat, 27 Jul 2013 17:11:30 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1V3Aep-0002O3-3F for qemu-devel@nongnu.org; Sat, 27 Jul 2013 16:00:08 -0400 Received: from oxygen.pond.sub.org ([2a01:4f8:121:10e4::3]:55992) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V36dC-0006fy-0f for qemu-devel@nongnu.org; Sat, 27 Jul 2013 11:42:06 -0400 Received: from blackfin.pond.sub.org (p57B0FC86.dip0.t-ipconnect.de [87.176.252.134]) by oxygen.pond.sub.org (Postfix) with ESMTPA id 5B65AA5C68; Sat, 27 Jul 2013 17:42:02 +0200 (CEST) Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id D39E2200B9; Sat, 27 Jul 2013 17:42:01 +0200 (CEST) From: Markus Armbruster To: qemu-devel@nongnu.org Date: Sat, 27 Jul 2013 17:41:56 +0200 Message-Id: <1374939721-7876-5-git-send-email-armbru@redhat.com> X-Mailer: git-send-email 1.7.11.7 In-Reply-To: <1374939721-7876-1-git-send-email-armbru@redhat.com> References: <1374939721-7876-1-git-send-email-armbru@redhat.com> X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2a01:4f8:121:10e4::3 Cc: aliguori@us.ibm.com, akong@redhat.com, mdroth@linux.vnet.ibm.com Subject: [Qemu-devel] [PATCH v2 4/9] qapi.py: Decent syntax error reporting X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake --- scripts/qapi.py | 29 +++++++++++++++++++++++++++-- tests/qapi-schema/test-qapi.py | 2 ++ tests/qapi-schema/unclosed-string.err | 2 +- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/scripts/qapi.py b/scripts/qapi.py index 58e315b..342d16c 100644 --- a/scripts/qapi.py +++ b/scripts/qapi.py @@ -12,6 +12,7 @@ # See the COPYING.LIB file in the top-level directory. from ordereddict import OrderedDict +import sys builtin_types = [ 'str', 'int', 'number', 'bool', @@ -34,6 +35,23 @@ builtin_type_qtypes = { 'uint64': 'QTYPE_QINT', } +class QAPISchemaError(Exception): + def __init__(self, schema, msg): + self.fp = schema.fp + self.msg = msg + self.line = self.col = 1 + for ch in schema.src[0:schema.pos]: + if ch == '\n': + self.line += 1 + self.col = 1 + elif ch == '\t': + self.col = (self.col + 7) % 8 + 1 + else: + self.col += 1 + + def __str__(self): + return "%s:%s:%s: %s" % (self.fp.name, self.line, self.col, self.msg) + class QAPISchema: def __init__(self, fp): @@ -52,6 +70,7 @@ class QAPISchema: while True: bol = self.cursor == 0 or self.src[self.cursor-1] == '\n' self.tok = self.src[self.cursor] + self.pos = self.cursor self.cursor += 1 self.val = None @@ -66,7 +85,8 @@ class QAPISchema: ch = self.src[self.cursor] self.cursor += 1 if ch == '\n': - raise Exception("Mismatched quotes") + raise QAPISchemaError(self, + 'Missing terminating "\'"') if esc: string += ch esc = False @@ -116,7 +136,12 @@ class QAPISchema: return expr def parse_schema(fp): - schema = QAPISchema(fp) + try: + schema = QAPISchema(fp) + except QAPISchemaError as e: + print >>sys.stderr, e + exit(1) + exprs = [] for expr_eval in schema.exprs: diff --git a/tests/qapi-schema/test-qapi.py b/tests/qapi-schema/test-qapi.py index 3280eff..b3d1e1d 100644 --- a/tests/qapi-schema/test-qapi.py +++ b/tests/qapi-schema/test-qapi.py @@ -16,6 +16,8 @@ import sys try: exprs = parse_schema(sys.stdin) +except SystemExit: + raise except: print >>sys.stderr, "Crashed:", sys.exc_info()[0] exit(1) diff --git a/tests/qapi-schema/unclosed-string.err b/tests/qapi-schema/unclosed-string.err index 5af46c2..948d883 100644 --- a/tests/qapi-schema/unclosed-string.err +++ b/tests/qapi-schema/unclosed-string.err @@ -1 +1 @@ -Crashed: +:1:11: Missing terminating "'"