Parser: Make use of `happy`'s `%error` declaration to get suggestions for valid tokens at error position
https://haskell-happy.readthedocs.io/en/latest/syntax.html#error-declaration
It appears that happy
has some limited support for suggesting valid tokens at an error position.
For that we'd simply say
%error GHC.Parser.Lexer.srcParseFail
%errorhandlertype explist
and where currently in GHC.Parser.Lexer we have:
-- Report a parse failure, giving the span of the previous token as
-- the location of the error. This is the entry point for errors
-- detected during parsing.
srcParseFail :: P a
srcParseFail = P $ \s@PState{ buffer = buf, options = o, last_len = len,
last_loc = last_loc } ->
unP (addFatalError $ srcParseErr o buf len (mkSrcSpanPs last_loc)) s
we'd refactor to
-- Report a parse failure, giving the span of the previous token as
-- the location of the error. This is the entry point for errors
-- detected during parsing.
srcParseFail :: [String] -> Located Token -> P a
srcParseFail expected last_loc =
P $ \s@PState{ buffer = buf, options = o, last_len = len } ->
unP (addFatalError $ srcParseErr o buf len (mkSrcSpanPs last_loc)) s
and perhaps consider expected
when generating the srcParseErr
. (I find it a bit strange to generate a [String]
rather than [Token]
here, but that's the happy
we have to live with.)