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

Improve error messages for fallible conversion #25

Open
drhagen opened this issue Sep 23, 2021 · 0 comments
Open

Improve error messages for fallible conversion #25

drhagen opened this issue Sep 23, 2021 · 0 comments

Comments

@drhagen
Copy link
Owner

drhagen commented Sep 23, 2021

Currently, error messages always display the farthest that parsing got and the next token as the actual value that stopped parsing. This worked fine until the transformation parser, which can succeed in parsing input and then fail to accept that parsed value. The message the parsing failed on the token after the successful parse is not particularly correct. The error is the parsed token itself, which can be seen in this example:

from dataclasses import dataclass

from parsita import *

@dataclass
class Percent:
    number: int

def to_percent(number: int):
    if not 0 <= number <= 100:
        return failure("a number between 0 and 100")
    else:
        return success(Percent(number))

class TestParsers(TextParsers):
    percent = (reg(r"[0-9]+") > int) >= to_percent

TestParsers.percent.parse("150").or_die()

This displays the error Expected a number between 0 and 100 but found end of source, when Expected a number between 0 and 100 but found 150 would be more sensible.

To fix this would require that the actual value and farthest point be configurable separately by the parser.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant