You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm building a parser that accepts custom token stream.
I've made TokenStream (from lexer-applicative) an instance of Stream
instanceStream (TokenStream (Ltok)) where
And that's wonderful, everything worked as expected, until a "lexcial error" appear in my token stream
--| A stream of tokensdataTokenStreamtok=TsTokentok (TokenStreamtok)
| TsEof
| TsErrorLexicalError
The parser complained about unexpected end of input, that's because I had no choice but had to treat TsError like TsEof.
I think there are 3 ways of solving this:
Make Stream "aware" of these lexical errors: for example, let take1_ return a Either value instead of just a Maybe value.
Make the parser incremental: so that users can check if the next token is TsError, before feeding it to the parser.
The "happy" way, something between 1. and 2.
I'll explain more about how it can be done in happy:
Happy also allows user to choose their own type token stream (usually with alex). As long as we tell happy what is the token for eof:
%lexer { <lexer> } { <eof> }
and what to do when a token comes in:
lexer:: (Token->Pa) ->Pa
For example, this is how to deal with a token stream from lexer-applicative:
lexer:: (Token->Pa) ->Pa
lexer f = scanNext >>= f
scanNext::PToken
scanNext =do
stream <- gets tokenStream
case stream ofTsToken (L _ tok) stream ->return tok
TsEof->returnTokenEOFTsError (LexicalError pos) -> throwError $Lexical pos
I think this is the best among the 3 solutions, because it allows users to handle lexical errors the way they like, and it's not an overkill like making megaparsec incremental.
But I'm still not sure about how to incorporate this into the Stream class, if we are going to do this.
The text was updated successfully, but these errors were encountered:
I don't know if you still need this, but another workaround is to have type Token s = Either String tok then throw a parser error whenever you get a Left. It'll unfortunately mean you'll end up with expected tokens that are always Right _, so you could use a label for that instead.
I'm building a parser that accepts custom token stream.
I've made TokenStream (from
lexer-applicative
) an instance ofStream
And that's wonderful, everything worked as expected, until a "lexcial error" appear in my token stream
The parser complained about
unexpected end of input
, that's because I had no choice but had to treatTsError
likeTsEof
.I think there are 3 ways of solving this:
Stream
"aware" of these lexical errors: for example, lettake1_
return aEither
value instead of just aMaybe
value.TsError
, before feeding it to the parser.I'll explain more about how it can be done in
happy
:Happy also allows user to choose their own type token stream (usually with
alex
). As long as we tellhappy
what is the token foreof
:and what to do when a token comes in:
For example, this is how to deal with a token stream from
lexer-applicative
:I think this is the best among the 3 solutions, because it allows users to handle lexical errors the way they like, and it's not an overkill like making
megaparsec
incremental.But I'm still not sure about how to incorporate this into the
Stream
class, if we are going to do this.The text was updated successfully, but these errors were encountered: