@@ -1,60 +1,103 @@
1{-# OPTIONS_GHC -Wall #-}
1{-# OPTIONS_GHC -Wall #-}
2module Reporting.Annotation
2module Reporting.Annotation
3 ( Located(..)
3 ( Located(..)
4− , at, merge
4+ , Position(..)
7 , toRegion
10 , toRegion
8− , traverse
11+ , mergeRegions
13−import Prelude hiding (map, traverse)
18+import Prelude hiding (traverse)
14−import qualified Reporting.Region as R
19+import Control.Monad (liftM2)
20+import Data.Binary (Binary, get, put)
21+import Data.Word (Word16)
18−-- ANNOTATION
25+-- LOCATED
21data Located a =
28data Located a =
22− At R.Region a
29+ At Region a -- TODO see if unpacking region is helpful
32+instance Functor Located where
33+ fmap f (At region a) =
37+traverse :: (Functor f) => (a -> f b) -> Located a -> f (Located b)
38+traverse func (At region value) =
39+ At region <$> func value
29−at :: R.Position -> R.Position -> a -> Located a
42+toValue :: Located a -> a
30−at start end value =
43+toValue (At _ value) =
31− At (R.Region start end) value
44+ value
34merge :: Located a -> Located b -> value -> Located value
47merge :: Located a -> Located b -> value -> Located value
35−merge (At region1 _) (At region2 _) value =
48+merge (At r1 _) (At r2 _) value =
36− At (R.merge region1 region2) value
49+ At (mergeRegions r1 r2) value
40−-- MANIPULATE
53+-- POSITION
43−map :: (a -> b) -> Located a -> Located b
56+data Position =
44−map f (At info value) =
57+ Position
45− At info (f value)
58+ {-# UNPACK #-} !Word16
59+ {-# UNPACK #-} !Word16
63+at :: Position -> Position -> a -> Located a
65+ At (Region start end) a
48−toValue :: Located a -> a
49−toValue (At _ value) =
72+data Region = Region Position Position
53−toRegion :: Located a -> R.Region
76+toRegion :: Located a -> Region
54toRegion (At region _) =
77toRegion (At region _) =
58−traverse :: (Functor f) => (a -> f b) -> Located a -> f (Located b)
81+mergeRegions :: Region -> Region -> Region
59−traverse func (At region value) =
82+mergeRegions (Region start _) (Region _ end) =
60− At region <$> func value
83+ Region start end
88+ Region (Position 0 0) (Position 0 0)
93+ Region (Position 1 1) (Position 1 1)
96+instance Binary Region where
97+ put (Region a b) = put a >> put b
98+ get = liftM2 Region get get
101+instance Binary Position where
102+ put (Position a b) = put a >> put b
103+ get = liftM2 Position get get