# 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 = []
-events = []
+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)
self.sections = []
# the current section
self.section = self.body
- # associated expression (to be set by expression parser)
- self.expr = None
def has_section(self, name):
"""Return True if we have a section with this name."""
else:
self._append_freeform(line)
+ def end_comment(self):
+ self._end_section()
+
def _append_symbol_line(self, line):
name = line.split(' ', 1)[0]
raise QAPIParseError(self.parser,
"'@%s:' can't follow '%s' section"
% (name, self.sections[0].name))
+ self._end_section()
self.section = QAPIDoc.ArgSection(name)
self.args[name] = self.section
if name in ('Returns', 'Since') and self.has_section(name):
raise QAPIParseError(self.parser,
"Duplicated '%s' section" % name)
+ self._end_section()
self.section = QAPIDoc.Section(name)
self.sections.append(self.section)
+ def _end_section(self):
+ if self.section:
+ contents = str(self.section)
+ if self.section.name and (not contents or contents.isspace()):
+ raise QAPIParseError(self.parser, "Empty doc section '%s'"
+ % self.section.name)
+ self.section = None
+
def _append_freeform(self, line):
in_arg = isinstance(self.section, QAPIDoc.ArgSection)
if (in_arg and self.section.content
if (in_arg or not self.section.name
or not self.section.name.startswith('Example')):
line = line.strip()
+ match = re.match(r'(@\S+:)', line)
+ if match:
+ raise QAPIParseError(self.parser,
+ "'%s' not allowed in free-form documentation"
+ % match.group(1))
# TODO Drop this once the dust has settled
if (isinstance(self.section, QAPIDoc.ArgSection)
and '#optional' in line):
self.args[member.name] = QAPIDoc.ArgSection(member.name)
self.args[member.name].connect(member)
+ def check_expr(self, expr):
+ if self.has_section('Returns') and 'command' not in expr:
+ raise QAPISemError(self.info,
+ "'Returns:' is only valid for commands")
+
+ def check(self):
+ bogus = [name for name, section in self.args.iteritems()
+ if not section.member]
+ if bogus:
+ raise QAPISemError(
+ self.info,
+ "The following documented members are not in "
+ "the declaration: %s" % ", ".join(bogus))
+
class QAPISchemaParser(object):
self.line_pos = 0
self.exprs = []
self.docs = []
+ self.cur_doc = None
self.accept()
while self.tok is not None:
info = {'file': fname, 'line': self.line,
'parent': self.incl_info}
if self.tok == '#':
- doc = self.get_doc(info)
- self.docs.append(doc)
+ self.reject_expr_doc()
+ self.cur_doc = self.get_doc(info)
+ self.docs.append(self.cur_doc)
continue
expr = self.get_expr(False)
if 'include' in expr:
+ self.reject_expr_doc()
if len(expr) != 1:
raise QAPISemError(info, "Invalid 'include' directive")
include = expr['include']
self._include(include, info, os.path.dirname(abs_fname),
previously_included)
elif "pragma" in expr:
+ self.reject_expr_doc()
if len(expr) != 1:
raise QAPISemError(info, "Invalid 'pragma' directive")
pragma = expr['pragma']
else:
expr_elem = {'expr': expr,
'info': info}
- if (self.docs
- and self.docs[-1].info['file'] == fname
- and not self.docs[-1].expr):
- self.docs[-1].expr = expr
- expr_elem['doc'] = self.docs[-1]
-
+ if self.cur_doc:
+ if not self.cur_doc.symbol:
+ raise QAPISemError(
+ self.cur_doc.info,
+ "Expression documentation required")
+ expr_elem['doc'] = self.cur_doc
self.exprs.append(expr_elem)
+ self.cur_doc = None
+ self.reject_expr_doc()
+
+ def reject_expr_doc(self):
+ if self.cur_doc and self.cur_doc.symbol:
+ raise QAPISemError(
+ self.cur_doc.info,
+ "Documentation for '%s' is not followed by the definition"
+ % self.cur_doc.symbol)
def _include(self, include, info, base_dir, previously_included):
incl_abs_fname = os.path.join(base_dir, include)
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):
if self.val != '##':
raise QAPIParseError(self, "Junk after '##' at end of "
"documentation comment")
+ doc.end_comment()
self.accept()
return doc
else:
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
- name = definition['struct']
- add_name(name, info, 'struct')
- 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
- name = definition['union']
- add_name(name, info, 'union')
- 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(name, info, enum_values=None, implicit=False):
- global enum_types
- add_name(name, info, 'enum', implicit)
- enum_types.append({'enum_name': name, 'enum_values': enum_values})
-
-
-def find_enum(name):
- global enum_types
- for enum in enum_types:
- if enum['enum_name'] == 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=[]):
def check_event(expr, info):
- global events
name = expr['event']
boxed = expr.get('boxed', False)
meta = ['struct']
if boxed:
meta += ['union', 'alternate']
- events.append(name)
check_type(info, "'data' for event '%s'" % name,
expr.get('data'), allow_dict=not boxed, allow_optional=True,
allow_metas=meta)
"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 the discriminator names an enum type, then all members
# of 'data' must also be members of the enum type.
if enum_define:
- if key not in enum_define['enum_values']:
+ if key not in enum_define['data']:
raise QAPISemError(info,
"Discriminator value '%s' is not found in "
"enum '%s'"
- % (key, enum_define['enum_name']))
+ % (key, enum_define['enum']))
# If discriminator is user-defined, ensure all values are covered
if enum_define:
- for value in enum_define['enum_values']:
+ for value in enum_define['data']:
if value not in members.keys():
raise QAPISemError(info, "Union '%s' data missing '%s' branch"
% (name, value))
def check_exprs(exprs):
global all_names
- # Learn the types and check for valid expression keys
+ # Populate name table with names of built-in types
for builtin in builtin_types.keys():
all_names[builtin] = 'built-in'
+
+ # Learn the types and check for valid expression keys
for expr_elem in exprs:
expr = expr_elem['expr']
info = expr_elem['info']
+ doc = expr_elem.get('doc')
- if 'doc' not in expr_elem and doc_required:
+ if not doc and doc_required:
raise QAPISemError(info,
"Expression missing documentation comment")
if 'enum' in expr:
+ meta = 'enum'
check_keys(expr_elem, 'enum', ['data'], ['prefix'])
- add_enum(expr['enum'], info, expr['data'])
+ 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'])
- add_name(expr['alternate'], info, 'alternate')
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', [],
['data', 'returns', 'gen', 'success-response', 'boxed'])
- add_name(expr['command'], info, 'command')
elif 'event' in expr:
+ meta = 'event'
check_keys(expr_elem, 'event', [], ['data', 'boxed'])
- add_name(expr['event'], info, 'event')
else:
raise QAPISemError(expr_elem['info'],
"Expression is missing metatype")
+ name = expr[meta]
+ add_name(name, info, meta)
+ if doc and doc.symbol != name:
+ raise QAPISemError(info, "Definition of '%s' follows documentation"
+ " for '%s'" % (name, doc.symbol))
# Try again for hidden UnionKind enum
for expr_elem in exprs:
expr = expr_elem['expr']
- if 'union' in expr:
- if not discriminator_find_enum_define(expr):
- add_enum('%sKind' % expr['union'], expr_elem['info'],
- implicit=True)
+ if 'union' in expr and not discriminator_find_enum_define(expr):
+ name = '%sKind' % expr['union']
elif 'alternate' in expr:
- add_enum('%sKind' % expr['alternate'], expr_elem['info'],
- implicit=True)
+ name = '%sKind' % expr['alternate']
+ else:
+ continue
+ enum_types[name] = {'enum': name}
+ add_name(name, info, 'enum', implicit=True)
# Validate that exprs make sense
for expr_elem in exprs:
expr = expr_elem['expr']
info = expr_elem['info']
+ doc = expr_elem.get('doc')
if 'enum' in expr:
check_enum(expr, info)
else:
assert False, 'unexpected meta type'
- return exprs
-
+ if doc:
+ doc.check_expr(expr)
-def check_freeform_doc(doc):
- if doc.symbol:
- raise QAPISemError(doc.info,
- "Documention for '%s' is not followed"
- " by the definition" % doc.symbol)
-
- body = str(doc.body)
- if re.search(r'@\S+:', body, re.MULTILINE):
- raise QAPISemError(doc.info,
- "Free-form documentation block must not contain"
- " @NAME: sections")
-
-
-def check_definition_doc(doc, expr, info):
- for i in ('enum', 'union', 'alternate', 'struct', 'command', 'event'):
- if i in expr:
- meta = i
- break
-
- name = expr[meta]
- if doc.symbol != name:
- raise QAPISemError(info, "Definition of '%s' follows documentation"
- " for '%s'" % (name, doc.symbol))
- if doc.has_section('Returns') and 'command' not in expr:
- raise QAPISemError(info, "'Returns:' is only valid for commands")
-
- if meta == 'union':
- args = expr.get('base', [])
- else:
- args = expr.get('data', [])
- if isinstance(args, str):
- return
- if isinstance(args, dict):
- args = args.keys()
- assert isinstance(args, list)
-
- if (meta == 'alternate'
- or (meta == 'union' and not expr.get('discriminator'))):
- args.append('type')
-
- doc_args = set(doc.args.keys())
- args = set([name.strip('*') for name in args])
- if not doc_args.issubset(args):
- raise QAPISemError(info, "The following documented members are not in "
- "the declaration: %s" % ', '.join(doc_args - args))
-
-
-def check_docs(docs):
- for doc in docs:
- for section in doc.args.values() + doc.sections:
- content = str(section)
- if not content or content.isspace():
- raise QAPISemError(doc.info,
- "Empty doc section '%s'" % section.name)
-
- if not doc.expr:
- check_freeform_doc(doc)
- else:
- check_definition_doc(doc, doc.expr, doc.info)
-
- return docs
+ return exprs
#
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):
v.check_clash(self.info, seen)
if self.doc:
self.doc.connect_member(v)
+ if self.doc:
+ self.doc.check()
def c_type(self):
return c_name(self.name) + pointer_suffix
try:
parser = QAPISchemaParser(open(fname, 'r'))
self.exprs = check_exprs(parser.exprs)
- self.docs = check_docs(parser.docs)
+ self.docs = parser.docs
self._entity_dict = {}
self._predefining = True
self._def_predefineds()
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()]