Commit 95b06fd4 authored by di68kap's avatar di68kap
Browse files

- parser.py: bug fixes and slight refactorings

parent 17d536bc
...@@ -137,10 +137,10 @@ class HistoryRecord: ...@@ -137,10 +137,10 @@ class HistoryRecord:
ERROR = "ERROR" ERROR = "ERROR"
FAIL = "FAIL" FAIL = "FAIL"
def __init__(self, call_stack, node, remaining): def __init__(self, call_stack: List['Parser'], node: Node, remaining: int) -> None:
self.call_stack = call_stack self.call_stack = call_stack # type: List['Parser']
self.node = node self.node = node # type: Node
self.remaining = remaining self.remaining = remaining # type: int
def err_msg(self) -> str: def err_msg(self) -> str:
return self.ERROR + ": " + "; ".join(self.node._errors).replace('\n', '\\') return self.ERROR + ": " + "; ".join(self.node._errors).replace('\n', '\\')
...@@ -155,9 +155,9 @@ class HistoryRecord: ...@@ -155,9 +155,9 @@ class HistoryRecord:
self.err_msg() if self.node._errors else self.MATCH self.err_msg() if self.node._errors else self.MATCH
@property @property
def extent(self) -> Tuple[int, int]: def extent(self) -> slice:
return ((-self.remaining - self.node.len, -self.remaining) if self.node return (slice(-self.remaining - self.node.len, -self.remaining) if self.node
else (-self.remaining, None)) else slice(-self.remaining, None))
def add_parser_guard(parser_func): def add_parser_guard(parser_func):
...@@ -471,7 +471,7 @@ class Grammar: ...@@ -471,7 +471,7 @@ class Grammar:
document. document.
""" """
def prepare_line(record): def prepare_line(record):
excerpt = self.document__.__getitem__(slice(*record.extent))[:25].replace('\n', '\\n') excerpt = self.document__.__getitem__(record.extent)[:25].replace('\n', '\\n')
excerpt = "'%s'" % excerpt if len(excerpt) < 25 else "'%s...'" % excerpt excerpt = "'%s'" % excerpt if len(excerpt) < 25 else "'%s...'" % excerpt
return record.stack, record.status, excerpt return record.stack, record.status, excerpt
...@@ -573,19 +573,19 @@ class ScannerToken(Parser): ...@@ -573,19 +573,19 @@ class ScannerToken(Parser):
if end < 0: if end < 0:
node = Node(self, '').add_error( node = Node(self, '').add_error(
'END_SCANNER_TOKEN delimiter missing from scanner token. ' 'END_SCANNER_TOKEN delimiter missing from scanner token. '
'(Most likely due to a scanner bug!)') '(Most likely due to a scanner bug!)') # type: Node
return node, text[1:] return node, text[1:]
elif end == 0: elif end == 0:
node = Node(self, '').add_error( node = Node(self, '').add_error(
'Scanner token cannot have zero length. ' 'Scanner token cannot have zero length. '
'(Most likely due to a scanner bug!)') '(Most likely due to a scanner bug!)') # type: Node
return node, text[2:] return node, text[2:]
elif text.find(BEGIN_SCANNER_TOKEN, 1, end) >= 0: elif text.find(BEGIN_SCANNER_TOKEN, 1, end) >= 0:
node = Node(self, text[len(self.name) + 1:end]) node = Node(self, text[len(self.name) + 1:end])
node.add_error( node.add_error(
'Scanner tokens must not be nested or contain ' 'Scanner tokens must not be nested or contain '
'BEGIN_SCANNER_TOKEN delimiter as part of their argument. ' 'BEGIN_SCANNER_TOKEN delimiter as part of their argument. '
'(Most likely due to a scanner bug!)') '(Most likely due to a scanner bug!)') # type: Node
return node, text[end:] return node, text[end:]
if text[1:len(self.name) + 1] == self.name: if text[1:len(self.name) + 1] == self.name:
return Node(self, text[len(self.name) + 1:end]), \ return Node(self, text[len(self.name) + 1:end]), \
...@@ -675,7 +675,7 @@ class RE(Parser): ...@@ -675,7 +675,7 @@ class RE(Parser):
def __call__(self, text: str) -> Tuple[Node, str]: def __call__(self, text: str) -> Tuple[Node, str]:
# assert self.main.regexp.pattern != "@" # assert self.main.regexp.pattern != "@"
t = text t = text # type: str
wL, t = self.wspLeft(t) wL, t = self.wspLeft(t)
main, t = self.main(t) main, t = self.main(t)
if main: if main:
...@@ -1257,8 +1257,8 @@ compiler (function): A compiler function or compiler class ...@@ -1257,8 +1257,8 @@ compiler (function): A compiler function or compiler class
assert syntax_tree.error_flag or str(syntax_tree) == source_text, str(syntax_tree) assert syntax_tree.error_flag or str(syntax_tree) == source_text, str(syntax_tree)
# only compile if there were no syntax errors, for otherwise it is # only compile if there were no syntax errors, for otherwise it is
# likely that error list gets littered with compile error messages # likely that error list gets littered with compile error messages
result = None
if syntax_tree.error_flag: if syntax_tree.error_flag:
result = None
errors = syntax_tree.collect_errors() errors = syntax_tree.collect_errors()
else: else:
transformer(syntax_tree) transformer(syntax_tree)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment