[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 07/13] qapi/parser: Introduce NullSection
From: |
Markus Armbruster |
Subject: |
[PULL 07/13] qapi/parser: Introduce NullSection |
Date: |
Sat, 2 Oct 2021 11:56:49 +0200 |
From: John Snow <jsnow@redhat.com>
Here's the weird bit. QAPIDoc generally expects -- virtually everywhere
-- that it will always have a current section. The sole exception to
this is in the case that end_comment() is called, which leaves us with
*no* section. However, in this case, we also don't expect to actually
ever mutate the comment contents ever again.
NullSection is just a Null-object that allows us to maintain the
invariant that we *always* have a current section, enforced by static
typing -- allowing us to type that field as QAPIDoc.Section instead of
the more ambiguous Optional[QAPIDoc.Section].
end_section is renamed to switch_section and now accepts as an argument
the new section to activate, clarifying that no callers ever just
unilaterally end a section; they only do so when starting a new section.
Signed-off-by: John Snow <jsnow@redhat.com>
Message-Id: <20210930205716.1148693-8-jsnow@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
---
scripts/qapi/parser.py | 27 ++++++++++++++++-----------
1 file changed, 16 insertions(+), 11 deletions(-)
diff --git a/scripts/qapi/parser.py b/scripts/qapi/parser.py
index 82f1d952b1..40c5da4b17 100644
--- a/scripts/qapi/parser.py
+++ b/scripts/qapi/parser.py
@@ -478,6 +478,13 @@ def __init__(self, parser, name, indent=0):
def connect(self, member):
self.member = member
+ class NullSection(Section):
+ """
+ Immutable dummy section for use at the end of a doc block.
+ """
+ def append(self, line):
+ assert False, "Text appended after end_comment() called."
+
def __init__(self, parser, info):
# self._parser is used to report errors with QAPIParseError. The
# resulting error position depends on the state of the parser.
@@ -525,7 +532,7 @@ def append(self, line):
self._append_line(line)
def end_comment(self):
- self._end_section()
+ self._switch_section(QAPIDoc.NullSection(self._parser))
@staticmethod
def _is_section_tag(name):
@@ -699,9 +706,9 @@ def _start_symbol_section(self, symbols_dict, name, indent):
raise QAPIParseError(self._parser,
"'%s' parameter name duplicated" % name)
assert not self.sections
- self._end_section()
- self._section = QAPIDoc.ArgSection(self._parser, name, indent)
- symbols_dict[name] = self._section
+ new_section = QAPIDoc.ArgSection(self._parser, name, indent)
+ self._switch_section(new_section)
+ symbols_dict[name] = new_section
def _start_args_section(self, name, indent):
self._start_symbol_section(self.args, name, indent)
@@ -713,13 +720,11 @@ 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)
- self._end_section()
- self._section = QAPIDoc.Section(self._parser, name, indent)
- self.sections.append(self._section)
-
- def _end_section(self):
- assert self._section is not None
+ new_section = QAPIDoc.Section(self._parser, name, indent)
+ self._switch_section(new_section)
+ self.sections.append(new_section)
+ def _switch_section(self, new_section):
text = self._section.text = self._section.text.strip()
# Only the 'body' section is allowed to have an empty body.
@@ -732,7 +737,7 @@ def _end_section(self):
self._parser,
"empty doc section '%s'" % self._section.name)
- self._section = None
+ self._section = new_section
def _append_freeform(self, line):
match = re.match(r'(@\S+:)', line)
--
2.31.1
- [PULL 00/13] QAPI patches patches for 2021-10-02, Markus Armbruster, 2021/10/02
- [PULL 02/13] qapi/gen: use dict.items() to iterate over _modules, Markus Armbruster, 2021/10/02
- [PULL 07/13] qapi/parser: Introduce NullSection,
Markus Armbruster <=
- [PULL 11/13] qapi/parser: enable mypy checks, Markus Armbruster, 2021/10/02
- [PULL 12/13] qapi/parser: Silence too-few-public-methods warning, Markus Armbruster, 2021/10/02
- [PULL 01/13] qapi/pylintrc: ignore 'consider-using-f-string' warning, Markus Armbruster, 2021/10/02
- [PULL 04/13] qapi: Add spaces after symbol declaration for consistency, Markus Armbruster, 2021/10/02
- [PULL 09/13] qapi/parser: add type hint annotations (QAPIDoc), Markus Armbruster, 2021/10/02
- [PULL 06/13] qapi/parser: clarify _end_section() logic, Markus Armbruster, 2021/10/02
- [PULL 13/13] qapi/parser: enable pylint checks, Markus Armbruster, 2021/10/02
- [PULL 05/13] qapi/parser: remove FIXME comment from _append_body_line, Markus Armbruster, 2021/10/02
- [PULL 03/13] qapi/parser: fix unused check_args_section arguments, Markus Armbruster, 2021/10/02
- [PULL 10/13] qapi/parser: Add FIXME for consolidating JSON-related types, Markus Armbruster, 2021/10/02