[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v2 20/21] qapi: [WIP] Add QAPIDocError
From: |
John Snow |
Subject: |
[PATCH v2 20/21] qapi: [WIP] Add QAPIDocError |
Date: |
Tue, 11 May 2021 18:06:00 -0400 |
Raise this error instead of QAPIParseError and delegate the context up
to the parent parser.
In a chat off-list, we discussed how this design forces us to continue
having less accurate error context information.
Still, it's useful for an extremely simple split without a lot of fuss.
Signed-off-by: John Snow <jsnow@redhat.com>
---
scripts/qapi/parser.py | 12 ++++++++++--
scripts/qapi/qapidoc.py | 36 +++++++++++++++++-------------------
2 files changed, 27 insertions(+), 21 deletions(-)
diff --git a/scripts/qapi/parser.py b/scripts/qapi/parser.py
index 940a427b1ae..cde9782c5b0 100644
--- a/scripts/qapi/parser.py
+++ b/scripts/qapi/parser.py
@@ -26,7 +26,7 @@
from .common import must_match
from .error import QAPISemError, QAPISourceError
-from .qapidoc import QAPIDoc
+from .qapidoc import QAPIDoc, QAPIDocError
from .source import QAPISourceInfo
@@ -391,7 +391,7 @@ def get_expr(self) -> _ExprValue:
self, "expected '{', '[', string, or boolean")
return expr
- def get_doc(self, info: QAPISourceInfo) -> List['QAPIDoc']:
+ def _get_doc(self, info: QAPISourceInfo) -> List['QAPIDoc']:
if self.val != '##':
raise QAPIParseError(
self, "junk after '##' at start of documentation comment")
@@ -424,3 +424,11 @@ def get_doc(self, info: QAPISourceInfo) -> List['QAPIDoc']:
self.accept(False)
raise QAPIParseError(self, "documentation comment must end with '##'")
+
+ def get_doc(self, info: QAPISourceInfo) -> List['QAPIDoc']:
+ # Tie QAPIDocError exceptions to the current parser state,
+ # re-raise as QAPIParseError.
+ try:
+ return self._get_doc(info)
+ except QAPIDocError as err:
+ raise QAPIParseError(self, str(err)) from err
diff --git a/scripts/qapi/qapidoc.py b/scripts/qapi/qapidoc.py
index 4985d9565b8..938e2009dcd 100644
--- a/scripts/qapi/qapidoc.py
+++ b/scripts/qapi/qapidoc.py
@@ -18,7 +18,11 @@
import re
from .common import must_match
-from .error import QAPISemError
+from .error import QAPIError, QAPISemError
+
+
+class QAPIDocError(QAPIError):
+ """QAPIDoc parsing errors."""
class QAPIDoc:
@@ -56,8 +60,7 @@ def append(self, line):
if line:
indent = must_match(r'\s*', line).end()
if indent < self._indent:
- raise QAPIParseError(
- self._parser,
+ raise QAPIDocError(
"unexpected de-indent (expected at least %d spaces)" %
self._indent)
line = line[self._indent:]
@@ -114,7 +117,7 @@ def append(self, line):
return
if line[0] != ' ':
- raise QAPIParseError(self._parser, "missing space after #")
+ raise QAPIDocError("missing space after #")
line = line[1:]
self._append_line(line)
@@ -148,11 +151,11 @@ def _append_body_line(self, line):
# recognized, and get silently treated as ordinary text
if not self.symbol and not self.body.text and line.startswith('@'):
if not line.endswith(':'):
- raise QAPIParseError(self._parser, "line should end with ':'")
+ raise QAPIDocError("line should end with ':'")
self.symbol = line[1:-1]
# FIXME invalid names other than the empty string aren't flagged
if not self.symbol:
- raise QAPIParseError(self._parser, "invalid name")
+ raise QAPIDocError("invalid name")
elif self.symbol:
# This is a definition documentation block
if name.startswith('@') and name.endswith(':'):
@@ -261,9 +264,8 @@ def _append_various_line(self, line):
name = line.split(' ', 1)[0]
if name.startswith('@') and name.endswith(':'):
- raise QAPIParseError(self._parser,
- "'%s' can't follow '%s' section"
- % (name, self.sections[0].name))
+ raise QAPIDocError("'%s' can't follow '%s' section"
+ % (name, self.sections[0].name))
if self._is_section_tag(name):
# If line is "Section: first line of description", find
# the index of 'f', which is the indent we expect for any
@@ -286,10 +288,9 @@ def _append_various_line(self, line):
def _start_symbol_section(self, symbols_dict, name, indent):
# FIXME invalid names other than the empty string aren't flagged
if not name:
- raise QAPIParseError(self._parser, "invalid parameter name")
+ raise QAPIDocError("invalid parameter name")
if name in symbols_dict:
- raise QAPIParseError(self._parser,
- "'%s' parameter name duplicated" % name)
+ raise QAPIDocError("'%s' parameter name duplicated" % name)
assert not self.sections
self._end_section()
self._section = QAPIDoc.ArgSection(self._parser, name, indent)
@@ -303,8 +304,7 @@ def _start_features_section(self, name, indent):
def _start_section(self, name=None, indent=0):
if name in ('Returns', 'Since') and self.has_section(name):
- raise QAPIParseError(self._parser,
- "duplicated '%s' section" % name)
+ raise QAPIDocError("duplicated '%s' section" % name)
self._end_section()
self._section = QAPIDoc.Section(self._parser, name, indent)
self.sections.append(self._section)
@@ -313,17 +313,15 @@ def _end_section(self):
if self._section:
text = self._section.text = self._section.text.strip()
if self._section.name and (not text or text.isspace()):
- raise QAPIParseError(
- self._parser,
+ raise QAPIDocError(
"empty doc section '%s'" % self._section.name)
self._section = None
def _append_freeform(self, line):
match = re.match(r'(@\S+:)', line)
if match:
- raise QAPIParseError(self._parser,
- "'%s' not allowed in free-form documentation"
- % match.group(1))
+ raise QAPIDocError("'%s' not allowed in free-form documentation"
+ % match.group(1))
self._section.append(line)
def connect_member(self, member):
--
2.30.2
- [PATCH v2 11/21] qapi/parser: Rework _check_pragma_list_of_str as a TypeGuard, (continued)
- [PATCH v2 11/21] qapi/parser: Rework _check_pragma_list_of_str as a TypeGuard, John Snow, 2021/05/11
- [PATCH v2 12/21] qapi/parser: add type hint annotations, John Snow, 2021/05/11
- [PATCH v2 13/21] qapi/parser: Remove superfluous list comprehension, John Snow, 2021/05/11
- [PATCH v2 15/21] qapi/parser: add docstrings, John Snow, 2021/05/11
- [PATCH v2 14/21] qapi/parser: allow 'ch' variable name, John Snow, 2021/05/11
- [PATCH v2 16/21] CHECKPOINT, John Snow, 2021/05/11
- [PATCH v2 19/21] qapi: [WIP] Import QAPIDoc from qapidoc Signed-off-by: John Snow <address@hidden>, John Snow, 2021/05/11
- [PATCH v2 18/21] qapi: [WIP] Add type ignores for qapidoc.py, John Snow, 2021/05/11
- [PATCH v2 21/21] qapi: [WIP] Enable linters on parser.py, John Snow, 2021/05/11
- [PATCH v2 20/21] qapi: [WIP] Add QAPIDocError,
John Snow <=
- [PATCH v2 17/21] qapi: [WIP] Rip QAPIDoc out of parser.py, John Snow, 2021/05/11