OSDN Git Service

qapi.py: Fix schema parser to check syntax systematically
authorMarkus Armbruster <armbru@redhat.com>
Sat, 27 Jul 2013 15:41:58 +0000 (17:41 +0200)
committerAnthony Liguori <aliguori@us.ibm.com>
Mon, 29 Jul 2013 15:37:11 +0000 (10:37 -0500)
Fixes at least the following parser bugs:

* accepts any token in place of a colon

* treats comma as optional

* crashes when closing braces or brackets are missing

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Message-id: 1374939721-7876-7-git-send-email-armbru@redhat.com
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
18 files changed:
scripts/qapi.py
tests/qapi-schema/missing-colon.err
tests/qapi-schema/missing-colon.exit
tests/qapi-schema/missing-colon.out
tests/qapi-schema/missing-comma-list.err
tests/qapi-schema/missing-comma-list.exit
tests/qapi-schema/missing-comma-list.out
tests/qapi-schema/missing-comma-object.err
tests/qapi-schema/missing-comma-object.exit
tests/qapi-schema/missing-comma-object.out
tests/qapi-schema/trailing-comma-list.err
tests/qapi-schema/trailing-comma-list.exit
tests/qapi-schema/trailing-comma-list.out
tests/qapi-schema/trailing-comma-object.err
tests/qapi-schema/trailing-comma-object.exit
tests/qapi-schema/trailing-comma-object.out
tests/qapi-schema/unclosed-list.err
tests/qapi-schema/unclosed-object.err

index 0b48a1e..12fb29a 100644 (file)
@@ -106,24 +106,42 @@ class QAPISchema:
 
     def get_members(self):
         expr = OrderedDict()
-        while self.tok != '}':
+        if self.tok == '}':
+            self.accept()
+            return expr
+        if self.tok != "'":
+            raise QAPISchemaError(self, 'Expected string or "}"')
+        while True:
             key = self.val
             self.accept()
-            self.accept()        # :
+            if self.tok != ':':
+                raise QAPISchemaError(self, 'Expected ":"')
+            self.accept()
             expr[key] = self.get_expr()
-            if self.tok == ',':
+            if self.tok == '}':
                 self.accept()
-        self.accept()
-        return expr
+                return expr
+            if self.tok != ',':
+                raise QAPISchemaError(self, 'Expected "," or "}"')
+            self.accept()
+            if self.tok != "'":
+                raise QAPISchemaError(self, 'Expected string')
 
     def get_values(self):
         expr = []
-        while self.tok != ']':
+        if self.tok == ']':
+            self.accept()
+            return expr
+        if not self.tok in [ '{', '[', "'" ]:
+            raise QAPISchemaError(self, 'Expected "{", "[", "]" or string')
+        while True:
             expr.append(self.get_expr())
-            if self.tok == ',':
+            if self.tok == ']':
                 self.accept()
-        self.accept()
-        return expr
+                return expr
+            if self.tok != ',':
+                raise QAPISchemaError(self, 'Expected "," or "]"')
+            self.accept()
 
     def get_expr(self):
         if self.tok == '{':
@@ -132,9 +150,11 @@ class QAPISchema:
         elif self.tok == '[':
             self.accept()
             expr = self.get_values()
-        else:
+        elif self.tok == "'":
             expr = self.val
             self.accept()
+        else:
+            raise QAPISchemaError(self, 'Expected "{", "[" or string')
         return expr
 
 def parse_schema(fp):
index e67068c..e69de29 100644 (file)
@@ -1,3 +0,0 @@
-[OrderedDict([('enum', None), ('data', ['good', 'bad', 'ugly'])])]
-[None]
-[]
index e3bd904..e69de29 100644 (file)
@@ -1,3 +0,0 @@
-[OrderedDict([('enum', 'Status'), ('data', ['good', 'bad', 'ugly'])])]
-['Status']
-[]
index e3bd904..e69de29 100644 (file)
@@ -1,3 +0,0 @@
-[OrderedDict([('enum', 'Status'), ('data', ['good', 'bad', 'ugly'])])]
-['Status']
-[]
index e3bd904..e69de29 100644 (file)
@@ -1,3 +0,0 @@
-[OrderedDict([('enum', 'Status'), ('data', ['good', 'bad', 'ugly'])])]
-['Status']
-[]
index e3bd904..e69de29 100644 (file)
@@ -1,3 +0,0 @@
-[OrderedDict([('enum', 'Status'), ('data', ['good', 'bad', 'ugly'])])]
-['Status']
-[]
index f9a9c2a..0e837a7 100644 (file)
@@ -1 +1 @@
-Crashed: <type 'exceptions.IndexError'>
+<stdin>:1:20: Expected "," or "]"
index f9a9c2a..e6dc950 100644 (file)
@@ -1 +1 @@
-Crashed: <type 'exceptions.IndexError'>
+<stdin>:1:21: Expected "," or "}"