# This work is licensed under the terms of the GNU GPL, version 2.
# See the COPYING file in the top-level directory.
-import re
-from ordereddict import OrderedDict
import errno
import getopt
import os
-import sys
+import re
import string
+import sys
+from ordereddict import OrderedDict
builtin_types = {
'str': 'QTYPE_QSTRING',
# Whitelist of entities allowed to violate case conventions
name_case_whitelist = []
-enum_types = []
-struct_types = []
-union_types = []
+enum_types = {}
+struct_types = {}
+union_types = {}
all_names = {}
#
self.name = name
# the list of lines for this section
self.content = []
- self.optional = False
def append(self, line):
self.content.append(line)
expr = self.val
self.accept()
else:
- raise QAPIParseError(self, 'Expected "{", "[" or string')
+ raise QAPIParseError(self, 'Expected "{", "[", string, '
+ 'boolean or "null"')
return expr
def get_doc(self, info):
def find_base_members(base):
if isinstance(base, dict):
return base
- base_struct_define = find_struct(base)
+ base_struct_define = struct_types.get(base)
if not base_struct_define:
return None
return base_struct_define['data']
def find_alternate_member_qtype(qapi_type):
if qapi_type in builtin_types:
return builtin_types[qapi_type]
- elif find_struct(qapi_type):
+ elif qapi_type in struct_types:
return 'QTYPE_QDICT'
- elif find_enum(qapi_type):
+ elif qapi_type in enum_types:
return 'QTYPE_QSTRING'
- elif find_union(qapi_type):
+ elif qapi_type in union_types:
return 'QTYPE_QDICT'
return None
if not discriminator_type:
return None
- return find_enum(discriminator_type)
+ return enum_types.get(discriminator_type)
# Names must be letters, numbers, -, and _. They must start with letter,
all_names[name] = meta
-def add_struct(definition, info):
- global struct_types
- struct_types.append(definition)
-
-
-def find_struct(name):
- global struct_types
- for struct in struct_types:
- if struct['struct'] == name:
- return struct
- return None
-
-
-def add_union(definition, info):
- global union_types
- union_types.append(definition)
-
-
-def find_union(name):
- global union_types
- for union in union_types:
- if union['union'] == name:
- return union
- return None
-
-
-def add_enum(definition, info):
- global enum_types
- enum_types.append(definition)
-
-
-def find_enum(name):
- global enum_types
- for enum in enum_types:
- if enum['enum'] == name:
- return enum
- return None
-
-
-def is_enum(name):
- return find_enum(name) is not None
-
-
def check_type(info, source, value, allow_array=False,
allow_dict=False, allow_optional=False,
allow_metas=[]):
"Discriminator '%s' is not a member of base "
"struct '%s'"
% (discriminator, base))
- enum_define = find_enum(discriminator_type)
+ enum_define = enum_types.get(discriminator_type)
allow_metas = ['struct']
# Do not allow string discriminator
if not enum_define:
if 'enum' in expr:
meta = 'enum'
check_keys(expr_elem, 'enum', ['data'], ['prefix'])
- add_enum(expr, info)
+ enum_types[expr[meta]] = expr
elif 'union' in expr:
meta = 'union'
check_keys(expr_elem, 'union', ['data'],
['base', 'discriminator'])
- add_union(expr, info)
+ union_types[expr[meta]] = expr
elif 'alternate' in expr:
meta = 'alternate'
check_keys(expr_elem, 'alternate', ['data'])
elif 'struct' in expr:
meta = 'struct'
check_keys(expr_elem, 'struct', ['data'], ['base'])
- add_struct(expr, info)
+ struct_types[expr[meta]] = expr
elif 'command' in expr:
meta = 'command'
check_keys(expr_elem, 'command', [],
name = '%sKind' % expr['alternate']
else:
continue
- add_enum({ 'enum': name }, expr_elem['info'])
+ enum_types[name] = {'enum': name}
add_name(name, info, 'enum', implicit=True)
# Validate that exprs make sense
self.base = schema.lookup_type(self._base_name)
assert isinstance(self.base, QAPISchemaObjectType)
self.base.check(schema)
- self.base.check_clash(schema, self.info, seen)
+ self.base.check_clash(self.info, seen)
for m in self.local_members:
m.check(schema)
m.check_clash(self.info, seen)
if self.variants:
self.variants.check(schema, seen)
assert self.variants.tag_member in self.members
- self.variants.check_clash(schema, self.info, seen)
+ self.variants.check_clash(self.info, seen)
if self.doc:
self.doc.check()
# Check that the members of this type do not cause duplicate JSON members,
# and update seen to track the members seen so far. Report any errors
# on behalf of info, which is not necessarily self.info
- def check_clash(self, schema, info, seen):
+ def check_clash(self, info, seen):
assert not self.variants # not implemented
for m in self.members:
m.check_clash(info, seen)
assert isinstance(v.type, QAPISchemaObjectType)
v.type.check(schema)
- def check_clash(self, schema, info, seen):
+ def check_clash(self, info, seen):
for v in self.variants:
# Reset seen map for each variant, since qapi names from one
# branch do not affect another branch
assert isinstance(v.type, QAPISchemaObjectType)
- v.type.check_clash(schema, info, dict(seen))
+ v.type.check_clash(info, dict(seen))
class QAPISchemaObjectTypeVariant(QAPISchemaObjectTypeMember):
tag_member = None
if isinstance(base, dict):
base = (self._make_implicit_object_type(
- name, info, doc, 'base', self._make_members(base, info)))
+ name, info, doc, 'base', self._make_members(base, info)))
if tag_name:
variants = [self._make_variant(key, value)
for (key, value) in data.iteritems()]