on 357dedf help sign in
elk/src/Reporting/Task.hs
51 lines 1.0 kB history raw
1module Reporting.Task where
3import Control.Monad.Except (Except, ExceptT, runExcept, runExceptT, throwError, withExceptT)
4import qualified Control.Monad.State as State
6import qualified Reporting.Annotation as A
7import qualified Reporting.Region as R
10-- TASK
12type Task warning error result =
13 ExceptT [A.Located error] (State.State [A.Located warning]) result
16-- RUN TASKS
18run :: Task w e a -> (Either [A.Located e] a, [A.Located w])
19run task =
20 State.runState (runExceptT task) []
23-- WARNINGS
25addWarnings :: [A.Located w] -> Task w e ()
26addWarnings warnings =
27 State.modify (warnings++)
30-- ERRORS
32throw :: R.Region -> e -> Task w e a
33throw region err =
34 throwError [A.A region err]
37mapError :: (e -> e') -> Task w e a -> Task w e' a
38mapError f task =
39 withExceptT (map (A.map f)) task
42from :: (e -> e') -> Except [A.Located e] a -> Task w e' a
43from f except =
44 case runExcept except of
45 Right answer ->
46 return answer
48 Left errors ->
49 throwError (map (A.map f) errors)