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