Commit aadc7396 authored by Igor Dejanovic's avatar Igor Dejanovic

Added information about file name to facilitate better error reporting.

parent 3791d3ba
...@@ -63,7 +63,7 @@ class NoMatch(Exception): ...@@ -63,7 +63,7 @@ class NoMatch(Exception):
occurred. occurred.
parser (Parser): An instance of a parser. parser (Parser): An instance of a parser.
exp_str(str): What is expected? If not given it is deduced from exp_str(str): What is expected? If not given it is deduced from
the rule. the rule. Used in error messages.
soft(bool): Used to indicate soft no match exception. soft(bool): Used to indicate soft no match exception.
""" """
def __init__(self, rule, position, parser, exp_str=None, soft=False): def __init__(self, rule, position, parser, exp_str=None, soft=False):
...@@ -89,10 +89,17 @@ class NoMatch(Exception): ...@@ -89,10 +89,17 @@ class NoMatch(Exception):
self._up = True self._up = True
def __str__(self): def __str__(self):
return "Expected '{}' at position {} => '{}'."\ if self.parser.file_name:
.format(self.exp_str, return "Expected '{}' at {}{} => '{}'."\
text(self.parser.pos_to_linecol(self.position)), .format(self.exp_str,
self.parser.context(position=self.position)) self.parser.file_name,
text(self.parser.pos_to_linecol(self.position)),
self.parser.context(position=self.position))
else:
return "Expected '{}' at position {} => '{}'."\
.format(self.exp_str,
text(self.parser.pos_to_linecol(self.position)),
self.parser.context(position=self.position))
def __unicode__(self): def __unicode__(self):
return self.__str__() return self.__str__()
...@@ -1188,11 +1195,20 @@ class Parser(object): ...@@ -1188,11 +1195,20 @@ class Parser(object):
else: else:
self._ws = self._real_ws self._ws = self._real_ws
def parse(self, _input): def parse(self, _input, file_name=None):
"""
Parses input and produces parse tree.
Args:
_input(str): An input string to parse.
file_name(str): If input is loaded from file this can be
set to file name. It is used in error messages.
"""
self.position = 0 # Input position self.position = 0 # Input position
self.nm = None # Last NoMatch exception self.nm = None # Last NoMatch exception
self.line_ends = [] self.line_ends = []
self.input = _input self.input = _input
self.file_name = file_name
self.parser_model.clear_cache() self.parser_model.clear_cache()
if self.comments_model: if self.comments_model:
self.comments_model.clear_cache() self.comments_model.clear_cache()
...@@ -1216,7 +1232,7 @@ class Parser(object): ...@@ -1216,7 +1232,7 @@ class Parser(object):
with codecs.open(file_name, 'r', 'utf-8') as f: with codecs.open(file_name, 'r', 'utf-8') as f:
content = f.read() content = f.read()
return self.parse(content) return self.parse(content, file_name=file_name)
def getASG(self, sem_actions=None, defaults=True): def getASG(self, sem_actions=None, defaults=True):
""" """
......
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