Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

off-by-1 errors and misleading error messages #79

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions ometa/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,22 @@ def formatError(self):
Return a pretty string containing error info about string
parsing failure.
"""
#de-twineifying
# calculate line+row from linear index
lines = str(self.input).split('\n')
counter = 0
lineNo = 1
columnNo = 0
remaining = self.position
lineNo, columnNo = 0,0 # zero-based indices
for line in lines:
newCounter = counter + len(line)
if newCounter > self.position:
columnNo = self.position - counter
if remaining <= len(line): # can point at newline/EOF
columnNo = remaining
break
else:
counter += len(line) + 1
lineNo += 1
remaining -= len(line) + 1 # newline
lineNo += 1
assert lineNo < len(lines)

reason = self.formatReason()
return ('\n' + line + '\n' + (' ' * columnNo + '^') +
"\nParse error at line %s, column %s: %s. trail: [%s]\n"
% (lineNo, columnNo, reason, ' '.join(self.trail)))
% (lineNo+1, columnNo+1, reason, ' '.join(self.trail)))


def __str__(self):
Expand Down Expand Up @@ -128,7 +127,7 @@ def eof():
"""
Return an indication that the end of the input was reached.
"""
return [("message", "end of input")]
return [("message", "unexpected EOF")]


def joinErrors(errors):
Expand Down Expand Up @@ -234,7 +233,7 @@ def head(self):
data = self.data.__class__('').join(self.data)
else:
data = self.data
raise EOFError(data, self.position + 1)
raise EOFError(data, self.position)
return self.data[self.position], self.error

def nullError(self, msg=None):
Expand Down
4 changes: 2 additions & 2 deletions parsley.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ def invokeRule(*args, **kwargs):
return ret
else:
# problem is that input remains, so:
err = ParseError(err.input, err.position + 1,
[["message", "expected EOF"]], err.trail)
err = ParseError(
err.input, err.position, [["message", "can't parse remainder"]], err.trail)
raise err
return invokeRule

Expand Down