{-# LANGUAGE OverloadedStrings #-}
module LH.Data.Error (
LHsError (..),
LHsMaxError (..),
underline, (+>)
) where
import Data.Text (Text)
import Errata
import Errata.Styles
import qualified Data.Text as Text
import qualified Data.Text.Lazy as LazyText
import LH.Data.SrcLoc (LHsPos (..), LHsSpan (..))
class LHsError a where
renderError :: FilePath
-> Text
-> a
-> Text
renderError FilePath
file Text
src a
err =
Text -> Text
LazyText.toStrict (Text -> [Errata] -> Text
forall source. Source source => source -> [Errata] -> Text
prettyErrors Text
src [FilePath -> a -> Errata
forall a. LHsError a => FilePath -> a -> Errata
buildError FilePath
file a
err])
buildError :: FilePath
-> a
-> Errata
instance LHsError a => LHsError [a] where
renderError :: FilePath -> Text -> [a] -> Text
renderError FilePath
file Text
src [a]
errs =
Text -> Text
LazyText.toStrict (Text -> [Errata] -> Text
forall source. Source source => source -> [Errata] -> Text
prettyErrors Text
src ((a -> Errata) -> [a] -> [Errata]
forall a b. (a -> b) -> [a] -> [b]
map (FilePath -> a -> Errata
forall a. LHsError a => FilePath -> a -> Errata
buildError FilePath
file) [a]
errs))
buildError :: FilePath -> [a] -> Errata
buildError FilePath
file [a]
errs =
Maybe Text -> [Block] -> Maybe Text -> Errata
Errata
(Text -> Maybe Text
forall a. a -> Maybe a
Just (Text -> [Text] -> Text
Text.intercalate Text
"\n" [Text
header | Errata
err <- [Errata]
built, Just Text
header <- [Errata -> Maybe Text
errataHeader Errata
err]]))
((Errata -> [Block]) -> [Errata] -> [Block]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Errata -> [Block]
errataBlocks [Errata]
built)
Maybe Text
forall a. Maybe a
Nothing
where built :: [Errata]
built = (a -> Errata) -> [a] -> [Errata]
forall a b. (a -> b) -> [a] -> [b]
map (FilePath -> a -> Errata
forall a. LHsError a => FilePath -> a -> Errata
buildError FilePath
file) [a]
errs
newtype LHsMaxError a = LHsMaxError (Int, [a])
instance LHsError a => LHsError (LHsMaxError a) where
renderError :: FilePath -> Text -> LHsMaxError a -> Text
renderError FilePath
file Text
src (LHsMaxError (Int
n, [a]
errs)) =
let m :: Int
m = [a] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
errs in
FilePath -> Text -> [a] -> Text
forall a. LHsError a => FilePath -> Text -> a -> Text
renderError FilePath
file Text
src (Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
take Int
n [a]
errs) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>
if Int
m Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
n
then Text
"\n...and " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> FilePath -> Text
Text.pack (Int -> FilePath
forall a. Show a => a -> FilePath
show (Int
m Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
n)) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" more errors"
else Text
""
buildError :: FilePath -> LHsMaxError a -> Errata
buildError FilePath
file (LHsMaxError (Int
n, [a]
errs)) =
let m :: Int
m = [a] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
errs in
(FilePath -> [a] -> Errata
forall a. LHsError a => FilePath -> a -> Errata
buildError FilePath
file (Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
take Int
n [a]
errs))
{ errataBody =
if m > n
then Just $ "\n...and " <> Text.pack (show (m - n)) <> " more errors"
else Nothing
}
underline :: FilePath
-> LHsSpan
-> Text
-> Block
underline :: FilePath -> LHsSpan -> Text -> Block
underline FilePath
file (LHsSpan (LHsPos Int
l Int
c) (LHsPos Int
_ Int
c')) Text
label =
Style
-> PointerStyle
-> FilePath
-> Maybe Text
-> (Int, Int, Int, Maybe Text)
-> Maybe Text
-> Block
blockSimple
Style
fancyRedStyle
PointerStyle
fancyRedPointer
FilePath
file
Maybe Text
forall a. Maybe a
Nothing
(Int
l, Int
c, Int
c', Text -> Maybe Text
forall a. a -> Maybe a
Just Text
label)
Maybe Text
forall a. Maybe a
Nothing
infix 5 +>
(+>) :: LHsPos -> Int -> LHsSpan
LHsPos
pos +> :: LHsPos -> Int -> LHsSpan
+> Int
len = LHsPos -> LHsPos -> LHsSpan
LHsSpan LHsPos
pos (LHsPos
pos { posCol = posCol pos + len })