parser.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  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. from collections import OrderedDict
  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(OrderedDict({'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] = OrderedDict()
  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. line):
  488. # tagged section
  489. doc.new_tagged_section(self.info, match.group(1))
  490. text = line[match.end():]
  491. if text:
  492. doc.append_line(text)
  493. line = self.get_doc_indented(doc)
  494. no_more_args = True
  495. elif line.startswith('='):
  496. raise QAPIParseError(
  497. self,
  498. "unexpected '=' markup in definition documentation")
  499. else:
  500. # tag-less paragraph
  501. doc.ensure_untagged_section(self.info)
  502. doc.append_line(line)
  503. line = self.get_doc_paragraph(doc)
  504. else:
  505. # Free-form documentation
  506. doc = QAPIDoc(info)
  507. doc.ensure_untagged_section(self.info)
  508. first = True
  509. while line is not None:
  510. if match := self._match_at_name_colon(line):
  511. raise QAPIParseError(
  512. self,
  513. "'@%s:' not allowed in free-form documentation"
  514. % match.group(1))
  515. if line.startswith('='):
  516. if not first:
  517. raise QAPIParseError(
  518. self,
  519. "'=' heading must come first in a comment block")
  520. doc.append_line(line)
  521. self.accept(False)
  522. line = self.get_doc_line()
  523. first = False
  524. self.accept(False)
  525. doc.end()
  526. return doc
  527. class QAPIDoc:
  528. """
  529. A documentation comment block, either definition or free-form
  530. Definition documentation blocks consist of
  531. * a body section: one line naming the definition, followed by an
  532. overview (any number of lines)
  533. * argument sections: a description of each argument (for commands
  534. and events) or member (for structs, unions and alternates)
  535. * features sections: a description of each feature flag
  536. * additional (non-argument) sections, possibly tagged
  537. Free-form documentation blocks consist only of a body section.
  538. """
  539. class Section:
  540. # pylint: disable=too-few-public-methods
  541. def __init__(self, info: QAPISourceInfo,
  542. tag: Optional[str] = None):
  543. # section source info, i.e. where it begins
  544. self.info = info
  545. # section tag, if any ('Returns', '@name', ...)
  546. self.tag = tag
  547. # section text without tag
  548. self.text = ''
  549. def append_line(self, line: str) -> None:
  550. self.text += line + '\n'
  551. class ArgSection(Section):
  552. def __init__(self, info: QAPISourceInfo, tag: str):
  553. super().__init__(info, tag)
  554. self.member: Optional['QAPISchemaMember'] = None
  555. def connect(self, member: 'QAPISchemaMember') -> None:
  556. self.member = member
  557. def __init__(self, info: QAPISourceInfo, symbol: Optional[str] = None):
  558. # info points to the doc comment block's first line
  559. self.info = info
  560. # definition doc's symbol, None for free-form doc
  561. self.symbol: Optional[str] = symbol
  562. # the sections in textual order
  563. self.all_sections: List[QAPIDoc.Section] = [QAPIDoc.Section(info)]
  564. # the body section
  565. self.body: Optional[QAPIDoc.Section] = self.all_sections[0]
  566. # dicts mapping parameter/feature names to their description
  567. self.args: Dict[str, QAPIDoc.ArgSection] = {}
  568. self.features: Dict[str, QAPIDoc.ArgSection] = {}
  569. # a command's "Returns" and "Errors" section
  570. self.returns: Optional[QAPIDoc.Section] = None
  571. self.errors: Optional[QAPIDoc.Section] = None
  572. # "Since" section
  573. self.since: Optional[QAPIDoc.Section] = None
  574. # sections other than .body, .args, .features
  575. self.sections: List[QAPIDoc.Section] = []
  576. def end(self) -> None:
  577. for section in self.all_sections:
  578. section.text = section.text.strip('\n')
  579. if section.tag is not None and section.text == '':
  580. raise QAPISemError(
  581. section.info, "text required after '%s:'" % section.tag)
  582. def ensure_untagged_section(self, info: QAPISourceInfo) -> None:
  583. if self.all_sections and not self.all_sections[-1].tag:
  584. # extend current section
  585. self.all_sections[-1].text += '\n'
  586. return
  587. # start new section
  588. section = self.Section(info)
  589. self.sections.append(section)
  590. self.all_sections.append(section)
  591. def new_tagged_section(self, info: QAPISourceInfo, tag: str) -> None:
  592. section = self.Section(info, tag)
  593. if tag == 'Returns':
  594. if self.returns:
  595. raise QAPISemError(
  596. info, "duplicated '%s' section" % tag)
  597. self.returns = section
  598. elif tag == 'Errors':
  599. if self.errors:
  600. raise QAPISemError(
  601. info, "duplicated '%s' section" % tag)
  602. self.errors = section
  603. elif tag == 'Since':
  604. if self.since:
  605. raise QAPISemError(
  606. info, "duplicated '%s' section" % tag)
  607. self.since = section
  608. self.sections.append(section)
  609. self.all_sections.append(section)
  610. def _new_description(self, info: QAPISourceInfo, name: str,
  611. desc: Dict[str, ArgSection]) -> None:
  612. if not name:
  613. raise QAPISemError(info, "invalid parameter name")
  614. if name in desc:
  615. raise QAPISemError(info, "'%s' parameter name duplicated" % name)
  616. section = self.ArgSection(info, '@' + name)
  617. self.all_sections.append(section)
  618. desc[name] = section
  619. def new_argument(self, info: QAPISourceInfo, name: str) -> None:
  620. self._new_description(info, name, self.args)
  621. def new_feature(self, info: QAPISourceInfo, name: str) -> None:
  622. self._new_description(info, name, self.features)
  623. def append_line(self, line: str) -> None:
  624. self.all_sections[-1].append_line(line)
  625. def connect_member(self, member: 'QAPISchemaMember') -> None:
  626. if member.name not in self.args:
  627. assert member.info
  628. if self.symbol not in member.info.pragma.documentation_exceptions:
  629. raise QAPISemError(member.info,
  630. "%s '%s' lacks documentation"
  631. % (member.role, member.name))
  632. self.args[member.name] = QAPIDoc.ArgSection(
  633. self.info, '@' + member.name)
  634. self.args[member.name].connect(member)
  635. def connect_feature(self, feature: 'QAPISchemaFeature') -> None:
  636. if feature.name not in self.features:
  637. raise QAPISemError(feature.info,
  638. "feature '%s' lacks documentation"
  639. % feature.name)
  640. self.features[feature.name].connect(feature)
  641. def check_expr(self, expr: QAPIExpression) -> None:
  642. if 'command' in expr:
  643. if self.returns and 'returns' not in expr:
  644. raise QAPISemError(
  645. self.returns.info,
  646. "'Returns' section, but command doesn't return anything")
  647. else:
  648. if self.returns:
  649. raise QAPISemError(
  650. self.returns.info,
  651. "'Returns' section is only valid for commands")
  652. if self.errors:
  653. raise QAPISemError(
  654. self.errors.info,
  655. "'Errors' section is only valid for commands")
  656. def check(self) -> None:
  657. def check_args_section(
  658. args: Dict[str, QAPIDoc.ArgSection], what: str
  659. ) -> None:
  660. bogus = [name for name, section in args.items()
  661. if not section.member]
  662. if bogus:
  663. raise QAPISemError(
  664. args[bogus[0]].info,
  665. "documented %s%s '%s' %s not exist" % (
  666. what,
  667. "s" if len(bogus) > 1 else "",
  668. "', '".join(bogus),
  669. "do" if len(bogus) > 1 else "does"
  670. ))
  671. check_args_section(self.args, 'member')
  672. check_args_section(self.features, 'feature')