-
If I have a parser like so: Parser<String> id() => ((letter() | char('_')) &
(letter() | digit() | anyOf('_- ()')).star().flatten())
.map((vals) => vals.listOf<String>().join()); and I feed it input like 'foo.1', I'll get an error like this:
but the error I want to supply is:
How can I get better error messages in my parsers? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
The problem in your example is that 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 Note: I also moved the |
Beta Was this translation helpful? Give feedback.
The problem in your example is that
foo
is a perfectly valid identifier as part of your specification and theid
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.
For the input
foo.1
this givesend of id expected at 1:4
.Note: I also moved the
flatten
operator around everything, then you don't need the custom flattening with themap
operator.