|
@@ -37,23 +37,39 @@ def __init__(self, parser, msg):
|
|
class QAPISchemaParser:
|
|
class QAPISchemaParser:
|
|
|
|
|
|
def __init__(self, fname, previously_included=None, incl_info=None):
|
|
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 = ''
|
|
|
|
+
|
|
|
|
+ # Lexer state (see `accept` for details):
|
|
|
|
+ self.info = QAPISourceInfo(self._fname, incl_info)
|
|
|
|
+ self.tok = None
|
|
|
|
+ self.pos = 0
|
|
|
|
+ self.cursor = 0
|
|
|
|
+ self.val = None
|
|
|
|
+ self.line_pos = 0
|
|
|
|
+
|
|
|
|
+ # Parser output:
|
|
|
|
+ self.exprs = []
|
|
|
|
+ self.docs = []
|
|
|
|
+
|
|
|
|
+ # Showtime!
|
|
|
|
+ self._parse()
|
|
|
|
+
|
|
|
|
+ def _parse(self):
|
|
|
|
+ cur_doc = None
|
|
|
|
|
|
# May raise OSError; allow the caller to handle it.
|
|
# May raise OSError; allow the caller to handle it.
|
|
- with open(fname, 'r', encoding='utf-8') as fp:
|
|
|
|
|
|
+ with open(self._fname, 'r', encoding='utf-8') as fp:
|
|
self.src = fp.read()
|
|
self.src = fp.read()
|
|
-
|
|
|
|
if self.src == '' or self.src[-1] != '\n':
|
|
if self.src == '' or self.src[-1] != '\n':
|
|
self.src += '\n'
|
|
self.src += '\n'
|
|
- self.cursor = 0
|
|
|
|
- self.info = QAPISourceInfo(fname, incl_info)
|
|
|
|
- self.line_pos = 0
|
|
|
|
- self.exprs = []
|
|
|
|
- self.docs = []
|
|
|
|
|
|
+
|
|
|
|
+ # Prime the lexer:
|
|
self.accept()
|
|
self.accept()
|
|
- cur_doc = None
|
|
|
|
|
|
|
|
|
|
+ # Parse until done:
|
|
while self.tok is not None:
|
|
while self.tok is not None:
|
|
info = self.info
|
|
info = self.info
|
|
if self.tok == '#':
|
|
if self.tok == '#':
|
|
@@ -71,12 +87,12 @@ def __init__(self, fname, previously_included=None, incl_info=None):
|
|
if not isinstance(include, str):
|
|
if not isinstance(include, str):
|
|
raise QAPISemError(info,
|
|
raise QAPISemError(info,
|
|
"value of 'include' must be a string")
|
|
"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)
|
|
include)
|
|
self.exprs.append({'expr': {'include': incl_fname},
|
|
self.exprs.append({'expr': {'include': incl_fname},
|
|
'info': info})
|
|
'info': info})
|
|
exprs_include = self._include(include, info, incl_fname,
|
|
exprs_include = self._include(include, info, incl_fname,
|
|
- previously_included)
|
|
|
|
|
|
+ self._included)
|
|
if exprs_include:
|
|
if exprs_include:
|
|
self.exprs.extend(exprs_include.exprs)
|
|
self.exprs.extend(exprs_include.exprs)
|
|
self.docs.extend(exprs_include.docs)
|
|
self.docs.extend(exprs_include.docs)
|