Skip to content

Improve error messages for fallible conversion #25

@drhagen

Description

@drhagen

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions