qapi.py: Fix schema parser to check syntax systematically

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>
This commit is contained in:
Markus Armbruster
2013-07-29 10:37:11 -05:00
committed by Anthony Liguori
parent 9213aa5391
commit 6974ccd542
18 changed files with 42 additions and 32 deletions
+30 -10
View 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):
+1
View File
@@ -0,0 +1 @@
<stdin>:1:10: Expected ":"
+1 -1
View File
@@ -1 +1 @@
0
1
-3
View File
@@ -1,3 +0,0 @@
[OrderedDict([('enum', None), ('data', ['good', 'bad', 'ugly'])])]
[None]
[]
+1
View File
@@ -0,0 +1 @@
<stdin>:2:20: Expected "," or "]"
+1 -1
View File
@@ -1 +1 @@
0
1
-3
View File
@@ -1,3 +0,0 @@
[OrderedDict([('enum', 'Status'), ('data', ['good', 'bad', 'ugly'])])]
['Status']
[]
@@ -0,0 +1 @@
<stdin>:2:3: Expected "," or "}"
+1 -1
View File
@@ -1 +1 @@
0
1
@@ -1,3 +0,0 @@
[OrderedDict([('enum', 'Status'), ('data', ['good', 'bad', 'ugly'])])]
['Status']
[]
@@ -0,0 +1 @@
<stdin>:2:36: Expected "{", "[" or string
+1 -1
View File
@@ -1 +1 @@
0
1
@@ -1,3 +0,0 @@
[OrderedDict([('enum', 'Status'), ('data', ['good', 'bad', 'ugly'])])]
['Status']
[]
@@ -0,0 +1 @@
<stdin>:2:38: Expected string
+1 -1
View File
@@ -1 +1 @@
0
1
@@ -1,3 +0,0 @@
[OrderedDict([('enum', 'Status'), ('data', ['good', 'bad', 'ugly'])])]
['Status']
[]
+1 -1
View File
@@ -1 +1 @@
Crashed: <type 'exceptions.IndexError'>
<stdin>:1:20: Expected "," or "]"
+1 -1
View File
@@ -1 +1 @@
Crashed: <type 'exceptions.IndexError'>
<stdin>:1:21: Expected "," or "}"