{-# LANGUAGE Rank2Types #-}
-----------------------------------------------------------------------------
-- This module is meant to be local-only to Distribution...
{-# OPTIONS_HADDOCK hide #-}

-- |
-- Module      :  Distribution.Deprecated.ParseUtils
-- Copyright   :  (c) The University of Glasgow 2004
-- License     :  BSD3
--
-- Maintainer  :  cabal-devel@haskell.org
-- Portability :  portable
--
-- Utilities for parsing 'PackageDescription' and 'InstalledPackageInfo'.
--
-- The @.cabal@ file format is not trivial, especially with the introduction
-- of configurations and the section syntax that goes with that. This module
-- has a bunch of parsing functions that is used by the @.cabal@ parser and a
-- couple others. It has the parsing framework code and also little parsers for
-- many of the formats we get in various @.cabal@ file fields, like module
-- names, comma separated lists etc.
module Distribution.Deprecated.ParseUtils
  ( LineNo
  , PError (..)
  , PWarning (..)
  , locatedErrorMsg
  , syntaxError
  , warning
  , runP
  , runE
  , ParseResult (..)
  , parseFail
  , showPWarning
  , Field (..)
  , lineNo
  , FieldDescr (..)
  , readFields
  , parseHaskellString
  , parseTokenQ
  , parseSpaceList
  , parseOptCommaList
  , showFilePath
  , showToken
  , showFreeText
  , field
  , simpleField
  , monoidField
  , listField
  , listFieldWithSep
  , spaceListField
  , newLineListField
  , liftField
  , readPToMaybe
  , fieldParsec
  , simpleFieldParsec
  , monoidFieldParsec
  , listFieldParsec
  , commaListFieldParsec
  , commaNewLineListFieldParsec
  , UnrecFieldParser
  ) where

import Distribution.Client.Compat.Prelude hiding (get)
import Prelude ()

import Distribution.Deprecated.ReadP as ReadP hiding (get)

import Distribution.Pretty
import Distribution.ReadE
import Distribution.Utils.Generic

import System.FilePath (normalise)
import Text.PrettyPrint (Doc, comma, fsep, punctuate, sep)
import qualified Text.Read as Read

import qualified Control.Monad.Fail as Fail
import Distribution.Parsec (ParsecParser, parsecLeadingCommaList, parsecLeadingOptCommaList)

import qualified Data.ByteString as BS
import qualified Distribution.Fields as Fields
import qualified Distribution.Fields.Field as Fields
import qualified Distribution.Fields.LexerMonad as Fields
import qualified Distribution.Parsec as Parsec
import qualified Text.Parsec.Error as PE
import qualified Text.Parsec.Pos as PP

-- -----------------------------------------------------------------------------

type LineNo = Int

data PError
  = AmbiguousParse String LineNo
  | NoParse String LineNo
  | TabsError LineNo
  | FieldShouldBeStanza String LineNo
  | FromString String (Maybe LineNo)
  deriving (PError -> PError -> Bool
(PError -> PError -> Bool)
-> (PError -> PError -> Bool) -> Eq PError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PError -> PError -> Bool
== :: PError -> PError -> Bool
$c/= :: PError -> PError -> Bool
/= :: PError -> PError -> Bool
Eq, Int -> PError -> ShowS
[PError] -> ShowS
PError -> [Char]
(Int -> PError -> ShowS)
-> (PError -> [Char]) -> ([PError] -> ShowS) -> Show PError
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PError -> ShowS
showsPrec :: Int -> PError -> ShowS
$cshow :: PError -> [Char]
show :: PError -> [Char]
$cshowList :: [PError] -> ShowS
showList :: [PError] -> ShowS
Show)

data PWarning
  = PWarning String
  | UTFWarning LineNo String
  deriving (PWarning -> PWarning -> Bool
(PWarning -> PWarning -> Bool)
-> (PWarning -> PWarning -> Bool) -> Eq PWarning
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PWarning -> PWarning -> Bool
== :: PWarning -> PWarning -> Bool
$c/= :: PWarning -> PWarning -> Bool
/= :: PWarning -> PWarning -> Bool
Eq, Int -> PWarning -> ShowS
[PWarning] -> ShowS
PWarning -> [Char]
(Int -> PWarning -> ShowS)
-> (PWarning -> [Char]) -> ([PWarning] -> ShowS) -> Show PWarning
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PWarning -> ShowS
showsPrec :: Int -> PWarning -> ShowS
$cshow :: PWarning -> [Char]
show :: PWarning -> [Char]
$cshowList :: [PWarning] -> ShowS
showList :: [PWarning] -> ShowS
Show)

showPWarning :: FilePath -> PWarning -> String
showPWarning :: [Char] -> PWarning -> [Char]
showPWarning [Char]
fpath (PWarning [Char]
msg) =
  ShowS
normalise [Char]
fpath [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
": " [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
msg
showPWarning [Char]
fpath (UTFWarning Int
line [Char]
fname) =
  ShowS
normalise [Char]
fpath
    [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
":"
    [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> [Char]
forall a. Show a => a -> [Char]
show Int
line
    [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
": Invalid UTF-8 text in the '"
    [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
fname
    [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
"' field."

data ParseResult a = ParseFailed PError | ParseOk [PWarning] a
  deriving (Int -> ParseResult a -> ShowS
[ParseResult a] -> ShowS
ParseResult a -> [Char]
(Int -> ParseResult a -> ShowS)
-> (ParseResult a -> [Char])
-> ([ParseResult a] -> ShowS)
-> Show (ParseResult a)
forall a. Show a => Int -> ParseResult a -> ShowS
forall a. Show a => [ParseResult a] -> ShowS
forall a. Show a => ParseResult a -> [Char]
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> ParseResult a -> ShowS
showsPrec :: Int -> ParseResult a -> ShowS
$cshow :: forall a. Show a => ParseResult a -> [Char]
show :: ParseResult a -> [Char]
$cshowList :: forall a. Show a => [ParseResult a] -> ShowS
showList :: [ParseResult a] -> ShowS
Show)

instance Functor ParseResult where
  fmap :: forall a b. (a -> b) -> ParseResult a -> ParseResult b
fmap a -> b
_ (ParseFailed PError
err) = PError -> ParseResult b
forall a. PError -> ParseResult a
ParseFailed PError
err
  fmap a -> b
f (ParseOk [PWarning]
ws a
x) = [PWarning] -> b -> ParseResult b
forall a. [PWarning] -> a -> ParseResult a
ParseOk [PWarning]
ws (b -> ParseResult b) -> b -> ParseResult b
forall a b. (a -> b) -> a -> b
$ a -> b
f a
x

instance Applicative ParseResult where
  pure :: forall a. a -> ParseResult a
pure = [PWarning] -> a -> ParseResult a
forall a. [PWarning] -> a -> ParseResult a
ParseOk []
  <*> :: forall a b. ParseResult (a -> b) -> ParseResult a -> ParseResult b
(<*>) = ParseResult (a -> b) -> ParseResult a -> ParseResult b
forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
ap

instance Monad ParseResult where
  return :: forall a. a -> ParseResult a
return = a -> ParseResult a
forall a. a -> ParseResult a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
  ParseFailed PError
err >>= :: forall a b. ParseResult a -> (a -> ParseResult b) -> ParseResult b
>>= a -> ParseResult b
_ = PError -> ParseResult b
forall a. PError -> ParseResult a
ParseFailed PError
err
  ParseOk [PWarning]
ws a
x >>= a -> ParseResult b
f = case a -> ParseResult b
f a
x of
    ParseFailed PError
err -> PError -> ParseResult b
forall a. PError -> ParseResult a
ParseFailed PError
err
    ParseOk [PWarning]
ws' b
x' -> [PWarning] -> b -> ParseResult b
forall a. [PWarning] -> a -> ParseResult a
ParseOk ([PWarning]
ws' [PWarning] -> [PWarning] -> [PWarning]
forall a. [a] -> [a] -> [a]
++ [PWarning]
ws) b
x'

instance Foldable ParseResult where
  foldMap :: forall m a. Monoid m => (a -> m) -> ParseResult a -> m
foldMap a -> m
_ (ParseFailed PError
_) = m
forall a. Monoid a => a
mempty
  foldMap a -> m
f (ParseOk [PWarning]
_ a
x) = a -> m
f a
x

instance Traversable ParseResult where
  traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> ParseResult a -> f (ParseResult b)
traverse a -> f b
_ (ParseFailed PError
err) = ParseResult b -> f (ParseResult b)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PError -> ParseResult b
forall a. PError -> ParseResult a
ParseFailed PError
err)
  traverse a -> f b
f (ParseOk [PWarning]
ws a
x) = [PWarning] -> b -> ParseResult b
forall a. [PWarning] -> a -> ParseResult a
ParseOk [PWarning]
ws (b -> ParseResult b) -> f b -> f (ParseResult b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
x

instance Fail.MonadFail ParseResult where
  fail :: forall a. HasCallStack => [Char] -> ParseResult a
fail = [Char] -> ParseResult a
forall a. [Char] -> ParseResult a
parseResultFail

parseResultFail :: String -> ParseResult a
parseResultFail :: forall a. [Char] -> ParseResult a
parseResultFail [Char]
s = PError -> ParseResult a
forall a. PError -> ParseResult a
parseFail ([Char] -> Maybe Int -> PError
FromString [Char]
s Maybe Int
forall a. Maybe a
Nothing)

parseFail :: PError -> ParseResult a
parseFail :: forall a. PError -> ParseResult a
parseFail = PError -> ParseResult a
forall a. PError -> ParseResult a
ParseFailed

runP :: LineNo -> String -> ReadP a a -> String -> ParseResult a
runP :: forall a. Int -> [Char] -> ReadP a a -> [Char] -> ParseResult a
runP Int
line [Char]
fieldname ReadP a a
p [Char]
s =
  case [a
x | (a
x, [Char]
"") <- [(a, [Char])]
results] of
    [a
a] -> [PWarning] -> a -> ParseResult a
forall a. [PWarning] -> a -> ParseResult a
ParseOk (Int -> [Char] -> [Char] -> [PWarning]
utf8Warnings Int
line [Char]
fieldname [Char]
s) a
a
    -- TODO: what is this double parse thing all about?
    --      Can't we just do the all isSpace test the first time?
    [] -> case [a
x | (a
x, [Char]
ys) <- [(a, [Char])]
results, (Char -> Bool) -> [Char] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Char -> Bool
isSpace [Char]
ys] of
      [a
a] -> [PWarning] -> a -> ParseResult a
forall a. [PWarning] -> a -> ParseResult a
ParseOk (Int -> [Char] -> [Char] -> [PWarning]
utf8Warnings Int
line [Char]
fieldname [Char]
s) a
a
      [] -> PError -> ParseResult a
forall a. PError -> ParseResult a
ParseFailed ([Char] -> Int -> PError
NoParse [Char]
fieldname Int
line)
      [a]
_ -> PError -> ParseResult a
forall a. PError -> ParseResult a
ParseFailed ([Char] -> Int -> PError
AmbiguousParse [Char]
fieldname Int
line)
    [a]
_ -> PError -> ParseResult a
forall a. PError -> ParseResult a
ParseFailed ([Char] -> Int -> PError
AmbiguousParse [Char]
fieldname Int
line)
  where
    results :: [(a, [Char])]
results = ReadP a a -> ReadS a
forall a. ReadP a a -> ReadS a
readP_to_S ReadP a a
p [Char]
s

runE :: LineNo -> String -> ReadE a -> String -> ParseResult a
runE :: forall a. Int -> [Char] -> ReadE a -> [Char] -> ParseResult a
runE Int
line [Char]
fieldname ReadE a
p [Char]
s =
  case ReadE a -> [Char] -> Either [Char] a
forall a. ReadE a -> [Char] -> Either [Char] a
runReadE ReadE a
p [Char]
s of
    Right a
a -> [PWarning] -> a -> ParseResult a
forall a. [PWarning] -> a -> ParseResult a
ParseOk (Int -> [Char] -> [Char] -> [PWarning]
utf8Warnings Int
line [Char]
fieldname [Char]
s) a
a
    Left [Char]
e ->
      Int -> [Char] -> ParseResult a
forall a. Int -> [Char] -> ParseResult a
syntaxError Int
line ([Char] -> ParseResult a) -> [Char] -> ParseResult a
forall a b. (a -> b) -> a -> b
$
        [Char]
"Parse of field '" [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
fieldname [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
"' failed (" [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
e [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
"): " [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
s

utf8Warnings :: LineNo -> String -> String -> [PWarning]
utf8Warnings :: Int -> [Char] -> [Char] -> [PWarning]
utf8Warnings Int
line [Char]
fieldname [Char]
s =
  Int -> [PWarning] -> [PWarning]
forall a. Int -> [a] -> [a]
take
    Int
1
    [ Int -> [Char] -> PWarning
UTFWarning Int
n [Char]
fieldname
    | (Int
n, [Char]
l) <- [Int] -> [[Char]] -> [(Int, [Char])]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
line ..] ([Char] -> [[Char]]
lines [Char]
s)
    , Char
'\xfffd' Char -> [Char] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Char]
l
    ]

locatedErrorMsg :: PError -> (Maybe LineNo, String)
locatedErrorMsg :: PError -> (Maybe Int, [Char])
locatedErrorMsg (AmbiguousParse [Char]
f Int
n) =
  ( Int -> Maybe Int
forall a. a -> Maybe a
Just Int
n
  , [Char]
"Ambiguous parse in field '" [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
f [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
"'."
  )
locatedErrorMsg (NoParse [Char]
f Int
n) =
  ( Int -> Maybe Int
forall a. a -> Maybe a
Just Int
n
  , [Char]
"Parse of field '" [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
f [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
"' failed."
  )
locatedErrorMsg (TabsError Int
n) = (Int -> Maybe Int
forall a. a -> Maybe a
Just Int
n, [Char]
"Tab used as indentation.")
locatedErrorMsg (FieldShouldBeStanza [Char]
name Int
lineNumber) =
  ( Int -> Maybe Int
forall a. a -> Maybe a
Just Int
lineNumber
  , [Char]
"'" [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
name [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
"' is a stanza, not a field. Remove the trailing ':' to parse a stanza."
  )
locatedErrorMsg (FromString [Char]
s Maybe Int
n) = (Maybe Int
n, [Char]
s)

syntaxError :: LineNo -> String -> ParseResult a
syntaxError :: forall a. Int -> [Char] -> ParseResult a
syntaxError Int
n [Char]
s = PError -> ParseResult a
forall a. PError -> ParseResult a
ParseFailed (PError -> ParseResult a) -> PError -> ParseResult a
forall a b. (a -> b) -> a -> b
$ [Char] -> Maybe Int -> PError
FromString [Char]
s (Int -> Maybe Int
forall a. a -> Maybe a
Just Int
n)

warning :: String -> ParseResult ()
warning :: [Char] -> ParseResult ()
warning [Char]
s = [PWarning] -> () -> ParseResult ()
forall a. [PWarning] -> a -> ParseResult a
ParseOk [[Char] -> PWarning
PWarning [Char]
s] ()

-- | Field descriptor.  The parameter @a@ parameterizes over where the field's
--   value is stored in.
data FieldDescr a = FieldDescr
  { forall a. FieldDescr a -> [Char]
fieldName :: String
  , forall a. FieldDescr a -> a -> Doc
fieldGet :: a -> Doc
  , forall a. FieldDescr a -> Int -> [Char] -> a -> ParseResult a
fieldSet :: LineNo -> String -> a -> ParseResult a
  -- ^ @fieldSet n str x@ Parses the field value from the given input
  -- string @str@ and stores the result in @x@ if the parse was
  -- successful.  Otherwise, reports an error on line number @n@.
  }

field :: String -> (a -> Doc) -> ReadP a a -> FieldDescr a
field :: forall a. [Char] -> (a -> Doc) -> ReadP a a -> FieldDescr a
field [Char]
name a -> Doc
showF ReadP a a
readF =
  [Char]
-> (a -> Doc)
-> (Int -> [Char] -> a -> ParseResult a)
-> FieldDescr a
forall a.
[Char]
-> (a -> Doc)
-> (Int -> [Char] -> a -> ParseResult a)
-> FieldDescr a
FieldDescr [Char]
name a -> Doc
showF (\Int
line [Char]
val a
_st -> Int -> [Char] -> ReadP a a -> [Char] -> ParseResult a
forall a. Int -> [Char] -> ReadP a a -> [Char] -> ParseResult a
runP Int
line [Char]
name ReadP a a
readF [Char]
val)

fieldParsec :: String -> (a -> Doc) -> ParsecParser a -> FieldDescr a
fieldParsec :: forall a. [Char] -> (a -> Doc) -> ParsecParser a -> FieldDescr a
fieldParsec [Char]
name a -> Doc
showF ParsecParser a
readF =
  [Char]
-> (a -> Doc)
-> (Int -> [Char] -> a -> ParseResult a)
-> FieldDescr a
forall a.
[Char]
-> (a -> Doc)
-> (Int -> [Char] -> a -> ParseResult a)
-> FieldDescr a
FieldDescr [Char]
name a -> Doc
showF ((Int -> [Char] -> a -> ParseResult a) -> FieldDescr a)
-> (Int -> [Char] -> a -> ParseResult a) -> FieldDescr a
forall a b. (a -> b) -> a -> b
$ \Int
line [Char]
val a
_st -> case ParsecParser a -> [Char] -> Either [Char] a
forall a. ParsecParser a -> [Char] -> Either [Char] a
explicitEitherParsec ParsecParser a
readF [Char]
val of
    Left [Char]
err -> PError -> ParseResult a
forall a. PError -> ParseResult a
ParseFailed ([Char] -> Maybe Int -> PError
FromString [Char]
err (Int -> Maybe Int
forall a. a -> Maybe a
Just Int
line))
    Right a
x -> [PWarning] -> a -> ParseResult a
forall a. [PWarning] -> a -> ParseResult a
ParseOk [] a
x

-- Lift a field descriptor storing into an 'a' to a field descriptor storing
-- into a 'b'.
liftField :: (b -> a) -> (a -> b -> b) -> FieldDescr a -> FieldDescr b
liftField :: forall b a.
(b -> a) -> (a -> b -> b) -> FieldDescr a -> FieldDescr b
liftField b -> a
get a -> b -> b
set (FieldDescr [Char]
name a -> Doc
showF Int -> [Char] -> a -> ParseResult a
parseF) =
  [Char]
-> (b -> Doc)
-> (Int -> [Char] -> b -> ParseResult b)
-> FieldDescr b
forall a.
[Char]
-> (a -> Doc)
-> (Int -> [Char] -> a -> ParseResult a)
-> FieldDescr a
FieldDescr
    [Char]
name
    (a -> Doc
showF (a -> Doc) -> (b -> a) -> b -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> a
get)
    ( \Int
line [Char]
str b
b -> do
        a <- Int -> [Char] -> a -> ParseResult a
parseF Int
line [Char]
str (b -> a
get b
b)
        return (set a b)
    )

-- Parser combinator for simple fields.  Takes a field name, a pretty printer,
-- a parser function, an accessor, and a setter, returns a FieldDescr over the
-- compoid structure.
simpleField
  :: String
  -> (a -> Doc)
  -> ReadP a a
  -> (b -> a)
  -> (a -> b -> b)
  -> FieldDescr b
simpleField :: forall a b.
[Char]
-> (a -> Doc)
-> ReadP a a
-> (b -> a)
-> (a -> b -> b)
-> FieldDescr b
simpleField [Char]
name a -> Doc
showF ReadP a a
readF b -> a
get a -> b -> b
set =
  (b -> a) -> (a -> b -> b) -> FieldDescr a -> FieldDescr b
forall b a.
(b -> a) -> (a -> b -> b) -> FieldDescr a -> FieldDescr b
liftField b -> a
get a -> b -> b
set (FieldDescr a -> FieldDescr b) -> FieldDescr a -> FieldDescr b
forall a b. (a -> b) -> a -> b
$ [Char] -> (a -> Doc) -> ReadP a a -> FieldDescr a
forall a. [Char] -> (a -> Doc) -> ReadP a a -> FieldDescr a
field [Char]
name a -> Doc
showF ReadP a a
readF

simpleFieldParsec
  :: String
  -> (a -> Doc)
  -> ParsecParser a
  -> (b -> a)
  -> (a -> b -> b)
  -> FieldDescr b
simpleFieldParsec :: forall a b.
[Char]
-> (a -> Doc)
-> ParsecParser a
-> (b -> a)
-> (a -> b -> b)
-> FieldDescr b
simpleFieldParsec [Char]
name a -> Doc
showF ParsecParser a
readF b -> a
get a -> b -> b
set =
  (b -> a) -> (a -> b -> b) -> FieldDescr a -> FieldDescr b
forall b a.
(b -> a) -> (a -> b -> b) -> FieldDescr a -> FieldDescr b
liftField b -> a
get a -> b -> b
set (FieldDescr a -> FieldDescr b) -> FieldDescr a -> FieldDescr b
forall a b. (a -> b) -> a -> b
$ [Char] -> (a -> Doc) -> ParsecParser a -> FieldDescr a
forall a. [Char] -> (a -> Doc) -> ParsecParser a -> FieldDescr a
fieldParsec [Char]
name a -> Doc
showF ParsecParser a
readF

monoidField
  :: Semigroup a
  => String
  -> (a -> Doc)
  -> ReadP a a
  -> (b -> a)
  -> (a -> b -> b)
  -> FieldDescr b
monoidField :: forall a b.
Semigroup a =>
[Char]
-> (a -> Doc)
-> ReadP a a
-> (b -> a)
-> (a -> b -> b)
-> FieldDescr b
monoidField [Char]
name a -> Doc
showF ReadP a a
readF b -> a
get a -> b -> b
set =
  (b -> a) -> (a -> b -> b) -> FieldDescr a -> FieldDescr b
forall b a.
(b -> a) -> (a -> b -> b) -> FieldDescr a -> FieldDescr b
liftField b -> a
get a -> b -> b
set' (FieldDescr a -> FieldDescr b) -> FieldDescr a -> FieldDescr b
forall a b. (a -> b) -> a -> b
$ [Char] -> (a -> Doc) -> ReadP a a -> FieldDescr a
forall a. [Char] -> (a -> Doc) -> ReadP a a -> FieldDescr a
field [Char]
name a -> Doc
showF ReadP a a
readF
  where
    set' :: a -> b -> b
set' a
xs b
b = a -> b -> b
set (b -> a
get b
b a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
xs) b
b

monoidFieldParsec
  :: Semigroup a
  => String
  -> (a -> Doc)
  -> ParsecParser a
  -> (b -> a)
  -> (a -> b -> b)
  -> FieldDescr b
monoidFieldParsec :: forall a b.
Semigroup a =>
[Char]
-> (a -> Doc)
-> ParsecParser a
-> (b -> a)
-> (a -> b -> b)
-> FieldDescr b
monoidFieldParsec [Char]
name a -> Doc
showF ParsecParser a
readF b -> a
get a -> b -> b
set =
  (b -> a) -> (a -> b -> b) -> FieldDescr a -> FieldDescr b
forall b a.
(b -> a) -> (a -> b -> b) -> FieldDescr a -> FieldDescr b
liftField b -> a
get a -> b -> b
set' (FieldDescr a -> FieldDescr b) -> FieldDescr a -> FieldDescr b
forall a b. (a -> b) -> a -> b
$ [Char] -> (a -> Doc) -> ParsecParser a -> FieldDescr a
forall a. [Char] -> (a -> Doc) -> ParsecParser a -> FieldDescr a
fieldParsec [Char]
name a -> Doc
showF ParsecParser a
readF
  where
    set' :: a -> b -> b
set' a
xs b
b = a -> b -> b
set (b -> a
get b
b a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
xs) b
b

commaListFieldWithSepParsec
  :: Separator
  -> String
  -> (a -> Doc)
  -> ParsecParser a
  -> (b -> [a])
  -> ([a] -> b -> b)
  -> FieldDescr b
commaListFieldWithSepParsec :: forall a b.
Separator
-> [Char]
-> (a -> Doc)
-> ParsecParser a
-> (b -> [a])
-> ([a] -> b -> b)
-> FieldDescr b
commaListFieldWithSepParsec Separator
separator [Char]
name a -> Doc
showF ParsecParser a
readF b -> [a]
get [a] -> b -> b
set =
  (b -> [a]) -> ([a] -> b -> b) -> FieldDescr [a] -> FieldDescr b
forall b a.
(b -> a) -> (a -> b -> b) -> FieldDescr a -> FieldDescr b
liftField b -> [a]
get [a] -> b -> b
set' (FieldDescr [a] -> FieldDescr b) -> FieldDescr [a] -> FieldDescr b
forall a b. (a -> b) -> a -> b
$
    [Char] -> ([a] -> Doc) -> ParsecParser [a] -> FieldDescr [a]
forall a. [Char] -> (a -> Doc) -> ParsecParser a -> FieldDescr a
fieldParsec [Char]
name [a] -> Doc
showF' (ParsecParser a -> ParsecParser [a]
forall (m :: * -> *) a. CabalParsing m => m a -> m [a]
parsecLeadingCommaList ParsecParser a
readF)
  where
    set' :: [a] -> b -> b
set' [a]
xs b
b = [a] -> b -> b
set (b -> [a]
get b
b [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ [a]
xs) b
b
    showF' :: [a] -> Doc
showF' = Separator
separator Separator -> ([a] -> [Doc]) -> [a] -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Doc -> [Doc] -> [Doc]
punctuate Doc
comma ([Doc] -> [Doc]) -> ([a] -> [Doc]) -> [a] -> [Doc]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Doc) -> [a] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map a -> Doc
showF

commaListFieldParsec
  :: String
  -> (a -> Doc)
  -> ParsecParser a
  -> (b -> [a])
  -> ([a] -> b -> b)
  -> FieldDescr b
commaListFieldParsec :: forall a b.
[Char]
-> (a -> Doc)
-> ParsecParser a
-> (b -> [a])
-> ([a] -> b -> b)
-> FieldDescr b
commaListFieldParsec = Separator
-> [Char]
-> (a -> Doc)
-> ParsecParser a
-> (b -> [a])
-> ([a] -> b -> b)
-> FieldDescr b
forall a b.
Separator
-> [Char]
-> (a -> Doc)
-> ParsecParser a
-> (b -> [a])
-> ([a] -> b -> b)
-> FieldDescr b
commaListFieldWithSepParsec Separator
fsep

commaNewLineListFieldParsec
  :: String
  -> (a -> Doc)
  -> ParsecParser a
  -> (b -> [a])
  -> ([a] -> b -> b)
  -> FieldDescr b
commaNewLineListFieldParsec :: forall a b.
[Char]
-> (a -> Doc)
-> ParsecParser a
-> (b -> [a])
-> ([a] -> b -> b)
-> FieldDescr b
commaNewLineListFieldParsec = Separator
-> [Char]
-> (a -> Doc)
-> ParsecParser a
-> (b -> [a])
-> ([a] -> b -> b)
-> FieldDescr b
forall a b.
Separator
-> [Char]
-> (a -> Doc)
-> ParsecParser a
-> (b -> [a])
-> ([a] -> b -> b)
-> FieldDescr b
commaListFieldWithSepParsec Separator
sep

spaceListField
  :: String
  -> (a -> Doc)
  -> ReadP [a] a
  -> (b -> [a])
  -> ([a] -> b -> b)
  -> FieldDescr b
spaceListField :: forall a b.
[Char]
-> (a -> Doc)
-> ReadP [a] a
-> (b -> [a])
-> ([a] -> b -> b)
-> FieldDescr b
spaceListField [Char]
name a -> Doc
showF ReadP [a] a
readF b -> [a]
get [a] -> b -> b
set =
  (b -> [a]) -> ([a] -> b -> b) -> FieldDescr [a] -> FieldDescr b
forall b a.
(b -> a) -> (a -> b -> b) -> FieldDescr a -> FieldDescr b
liftField b -> [a]
get [a] -> b -> b
set' (FieldDescr [a] -> FieldDescr b) -> FieldDescr [a] -> FieldDescr b
forall a b. (a -> b) -> a -> b
$
    [Char] -> ([a] -> Doc) -> ReadP [a] [a] -> FieldDescr [a]
forall a. [Char] -> (a -> Doc) -> ReadP a a -> FieldDescr a
field [Char]
name [a] -> Doc
showF' (ReadP [a] a -> ReadP [a] [a]
forall r a. ReadP r a -> ReadP r [a]
parseSpaceList ReadP [a] a
readF)
  where
    set' :: [a] -> b -> b
set' [a]
xs b
b = [a] -> b -> b
set (b -> [a]
get b
b [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ [a]
xs) b
b
    showF' :: [a] -> Doc
showF' = Separator
fsep Separator -> ([a] -> [Doc]) -> [a] -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Doc) -> [a] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map a -> Doc
showF

-- this is a different definition from listField, like
-- commaNewLineListField it pretty prints on multiple lines
newLineListField
  :: String
  -> (a -> Doc)
  -> ReadP [a] a
  -> (b -> [a])
  -> ([a] -> b -> b)
  -> FieldDescr b
newLineListField :: forall a b.
[Char]
-> (a -> Doc)
-> ReadP [a] a
-> (b -> [a])
-> ([a] -> b -> b)
-> FieldDescr b
newLineListField = Separator
-> [Char]
-> (a -> Doc)
-> ReadP [a] a
-> (b -> [a])
-> ([a] -> b -> b)
-> FieldDescr b
forall a b.
Separator
-> [Char]
-> (a -> Doc)
-> ReadP [a] a
-> (b -> [a])
-> ([a] -> b -> b)
-> FieldDescr b
listFieldWithSep Separator
sep

listFieldWithSep
  :: Separator
  -> String
  -> (a -> Doc)
  -> ReadP [a] a
  -> (b -> [a])
  -> ([a] -> b -> b)
  -> FieldDescr b
listFieldWithSep :: forall a b.
Separator
-> [Char]
-> (a -> Doc)
-> ReadP [a] a
-> (b -> [a])
-> ([a] -> b -> b)
-> FieldDescr b
listFieldWithSep Separator
separator [Char]
name a -> Doc
showF ReadP [a] a
readF b -> [a]
get [a] -> b -> b
set =
  (b -> [a]) -> ([a] -> b -> b) -> FieldDescr [a] -> FieldDescr b
forall b a.
(b -> a) -> (a -> b -> b) -> FieldDescr a -> FieldDescr b
liftField b -> [a]
get [a] -> b -> b
set' (FieldDescr [a] -> FieldDescr b) -> FieldDescr [a] -> FieldDescr b
forall a b. (a -> b) -> a -> b
$
    [Char] -> ([a] -> Doc) -> ReadP [a] [a] -> FieldDescr [a]
forall a. [Char] -> (a -> Doc) -> ReadP a a -> FieldDescr a
field [Char]
name [a] -> Doc
showF' (ReadP [a] a -> ReadP [a] [a]
forall r a. ReadP r a -> ReadP r [a]
parseOptCommaList ReadP [a] a
readF)
  where
    set' :: [a] -> b -> b
set' [a]
xs b
b = [a] -> b -> b
set (b -> [a]
get b
b [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ [a]
xs) b
b
    showF' :: [a] -> Doc
showF' = Separator
separator Separator -> ([a] -> [Doc]) -> [a] -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Doc) -> [a] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map a -> Doc
showF

listFieldWithSepParsec
  :: Separator
  -> String
  -> (a -> Doc)
  -> ParsecParser a
  -> (b -> [a])
  -> ([a] -> b -> b)
  -> FieldDescr b
listFieldWithSepParsec :: forall a b.
Separator
-> [Char]
-> (a -> Doc)
-> ParsecParser a
-> (b -> [a])
-> ([a] -> b -> b)
-> FieldDescr b
listFieldWithSepParsec Separator
separator [Char]
name a -> Doc
showF ParsecParser a
readF b -> [a]
get [a] -> b -> b
set =
  (b -> [a]) -> ([a] -> b -> b) -> FieldDescr [a] -> FieldDescr b
forall b a.
(b -> a) -> (a -> b -> b) -> FieldDescr a -> FieldDescr b
liftField b -> [a]
get [a] -> b -> b
set' (FieldDescr [a] -> FieldDescr b) -> FieldDescr [a] -> FieldDescr b
forall a b. (a -> b) -> a -> b
$
    [Char] -> ([a] -> Doc) -> ParsecParser [a] -> FieldDescr [a]
forall a. [Char] -> (a -> Doc) -> ParsecParser a -> FieldDescr a
fieldParsec [Char]
name [a] -> Doc
showF' (ParsecParser a -> ParsecParser [a]
forall (m :: * -> *) a. CabalParsing m => m a -> m [a]
parsecLeadingOptCommaList ParsecParser a
readF)
  where
    set' :: [a] -> b -> b
set' [a]
xs b
b = [a] -> b -> b
set (b -> [a]
get b
b [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ [a]
xs) b
b
    showF' :: [a] -> Doc
showF' = Separator
separator Separator -> ([a] -> [Doc]) -> [a] -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Doc) -> [a] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map a -> Doc
showF

listField
  :: String
  -> (a -> Doc)
  -> ReadP [a] a
  -> (b -> [a])
  -> ([a] -> b -> b)
  -> FieldDescr b
listField :: forall a b.
[Char]
-> (a -> Doc)
-> ReadP [a] a
-> (b -> [a])
-> ([a] -> b -> b)
-> FieldDescr b
listField = Separator
-> [Char]
-> (a -> Doc)
-> ReadP [a] a
-> (b -> [a])
-> ([a] -> b -> b)
-> FieldDescr b
forall a b.
Separator
-> [Char]
-> (a -> Doc)
-> ReadP [a] a
-> (b -> [a])
-> ([a] -> b -> b)
-> FieldDescr b
listFieldWithSep Separator
fsep

listFieldParsec
  :: String
  -> (a -> Doc)
  -> ParsecParser a
  -> (b -> [a])
  -> ([a] -> b -> b)
  -> FieldDescr b
listFieldParsec :: forall a b.
[Char]
-> (a -> Doc)
-> ParsecParser a
-> (b -> [a])
-> ([a] -> b -> b)
-> FieldDescr b
listFieldParsec = Separator
-> [Char]
-> (a -> Doc)
-> ParsecParser a
-> (b -> [a])
-> ([a] -> b -> b)
-> FieldDescr b
forall a b.
Separator
-> [Char]
-> (a -> Doc)
-> ParsecParser a
-> (b -> [a])
-> ([a] -> b -> b)
-> FieldDescr b
listFieldWithSepParsec Separator
fsep

-- | The type of a function which, given a name-value pair of an
--   unrecognized field, and the current structure being built,
--   decides whether to incorporate the unrecognized field
--   (by returning  Just x, where x is a possibly modified version
--   of the structure being built), or not (by returning Nothing).
type UnrecFieldParser a = (String, String) -> a -> Maybe a

------------------------------------------------------------------------------

-- The data type for our three syntactic categories
data Field
  = -- | A regular @<property>: <value>@ field
    F LineNo String String
  | -- | A section with a name and possible parameter.  The syntactic
    -- structure is:
    --
    -- @
    --   <sectionname> <arg> {
    --     <field>*
    --   }
    -- @
    Section LineNo String String [Field]
  deriving
    ( Int -> Field -> ShowS
[Field] -> ShowS
Field -> [Char]
(Int -> Field -> ShowS)
-> (Field -> [Char]) -> ([Field] -> ShowS) -> Show Field
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Field -> ShowS
showsPrec :: Int -> Field -> ShowS
$cshow :: Field -> [Char]
show :: Field -> [Char]
$cshowList :: [Field] -> ShowS
showList :: [Field] -> ShowS
Show
    , Field -> Field -> Bool
(Field -> Field -> Bool) -> (Field -> Field -> Bool) -> Eq Field
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Field -> Field -> Bool
== :: Field -> Field -> Bool
$c/= :: Field -> Field -> Bool
/= :: Field -> Field -> Bool
Eq -- for testing
    )

lineNo :: Field -> LineNo
lineNo :: Field -> Int
lineNo (F Int
n [Char]
_ [Char]
_) = Int
n
lineNo (Section Int
n [Char]
_ [Char]
_ [Field]
_) = Int
n

readFields :: BS.ByteString -> ParseResult [Field]
readFields :: ByteString -> ParseResult [Field]
readFields ByteString
input = case ByteString -> Either ParseError ([Field Position], [LexWarning])
Fields.readFields' ByteString
input of
  Right ([Field Position]
fs, [LexWarning]
ws) ->
    [PWarning] -> [Field] -> ParseResult [Field]
forall a. [PWarning] -> a -> ParseResult a
ParseOk
      [[Char] -> PWarning
PWarning [Char]
msg | Fields.PWarning PWarnType
_ Position
_ [Char]
msg <- [LexWarning] -> [PWarning]
Fields.toPWarnings [LexWarning]
ws]
      ([Field Position] -> [Field]
legacyFields [Field Position]
fs)
  Left ParseError
perr ->
    PError -> ParseResult [Field]
forall a. PError -> ParseResult a
ParseFailed (PError -> ParseResult [Field]) -> PError -> ParseResult [Field]
forall a b. (a -> b) -> a -> b
$
      [Char] -> Int -> PError
NoParse
        ( [Char]
-> [Char] -> [Char] -> [Char] -> [Char] -> [Message] -> [Char]
PE.showErrorMessages
            [Char]
"or"
            [Char]
"unknown parse error"
            [Char]
"expecting"
            [Char]
"unexpected"
            [Char]
"end of file"
            (ParseError -> [Message]
PE.errorMessages ParseError
perr)
        )
        (SourcePos -> Int
PP.sourceLine SourcePos
pos)
    where
      pos :: SourcePos
pos = ParseError -> SourcePos
PE.errorPos ParseError
perr

legacyFields :: [Fields.Field Parsec.Position] -> [Field]
legacyFields :: [Field Position] -> [Field]
legacyFields = (Field Position -> Field) -> [Field Position] -> [Field]
forall a b. (a -> b) -> [a] -> [b]
map Field Position -> Field
legacyField

legacyField :: Fields.Field Parsec.Position -> Field
legacyField :: Field Position -> Field
legacyField (Fields.Field (Fields.Name Position
pos ByteString
name) [FieldLine Position]
fls) =
  Int -> [Char] -> [Char] -> Field
F (Position -> Int
posToLineNo Position
pos) (ByteString -> [Char]
fromUTF8BS ByteString
name) ([FieldLine Position] -> [Char]
forall ann. [FieldLine ann] -> [Char]
Fields.fieldLinesToString [FieldLine Position]
fls)
legacyField (Fields.Section (Fields.Name Position
pos ByteString
name) [SectionArg Position]
args [Field Position]
fs) =
  Int -> [Char] -> [Char] -> [Field] -> Field
Section (Position -> Int
posToLineNo Position
pos) (ByteString -> [Char]
fromUTF8BS ByteString
name) ([SectionArg Position] -> [Char]
forall ann. [SectionArg ann] -> [Char]
Fields.sectionArgsToString [SectionArg Position]
args) ([Field Position] -> [Field]
legacyFields [Field Position]
fs)

posToLineNo :: Parsec.Position -> LineNo
posToLineNo :: Position -> Int
posToLineNo (Parsec.Position Int
row Int
_) = Int
row

------------------------------------------------------------------------------

-- urgh, we can't define optQuotes :: ReadP r a -> ReadP r a
-- because the "compat" version of ReadP isn't quite powerful enough.  In
-- particular, the type of <++ is ReadP r r -> ReadP r a -> ReadP r a
-- Hence the trick above to make 'lic' polymorphic.

-- Different than the naive version. it turns out Read instance for String accepts
-- the ['a', 'b'] syntax, which we do not want. In particular it messes
-- up any token starting with [].
parseHaskellString :: ReadP r String
parseHaskellString :: forall r. ReadP r [Char]
parseHaskellString =
  ReadS [Char] -> ReadP r [Char]
forall a r. ReadS a -> ReadP r a
readS_to_P (ReadS [Char] -> ReadP r [Char]) -> ReadS [Char] -> ReadP r [Char]
forall a b. (a -> b) -> a -> b
$
    ReadPrec [Char] -> Int -> ReadS [Char]
forall a. ReadPrec a -> Int -> ReadS a
Read.readPrec_to_S (do Read.String s <- ReadPrec Lexeme
Read.lexP; return s) Int
0

parseTokenQ :: ReadP r String
parseTokenQ :: forall r. ReadP r [Char]
parseTokenQ = ReadP [Char] [Char]
forall r. ReadP r [Char]
parseHaskellString ReadP [Char] [Char] -> ReadP r [Char] -> ReadP r [Char]
forall a r. ReadP a a -> ReadP r a -> ReadP r a
<++ (Char -> Bool) -> ReadP r [Char]
forall r. (Char -> Bool) -> ReadP r [Char]
munch1 (\Char
x -> Bool -> Bool
not (Char -> Bool
isSpace Char
x) Bool -> Bool -> Bool
&& Char
x Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
',')

parseSpaceList
  :: ReadP r a
  -- ^ The parser for the stuff between commas
  -> ReadP r [a]
parseSpaceList :: forall r a. ReadP r a -> ReadP r [a]
parseSpaceList ReadP r a
p = ReadP r a -> ReadP r () -> ReadP r [a]
forall r a sep. ReadP r a -> ReadP r sep -> ReadP r [a]
sepBy ReadP r a
p ReadP r ()
forall r. ReadP r ()
skipSpaces

-- This version avoid parse ambiguity for list element parsers
-- that have multiple valid parses of prefixes.
parseOptCommaList :: ReadP r a -> ReadP r [a]
parseOptCommaList :: forall r a. ReadP r a -> ReadP r [a]
parseOptCommaList ReadP r a
p = ReadP r a -> ReadP r () -> ReadP r [a]
forall r a sep. ReadP r a -> ReadP r sep -> ReadP r [a]
sepBy ReadP r a
p ReadP r ()
forall r. ReadP r ()
localSep
  where
    -- The separator must not be empty or it introduces ambiguity
    localSep :: ReadP r ()
localSep =
      (ReadP r ()
forall r. ReadP r ()
skipSpaces ReadP r () -> Parser r Char Char -> Parser r Char Char
forall a b. Parser r Char a -> Parser r Char b -> Parser r Char b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Char -> Parser r Char Char
forall r. Char -> ReadP r Char
char Char
',' Parser r Char Char -> ReadP r () -> ReadP r ()
forall a b. Parser r Char a -> Parser r Char b -> Parser r Char b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ReadP r ()
forall r. ReadP r ()
skipSpaces)
        ReadP r () -> ReadP r () -> ReadP r ()
forall r a. ReadP r a -> ReadP r a -> ReadP r a
+++ ((Char -> Bool) -> Parser r Char Char
forall r. (Char -> Bool) -> ReadP r Char
satisfy Char -> Bool
isSpace Parser r Char Char -> ReadP r () -> ReadP r ()
forall a b. Parser r Char a -> Parser r Char b -> Parser r Char b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ReadP r ()
forall r. ReadP r ()
skipSpaces)

readPToMaybe :: ReadP a a -> String -> Maybe a
readPToMaybe :: forall a. ReadP a a -> [Char] -> Maybe a
readPToMaybe ReadP a a
p [Char]
str =
  [a] -> Maybe a
forall a. [a] -> Maybe a
listToMaybe
    [ a
r | (a
r, [Char]
s) <- ReadP a a -> ReadS a
forall a. ReadP a a -> ReadS a
readP_to_S ReadP a a
p [Char]
str, (Char -> Bool) -> [Char] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Char -> Bool
isSpace [Char]
s
    ]