parser.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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 .error import QAPIParseError, QAPISemError
  20. from .source import QAPISourceInfo
  21. class QAPISchemaParser:
  22. def __init__(self, fname, previously_included=None, incl_info=None):
  23. previously_included = previously_included or set()
  24. previously_included.add(os.path.abspath(fname))
  25. try:
  26. fp = open(fname, 'r', encoding='utf-8')
  27. self.src = fp.read()
  28. except IOError as e:
  29. raise QAPISemError(incl_info or QAPISourceInfo(None, None, None),
  30. "can't read %s file '%s': %s"
  31. % ("include" if incl_info else "schema",
  32. fname,
  33. e.strerror))
  34. if self.src == '' or self.src[-1] != '\n':
  35. self.src += '\n'
  36. self.cursor = 0
  37. self.info = QAPISourceInfo(fname, 1, incl_info)
  38. self.line_pos = 0
  39. self.exprs = []
  40. self.docs = []
  41. self.accept()
  42. cur_doc = None
  43. while self.tok is not None:
  44. info = self.info
  45. if self.tok == '#':
  46. self.reject_expr_doc(cur_doc)
  47. for cur_doc in self.get_doc(info):
  48. self.docs.append(cur_doc)
  49. continue
  50. expr = self.get_expr(False)
  51. if 'include' in expr:
  52. self.reject_expr_doc(cur_doc)
  53. if len(expr) != 1:
  54. raise QAPISemError(info, "invalid 'include' directive")
  55. include = expr['include']
  56. if not isinstance(include, str):
  57. raise QAPISemError(info,
  58. "value of 'include' must be a string")
  59. incl_fname = os.path.join(os.path.dirname(fname),
  60. include)
  61. self.exprs.append({'expr': {'include': incl_fname},
  62. 'info': info})
  63. exprs_include = self._include(include, info, incl_fname,
  64. previously_included)
  65. if exprs_include:
  66. self.exprs.extend(exprs_include.exprs)
  67. self.docs.extend(exprs_include.docs)
  68. elif "pragma" in expr:
  69. self.reject_expr_doc(cur_doc)
  70. if len(expr) != 1:
  71. raise QAPISemError(info, "invalid 'pragma' directive")
  72. pragma = expr['pragma']
  73. if not isinstance(pragma, dict):
  74. raise QAPISemError(
  75. info, "value of 'pragma' must be an object")
  76. for name, value in pragma.items():
  77. self._pragma(name, value, info)
  78. else:
  79. expr_elem = {'expr': expr,
  80. 'info': info}
  81. if cur_doc:
  82. if not cur_doc.symbol:
  83. raise QAPISemError(
  84. cur_doc.info, "definition documentation required")
  85. expr_elem['doc'] = cur_doc
  86. self.exprs.append(expr_elem)
  87. cur_doc = None
  88. self.reject_expr_doc(cur_doc)
  89. @staticmethod
  90. def reject_expr_doc(doc):
  91. if doc and doc.symbol:
  92. raise QAPISemError(
  93. doc.info,
  94. "documentation for '%s' is not followed by the definition"
  95. % doc.symbol)
  96. def _include(self, include, info, incl_fname, previously_included):
  97. incl_abs_fname = os.path.abspath(incl_fname)
  98. # catch inclusion cycle
  99. inf = info
  100. while inf:
  101. if incl_abs_fname == os.path.abspath(inf.fname):
  102. raise QAPISemError(info, "inclusion loop for %s" % include)
  103. inf = inf.parent
  104. # skip multiple include of the same file
  105. if incl_abs_fname in previously_included:
  106. return None
  107. return QAPISchemaParser(incl_fname, previously_included, info)
  108. def _check_pragma_list_of_str(self, name, value, info):
  109. if (not isinstance(value, list)
  110. or any([not isinstance(elt, str) for elt in value])):
  111. raise QAPISemError(
  112. info,
  113. "pragma %s must be a list of strings" % name)
  114. def _pragma(self, name, value, info):
  115. if name == 'doc-required':
  116. if not isinstance(value, bool):
  117. raise QAPISemError(info,
  118. "pragma 'doc-required' must be boolean")
  119. info.pragma.doc_required = value
  120. elif name == 'command-name-exceptions':
  121. self._check_pragma_list_of_str(name, value, info)
  122. info.pragma.command_name_exceptions = value
  123. elif name == 'command-returns-exceptions':
  124. self._check_pragma_list_of_str(name, value, info)
  125. info.pragma.command_returns_exceptions = value
  126. elif name == 'member-name-exceptions':
  127. self._check_pragma_list_of_str(name, value, info)
  128. info.pragma.member_name_exceptions = value
  129. else:
  130. raise QAPISemError(info, "unknown pragma '%s'" % name)
  131. def accept(self, skip_comment=True):
  132. while True:
  133. self.tok = self.src[self.cursor]
  134. self.pos = self.cursor
  135. self.cursor += 1
  136. self.val = None
  137. if self.tok == '#':
  138. if self.src[self.cursor] == '#':
  139. # Start of doc comment
  140. skip_comment = False
  141. self.cursor = self.src.find('\n', self.cursor)
  142. if not skip_comment:
  143. self.val = self.src[self.pos:self.cursor]
  144. return
  145. elif self.tok in '{}:,[]':
  146. return
  147. elif self.tok == "'":
  148. # Note: we accept only printable ASCII
  149. string = ''
  150. esc = False
  151. while True:
  152. ch = self.src[self.cursor]
  153. self.cursor += 1
  154. if ch == '\n':
  155. raise QAPIParseError(self, "missing terminating \"'\"")
  156. if esc:
  157. # Note: we recognize only \\ because we have
  158. # no use for funny characters in strings
  159. if ch != '\\':
  160. raise QAPIParseError(self,
  161. "unknown escape \\%s" % ch)
  162. esc = False
  163. elif ch == '\\':
  164. esc = True
  165. continue
  166. elif ch == "'":
  167. self.val = string
  168. return
  169. if ord(ch) < 32 or ord(ch) >= 127:
  170. raise QAPIParseError(
  171. self, "funny character in string")
  172. string += ch
  173. elif self.src.startswith('true', self.pos):
  174. self.val = True
  175. self.cursor += 3
  176. return
  177. elif self.src.startswith('false', self.pos):
  178. self.val = False
  179. self.cursor += 4
  180. return
  181. elif self.tok == '\n':
  182. if self.cursor == len(self.src):
  183. self.tok = None
  184. return
  185. self.info = self.info.next_line()
  186. self.line_pos = self.cursor
  187. elif not self.tok.isspace():
  188. # Show up to next structural, whitespace or quote
  189. # character
  190. match = re.match('[^[\\]{}:,\\s\'"]+',
  191. self.src[self.cursor-1:])
  192. raise QAPIParseError(self, "stray '%s'" % match.group(0))
  193. def get_members(self):
  194. expr = OrderedDict()
  195. if self.tok == '}':
  196. self.accept()
  197. return expr
  198. if self.tok != "'":
  199. raise QAPIParseError(self, "expected string or '}'")
  200. while True:
  201. key = self.val
  202. self.accept()
  203. if self.tok != ':':
  204. raise QAPIParseError(self, "expected ':'")
  205. self.accept()
  206. if key in expr:
  207. raise QAPIParseError(self, "duplicate key '%s'" % key)
  208. expr[key] = self.get_expr(True)
  209. if self.tok == '}':
  210. self.accept()
  211. return expr
  212. if self.tok != ',':
  213. raise QAPIParseError(self, "expected ',' or '}'")
  214. self.accept()
  215. if self.tok != "'":
  216. raise QAPIParseError(self, "expected string")
  217. def get_values(self):
  218. expr = []
  219. if self.tok == ']':
  220. self.accept()
  221. return expr
  222. if self.tok not in "{['tf":
  223. raise QAPIParseError(
  224. self, "expected '{', '[', ']', string, or boolean")
  225. while True:
  226. expr.append(self.get_expr(True))
  227. if self.tok == ']':
  228. self.accept()
  229. return expr
  230. if self.tok != ',':
  231. raise QAPIParseError(self, "expected ',' or ']'")
  232. self.accept()
  233. def get_expr(self, nested):
  234. if self.tok != '{' and not nested:
  235. raise QAPIParseError(self, "expected '{'")
  236. if self.tok == '{':
  237. self.accept()
  238. expr = self.get_members()
  239. elif self.tok == '[':
  240. self.accept()
  241. expr = self.get_values()
  242. elif self.tok in "'tf":
  243. expr = self.val
  244. self.accept()
  245. else:
  246. raise QAPIParseError(
  247. self, "expected '{', '[', string, or boolean")
  248. return expr
  249. def get_doc(self, info):
  250. if self.val != '##':
  251. raise QAPIParseError(
  252. self, "junk after '##' at start of documentation comment")
  253. docs = []
  254. cur_doc = QAPIDoc(self, info)
  255. self.accept(False)
  256. while self.tok == '#':
  257. if self.val.startswith('##'):
  258. # End of doc comment
  259. if self.val != '##':
  260. raise QAPIParseError(
  261. self,
  262. "junk after '##' at end of documentation comment")
  263. cur_doc.end_comment()
  264. docs.append(cur_doc)
  265. self.accept()
  266. return docs
  267. if self.val.startswith('# ='):
  268. if cur_doc.symbol:
  269. raise QAPIParseError(
  270. self,
  271. "unexpected '=' markup in definition documentation")
  272. if cur_doc.body.text:
  273. cur_doc.end_comment()
  274. docs.append(cur_doc)
  275. cur_doc = QAPIDoc(self, info)
  276. cur_doc.append(self.val)
  277. self.accept(False)
  278. raise QAPIParseError(self, "documentation comment must end with '##'")
  279. class QAPIDoc:
  280. """
  281. A documentation comment block, either definition or free-form
  282. Definition documentation blocks consist of
  283. * a body section: one line naming the definition, followed by an
  284. overview (any number of lines)
  285. * argument sections: a description of each argument (for commands
  286. and events) or member (for structs, unions and alternates)
  287. * features sections: a description of each feature flag
  288. * additional (non-argument) sections, possibly tagged
  289. Free-form documentation blocks consist only of a body section.
  290. """
  291. class Section:
  292. def __init__(self, parser, name=None, indent=0):
  293. # parser, for error messages about indentation
  294. self._parser = parser
  295. # optional section name (argument/member or section name)
  296. self.name = name
  297. self.text = ''
  298. # the expected indent level of the text of this section
  299. self._indent = indent
  300. def append(self, line):
  301. # Strip leading spaces corresponding to the expected indent level
  302. # Blank lines are always OK.
  303. if line:
  304. indent = re.match(r'\s*', line).end()
  305. if indent < self._indent:
  306. raise QAPIParseError(
  307. self._parser,
  308. "unexpected de-indent (expected at least %d spaces)" %
  309. self._indent)
  310. line = line[self._indent:]
  311. self.text += line.rstrip() + '\n'
  312. class ArgSection(Section):
  313. def __init__(self, parser, name, indent=0):
  314. super().__init__(parser, name, indent)
  315. self.member = None
  316. def connect(self, member):
  317. self.member = member
  318. def __init__(self, parser, info):
  319. # self._parser is used to report errors with QAPIParseError. The
  320. # resulting error position depends on the state of the parser.
  321. # It happens to be the beginning of the comment. More or less
  322. # servicable, but action at a distance.
  323. self._parser = parser
  324. self.info = info
  325. self.symbol = None
  326. self.body = QAPIDoc.Section(parser)
  327. # dict mapping parameter name to ArgSection
  328. self.args = OrderedDict()
  329. self.features = OrderedDict()
  330. # a list of Section
  331. self.sections = []
  332. # the current section
  333. self._section = self.body
  334. self._append_line = self._append_body_line
  335. def has_section(self, name):
  336. """Return True if we have a section with this name."""
  337. for i in self.sections:
  338. if i.name == name:
  339. return True
  340. return False
  341. def append(self, line):
  342. """
  343. Parse a comment line and add it to the documentation.
  344. The way that the line is dealt with depends on which part of
  345. the documentation we're parsing right now:
  346. * The body section: ._append_line is ._append_body_line
  347. * An argument section: ._append_line is ._append_args_line
  348. * A features section: ._append_line is ._append_features_line
  349. * An additional section: ._append_line is ._append_various_line
  350. """
  351. line = line[1:]
  352. if not line:
  353. self._append_freeform(line)
  354. return
  355. if line[0] != ' ':
  356. raise QAPIParseError(self._parser, "missing space after #")
  357. line = line[1:]
  358. self._append_line(line)
  359. def end_comment(self):
  360. self._end_section()
  361. @staticmethod
  362. def _is_section_tag(name):
  363. return name in ('Returns:', 'Since:',
  364. # those are often singular or plural
  365. 'Note:', 'Notes:',
  366. 'Example:', 'Examples:',
  367. 'TODO:')
  368. def _append_body_line(self, line):
  369. """
  370. Process a line of documentation text in the body section.
  371. If this a symbol line and it is the section's first line, this
  372. is a definition documentation block for that symbol.
  373. If it's a definition documentation block, another symbol line
  374. begins the argument section for the argument named by it, and
  375. a section tag begins an additional section. Start that
  376. section and append the line to it.
  377. Else, append the line to the current section.
  378. """
  379. name = line.split(' ', 1)[0]
  380. # FIXME not nice: things like '# @foo:' and '# @foo: ' aren't
  381. # recognized, and get silently treated as ordinary text
  382. if not self.symbol and not self.body.text and line.startswith('@'):
  383. if not line.endswith(':'):
  384. raise QAPIParseError(self._parser, "line should end with ':'")
  385. self.symbol = line[1:-1]
  386. # FIXME invalid names other than the empty string aren't flagged
  387. if not self.symbol:
  388. raise QAPIParseError(self._parser, "invalid name")
  389. elif self.symbol:
  390. # This is a definition documentation block
  391. if name.startswith('@') and name.endswith(':'):
  392. self._append_line = self._append_args_line
  393. self._append_args_line(line)
  394. elif line == 'Features:':
  395. self._append_line = self._append_features_line
  396. elif self._is_section_tag(name):
  397. self._append_line = self._append_various_line
  398. self._append_various_line(line)
  399. else:
  400. self._append_freeform(line)
  401. else:
  402. # This is a free-form documentation block
  403. self._append_freeform(line)
  404. def _append_args_line(self, line):
  405. """
  406. Process a line of documentation text in an argument section.
  407. A symbol line begins the next argument section, a section tag
  408. section or a non-indented line after a blank line begins an
  409. additional section. Start that section and append the line to
  410. it.
  411. Else, append the line to the current section.
  412. """
  413. name = line.split(' ', 1)[0]
  414. if name.startswith('@') and name.endswith(':'):
  415. # If line is "@arg: first line of description", find
  416. # the index of 'f', which is the indent we expect for any
  417. # following lines. We then remove the leading "@arg:"
  418. # from line and replace it with spaces so that 'f' has the
  419. # same index as it did in the original line and can be
  420. # handled the same way we will handle following lines.
  421. indent = re.match(r'@\S*:\s*', line).end()
  422. line = line[indent:]
  423. if not line:
  424. # Line was just the "@arg:" header; following lines
  425. # are not indented
  426. indent = 0
  427. else:
  428. line = ' ' * indent + line
  429. self._start_args_section(name[1:-1], indent)
  430. elif self._is_section_tag(name):
  431. self._append_line = self._append_various_line
  432. self._append_various_line(line)
  433. return
  434. elif (self._section.text.endswith('\n\n')
  435. and line and not line[0].isspace()):
  436. if line == 'Features:':
  437. self._append_line = self._append_features_line
  438. else:
  439. self._start_section()
  440. self._append_line = self._append_various_line
  441. self._append_various_line(line)
  442. return
  443. self._append_freeform(line)
  444. def _append_features_line(self, line):
  445. name = line.split(' ', 1)[0]
  446. if name.startswith('@') and name.endswith(':'):
  447. # If line is "@arg: first line of description", find
  448. # the index of 'f', which is the indent we expect for any
  449. # following lines. We then remove the leading "@arg:"
  450. # from line and replace it with spaces so that 'f' has the
  451. # same index as it did in the original line and can be
  452. # handled the same way we will handle following lines.
  453. indent = re.match(r'@\S*:\s*', line).end()
  454. line = line[indent:]
  455. if not line:
  456. # Line was just the "@arg:" header; following lines
  457. # are not indented
  458. indent = 0
  459. else:
  460. line = ' ' * indent + line
  461. self._start_features_section(name[1:-1], indent)
  462. elif self._is_section_tag(name):
  463. self._append_line = self._append_various_line
  464. self._append_various_line(line)
  465. return
  466. elif (self._section.text.endswith('\n\n')
  467. and line and not line[0].isspace()):
  468. self._start_section()
  469. self._append_line = self._append_various_line
  470. self._append_various_line(line)
  471. return
  472. self._append_freeform(line)
  473. def _append_various_line(self, line):
  474. """
  475. Process a line of documentation text in an additional section.
  476. A symbol line is an error.
  477. A section tag begins an additional section. Start that
  478. section and append the line to it.
  479. Else, append the line to the current section.
  480. """
  481. name = line.split(' ', 1)[0]
  482. if name.startswith('@') and name.endswith(':'):
  483. raise QAPIParseError(self._parser,
  484. "'%s' can't follow '%s' section"
  485. % (name, self.sections[0].name))
  486. if self._is_section_tag(name):
  487. # If line is "Section: first line of description", find
  488. # the index of 'f', which is the indent we expect for any
  489. # following lines. We then remove the leading "Section:"
  490. # from line and replace it with spaces so that 'f' has the
  491. # same index as it did in the original line and can be
  492. # handled the same way we will handle following lines.
  493. indent = re.match(r'\S*:\s*', line).end()
  494. line = line[indent:]
  495. if not line:
  496. # Line was just the "Section:" header; following lines
  497. # are not indented
  498. indent = 0
  499. else:
  500. line = ' ' * indent + line
  501. self._start_section(name[:-1], indent)
  502. self._append_freeform(line)
  503. def _start_symbol_section(self, symbols_dict, name, indent):
  504. # FIXME invalid names other than the empty string aren't flagged
  505. if not name:
  506. raise QAPIParseError(self._parser, "invalid parameter name")
  507. if name in symbols_dict:
  508. raise QAPIParseError(self._parser,
  509. "'%s' parameter name duplicated" % name)
  510. assert not self.sections
  511. self._end_section()
  512. self._section = QAPIDoc.ArgSection(self._parser, name, indent)
  513. symbols_dict[name] = self._section
  514. def _start_args_section(self, name, indent):
  515. self._start_symbol_section(self.args, name, indent)
  516. def _start_features_section(self, name, indent):
  517. self._start_symbol_section(self.features, name, indent)
  518. def _start_section(self, name=None, indent=0):
  519. if name in ('Returns', 'Since') and self.has_section(name):
  520. raise QAPIParseError(self._parser,
  521. "duplicated '%s' section" % name)
  522. self._end_section()
  523. self._section = QAPIDoc.Section(self._parser, name, indent)
  524. self.sections.append(self._section)
  525. def _end_section(self):
  526. if self._section:
  527. text = self._section.text = self._section.text.strip()
  528. if self._section.name and (not text or text.isspace()):
  529. raise QAPIParseError(
  530. self._parser,
  531. "empty doc section '%s'" % self._section.name)
  532. self._section = None
  533. def _append_freeform(self, line):
  534. match = re.match(r'(@\S+:)', line)
  535. if match:
  536. raise QAPIParseError(self._parser,
  537. "'%s' not allowed in free-form documentation"
  538. % match.group(1))
  539. self._section.append(line)
  540. def connect_member(self, member):
  541. if member.name not in self.args:
  542. # Undocumented TODO outlaw
  543. self.args[member.name] = QAPIDoc.ArgSection(self._parser,
  544. member.name)
  545. self.args[member.name].connect(member)
  546. def connect_feature(self, feature):
  547. if feature.name not in self.features:
  548. raise QAPISemError(feature.info,
  549. "feature '%s' lacks documentation"
  550. % feature.name)
  551. self.features[feature.name].connect(feature)
  552. def check_expr(self, expr):
  553. if self.has_section('Returns') and 'command' not in expr:
  554. raise QAPISemError(self.info,
  555. "'Returns:' is only valid for commands")
  556. def check(self):
  557. def check_args_section(args, info, what):
  558. bogus = [name for name, section in args.items()
  559. if not section.member]
  560. if bogus:
  561. raise QAPISemError(
  562. self.info,
  563. "documented member%s '%s' %s not exist"
  564. % ("s" if len(bogus) > 1 else "",
  565. "', '".join(bogus),
  566. "do" if len(bogus) > 1 else "does"))
  567. check_args_section(self.args, self.info, 'members')
  568. check_args_section(self.features, self.info, 'features')