2
0

parser.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  1. # -*- coding: utf-8 -*-
  2. #
  3. # QAPI schema parser
  4. #
  5. # Copyright IBM, Corp. 2011
  6. # Copyright (c) 2013-2019 Red Hat Inc.
  7. #
  8. # Authors:
  9. # Anthony Liguori <aliguori@us.ibm.com>
  10. # Markus Armbruster <armbru@redhat.com>
  11. # Marc-André Lureau <marcandre.lureau@redhat.com>
  12. # Kevin Wolf <kwolf@redhat.com>
  13. #
  14. # This work is licensed under the terms of the GNU GPL, version 2.
  15. # See the COPYING file in the top-level directory.
  16. import enum
  17. import os
  18. import re
  19. from typing import (
  20. TYPE_CHECKING,
  21. Any,
  22. Dict,
  23. List,
  24. Mapping,
  25. Match,
  26. Optional,
  27. Set,
  28. Union,
  29. )
  30. from .common import must_match
  31. from .error import QAPISemError, QAPISourceError
  32. from .source import QAPISourceInfo
  33. if TYPE_CHECKING:
  34. # pylint: disable=cyclic-import
  35. # TODO: Remove cycle. [schema -> expr -> parser -> schema]
  36. from .schema import QAPISchemaFeature, QAPISchemaMember
  37. # Return value alias for get_expr().
  38. _ExprValue = Union[List[object], Dict[str, object], str, bool]
  39. class QAPIExpression(Dict[str, Any]):
  40. # pylint: disable=too-few-public-methods
  41. def __init__(self,
  42. data: Mapping[str, object],
  43. info: QAPISourceInfo,
  44. doc: Optional['QAPIDoc'] = None):
  45. super().__init__(data)
  46. self.info = info
  47. self.doc: Optional['QAPIDoc'] = doc
  48. class QAPIParseError(QAPISourceError):
  49. """Error class for all QAPI schema parsing errors."""
  50. def __init__(self, parser: 'QAPISchemaParser', msg: str):
  51. col = 1
  52. for ch in parser.src[parser.line_pos:parser.pos]:
  53. if ch == '\t':
  54. col = (col + 7) % 8 + 1
  55. else:
  56. col += 1
  57. super().__init__(parser.info, msg, col)
  58. class QAPISchemaParser:
  59. """
  60. Parse QAPI schema source.
  61. Parse a JSON-esque schema file and process directives. See
  62. qapi-code-gen.rst section "Schema Syntax" for the exact syntax.
  63. Grammatical validation is handled later by `expr.check_exprs()`.
  64. :param fname: Source file name.
  65. :param previously_included:
  66. The absolute names of previously included source files,
  67. if being invoked from another parser.
  68. :param incl_info:
  69. `QAPISourceInfo` belonging to the parent module.
  70. ``None`` implies this is the root module.
  71. :ivar exprs: Resulting parsed expressions.
  72. :ivar docs: Resulting parsed documentation blocks.
  73. :raise OSError: For problems reading the root schema document.
  74. :raise QAPIError: For errors in the schema source.
  75. """
  76. def __init__(self,
  77. fname: str,
  78. previously_included: Optional[Set[str]] = None,
  79. incl_info: Optional[QAPISourceInfo] = None):
  80. self._fname = fname
  81. self._included = previously_included or set()
  82. self._included.add(os.path.abspath(self._fname))
  83. self.src = ''
  84. # Lexer state (see `accept` for details):
  85. self.info = QAPISourceInfo(self._fname, incl_info)
  86. self.tok: Union[None, str] = None
  87. self.pos = 0
  88. self.cursor = 0
  89. self.val: Optional[Union[bool, str]] = None
  90. self.line_pos = 0
  91. # Parser output:
  92. self.exprs: List[QAPIExpression] = []
  93. self.docs: List[QAPIDoc] = []
  94. # Showtime!
  95. self._parse()
  96. def _parse(self) -> None:
  97. """
  98. Parse the QAPI schema document.
  99. :return: None. Results are stored in ``.exprs`` and ``.docs``.
  100. """
  101. cur_doc = None
  102. # May raise OSError; allow the caller to handle it.
  103. with open(self._fname, 'r', encoding='utf-8') as fp:
  104. self.src = fp.read()
  105. if self.src == '' or self.src[-1] != '\n':
  106. self.src += '\n'
  107. # Prime the lexer:
  108. self.accept()
  109. # Parse until done:
  110. while self.tok is not None:
  111. info = self.info
  112. if self.tok == '#':
  113. self.reject_expr_doc(cur_doc)
  114. cur_doc = self.get_doc()
  115. self.docs.append(cur_doc)
  116. continue
  117. expr = self.get_expr()
  118. if not isinstance(expr, dict):
  119. raise QAPISemError(
  120. info, "top-level expression must be an object")
  121. if 'include' in expr:
  122. self.reject_expr_doc(cur_doc)
  123. if len(expr) != 1:
  124. raise QAPISemError(info, "invalid 'include' directive")
  125. include = expr['include']
  126. if not isinstance(include, str):
  127. raise QAPISemError(info,
  128. "value of 'include' must be a string")
  129. incl_fname = os.path.join(os.path.dirname(self._fname),
  130. include)
  131. self._add_expr({'include': incl_fname}, info)
  132. exprs_include = self._include(include, info, incl_fname,
  133. self._included)
  134. if exprs_include:
  135. self.exprs.extend(exprs_include.exprs)
  136. self.docs.extend(exprs_include.docs)
  137. elif "pragma" in expr:
  138. self.reject_expr_doc(cur_doc)
  139. if len(expr) != 1:
  140. raise QAPISemError(info, "invalid 'pragma' directive")
  141. pragma = expr['pragma']
  142. if not isinstance(pragma, dict):
  143. raise QAPISemError(
  144. info, "value of 'pragma' must be an object")
  145. for name, value in pragma.items():
  146. self._pragma(name, value, info)
  147. else:
  148. if cur_doc and not cur_doc.symbol:
  149. raise QAPISemError(
  150. cur_doc.info, "definition documentation required")
  151. self._add_expr(expr, info, cur_doc)
  152. cur_doc = None
  153. self.reject_expr_doc(cur_doc)
  154. def _add_expr(self, expr: Mapping[str, object],
  155. info: QAPISourceInfo,
  156. doc: Optional['QAPIDoc'] = None) -> None:
  157. self.exprs.append(QAPIExpression(expr, info, doc))
  158. @staticmethod
  159. def reject_expr_doc(doc: Optional['QAPIDoc']) -> None:
  160. if doc and doc.symbol:
  161. raise QAPISemError(
  162. doc.info,
  163. "documentation for '%s' is not followed by the definition"
  164. % doc.symbol)
  165. @staticmethod
  166. def _include(include: str,
  167. info: QAPISourceInfo,
  168. incl_fname: str,
  169. previously_included: Set[str]
  170. ) -> Optional['QAPISchemaParser']:
  171. incl_abs_fname = os.path.abspath(incl_fname)
  172. # catch inclusion cycle
  173. inf: Optional[QAPISourceInfo] = info
  174. while inf:
  175. if incl_abs_fname == os.path.abspath(inf.fname):
  176. raise QAPISemError(info, "inclusion loop for %s" % include)
  177. inf = inf.parent
  178. # skip multiple include of the same file
  179. if incl_abs_fname in previously_included:
  180. return None
  181. try:
  182. return QAPISchemaParser(incl_fname, previously_included, info)
  183. except OSError as err:
  184. raise QAPISemError(
  185. info,
  186. f"can't read include file '{incl_fname}': {err.strerror}"
  187. ) from err
  188. @staticmethod
  189. def _pragma(name: str, value: object, info: QAPISourceInfo) -> None:
  190. def check_list_str(name: str, value: object) -> List[str]:
  191. if (not isinstance(value, list) or
  192. any(not isinstance(elt, str) for elt in value)):
  193. raise QAPISemError(
  194. info,
  195. "pragma %s must be a list of strings" % name)
  196. return value
  197. pragma = info.pragma
  198. if name == 'doc-required':
  199. if not isinstance(value, bool):
  200. raise QAPISemError(info,
  201. "pragma 'doc-required' must be boolean")
  202. pragma.doc_required = value
  203. elif name == 'command-name-exceptions':
  204. pragma.command_name_exceptions = check_list_str(name, value)
  205. elif name == 'command-returns-exceptions':
  206. pragma.command_returns_exceptions = check_list_str(name, value)
  207. elif name == 'documentation-exceptions':
  208. pragma.documentation_exceptions = check_list_str(name, value)
  209. elif name == 'member-name-exceptions':
  210. pragma.member_name_exceptions = check_list_str(name, value)
  211. else:
  212. raise QAPISemError(info, "unknown pragma '%s'" % name)
  213. def accept(self, skip_comment: bool = True) -> None:
  214. """
  215. Read and store the next token.
  216. :param skip_comment:
  217. When false, return COMMENT tokens ("#").
  218. This is used when reading documentation blocks.
  219. :return:
  220. None. Several instance attributes are updated instead:
  221. - ``.tok`` represents the token type. See below for values.
  222. - ``.info`` describes the token's source location.
  223. - ``.val`` is the token's value, if any. See below.
  224. - ``.pos`` is the buffer index of the first character of
  225. the token.
  226. * Single-character tokens:
  227. These are "{", "}", ":", ",", "[", and "]".
  228. ``.tok`` holds the single character and ``.val`` is None.
  229. * Multi-character tokens:
  230. * COMMENT:
  231. This token is not normally returned by the lexer, but it can
  232. be when ``skip_comment`` is False. ``.tok`` is "#", and
  233. ``.val`` is a string including all chars until end-of-line,
  234. including the "#" itself.
  235. * STRING:
  236. ``.tok`` is "'", the single quote. ``.val`` contains the
  237. string, excluding the surrounding quotes.
  238. * TRUE and FALSE:
  239. ``.tok`` is either "t" or "f", ``.val`` will be the
  240. corresponding bool value.
  241. * EOF:
  242. ``.tok`` and ``.val`` will both be None at EOF.
  243. """
  244. while True:
  245. self.tok = self.src[self.cursor]
  246. self.pos = self.cursor
  247. self.cursor += 1
  248. self.val = None
  249. if self.tok == '#':
  250. if self.src[self.cursor] == '#':
  251. # Start of doc comment
  252. skip_comment = False
  253. self.cursor = self.src.find('\n', self.cursor)
  254. if not skip_comment:
  255. self.val = self.src[self.pos:self.cursor]
  256. return
  257. elif self.tok in '{}:,[]':
  258. return
  259. elif self.tok == "'":
  260. # Note: we accept only printable ASCII
  261. string = ''
  262. esc = False
  263. while True:
  264. ch = self.src[self.cursor]
  265. self.cursor += 1
  266. if ch == '\n':
  267. raise QAPIParseError(self, "missing terminating \"'\"")
  268. if esc:
  269. # Note: we recognize only \\ because we have
  270. # no use for funny characters in strings
  271. if ch != '\\':
  272. raise QAPIParseError(self,
  273. "unknown escape \\%s" % ch)
  274. esc = False
  275. elif ch == '\\':
  276. esc = True
  277. continue
  278. elif ch == "'":
  279. self.val = string
  280. return
  281. if ord(ch) < 32 or ord(ch) >= 127:
  282. raise QAPIParseError(
  283. self, "funny character in string")
  284. string += ch
  285. elif self.src.startswith('true', self.pos):
  286. self.val = True
  287. self.cursor += 3
  288. return
  289. elif self.src.startswith('false', self.pos):
  290. self.val = False
  291. self.cursor += 4
  292. return
  293. elif self.tok == '\n':
  294. if self.cursor == len(self.src):
  295. self.tok = None
  296. return
  297. self.info = self.info.next_line()
  298. self.line_pos = self.cursor
  299. elif not self.tok.isspace():
  300. # Show up to next structural, whitespace or quote
  301. # character
  302. match = must_match('[^[\\]{}:,\\s\']+',
  303. self.src[self.cursor-1:])
  304. raise QAPIParseError(self, "stray '%s'" % match.group(0))
  305. def get_members(self) -> Dict[str, object]:
  306. expr: Dict[str, object] = {}
  307. if self.tok == '}':
  308. self.accept()
  309. return expr
  310. if self.tok != "'":
  311. raise QAPIParseError(self, "expected string or '}'")
  312. while True:
  313. key = self.val
  314. assert isinstance(key, str) # Guaranteed by tok == "'"
  315. self.accept()
  316. if self.tok != ':':
  317. raise QAPIParseError(self, "expected ':'")
  318. self.accept()
  319. if key in expr:
  320. raise QAPIParseError(self, "duplicate key '%s'" % key)
  321. expr[key] = self.get_expr()
  322. if self.tok == '}':
  323. self.accept()
  324. return expr
  325. if self.tok != ',':
  326. raise QAPIParseError(self, "expected ',' or '}'")
  327. self.accept()
  328. if self.tok != "'":
  329. raise QAPIParseError(self, "expected string")
  330. def get_values(self) -> List[object]:
  331. expr: List[object] = []
  332. if self.tok == ']':
  333. self.accept()
  334. return expr
  335. if self.tok not in tuple("{['tf"):
  336. raise QAPIParseError(
  337. self, "expected '{', '[', ']', string, or boolean")
  338. while True:
  339. expr.append(self.get_expr())
  340. if self.tok == ']':
  341. self.accept()
  342. return expr
  343. if self.tok != ',':
  344. raise QAPIParseError(self, "expected ',' or ']'")
  345. self.accept()
  346. def get_expr(self) -> _ExprValue:
  347. expr: _ExprValue
  348. if self.tok == '{':
  349. self.accept()
  350. expr = self.get_members()
  351. elif self.tok == '[':
  352. self.accept()
  353. expr = self.get_values()
  354. elif self.tok in tuple("'tf"):
  355. assert isinstance(self.val, (str, bool))
  356. expr = self.val
  357. self.accept()
  358. else:
  359. raise QAPIParseError(
  360. self, "expected '{', '[', string, or boolean")
  361. return expr
  362. def get_doc_line(self) -> Optional[str]:
  363. if self.tok != '#':
  364. raise QAPIParseError(
  365. self, "documentation comment must end with '##'")
  366. assert isinstance(self.val, str)
  367. if self.val.startswith('##'):
  368. # End of doc comment
  369. if self.val != '##':
  370. raise QAPIParseError(
  371. self, "junk after '##' at end of documentation comment")
  372. return None
  373. if self.val == '#':
  374. return ''
  375. if self.val[1] != ' ':
  376. raise QAPIParseError(self, "missing space after #")
  377. return self.val[2:].rstrip()
  378. @staticmethod
  379. def _match_at_name_colon(string: str) -> Optional[Match[str]]:
  380. return re.match(r'@([^:]*): *', string)
  381. def get_doc_indented(self, doc: 'QAPIDoc') -> Optional[str]:
  382. self.accept(False)
  383. line = self.get_doc_line()
  384. while line == '':
  385. doc.append_line(line)
  386. self.accept(False)
  387. line = self.get_doc_line()
  388. if line is None:
  389. return line
  390. indent = must_match(r'\s*', line).end()
  391. if not indent:
  392. return line
  393. doc.append_line(line)
  394. prev_line_blank = False
  395. while True:
  396. self.accept(False)
  397. line = self.get_doc_line()
  398. if line is None:
  399. return line
  400. if self._match_at_name_colon(line):
  401. return line
  402. cur_indent = must_match(r'\s*', line).end()
  403. if line != '' and cur_indent < indent:
  404. if prev_line_blank:
  405. return line
  406. raise QAPIParseError(
  407. self,
  408. "unexpected de-indent (expected at least %d spaces)" %
  409. indent)
  410. doc.append_line(line)
  411. prev_line_blank = True
  412. def get_doc_paragraph(self, doc: 'QAPIDoc') -> Optional[str]:
  413. while True:
  414. self.accept(False)
  415. line = self.get_doc_line()
  416. if line is None:
  417. return line
  418. if line == '':
  419. return line
  420. doc.append_line(line)
  421. def get_doc(self) -> 'QAPIDoc':
  422. if self.val != '##':
  423. raise QAPIParseError(
  424. self, "junk after '##' at start of documentation comment")
  425. info = self.info
  426. self.accept(False)
  427. line = self.get_doc_line()
  428. if line is not None and line.startswith('@'):
  429. # Definition documentation
  430. if not line.endswith(':'):
  431. raise QAPIParseError(self, "line should end with ':'")
  432. # Invalid names are not checked here, but the name
  433. # provided *must* match the following definition,
  434. # which *is* validated in expr.py.
  435. symbol = line[1:-1]
  436. if not symbol:
  437. raise QAPIParseError(self, "name required after '@'")
  438. doc = QAPIDoc(info, symbol)
  439. self.accept(False)
  440. line = self.get_doc_line()
  441. no_more_args = False
  442. while line is not None:
  443. # Blank lines
  444. while line == '':
  445. self.accept(False)
  446. line = self.get_doc_line()
  447. if line is None:
  448. break
  449. # Non-blank line, first of a section
  450. if line == 'Features:':
  451. if doc.features:
  452. raise QAPIParseError(
  453. self, "duplicated 'Features:' line")
  454. self.accept(False)
  455. line = self.get_doc_line()
  456. while line == '':
  457. self.accept(False)
  458. line = self.get_doc_line()
  459. while (line is not None
  460. and (match := self._match_at_name_colon(line))):
  461. doc.new_feature(self.info, match.group(1))
  462. text = line[match.end():]
  463. if text:
  464. doc.append_line(text)
  465. line = self.get_doc_indented(doc)
  466. if not doc.features:
  467. raise QAPIParseError(
  468. self, 'feature descriptions expected')
  469. no_more_args = True
  470. elif match := self._match_at_name_colon(line):
  471. # description
  472. if no_more_args:
  473. raise QAPIParseError(
  474. self,
  475. "description of '@%s:' follows a section"
  476. % match.group(1))
  477. while (line is not None
  478. and (match := self._match_at_name_colon(line))):
  479. doc.new_argument(self.info, match.group(1))
  480. text = line[match.end():]
  481. if text:
  482. doc.append_line(text)
  483. line = self.get_doc_indented(doc)
  484. no_more_args = True
  485. elif match := re.match(
  486. r'(Returns|Errors|Since|Notes?|Examples?|TODO)'
  487. r'(?!::): *',
  488. line,
  489. ):
  490. # tagged section
  491. # Note: "sections" with two colons are left alone as
  492. # rST markup and not interpreted as a section heading.
  493. # TODO: Remove these errors sometime in 2025 or so
  494. # after we've fully transitioned to the new qapidoc
  495. # generator.
  496. # See commit message for more markup suggestions O:-)
  497. if 'Note' in match.group(1):
  498. emsg = (
  499. f"The '{match.group(1)}' section is no longer "
  500. "supported. Please use rST's '.. note::' or "
  501. "'.. admonition:: notes' directives, or another "
  502. "suitable admonition instead."
  503. )
  504. raise QAPIParseError(self, emsg)
  505. if 'Example' in match.group(1):
  506. emsg = (
  507. f"The '{match.group(1)}' section is no longer "
  508. "supported. Please use the '.. qmp-example::' "
  509. "directive, or other suitable markup instead."
  510. )
  511. raise QAPIParseError(self, emsg)
  512. doc.new_tagged_section(
  513. self.info,
  514. QAPIDoc.Kind.from_string(match.group(1))
  515. )
  516. text = line[match.end():]
  517. if text:
  518. doc.append_line(text)
  519. line = self.get_doc_indented(doc)
  520. no_more_args = True
  521. elif line.startswith('='):
  522. raise QAPIParseError(
  523. self,
  524. "unexpected '=' markup in definition documentation")
  525. else:
  526. # plain paragraph
  527. doc.ensure_untagged_section(self.info)
  528. doc.append_line(line)
  529. line = self.get_doc_paragraph(doc)
  530. else:
  531. # Free-form documentation
  532. doc = QAPIDoc(info)
  533. doc.ensure_untagged_section(self.info)
  534. first = True
  535. while line is not None:
  536. if match := self._match_at_name_colon(line):
  537. raise QAPIParseError(
  538. self,
  539. "'@%s:' not allowed in free-form documentation"
  540. % match.group(1))
  541. if line.startswith('='):
  542. if not first:
  543. raise QAPIParseError(
  544. self,
  545. "'=' heading must come first in a comment block")
  546. doc.append_line(line)
  547. self.accept(False)
  548. line = self.get_doc_line()
  549. first = False
  550. self.accept()
  551. doc.end()
  552. return doc
  553. class QAPIDoc:
  554. """
  555. A documentation comment block, either definition or free-form
  556. Definition documentation blocks consist of
  557. * a body section: one line naming the definition, followed by an
  558. overview (any number of lines)
  559. * argument sections: a description of each argument (for commands
  560. and events) or member (for structs, unions and alternates)
  561. * features sections: a description of each feature flag
  562. * additional (non-argument) sections, possibly tagged
  563. Free-form documentation blocks consist only of a body section.
  564. """
  565. class Kind(enum.Enum):
  566. PLAIN = 0
  567. MEMBER = 1
  568. FEATURE = 2
  569. RETURNS = 3
  570. ERRORS = 4
  571. SINCE = 5
  572. TODO = 6
  573. @staticmethod
  574. def from_string(kind: str) -> 'QAPIDoc.Kind':
  575. return QAPIDoc.Kind[kind.upper()]
  576. def __str__(self) -> str:
  577. return self.name.title()
  578. class Section:
  579. # pylint: disable=too-few-public-methods
  580. def __init__(
  581. self,
  582. info: QAPISourceInfo,
  583. kind: 'QAPIDoc.Kind',
  584. ):
  585. # section source info, i.e. where it begins
  586. self.info = info
  587. # section kind
  588. self.kind = kind
  589. # section text without tag
  590. self.text = ''
  591. def __repr__(self) -> str:
  592. return f"<QAPIDoc.Section kind={self.kind!r} text={self.text!r}>"
  593. def append_line(self, line: str) -> None:
  594. self.text += line + '\n'
  595. class ArgSection(Section):
  596. def __init__(
  597. self,
  598. info: QAPISourceInfo,
  599. kind: 'QAPIDoc.Kind',
  600. name: str
  601. ):
  602. super().__init__(info, kind)
  603. self.name = name
  604. self.member: Optional['QAPISchemaMember'] = None
  605. def connect(self, member: 'QAPISchemaMember') -> None:
  606. self.member = member
  607. def __init__(self, info: QAPISourceInfo, symbol: Optional[str] = None):
  608. # info points to the doc comment block's first line
  609. self.info = info
  610. # definition doc's symbol, None for free-form doc
  611. self.symbol: Optional[str] = symbol
  612. # the sections in textual order
  613. self.all_sections: List[QAPIDoc.Section] = [
  614. QAPIDoc.Section(info, QAPIDoc.Kind.PLAIN)
  615. ]
  616. # the body section
  617. self.body: Optional[QAPIDoc.Section] = self.all_sections[0]
  618. # dicts mapping parameter/feature names to their description
  619. self.args: Dict[str, QAPIDoc.ArgSection] = {}
  620. self.features: Dict[str, QAPIDoc.ArgSection] = {}
  621. # a command's "Returns" and "Errors" section
  622. self.returns: Optional[QAPIDoc.Section] = None
  623. self.errors: Optional[QAPIDoc.Section] = None
  624. # "Since" section
  625. self.since: Optional[QAPIDoc.Section] = None
  626. # sections other than .body, .args, .features
  627. self.sections: List[QAPIDoc.Section] = []
  628. def end(self) -> None:
  629. for section in self.all_sections:
  630. section.text = section.text.strip('\n')
  631. if section.kind != QAPIDoc.Kind.PLAIN and section.text == '':
  632. raise QAPISemError(
  633. section.info, "text required after '%s:'" % section.kind)
  634. def ensure_untagged_section(self, info: QAPISourceInfo) -> None:
  635. kind = QAPIDoc.Kind.PLAIN
  636. if self.all_sections and self.all_sections[-1].kind == kind:
  637. # extend current section
  638. section = self.all_sections[-1]
  639. if not section.text:
  640. # Section is empty so far; update info to start *here*.
  641. section.info = info
  642. section.text += '\n'
  643. return
  644. # start new section
  645. section = self.Section(info, kind)
  646. self.sections.append(section)
  647. self.all_sections.append(section)
  648. def new_tagged_section(
  649. self,
  650. info: QAPISourceInfo,
  651. kind: 'QAPIDoc.Kind',
  652. ) -> None:
  653. section = self.Section(info, kind)
  654. if kind == QAPIDoc.Kind.RETURNS:
  655. if self.returns:
  656. raise QAPISemError(
  657. info, "duplicated '%s' section" % kind)
  658. self.returns = section
  659. elif kind == QAPIDoc.Kind.ERRORS:
  660. if self.errors:
  661. raise QAPISemError(
  662. info, "duplicated '%s' section" % kind)
  663. self.errors = section
  664. elif kind == QAPIDoc.Kind.SINCE:
  665. if self.since:
  666. raise QAPISemError(
  667. info, "duplicated '%s' section" % kind)
  668. self.since = section
  669. self.sections.append(section)
  670. self.all_sections.append(section)
  671. def _new_description(
  672. self,
  673. info: QAPISourceInfo,
  674. name: str,
  675. kind: 'QAPIDoc.Kind',
  676. desc: Dict[str, ArgSection]
  677. ) -> None:
  678. if not name:
  679. raise QAPISemError(info, "invalid parameter name")
  680. if name in desc:
  681. raise QAPISemError(info, "'%s' parameter name duplicated" % name)
  682. section = self.ArgSection(info, kind, name)
  683. self.all_sections.append(section)
  684. desc[name] = section
  685. def new_argument(self, info: QAPISourceInfo, name: str) -> None:
  686. self._new_description(info, name, QAPIDoc.Kind.MEMBER, self.args)
  687. def new_feature(self, info: QAPISourceInfo, name: str) -> None:
  688. self._new_description(info, name, QAPIDoc.Kind.FEATURE, self.features)
  689. def append_line(self, line: str) -> None:
  690. self.all_sections[-1].append_line(line)
  691. def connect_member(self, member: 'QAPISchemaMember') -> None:
  692. if member.name not in self.args:
  693. assert member.info
  694. if self.symbol not in member.info.pragma.documentation_exceptions:
  695. raise QAPISemError(member.info,
  696. "%s '%s' lacks documentation"
  697. % (member.role, member.name))
  698. # Insert stub documentation section for missing member docs.
  699. # TODO: drop when undocumented members are outlawed
  700. section = QAPIDoc.ArgSection(
  701. self.info, QAPIDoc.Kind.MEMBER, member.name)
  702. self.args[member.name] = section
  703. # Determine where to insert stub doc - it should go at the
  704. # end of the members section(s), if any. Note that index 0
  705. # is assumed to be an untagged intro section, even if it is
  706. # empty.
  707. index = 1
  708. if len(self.all_sections) > 1:
  709. while self.all_sections[index].kind == QAPIDoc.Kind.MEMBER:
  710. index += 1
  711. self.all_sections.insert(index, section)
  712. self.args[member.name].connect(member)
  713. def connect_feature(self, feature: 'QAPISchemaFeature') -> None:
  714. if feature.name not in self.features:
  715. raise QAPISemError(feature.info,
  716. "feature '%s' lacks documentation"
  717. % feature.name)
  718. self.features[feature.name].connect(feature)
  719. def check_expr(self, expr: QAPIExpression) -> None:
  720. if 'command' in expr:
  721. if self.returns and 'returns' not in expr:
  722. raise QAPISemError(
  723. self.returns.info,
  724. "'Returns' section, but command doesn't return anything")
  725. else:
  726. if self.returns:
  727. raise QAPISemError(
  728. self.returns.info,
  729. "'Returns' section is only valid for commands")
  730. if self.errors:
  731. raise QAPISemError(
  732. self.errors.info,
  733. "'Errors' section is only valid for commands")
  734. def check(self) -> None:
  735. def check_args_section(
  736. args: Dict[str, QAPIDoc.ArgSection], what: str
  737. ) -> None:
  738. bogus = [name for name, section in args.items()
  739. if not section.member]
  740. if bogus:
  741. raise QAPISemError(
  742. args[bogus[0]].info,
  743. "documented %s%s '%s' %s not exist" % (
  744. what,
  745. "s" if len(bogus) > 1 else "",
  746. "', '".join(bogus),
  747. "do" if len(bogus) > 1 else "does"
  748. ))
  749. check_args_section(self.args, 'member')
  750. check_args_section(self.features, 'feature')