In January 2021 we will introduce a 10 GB quota for project repositories. Higher limits for individual projects will be available on request. Please see https://doku.lrz.de/display/PUBLIC/GitLab for more information.

Commit 0f447391 authored by di68kap's avatar di68kap

error reporting error fixed

parent 8d615f2c
...@@ -59,7 +59,8 @@ class Error: ...@@ -59,7 +59,8 @@ class Error:
# error levels # error levels
NO_ERROR = 0 NO_ERROR = 0
WARNING = 1 MESSAGE = 1
WARNING = 10
ERROR = 1000 ERROR = 1000
HIGHEST = ERROR HIGHEST = ERROR
......
...@@ -156,7 +156,7 @@ def add_parser_guard(parser_func): ...@@ -156,7 +156,7 @@ def add_parser_guard(parser_func):
if grammar.history_tracking__: if grammar.history_tracking__:
# don't track returning parsers except in case an error has occurred # don't track returning parsers except in case an error has occurred
# remaining = len(rest) # remaining = len(rest)
if grammar.moving_forward__ or (node and node.errors): if (grammar.moving_forward__ or (node and node.errors)):
record = HistoryRecord(grammar.call_stack__, node, text) record = HistoryRecord(grammar.call_stack__, node, text)
grammar.history__.append(record) grammar.history__.append(record)
# print(record.stack, record.status, rest[:20].replace('\n', '|')) # print(record.stack, record.status, rest[:20].replace('\n', '|'))
...@@ -1249,17 +1249,22 @@ class ZeroOrMore(Option): ...@@ -1249,17 +1249,22 @@ class ZeroOrMore(Option):
def __call__(self, text: StringView) -> Tuple[Optional[Node], StringView]: def __call__(self, text: StringView) -> Tuple[Optional[Node], StringView]:
results = () # type: Tuple[Node, ...] results = () # type: Tuple[Node, ...]
n = len(text) + 1 n = len(text) + 1 # type: int
infinite_loop_error = None # type: Optional[Error]
while text and len(text) < n: while text and len(text) < n:
n = len(text) n = len(text)
node, text = self.parser(text) node, text = self.parser(text)
if not node: if not node:
break break
if len(text) == n: if len(text) == n:
self.grammar.tree__.add_error(node, err_msg = dsl_error_msg(self, 'Infinite Loop encountered.')
dsl_error_msg(self, 'Infinite Loop encountered.')) node.errors.append(Error(err_msg, Error.MESSAGE, node=node))
infinite_loop_error = Error(err_msg, node=node)
results += (node,) results += (node,)
return Node(self, results), text node = Node(self, results)
if infinite_loop_error:
self.grammar.tree__.add_error(node, infinite_loop_error)
return node, text
def __repr__(self): def __repr__(self):
return '{' + (self.parser.repr[1:-1] if isinstance(self.parser, Alternative) return '{' + (self.parser.repr[1:-1] if isinstance(self.parser, Alternative)
...@@ -1294,19 +1299,24 @@ class OneOrMore(UnaryOperator): ...@@ -1294,19 +1299,24 @@ class OneOrMore(UnaryOperator):
def __call__(self, text: StringView) -> Tuple[Optional[Node], StringView]: def __call__(self, text: StringView) -> Tuple[Optional[Node], StringView]:
results = () # type: Tuple[Node, ...] results = () # type: Tuple[Node, ...]
text_ = text # type: StringView text_ = text # type: StringView
n = len(text) + 1 n = len(text) + 1 # type: int
infinite_loop_error = None # type: Optional[Error]
while text_ and len(text_) < n: while text_ and len(text_) < n:
n = len(text_) n = len(text_)
node, text_ = self.parser(text_) node, text_ = self.parser(text_)
if not node: if not node:
break break
if len(text_) == n: if len(text_) == n:
self.grammar.tree__.add_error(node, err_msg = dsl_error_msg(self, 'Infinite Loop encountered.')
dsl_error_msg(self, 'Infinite Loop encountered.')) node.errors.append(Error(err_msg, Error.MESSAGE, node=node))
infinite_loop_error = Error(err_msg, node=node)
results += (node,) results += (node,)
if results == (): if results == ():
return None, text return None, text
return Node(self, results), text_ node = Node(self, results)
if infinite_loop_error:
self.grammar.tree__.add_error(node, infinite_loop_error)
return node, text_
def __repr__(self): def __repr__(self):
return '{' + (self.parser.repr[1:-1] if isinstance(self.parser, Alternative) return '{' + (self.parser.repr[1:-1] if isinstance(self.parser, Alternative)
...@@ -1351,6 +1361,7 @@ class Series(NaryOperator): ...@@ -1351,6 +1361,7 @@ class Series(NaryOperator):
results = () # type: Tuple[Node, ...] results = () # type: Tuple[Node, ...]
text_ = text # type: StringView text_ = text # type: StringView
pos = 0 pos = 0
mandatory_violation = None
for parser in self.parsers: for parser in self.parsers:
node, text_ = parser(text_) node, text_ = parser(text_)
if not node: if not node:
...@@ -1360,19 +1371,23 @@ class Series(NaryOperator): ...@@ -1360,19 +1371,23 @@ class Series(NaryOperator):
# Provide useful error messages # Provide useful error messages
match = text.search(Series.RX_ARGUMENT) match = text.search(Series.RX_ARGUMENT)
i = max(1, text.index(match.regs[1][0])) if match else 1 i = max(1, text.index(match.regs[1][0])) if match else 1
node = Node(self, text_[:i]).init_pos(self.grammar.document_length__ location = self.grammar.document_length__ - len(text_)
- len(text_)) node = Node(self, text_[:i]).init_pos(location)
self.grammar.tree__.add_error( msg = '%s expected, "%s" found!' \
node, '%s expected; "%s" found!' % (parser.repr, text_[:10].replace('\n', '\\n '))
% (parser.repr, text_[:10].replace('\n', '\\n ')), node.errors.append(Error(msg, Error.MESSAGE, location))
code=Error.MANDATORY_CONTINUATION) if not mandatory_violation:
mandatory_violation = Error(msg, Error.ERROR, location)
text_ = text_[i:] text_ = text_[i:]
results += (node,) results += (node,)
# if node.error_flag: # break on first error # if node.error_flag: # break on first error
# break # break
pos += 1 pos += 1
assert len(results) <= len(self.parsers) assert len(results) <= len(self.parsers)
return Node(self, results), text_ node = Node(self, results)
if mandatory_violation:
self.grammar.tree__.add_error_obj(node, mandatory_violation)
return node, text_
def __repr__(self): def __repr__(self):
return " ".join([parser.repr for parser in self.parsers[:self.mandatory]] return " ".join([parser.repr for parser in self.parsers[:self.mandatory]]
......
...@@ -717,10 +717,17 @@ class RootNode(Node): ...@@ -717,10 +717,17 @@ class RootNode(Node):
self._content = node._content self._content = node._content
return self return self
def add_error_obj(self, node: Node, error: Error) -> 'RootNode':
self.all_errors.append(error)
self.error_flag = max(self.error_flag, error.code)
node.errors.append(error)
self.err_nodes_keep.append(node)
return self
def add_error(self, def add_error(self,
node: Node, node: Node,
message: str, message: str,
code: int = Error.ERROR) -> 'Node': code: int = Error.ERROR) -> 'RootNode':
""" """
Adds an error to this tree. Adds an error to this tree.
Parameters: Parameters:
...@@ -729,11 +736,8 @@ class RootNode(Node): ...@@ -729,11 +736,8 @@ class RootNode(Node):
code(int): An error code to identify the kind of error code(int): An error code to identify the kind of error
""" """
error = Error(message, code, node=node) error = Error(message, code, node=node)
self.all_errors.append(error) self.add_error_obj(error)
self.error_flag = max(self.error_flag, code)
node.errors.append(error)
self.err_nodes_keep.append(node)
return self
def collect_errors(self) -> List[Error]: def collect_errors(self) -> List[Error]:
"""Returns the list of errors, ordered bv their position. """Returns the list of errors, ordered bv their position.
......
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