[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 19/26] qapi/parser.py: add parent argument
From: |
John Snow |
Subject: |
[PATCH 19/26] qapi/parser.py: add parent argument |
Date: |
Tue, 22 Sep 2020 18:35:18 -0400 |
Instead of passing previously_included and info separately, we can pass
the parent parser itself. This cuts down on the number of parameters to
pass when creating a parser; and makes it easier to add new shared data
members between parent and child.
Signed-off-by: John Snow <jsnow@redhat.com>
---
scripts/qapi/parser.py | 25 +++++++++++--------------
1 file changed, 11 insertions(+), 14 deletions(-)
diff --git a/scripts/qapi/parser.py b/scripts/qapi/parser.py
index 77067b2f5d..fa0ddad922 100644
--- a/scripts/qapi/parser.py
+++ b/scripts/qapi/parser.py
@@ -68,26 +68,23 @@ class QAPISchemaParser:
"""
Performs parsing of a QAPI schema source file.
- :param fname: Path to the source file
- :param previously_included: Set of absolute paths of previously included
- source files; these will not be parsed again.
- :param incl_info: QAPISourceInfo for the parent document;
- Can be None for the parent document.
+ :param fname: Path to the source file
+ :param parent: Parent parser, if this is an included file.
"""
- def __init__(self,
- fname: str,
- previously_included: Optional[Set[str]] = None,
- incl_info: Optional[QAPISourceInfo] = None):
+ def __init__(self, fname: str,
+ parent: Optional['QAPISchemaParser'] = None):
self._fname = fname
- self._included = previously_included or set()
+ self._included: Set[str] = parent._included if parent else set()
self._included.add(os.path.abspath(self._fname))
+ parent_info = parent.info if parent else None
# Lexer state (see `accept` for details):
self.tok: Optional[str] = None
self.pos = 0
self.cursor = 0
self.val: Optional[Union[bool, str]] = None
- self.info = QAPISourceInfo(self._fname, parent=incl_info)
+ self.info: QAPISourceInfo = QAPISourceInfo(self._fname,
+ parent=parent_info)
self.line_pos = 0
# Parser output:
@@ -100,11 +97,11 @@ def __init__(self,
self.src = fp.read()
except IOError as e:
msg = "can't read {kind:s} file '{fname:s}': {errmsg:s}".format(
- kind='include' if incl_info else 'schema',
+ kind='include' if parent else 'schema',
fname=self._fname,
errmsg=e.strerror
)
- context = incl_info or self.info
+ context = parent_info if parent_info else self.info
raise QAPIParseError(context, msg) from e
self._parse()
@@ -193,7 +190,7 @@ def _include(self, include: str,
if incl_abs_fname in self._included:
return None
- return QAPISchemaParser(incl_fname, self._included, self.info)
+ return QAPISchemaParser(incl_fname, self)
def accept(self, skip_comment: bool = True) -> None:
"""
--
2.26.2
- [PATCH 11/26] qapi/parser.py: Convert several methods to @classmethod, (continued)
- [PATCH 11/26] qapi/parser.py: Convert several methods to @classmethod, John Snow, 2020/09/22
- [PATCH 12/26] qapi/parser.py: add casts to pragma checks, John Snow, 2020/09/22
- [PATCH 13/26] qapi/parser.py: add type hint annotations, John Snow, 2020/09/22
- [PATCH 14/26] qapi/parser.py: add docstrings, John Snow, 2020/09/22
- [PATCH 15/26] qapi/parser.py: add ParsedExpression type, John Snow, 2020/09/22
- [PATCH 16/26] qapi/pragma.py: Move QAPISchemaPragma into its own module, John Snow, 2020/09/22
- [PATCH 18/26] qapi/parser.py: Modify _include() to use parser state, John Snow, 2020/09/22
- [PATCH 17/26] qapi/pragma.py: Move pragma parsing out of parser.py, John Snow, 2020/09/22
- [PATCH 20/26] qapi/parser.py: remove unused check_args_section arguments, John Snow, 2020/09/22
- [PATCH 21/26] qapi/parser.py: QAPIDoc: convert @staticmethod to @classmethod, John Snow, 2020/09/22
- [PATCH 19/26] qapi/parser.py: add parent argument,
John Snow <=
- [PATCH 23/26] qapi/parser.py: enable mypy checks, John Snow, 2020/09/22
- [PATCH 24/26] qapi/parser.py: remove one and two-letter variables, John Snow, 2020/09/22
- [PATCH 25/26] qapi/parser.py: Silence too-few-public-methods warning, John Snow, 2020/09/22
- [PATCH 26/26] qapi/parser.py: enable pylint checks, John Snow, 2020/09/22
- [PATCH 22/26] qapi/parser.py: add type hint annotations (QAPIDoc), John Snow, 2020/09/22
- Re: [PATCH 00/26] qapi: static typing conversion, pt5, John Snow, 2020/09/22