Did you know, a POk
parse result from the GHC parser doesn't necessarily mean the parse was OK? This blog explains what's up with that. The source code below is from this example in the ghc-lib
repo.
Here is code that tries to make a parse tree of a Haskell module.
parse :: String -> DynFlags -> String -> ParseResult (Located (HsModule GhcPs))
parse filename flags str =
unP Parser.parseModule parseState
where
location = mkRealSrcLoc (mkFastString filename) 1 1
buffer = stringToStringBuffer str
parseState = mkPState flags buffer location
The way to call the above code is like this.
case parse file flags s of
PFailed s ->
report flags $ snd (getMessages s flags)
POk s m -> do
report flags $ fst (getMessages s flags)
analyzeModule flags m
In the PFailed s
case (where s
is the parse state), the expression snd (getMessages s flags)
retrieves the errors and we report them. In the POk
case, we report warnings and do whatever it is we wanted to do with the parse tree m
right?
Not quite. The problem is that the parser produces two sorts of errors : "fatal" and "non-fatal". Thus far, we have only considered the "fatal" ones.
Fatal errors are such that production of a parse tree is impossible. Non-fatal parse errors are those that don't prevent construction of a parse tree. A parse that generates non-fatal errors is going to associate with a parse tree in some way non-conforming to the Haskell language specification.
The right way to write the POk
case is like this.
POk s m -> do
let (warns, errs) = getMessages s flags
report flags warns
report flags errs
when (null errs) $ analyzeModule flags m
The key point is analyzeModule
is called only if there are absolutely no parse errors at all.
A non-fatal error example is provided by the ImportQualifiedPost
language extension (see this post for how to add a GHC language extension). Specifically, it is only legal to write import M qualified
if the extension is in effect via pragma or the option -XImportQualifiedPost
. In the event this syntax is used when the extension is not in effect, the user should see an error like
test/MiniHlintTest_non_fatal_error.hs:6:18: error: Found `qualified' in postpositive position. To allow this, enable language extension 'ImportQualifiedPost'and further analysis of the parse abandoned.