From fa46958264b3447f1e2b4b4f0eca1964d84ebc16 Mon Sep 17 00:00:00 2001 From: Genarito Date: Wed, 24 Nov 2021 22:49:34 -0300 Subject: [PATCH] Several fixes to error messages + Fixed line report + Minor improvements --- gura/GuraParser.py | 15 +++++++-------- gura/Parser.py | 24 +++++++++++++++--------- gura/__init__.py | 2 +- setup.py | 2 +- 4 files changed, 24 insertions(+), 19 deletions(-) diff --git a/gura/GuraParser.py b/gura/GuraParser.py index aca8ca3..b047373 100644 --- a/gura/GuraParser.py +++ b/gura/GuraParser.py @@ -125,9 +125,8 @@ def __restart_params(self, text: str): def new_line(self): """Matches with a new line""" - res = self.char('\f\v\r\n') - if res is not None: - self.line += 1 + self.char('\f\v\r\n') + self.line += 1 # If this line is reached then new line matched def comment(self) -> MatchResult: """ @@ -495,11 +494,12 @@ def key(self) -> str: key = self.match('unquoted_string') if type(key) is not str: + error_pos = self.pos + 1 raise ParseError( - self.pos + 1, + error_pos, self.line, 'Expected string for key but got "%s"', - self.text[self.pos + 1] + self.text[error_pos] ) self.keyword(':') @@ -511,8 +511,7 @@ def pair(self) -> Optional[MatchResult]: :return: Matched key-value pair. None if the indentation level is lower than the last one (to indicate the ending of a parent object) """ - pos_before_pair = self.pos - initial_pos = self.pos # To report correct position in case of exception + pos_before_pair = self.pos # To report correct position in case of exception current_indentation_level = self.maybe_match('ws_with_indentation') key = self.match('key') @@ -524,7 +523,7 @@ def pair(self) -> Optional[MatchResult]: # Check if indentation is divisible by 4 if current_indentation_level % 4 != 0: raise InvalidIndentationError( - initial_pos, + pos_before_pair, self.line, f'Indentation block ({current_indentation_level}) must be divisible by 4' ) diff --git a/gura/Parser.py b/gura/Parser.py index d408c7a..61c39a0 100644 --- a/gura/Parser.py +++ b/gura/Parser.py @@ -10,7 +10,7 @@ def __init__(self, pos: int, line: int, msg: str, *args): self.args = args def __str__(self): - return '%s at line %s position %s' % (self.msg % self.args, self.line, self.pos) + return '%s at line %s (text position = %s)' % (self.msg % self.args, self.line, self.pos) class ParseError(GuraError): @@ -34,11 +34,12 @@ def assert_end(self): :raise: ParseError if EOL has not been reached """ if self.pos < self.len: + error_pos = self.pos + 1 raise ParseError( - self.pos + 1, + error_pos, self.line, 'Expected end of string but got "%s"', - self.text[self.pos + 1] + self.text[error_pos] ) def split_char_ranges(self, chars: str): @@ -82,10 +83,11 @@ def char(self, chars: Optional[str] = None) -> str: self.pos + 1, self.line, 'Expected %s but got end of string', - 'character' if chars is None else '[%s]' % chars + 'next character' if chars is None else '[%s]' % chars ) - next_char = self.text[self.pos + 1] + next_char_pos = self.pos + 1 + next_char = self.text[next_char_pos] if chars is None: self.pos += 1 return next_char @@ -100,9 +102,9 @@ def char(self, chars: Optional[str] = None) -> str: return next_char raise ParseError( - self.pos + 1, + next_char_pos, self.line, - 'Expected [%s] but got "%s"' % (chars, next_char) + 'Expected chars [%s] but got "%s"' % (chars, next_char) ) def keyword(self, *keywords: str): @@ -128,12 +130,13 @@ def keyword(self, *keywords: str): self.pos += len(keyword) return keyword + error_pos = self.pos + 1 raise ParseError( - self.pos + 1, + error_pos, self.line, 'Expected "%s" but got "%s"', ', '.join(keywords), - self.text[self.pos], + self.text[error_pos], ) def match(self, *rules: str): @@ -150,11 +153,14 @@ def match(self, *rules: str): for rule in rules: initial_pos = self.pos + initial_line = self.line + try: result = getattr(self, rule)() return result except ParseError as e: self.pos = initial_pos + self.line = initial_line if e.pos > last_error_pos: last_exception = e diff --git a/gura/__init__.py b/gura/__init__.py index 7380fa2..37973ef 100644 --- a/gura/__init__.py +++ b/gura/__init__.py @@ -2,7 +2,7 @@ VariableNotDefinedError, DuplicatedImportError, loads, dumps from gura.Parser import ParseError, GuraError -__version__ = "1.4.3" +__version__ = "1.4.4" loads = loads dumps = dumps diff --git a/setup.py b/setup.py index c0405d7..3ea8a1e 100644 --- a/setup.py +++ b/setup.py @@ -15,7 +15,7 @@ author='JWare', author_email='jware.organization@gmail.com', url='https://github.com/gura-conf/gura-python-parser', - download_url='https://github.com/gura-conf/gura-python-parser/archive/refs/tags/1.4.3.tar.gz', + download_url='https://github.com/gura-conf/gura-python-parser/archive/refs/tags/1.4.4.tar.gz', keywords=['Gura', 'parser', 'loads', 'dumps', 'encode', 'decode'], install_requires=[ 'wheel'