{-# LANGUAGE OverloadedStrings #-}

module Distribution.Fields.ConfVar (parseConditionConfVar, parseConditionConfVarFromClause) where

import Distribution.Compat.CharParsing (char, integral)
import Distribution.Compat.Prelude
import Distribution.Fields.Field (Field (..), SectionArg (..))
import Distribution.Fields.ParseResult
import Distribution.Fields.Parser (readFields)
import Distribution.Parsec (Parsec (..), Position (..), runParsecParser)
import Distribution.Parsec.FieldLineStream (fieldLineStreamFromBS)
import Distribution.Types.Condition
import Distribution.Types.ConfVar (ConfVar (..))
import Distribution.Version
  ( anyVersion
  , earlierVersion
  , intersectVersionRanges
  , laterVersion
  , majorBoundVersion
  , mkVersion
  , noVersion
  , orEarlierVersion
  , orLaterVersion
  , thisVersion
  , unionVersionRanges
  , withinVersion
  )
import Prelude ()

import qualified Data.ByteString.Char8 as B8
import qualified Text.Parsec as P
import qualified Text.Parsec.Error as P
import qualified Text.Parsec.Pos as P

parseConditionConfVarFromClause :: B8.ByteString -> Either P.ParseError (Condition ConfVar)
parseConditionConfVarFromClause :: ByteString -> Either ParseError (Condition ConfVar)
parseConditionConfVarFromClause ByteString
x =
  ByteString -> Either ParseError [Field Position]
readFields ByteString
x Either ParseError [Field Position]
-> ([Field Position] -> Either ParseError (Condition ConfVar))
-> Either ParseError (Condition ConfVar)
forall a b.
Either ParseError a
-> (a -> Either ParseError b) -> Either ParseError b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \[Field Position]
r -> case [Field Position]
r of
    (Section Name Position
_ [SectionArg Position]
xs [Field Position]
_ : [Field Position]
_) -> Parsec [SectionArg Position] () (Condition ConfVar)
-> ()
-> SourceName
-> [SectionArg Position]
-> Either ParseError (Condition ConfVar)
forall s t u a.
Stream s Identity t =>
Parsec s u a -> u -> SourceName -> s -> Either ParseError a
P.runParser (Parsec [SectionArg Position] () (Condition ConfVar)
parser Parsec [SectionArg Position] () (Condition ConfVar)
-> ParsecT [SectionArg Position] () Identity ()
-> Parsec [SectionArg Position] () (Condition ConfVar)
forall a b.
ParsecT [SectionArg Position] () Identity a
-> ParsecT [SectionArg Position] () Identity b
-> ParsecT [SectionArg Position] () Identity a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* ParsecT [SectionArg Position] () Identity ()
forall s (m :: * -> *) t u.
(Stream s m t, Show t) =>
ParsecT s u m ()
P.eof) () SourceName
"<condition>" [SectionArg Position]
xs
    [Field Position]
_ -> ParseError -> Either ParseError (Condition ConfVar)
forall a b. a -> Either a b
Left (ParseError -> Either ParseError (Condition ConfVar))
-> ParseError -> Either ParseError (Condition ConfVar)
forall a b. (a -> b) -> a -> b
$ Message -> SourcePos -> ParseError
P.newErrorMessage (SourceName -> Message
P.Message SourceName
"No fields in clause") (SourceName -> SourcePos
P.initialPos SourceName
"<condition>")

-- | Parse @'Condition' 'ConfVar'@ from section arguments provided by parsec
-- based outline parser.
parseConditionConfVar :: [SectionArg Position] -> ParseResult (Condition ConfVar)
parseConditionConfVar :: [SectionArg Position] -> ParseResult (Condition ConfVar)
parseConditionConfVar [SectionArg Position]
args =
  -- The name of the input file is irrelevant, as we reformat the error message.
  case Parsec [SectionArg Position] () (Condition ConfVar)
-> ()
-> SourceName
-> [SectionArg Position]
-> Either ParseError (Condition ConfVar)
forall s t u a.
Stream s Identity t =>
Parsec s u a -> u -> SourceName -> s -> Either ParseError a
P.runParser (Parsec [SectionArg Position] () (Condition ConfVar)
parser Parsec [SectionArg Position] () (Condition ConfVar)
-> ParsecT [SectionArg Position] () Identity ()
-> Parsec [SectionArg Position] () (Condition ConfVar)
forall a b.
ParsecT [SectionArg Position] () Identity a
-> ParsecT [SectionArg Position] () Identity b
-> ParsecT [SectionArg Position] () Identity a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* ParsecT [SectionArg Position] () Identity ()
forall s (m :: * -> *) t u.
(Stream s m t, Show t) =>
ParsecT s u m ()
P.eof) () SourceName
"<condition>" [SectionArg Position]
args of
    Right Condition ConfVar
x -> Condition ConfVar -> ParseResult (Condition ConfVar)
forall a. a -> ParseResult a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Condition ConfVar
x
    Left ParseError
err -> do
      -- Mangle the position to the actual one
      let ppos :: SourcePos
ppos = ParseError -> SourcePos
P.errorPos ParseError
err
      let epos :: Position
epos = Int -> Int -> Position
Position (SourcePos -> Int
P.sourceLine SourcePos
ppos) (SourcePos -> Int
P.sourceColumn SourcePos
ppos)
      let msg :: SourceName
msg =
            SourceName
-> SourceName
-> SourceName
-> SourceName
-> SourceName
-> [Message]
-> SourceName
P.showErrorMessages
              SourceName
"or"
              SourceName
"unknown parse error"
              SourceName
"expecting"
              SourceName
"unexpected"
              SourceName
"end of input"
              (ParseError -> [Message]
P.errorMessages ParseError
err)
      Position -> SourceName -> ParseResult ()
parseFailure Position
epos SourceName
msg
      Condition ConfVar -> ParseResult (Condition ConfVar)
forall a. a -> ParseResult a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Condition ConfVar -> ParseResult (Condition ConfVar))
-> Condition ConfVar -> ParseResult (Condition ConfVar)
forall a b. (a -> b) -> a -> b
$ Bool -> Condition ConfVar
forall c. Bool -> Condition c
Lit Bool
True

type Parser = P.Parsec [SectionArg Position] ()

sepByNonEmpty :: Parser a -> Parser sep -> Parser (NonEmpty a)
sepByNonEmpty :: forall a sep. Parser a -> Parser sep -> Parser (NonEmpty a)
sepByNonEmpty Parser a
p Parser sep
sep = a -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
(:|) (a -> [a] -> NonEmpty a)
-> Parser a
-> ParsecT [SectionArg Position] () Identity ([a] -> NonEmpty a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser a
p ParsecT [SectionArg Position] () Identity ([a] -> NonEmpty a)
-> ParsecT [SectionArg Position] () Identity [a]
-> ParsecT [SectionArg Position] () Identity (NonEmpty a)
forall a b.
ParsecT [SectionArg Position] () Identity (a -> b)
-> ParsecT [SectionArg Position] () Identity a
-> ParsecT [SectionArg Position] () Identity b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser a -> ParsecT [SectionArg Position] () Identity [a]
forall a.
ParsecT [SectionArg Position] () Identity a
-> ParsecT [SectionArg Position] () Identity [a]
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many (Parser sep
sep Parser sep -> Parser a -> Parser a
forall a b.
ParsecT [SectionArg Position] () Identity a
-> ParsecT [SectionArg Position] () Identity b
-> ParsecT [SectionArg Position] () Identity b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser a
p)

parser :: Parser (Condition ConfVar)
parser :: Parsec [SectionArg Position] () (Condition ConfVar)
parser = Parsec [SectionArg Position] () (Condition ConfVar)
condOr
  where
    condOr :: Parsec [SectionArg Position] () (Condition ConfVar)
condOr = Parsec [SectionArg Position] () (Condition ConfVar)
-> ParsecT [SectionArg Position] () Identity ()
-> Parser (NonEmpty (Condition ConfVar))
forall a sep. Parser a -> Parser sep -> Parser (NonEmpty a)
sepByNonEmpty Parsec [SectionArg Position] () (Condition ConfVar)
condAnd (ByteString -> ParsecT [SectionArg Position] () Identity ()
forall {u}.
ByteString -> ParsecT [SectionArg Position] u Identity ()
oper ByteString
"||") Parser (NonEmpty (Condition ConfVar))
-> (NonEmpty (Condition ConfVar)
    -> Parsec [SectionArg Position] () (Condition ConfVar))
-> Parsec [SectionArg Position] () (Condition ConfVar)
forall a b.
ParsecT [SectionArg Position] () Identity a
-> (a -> ParsecT [SectionArg Position] () Identity b)
-> ParsecT [SectionArg Position] () Identity b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Condition ConfVar
-> Parsec [SectionArg Position] () (Condition ConfVar)
forall a. a -> ParsecT [SectionArg Position] () Identity a
forall (m :: * -> *) a. Monad m => a -> m a
return (Condition ConfVar
 -> Parsec [SectionArg Position] () (Condition ConfVar))
-> (NonEmpty (Condition ConfVar) -> Condition ConfVar)
-> NonEmpty (Condition ConfVar)
-> Parsec [SectionArg Position] () (Condition ConfVar)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Condition ConfVar -> Condition ConfVar -> Condition ConfVar)
-> NonEmpty (Condition ConfVar) -> Condition ConfVar
forall a. (a -> a -> a) -> NonEmpty a -> a
foldl1 Condition ConfVar -> Condition ConfVar -> Condition ConfVar
forall c. Condition c -> Condition c -> Condition c
COr
    condAnd :: Parsec [SectionArg Position] () (Condition ConfVar)
