hw/intc/i8259: Fix Kconfig dependency on ISA bus
[qemu/ar7.git] / scripts / qapi / doc.py
blob6f1c17f71f7b8ddd5ad59bba10972d0d5283404e
1 # QAPI texi generator
3 # This work is licensed under the terms of the GNU LGPL, version 2+.
4 # See the COPYING file in the top-level directory.
5 """This script produces the documentation of a qapi schema in texinfo format"""
7 from __future__ import print_function
8 import re
9 from qapi.gen import QAPIGenDoc, QAPISchemaVisitor
12 MSG_FMT = """
13 @deftypefn {type} {{}} {name}
15 {body}{members}{features}{sections}
16 @end deftypefn
18 """.format
20 TYPE_FMT = """
21 @deftp {{{type}}} {name}
23 {body}{members}{features}{sections}
24 @end deftp
26 """.format
28 EXAMPLE_FMT = """@example
29 {code}
30 @end example
31 """.format
34 def subst_strong(doc):
35 """Replaces *foo* by @strong{foo}"""
36 return re.sub(r'\*([^*\n]+)\*', r'@strong{\1}', doc)
39 def subst_emph(doc):
40 """Replaces _foo_ by @emph{foo}"""
41 return re.sub(r'\b_([^_\n]+)_\b', r'@emph{\1}', doc)
44 def subst_vars(doc):
45 """Replaces @var by @code{var}"""
46 return re.sub(r'@([\w-]+)', r'@code{\1}', doc)
49 def subst_braces(doc):
50 """Replaces {} with @{ @}"""
51 return doc.replace('{', '@{').replace('}', '@}')
54 def texi_example(doc):
55 """Format @example"""
56 # TODO: Neglects to escape @ characters.
57 # We should probably escape them in subst_braces(), and rename the
58 # function to subst_special() or subs_texi_special(). If we do that, we
59 # need to delay it until after subst_vars() in texi_format().
60 doc = subst_braces(doc).strip('\n')
61 return EXAMPLE_FMT(code=doc)
64 def texi_format(doc):
65 """
66 Format documentation
68 Lines starting with:
69 - |: generates an @example
70 - =: generates @section
71 - ==: generates @subsection
72 - 1. or 1): generates an @enumerate @item
73 - */-: generates an @itemize list
74 """
75 ret = ''
76 doc = subst_braces(doc)
77 doc = subst_vars(doc)
78 doc = subst_emph(doc)
79 doc = subst_strong(doc)
80 inlist = ''
81 lastempty = False
82 for line in doc.split('\n'):
83 empty = line == ''
85 # FIXME: Doing this in a single if / elif chain is
86 # problematic. For instance, a line without markup terminates
87 # a list if it follows a blank line (reaches the final elif),
88 # but a line with some *other* markup, such as a = title
89 # doesn't.
91 # Make sure to update section "Documentation markup" in
92 # docs/devel/qapi-code-gen.txt when fixing this.
93 if line.startswith('| '):
94 line = EXAMPLE_FMT(code=line[2:])
95 elif line.startswith('= '):
96 line = '@section ' + line[2:]
97 elif line.startswith('== '):
98 line = '@subsection ' + line[3:]
99 elif re.match(r'^([0-9]*\.) ', line):
100 if not inlist:
101 ret += '@enumerate\n'
102 inlist = 'enumerate'
103 ret += '@item\n'
104 line = line[line.find(' ')+1:]
105 elif re.match(r'^[*-] ', line):
106 if not inlist:
107 ret += '@itemize %s\n' % {'*': '@bullet',
108 '-': '@minus'}[line[0]]
109 inlist = 'itemize'
110 ret += '@item\n'
111 line = line[2:]
112 elif lastempty and inlist:
113 ret += '@end %s\n\n' % inlist
114 inlist = ''
116 lastempty = empty
117 ret += line + '\n'
119 if inlist:
120 ret += '@end %s\n\n' % inlist
121 return ret
124 def texi_body(doc):
125 """Format the main documentation body"""
126 return texi_format(doc.body.text)
129 def texi_if(ifcond, prefix='\n', suffix='\n'):
130 """Format the #if condition"""
131 if not ifcond:
132 return ''
133 return '%s@b{If:} @code{%s}%s' % (prefix, ', '.join(ifcond), suffix)
136 def texi_enum_value(value, desc, suffix):
137 """Format a table of members item for an enumeration value"""
138 return '@item @code{%s}\n%s%s' % (
139 value.name, desc, texi_if(value.ifcond, prefix='@*'))
142 def texi_member(member, desc, suffix):
143 """Format a table of members item for an object type member"""
144 typ = member.type.doc_type()
145 membertype = ': ' + typ if typ else ''
146 return '@item @code{%s%s}%s%s\n%s%s' % (
147 member.name, membertype,
148 ' (optional)' if member.optional else '',
149 suffix, desc, texi_if(member.ifcond, prefix='@*'))
152 def texi_members(doc, what, base=None, variants=None,
153 member_func=texi_member):
154 """Format the table of members"""
155 items = ''
156 for section in doc.args.values():
157 # TODO Drop fallbacks when undocumented members are outlawed
158 if section.text:
159 desc = texi_format(section.text)
160 elif (variants and variants.tag_member == section.member
161 and not section.member.type.doc_type()):
162 values = section.member.type.member_names()
163 members_text = ', '.join(['@t{"%s"}' % v for v in values])
164 desc = 'One of ' + members_text + '\n'
165 else:
166 desc = 'Not documented\n'
167 items += member_func(section.member, desc, suffix='')
168 if base:
169 items += '@item The members of @code{%s}\n' % base.doc_type()
170 if variants:
171 for v in variants.variants:
172 when = ' when @code{%s} is @t{"%s"}%s' % (
173 variants.tag_member.name, v.name, texi_if(v.ifcond, " (", ")"))
174 if v.type.is_implicit():
175 assert not v.type.base and not v.type.variants
176 for m in v.type.local_members:
177 items += member_func(m, desc='', suffix=when)
178 else:
179 items += '@item The members of @code{%s}%s\n' % (
180 v.type.doc_type(), when)
181 if not items:
182 return ''
183 return '\n@b{%s:}\n@table @asis\n%s@end table\n' % (what, items)
186 def texi_arguments(doc, boxed_arg_type):
187 if boxed_arg_type:
188 assert not doc.args
189 return ('\n@b{Arguments:} the members of @code{%s}\n'
190 % boxed_arg_type.name)
191 return texi_members(doc, 'Arguments')
194 def texi_features(doc):
195 """Format the table of features"""
196 items = ''
197 for section in doc.features.values():
198 desc = texi_format(section.text)
199 items += '@item @code{%s}\n%s' % (section.name, desc)
200 if not items:
201 return ''
202 return '\n@b{Features:}\n@table @asis\n%s@end table\n' % (items)
205 def texi_sections(doc, ifcond):
206 """Format additional sections following arguments"""
207 body = ''
208 for section in doc.sections:
209 if section.name:
210 # prefer @b over @strong, so txt doesn't translate it to *Foo:*
211 body += '\n@b{%s:}\n' % section.name
212 if section.name and section.name.startswith('Example'):
213 body += texi_example(section.text)
214 else:
215 body += texi_format(section.text)
216 body += texi_if(ifcond, suffix='')
217 return body
220 def texi_type(typ, doc, ifcond, members):
221 return TYPE_FMT(type=typ,
222 name=doc.symbol,
223 body=texi_body(doc),
224 members=members,
225 features=texi_features(doc),
226 sections=texi_sections(doc, ifcond))
229 def texi_msg(typ, doc, ifcond, members):
230 return MSG_FMT(type=typ,
231 name=doc.symbol,
232 body=texi_body(doc),
233 members=members,
234 features=texi_features(doc),
235 sections=texi_sections(doc, ifcond))
238 class QAPISchemaGenDocVisitor(QAPISchemaVisitor):
239 def __init__(self, prefix):
240 self._prefix = prefix
241 self._gen = QAPIGenDoc(self._prefix + 'qapi-doc.texi')
242 self.cur_doc = None
244 def write(self, output_dir):
245 self._gen.write(output_dir)
247 def visit_enum_type(self, name, info, ifcond, members, prefix):
248 doc = self.cur_doc
249 self._gen.add(texi_type('Enum', doc, ifcond,
250 texi_members(doc, 'Values',
251 member_func=texi_enum_value)))
253 def visit_object_type(self, name, info, ifcond, base, members, variants,
254 features):
255 doc = self.cur_doc
256 if base and base.is_implicit():
257 base = None
258 self._gen.add(texi_type('Object', doc, ifcond,
259 texi_members(doc, 'Members', base, variants)))
261 def visit_alternate_type(self, name, info, ifcond, variants):
262 doc = self.cur_doc
263 self._gen.add(texi_type('Alternate', doc, ifcond,
264 texi_members(doc, 'Members')))
266 def visit_command(self, name, info, ifcond, arg_type, ret_type, gen,
267 success_response, boxed, allow_oob, allow_preconfig,
268 features):
269 doc = self.cur_doc
270 self._gen.add(texi_msg('Command', doc, ifcond,
271 texi_arguments(doc,
272 arg_type if boxed else None)))
274 def visit_event(self, name, info, ifcond, arg_type, boxed):
275 doc = self.cur_doc
276 self._gen.add(texi_msg('Event', doc, ifcond,
277 texi_arguments(doc,
278 arg_type if boxed else None)))
280 def symbol(self, doc, entity):
281 if self._gen._body:
282 self._gen.add('\n')
283 self.cur_doc = doc
284 entity.visit(self)
285 self.cur_doc = None
287 def freeform(self, doc):
288 assert not doc.args
289 if self._gen._body:
290 self._gen.add('\n')
291 self._gen.add(texi_body(doc) + texi_sections(doc, None))
294 def gen_doc(schema, output_dir, prefix):
295 vis = QAPISchemaGenDocVisitor(prefix)
296 vis.visit_begin(schema)
297 for doc in schema.docs:
298 if doc.symbol:
299 vis.symbol(doc, schema.lookup_entity(doc.symbol))
300 else:
301 vis.freeform(doc)
302 vis.write(output_dir)