parser.py 21 KB

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