|
| 1 | +{-# LANGUAGE TemplateHaskell #-} |
| 2 | +module Development.IDE.GHC.Compat.Error ( |
| 3 | + -- * Top-level error types and lens for easy access |
| 4 | + MsgEnvelope(..), |
| 5 | + msgEnvelopeErrorL, |
| 6 | + GhcMessage(..), |
| 7 | + -- * Tc - Rn phase error messages |
| 8 | + TcRnMessage (..), |
| 9 | + TcRnMessageDetailed (..), |
| 10 | + flatTcRnMessage, |
| 11 | + -- * Parsing error message |
| 12 | + PsMessage(..), |
| 13 | + -- * Desugaring diagnostic |
| 14 | + DsMessage (..), |
| 15 | + -- * Driver error message |
| 16 | + DriverMessage (..), |
| 17 | + -- * Unkown errors |
| 18 | + UnknownDiagnostic(..), |
| 19 | + -- * General Diagnostics |
| 20 | + Diagnostic(..), |
| 21 | + -- * Prisms for error selection |
| 22 | + _TcRnMessage, |
| 23 | + _GhcPsMessage, |
| 24 | + _GhcDsMessage, |
| 25 | + _GhcDriverMessage, |
| 26 | + _GhcUnknownMessage, |
| 27 | + ) where |
| 28 | + |
| 29 | +import Control.Lens |
| 30 | +import GHC.Driver.Errors.Types |
| 31 | +import GHC.HsToCore.Errors.Types |
| 32 | +import GHC.Tc.Errors.Types |
| 33 | +import GHC.Types.Error |
| 34 | + |
| 35 | +_TcRnMessage :: Prism' GhcMessage TcRnMessage |
| 36 | +_TcRnMessage = prism' GhcTcRnMessage (\case |
| 37 | + GhcTcRnMessage tcRnMsg -> Just tcRnMsg |
| 38 | + _ -> Nothing) |
| 39 | + |
| 40 | +_GhcPsMessage :: Prism' GhcMessage PsMessage |
| 41 | +_GhcPsMessage = prism' GhcPsMessage (\case |
| 42 | + GhcPsMessage psMsg -> Just psMsg |
| 43 | + _ -> Nothing) |
| 44 | + |
| 45 | +_GhcDsMessage :: Prism' GhcMessage DsMessage |
| 46 | +_GhcDsMessage = prism' GhcDsMessage (\case |
| 47 | + GhcDsMessage dsMsg -> Just dsMsg |
| 48 | + _ -> Nothing) |
| 49 | + |
| 50 | +_GhcDriverMessage :: Prism' GhcMessage DriverMessage |
| 51 | +_GhcDriverMessage = prism' GhcDriverMessage (\case |
| 52 | + GhcDriverMessage driverMsg -> Just driverMsg |
| 53 | + _ -> Nothing) |
| 54 | + |
| 55 | +_GhcUnknownMessage :: Prism' GhcMessage UnknownDiagnostic |
| 56 | +_GhcUnknownMessage = prism' GhcUnknownMessage (\case |
| 57 | + GhcUnknownMessage unknownMsg -> Just unknownMsg |
| 58 | + _ -> Nothing) |
| 59 | + |
| 60 | +-- | Some 'TcRnMessage's are nested in other constructors for additional context. |
| 61 | +-- For example, 'TcRnWithHsDocContext' and 'TcRnMessageWithInfo'. |
| 62 | +-- However, in some occasions you don't need the additional context and you just want |
| 63 | +-- the error message. @'flatTcRnMessage'@ recursively unwraps these constructors, |
| 64 | +-- until there are no more constructors with additional context. |
| 65 | +-- |
| 66 | +flatTcRnMessage :: TcRnMessage -> TcRnMessage |
| 67 | +flatTcRnMessage = \case |
| 68 | + TcRnWithHsDocContext _ tcMsg -> flatTcRnMessage tcMsg |
| 69 | + TcRnMessageWithInfo _ (TcRnMessageDetailed _ tcMsg) -> flatTcRnMessage tcMsg |
| 70 | + msg -> msg |
| 71 | + |
| 72 | +msgEnvelopeErrorL :: Lens' (MsgEnvelope e) e |
| 73 | +msgEnvelopeErrorL = lens errMsgDiagnostic (\envelope e -> envelope { errMsgDiagnostic = e } ) |
0 commit comments