condAnd = Parsec [SectionArg Position] () (Condition ConfVar)
-> ParsecT [SectionArg Position] () Identity ()
-> Parser (NonEmpty (Condition ConfVar))
forall a sep. Parser a -> Parser sep -> Parser (NonEmpty a)
sepByNonEmpty Parsec [SectionArg Position] () (Condition ConfVar)
cond (ByteString -> ParsecT [SectionArg Position] () Identity ()
forall {u}.
ByteString -> ParsecT [SectionArg Position] u Identity ()
oper ByteString
"&&") Parser (NonEmpty (Condition ConfVar))
-> (NonEmpty (Condition ConfVar)
    -> Parsec [SectionArg Position] () (Condition ConfVar))
-> Parsec [SectionArg Position] () (Condition ConfVar)
forall a b.
ParsecT [SectionArg Position] () Identity a
-> (a -> ParsecT [SectionArg Position] () Identity b)
-> ParsecT [SectionArg Position] () Identity b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Condition ConfVar
-> Parsec [SectionArg Position] () (Condition ConfVar)
forall a. a -> ParsecT [SectionArg Position] () Identity a
forall (m :: * -> *) a. Monad m => a -> m a
return (Condition ConfVar
 -> Parsec [SectionArg Position] () (Condition ConfVar))
-> (NonEmpty (Condition ConfVar) -> Condition ConfVar)
-> NonEmpty (Condition ConfVar)
-> Parsec [SectionArg Position] () (Condition ConfVar)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Condition ConfVar -> Condition ConfVar -> Condition ConfVar)
-> NonEmpty (Condition ConfVar) -> Condition ConfVar
forall a. (a -> a -> a) -> NonEmpty a -> a
foldl1 Condition ConfVar -> Condition ConfVar -> Condition ConfVar
forall c. Condition c -> Condition c -> Condition c
CAnd
    cond :: Parsec [SectionArg Position] () (Condition ConfVar)
cond =
      [Parsec [SectionArg Position] () (Condition ConfVar)]
-> Parsec [SectionArg Position] () (Condition ConfVar)
forall s (m :: * -> *) t u a.
Stream s m t =>
[ParsecT s u m a] -> ParsecT s u m a
P.choice
        [Parsec [SectionArg Position] () (Condition ConfVar)
forall {u} {c}.
ParsecT [SectionArg Position] u Identity (Condition c)
boolLiteral, Parsec [SectionArg Position] () (Condition ConfVar)
-> Parsec [SectionArg Position] () (Condition ConfVar)
forall {u} {a}.
ParsecT [SectionArg Position] u Identity a
-> ParsecT [SectionArg Position] u Identity a
parens Parsec [SectionArg Position] () (Condition ConfVar)
condOr, Parsec [SectionArg Position] () (Condition ConfVar)
notCond, Parsec [SectionArg Position] () (Condition ConfVar)
osCond, Parsec [SectionArg Position] () (Condition ConfVar)
archCond, Parsec [SectionArg Position] () (Condition ConfVar)
flagCond, Parsec [SectionArg Position] () (Condition ConfVar)
implCond]

    notCond :: Parsec [SectionArg Position] () (Condition ConfVar)
notCond = Condition ConfVar -> Condition ConfVar
forall c. Condition c -> Condition c
CNot (Condition ConfVar -> Condition ConfVar)
-> ParsecT [SectionArg Position] () Identity ()
-> ParsecT
     [SectionArg Position]
     ()
     Identity
     (Condition ConfVar -> Condition ConfVar)
forall a b.
a
-> ParsecT [SectionArg Position] () Identity b
-> ParsecT [SectionArg Position] () Identity a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ByteString -> ParsecT [SectionArg Position] () Identity ()
forall {u}.
ByteString -> ParsecT [SectionArg Position] u Identity ()
oper ByteString
"!" ParsecT
  [SectionArg Position]
  ()
  Identity
  (Condition ConfVar -> Condition ConfVar)
-> Parsec [SectionArg Position] () (Condition ConfVar)
-> Parsec [SectionArg Position] () (Condition ConfVar)
forall a b.
ParsecT [SectionArg Position] () Identity (a -> b)
-> ParsecT [SectionArg Position] () Identity a
-> ParsecT [SectionArg Position] () Identity b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parsec [SectionArg Position] () (Condition ConfVar)
cond

    boolLiteral :: ParsecT [SectionArg Position] u Identity (Condition c)
boolLiteral = Bool -> Condition c
forall c. Bool -> Condition c
Lit (Bool -> Condition c)
-> ParsecT [SectionArg Position] u Identity Bool
-> ParsecT [SectionArg Position] u Identity (Condition c)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT [SectionArg Position] u Identity Bool
forall {u}. ParsecT [SectionArg Position] u Identity Bool
boolLiteral'
    osCond :: Parsec [SectionArg Position] () (Condition ConfVar)
osCond = ConfVar -> Condition ConfVar
forall c. c -> Condition c
Var (ConfVar -> Condition ConfVar)
-> (OS -> ConfVar) -> OS -> Condition ConfVar
forall b c a. (b -> c) -> (a -> b) -> a -> c
. OS -> ConfVar
OS (OS -> Condition ConfVar)
-> ParsecT [SectionArg Position] () Identity ()
-> ParsecT
     [SectionArg Position] () Identity (OS -> Condition ConfVar)
