[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 04/22] qapi/parser: factor parsing routine into method
From: |
John Snow |
Subject: |
[PATCH 04/22] qapi/parser: factor parsing routine into method |
Date: |
Wed, 21 Apr 2021 23:07:02 -0400 |
For the sake of keeping __init__ smaller (and treating it more like a
gallery of what state variables we can expect to see), put the actual
parsing action into a parse method. We can still invoke it from the init
method to reduce churn.
To accomplish this, 'previously_included' because the private data
member '_included', and the filename is stashed as _fname.
Add any missing declarations to the init method, and group them by
function so they can be understood quickly at a glance.
Signed-off-by: John Snow <jsnow@redhat.com>
---
scripts/qapi/parser.py | 39 +++++++++++++++++++++++++++------------
1 file changed, 27 insertions(+), 12 deletions(-)
diff --git a/scripts/qapi/parser.py b/scripts/qapi/parser.py
index edd0af33ae0..f519518075e 100644
--- a/scripts/qapi/parser.py
+++ b/scripts/qapi/parser.py
@@ -37,23 +37,38 @@ def __init__(self, parser, msg):
class QAPISchemaParser:
def __init__(self, fname, previously_included=None, incl_info=None):
- previously_included = previously_included or set()
- previously_included.add(os.path.abspath(fname))
+ self._fname = fname
+ self._included = previously_included or set()
+ self._included.add(os.path.abspath(self._fname))
+ self.src = ''
- # Allow the caller to catch this error.
- with open(fname, 'r', encoding='utf-8') as fp:
- self.src = fp.read()
-
- if self.src == '' or self.src[-1] != '\n':
- self.src += '\n'
+ # Lexer state (see `accept` for details):
+ self.info = QAPISourceInfo(self._fname, incl_info)
+ self.tok = None
+ self.pos = 0
self.cursor = 0
- self.info = QAPISourceInfo(fname, incl_info)
+ self.val = None
self.line_pos = 0
+
+ # Parser output:
self.exprs = []
self.docs = []
- self.accept()
+
+ # Showtime!
+ self._parse()
+
+ def _parse(self):
cur_doc = None
+ with open(self._fname, 'r', encoding='utf-8') as fp:
+ self.src = fp.read()
+ if self.src == '' or self.src[-1] != '\n':
+ self.src += '\n'
+
+ # Prime the lexer:
+ self.accept()
+
+ # Parse until done:
while self.tok is not None:
info = self.info
if self.tok == '#':
@@ -71,12 +86,12 @@ def __init__(self, fname, previously_included=None,
incl_info=None):
if not isinstance(include, str):
raise QAPISemError(info,
"value of 'include' must be a string")
- incl_fname = os.path.join(os.path.dirname(fname),
+ incl_fname = os.path.join(os.path.dirname(self._fname),
include)
self.exprs.append({'expr': {'include': incl_fname},
'info': info})
exprs_include = self._include(include, info, incl_fname,
- previously_included)
+ self._included)
if exprs_include:
self.exprs.extend(exprs_include.exprs)
self.docs.extend(exprs_include.docs)
--
2.30.2
- Re: [PATCH 03/22] qapi/source: Remove line number from QAPISourceInfo initializer, (continued)
[PATCH 01/22] qapi/parser: Don't try to handle file errors, John Snow, 2021/04/21
[PATCH 04/22] qapi/parser: factor parsing routine into method,
John Snow <=
[PATCH 08/22] qapi/parser: Use @staticmethod where appropriate, John Snow, 2021/04/21
[PATCH 06/22] qapi/parser: assert get_expr returns object in outer loop, John Snow, 2021/04/21
[PATCH 09/22] qapi: add match_nofail helper, John Snow, 2021/04/21
[PATCH 05/22] qapi/parser: Assert lexer value is a string, John Snow, 2021/04/21