Skip to content

Commit 7211f79

Browse files
committed
Expr.Types.Annotated: org: reorg, add headlines, upd docs
1 parent 2815371 commit 7211f79

File tree

1 file changed

+37
-23
lines changed

1 file changed

+37
-23
lines changed

src/Nix/Expr/Types/Annotated.hs

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,30 @@ import Text.Megaparsec.Pos ( SourcePos(..) )
4747
import Text.Read.Deriving
4848
import Text.Show.Deriving
4949

50-
-- | A location in a source file
50+
-- * data type @SrcSpan@ - a zone in a source file
51+
52+
-- | Demarcation of a chunk in a source file.
5153
data SrcSpan = SrcSpan
5254
{ spanBegin :: SourcePos
5355
, spanEnd :: SourcePos
5456
}
5557
deriving (Ord, Eq, Generic, Typeable, Data, Show, NFData, Hashable)
5658

59+
-- ** Instances
60+
61+
instance Semigroup SrcSpan where
62+
s1 <> s2 = SrcSpan ((min `on` spanBegin) s1 s2) ((max `on` spanEnd) s1 s2)
63+
64+
instance Binary SrcSpan
65+
instance ToJSON SrcSpan
66+
instance FromJSON SrcSpan
67+
5768
#ifdef MIN_VERSION_serialise
5869
instance Serialise SrcSpan
5970
#endif
6071

72+
-- * data type @Ann@
73+
6174
-- | A type constructor applied to a type along with an annotation
6275
--
6376
-- Intended to be used with 'Fix':
@@ -69,11 +82,8 @@ data Ann ann a = Ann
6982
deriving (Ord, Eq, Data, Generic, Generic1, Typeable, Functor, Foldable,
7083
Traversable, Read, Show, NFData, Hashable)
7184

72-
instance Hashable ann => Hashable1 (Ann ann)
85+
type AnnF ann f = Compose (Ann ann) f
7386

74-
#ifdef MIN_VERSION_serialise
75-
instance (Serialise ann, Serialise a) => Serialise (Ann ann a)
76-
#endif
7787
pattern AnnE
7888
:: forall ann (g :: * -> *)
7989
. ann
@@ -82,8 +92,17 @@ pattern AnnE
8292
pattern AnnE ann a = Fix (Compose (Ann ann a))
8393
{-# complete AnnE #-}
8494

95+
annToAnnF :: Ann ann (f (Fix (AnnF ann f))) -> Fix (AnnF ann f)
96+
annToAnnF (Ann ann a) = AnnE ann a
97+
98+
-- ** Instances
99+
100+
instance Hashable ann => Hashable1 (Ann ann)
101+
85102
instance NFData ann => NFData1 (Ann ann)
86103

104+
instance (Binary ann, Binary a) => Binary (Ann ann a)
105+
87106
$(deriveEq1 ''Ann)
88107
$(deriveEq2 ''Ann)
89108
$(deriveOrd1 ''Ann)
@@ -95,37 +114,32 @@ $(deriveShow2 ''Ann)
95114
$(deriveJSON1 defaultOptions ''Ann)
96115
$(deriveJSON2 defaultOptions ''Ann)
97116

98-
instance Semigroup SrcSpan where
99-
s1 <> s2 = SrcSpan ((min `on` spanBegin) s1 s2) ((max `on` spanEnd) s1 s2)
117+
#ifdef MIN_VERSION_serialise
118+
instance (Serialise ann, Serialise a) => Serialise (Ann ann a)
119+
#endif
100120

101-
type AnnF ann f = Compose (Ann ann) f
121+
#ifdef MIN_VERSION_serialise
122+
instance Serialise r => Serialise (Compose (Ann SrcSpan) NExprF r) where
123+
encode (Compose (Ann ann a)) = encode ann <> encode a
124+
decode = (Compose .) . Ann <$> decode <*> decode
125+
#endif
102126

103-
annToAnnF :: Ann ann (f (Fix (AnnF ann f))) -> Fix (AnnF ann f)
104-
annToAnnF (Ann ann a) = AnnE ann a
127+
-- ** @NExprLoc{,F}@ - annotated Nix expression
105128

106129
type NExprLocF = AnnF SrcSpan NExprF
107130

108-
-- | A nix expression with source location at each subexpression.
131+
instance Binary r => Binary (NExprLocF r)
132+
133+
-- | Annotated Nix expression (each subexpression direct to its source location).
109134
type NExprLoc = Fix NExprLocF
110135

111136
#ifdef MIN_VERSION_serialise
112137
instance Serialise NExprLoc
113138
#endif
114139

115-
instance Binary SrcSpan
116-
instance (Binary ann, Binary a) => Binary (Ann ann a)
117-
instance Binary r => Binary (NExprLocF r)
118140
instance Binary NExprLoc
119141

120-
instance ToJSON SrcSpan
121-
instance FromJSON SrcSpan
122-
123-
#ifdef MIN_VERSION_serialise
124-
instance Serialise r => Serialise (Compose (Ann SrcSpan) NExprF r) where
125-
encode (Compose (Ann ann a)) = encode ann <> encode a
126-
decode = (Compose .) . Ann <$> decode <*> decode
127-
#endif
128-
142+
-- * Other
129143

130144
stripAnnotation :: Functor f => Fix (AnnF ann f) -> Fix f
131145
stripAnnotation = unfoldFix (annotated . getCompose . unFix)

0 commit comments

Comments
 (0)