forall a b.
a
-> ParsecT [SectionArg Position] () Identity b
-> ParsecT [SectionArg Position] () Identity a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ByteString -> ParsecT [SectionArg Position] () Identity ()
forall {u}.
ByteString -> ParsecT [SectionArg Position] u Identity ()
string ByteString
"os" ParsecT [SectionArg Position] () Identity (OS -> Condition ConfVar)
-> ParsecT [SectionArg Position] () Identity OS
-> Parsec [SectionArg Position] () (Condition ConfVar)
forall a b.
ParsecT [SectionArg Position] () Identity (a -> b)
-> ParsecT [SectionArg Position] () Identity a
-> ParsecT [SectionArg Position] () Identity b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT [SectionArg Position] () Identity OS
-> ParsecT [SectionArg Position] () Identity OS
forall {u} {a}.
ParsecT [SectionArg Position] u Identity a
-> ParsecT [SectionArg Position] u Identity a
parens ParsecT [SectionArg Position] () Identity OS
forall a. Parsec a => Parser a
fromParsec
    flagCond :: Parsec [SectionArg Position] () (Condition ConfVar)
flagCond = ConfVar -> Condition ConfVar
forall c. c -> Condition c
Var (ConfVar -> Condition ConfVar)
-> (FlagName -> ConfVar) -> FlagName -> Condition ConfVar
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FlagName -> ConfVar
PackageFlag (FlagName -> Condition ConfVar)
-> ParsecT [SectionArg Position] () Identity ()
-> ParsecT
     [SectionArg Position] () Identity (FlagName -> Condition ConfVar)
forall a b.
a
-> ParsecT [SectionArg Position] () Identity b
-> ParsecT [SectionArg Position] () Identity a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ByteString -> ParsecT [SectionArg Position] () Identity ()
forall {u}.
ByteString -> ParsecT [SectionArg Position] u Identity ()
string ByteString
"flag" ParsecT
  [SectionArg Position] () Identity (FlagName -> Condition ConfVar)
-> ParsecT [SectionArg Position] () Identity FlagName
-> Parsec [SectionArg Position] () (Condition ConfVar)
forall a b.
ParsecT [SectionArg Position] () Identity (a -> b)
-> ParsecT [SectionArg Position] () Identity a
-> ParsecT [SectionArg Position] () Identity b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT [SectionArg Position] () Identity FlagName
-> ParsecT [SectionArg Position] () Identity FlagName
forall {u} {a}.
ParsecT [SectionArg Position] u Identity a
-> ParsecT [SectionArg Position] u Identity a
parens ParsecT [SectionArg Position] () Identity FlagName
forall a. Parsec a => Parser a
fromParsec
    archCond :: Parsec [SectionArg Position] () (Condition ConfVar)
archCond = ConfVar -> Condition ConfVar
forall c. c -> Condition c
Var (ConfVar -> Condition ConfVar)
-> (Arch -> ConfVar) -> Arch -> Condition ConfVar
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Arch -> ConfVar
Arch (Arch -> Condition ConfVar)
-> ParsecT [SectionArg Position] () Identity ()
-> ParsecT
     [SectionArg Position] () Identity (Arch -> Condition ConfVar)
forall a b.
a
-> ParsecT [SectionArg Position] () Identity b
-> ParsecT [SectionArg Position] () Identity a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ByteString -> ParsecT [SectionArg Position] () Identity ()
forall {u}.
ByteString -> ParsecT [SectionArg Position] u Identity ()
string ByteString
"arch" ParsecT
  [SectionArg Position] () Identity (Arch -> Condition ConfVar)
-> ParsecT [SectionArg Position] () Identity Arch
-> Parsec [SectionArg Position] () (Condition ConfVar)
forall a b.
ParsecT [SectionArg Position] () Identity (a -> b)
-> ParsecT [SectionArg Position] () Identity a
-> ParsecT [SectionArg Position] () Identity b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT [SectionArg Position] () Identity Arch
-> ParsecT [SectionArg Position] () Identity Arch
forall {u} {a}.
ParsecT [SectionArg Position] u Identity a
-> ParsecT [SectionArg Position] u Identity a
parens ParsecT [SectionArg Position] () Identity Arch
forall a. Parsec a => Parser a
fromParsec
    implCond :: Parsec [SectionArg Position] () (Condition ConfVar)
implCond = ConfVar -> Condition ConfVar
forall c. c -> Condition c
Var (ConfVar -> Condition ConfVar)
-> ParsecT [SectionArg Position] () Identity ()
-> ParsecT
     [SectionArg Position] () Identity (ConfVar -> Condition ConfVar)
forall a b.
a
-> ParsecT [SectionArg Position] () Identity b
-> ParsecT [SectionArg Position] () Identity a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ByteString -> ParsecT [SectionArg Position] () Identity ()
forall {u}.
ByteString -> ParsecT [SectionArg Position] u Identity ()
string ByteString
"impl" ParsecT
  [SectionArg Position] () Identity (ConfVar -> Condition ConfVar)
