combine icon indicating copy to clipboard operation
combine copied to clipboard

Throw stream errors

Open ark0f opened this issue 4 years ago • 5 comments

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?

ark0f avatar Aug 12 '21 15:08 ark0f

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)

Marwes avatar Aug 14 '21 07:08 Marwes

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?

ark0f avatar Aug 14 '21 09:08 ark0f

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?

Marwes avatar Aug 23 '21 12:08 Marwes

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

ark0f avatar Aug 23 '21 12:08 ark0f

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

Marwes avatar Aug 23 '21 12:08 Marwes