{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE StrictData        #-}
module Language.Cimple.Diagnostics
  ( Diagnostics
  , warn
  ) where

import           Control.Monad.State.Lazy (State)
import qualified Control.Monad.State.Lazy as State
import           Data.Text                (Text)
import qualified Data.Text                as Text
import           Language.Cimple.Lexer    (Lexeme (..), lexemeLine)

type Diagnostics a = State [Text] a

warn :: FilePath -> Lexeme Text -> Text -> Diagnostics ()
warn :: FilePath -> Lexeme Text -> Text -> Diagnostics ()
warn FilePath
file Lexeme Text
l Text
w = do
    [Text]
diags <- StateT [Text] Identity [Text]
forall s (m :: * -> *). MonadState s m => m s
State.get
    [Text] -> Diagnostics ()
forall s (m :: * -> *). MonadState s m => s -> m ()
State.put ([Text] -> Diagnostics ()) -> [Text] -> Diagnostics ()
forall a b. (a -> b) -> a -> b
$ Text
diag Text -> [Text] -> [Text]
forall a. a -> [a] -> [a]
: [Text]
diags
  where
    diag :: Text
diag = FilePath -> Text
Text.pack FilePath
file Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
":" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> FilePath -> Text
Text.pack (Int -> FilePath
forall a. Show a => a -> FilePath
show (Lexeme Text -> Int
forall text. Lexeme text -> Int
lexemeLine Lexeme Text
l)) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
": " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
w