    -- |
    -- Module      :  Data.Csv.Parser.Megaparsec.Internals
    -- Copyright   :  © 2016–2021 Stack Builders
    -- License     :  MIT
    --

    {-# LANGUAGE BangPatterns       #-}
    {-# LANGUAGE DeriveDataTypeable #-}
    {-# LANGUAGE OverloadedStrings  #-}
    {-# LANGUAGE RecordWildCards    #-}

    module Data.Csv.Parser.Megaparsec.Internals
        ( ConversionError (..)
        , Parser
        , csv
        , csvWithHeader
        , decodeWithC
        , toNamedRecord
        , header
        , name
        , record
        , field
        , escapedField
        , unescapedField)
    where

    import Control.Monad
    import Data.ByteString (ByteString)
    import Data.Csv hiding
      ( Parser
      , record
      , header
      , toNamedRecord )
    import Data.Data
    import Data.Vector (Vector)
    import Data.Word (Word8)
    import Text.Megaparsec
    import Text.Megaparsec.Byte
    import qualified Data.ByteString      as B
    import qualified Data.ByteString.Lazy as BL
    import qualified Data.Csv             as C
    import qualified Data.HashMap.Strict  as H
    import qualified Data.Vector          as V

    ----------------------------------------------------------------------------
    -- Custom error component and other types

    -- | Custom error component for CSV parsing. It allows typed reporting of
    -- conversion errors.

    newtype ConversionError = ConversionError String
      deriving (ConversionError -> ConversionError -> Bool
    (ConversionError -> ConversionError -> Bool)
    -> (ConversionError -> ConversionError -> Bool)
    -> Eq ConversionError
    forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
    $c== :: ConversionError -> ConversionError -> Bool
    == :: ConversionError -> ConversionError -> Bool
    $c/= :: ConversionError -> ConversionError -> Bool
    /= :: ConversionError -> ConversionError -> Bool
    Eq, Typeable ConversionError
    Typeable ConversionError =>
    (forall (c :: * -> *).
     (forall d b. Data d => c (d -> b) -> d -> c b)
     -> (forall g. g -> c g) -> ConversionError -> c ConversionError)
    -> (forall (c :: * -> *).
        (forall b r. Data b => c (b -> r) -> c r)
        -> (forall r. r -> c r) -> Constr -> c ConversionError)
    -> (ConversionError -> Constr)
    -> (ConversionError -> DataType)
    -> (forall (t :: * -> *) (c :: * -> *).
        Typeable t =>
        (forall d. Data d => c (t d)) -> Maybe (c ConversionError))
    -> (forall (t :: * -> * -> *) (c :: * -> *).
        Typeable t =>
        (forall d e. (Data d, Data e) => c (t d e))
        -> Maybe (c ConversionError))
    -> ((forall b. Data b => b -> b)
        -> ConversionError -> ConversionError)
    -> (forall r r'.
        (r -> r' -> r)
        -> r -> (forall d. Data d => d -> r') -> ConversionError -> r)
    -> (forall r r'.
        (r' -> r -> r)
        -> r -> (forall d. Data d => d -> r') -> ConversionError -> r)
    -> (forall u.
        (forall d. Data d => d -> u) -> ConversionError -> [u])
    -> (forall u.
        Int -> (forall d. Data d => d -> u) -> ConversionError -> u)
    -> (forall (m :: * -> *).
        Monad m =>
        (forall d. Data d => d -> m d)
        -> ConversionError -> m ConversionError)
    -> (forall (m :: * -> *).
        MonadPlus m =>
        (forall d. Data d => d -> m d)
        -> ConversionError -> m ConversionError)
    -> (forall (m :: * -> *).
        MonadPlus m =>
        (forall d. Data d => d -> m d)
        -> ConversionError -> m ConversionError)
    -> Data ConversionError
    ConversionError -> Constr
    ConversionError -> DataType
    (forall b. Data b => b -> b) -> ConversionError -> ConversionError
    forall a.
    Typeable a =>
    (forall (c :: * -> *).
     (forall d b. Data d => c (d -> b) -> d -> c b)
     -> (forall g. g -> c g) -> a -> c a)
    -> (forall (c :: * -> *).
        (forall b r. Data b => c (b -> r) -> c r)
        -> (forall r. r -> c r) -> Constr -> c a)
    -> (a -> Constr)
    -> (a -> DataType)
    -> (forall (t :: * -> *) (c :: * -> *).
        Typeable t =>
        (forall d. Data d => c (t d)) -> Maybe (c a))
    -> (forall (t :: * -> * -> *) (c :: * -> *).
        Typeable t =>
        (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
    -> ((forall b. Data b => b -> b) -> a -> a)
    -> (forall r r'.
        (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
    -> (forall r r'.
        (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
    -> (forall u. (forall d. Data d => d -> u) -> a -> [u])
    -> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
    -> (forall (m :: * -> *).
        Monad m =>
        (forall d. Data d => d -> m d) -> a -> m a)
    -> (forall (m :: * -> *).
        MonadPlus m =>
        (forall d. Data d => d -> m d) -> a -> m a)
    -> (forall (m :: * -> *).
        MonadPlus m =>
        (forall d. Data d => d -> m d) -> a -> m a)
    -> Data a
    forall u.
    Int -> (forall d. Data d => d -> u) -> ConversionError -> u
    forall u. (forall d. Data d => d -> u) -> ConversionError -> [u]
    forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> ConversionError -> r
    forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> ConversionError -> r
    forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d)
    -> ConversionError -> m ConversionError
    forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> ConversionError -> m ConversionError
    forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c ConversionError
    forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> ConversionError -> c ConversionError
    forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c ConversionError)
    forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c ConversionError)
    $cgfoldl :: forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> ConversionError -> c ConversionError
    gfoldl :: forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> ConversionError -> c ConversionError
    $cgunfold :: forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c ConversionError
    gunfold :: forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c ConversionError
    $ctoConstr :: ConversionError -> Constr
    toConstr :: ConversionError -> Constr
    $cdataTypeOf :: ConversionError -> DataType
    dataTypeOf :: ConversionError -> DataType
    $cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c ConversionError)
    dataCast1 :: forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c ConversionError)
    $cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c ConversionError)
    dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c ConversionError)
    $cgmapT :: (forall b. Data b => b -> b) -> ConversionError -> ConversionError
    gmapT :: (forall b. Data b => b -> b) -> ConversionError -> ConversionError
    $cgmapQl :: forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> ConversionError -> r
    gmapQl :: forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> ConversionError -> r
    $cgmapQr :: forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> ConversionError -> r
    gmapQr :: forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> ConversionError -> r
    $cgmapQ :: forall u. (forall d. Data d => d -> u) -> ConversionError -> [u]
    gmapQ :: forall u. (forall d. Data d => d -> u) -> ConversionError -> [u]
    $cgmapQi :: forall u.
    Int -> (forall d. Data d => d -> u) -> ConversionError -> u
    gmapQi :: forall u.
    Int -> (forall d. Data d => d -> u) -> ConversionError -> u
    $cgmapM :: forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d)
    -> ConversionError -> m ConversionError
    gmapM :: forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d)
    -> ConversionError -> m ConversionError
    $cgmapMp :: forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> ConversionError -> m ConversionError
    gmapMp :: forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> ConversionError -> m ConversionError
    $cgmapMo :: forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> ConversionError -> m ConversionError
    gmapMo :: forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> ConversionError -> m ConversionError
    Data, Typeable, Eq ConversionError
    Eq ConversionError =>
    (ConversionError -> ConversionError -> Ordering)
    -> (ConversionError -> ConversionError -> Bool)
    -> (ConversionError -> ConversionError -> Bool)
    -> (ConversionError -> ConversionError -> Bool)
    -> (ConversionError -> ConversionError -> Bool)
    -> (ConversionError -> ConversionError -> ConversionError)
    -> (ConversionError -> ConversionError -> ConversionError)
    -> Ord ConversionError
    ConversionError -> ConversionError -> Bool
    ConversionError -> ConversionError -> Ordering
    ConversionError -> ConversionError -> ConversionError
    forall a.
    Eq a =>
    (a -> a -> Ordering)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> a)
    -> (a -> a -> a)
    -> Ord a
    $ccompare :: ConversionError -> ConversionError -> Ordering
    compare :: ConversionError -> ConversionError -> Ordering
    $c< :: ConversionError -> ConversionError -> Bool
    < :: ConversionError -> ConversionError -> Bool
    $c<= :: ConversionError -> ConversionError -> Bool
    <= :: ConversionError -> ConversionError -> Bool
    $c> :: ConversionError -> ConversionError -> Bool
    > :: ConversionError -> ConversionError -> Bool
    $c>= :: ConversionError -> ConversionError -> Bool
    >= :: ConversionError -> ConversionError -> Bool
    $cmax :: ConversionError -> ConversionError -> ConversionError
    max :: ConversionError -> ConversionError -> ConversionError
    $cmin :: ConversionError -> ConversionError -> ConversionError
    min :: ConversionError -> ConversionError -> ConversionError
    Ord, ReadPrec [ConversionError]
    ReadPrec ConversionError
    Int -> ReadS ConversionError
    ReadS [ConversionError]
    (Int -> ReadS ConversionError)
    -> ReadS [ConversionError]
    -> ReadPrec ConversionError
    -> ReadPrec [ConversionError]
    -> Read ConversionError
    forall a.
    (Int -> ReadS a)
    -> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
    $creadsPrec :: Int -> ReadS ConversionError
    readsPrec :: Int -> ReadS ConversionError
    $creadList :: ReadS [ConversionError]
    readList :: ReadS [ConversionError]
    $creadPrec :: ReadPrec ConversionError
    readPrec :: ReadPrec ConversionError
    $creadListPrec :: ReadPrec [ConversionError]
    readListPrec :: ReadPrec [ConversionError]
    Read, Int -> ConversionError -> String -> String
    [ConversionError] -> String -> String
    ConversionError -> String
    (Int -> ConversionError -> String -> String)
    -> (ConversionError -> String)
    -> ([ConversionError] -> String -> String)
    -> Show ConversionError
    forall a.
    (Int -> a -> String -> String)
    -> (a -> String) -> ([a] -> String -> String) -> Show a
    $cshowsPrec :: Int -> ConversionError -> String -> String
    showsPrec :: Int -> ConversionError -> String -> String
    $cshow :: ConversionError -> String
    show :: ConversionError -> String
    $cshowList :: [ConversionError] -> String -> String
    showList :: [ConversionError] -> String -> String
    Show)

    instance ShowErrorComponent ConversionError where
      showErrorComponent :: ConversionError -> String
    showErrorComponent (ConversionError String
    msg) =
        String
    "conversion error: " String -> String -> String
    forall a. [a] -> [a] -> [a]
    ++ String
    msg

    -- | Parser type that uses “custom error component” 'ConversionError'.

    type Parser = Parsec ConversionError BL.ByteString

    ----------------------------------------------------------------------------
    -- The parser

    -- | Parse a CSV file that does not include a header.

    csv :: FromRecord a
      => DecodeOptions     -- ^ Decoding options
      -> Parser (Vector a) -- ^ The parser that parses collection of records
    csv :: forall a. FromRecord a => DecodeOptions -> Parser (Vector a)
    csv DecodeOptions {Word8
    decDelimiter :: Word8
    decDelimiter :: DecodeOptions -> Word8
    ..} = do
      xs <- ParsecT ConversionError ByteString Identity a
    -> ParsecT ConversionError ByteString Identity (Tokens ByteString)
    -> ParsecT ConversionError ByteString Identity [a]
    forall (m :: * -> *) a sep. MonadPlus m => m a -> m sep -> m [a]
    sepEndBy1 (Word8
    -> (Record -> Parser a)
    -> ParsecT ConversionError ByteString Identity a
    forall a. Word8 -> (Record -> Parser a) -> Parser a
    record Word8
    decDelimiter Record -> Parser a
    forall a. FromRecord a => Record -> Parser a
    parseRecord) ParsecT ConversionError ByteString Identity (Tokens ByteString)
    forall e s (m :: * -> *).
    (MonadParsec e s m, Token s ~ Word8) =>
    m (Tokens s)
    eol
      eof
      return $! V.fromList xs

    -- | Parse a CSV file that includes a header.

    csvWithHeader :: FromNamedRecord a
      => DecodeOptions     -- ^ Decoding options
      -> Parser (Header, Vector a)
         -- ^ The parser that parser collection of named records
    csvWithHeader :: forall a.
    FromNamedRecord a =>
    DecodeOptions -> Parser (Record, Vector a)
    csvWithHeader DecodeOptions {Word8
    decDelimiter :: DecodeOptions -> Word8
    decDelimiter :: Word8
    ..} = do
      !hdr <- Word8 -> Parser Record
    header Word8
    decDelimiter
      let f = NamedRecord -> Parser a
    forall a. FromNamedRecord a => NamedRecord -> Parser a
    parseNamedRecord (NamedRecord -> Parser a)
    -> (Record -> NamedRecord) -> Record -> Parser a
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Record -> Record -> NamedRecord
    toNamedRecord Record
    hdr
      xs   <- sepEndBy1 (record decDelimiter f) eol
      eof
      return $ let !v = [a] -> Vector a
    forall a. [a] -> Vector a
    V.fromList [a]
    xs in (hdr, v)

    -- | Decode CSV data using the provided parser, skipping a leading header if
    -- necessary.

    decodeWithC
      :: (DecodeOptions -> Parser a)
         -- ^ Parsing function parametrized by 'DecodeOptions'
      -> DecodeOptions
         -- ^ Decoding options
      -> HasHeader
         -- ^ Whether to expect a header in the input
      -> FilePath
         -- ^ File name (only for displaying in parse error messages, use empty
         -- string if you have none)
      -> BL.ByteString
         -- ^ CSV data
      -> Either (ParseErrorBundle BL.ByteString ConversionError) a
    decodeWithC :: forall a.
    (DecodeOptions -> Parser a)
    -> DecodeOptions
    -> HasHeader
    -> String
    -> ByteString
    -> Either (ParseErrorBundle ByteString ConversionError) a
    decodeWithC DecodeOptions -> Parser a
    p opts :: DecodeOptions
    opts@DecodeOptions {Word8
    decDelimiter :: DecodeOptions -> Word8
    decDelimiter :: Word8
    ..} HasHeader
    hasHeader = Parser a
    -> String
    -> ByteString
    -> Either (ParseErrorBundle ByteString ConversionError) a
    forall e s a.
    Parsec e s a -> String -> s -> Either (ParseErrorBundle s e) a
    parse Parser a
    parser
      where
        parser :: Parser a
    parser = case HasHeader
    hasHeader of
          HasHeader
    HasHeader -> Word8 -> Parser Record
    header Word8
    decDelimiter Parser Record -> Parser a -> Parser a
    forall a b.
    ParsecT ConversionError ByteString Identity a
    -> ParsecT ConversionError ByteString Identity b
    -> ParsecT ConversionError ByteString Identity b
    forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
    *> DecodeOptions -> Parser a
    p DecodeOptions
    opts
          HasHeader
    NoHeader  -> DecodeOptions -> Parser a
    p DecodeOptions
    opts
    {-# INLINE decodeWithC #-}

    -- | Convert a 'Record' to a 'NamedRecord' by attaching column names. The
    -- 'Header' and 'Record' must be of the same length.

    toNamedRecord :: Header -> Record -> NamedRecord
    toNamedRecord :: Record -> Record -> NamedRecord
    toNamedRecord Record
    hdr Record
    v = [(ByteString, ByteString)] -> NamedRecord
    forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
    H.fromList ([(ByteString, ByteString)] -> NamedRecord)
    -> (Vector (ByteString, ByteString) -> [(ByteString, ByteString)])
    -> Vector (ByteString, ByteString)
    -> NamedRecord
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Vector (ByteString, ByteString) -> [(ByteString, ByteString)]
    forall a. Vector a -> [a]
    V.toList (Vector (ByteString, ByteString) -> NamedRecord)
    -> Vector (ByteString, ByteString) -> NamedRecord
    forall a b. (a -> b) -> a -> b
    $ Record -> Record -> Vector (ByteString, ByteString)
    forall a b. Vector a -> Vector b -> Vector (a, b)
    V.zip Record
    hdr Record
    v
    {-# INLINE toNamedRecord #-}

    -- | Parse a header, including the terminating line separator.

    header :: Word8 -> Parser Header
    header :: Word8 -> Parser Record
    header Word8
    del = [ByteString] -> Record
    forall a. [a] -> Vector a
    V.fromList ([ByteString] -> Record)
    -> ParsecT ConversionError ByteString Identity [ByteString]
    -> Parser Record
    forall (m :: * -> *) a b. Monad m => (a -> b) -> m a -> m b
    <$!> ParsecT ConversionError ByteString Identity [ByteString]
    p Parser Record
    -> ParsecT ConversionError ByteString Identity (Tokens ByteString)
    -> Parser Record
    forall a b.
    ParsecT ConversionError ByteString Identity a
    -> ParsecT ConversionError ByteString Identity b
    -> ParsecT ConversionError ByteString Identity a
    forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
    <* ParsecT ConversionError ByteString Identity (Tokens ByteString)
    forall e s (m :: * -> *).
    (MonadParsec e s m, Token s ~ Word8) =>
    m (Tokens s)
    eol
      where
        p :: ParsecT ConversionError ByteString Identity [ByteString]
    p = ParsecT ConversionError ByteString Identity ByteString
    -> ParsecT ConversionError ByteString Identity ()
    -> ParsecT ConversionError ByteString Identity [ByteString]
    forall (m :: * -> *) a sep. MonadPlus m => m a -> m sep -> m [a]
    sepBy1 (Word8 -> ParsecT ConversionError ByteString Identity ByteString
    name Word8
    del) (ParsecT ConversionError ByteString Identity Word8
    -> ParsecT ConversionError ByteString Identity ()
    forall (f :: * -> *) a. Functor f => f a -> f ()
    void (ParsecT ConversionError ByteString Identity Word8
     -> ParsecT ConversionError ByteString Identity ())
    -> ParsecT ConversionError ByteString Identity Word8
    -> ParsecT ConversionError ByteString Identity ()
    forall a b. (a -> b) -> a -> b
    $ Token ByteString
    -> ParsecT ConversionError ByteString Identity (Token ByteString)
    forall e s (m :: * -> *).
    (MonadParsec e s m, Token s ~ Word8) =>
    Token s -> m (Token s)
    char Word8
    Token ByteString
    del) ParsecT ConversionError ByteString Identity [ByteString]
    -> String
    -> ParsecT ConversionError ByteString Identity [ByteString]
    forall e s (m :: * -> *) a.
    MonadParsec e s m =>
    m a -> String -> m a
    <?> String
    "file header"
    {-# INLINE header #-}

    -- | Parse a header name. Header names have the same format as regular
    -- 'field's.

    name :: Word8 -> Parser Name
    name :: Word8 -> ParsecT ConversionError ByteString Identity ByteString
    name Word8
    del = Word8 -> ParsecT ConversionError ByteString Identity ByteString
    field Word8
    del ParsecT ConversionError ByteString Identity ByteString
    -> String -> ParsecT ConversionError ByteString Identity ByteString
    forall e s (m :: * -> *) a.
    MonadParsec e s m =>
    m a -> String -> m a
    <?> String
    "name in header"
    {-# INLINE name #-}

    -- | Parse a record, not including the terminating line separator. The
    -- terminating line separate is not included as the last record in a CSV
    -- file is allowed to not have a terminating line separator.

    record
      :: Word8             -- ^ Field delimiter
      -> (Record -> C.Parser a)
         -- ^ How to “parse” record to get the data of interest
      -> Parser a
    record :: forall a. Word8 -> (Record -> Parser a) -> Parser a
    record Word8
    del Record -> Parser a
    f = do
      ParsecT ConversionError ByteString Identity ()
    -> ParsecT ConversionError ByteString Identity ()
    forall a.
    ParsecT ConversionError ByteString Identity a
    -> ParsecT ConversionError ByteString Identity ()
    forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m ()
    notFollowedBy ParsecT ConversionError ByteString Identity ()
    forall e s (m :: * -> *). MonadParsec e s m => m ()
    eof -- to prevent reading empty line at the end of file
      r <- [ByteString] -> Record
    forall a. [a] -> Vector a
    V.fromList ([ByteString] -> Record)
    -> ParsecT ConversionError ByteString Identity [ByteString]
    -> Parser Record
    forall (m :: * -> *) a b. Monad m => (a -> b) -> m a -> m b
    <$!> (ParsecT ConversionError ByteString Identity ByteString
    -> ParsecT ConversionError ByteString Identity ()
    -> ParsecT ConversionError ByteString Identity [ByteString]
    forall (m :: * -> *) a sep. MonadPlus m => m a -> m sep -> m [a]
    sepBy1 (Word8 -> ParsecT ConversionError ByteString Identity ByteString
    field Word8
    del) (ParsecT ConversionError ByteString Identity Word8
    -> ParsecT ConversionError ByteString Identity ()
    forall (f :: * -> *) a. Functor f => f a -> f ()
    void (ParsecT ConversionError ByteString Identity Word8
     -> ParsecT ConversionError ByteString Identity ())
    -> ParsecT ConversionError ByteString Identity Word8
    -> ParsecT ConversionError ByteString Identity ()
    forall a b. (a -> b) -> a -> b
    $ Token ByteString
    -> ParsecT ConversionError ByteString Identity (Token ByteString)
    forall e s (m :: * -> *).
    (MonadParsec e s m, Token s ~ Word8) =>
    Token s -> m (Token s)
    char Word8
    Token ByteString
    del) ParsecT ConversionError ByteString Identity [ByteString]
    -> String
    -> ParsecT ConversionError ByteString Identity [ByteString]
    forall e s (m :: * -> *) a.
    MonadParsec e s m =>
    m a -> String -> m a
    <?> String
    "record")
      case C.runParser (f r) of
        Left String
    msg -> ConversionError -> ParsecT ConversionError ByteString Identity a
    forall e s (m :: * -> *) a. MonadParsec e s m => e -> m a
    customFailure (String -> ConversionError
    ConversionError String
    msg)
        Right a
    x  -> a -> ParsecT ConversionError ByteString Identity a
    forall a. a -> ParsecT ConversionError ByteString Identity a
    forall (m :: * -> *) a. Monad m => a -> m a
    return a
    x
    {-# INLINE record #-}

    -- | Parse a field. The field may be in either the escaped or non-escaped
    -- format. The returned value is unescaped.

    field :: Word8 -> Parser Field
    field :: Word8 -> ParsecT ConversionError ByteString Identity ByteString
    field Word8
    del = String
    -> ParsecT ConversionError ByteString Identity ByteString
    -> ParsecT ConversionError ByteString Identity ByteString
    forall a.
    String
    -> ParsecT ConversionError ByteString Identity a
    -> ParsecT ConversionError ByteString Identity a
    forall e s (m :: * -> *) a.
    MonadParsec e s m =>
    String -> m a -> m a
    label String
    "field" (ParsecT ConversionError ByteString Identity ByteString
    escapedField ParsecT ConversionError ByteString Identity ByteString
    -> ParsecT ConversionError ByteString Identity ByteString
    -> ParsecT ConversionError ByteString Identity ByteString
    forall a.
    ParsecT ConversionError ByteString Identity a
    -> ParsecT ConversionError ByteString Identity a
    -> ParsecT ConversionError ByteString Identity a
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    <|> Word8 -> ParsecT ConversionError ByteString Identity ByteString
    unescapedField Word8
    del)
    {-# INLINE field #-}

    -- | Parse an escaped field.

    escapedField :: Parser ByteString
    escapedField :: ParsecT ConversionError ByteString Identity ByteString
    escapedField =
      [Word8] -> ByteString
    B.pack ([Word8] -> ByteString)
    -> ParsecT ConversionError ByteString Identity [Word8]
    -> ParsecT ConversionError ByteString Identity ByteString
    forall (m :: * -> *) a b. Monad m => (a -> b) -> m a -> m b
    <$!> ParsecT ConversionError ByteString Identity Word8
    -> ParsecT ConversionError ByteString Identity Word8
    -> ParsecT ConversionError ByteString Identity [Word8]
    -> ParsecT ConversionError ByteString Identity [Word8]
    forall (m :: * -> *) open close a.
    Applicative m =>
    m open -> m close -> m a -> m a
    between (Token ByteString
    -> ParsecT ConversionError ByteString Identity (Token ByteString)
    forall e s (m :: * -> *).
    (MonadParsec e s m, Token s ~ Word8) =>
    Token s -> m (Token s)
    char Word8
    Token ByteString
    34) (Token ByteString
    -> ParsecT ConversionError ByteString Identity (Token ByteString)
    forall e s (m :: * -> *).
    (MonadParsec e s m, Token s ~ Word8) =>
    Token s -> m (Token s)
    char Word8
    Token ByteString
    34) (ParsecT ConversionError ByteString Identity Word8
    -> ParsecT ConversionError ByteString Identity [Word8]
    forall (m :: * -> *) a. MonadPlus m => m a -> m [a]
    many (ParsecT ConversionError ByteString Identity Word8
     -> ParsecT ConversionError ByteString Identity [Word8])
    -> ParsecT ConversionError ByteString Identity Word8
    -> ParsecT ConversionError ByteString Identity [Word8]
    forall a b. (a -> b) -> a -> b
    $ ParsecT ConversionError ByteString Identity Word8
    ParsecT ConversionError ByteString Identity (Token ByteString)
    normalChar ParsecT ConversionError ByteString Identity Word8
    -> ParsecT ConversionError ByteString Identity Word8
    -> ParsecT ConversionError ByteString Identity Word8
    forall a.
    ParsecT ConversionError ByteString Identity a
    -> ParsecT ConversionError ByteString Identity a
    -> ParsecT ConversionError ByteString Identity a
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    <|> ParsecT ConversionError ByteString Identity Word8
    escapedDq)
      where
        normalChar :: ParsecT ConversionError ByteString Identity (Token ByteString)
    normalChar = Token ByteString
    -> ParsecT ConversionError ByteString Identity (Token ByteString)
    forall e s (m :: * -> *).
    MonadParsec e s m =>
    Token s -> m (Token s)
    anySingleBut Token ByteString
    34 ParsecT ConversionError ByteString Identity (Token ByteString)
    -> String
    -> ParsecT ConversionError ByteString Identity (Token ByteString)
    forall e s (m :: * -> *) a.
    MonadParsec e s m =>
    m a -> String -> m a
    <?> String
    "unescaped character"
        escapedDq :: ParsecT ConversionError ByteString Identity Word8
    escapedDq  = String
    -> ParsecT ConversionError ByteString Identity Word8
    -> ParsecT ConversionError ByteString Identity Word8
    forall a.
    String
    -> ParsecT ConversionError ByteString Identity a
    -> ParsecT ConversionError ByteString Identity a
    forall e s (m :: * -> *) a.
    MonadParsec e s m =>
    String -> m a -> m a
    label String
    "escaped double-quote" (Word8
    34 Word8
    -> ParsecT ConversionError ByteString Identity (Tokens ByteString)
    -> ParsecT ConversionError ByteString Identity Word8
    forall a b.
    a
    -> ParsecT ConversionError ByteString Identity b
    -> ParsecT ConversionError ByteString Identity a
    forall (f :: * -> *) a b. Functor f => a -> f b -> f a
    <$ Tokens ByteString
    -> ParsecT ConversionError ByteString Identity (Tokens ByteString)
    forall e s (m :: * -> *).
    MonadParsec e s m =>
    Tokens s -> m (Tokens s)
    string Tokens ByteString
    "\"\"")
    {-# INLINE escapedField #-}

    -- | Parse an unescaped field.

    unescapedField :: Word8 -> Parser ByteString
    unescapedField :: Word8 -> ParsecT ConversionError ByteString Identity ByteString
    unescapedField Word8
    del = ByteString -> ByteString
    BL.toStrict (ByteString -> ByteString)
    -> ParsecT ConversionError ByteString Identity ByteString
    -> ParsecT ConversionError ByteString Identity ByteString
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Maybe String
    -> (Token ByteString -> Bool)
    -> ParsecT ConversionError ByteString Identity (Tokens ByteString)
    forall e s (m :: * -> *).
    MonadParsec e s m =>
    Maybe String -> (Token s -> Bool) -> m (Tokens s)
    takeWhileP (String -> Maybe String
    forall a. a -> Maybe a
    Just String
    "unescaped character") Word8 -> Bool
    Token ByteString -> Bool
    f
      where
        f :: Word8 -> Bool
    f Word8
    x = Word8
    x Word8 -> Word8 -> Bool
    forall a. Eq a => a -> a -> Bool
    /= Word8
    del Bool -> Bool -> Bool
    && Word8
    x Word8 -> Word8 -> Bool
    forall a. Eq a => a -> a -> Bool
    /= Word8
    34 Bool -> Bool -> Bool
    && Word8
    x Word8 -> Word8 -> Bool
    forall a. Eq a => a -> a -> Bool
    /= Word8
    10 Bool -> Bool -> Bool
    && Word8
    x Word8 -> Word8 -> Bool
    forall a. Eq a => a -> a -> Bool
    /= Word8
    13
    {-# INLINE unescapedField #-}