on 087045f help sign in
elk/src/Reporting/Annotation.hs
81 lines 1.4 kB history raw
1{-# OPTIONS_GHC -Wall #-}
2module Reporting.Annotation
3 ( Annotated(..)
4 , Located
5 , Commented
6 , at, merge, sameAs
7 , map, drop
8 , listToDict
9 )
10 where
13import Prelude hiding (drop, map)
14import qualified Data.List as List
15import Data.List.NonEmpty (NonEmpty((:|)))
16import qualified Data.Map as Map
17import Data.Semigroup ((<>))
18import Data.Text (Text)
19import qualified Reporting.Region as R
23-- ANNOTATION
26data Annotated annotation a =
27 A annotation a
30type Located a =
31 Annotated R.Region a
34type Commented a =
35 Annotated (R.Region, Maybe Text) a
39-- CREATE
42at :: R.Position -> R.Position -> a -> Located a
43at start end value =
44 A (R.Region start end) value
47merge :: Located a -> Located b -> value -> Located value
48merge (A region1 _) (A region2 _) value =
49 A (R.merge region1 region2) value
52sameAs :: Annotated info a -> b -> Annotated info b
53sameAs (A annotation _) value =
54 A annotation value
58-- MANIPULATE
61map :: (a -> b) -> Annotated info a -> Annotated info b
62map f (A info value) =
63 A info (f value)
66drop :: Annotated info a -> a
67drop (A _ value) =
68 value
72-- ANALYZE
75listToDict :: (Ord key) => (a -> key) -> [Annotated i a] -> Map.Map key (NonEmpty i)
76listToDict toKey list =
77 let
78 add dict (A info value) =
79 Map.insertWith (<>) (toKey value) (info :| []) dict
80 in
81 List.foldl' add Map.empty list