{-# LANGUAGE DeriveFoldable    #-}
{-# LANGUAGE DeriveFunctor     #-}
{-# LANGUAGE DeriveTraversable #-}
-- | Cabal-like file AST types: 'Field', 'Section' etc
--
-- These types are parametrized by an annotation.
module Distribution.Fields.Field (
    -- * Cabal file
    Field (..),
    fieldName,
    fieldAnn,
    fieldUniverse,
    FieldLine (..),
    fieldLineAnn,
    fieldLineBS,
    SectionArg (..),
    sectionArgAnn,
    -- * Name
    FieldName,
    Name (..),
    mkName,
    getName,
    nameAnn,
    -- * Conversions to String
    sectionArgsToString,
    fieldLinesToString,
    ) where

import           Data.ByteString             (ByteString)
import qualified Data.ByteString.Char8       as B
import qualified Data.Char                   as Char
import           Distribution.Compat.Prelude
import           Distribution.Pretty         (showTokenStr)
import           Distribution.Simple.Utils   (fromUTF8BS)
import           Prelude ()


-------------------------------------------------------------------------------
-- Cabal file
-------------------------------------------------------------------------------

-- | A Cabal-like file consists of a series of fields (@foo: bar@) and sections (@library ...@).
data Field ann
    = Field   !(Name ann) [FieldLine ann]
    | Section !(Name ann) [SectionArg ann] [Field ann]
  deriving (Field ann -> Field ann -> Bool
(Field ann -> Field ann -> Bool)
-> (Field ann -> Field ann -> Bool) -> Eq (Field ann)
forall ann. Eq ann => Field ann -> Field ann -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Field ann -> Field ann -> Bool
$c/= :: forall ann. Eq ann => Field ann -> Field ann -> Bool
== :: Field ann -> Field ann -> Bool
$c== :: forall ann. Eq ann => Field ann -> Field ann -> Bool
Eq, Int -> Field ann -> ShowS
[Field ann] -> ShowS
Field ann -> String
(Int -> Field ann -> ShowS)
-> (Field ann -> String)
-> ([Field ann] -> ShowS)
-> Show (Field ann)
forall ann. Show ann => Int -> Field ann -> ShowS
forall ann. Show ann => [Field ann] -> ShowS
forall ann. Show ann => Field ann -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Field ann] -> ShowS
$cshowList :: forall ann. Show ann => [Field ann] -> ShowS
show :: Field ann -> String
$cshow :: forall ann. Show ann => Field ann -> String
showsPrec :: Int -> Field ann -> ShowS
$cshowsPrec :: forall ann. Show ann => Int -> Field ann -> ShowS
Show, (forall a b. (a -> b) -> Field a -> Field b)
-> (forall a b. a -> Field b -> Field a) -> Functor Field
forall a b. a -> Field b -> Field a
forall a b. (a -> b) -> Field a -> Field b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> Field b -> Field a
$c<$ :: forall a b. a -> Field b -> Field a
fmap :: forall a b. (a -> b) -> Field a -> Field b
$cfmap :: forall a b. (a -> b) -> Field a -> Field b
Functor, (forall m. Monoid m => Field m -> m)
-> (forall m a. Monoid m => (a -> m) -> Field a -> m)
-> (forall m a. Monoid m => (a -> m) -> Field a -> m)
-> (forall a b. (a -> b -> b) -> b -> Field a -> b)
-> (forall a b. (a -> b -> b) -> b -> Field a -> b)
-> (forall b a. (b -> a -> b) -> b -> Field a -> b)
-> (forall b a. (b -> a -> b) -> b -> Field a -> b)
-> (forall a. (a -> a -> a) -> Field a -> a)
-> (forall a. (a -> a -> a) -> Field a -> a)
-> (forall a. Field a -> [a])
-> (forall a. Field a -> Bool)
-> (forall a. Field a -> Int)
-> (forall a. Eq a => a -> Field a -> Bool)
-> (forall a. Ord a => Field a -> a)
-> (forall a. Ord a => Field a -> a)
-> (forall a. Num a => Field a -> a)
-> (forall a. Num a => Field a -> a)
-> Foldable Field
forall a. Eq a => a -> Field a -> Bool
forall a. Num a => Field a -> a
forall a. Ord a => Field a -> a
forall m. Monoid m => Field m -> m
forall a. Field a -> Bool
forall a. Field a -> Int
forall a. Field a -> [a]
forall a. (a -> a -> a) -> Field a -> a
forall m a. Monoid m => (a -> m) -> Field a -> m
forall b a. (b -> a -> b) -> b -> Field a -> b
forall a b. (a -> b -> b) -> b -> Field a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: forall a. Num a => Field a -> a
$cproduct :: forall a. Num a => Field a -> a
sum :: forall a. Num a => Field a -> a
$csum :: forall a. Num a => Field a -> a
minimum :: forall a. Ord a => Field a -> a
$cminimum :: forall a. Ord a => Field a -> a
maximum :: forall a. Ord a => Field a -> a
$cmaximum :: forall a. Ord a => Field a -> a
elem :: forall a. Eq a => a -> Field a -> Bool
$celem :: forall a. Eq a => a -> Field a -> Bool
length :: forall a. Field a -> Int
$clength :: forall a. Field a -> Int
null :: forall a. Field a -> Bool
$cnull :: forall a. Field a -> Bool
toList :: forall a. Field a -> [a]
$ctoList :: forall a. Field a -> [a]
foldl1 :: forall a. (a -> a -> a) -> Field a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> Field a -> a
foldr1 :: forall a. (a -> a -> a) -> Field a -> a
$cfoldr1 :: forall a. (a -> a -> a) -> Field a -> a
foldl' :: forall b a. (b -> a -> b) -> b -> Field a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> Field a -> b
foldl :: forall b a. (b -> a -> b) -> b -> Field a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> Field a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> Field a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> Field a -> b
foldr :: forall a b. (a -> b -> b) -> b -> Field a -> b
$cfoldr :: forall a b. (a -> b -> b) -> b -> Field a -> b
foldMap' :: forall m a. Monoid m => (a -> m) -> Field a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> Field a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> Field a -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> Field a -> m
fold :: forall m. Monoid m => Field m -> m
$cfold :: forall m. Monoid m => Field m -> m
Foldable, Functor Field
Foldable Field
Functor Field
-> Foldable Field
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> Field a -> f (Field b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    Field (f a) -> f (Field a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> Field a -> m (Field b))
-> (forall (m :: * -> *) a. Monad m => Field (m a) -> m (Field a))
-> Traversable Field
forall (t :: * -> *).
Functor t
-> Foldable t
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a. Monad m => Field (m a) -> m (Field a)
forall (f :: * -> *) a. Applicative f => Field (f a) -> f (Field a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Field a -> m (Field b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Field a -> f (Field b)
sequence :: forall (m :: * -> *) a. Monad m => Field (m a) -> m (Field a)
$csequence :: forall (m :: * -> *) a. Monad m => Field (m a) -> m (Field a)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Field a -> m (Field b)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Field a -> m (Field b)
sequenceA :: forall (f :: * -> *) a. Applicative f => Field (f a) -> f (Field a)
$csequenceA :: forall (f :: * -> *) a. Applicative f => Field (f a) -> f (Field a)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Field a -> f (Field b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Field a -> f (Field b)
Traversable)

-- | Section of field name
fieldName :: Field ann -> Name ann
fieldName :: forall ann. Field ann -> Name ann
fieldName (Field Name ann
n [FieldLine ann]
_ )    = Name ann
n
fieldName (Section Name ann
n [SectionArg ann]
_ [Field ann]
_) = Name ann
n

fieldAnn :: Field ann -> ann
fieldAnn :: forall ann. Field ann -> ann
fieldAnn = Name ann -> ann
forall ann. Name ann -> ann
nameAnn (Name ann -> ann) -> (Field ann -> Name ann) -> Field ann -> ann
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Field ann -> Name ann
forall ann. Field ann -> Name ann
fieldName

-- | All transitive descendants of 'Field', including itself.
--
-- /Note:/ the resulting list is never empty.
--
fieldUniverse :: Field ann -> [Field ann]
fieldUniverse :: forall ann. Field ann -> [Field ann]
fieldUniverse f :: Field ann
f@(Section Name ann
_ [SectionArg ann]
_ [Field ann]
fs) = Field ann
f Field ann -> [Field ann] -> [Field ann]
forall a. a -> [a] -> [a]
: (Field ann -> [Field ann]) -> [Field ann] -> [Field ann]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Field ann -> [Field ann]
forall ann. Field ann -> [Field ann]
fieldUniverse [Field ann]
fs
fieldUniverse f :: Field ann
f@(Field Name ann
_ [FieldLine ann]
_)      = [Field ann
f]

-- | A line of text representing the value of a field from a Cabal file.
-- A field may contain multiple lines.
--
-- /Invariant:/ 'ByteString' has no newlines.
data FieldLine ann  = FieldLine  !ann !ByteString
  deriving (FieldLine ann -> FieldLine ann -> Bool
(FieldLine ann -> FieldLine ann -> Bool)
-> (FieldLine ann -> FieldLine ann -> Bool) -> Eq (FieldLine ann)
forall ann. Eq ann => FieldLine ann -> FieldLine ann -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: FieldLine ann -> FieldLine ann -> Bool
$c/= :: forall ann. Eq ann => FieldLine ann -> FieldLine ann -> Bool
== :: FieldLine ann -> FieldLine ann -> Bool
$c== :: forall ann. Eq ann => FieldLine ann -> FieldLine ann -> Bool
Eq, Int -> FieldLine ann -> ShowS
[FieldLine ann] -> ShowS
FieldLine ann -> String
(Int -> FieldLine ann -> ShowS)
-> (FieldLine ann -> String)
-> ([FieldLine ann] -> ShowS)
-> Show (FieldLine ann)
forall ann. Show ann => Int -> FieldLine ann -> ShowS
forall ann. Show ann => [FieldLine ann] -> ShowS
forall ann. Show ann => FieldLine ann -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [FieldLine ann] -> ShowS
$cshowList :: forall ann. Show ann => [FieldLine ann] -> ShowS
show :: FieldLine ann -> String
$cshow :: forall ann. Show ann => FieldLine ann -> String
showsPrec :: Int -> FieldLine ann -> ShowS
$cshowsPrec :: forall ann. Show ann => Int -> FieldLine ann -> ShowS
Show, (forall a b. (a -> b) -> FieldLine a -> FieldLine b)
-> (forall a b. a -> FieldLine b -> FieldLine a)
-> Functor FieldLine
forall a b. a -> FieldLine b -> FieldLine a
forall a b. (a -> b) -> FieldLine a -> FieldLine b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> FieldLine b -> FieldLine a
$c<$ :: forall a b. a -> FieldLine b -> FieldLine a
fmap :: forall a b. (a -> b) -> FieldLine a -> FieldLine b
$cfmap :: forall a b. (a -> b) -> FieldLine a -> FieldLine b
Functor, (forall m. Monoid m => FieldLine m -> m)
-> (forall m a. Monoid m => (a -> m) -> FieldLine a -> m)
-> (forall m a. Monoid m => (a -> m) -> FieldLine a -> m)
-> (forall a b. (a -> b -> b) -> b -> FieldLine a -> b)
-> (forall a b. (a -> b -> b) -> b -> FieldLine a -> b)
-> (forall b a. (b -> a -> b) -> b -> FieldLine a -> b)
-> (forall b a. (b -> a -> b) -> b -> FieldLine a -> b)
-> (forall a. (a -> a -> a) -> FieldLine a -> a)
-> (forall a. (a -> a -> a) -> FieldLine a -> a)
-> (forall a. FieldLine a -> [a])
-> (forall a. FieldLine a -> Bool)
-> (forall a. FieldLine a -> Int)
-> (forall a. Eq a => a -> FieldLine a -> Bool)
-> (forall a. Ord a => FieldLine a -> a)
-> (forall a. Ord a => FieldLine a -> a)
-> (forall a. Num a => FieldLine a -> a)
-> (forall a. Num a => FieldLine a -> a)
-> Foldable FieldLine
forall a. Eq a => a -> FieldLine a -> Bool
forall a. Num a => FieldLine a -> a
forall a. Ord a => FieldLine a -> a
forall m. Monoid m => FieldLine m -> m
forall a. FieldLine a -> Bool
forall a. FieldLine a -> Int
forall a. FieldLine a -> [a]
forall a. (a -> a -> a) -> FieldLine a -> a
forall m a. Monoid m => (a -> m) -> FieldLine a -> m
forall b a. (b -> a -> b) -> b -> FieldLine a -> b
forall a b. (a -> b -> b) -> b -> FieldLine a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: forall a. Num a => FieldLine a -> a
$cproduct :: forall a. Num a => FieldLine a -> a
sum :: forall a. Num a => FieldLine a -> a
$csum :: forall a. Num a => FieldLine a -> a
minimum :: forall a. Ord a => FieldLine a -> a
$cminimum :: forall a. Ord a => FieldLine a -> a
maximum :: forall a. Ord a => FieldLine a -> a
$cmaximum :: forall a. Ord a => FieldLine a -> a
elem :: forall a. Eq a => a -> FieldLine a -> Bool
$celem :: forall a. Eq a => a -> FieldLine a -> Bool
length :: forall a. FieldLine a -> Int
$clength :: forall a. FieldLine a -> Int
null :: forall a. FieldLine a -> Bool
$cnull :: forall a. FieldLine a -> Bool
toList :: forall a. FieldLine a -> [a]
$ctoList :: forall a. FieldLine a -> [a]
foldl1 :: forall a. (a -> a -> a) -> FieldLine a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> FieldLine a -> a
foldr1 :: forall a. (a -> a -> a) -> FieldLine a -> a
$cfoldr1 :: forall a. (a -> a -> a) -> FieldLine a -> a
foldl' :: forall b a. (b -> a -> b) -> b -> FieldLine a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> FieldLine a -> b
foldl :: forall b a. (b -> a -> b) -> b -> FieldLine a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> FieldLine a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> FieldLine a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> FieldLine a -> b
foldr :: forall a b. (a -> b -> b) -> b -> FieldLine a -> b
$cfoldr :: forall a b. (a -> b -> b) -> b -> FieldLine a -> b
foldMap' :: forall m a. Monoid m => (a -> m) -> FieldLine a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> FieldLine a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> FieldLine a -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> FieldLine a -> m
fold :: forall m. Monoid m => FieldLine m -> m
$cfold :: forall m. Monoid m => FieldLine m -> m
Foldable, Functor FieldLine
Foldable FieldLine
Functor FieldLine
-> Foldable FieldLine
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> FieldLine a -> f (FieldLine b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    FieldLine (f a) -> f (FieldLine a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> FieldLine a -> m (FieldLine b))
-> (forall (m :: * -> *) a.
    Monad m =>
    FieldLine (m a) -> m (FieldLine a))
-> Traversable FieldLine
forall (t :: * -> *).
Functor t
-> Foldable t
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a.
Monad m =>
FieldLine (m a) -> m (FieldLine a)
forall (f :: * -> *) a.
Applicative f =>
FieldLine (f a) -> f (FieldLine a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> FieldLine a -> m (FieldLine b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> FieldLine a -> f (FieldLine b)
sequence :: forall (m :: * -> *) a.
Monad m =>
FieldLine (m a) -> m (FieldLine a)
$csequence :: forall (m :: * -> *) a.
Monad m =>
FieldLine (m a) -> m (FieldLine a)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> FieldLine a -> m (FieldLine b)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> FieldLine a -> m (FieldLine b)
sequenceA :: forall (f :: * -> *) a.
Applicative f =>
FieldLine (f a) -> f (FieldLine a)
$csequenceA :: forall (f :: * -> *) a.
Applicative f =>
FieldLine (f a) -> f (FieldLine a)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> FieldLine a -> f (FieldLine b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> FieldLine a -> f (FieldLine b)
Traversable)

-- | @since 3.0.0.0
fieldLineAnn :: FieldLine ann -> ann
fieldLineAnn :: forall ann. FieldLine ann -> ann
fieldLineAnn (FieldLine ann
ann ByteString
_) = ann
ann

-- | @since 3.0.0.0
fieldLineBS :: FieldLine ann -> ByteString
fieldLineBS :: forall ann. FieldLine ann -> ByteString
fieldLineBS (FieldLine ann
_ ByteString
bs) = ByteString
bs

-- | Section arguments, e.g. name of the library
data SectionArg ann
    = SecArgName  !ann !ByteString
      -- ^ identifier, or something which looks like number. Also many dot numbers, i.e. "7.6.3"
    | SecArgStr   !ann !ByteString
      -- ^ quoted string
    | SecArgOther !ann !ByteString
      -- ^ everything else, mm. operators (e.g. in if-section conditionals)
  deriving (SectionArg ann -> SectionArg ann -> Bool
(SectionArg ann -> SectionArg ann -> Bool)
-> (SectionArg ann -> SectionArg ann -> Bool)
-> Eq (SectionArg ann)
forall ann. Eq ann => SectionArg ann -> SectionArg ann -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SectionArg ann -> SectionArg ann -> Bool
$c/= :: forall ann. Eq ann => SectionArg ann -> SectionArg ann -> Bool
== :: SectionArg ann -> SectionArg ann -> Bool
$c== :: forall ann. Eq ann => SectionArg ann -> SectionArg ann -> Bool
Eq, Int -> SectionArg ann -> ShowS
[SectionArg ann] -> ShowS
SectionArg ann -> String
(Int -> SectionArg ann -> ShowS)
-> (SectionArg ann -> String)
-> ([SectionArg ann] -> ShowS)
-> Show (SectionArg ann)
forall ann. Show ann => Int -> SectionArg ann -> ShowS
forall ann. Show ann => [SectionArg ann] -> ShowS
forall ann. Show ann => SectionArg ann -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SectionArg ann] -> ShowS
$cshowList :: forall ann. Show ann => [SectionArg ann] -> ShowS
show :: SectionArg ann -> String
$cshow :: forall ann. Show ann => SectionArg ann -> String
showsPrec :: Int -> SectionArg ann -> ShowS
$cshowsPrec :: forall ann. Show ann => Int -> SectionArg ann -> ShowS
Show, (forall a b. (a -> b) -> SectionArg a -> SectionArg b)
-> (forall a b. a -> SectionArg b -> SectionArg a)
-> Functor SectionArg
forall a b. a -> SectionArg b -> SectionArg a
forall a b. (a -> b) -> SectionArg a -> SectionArg b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> SectionArg b -> SectionArg a
$c<$ :: forall a b. a -> SectionArg b -> SectionArg a
fmap :: forall a b. (a -> b) -> SectionArg a -> SectionArg b
$cfmap :: forall a b. (a -> b) -> SectionArg a -> SectionArg b
Functor, (forall m. Monoid m => SectionArg m -> m)
-> (forall m a. Monoid m => (a -> m) -> SectionArg a -> m)
-> (forall m a. Monoid m => (a -> m) -> SectionArg a -> m)
-> (forall a b. (a -> b -> b) -> b -> SectionArg a -> b)
-> (forall a b. (a -> b -> b) -> b -> SectionArg a -> b)
-> (forall b a. (b -> a -> b) -> b -> SectionArg a -> b)
-> (forall b a. (b -> a -> b) -> b -> SectionArg a -> b)
-> (forall a. (a -> a -> a) -> SectionArg a -> a)
-> (forall a. (a -> a -> a) -> SectionArg a -> a)
-> (forall a. SectionArg a -> [a])
-> (forall a. SectionArg a -> Bool)
-> (forall a. SectionArg a -> Int)
-> (forall a. Eq a => a -> SectionArg a -> Bool)
-> (forall a. Ord a => SectionArg a -> a)
-> (forall a. Ord a => SectionArg a -> a)
-> (forall a. Num a => SectionArg a -> a)
-> (forall a. Num a => SectionArg a -> a)
-> Foldable SectionArg
forall a. Eq a => a -> SectionArg a -> Bool
forall a. Num a => SectionArg a -> a
forall a. Ord a => SectionArg a -> a
forall m. Monoid m => SectionArg m -> m
forall a. SectionArg a -> Bool
forall a. SectionArg a -> Int
forall a. SectionArg a -> [a]
forall a. (a -> a -> a) -> SectionArg a -> a
forall m a. Monoid m => (a -> m) -> SectionArg a -> m
forall b a. (b -> a -> b) -> b -> SectionArg a -> b
forall a b. (a -> b -> b) -> b -> SectionArg a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: forall a. Num a => SectionArg a -> a
$cproduct :: forall a. Num a => SectionArg a -> a
sum :: forall a. Num a => SectionArg a -> a
$csum :: forall a. Num a => SectionArg a -> a
minimum :: forall a. Ord a => SectionArg a -> a
$cminimum :: forall a. Ord a => SectionArg a -> a
maximum :: forall a. Ord a => SectionArg a -> a
$cmaximum :: forall a. Ord a => SectionArg a -> a
elem :: forall a. Eq a => a -> SectionArg a -> Bool
$celem :: forall a. Eq a => a -> SectionArg a -> Bool
length :: forall a. SectionArg a -> Int
$clength :: forall a. SectionArg a -> Int
null :: forall a. SectionArg a -> Bool
$cnull :: forall a. SectionArg a -> Bool
toList :: forall a. SectionArg a -> [a]
$ctoList :: forall a. SectionArg a -> [a]
foldl1 :: forall a. (a -> a -> a) -> SectionArg a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> SectionArg a -> a
foldr1 :: forall a. (a -> a -> a) -> SectionArg a -> a
$cfoldr1 :: forall a. (a -> a -> a) -> SectionArg a -> a
foldl' :: forall b a. (b -> a -> b) -> b -> SectionArg a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> SectionArg a -> b
foldl :: forall b a. (b -> a -> b) -> b -> SectionArg a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> SectionArg a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> SectionArg a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> SectionArg a -> b
foldr :: forall a b. (a -> b -> b) -> b -> SectionArg a -> b
$cfoldr :: forall a b. (a -> b -> b) -> b -> SectionArg a -> b
foldMap' :: forall m a. Monoid m => (a -> m) -> SectionArg a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> SectionArg a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> SectionArg a -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> SectionArg a -> m
fold :: forall m. Monoid m => SectionArg m -> m
$cfold :: forall m. Monoid m => SectionArg m -> m
Foldable, Functor SectionArg
Foldable SectionArg
Functor SectionArg
-> Foldable SectionArg
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> SectionArg a -> f (SectionArg b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    SectionArg (f a) -> f (SectionArg a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> SectionArg a -> m (SectionArg b))
-> (forall (m :: * -> *) a.
    Monad m =>
    SectionArg (m a) -> m (SectionArg a))
-> Traversable SectionArg
forall (t :: * -> *).
Functor t
-> Foldable t
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a.
Monad m =>
SectionArg (m a) -> m (SectionArg a)
forall (f :: * -> *) a.
Applicative f =>
SectionArg (f a) -> f (SectionArg a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> SectionArg a -> m (SectionArg b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> SectionArg a -> f (SectionArg b)
sequence :: forall (m :: * -> *) a.
Monad m =>
SectionArg (m a) -> m (SectionArg a)
$csequence :: forall (m :: * -> *) a.
Monad m =>
SectionArg (m a) -> m (SectionArg a)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> SectionArg a -> m (SectionArg b)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> SectionArg a -> m (SectionArg b)
sequenceA :: forall (f :: * -> *) a.
Applicative f =>
SectionArg (f a) -> f (SectionArg a)
$csequenceA :: forall (f :: * -> *) a.
Applicative f =>
SectionArg (f a) -> f (SectionArg a)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> SectionArg a -> f (SectionArg b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> SectionArg a -> f (SectionArg b)
Traversable)

-- | Extract annotation from 'SectionArg'.
sectionArgAnn :: SectionArg ann -> ann
sectionArgAnn :: forall ann. SectionArg ann -> ann
sectionArgAnn (SecArgName ann
ann ByteString
_)  = ann
ann
sectionArgAnn (SecArgStr ann
ann ByteString
_)   = ann
ann
sectionArgAnn (SecArgOther ann
ann ByteString
_) = ann
ann

-------------------------------------------------------------------------------
-- Name
-------------------------------------------------------------------------------

type FieldName = ByteString

-- | A field name.
--
-- /Invariant/: 'ByteString' is lower-case ASCII.
data Name ann  = Name       !ann !FieldName
  deriving (Name ann -> Name ann -> Bool
(Name ann -> Name ann -> Bool)
-> (Name ann -> Name ann -> Bool) -> Eq (Name ann)
forall ann. Eq ann => Name ann -> Name ann -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Name ann -> Name ann -> Bool
$c/= :: forall ann. Eq ann => Name ann -> Name ann -> Bool
== :: Name ann -> Name ann -> Bool
$c== :: forall ann. Eq ann => Name ann -> Name ann -> Bool
Eq, Int -> Name ann -> ShowS
[Name ann] -> ShowS
Name ann -> String
(Int -> Name ann -> ShowS)
-> (Name ann -> String) -> ([Name ann] -> ShowS) -> Show (Name ann)
forall ann. Show ann => Int -> Name ann -> ShowS
forall ann. Show ann => [Name ann] -> ShowS
forall ann. Show ann => Name ann -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Name ann] -> ShowS
$cshowList :: forall ann. Show ann => [Name ann] -> ShowS
show :: Name ann -> String
$cshow :: forall ann. Show ann => Name ann -> String
showsPrec :: Int -> Name ann -> ShowS
$cshowsPrec :: forall ann. Show ann => Int -> Name ann -> ShowS
Show, (forall a b. (a -> b) -> Name a -> Name b)
-> (forall a b. a -> Name b -> Name a) -> Functor Name
forall a b. a -> Name b -> Name a
forall a b. (a -> b) -> Name a -> Name b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> Name b -> Name a
$c<$ :: forall a b. a -> Name b -> Name a
fmap :: forall a b. (a -> b) -> Name a -> Name b
$cfmap :: forall a b. (a -> b) -> Name a -> Name b
Functor, (forall m. Monoid m => Name m -> m)
-> (forall m a. Monoid m => (a -> m) -> Name a -> m)
-> (forall m a. Monoid m => (a -> m) -> Name a -> m)
-> (forall a b. (a -> b -> b) -> b -> Name a -> b)
-> (forall a b. (a -> b -> b) -> b -> Name a -> b)
-> (forall b a. (b -> a -> b) -> b -> Name a -> b)
-> (forall b a. (b -> a -> b) -> b -> Name a -> b)
-> (forall a. (a -> a -> a) -> Name a -> a)
-> (forall a. (a -> a -> a) -> Name a -> a)
-> (forall a. Name a -> [a])
-> (forall a. Name a -> Bool)
-> (forall a. Name a -> Int)
-> (forall a. Eq a => a -> Name a -> Bool)
-> (forall a. Ord a => Name a -> a)
-> (forall a. Ord a => Name a -> a)
-> (forall a. Num a => Name a -> a)
-> (forall a. Num a => Name a -> a)
-> Foldable Name
forall a. Eq a => a -> Name a -> Bool
forall a. Num a => Name a -> a
forall a. Ord a => Name a -> a
forall m. Monoid m => Name m -> m
forall a. Name a -> Bool
forall a. Name a -> Int
forall a. Name a -> [a]
forall a. (a -> a -> a) -> Name a -> a
forall m a. Monoid m => (a -> m) -> Name a -> m
forall b a. (b -> a -> b) -> b -> Name a -> b
forall a b. (a -> b -> b) -> b -> Name a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: forall a. Num a => Name a -> a
$cproduct :: forall a. Num a => Name a -> a
sum :: forall a. Num a => Name a -> a
$csum :: forall a. Num a => Name a -> a
minimum :: forall a. Ord a => Name a -> a
$cminimum :: forall a. Ord a => Name a -> a
maximum :: forall a. Ord a => Name a -> a
$cmaximum :: forall a. Ord a => Name a -> a
elem :: forall a. Eq a => a -> Name a -> Bool
$celem :: forall a. Eq a => a -> Name a -> Bool
length :: forall a. Name a -> Int
$clength :: forall a. Name a -> Int
null :: forall a. Name a -> Bool
$cnull :: forall a. Name a -> Bool
toList :: forall a. Name a -> [a]
$ctoList :: forall a. Name a -> [a]
foldl1 :: forall a. (a -> a -> a) -> Name a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> Name a -> a
foldr1 :: forall a. (a -> a -> a) -> Name a -> a
$cfoldr1 :: forall a. (a -> a -> a) -> Name a -> a
foldl' :: forall b a. (b -> a -> b) -> b -> Name a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> Name a -> b
foldl :: forall b a. (b -> a -> b) -> b -> Name a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> Name a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> Name a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> Name a -> b
foldr :: forall a b. (a -> b -> b) -> b -> Name a -> b
$cfoldr :: forall a b. (a -> b -> b) -> b -> Name a -> b
foldMap' :: forall m a. Monoid m => (a -> m) -> Name a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> Name a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> Name a -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> Name a -> m
fold :: forall m. Monoid m => Name m -> m
$cfold :: forall m. Monoid m => Name m -> m
Foldable, Functor Name
Foldable Name
Functor Name
-> Foldable Name
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> Name a -> f (Name b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    Name (f a) -> f (Name a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> Name a -> m (Name b))
-> (forall (m :: * -> *) a. Monad m => Name (m a) -> m (Name a))
-> Traversable Name
forall (t :: * -> *).
Functor t
-> Foldable t
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a. Monad m => Name (m a) -> m (Name a)
forall (f :: * -> *) a. Applicative f => Name (f a) -> f (Name a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Name a -> m (Name b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Name a -> f (Name b)
sequence :: forall (m :: * -> *) a. Monad m => Name (m a) -> m (Name a)
$csequence :: forall (m :: * -> *) a. Monad m => Name (m a) -> m (Name a)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Name a -> m (Name b)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Name a -> m (Name b)
sequenceA :: forall (f :: * -> *) a. Applicative f => Name (f a) -> f (Name a)
$csequenceA :: forall (f :: * -> *) a. Applicative f => Name (f a) -> f (Name a)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Name a -> f (Name b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Name a -> f (Name b)
Traversable)

mkName :: ann -> FieldName -> Name ann
mkName :: forall ann. ann -> ByteString -> Name ann
mkName ann
ann ByteString
bs = ann -> ByteString -> Name ann
forall ann. ann -> ByteString -> Name ann
Name ann
ann ((Char -> Char) -> ByteString -> ByteString
B.map Char -> Char
Char.toLower ByteString
bs)

getName :: Name ann -> FieldName
getName :: forall ann. Name ann -> ByteString
getName (Name ann
_ ByteString
bs) = ByteString
bs

nameAnn :: Name ann -> ann
nameAnn :: forall ann. Name ann -> ann
nameAnn (Name ann
ann ByteString
_) = ann
ann

-------------------------------------------------------------------------------
-- To Strings
-------------------------------------------------------------------------------

-- |
--
-- @since 3.6.0.0
sectionArgsToString :: [SectionArg ann] -> String
sectionArgsToString :: forall ann. [SectionArg ann] -> String
sectionArgsToString = [String] -> String
unwords ([String] -> String)
-> ([SectionArg ann] -> [String]) -> [SectionArg ann] -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (SectionArg ann -> String) -> [SectionArg ann] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map SectionArg ann -> String
forall ann. SectionArg ann -> String
toStr where
    toStr :: SectionArg ann -> String
    toStr :: forall ann. SectionArg ann -> String
toStr (SecArgName ann
_ ByteString
bs)  = ShowS
showTokenStr (ByteString -> String
fromUTF8BS ByteString
bs)
    toStr (SecArgStr ann
_ ByteString
bs)   = ShowS
showTokenStr (ByteString -> String
fromUTF8BS ByteString
bs)
    toStr (SecArgOther ann
_ ByteString
bs) = ByteString -> String
fromUTF8BS ByteString
bs

-- | Convert @['FieldLine']@ into String.
--
-- /Note:/ this doesn't preserve indentation or empty lines,
-- as the annotations (e.g. positions) are ignored.
--
-- @since 3.6.0.0
fieldLinesToString :: [FieldLine ann] -> String
fieldLinesToString :: forall ann. [FieldLine ann] -> String
fieldLinesToString =
    -- intercalate to avoid trailing newline.
    String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
"\n" ([String] -> String)
-> ([FieldLine ann] -> [String]) -> [FieldLine ann] -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (FieldLine ann -> String) -> [FieldLine ann] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map FieldLine ann -> String
forall {ann}. FieldLine ann -> String
toStr
  where
    toStr :: FieldLine ann -> String
toStr (FieldLine ann
_ ByteString
bs) = ByteString -> String
fromUTF8BS ByteString
bs