Skip to content

Commit

Permalink
Several fixes to error messages
Browse files Browse the repository at this point in the history
+ Fixed line report
+ Minor improvements
  • Loading branch information
Genarito committed Nov 25, 2021
1 parent 04a4a3e commit fa46958
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 19 deletions.
15 changes: 7 additions & 8 deletions gura/GuraParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down Expand Up @@ -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(':')
Expand All @@ -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')
Expand All @@ -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'
)
Expand Down
24 changes: 15 additions & 9 deletions gura/Parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion gura/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
author='JWare',
author_email='[email protected]',
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'
Expand Down

0 comments on commit fa46958

Please sign in to comment.