qapi: qapi.py: allow the "'" character to be escaped
authorLuiz Capitulino <lcapitulino@redhat.com>
Fri, 29 Jun 2012 23:53:37 +0000 (20:53 -0300)
committerLuiz Capitulino <lcapitulino@redhat.com>
Wed, 1 Aug 2012 21:09:29 +0000 (18:09 -0300)
Support escaping the escape character, and make more robust (don't die
for '', handle ' without matching '.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Peter Maydell <peter.maydell@linaro.org>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
scripts/qapi.py

index 8082af3fcddb76285d7858547a95cd5ebb5d0334..d3b8b4d851f3d383aad8764bffe0fb1d9eceb89d 100644 (file)
@@ -13,18 +13,29 @@ from ordereddict import OrderedDict
 
 def tokenize(data):
     while len(data):
-        if data[0] in ['{', '}', ':', ',', '[', ']']:
-            yield data[0]
-            data = data[1:]
-        elif data[0] in ' \n':
-            data = data[1:]
-        elif data[0] == "'":
-            data = data[1:]
+        ch = data[0]
+        data = data[1:]
+        if ch in ['{', '}', ':', ',', '[', ']']:
+            yield ch
+        elif ch in ' \n':
+            None
+        elif ch == "'":
             string = ''
-            while data[0] != "'":
-                string += data[0]
+            esc = False
+            while True:
+                if (data == ''):
+                    raise Exception("Mismatched quotes")
+                ch = data[0]
                 data = data[1:]
-            data = data[1:]
+                if esc:
+                    string += ch
+                    esc = False
+                elif ch == "\\":
+                    esc = True
+                elif ch == "'":
+                    break
+                else:
+                    string += ch
             yield string
 
 def parse(tokens):