-> ParsecT [SectionArg Position] () Identity ConfVar
-> Parsec [SectionArg Position] () (Condition ConfVar)
forall a b.
ParsecT [SectionArg Position] () Identity (a -> b)
-> ParsecT [SectionArg Position] () Identity a
-> ParsecT [SectionArg Position] () Identity b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT [SectionArg Position] () Identity ConfVar
-> ParsecT [SectionArg Position] () Identity ConfVar
forall {u} {a}.
ParsecT [SectionArg Position] u Identity a
-> ParsecT [SectionArg Position] u Identity a
parens ParsecT [SectionArg Position] () Identity ConfVar
implCond'

    implCond' :: ParsecT [SectionArg Position] () Identity ConfVar
implCond' =
      CompilerFlavor -> VersionRange -> ConfVar
Impl
        (CompilerFlavor -> VersionRange -> ConfVar)
-> ParsecT [SectionArg Position] () Identity CompilerFlavor
-> ParsecT
     [SectionArg Position] () Identity (VersionRange -> ConfVar)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT [SectionArg Position] () Identity CompilerFlavor
forall a. Parsec a => Parser a
fromParsec
        ParsecT [SectionArg Position] () Identity (VersionRange -> ConfVar)
-> ParsecT [SectionArg Position] () Identity VersionRange
-> ParsecT [SectionArg Position] () Identity ConfVar
forall a b.
ParsecT [SectionArg Position] () Identity (a -> b)
-> ParsecT [SectionArg Position] () Identity a
-> ParsecT [SectionArg Position] () Identity b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> VersionRange
-> ParsecT [SectionArg Position] () Identity VersionRange
-> ParsecT [SectionArg Position] () Identity VersionRange
forall s (m :: * -> *) t a u.
Stream s m t =>
a -> ParsecT s u m a -> ParsecT s u m a
P.option VersionRange
anyVersion ParsecT [SectionArg Position] () Identity VersionRange
versionRange

    version :: Parser Version
version = Parser Version
forall a. Parsec a => Parser a
fromParsec
    versionStar :: ParsecT [SectionArg Position] u Identity Version
versionStar = [Int] -> Version
mkVersion ([Int] -> Version)
-> ParsecT [SectionArg Position] u Identity [Int]
-> ParsecT [SectionArg Position] u Identity Version
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecParser [Int]
-> ParsecT [SectionArg Position] u Identity [Int]
forall {b} {u}.
ParsecParser b -> ParsecT [SectionArg Position] u Identity b
fromParsec' ParsecParser [Int]
versionStar' ParsecT [SectionArg Position] u Identity Version
-> ParsecT [SectionArg Position] u Identity ()
-> ParsecT [SectionArg Position] u Identity Version
forall a b.
ParsecT [SectionArg Position] u Identity a
-> ParsecT [SectionArg Position] u Identity b
-> ParsecT [SectionArg Position] u Identity a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* ByteString -> ParsecT [SectionArg Position] u Identity ()
forall {u}.
ByteString -> ParsecT [SectionArg Position] u Identity ()
oper ByteString
"*"
    versionStar' :: ParsecParser [Int]
versionStar' = ParsecParser Int -> ParsecParser [Int]
forall a. ParsecParser a -> ParsecParser [a]
forall (f :: * -> *) a. Alternative f => f a -> f [a]
some (ParsecParser Int
forall (m :: * -> *) a. (CharParsing m, Integral a) => m a
integral ParsecParser Int -> ParsecParser Char -> ParsecParser Int
forall a b. ParsecParser a -> ParsecParser b -> ParsecParser a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Char -> ParsecParser Char
forall (m :: * -> *). CharParsing m => Char -> m Char
char Char
'.')

    versionRange :: ParsecT [SectionArg Position] () Identity VersionRange
versionRange = ParsecT [SectionArg Position] () Identity VersionRange
expr
      where
        expr :: ParsecT [SectionArg Position] () Identity VersionRange
expr = (VersionRange -> VersionRange -> VersionRange)
-> NonEmpty VersionRange -> VersionRange
forall a. (a -> a -> a) -> NonEmpty a -> a
foldl1 VersionRange -> VersionRange -> VersionRange
unionVersionRanges (NonEmpty VersionRange -> VersionRange)
-> ParsecT
     [SectionArg Position] () Identity (NonEmpty VersionRange)
-> ParsecT [SectionArg Position] () Identity VersionRange
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT [SectionArg Position] () Identity VersionRange
-> ParsecT [SectionArg Position] () Identity ()
-> ParsecT
     [SectionArg Position] () Identity (NonEmpty VersionRange)
forall a sep. Parser a -> Parser sep -> Parser (NonEmpty a)
sepByNonEmpty ParsecT [SectionArg Position] () Identity VersionRange
term (ByteString -> ParsecT [SectionArg Position] () Identity ()
forall {u}.
ByteString -> ParsecT [SectionArg Position] u Identity ()
oper ByteString
"||")
        term :: ParsecT [SectionArg Position] () Identity VersionRange
