{- |
Module      : LH.Data.Error
Description : A generic interface for errors emitted by Layered Haskell.
Copyright   : (c) Hangil Kim, 2026
                  Kairon Johnson, 2026
                  Leonidas Sallos, 2026
                  Oscar Abelanet, 2026
License     : MIT
Maintainer  : kimhang@oregonstate.edu
Stability   : experimental

This module provides an interface for errors emitted by Layered Haskell,
and provides a few formatting helpers to aid in the construction of error messages.
-}
{-# LANGUAGE OverloadedStrings #-}
module LH.Data.Error (
    -- * Errors
    LHsError (..),
    -- ** Reporting multiple errors
    LHsMaxError (..),
    -- ** Formatting errors
    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 (..))

-- | A type representing an error is obligated to implement two functions: 'renderError' and 'buildError'.
-- 
--   'renderError' displays an error as 'Text', whereas 'buildError' constructs the error message itself.
--   Note that 'renderError' has a default definition that will be sufficient in the vast majority of cases.
class LHsError a where
    -- | Renders an error as a message referencing the source text.
    renderError :: FilePath  -- ^ The filepath of the source text. 
                -> Text      -- ^ The source text itself.
                -> a         -- ^ An error.
                -> Text      -- ^ The resulting message.
    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])
    -- | Builds the message corresponding to an error.
    buildError  :: FilePath  -- ^ The filepath of the source text. 
                -> a         -- ^ An error.
                -> Errata    -- ^ A pretty-printable representation of the error.
                             --   This is where the error message itself should be written,
                             --   in addition to places in the source that it may need to point to.

-- | Multiple simultaneous errors are all printed.
-- 
--   'renderError' prints the errors separately, while 'buildError' merges them all into one.
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

-- | A type for representing a number of errors simultaneously, but only up to a maximum.
newtype LHsMaxError a = LHsMaxError (Int, [a])

-- | Multiple simultaneous errors /up to a maximum/ are printed, 
--   then elided with the message @"...and @\(m-n\)@ more errors"@ 
--   if the number of errors \(m\) exceeds the maximum \(n\).
-- 
--   'renderError' prints the errors separately, while 'buildError' merges them all into one.
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 
            }

-- | Underlines a __single-line__ span alongside a label.
underline :: FilePath    -- ^ The filepath of the source text.
          -> LHsSpan     -- ^ The source span to underline.
          -> Text        -- ^ A label.
          -> 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 +>

-- | Constructs a span of a given length starting at a given position.
(+>) :: LHsPos -> Int -> LHsSpan
LHsPos
pos +> :: LHsPos -> Int -> LHsSpan
+> Int
len = LHsPos -> LHsPos -> LHsSpan
LHsSpan LHsPos
pos (LHsPos
pos { posCol = posCol pos + len })