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-27 17:41:58 +02:00 committed by Anthony Liguori
parent 9213aa5391
commit 6974ccd542
18 changed files with 42 additions and 32 deletions

View File

@ -106,24 +106,42 @@ def accept(self):
def get_members(self):
expr = OrderedDict()
while self.tok != '}':
key = self.val
self.accept()
self.accept() # :
expr[key] = self.get_expr()
if self.tok == ',':
self.accept()
if self.tok == '}':
self.accept()
return expr
if self.tok != "'":
raise QAPISchemaError(self, 'Expected string or "}"')
while True:
key = self.val
self.accept()
if self.tok != ':':
raise QAPISchemaError(self, 'Expected ":"')
self.accept()
expr[key] = self.get_expr()
if self.tok == '}':
self.accept()
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 != ']':
expr.append(self.get_expr())
if self.tok == ',':
self.accept()
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 == ']':
self.accept()
return expr
if self.tok != ',':
raise QAPISchemaError(self, 'Expected "," or "]"')
self.accept()
def get_expr(self):
if self.tok == '{':
@ -132,9 +150,11 @@ def get_expr(self):
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):

View File

@ -0,0 +1 @@
<stdin>:1:10: Expected ":"

View File

@ -1 +1 @@
0
1

View File

@ -1,3 +0,0 @@
[OrderedDict([('enum', None), ('data', ['good', 'bad', 'ugly'])])]
[None]
[]

View File

@ -0,0 +1 @@
<stdin>:2:20: Expected "," or "]"

View File

@ -1 +1 @@
0
1

View File

@ -1,3 +0,0 @@
[OrderedDict([('enum', 'Status'), ('data', ['good', 'bad', 'ugly'])])]
['Status']
[]

View File

@ -0,0 +1 @@
<stdin>:2:3: Expected "," or "}"

View File

@ -1 +1 @@
0
1

View File

@ -1,3 +0,0 @@
[OrderedDict([('enum', 'Status'), ('data', ['good', 'bad', 'ugly'])])]
['Status']
[]

View File

@ -0,0 +1 @@
<stdin>:2:36: Expected "{", "[" or string

View File

@ -1 +1 @@
0
1

View File

@ -1,3 +0,0 @@
[OrderedDict([('enum', 'Status'), ('data', ['good', 'bad', 'ugly'])])]
['Status']
[]

View File

@ -0,0 +1 @@
<stdin>:2:38: Expected string

View File

@ -1 +1 @@
0
1

View File

@ -1,3 +0,0 @@
[OrderedDict([('enum', 'Status'), ('data', ['good', 'bad', 'ugly'])])]
['Status']
[]

View File

@ -1 +1 @@
Crashed: <type 'exceptions.IndexError'>
<stdin>:1:20: Expected "," or "]"

View File

@ -1 +1 @@
Crashed: <type 'exceptions.IndexError'>
<stdin>:1:21: Expected "," or "}"