module Hint.Parsers where

import Prelude hiding (span)

import Hint.Base

import Control.Monad.Trans (liftIO)

import qualified Hint.GHC as GHC

data ParseResult = ParseOk | ParseError GHC.SrcSpan GHC.Message

parseExpr :: MonadInterpreter m => String -> m ParseResult
parseExpr :: String -> m ParseResult
parseExpr = P (Maybe (LStmt GhcPs (LHsExpr GhcPs))) -> String -> m ParseResult
forall (m :: * -> *) a.
MonadInterpreter m =>
P a -> String -> m ParseResult
runParser P (Maybe (LStmt GhcPs (LHsExpr GhcPs)))
GHC.parseStmt

parseType :: MonadInterpreter m => String -> m ParseResult
parseType :: String -> m ParseResult
parseType = P (LHsType GhcPs) -> String -> m ParseResult
forall (m :: * -> *) a.
MonadInterpreter m =>
P a -> String -> m ParseResult
runParser P (LHsType GhcPs)
GHC.parseType

runParser :: MonadInterpreter m => GHC.P a -> String -> m ParseResult
runParser :: P a -> String -> m ParseResult
runParser parser :: P a
parser expr :: String
expr =
    do DynFlags
dyn_fl <- RunGhc m DynFlags
forall (m :: * -> *) a. MonadInterpreter m => RunGhc m a
runGhc forall (n :: * -> *). (MonadIO n, MonadMask n) => GhcT n DynFlags
forall (m :: * -> *). GhcMonad m => m DynFlags
GHC.getSessionDynFlags
       --
       StringBuffer
buf <- (StringBuffer -> m StringBuffer
forall (m :: * -> *) a. Monad m => a -> m a
return (StringBuffer -> m StringBuffer)
-> (String -> StringBuffer) -> String -> m StringBuffer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> StringBuffer
GHC.stringToStringBuffer) String
expr
       --
       -- ghc >= 7 panics if noSrcLoc is given
       let srcLoc :: RealSrcLoc
srcLoc = FastString -> Int -> Int -> RealSrcLoc
GHC.mkRealSrcLoc (String -> FastString
GHC.fsLit "<hint>") 1 1
       let parse_res :: ParseResult a
parse_res = P a -> PState -> ParseResult a
forall a. P a -> PState -> ParseResult a
GHC.unP P a
parser (DynFlags -> StringBuffer -> RealSrcLoc -> PState
GHC.mkPState DynFlags
dyn_fl StringBuffer
buf RealSrcLoc
srcLoc)
       --
       case ParseResult a
parse_res of
           GHC.POk{}            -> ParseResult -> m ParseResult
forall (m :: * -> *) a. Monad m => a -> m a
return ParseResult
ParseOk
           --
#if __GLASGOW_HASKELL__ >= 804
           GHC.PFailed _ span :: SrcSpan
span err :: MsgDoc
err
#else
           GHC.PFailed span err
#endif
                                -> ParseResult -> m ParseResult
forall (m :: * -> *) a. Monad m => a -> m a
return (SrcSpan -> MsgDoc -> ParseResult
ParseError SrcSpan
span MsgDoc
err)

failOnParseError :: MonadInterpreter m
                 => (String -> m ParseResult)
                 -> String
                 -> m ()
failOnParseError :: (String -> m ParseResult) -> String -> m ()
failOnParseError parser :: String -> m ParseResult
parser expr :: String
expr = m (Maybe ()) -> m ()
forall (m :: * -> *) a. MonadInterpreter m => m (Maybe a) -> m a
mayFail m (Maybe ())
go
    where go :: m (Maybe ())
go = do ParseResult
parsed <- String -> m ParseResult
parser String
expr
                  --
                  -- If there was a parsing error,
                  -- do the "standard" error reporting
                  case ParseResult
parsed of
                      ParseOk             -> Maybe () -> m (Maybe ())
forall (m :: * -> *) a. Monad m => a -> m a
return (() -> Maybe ()
forall a. a -> Maybe a
Just ())
                      --
                      ParseError span :: SrcSpan
span err :: MsgDoc
err ->
                          do -- parsing failed, so we report it just as all
                             -- other errors get reported....
                             GhcErrLogger
logger <- FromSession m GhcErrLogger
forall (m :: * -> *) a. MonadInterpreter m => FromSession m a
fromSession SessionData () -> GhcErrLogger
forall a. SessionData a -> GhcErrLogger
ghcErrLogger
                             DynFlags
dflags <- RunGhc m DynFlags
forall (m :: * -> *) a. MonadInterpreter m => RunGhc m a
runGhc forall (n :: * -> *). (MonadIO n, MonadMask n) => GhcT n DynFlags
forall (m :: * -> *). GhcMonad m => m DynFlags
GHC.getSessionDynFlags
                             let logger' :: WarnReason -> Severity -> SrcSpan -> PprStyle -> MsgDoc -> IO ()
logger'  = GhcErrLogger
logger DynFlags
dflags
                                 errStyle :: PprStyle
errStyle = DynFlags -> PprStyle
GHC.defaultErrStyle DynFlags
dflags
                             IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ WarnReason -> Severity -> SrcSpan -> PprStyle -> MsgDoc -> IO ()
logger'
                                              WarnReason
GHC.NoReason
                                              Severity
GHC.SevError
                                              SrcSpan
span
                                              PprStyle
errStyle
                                              MsgDoc
err
                             --
                             -- behave like the rest of the GHC API functions
                             -- do on error...
                             Maybe () -> m (Maybe ())
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe ()
forall a. Maybe a
Nothing