term = (VersionRange -> VersionRange -> VersionRange)
-> NonEmpty VersionRange -> VersionRange
forall a. (a -> a -> a) -> NonEmpty a -> a
foldl1 VersionRange -> VersionRange -> VersionRange
intersectVersionRanges (NonEmpty VersionRange -> VersionRange)
-> ParsecT
     [SectionArg Position] () Identity (NonEmpty VersionRange)
-> ParsecT [SectionArg Position] () Identity VersionRange
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT [SectionArg Position] () Identity VersionRange
-> ParsecT [SectionArg Position] () Identity ()
-> ParsecT
     [SectionArg Position] () Identity (NonEmpty VersionRange)
forall a sep. Parser a -> Parser sep -> Parser (NonEmpty a)
sepByNonEmpty ParsecT [SectionArg Position] () Identity VersionRange
factor (ByteString -> ParsecT [SectionArg Position] () Identity ()
forall {u}.
ByteString -> ParsecT [SectionArg Position] u Identity ()
oper ByteString
"&&")

        factor :: ParsecT [SectionArg Position] () Identity VersionRange
factor =
          [ParsecT [SectionArg Position] () Identity VersionRange]
-> ParsecT [SectionArg Position] () Identity VersionRange
forall s (m :: * -> *) t u a.
Stream s m t =>
[ParsecT s u m a] -> ParsecT s u m a
P.choice ([ParsecT [SectionArg Position] () Identity VersionRange]
 -> ParsecT [SectionArg Position] () Identity VersionRange)
-> [ParsecT [SectionArg Position] () Identity VersionRange]
-> ParsecT [SectionArg Position] () Identity VersionRange
forall a b. (a -> b) -> a -> b
$
            ParsecT [SectionArg Position] () Identity VersionRange
-> ParsecT [SectionArg Position] () Identity VersionRange
forall {u} {a}.
ParsecT [SectionArg Position] u Identity a
-> ParsecT [SectionArg Position] u Identity a
parens ParsecT [SectionArg Position] () Identity VersionRange
expr
              ParsecT [SectionArg Position] () Identity VersionRange
-> [ParsecT [SectionArg Position] () Identity VersionRange]
-> [ParsecT [SectionArg Position] () Identity VersionRange]
forall a. a -> [a] -> [a]
: ParsecT [SectionArg Position] () Identity VersionRange
forall {u}. ParsecT [SectionArg Position] u Identity VersionRange
parseAnyVersion
              ParsecT [SectionArg Position] () Identity VersionRange
-> [ParsecT [SectionArg Position] () Identity VersionRange]
-> [ParsecT [SectionArg Position] () Identity VersionRange]
forall a. a -> [a] -> [a]
: ParsecT [SectionArg Position] () Identity VersionRange
forall {u}. ParsecT [SectionArg Position] u Identity VersionRange
parseNoVersion
              ParsecT [SectionArg Position] () Identity VersionRange
-> [ParsecT [SectionArg Position] () Identity VersionRange]
-> [ParsecT [SectionArg Position] () Identity VersionRange]
forall a. a -> [a] -> [a]
: ParsecT [SectionArg Position] () Identity VersionRange
forall {u}. ParsecT [SectionArg Position] u Identity VersionRange
parseWildcardRange
              ParsecT [SectionArg Position] () Identity VersionRange
-> [ParsecT [SectionArg Position] () Identity VersionRange]
-> [ParsecT [SectionArg Position] () Identity VersionRange]
forall a. a -> [a] -> [a]
: ((ByteString, Version -> VersionRange)
 -> ParsecT [SectionArg Position] () Identity VersionRange)
-> [(ByteString, Version -> VersionRange)]
-> [ParsecT [SectionArg Position] () Identity VersionRange]
forall a b. (a -> b) -> [a] -> [b]
map (ByteString, Version -> VersionRange)
-> ParsecT [SectionArg Position] () Identity VersionRange
forall {a}.
(ByteString, Version -> a)
-> ParsecT [SectionArg Position] () Identity a
parseRangeOp [(ByteString, Version -> VersionRange)]
rangeOps

        parseAnyVersion :: ParsecT [SectionArg Position] u Identity VersionRange
parseAnyVersion = VersionRange
anyVersion VersionRange
-> ParsecT [SectionArg Position] u Identity ()
-> ParsecT [SectionArg Position] u Identity VersionRange
forall a b.
a
-> ParsecT [SectionArg Position] u Identity b
-> ParsecT [SectionArg Position] u Identity a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ByteString -> ParsecT [SectionArg Position] u Identity ()
forall {u}.
ByteString -> ParsecT [SectionArg Position] u Identity ()
string ByteString
"-any"
        parseNoVersion :: ParsecT [SectionArg Position] u Identity VersionRange
parseNoVersion = VersionRange
noVersion VersionRange
-> ParsecT [SectionArg Position] u Identity ()
-> ParsecT [SectionArg Position] u Identity VersionRange
forall a b.
a
-> ParsecT [SectionArg Position] u Identity b
-> ParsecT [SectionArg Position] u Identity a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ByteString -> ParsecT [SectionArg Position] u Identity ()
forall {u}.
ByteString -> ParsecT [SectionArg Position] u Identity ()
string ByteString
"-none"

        parseWildcardRange :: ParsecT [SectionArg Position] u Identity VersionRange
