Skip to content

How to get meaningfull getMessage() of a parse result if parsing fails in optional parser? #80

@meier-rene

Description

@meier-rene

Hi, this is more a question than an issue. I can not find out how to get a nice failure message in case of an parse failure in an optional parser. Instead I get the message that the following non optional parser fails.
Example:
A string to parse:

line1 value
line2 !
line3 value

My parser definition:

def("start",
ref("line1")
.seq(ref("line2").optional)
.seq(ref("line3"))
.end()

def("line1", StringParser.of("line1 ") 
.seq(CharacterParser.word().plusLazy(Token.NEWLINE_PARSER)
 .seq(Token.NEWLINE_PARSER));

def("line2", StringParser.of("line2 ") 
.seq(CharacterParser.word().plusLazy(Token.NEWLINE_PARSER)
 .seq(Token.NEWLINE_PARSER));

def("line3", StringParser.of("line3 ") 
.seq(CharacterParser.word().plusLazy(Token.NEWLINE_PARSER)
 .seq(Token.NEWLINE_PARSER));

If I parse the string I get line3 expected at the beginning of line 2 from getMessages(). This is in principle correct, but it would be more usefull if I would get something like letter or digit expected at the end of the line 2.

Can someone give me an advice how to get better Messages in case of failure of an optional parser?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions