Marwes / combine

A parser combinator library for Rust
https://docs.rs/combine/*/combine/
MIT License
1.29k stars 93 forks source link

Throw stream errors #323

Open ark0f opened 3 years ago

ark0f commented 3 years ago

I have stream of tokens. Token is enum like:

enum Token {
    Error,
    Num,
    Str,
    ...
}

StreamOnce::uncons documentation: Returns Err if no element could be retrieved. How I can throw error when I have Token::Error?

Marwes commented 3 years ago

You would return an error if the stream can no longer be used to retrieve more tokens. If you have a specific error token the I would assume that the stream may still be used, just that one token was an error so you would return Ok(Token::Error) to allow the parsing to continue (if the parser accepts, and recovers from the error token that is)

ark0f commented 3 years ago

I would assume that the stream may still be used

Not always. Unfinished string can break all following lexing

if the parser accepts, and recovers from the error token that is

That the only solution I found. I created my own token parser, but is it idiomatic way to handle such errors in combine?

Marwes commented 3 years ago

That the only solution I found. I created my own token parser, but is it idiomatic way to handle such errors in combine?

Yes, I think so at least. What would be the alternative?

ark0f commented 3 years ago

What would be the alternative?

Throw error from stream (maybe). It gives more guarentees that invalid input won't be skipped.

Otherwise I have to be sure that every parsing action is based on my own token parser

Marwes commented 3 years ago

I suppose you could instead use a parser which inspects the error being returned and recovers if you deem it to be a recoverable error