im using a cumsky/logos combo to parse a file format, and i have some reserved keywords encoded in special tokens, and a token for a general name, and i want to give one special error for "reserved keyword". just now it says "found [keyword] expected something else" which isn't very helpful. so i tried to parse the different keywords as well, and then emit a Rich::custom message, but since the parser is a sub-parser, the message becomes "found [keyword] expected [name]/line ending/comment" etc. i want to propagate the error to "top level", is there any way to do that? basically, i want the "found reserved keyword" to overwrite any other error, rather than "found ... expected x/y/z" overriding it
#make custom error override "default" chumsky error
3 messages · Page 1 of 1 (latest)
my parser looks like
fn item<'a, I>() -> impl Parser<'a, I, Item, extra::Err<Rich<'a, Token>>>
where
I: ValueInput<'a, Token = Token, Span = SimpleSpan>,
{
select! (
Token::Item(item) => item
)
.try_map(|item, span| Item::try_from(item).map_err(|e| Rich::custom(span, e)))
.labelled("item")
}
(found no better way to select one token. note that Item::try_from is infallible in this context since it only fails on reserved keywords)
I changed my parser to
fn item<'a, I>() -> impl Parser<'a, I, Item, extra::Err<Rich<'a, Token>>>
where
I: ValueInput<'a, Token = Token, Span = SimpleSpan>,
{
enum R {
Reserved,
Item(Arc<str>)
}
select! (
Token::Accepting => R::Reserved,
Token::Item(item) => R::Item(item)
)
.try_map(|item, span|
match item {
R::Reserved => {
Err(Rich::custom(span, "Reserved keyword"))
},
R::Item(item) => Ok(Item::try_from(item).unwrap())
}
)
.labelled("item")
}
which compiles and all, but the "reserved keyword" message gets overwritten when combining item() with other parsers (to something like "expected item or newline or comment)