9ae3a8
From b63bf0c1731359b15ec94c233bc5b3e11db089b4 Mon Sep 17 00:00:00 2001
9ae3a8
Message-Id: <b63bf0c1731359b15ec94c233bc5b3e11db089b4.1387369730.git.minovotn@redhat.com>
9ae3a8
In-Reply-To: <091eecc4fa42754760dfff393dabcc2b444e9693.1387369730.git.minovotn@redhat.com>
9ae3a8
References: <091eecc4fa42754760dfff393dabcc2b444e9693.1387369730.git.minovotn@redhat.com>
9ae3a8
From: Markus Armbruster <armbru@redhat.com>
9ae3a8
Date: Tue, 10 Dec 2013 15:29:14 +0100
9ae3a8
Subject: [PATCH 14/21] qapi.py: Decent syntax error reporting
9ae3a8
9ae3a8
RH-Author: Markus Armbruster <armbru@redhat.com>
9ae3a8
Message-id: <1386689361-30281-12-git-send-email-armbru@redhat.com>
9ae3a8
Patchwork-id: 56126
9ae3a8
O-Subject: [PATCH 7.0 qemu-kvm 11/18] qapi.py: Decent syntax error reporting
9ae3a8
Bugzilla: 997915
9ae3a8
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
9ae3a8
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
RH-Acked-by: Luiz Capitulino <lcapitulino@redhat.com>
9ae3a8
9ae3a8
From: Markus Armbruster <armbru@redhat.com>
9ae3a8
9ae3a8
Signed-off-by: Markus Armbruster <armbru@redhat.com>
9ae3a8
Reviewed-by: Eric Blake <eblake@redhat.com>
9ae3a8
Message-id: 1374939721-7876-5-git-send-email-armbru@redhat.com
9ae3a8
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
9ae3a8
(cherry picked from commit 2caba36cc61ee3993334bc423f0852f8006fdfcf)
9ae3a8
---
9ae3a8
 scripts/qapi.py                       | 29 +++++++++++++++++++++++++++--
9ae3a8
 tests/qapi-schema/test-qapi.py        |  2 ++
9ae3a8
 tests/qapi-schema/unclosed-string.err |  2 +-
9ae3a8
 3 files changed, 30 insertions(+), 3 deletions(-)
9ae3a8
9ae3a8
Signed-off-by: Michal Novotny <minovotn@redhat.com>
9ae3a8
---
9ae3a8
 scripts/qapi.py                       | 29 +++++++++++++++++++++++++++--
9ae3a8
 tests/qapi-schema/test-qapi.py        |  2 ++
9ae3a8
 tests/qapi-schema/unclosed-string.err |  2 +-
9ae3a8
 3 files changed, 30 insertions(+), 3 deletions(-)
9ae3a8
9ae3a8
diff --git a/scripts/qapi.py b/scripts/qapi.py
9ae3a8
index 58e315b..342d16c 100644
9ae3a8
--- a/scripts/qapi.py
9ae3a8
+++ b/scripts/qapi.py
9ae3a8
@@ -12,6 +12,7 @@
9ae3a8
 # See the COPYING.LIB file in the top-level directory.
9ae3a8
 
9ae3a8
 from ordereddict import OrderedDict
9ae3a8
+import sys
9ae3a8
 
9ae3a8
 builtin_types = [
9ae3a8
     'str', 'int', 'number', 'bool',
9ae3a8
@@ -34,6 +35,23 @@ builtin_type_qtypes = {
9ae3a8
     'uint64':   'QTYPE_QINT',
9ae3a8
 }
9ae3a8
 
9ae3a8
+class QAPISchemaError(Exception):
9ae3a8
+    def __init__(self, schema, msg):
9ae3a8
+        self.fp = schema.fp
9ae3a8
+        self.msg = msg
9ae3a8
+        self.line = self.col = 1
9ae3a8
+        for ch in schema.src[0:schema.pos]:
9ae3a8
+            if ch == '\n':
9ae3a8
+                self.line += 1
9ae3a8
+                self.col = 1
9ae3a8
+            elif ch == '\t':
9ae3a8
+                self.col = (self.col + 7) % 8 + 1
9ae3a8
+            else:
9ae3a8
+                self.col += 1
9ae3a8
+
9ae3a8
+    def __str__(self):
9ae3a8
+        return "%s:%s:%s: %s" % (self.fp.name, self.line, self.col, self.msg)
9ae3a8
+
9ae3a8
 class QAPISchema:
9ae3a8
 
9ae3a8
     def __init__(self, fp):
9ae3a8
@@ -52,6 +70,7 @@ class QAPISchema:
9ae3a8
         while True:
9ae3a8
             bol = self.cursor == 0 or self.src[self.cursor-1] == '\n'
9ae3a8
             self.tok = self.src[self.cursor]
9ae3a8
+            self.pos = self.cursor
9ae3a8
             self.cursor += 1
9ae3a8
             self.val = None
9ae3a8
 
9ae3a8
@@ -66,7 +85,8 @@ class QAPISchema:
9ae3a8
                     ch = self.src[self.cursor]
9ae3a8
                     self.cursor += 1
9ae3a8
                     if ch == '\n':
9ae3a8
-                        raise Exception("Mismatched quotes")
9ae3a8
+                        raise QAPISchemaError(self,
9ae3a8
+                                              'Missing terminating "\'"')
9ae3a8
                     if esc:
9ae3a8
                         string += ch
9ae3a8
                         esc = False
9ae3a8
@@ -116,7 +136,12 @@ class QAPISchema:
9ae3a8
         return expr
9ae3a8
 
9ae3a8
 def parse_schema(fp):
9ae3a8
-    schema = QAPISchema(fp)
9ae3a8
+    try:
9ae3a8
+        schema = QAPISchema(fp)
9ae3a8
+    except QAPISchemaError as e:
9ae3a8
+        print >>sys.stderr, e
9ae3a8
+        exit(1)
9ae3a8
+
9ae3a8
     exprs = []
9ae3a8
 
9ae3a8
     for expr_eval in schema.exprs:
9ae3a8
diff --git a/tests/qapi-schema/test-qapi.py b/tests/qapi-schema/test-qapi.py
9ae3a8
index 3280eff..b3d1e1d 100644
9ae3a8
--- a/tests/qapi-schema/test-qapi.py
9ae3a8
+++ b/tests/qapi-schema/test-qapi.py
9ae3a8
@@ -16,6 +16,8 @@ import sys
9ae3a8
 
9ae3a8
 try:
9ae3a8
     exprs = parse_schema(sys.stdin)
9ae3a8
+except SystemExit:
9ae3a8
+    raise
9ae3a8
 except:
9ae3a8
     print >>sys.stderr, "Crashed:", sys.exc_info()[0]
9ae3a8
     exit(1)
9ae3a8
diff --git a/tests/qapi-schema/unclosed-string.err b/tests/qapi-schema/unclosed-string.err
9ae3a8
index 5af46c2..948d883 100644
9ae3a8
--- a/tests/qapi-schema/unclosed-string.err
9ae3a8
+++ b/tests/qapi-schema/unclosed-string.err
9ae3a8
@@ -1 +1 @@
9ae3a8
-Crashed: <type 'exceptions.Exception'>
9ae3a8
+<stdin>:1:11: Missing terminating "'"
9ae3a8
-- 
9ae3a8
1.7.11.7
9ae3a8