Skip to content

How to provide better error messages? #125

Answered by renggli
csells asked this question in Q&A
Discussion options

You must be logged in to vote

The problem in your example is that foo is a perfectly valid identifier as part of your specification and the id parser succeeds. The error message "\n" expected at 1:4 is produced somewhere else in your grammar.

A common way to fix this is to use a negative lookahead. With it you can assert that your ID is not followed by a dot, i.e.

Parser<String> id() => (((letter() | char('_')) &
            (letter() | digit() | anyOf('_- ()')).star() &
            char('.').not('end of id expected')))
        .flatten();

For the input foo.1 this gives end of id expected at 1:4.

Note: I also moved the flatten operator around everything, then you don't need the custom flattening with the map operator.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by renggli
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #121 on December 29, 2021 09:06.