{-# LANGUAGE CPP #-}
module Smuggler2.Parser
( runParser,
)
where
import Control.Monad.IO.Class (MonadIO (liftIO))
import DynFlags (DynFlags, GeneralFlag (Opt_KeepRawTokenStream), gopt_set)
import GHC (ParsedSource)
import Language.Haskell.GHC.ExactPrint (Anns)
import Language.Haskell.GHC.ExactPrint.Parsers (parseModuleFromStringInternal)
#if MIN_VERSION_GLASGOW_HASKELL(8,10,1,0)
import ErrUtils (fatalErrorMsg, printBagOfErrors )
import Outputable (text)
#else
import ErrUtils (fatalErrorMsg)
import Outputable (ppr, showSDoc, text)
#endif
import TcRnTypes (RnM)
runParser ::
DynFlags -> FilePath -> String -> RnM (Either () (Anns, ParsedSource))
runParser :: DynFlags
-> FilePath -> FilePath -> RnM (Either () (Anns, ParsedSource))
runParser DynFlags
dflags FilePath
fileName FilePath
fileContents = do
let dflags' :: DynFlags
dflags' = DynFlags
dflags DynFlags -> GeneralFlag -> DynFlags
`gopt_set` GeneralFlag
Opt_KeepRawTokenStream
case Parser ParsedSource
parseModuleFromStringInternal DynFlags
dflags' FilePath
fileName FilePath
fileContents of
Left ErrorMessages
msg -> IO (Either () (Anns, ParsedSource))
-> RnM (Either () (Anns, ParsedSource))
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Either () (Anns, ParsedSource))
-> RnM (Either () (Anns, ParsedSource)))
-> IO (Either () (Anns, ParsedSource))
-> RnM (Either () (Anns, ParsedSource))
forall a b. (a -> b) -> a -> b
$ do
#if MIN_VERSION_GLASGOW_HASKELL(8,10,1,0)
DynFlags -> MsgDoc -> IO ()
fatalErrorMsg DynFlags
dflags (FilePath -> MsgDoc
text FilePath
"smuggler2 parse failure:")
DynFlags -> ErrorMessages -> IO ()
printBagOfErrors DynFlags
dflags ErrorMessages
msg
#else
fatalErrorMsg dflags (text $ "smuggler2 parse failure: " ++
showSDoc dflags (ppr $ fst msg) ++ ": " ++ snd msg)
#endif
Either () (Anns, ParsedSource)
-> IO (Either () (Anns, ParsedSource))
forall (m :: * -> *) a. Monad m => a -> m a
return (Either () (Anns, ParsedSource)
-> IO (Either () (Anns, ParsedSource)))
-> Either () (Anns, ParsedSource)
-> IO (Either () (Anns, ParsedSource))
forall a b. (a -> b) -> a -> b
$ () -> Either () (Anns, ParsedSource)
forall a b. a -> Either a b
Left ()
Right (Anns, ParsedSource)
x -> Either () (Anns, ParsedSource)
-> RnM (Either () (Anns, ParsedSource))
forall (m :: * -> *) a. Monad m => a -> m a
return (Either () (Anns, ParsedSource)
-> RnM (Either () (Anns, ParsedSource)))
-> Either () (Anns, ParsedSource)
-> RnM (Either () (Anns, ParsedSource))
forall a b. (a -> b) -> a -> b
$ (Anns, ParsedSource) -> Either () (Anns, ParsedSource)
forall a b. b -> Either a b
Right (Anns, ParsedSource)
x