2018-05-09 01:40:56 +02:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
{-# LANGUAGE DeriveAnyClass #-}
|
2018-04-07 21:02:50 +02:00
|
|
|
{-# LANGUAGE DeriveDataTypeable #-}
|
|
|
|
{-# LANGUAGE DeriveFoldable #-}
|
|
|
|
{-# LANGUAGE DeriveFunctor #-}
|
|
|
|
{-# LANGUAGE DeriveGeneric #-}
|
|
|
|
{-# LANGUAGE DeriveTraversable #-}
|
|
|
|
{-# LANGUAGE FlexibleInstances #-}
|
|
|
|
{-# LANGUAGE KindSignatures #-}
|
|
|
|
{-# LANGUAGE LambdaCase #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE PatternSynonyms #-}
|
|
|
|
{-# LANGUAGE RankNTypes #-}
|
|
|
|
{-# LANGUAGE TemplateHaskell #-}
|
|
|
|
|
|
|
|
-- | The source location annotated nix expression type and supporting types.
|
|
|
|
--
|
|
|
|
module Nix.Expr.Types.Annotated
|
|
|
|
( module Nix.Expr.Types.Annotated
|
2018-04-25 22:00:41 +02:00
|
|
|
, module Data.Functor.Compose
|
|
|
|
, SourcePos(..), unPos, mkPos
|
2018-08-05 21:48:44 +02:00
|
|
|
) where
|
2018-04-07 21:02:50 +02:00
|
|
|
|
2018-05-11 21:15:18 +02:00
|
|
|
#ifdef MIN_VERSION_serialise
|
2018-04-12 02:59:42 +02:00
|
|
|
import Codec.Serialise
|
2018-05-09 01:40:56 +02:00
|
|
|
#endif
|
2018-04-09 11:07:40 +02:00
|
|
|
import Control.DeepSeq
|
2018-04-17 23:06:59 +02:00
|
|
|
import Data.Aeson (ToJSON(..), FromJSON(..))
|
|
|
|
import Data.Aeson.TH
|
2018-04-17 22:55:34 +02:00
|
|
|
import Data.Binary (Binary(..))
|
2018-04-07 21:02:50 +02:00
|
|
|
import Data.Data
|
2018-04-17 22:55:34 +02:00
|
|
|
import Data.Eq.Deriving
|
2018-04-07 21:02:50 +02:00
|
|
|
import Data.Fix
|
|
|
|
import Data.Function (on)
|
|
|
|
import Data.Functor.Compose
|
2018-04-17 22:55:34 +02:00
|
|
|
import Data.Hashable
|
2018-05-09 01:40:56 +02:00
|
|
|
#if MIN_VERSION_hashable(1, 2, 5)
|
2018-04-17 22:55:34 +02:00
|
|
|
import Data.Hashable.Lifted
|
2018-05-09 01:40:56 +02:00
|
|
|
#endif
|
2018-04-17 22:55:34 +02:00
|
|
|
import Data.Ord.Deriving
|
2018-04-08 01:33:50 +02:00
|
|
|
import Data.Text (Text, pack)
|
2018-04-07 21:02:50 +02:00
|
|
|
import GHC.Generics
|
2018-04-20 10:33:04 +02:00
|
|
|
import Nix.Atoms
|
2018-04-07 21:02:50 +02:00
|
|
|
import Nix.Expr.Types
|
2018-04-22 07:16:32 +02:00
|
|
|
import Text.Megaparsec (unPos, mkPos)
|
2018-05-07 21:06:56 +02:00
|
|
|
import Text.Megaparsec.Pos (SourcePos(..))
|
2018-04-17 22:55:34 +02:00
|
|
|
import Text.Read.Deriving
|
|
|
|
import Text.Show.Deriving
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
|
|
-- | A location in a source file
|
2018-04-17 22:55:34 +02:00
|
|
|
data SrcSpan = SrcSpan
|
|
|
|
{ spanBegin :: SourcePos
|
|
|
|
, spanEnd :: SourcePos
|
|
|
|
}
|
2018-08-05 21:48:44 +02:00
|
|
|
deriving (Ord, Eq, Generic, Typeable, Data, Show, NFData, Hashable)
|
2018-04-07 21:02:50 +02:00
|
|
|
|
2018-05-11 21:15:18 +02:00
|
|
|
#ifdef MIN_VERSION_serialise
|
2018-05-09 01:40:56 +02:00
|
|
|
instance Serialise SrcSpan
|
|
|
|
#endif
|
|
|
|
|
2018-04-07 21:02:50 +02:00
|
|
|
-- | A type constructor applied to a type along with an annotation
|
|
|
|
--
|
|
|
|
-- Intended to be used with 'Fix':
|
|
|
|
-- @type MyType = Fix (Compose (Ann Annotation) F)@
|
2018-04-17 22:55:34 +02:00
|
|
|
data Ann ann a = Ann
|
|
|
|
{ annotation :: ann
|
|
|
|
, annotated :: a
|
|
|
|
}
|
|
|
|
deriving (Ord, Eq, Data, Generic, Generic1, Typeable, Functor, Foldable,
|
2018-05-09 01:40:56 +02:00
|
|
|
Traversable, Read, Show, NFData, Hashable)
|
|
|
|
|
|
|
|
#if MIN_VERSION_hashable(1, 2, 5)
|
|
|
|
instance Hashable ann => Hashable1 (Ann ann)
|
|
|
|
#endif
|
|
|
|
|
2018-05-11 21:15:18 +02:00
|
|
|
#ifdef MIN_VERSION_serialise
|
2018-05-09 01:40:56 +02:00
|
|
|
instance (Serialise ann, Serialise a) => Serialise (Ann ann a)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if MIN_VERSION_deepseq(1, 4, 3)
|
|
|
|
instance NFData ann => NFData1 (Ann ann)
|
|
|
|
#endif
|
2018-04-17 22:55:34 +02:00
|
|
|
|
|
|
|
$(deriveEq1 ''Ann)
|
|
|
|
$(deriveEq2 ''Ann)
|
|
|
|
$(deriveOrd1 ''Ann)
|
|
|
|
$(deriveOrd2 ''Ann)
|
|
|
|
$(deriveRead1 ''Ann)
|
|
|
|
$(deriveRead2 ''Ann)
|
2018-04-07 21:02:50 +02:00
|
|
|
$(deriveShow1 ''Ann)
|
2018-04-17 22:55:34 +02:00
|
|
|
$(deriveShow2 ''Ann)
|
2018-04-17 23:06:59 +02:00
|
|
|
$(deriveJSON1 defaultOptions ''Ann)
|
|
|
|
$(deriveJSON2 defaultOptions ''Ann)
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
|
|
instance Semigroup SrcSpan where
|
|
|
|
s1 <> s2 = SrcSpan ((min `on` spanBegin) s1 s2)
|
|
|
|
((max `on` spanEnd) s1 s2)
|
|
|
|
|
|
|
|
type AnnF ann f = Compose (Ann ann) f
|
|
|
|
|
|
|
|
annToAnnF :: Ann ann (f (Fix (AnnF ann f))) -> Fix (AnnF ann f)
|
|
|
|
annToAnnF (Ann ann a) = AnnE ann a
|
|
|
|
|
|
|
|
type NExprLocF = AnnF SrcSpan NExprF
|
|
|
|
|
|
|
|
-- | A nix expression with source location at each subexpression.
|
|
|
|
type NExprLoc = Fix NExprLocF
|
|
|
|
|
2018-05-11 00:26:37 +02:00
|
|
|
#if !MIN_VERSION_deepseq(1, 4, 3)
|
|
|
|
instance (NFData (f (g a)), NFData (g a)) => NFData (Compose f g a)
|
2018-05-09 01:40:56 +02:00
|
|
|
#endif
|
|
|
|
|
2018-05-11 00:26:37 +02:00
|
|
|
instance NFData NExprLoc
|
|
|
|
|
2018-05-11 21:15:18 +02:00
|
|
|
#ifdef MIN_VERSION_serialise
|
2018-04-12 02:59:42 +02:00
|
|
|
instance Serialise NExprLoc
|
2018-05-09 01:40:56 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if MIN_VERSION_hashable(1, 2, 5)
|
2018-04-17 22:55:34 +02:00
|
|
|
instance Hashable NExprLoc
|
2018-05-09 01:40:56 +02:00
|
|
|
#endif
|
2018-04-17 22:55:34 +02:00
|
|
|
|
|
|
|
instance Binary SrcSpan
|
|
|
|
instance (Binary ann, Binary a) => Binary (Ann ann a)
|
|
|
|
instance Binary r => Binary (NExprLocF r)
|
|
|
|
instance Binary NExprLoc
|
2018-04-12 02:59:42 +02:00
|
|
|
|
2018-04-17 23:06:59 +02:00
|
|
|
instance ToJSON SrcSpan
|
|
|
|
instance FromJSON SrcSpan
|
|
|
|
|
2018-05-11 21:15:18 +02:00
|
|
|
#ifdef MIN_VERSION_serialise
|
2018-04-12 02:59:42 +02:00
|
|
|
instance Serialise r => Serialise (Compose (Ann SrcSpan) NExprF r) where
|
|
|
|
encode (Compose (Ann ann a)) = encode ann <> encode a
|
|
|
|
decode = (Compose .) . Ann <$> decode <*> decode
|
2018-05-09 01:40:56 +02:00
|
|
|
#endif
|
2018-04-09 11:07:40 +02:00
|
|
|
|
2018-04-07 21:02:50 +02:00
|
|
|
pattern AnnE :: forall ann (g :: * -> *). ann
|
|
|
|
-> g (Fix (Compose (Ann ann) g)) -> Fix (Compose (Ann ann) g)
|
|
|
|
pattern AnnE ann a = Fix (Compose (Ann ann a))
|
|
|
|
|
|
|
|
stripAnnotation :: Functor f => Fix (AnnF ann f) -> Fix f
|
|
|
|
stripAnnotation = ana (annotated . getCompose . unFix)
|
|
|
|
|
2018-04-22 07:16:32 +02:00
|
|
|
stripAnn :: AnnF ann f r -> f r
|
|
|
|
stripAnn = annotated . getCompose
|
|
|
|
|
2018-04-07 21:02:50 +02:00
|
|
|
nUnary :: Ann SrcSpan NUnaryOp -> NExprLoc -> NExprLoc
|
|
|
|
nUnary (Ann s1 u) e1@(AnnE s2 _) = AnnE (s1 <> s2) (NUnary u e1)
|
|
|
|
nUnary _ _ = error "nUnary: unexpected"
|
|
|
|
|
|
|
|
nBinary :: Ann SrcSpan NBinaryOp -> NExprLoc -> NExprLoc -> NExprLoc
|
|
|
|
nBinary (Ann s1 b) e1@(AnnE s2 _) e2@(AnnE s3 _) =
|
|
|
|
AnnE (s1 <> s2 <> s3) (NBinary b e1 e2)
|
|
|
|
nBinary _ _ _ = error "nBinary: unexpected"
|
|
|
|
|
2018-04-10 02:12:16 +02:00
|
|
|
nSelectLoc :: NExprLoc -> Ann SrcSpan (NAttrPath NExprLoc) -> Maybe NExprLoc
|
|
|
|
-> NExprLoc
|
|
|
|
nSelectLoc e1@(AnnE s1 _) (Ann s2 ats) d = case d of
|
|
|
|
Nothing -> AnnE (s1 <> s2) (NSelect e1 ats Nothing)
|
|
|
|
Just (e2@(AnnE s3 _)) -> AnnE (s1 <> s2 <> s3) (NSelect e1 ats (Just e2))
|
|
|
|
_ -> error "nSelectLoc: unexpected"
|
|
|
|
nSelectLoc _ _ _ = error "nSelectLoc: unexpected"
|
|
|
|
|
|
|
|
nHasAttr :: NExprLoc -> Ann SrcSpan (NAttrPath NExprLoc) -> NExprLoc
|
|
|
|
nHasAttr e1@(AnnE s1 _) (Ann s2 ats) = AnnE (s1 <> s2) (NHasAttr e1 ats)
|
|
|
|
nHasAttr _ _ = error "nHasAttr: unexpected"
|
|
|
|
|
2018-04-10 06:35:46 +02:00
|
|
|
nApp :: NExprLoc -> NExprLoc -> NExprLoc
|
|
|
|
nApp e1@(AnnE s1 _) e2@(AnnE s2 _) = AnnE (s1 <> s2) (NBinary NApp e1 e2)
|
|
|
|
nApp _ _ = error "nApp: unexpected"
|
|
|
|
|
2018-04-07 21:02:50 +02:00
|
|
|
nAbs :: Ann SrcSpan (Params NExprLoc) -> NExprLoc -> NExprLoc
|
|
|
|
nAbs (Ann s1 ps) e1@(AnnE s2 _) = AnnE (s1 <> s2) (NAbs ps e1)
|
|
|
|
nAbs _ _ = error "nAbs: unexpected"
|
|
|
|
|
|
|
|
nStr :: Ann SrcSpan (NString NExprLoc) -> NExprLoc
|
|
|
|
nStr (Ann s1 s) = AnnE s1 (NStr s)
|
2018-04-08 01:33:50 +02:00
|
|
|
|
2018-04-10 01:11:31 +02:00
|
|
|
deltaInfo :: SourcePos -> (Text, Int, Int)
|
|
|
|
deltaInfo (SourcePos fp l c) = (pack fp, unPos l, unPos c)
|
2018-04-20 10:33:04 +02:00
|
|
|
|
2018-04-22 07:16:32 +02:00
|
|
|
nNull :: NExprLoc
|
2018-04-25 22:00:41 +02:00
|
|
|
nNull = Fix (Compose (Ann nullSpan (NConstant NNull)))
|
2018-04-22 07:16:32 +02:00
|
|
|
|
2018-04-25 22:00:41 +02:00
|
|
|
nullSpan :: SrcSpan
|
|
|
|
nullSpan = SrcSpan nullPos nullPos
|
2018-04-22 07:16:32 +02:00
|
|
|
|
2018-04-20 10:33:04 +02:00
|
|
|
-- | Pattern systems for matching on NExprLocF constructions.
|
|
|
|
|
|
|
|
pattern NSym_ :: SrcSpan -> VarName -> NExprLocF r
|
|
|
|
pattern NSym_ ann x = Compose (Ann ann (NSym x))
|
|
|
|
|
|
|
|
pattern NConstant_ :: SrcSpan -> NAtom -> NExprLocF r
|
|
|
|
pattern NConstant_ ann x = Compose (Ann ann (NConstant x))
|
|
|
|
|
|
|
|
pattern NStr_ :: SrcSpan -> NString r -> NExprLocF r
|
|
|
|
pattern NStr_ ann x = Compose (Ann ann (NStr x))
|
|
|
|
|
|
|
|
pattern NList_ :: SrcSpan -> [r] -> NExprLocF r
|
|
|
|
pattern NList_ ann x = Compose (Ann ann (NList x))
|
|
|
|
|
|
|
|
pattern NSet_ :: SrcSpan -> [Binding r] -> NExprLocF r
|
|
|
|
pattern NSet_ ann x = Compose (Ann ann (NSet x))
|
|
|
|
|
|
|
|
pattern NRecSet_ :: SrcSpan -> [Binding r] -> NExprLocF r
|
|
|
|
pattern NRecSet_ ann x = Compose (Ann ann (NRecSet x))
|
|
|
|
|
|
|
|
pattern NLiteralPath_ :: SrcSpan -> FilePath -> NExprLocF r
|
|
|
|
pattern NLiteralPath_ ann x = Compose (Ann ann (NLiteralPath x))
|
|
|
|
|
|
|
|
pattern NEnvPath_ :: SrcSpan -> FilePath -> NExprLocF r
|
|
|
|
pattern NEnvPath_ ann x = Compose (Ann ann (NEnvPath x))
|
|
|
|
|
|
|
|
pattern NSelect_ :: SrcSpan -> r -> NAttrPath r -> Maybe r -> NExprLocF r
|
|
|
|
pattern NSelect_ ann x p v = Compose (Ann ann (NSelect x p v))
|
|
|
|
|
|
|
|
pattern NHasAttr_ :: SrcSpan -> r -> NAttrPath r -> NExprLocF r
|
|
|
|
pattern NHasAttr_ ann x p = Compose (Ann ann (NHasAttr x p))
|
|
|
|
|
|
|
|
pattern NAbs_ :: SrcSpan -> Params r-> r -> NExprLocF r
|
|
|
|
pattern NAbs_ ann x b = Compose (Ann ann (NAbs x b))
|
|
|
|
|
|
|
|
pattern NLet_ :: SrcSpan -> [Binding r] -> r -> NExprLocF r
|
|
|
|
pattern NLet_ ann x b = Compose (Ann ann (NLet x b))
|
|
|
|
|
|
|
|
pattern NIf_ :: SrcSpan -> r -> r -> r -> NExprLocF r
|
|
|
|
pattern NIf_ ann c t e = Compose (Ann ann (NIf c t e))
|
|
|
|
|
|
|
|
pattern NWith_ :: SrcSpan -> r -> r -> NExprLocF r
|
|
|
|
pattern NWith_ ann x y = Compose (Ann ann (NWith x y))
|
|
|
|
|
|
|
|
pattern NAssert_ :: SrcSpan -> r -> r -> NExprLocF r
|
|
|
|
pattern NAssert_ ann x y = Compose (Ann ann (NAssert x y))
|
|
|
|
|
|
|
|
pattern NUnary_ :: SrcSpan -> NUnaryOp -> r -> NExprLocF r
|
|
|
|
pattern NUnary_ ann op x = Compose (Ann ann (NUnary op x))
|
|
|
|
|
|
|
|
pattern NBinary_ :: SrcSpan -> NBinaryOp -> r -> r -> NExprLocF r
|
|
|
|
pattern NBinary_ ann op x y = Compose (Ann ann (NBinary op x y))
|