parseWildcardRange = ParsecT [SectionArg Position] u Identity VersionRange
-> ParsecT [SectionArg Position] u Identity VersionRange
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
P.try (ParsecT [SectionArg Position] u Identity VersionRange
 -> ParsecT [SectionArg Position] u Identity VersionRange)
-> ParsecT [SectionArg Position] u Identity VersionRange
-> ParsecT [SectionArg Position] u Identity VersionRange
forall a b. (a -> b) -> a -> b
$ Version -> VersionRange
withinVersion (Version -> VersionRange)
-> ParsecT [SectionArg Position] u Identity ()
-> ParsecT
     [SectionArg Position] u Identity (Version -> VersionRange)
forall a b.
a
-> ParsecT [SectionArg Position] u Identity b
-> ParsecT [SectionArg Position] u Identity a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ByteString -> ParsecT [SectionArg Position] u Identity ()
forall {u}.
ByteString -> ParsecT [SectionArg Position] u Identity ()
oper ByteString
"==" ParsecT [SectionArg Position] u Identity (Version -> VersionRange)
-> ParsecT [SectionArg Position] u Identity Version
-> ParsecT [SectionArg Position] u Identity VersionRange
forall a b.
ParsecT [SectionArg Position] u Identity (a -> b)
-> ParsecT [SectionArg Position] u Identity a
-> ParsecT [SectionArg Position] u Identity b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT [SectionArg Position] u Identity Version
forall {u}. ParsecT [SectionArg Position] u Identity Version
versionStar

        parseRangeOp :: (ByteString, Version -> a)
-> ParsecT [SectionArg Position] () Identity a
parseRangeOp (ByteString
s, Version -> a
f) = ParsecT [SectionArg Position] () Identity a
-> ParsecT [SectionArg Position] () Identity a
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
P.try (Version -> a
f (Version -> a)
-> ParsecT [SectionArg Position] () Identity ()
-> ParsecT [SectionArg Position] () Identity (Version -> a)
forall a b.
a
-> ParsecT [SectionArg Position] () Identity b
-> ParsecT [SectionArg Position] () Identity a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ByteString -> ParsecT [SectionArg Position] () Identity ()
forall {u}.
ByteString -> ParsecT [SectionArg Position] u Identity ()
oper ByteString
s ParsecT [SectionArg Position] () Identity (Version -> a)
-> Parser Version -> ParsecT [SectionArg Position] () Identity a
forall a b.
ParsecT [SectionArg Position] () Identity (a -> b)
-> ParsecT [SectionArg Position] () Identity a
-> ParsecT [SectionArg Position] () Identity b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Version
version)
        rangeOps :: [(ByteString, Version -> VersionRange)]
rangeOps =
          [ (ByteString
"<", Version -> VersionRange
earlierVersion)
          , (ByteString
"<=", Version -> VersionRange
orEarlierVersion)
          , (ByteString
">", Version -> VersionRange
laterVersion)
          , (ByteString
">=", Version -> VersionRange
orLaterVersion)
          , (ByteString
"^>=", Version -> VersionRange
majorBoundVersion)
          , (ByteString
"==", Version -> VersionRange
thisVersion)
          ]

    -- Number token can have many dots in it: SecArgNum (Position 65 15) "7.6.1"
    identBS :: ParsecT [SectionArg Position] u Identity ByteString
identBS = (SectionArg Position -> Maybe ByteString)
-> ParsecT [SectionArg Position] u Identity ByteString
forall {a} {u}.
(SectionArg Position -> Maybe a)
-> ParsecT [SectionArg Position] u Identity a
tokenPrim ((SectionArg Position -> Maybe ByteString)
 -> ParsecT [SectionArg Position] u Identity ByteString)
-> (SectionArg Position -> Maybe ByteString)
-> ParsecT [SectionArg Position] u Identity ByteString
forall a b. (a -> b) -> a -> b
$ \SectionArg Position
t -> case SectionArg Position
t of
      SecArgName Position
_ ByteString
s -> ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just ByteString
s
      SectionArg Position
_ -> Maybe ByteString
forall a. Maybe a
Nothing

    boolLiteral' :: ParsecT [SectionArg Position] u Identity Bool
boolLiteral' = (SectionArg Position -> Maybe Bool)
-> ParsecT [SectionArg Position] u Identity Bool
forall {a} {u}.
(SectionArg Position -> Maybe a)
-> ParsecT [SectionArg Position] u Identity a
tokenPrim ((SectionArg Position -> Maybe Bool)
 -> ParsecT [SectionArg Position] u Identity Bool)
-> (SectionArg Position -> Maybe Bool)
-> ParsecT [SectionArg Position] u Identity Bool
forall a b. (a -> b) -> a -> b
$ \SectionArg Position
t -> case SectionArg Position
t of
      SecArgName Position
_ ByteString
s
        | ByteString
s ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString
"True" -> Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
True
        | ByteString
s ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString
"true" -> Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
True
        | ByteString
s ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString
"False" -> Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
False
        | ByteString
s ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString
"false" -> Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
False
      SectionArg Position
_ -> Maybe Bool
forall a. Maybe a
Nothing

    string :: ByteString -> ParsecT [SectionArg Position] u Identity ()
string ByteString
s = (SectionArg Position -> Maybe ())
-> ParsecT [SectionArg Position] u Identity ()
forall {a} {u}.
(SectionArg Position -> Maybe a)
-> ParsecT [SectionArg Position] u Identity a
tokenPrim ((SectionArg Position -> Maybe ())
 -> ParsecT [SectionArg Position] u Identity ())
-> (SectionArg Position -> Maybe ())
-> ParsecT [SectionArg Position] u Identity ()
forall a b. (a -> b) -> a -> b
$ \SectionArg Position
t -> case SectionArg Position
t of
      SecArgName Position
_ ByteString
s' | ByteString
s ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString
s' -> () -> Maybe ()
forall a. a -> Maybe a
Just ()
      SectionArg Position
_ -> Maybe ()
forall a. Maybe a
Nothing

    oper :: ByteString -> ParsecT [SectionArg Position] u Identity ()
oper ByteString
o = (SectionArg Position -> Maybe ())
-> ParsecT [SectionArg Position] u Identity ()
forall {a} {u}.
(SectionArg Position -> Maybe a)
-> ParsecT [SectionArg Position] u Identity a
tokenPrim ((SectionArg Position -> Maybe ())
 -> ParsecT [SectionArg Position] u Identity ())
-> (SectionArg Position -> Maybe ())
-> ParsecT [SectionArg Position] u Identity ()
forall a b. (a -> b) -> a -> b
$ \SectionArg Position
t -> case SectionArg Position
t of
      SecArgOther Position
_ ByteString
o' | ByteString
o ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString
o' -> () -> Maybe ()
forall a. a -> Maybe a
Just ()
      SectionArg Position
_ -> Maybe ()
forall a. Maybe a
Nothing

    parens :: ParsecT [SectionArg Position] u Identity a
-> ParsecT [SectionArg Position] u Identity a
parens = ParsecT [SectionArg Position] u Identity ()
-> ParsecT [SectionArg Position] u Identity ()
-> ParsecT [SectionArg Position] u Identity a
-> ParsecT [SectionArg Position] u Identity a
forall s (m :: * -> *) t u open close a.
Stream s m t =>
ParsecT s u m open
-> ParsecT s u m close -> ParsecT s u m a -> ParsecT s u m a
P.between (ByteString -> ParsecT [SectionArg Position] u Identity ()
forall {u}.
ByteString -> ParsecT [SectionArg Position] u Identity ()
oper ByteString
"(") (ByteString -> ParsecT [SectionArg Position] u Identity ()
forall {u}.
ByteString -> ParsecT [SectionArg Position] u Identity ()
oper ByteString
")")

    tokenPrim :: (SectionArg Position -> Maybe a)
-> ParsecT [SectionArg Position] u Identity a
tokenPrim = (SectionArg Position -> SourceName)
-> (SourcePos
    -> SectionArg Position -> [SectionArg Position] -> SourcePos)
-> (SectionArg Position -> Maybe a)
-> ParsecT [SectionArg Position] u Identity a
forall s (m :: * -> *) t a u.
Stream s m t =>
(t -> SourceName)
-> (SourcePos -> t -> s -> SourcePos)
-> (t -> Maybe a)
-> ParsecT s u m a
P.tokenPrim SectionArg Position -> SourceName
prettySectionArg SourcePos
-> SectionArg Position -> [SectionArg Position] -> SourcePos
forall {p} {p} {p}. p -> p -> p -> p
updatePosition
    -- TODO: check where the errors are reported
    updatePosition :: p -> p -> p -> p
updatePosition p
x p
_ p
_ = p
x
    prettySectionArg :: SectionArg Position -> SourceName
prettySectionArg = SectionArg Position -> SourceName
forall a. Show a => a -> SourceName
show

    fromParsec :: Parsec a => Parser a
    fromParsec :: forall a. Parsec a => Parser a
fromParsec = ParsecParser a -> ParsecT [SectionArg Position] () Identity a
forall {b} {u}.
ParsecParser b -> ParsecT [SectionArg Position] u Identity b
fromParsec' ParsecParser a
forall a (m :: * -> *). (Parsec a, CabalParsing m) => m a
forall (m :: * -> *). CabalParsing m => m a
parsec

    fromParsec' :: ParsecParser b -> ParsecT [SectionArg Position] u Identity b
fromParsec' ParsecParser b
p = do
      bs <- ParsecT [SectionArg Position] u Identity ByteString
forall {u}. ParsecT [SectionArg Position] u Identity ByteString
identBS
      let fls = ByteString -> FieldLineStream
fieldLineStreamFromBS ByteString
bs
      either (fail . show) pure (runParsecParser p "<fromParsec'>" fls)