    {-# LANGUAGE BangPatterns #-}
    {-# LANGUAGE BinaryLiterals #-}
    module Djot.Parse
    (   Parser
      , Chunk(..)
      , parse
      , asciiChar
      , satisfyByte
      , skipSatisfyByte
      , satisfy
      , anyChar
      , skipMany
      , skipSome
      , eof
      , getState
      , updateState
      , lookahead
      , peek
      , peekBack
      , fails
      , failed
      , withByteString
      , byteStringOf
      , notFollowedBy
      , optional_
      , byteString
      , getOffset
      , sourceLine
      , sourceColumn
      , branch
      , endline
      , restOfLine
      , ws
      , followedByWhitespace
      , followedByBlankLine
      , spaceOrTab
      , isWs
      , strToUtf8
      , utf8ToStr
    )
    where

    import qualified Data.ByteString as B
    import qualified Data.ByteString.Char8 as B8
    import Data.ByteString (ByteString)
    import Control.Applicative
    import Control.Monad (void, MonadPlus(..))
    import Data.Char (chr)
    import Data.Bits
    import Data.Maybe (fromMaybe)
    import Data.Text.Encoding (decodeUtf8With, encodeUtf8)
    import Data.Text.Encoding.Error (lenientDecode)
    import qualified Data.Text as T
    -- import Text.Printf
    -- import Debug.Trace

    newtype Parser s a =
      Parser{ forall s a. Parser s a -> ParserState s -> Maybe (ParserState s, a)
    runParser :: ParserState s -> Maybe (ParserState s, a) }

    instance Functor (Parser s) where
      fmap :: forall a b. (a -> b) -> Parser s a -> Parser s b
    fmap a -> b
    f Parser s a
    g = (ParserState s -> Maybe (ParserState s, b)) -> Parser s b
    forall s a.
    (ParserState s -> Maybe (ParserState s, a)) -> Parser s a
    Parser ((ParserState s -> Maybe (ParserState s, b)) -> Parser s b)
    -> (ParserState s -> Maybe (ParserState s, b)) -> Parser s b
    forall a b. (a -> b) -> a -> b
    $ \ParserState s
    s -> case Parser s a -> ParserState s -> Maybe (ParserState s, a)
    forall s a. Parser s a -> ParserState s -> Maybe (ParserState s, a)
    runParser Parser s a
    g ParserState s
    s of
                                     Maybe (ParserState s, a)
    Nothing -> Maybe (ParserState s, b)
    forall a. Maybe a
    Nothing
                                     Just (ParserState s
    s', !a
    x) -> (ParserState s, b) -> Maybe (ParserState s, b)
    forall a. a -> Maybe a
    Just (ParserState s
    s', a -> b
    f a
    x)

    instance Applicative (Parser s) where
      pure :: forall a. a -> Parser s a
    pure a
    x = (ParserState s -> Maybe (ParserState s, a)) -> Parser s a
    forall s a.
    (ParserState s -> Maybe (ParserState s, a)) -> Parser s a
    Parser (\ParserState s
    s -> (ParserState s, a) -> Maybe (ParserState s, a)
    forall a. a -> Maybe a
    Just (ParserState s
    s, a
    x))
      liftA2 :: forall a b c.
    (a -> b -> c) -> Parser s a -> Parser s b -> Parser s c
    liftA2 a -> b -> c
    f Parser s a
    g Parser s b
    h = (ParserState s -> Maybe (ParserState s, c)) -> Parser s c
    forall s a.
    (ParserState s -> Maybe (ParserState s, a)) -> Parser s a
    Parser ((ParserState s -> Maybe (ParserState s, c)) -> Parser s c)
    -> (ParserState s -> Maybe (ParserState s, c)) -> Parser s c
    forall a b. (a -> b) -> a -> b
    $ \ParserState s
    s ->
        case Parser s a -> ParserState s -> Maybe (ParserState s, a)
    forall s a. Parser s a -> ParserState s -> Maybe (ParserState s, a)
    runParser Parser s a
    g ParserState s
    s of
          Maybe (ParserState s, a)
    Nothing -> Maybe (ParserState s, c)
    forall a. Maybe a
    Nothing
          Just (ParserState s
    s', a
    x) ->
            case Parser s b -> ParserState s -> Maybe (ParserState s, b)
    forall s a. Parser s a -> ParserState s -> Maybe (ParserState s, a)
    runParser Parser s b
    h ParserState s
    s' of
              Maybe (ParserState s, b)
    Nothing -> Maybe (ParserState s, c)
    forall a. Maybe a
    Nothing
              Just (ParserState s
    s'', b
    y) -> (ParserState s, c) -> Maybe (ParserState s, c)
    forall a. a -> Maybe a
    Just (ParserState s
    s'', a -> b -> c
    f a
    x b
    y)

    instance Monad (Parser s) where
      return :: forall a. a -> Parser s a
    return = a -> Parser s a
    forall a. a -> Parser s a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure
      Parser s a
    f >>= :: forall a b. Parser s a -> (a -> Parser s b) -> Parser s b
    >>= a -> Parser s b
    g = (ParserState s -> Maybe (ParserState s, b)) -> Parser s b
    forall s a.
    (ParserState s -> Maybe (ParserState s, a)) -> Parser s a
    Parser ((ParserState s -> Maybe (ParserState s, b)) -> Parser s b)
    -> (ParserState s -> Maybe (ParserState s, b)) -> Parser s b
    forall a b. (a -> b) -> a -> b
    $ \ParserState s
    s ->
        case Parser s a -> ParserState s -> Maybe (ParserState s, a)
    forall s a. Parser s a -> ParserState s -> Maybe (ParserState s, a)
    runParser Parser s a
    f ParserState s
    s of
          Maybe (ParserState s, a)
    Nothing -> Maybe (ParserState s, b)
    forall a. Maybe a
    Nothing
          Just (ParserState s
    s', a
    x) -> Parser s b -> ParserState s -> Maybe (ParserState s, b)
    forall s a. Parser s a -> ParserState s -> Maybe (ParserState s, a)
    runParser (a -> Parser s b
    g a
    x) ParserState s
    s'

    instance Alternative (Parser s) where
      empty :: forall a. Parser s a
    empty = (ParserState s -> Maybe (ParserState s, a)) -> Parser s a
    forall s a.
    (ParserState s -> Maybe (ParserState s, a)) -> Parser s a
    Parser (Maybe (ParserState s, a)
    -> ParserState s -> Maybe (ParserState s, a)
    forall a b. a -> b -> a
    const Maybe (ParserState s, a)
    forall a. Maybe a
    Nothing)
      Parser s a
    f <|> :: forall a. Parser s a -> Parser s a -> Parser s a
    <|> Parser s a
    g = (ParserState s -> Maybe (ParserState s, a)) -> Parser s a
    forall s a.
    (ParserState s -> Maybe (ParserState s, a)) -> Parser s a
    Parser ((ParserState s -> Maybe (ParserState s, a)) -> Parser s a)
    -> (ParserState s -> Maybe (ParserState s, a)) -> Parser s a
    forall a b. (a -> b) -> a -> b
    $ \ParserState s
    s ->
        case Parser s a -> ParserState s -> Maybe (ParserState s, a)
    forall s a. Parser s a -> ParserState s -> Maybe (ParserState s, a)
    runParser Parser s a
    f ParserState s
    s of
          Just (ParserState s
    s', a
    x) -> (ParserState s, a) -> Maybe (ParserState s, a)
    forall a. a -> Maybe a
    Just (ParserState s
    s', a
    x)
          Maybe (ParserState s, a)
    Nothing -> Parser s a -> ParserState s -> Maybe (ParserState s, a)
    forall s a. Parser s a -> ParserState s -> Maybe (ParserState s, a)
    runParser Parser s a
    g ParserState s
    s

    instance MonadPlus (Parser s) where
      mzero :: forall a. Parser s a
    mzero = Parser s a
    forall a. Parser s a
    forall (f :: * -> *) a. Alternative f => f a
    empty
      mplus :: forall a. Parser s a -> Parser s a -> Parser s a
    mplus = Parser s a -> Parser s a -> Parser s a
    forall a. Parser s a -> Parser s a -> Parser s a
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    (<|>)

    data Chunk =
      Chunk{ Chunk -> Int
    chunkLine :: Int
           , Chunk -> Int
    chunkColumn :: Int
           , Chunk -> ByteString
    chunkBytes :: ByteString }
      deriving (Int -> Chunk -> ShowS
    [Chunk] -> ShowS
    Chunk -> String
    (Int -> Chunk -> ShowS)
    -> (Chunk -> String) -> ([Chunk] -> ShowS) -> Show Chunk
    forall a.
    (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
    $cshowsPrec :: Int -> Chunk -> ShowS
    showsPrec :: Int -> Chunk -> ShowS
    $cshow :: Chunk -> String
    show :: Chunk -> String
    $cshowList :: [Chunk] -> ShowS
    showList :: [Chunk] -> ShowS
    Show, Chunk -> Chunk -> Bool
    (Chunk -> Chunk -> Bool) -> (Chunk -> Chunk -> Bool) -> Eq Chunk
    forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
    $c== :: Chunk -> Chunk -> Bool
    == :: Chunk -> Chunk -> Bool
    $c/= :: Chunk -> Chunk -> Bool
    /= :: Chunk -> Chunk -> Bool
    Eq, Eq Chunk
    Eq Chunk =>
    (Chunk -> Chunk -> Ordering)
    -> (Chunk -> Chunk -> Bool)
    -> (Chunk -> Chunk -> Bool)
    -> (Chunk -> Chunk -> Bool)
    -> (Chunk -> Chunk -> Bool)
    -> (Chunk -> Chunk -> Chunk)
    -> (Chunk -> Chunk -> Chunk)
    -> Ord Chunk
    Chunk -> Chunk -> Bool
    Chunk -> Chunk -> Ordering
    Chunk -> Chunk -> Chunk
    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 :: Chunk -> Chunk -> Ordering
    compare :: Chunk -> Chunk -> Ordering
    $c< :: Chunk -> Chunk -> Bool
    < :: Chunk -> Chunk -> Bool
    $c<= :: Chunk -> Chunk -> Bool
    <= :: Chunk -> Chunk -> Bool
    $c> :: Chunk -> Chunk -> Bool
    > :: Chunk -> Chunk -> Bool
    $c>= :: Chunk -> Chunk -> Bool
    >= :: Chunk -> Chunk -> Bool
    $cmax :: Chunk -> Chunk -> Chunk
    max :: Chunk -> Chunk -> Chunk
    $cmin :: Chunk -> Chunk -> Chunk
    min :: Chunk -> Chunk -> Chunk
    Ord)

    data ParserState a =
      ParserState
      { forall a. ParserState a -> [Chunk]
    chunks :: [Chunk]
      , forall a. ParserState a -> ByteString
    subject :: !ByteString
      , forall a. ParserState a -> Int
    offset :: !Int
      , forall a. ParserState a -> Int
    line :: !Int
      , forall a. ParserState a -> Int
    column :: !Int
      , forall a. ParserState a -> a
    userState :: !a
      }
      deriving (Int -> ParserState a -> ShowS
    [ParserState a] -> ShowS
    ParserState a -> String
    (Int -> ParserState a -> ShowS)
    -> (ParserState a -> String)
    -> ([ParserState a] -> ShowS)
    -> Show (ParserState a)
    forall a. Show a => Int -> ParserState a -> ShowS
    forall a. Show a => [ParserState a] -> ShowS
    forall a. Show a => ParserState a -> String
    forall a.
    (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
    $cshowsPrec :: forall a. Show a => Int -> ParserState a -> ShowS
    showsPrec :: Int -> ParserState a -> ShowS
    $cshow :: forall a. Show a => ParserState a -> String
    show :: ParserState a -> String
    $cshowList :: forall a. Show a => [ParserState a] -> ShowS
    showList :: [ParserState a] -> ShowS
    Show)

    -- | Apply a parser to a bytestring with a given user state.
    -- Returns @Nothing@ on failure, @Just result@ on success.
    parse :: Parser s a -> s -> [Chunk] -> Maybe a
    parse :: forall s a. Parser s a -> s -> [Chunk] -> Maybe a
    parse Parser s a
    parser s
    ustate [Chunk]
    chunks'' =
      (ParserState s, a) -> a
    forall a b. (a, b) -> b
    snd ((ParserState s, a) -> a) -> Maybe (ParserState s, a) -> Maybe a
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$>
        Parser s a -> ParserState s -> Maybe (ParserState s, a)
    forall s a. Parser s a -> ParserState s -> Maybe (ParserState s, a)
    runParser Parser s a
    parser ParserState { chunks :: [Chunk]
    chunks = [Chunk]
    chunks'
                                     , subject :: ByteString
    subject = ByteString
    bs
                                     , offset :: Int
    offset = Int
    0
                                     , line :: Int
    line = Int
    startline
                                     , column :: Int
    column = Int
    startcol
                                     , userState :: s
    userState = s
    ustate }

     where
       ([Chunk]
    chunks', ByteString
    bs, Int
    startline, Int
    startcol) =
         case [Chunk]
    chunks'' of
           [] -> ([], ByteString
    forall a. Monoid a => a
    mempty, Int
    1, Int
    0)
           (Chunk
    c:[Chunk]
    cs) -> ([Chunk]
    cs, Chunk -> ByteString
    chunkBytes Chunk
    c, Chunk -> Int
    chunkLine Chunk
    c, Chunk -> Int
    chunkColumn Chunk
    c)

    -- | Given a number of bytes, advances the offset and updates line/column.
    unsafeAdvance :: Int -> ParserState s -> ParserState s
    unsafeAdvance :: forall s. Int -> ParserState s -> ParserState s
    unsafeAdvance Int
    0 = ParserState s -> ParserState s
    forall a. a -> a
    id
    unsafeAdvance !Int
    n = Int -> ParserState s -> ParserState s
    forall s. Int -> ParserState s -> ParserState s
    unsafeAdvance (Int
    n Int -> Int -> Int
    forall a. Num a => a -> a -> a
    - Int
    1) (ParserState s -> ParserState s)
    -> (ParserState s -> ParserState s)
    -> ParserState s
    -> ParserState s
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . ParserState s -> ParserState s
    forall s. ParserState s -> ParserState s
    unsafeAdvanceByte

    -- | Advance the offset and line/column for consuming a given byte.
    unsafeAdvanceByte :: ParserState s -> ParserState s
    unsafeAdvanceByte :: forall s. ParserState s -> ParserState s
    unsafeAdvanceByte ParserState s
    st
      | ParserState s -> Int
    forall a. ParserState a -> Int
    offset ParserState s
    st Int -> Int -> Int
    forall a. Num a => a -> a -> a
    + Int
    1 Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    >= ByteString -> Int
    B.length (ParserState s -> ByteString
    forall a. ParserState a -> ByteString
    subject ParserState s
    st)
      , Chunk
    c:[Chunk]
    cs <- ParserState s -> [Chunk]
    forall a. ParserState a -> [Chunk]
    chunks ParserState s
    st
       = ParserState s
    st{ chunks = cs
           , subject = chunkBytes c
           , offset = 0
           , line = chunkLine c
           , column = chunkColumn c }
      | Bool
    otherwise
         = case HasCallStack => ByteString -> Int -> Word8
    ByteString -> Int -> Word8
    B.index (ParserState s -> ByteString
    forall a. ParserState a -> ByteString
    subject ParserState s
    st) (ParserState s -> Int
    forall a. ParserState a -> Int
    offset ParserState s
    st) of
             Word8
    10 -> ParserState s
    st{ offset = offset st + 1
                     , line = line st + 1
                     , column = 1 }
             Word8
    9 -> ParserState s
    st{ offset = offset st + 1
                    , column = column st + (4 - (column st `mod` 4)) }
             !Word8
    w | Word8
    w Word8 -> Word8 -> Bool
    forall a. Ord a => a -> a -> Bool
    < Word8
    0x80 -> ParserState s
    st{ offset = offset st + 1
                                , column = column st + 1 }
                -- utf8 multibyte: only count byte 1:
                | Word8
    w Word8 -> Word8 -> Bool
    forall a. Ord a => a -> a -> Bool
    >= Word8
    0b11000000 -> ParserState s
    st{ offset = offset st + 1
                                       , column = column st + 1 }
                | Bool
    otherwise -> ParserState s
    st{ offset = offset st + 1 }

    -- | Returns current byte as Char.
    current :: ParserState s -> Maybe Char
    current :: forall s. ParserState s -> Maybe Char
    current ParserState s
    st = ParserState s -> ByteString
    forall a. ParserState a -> ByteString
    subject ParserState s
    st ByteString -> Int -> Maybe Char
    B8.!? ParserState s -> Int
    forall a. ParserState a -> Int
    offset ParserState s
    st

    -- | Returns current byte as Char.
    peek :: Parser s (Maybe Char)
    peek :: forall s. Parser s (Maybe Char)
    peek = (ParserState s -> Maybe (ParserState s, Maybe Char))
    -> Parser s (Maybe Char)
    forall s a.
    (ParserState s -> Maybe (ParserState s, a)) -> Parser s a
    Parser ((ParserState s -> Maybe (ParserState s, Maybe Char))
     -> Parser s (Maybe Char))
    -> (ParserState s -> Maybe (ParserState s, Maybe Char))
    -> Parser s (Maybe Char)
    forall a b. (a -> b) -> a -> b
    $ \ParserState s
    st -> (ParserState s, Maybe Char) -> Maybe (ParserState s, Maybe Char)
    forall a. a -> Maybe a
    Just (ParserState s
    st, ParserState s -> Maybe Char
    forall s. ParserState s -> Maybe Char
    current ParserState s
    st)

    -- | Returns previous byte as Char.  Doesn't cross chunk boundaries.
    peekBack :: Parser s (Maybe Char)
    peekBack :: forall s. Parser s (Maybe Char)
    peekBack = (ParserState s -> Maybe (ParserState s, Maybe Char))
    -> Parser s (Maybe Char)
    forall s a.
    (ParserState s -> Maybe (ParserState s, a)) -> Parser s a
    Parser ((ParserState s -> Maybe (ParserState s, Maybe Char))
     -> Parser s (Maybe Char))
    -> (ParserState s -> Maybe (ParserState s, Maybe Char))
    -> Parser s (Maybe Char)
    forall a b. (a -> b) -> a -> b
    $ \ParserState s
    st -> (ParserState s, Maybe Char) -> Maybe (ParserState s, Maybe Char)
    forall a. a -> Maybe a
    Just (ParserState s
    st, ParserState s -> ByteString
    forall a. ParserState a -> ByteString
    subject ParserState s
    st ByteString -> Int -> Maybe Char
    B8.!? (ParserState s -> Int
    forall a. ParserState a -> Int
    offset ParserState s
    st Int -> Int -> Int
    forall a. Num a => a -> a -> a
    - Int
    1))

    -- | Parse a byte satisfying a predicate.
    satisfyByte :: (Char -> Bool) -> Parser s Char
    satisfyByte :: forall s. (Char -> Bool) -> Parser s Char
    satisfyByte Char -> Bool
    f = (ParserState s -> Maybe (ParserState s, Char)) -> Parser s Char
    forall s a.
    (ParserState s -> Maybe (ParserState s, a)) -> Parser s a
    Parser ((ParserState s -> Maybe (ParserState s, Char)) -> Parser s Char)
    -> (ParserState s -> Maybe (ParserState s, Char)) -> Parser s Char
    forall a b. (a -> b) -> a -> b
    $ \ParserState s
    st ->
      case ParserState s -> Maybe Char
    forall s. ParserState s -> Maybe Char
    current ParserState s
    st of
        Just Char
    c | Char -> Bool
    f Char
    c -> (ParserState s, Char) -> Maybe (ParserState s, Char)
    forall a. a -> Maybe a
    Just (ParserState s -> ParserState s
    forall s. ParserState s -> ParserState s
    unsafeAdvanceByte ParserState s
    st, Char
    c)
        Maybe Char
    _ -> Maybe (ParserState s, Char)
    forall a. Maybe a
    Nothing

    -- | Skip byte satisfying a predicate.
    skipSatisfyByte :: (Char -> Bool) -> Parser s ()
    skipSatisfyByte :: forall s. (Char -> Bool) -> Parser s ()
    skipSatisfyByte Char -> Bool
    f = (ParserState s -> Maybe (ParserState s, ())) -> Parser s ()
    forall s a.
    (ParserState s -> Maybe (ParserState s, a)) -> Parser s a
    Parser ((ParserState s -> Maybe (ParserState s, ())) -> Parser s ())
    -> (ParserState s -> Maybe (ParserState s, ())) -> Parser s ()
    forall a b. (a -> b) -> a -> b
    $ \ParserState s
    st ->
      case ParserState s -> Maybe Char
    forall s. ParserState s -> Maybe Char
    current ParserState s
    st of
        Just Char
    c | Char -> Bool
    f Char
    c -> (ParserState s, ()) -> Maybe (ParserState s, ())
    forall a. a -> Maybe a
    Just (ParserState s -> ParserState s
    forall s. ParserState s -> ParserState s
    unsafeAdvanceByte ParserState s
    st, ())
        Maybe Char
    _ -> Maybe (ParserState s, ())
    forall a. Maybe a
    Nothing

    -- | Parse a (possibly multibyte) Char satisfying a predicate.
    -- Assumes UTF-8 encoding.
    satisfy :: (Char -> Bool) -> Parser s Char
    satisfy :: forall s. (Char -> Bool) -> Parser s Char
    satisfy Char -> Bool
    f = (ParserState s -> Maybe (ParserState s, Char)) -> Parser s Char
    forall s a.
    (ParserState s -> Maybe (ParserState s, a)) -> Parser s a
    Parser ((ParserState s -> Maybe (ParserState s, Char)) -> Parser s Char)
    -> (ParserState s -> Maybe (ParserState s, Char)) -> Parser s Char
    forall a b. (a -> b) -> a -> b
    $ \ParserState s
    st ->
      let peekWord :: Int -> Maybe Word8
    peekWord !Int
    n = ParserState s -> ByteString
    forall a. ParserState a -> ByteString
    subject ParserState s
    st ByteString -> Int -> Maybe Word8
    B.!? (ParserState s -> Int
    forall a. ParserState a -> Int
    offset ParserState s
    st Int -> Int -> Int
    forall a. Num a => a -> a -> a
    + Int
    n)
          b2 :: Word8
    b2 = Word8 -> Maybe Word8 -> Word8
    forall a. a -> Maybe a -> a
    fromMaybe Word8
    0 (Maybe Word8 -> Word8) -> Maybe Word8 -> Word8
    forall a b. (a -> b) -> a -> b
    $ Int -> Maybe Word8
    peekWord Int
    1
          b3 :: Word8
    b3 = Word8 -> Maybe Word8 -> Word8
    forall a. a -> Maybe a -> a
    fromMaybe Word8
    0 (Maybe Word8 -> Word8) -> Maybe Word8 -> Word8
    forall a b. (a -> b) -> a -> b
    $ Int -> Maybe Word8
    peekWord Int
    2
          b4 :: Word8
    b4 = Word8 -> Maybe Word8 -> Word8
    forall a. a -> Maybe a -> a
    fromMaybe Word8
    0 (Maybe Word8 -> Word8) -> Maybe Word8 -> Word8
    forall a b. (a -> b) -> a -> b
    $ Int -> Maybe Word8
    peekWord Int
    3
      in case Int -> Maybe Word8
    peekWord Int
    0 of
        Maybe Word8
    Nothing -> Maybe (ParserState s, Char)
    forall a. Maybe a
    Nothing
        Just Word8
    b1
          | Word8
    b1 Word8 -> Word8 -> Bool
    forall a. Ord a => a -> a -> Bool
    < Word8
    0b10000000
          , !Char
    c <- Int -> Char
    chr (Word8 -> Int
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Word8
    b1)
          , Char -> Bool
    f Char
    c -> (ParserState s, Char) -> Maybe (ParserState s, Char)
    forall a. a -> Maybe a
    Just (ParserState s -> ParserState s
    forall s. ParserState s -> ParserState s
    unsafeAdvanceByte ParserState s
    st, Char
    c)
          | Word8
    b1 Word8 -> Word8 -> Word8
    forall a. Bits a => a -> a -> a
    .&. Word8
    0b11100000 Word8 -> Word8 -> Bool
    forall a. Eq a => a -> a -> Bool
    == Word8
    0b11000000
          , Word8
    b2 Word8 -> Word8 -> Bool
    forall a. Ord a => a -> a -> Bool
    >= Word8
    0b10000000
          , !Char
    c <- Int -> Char
    chr (Word8 -> Word8 -> Int
    forall {a} {a} {a}.
    (Integral a, Integral a, Bits a, Bits a, Bits a, Num a) =>
    a -> a -> a
    toCodePoint2 Word8
    b1 Word8
    b2)
          , Char -> Bool
    f Char
    c -> (ParserState s, Char) -> Maybe (ParserState s, Char)
    forall a. a -> Maybe a
    Just (Int -> ParserState s -> ParserState s
    forall s. Int -> ParserState s -> ParserState s
    unsafeAdvance Int
    2 ParserState s
    st, Char
    c)
          | Word8
    b1 Word8 -> Word8 -> Word8
    forall a. Bits a => a -> a -> a
    .&. Word8
    0b11110000 Word8 -> Word8 -> Bool
    forall a. Eq a => a -> a -> Bool
    == Word8
    0b11100000
          , Word8
    b2 Word8 -> Word8 -> Bool
    forall a. Ord a => a -> a -> Bool
    >= Word8
    0b10000000
          , Word8
    b3 Word8 -> Word8 -> Bool
    forall a. Ord a => a -> a -> Bool
    >= Word8
    0b10000000
          , !Char
    c <- Int -> Char
    chr (Word8 -> Word8 -> Word8 -> Int
    forall {a} {a} {a} {a}.
    (Integral a, Integral a, Integral a, Bits a, Bits a, Bits a,
     Bits a, Num a) =>
    a -> a -> a -> a
    toCodePoint3 Word8
    b1 Word8
    b2 Word8
    b3)
          , Char -> Bool
    f Char
    c -> (ParserState s, Char) -> Maybe (ParserState s, Char)
    forall a. a -> Maybe a
    Just (Int -> ParserState s -> ParserState s
    forall s. Int -> ParserState s -> ParserState s
    unsafeAdvance Int
    3 ParserState s
    st, Char
    c)
          | Word8
    b1 Word8 -> Word8 -> Word8
    forall a. Bits a => a -> a -> a
    .&. Word8
    0b11111000 Word8 -> Word8 -> Bool
    forall a. Eq a => a -> a -> Bool
    == Word8
    0b11110000
          , Word8
    b2 Word8 -> Word8 -> Bool
    forall a. Ord a => a -> a -> Bool
    >= Word8
    0b10000000
          , Word8
    b3 Word8 -> Word8 -> Bool
    forall a. Ord a => a -> a -> Bool
    >= Word8
    0b10000000
          , Word8
    b4 Word8 -> Word8 -> Bool
    forall a. Ord a => a -> a -> Bool
    >= Word8
    0b10000000
          , !Char
    c <- Int -> Char
    chr (Word8 -> Word8 -> Word8 -> Word8 -> Int
    forall {a} {a} {a} {a} {a}.
    (Integral a, Integral a, Integral a, Integral a, Bits a, Bits a,
     Bits a, Bits a, Bits a, Num a) =>
    a -> a -> a -> a -> a
    toCodePoint4 Word8
    b1 Word8
    b2 Word8
    b3 Word8
    b4)
          , Char -> Bool
    f Char
    c -> (ParserState s, Char) -> Maybe (ParserState s, Char)
    forall a. a -> Maybe a
    Just (Int -> ParserState s -> ParserState s
    forall s. Int -> ParserState s -> ParserState s
    unsafeAdvance Int
    4 ParserState s
    st, Char
    c)
          | Bool
    otherwise -> Maybe (ParserState s, Char)
    forall a. Maybe a
    Nothing
     where
      toCodePoint2 :: a -> a -> a
    toCodePoint2 a
    a a
    b =
        (a -> a
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (a
    a a -> a -> a
    forall a. Bits a => a -> a -> a
    .&. a
    0b00011111) a -> Int -> a
    forall a. Bits a => a -> Int -> a
    `shiftL` Int
    6) a -> a -> a
    forall a. Num a => a -> a -> a
    +
         a -> a
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (a
    b a -> a -> a
    forall a. Bits a => a -> a -> a
    .&. a
    0b00111111)
      toCodePoint3 :: a -> a -> a -> a
    toCodePoint3 a
    a a
    b a
    c =
        (a -> a
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (a
    a a -> a -> a
    forall a. Bits a => a -> a -> a
    .&. a
    0b00001111) a -> Int -> a
    forall a. Bits a => a -> Int -> a
    `shiftL` Int
    12) a -> a -> a
    forall a. Num a => a -> a -> a
    +
        (a -> a
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (a
    b a -> a -> a
    forall a. Bits a => a -> a -> a
    .&. a
    0b00111111) a -> Int -> a
    forall a. Bits a => a -> Int -> a
    `shiftL` Int
    6) a -> a -> a
    forall a. Num a => a -> a -> a
    +
         a -> a
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (a
    c a -> a -> a
    forall a. Bits a => a -> a -> a
    .&. a
    0b00111111)
      toCodePoint4 :: a -> a -> a -> a -> a
    toCodePoint4 a
    a a
    b a
    c a
    d =
        (a -> a
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (a
    a a -> a -> a
    forall a. Bits a => a -> a -> a
    .&. a
    0b00000111) a -> Int -> a
    forall a. Bits a => a -> Int -> a
    `shiftL` Int
    18) a -> a -> a
    forall a. Num a => a -> a -> a
    +
        (a -> a
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (a
    b a -> a -> a
    forall a. Bits a => a -> a -> a
    .&. a
    0b00111111) a -> Int -> a
    forall a. Bits a => a -> Int -> a
    `shiftL` Int
    12) a -> a -> a
    forall a. Num a => a -> a -> a
    +
        (a -> a
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (a
    c a -> a -> a
    forall a. Bits a => a -> a -> a
    .&. a
    0b00111111) a -> Int -> a
    forall a. Bits a => a -> Int -> a
    `shiftL` Int
    6) a -> a -> a
    forall a. Num a => a -> a -> a
    +
         a -> a
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (a
    d a -> a -> a
    forall a. Bits a => a -> a -> a
    .&. a
    0b00111111)

    -- | Parse any character. Assumes UTF-8 encoding.
    anyChar :: Parser s Char
    anyChar :: forall s. Parser s Char
    anyChar = (Char -> Bool) -> Parser s Char
    forall s. (Char -> Bool) -> Parser s Char
    satisfy (Bool -> Char -> Bool
    forall a b. a -> b -> a
    const Bool
    True)

    -- | Parse an ASCII character.
    asciiChar :: Char -> Parser s ()
    asciiChar :: forall s. Char -> Parser s ()
    asciiChar !Char
    c = (ParserState s -> Maybe (ParserState s, ())) -> Parser s ()
    forall s a.
    (ParserState s -> Maybe (ParserState s, a)) -> Parser s a
    Parser ((ParserState s -> Maybe (ParserState s, ())) -> Parser s ())
    -> (ParserState s -> Maybe (ParserState s, ())) -> Parser s ()
    forall a b. (a -> b) -> a -> b
    $ \ParserState s
    st ->
      case ParserState s -> Maybe Char
    forall s. ParserState s -> Maybe Char
    current ParserState s
    st of
        Just Char
    d | Char
    d Char -> Char -> Bool
    forall a. Eq a => a -> a -> Bool
    == Char
    c -> (ParserState s, ()) -> Maybe (ParserState s, ())
    forall a. a -> Maybe a
    Just (ParserState s -> ParserState s
    forall s. ParserState s -> ParserState s
    unsafeAdvanceByte ParserState s
    st, ())
        Maybe Char
    _ -> Maybe (ParserState s, ())
    forall a. Maybe a
    Nothing

    -- | Apply parser 0 or more times, discarding result.
    skipMany :: Parser s a -> Parser s ()
    skipMany :: forall s a. Parser s a -> Parser s ()
    skipMany Parser s a
    parser = (ParserState s -> Maybe (ParserState s, ())) -> Parser s ()
    forall s a.
    (ParserState s -> Maybe (ParserState s, a)) -> Parser s a
    Parser ParserState s -> Maybe (ParserState s, ())
    go
     where
       go :: ParserState s -> Maybe (ParserState s, ())
    go ParserState s
    st = case Parser s a -> ParserState s -> Maybe (ParserState s, a)
    forall s a. Parser s a -> ParserState s -> Maybe (ParserState s, a)
    runParser Parser s a
    parser ParserState s
    st of
                 Maybe (ParserState s, a)
    Nothing -> (ParserState s, ()) -> Maybe (ParserState s, ())
    forall a. a -> Maybe a
    Just (ParserState s
    st, ())
                 Just (ParserState s
    st',a
    _) -> ParserState s -> Maybe (ParserState s, ())
    go ParserState s
    st'

    -- | Apply parser 1 or more times, discarding result.
    skipSome :: Parser s a -> Parser s ()
    skipSome :: forall s a. Parser s a -> Parser s ()
    skipSome Parser s a
    parser = Parser s a
    parser Parser s a -> Parser s () -> Parser s ()
    forall a b. Parser s a -> Parser s b -> Parser s b
    forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
    *> Parser s a -> Parser s ()
    forall s a. Parser s a -> Parser s ()
    skipMany Parser s a
    parser

    -- | Succeeds if no more input.
    eof :: Parser s ()
    eof :: forall s. Parser s ()
    eof = (ParserState s -> Maybe (ParserState s, ())) -> Parser s ()
    forall s a.
    (ParserState s -> Maybe (ParserState s, a)) -> Parser s a
    Parser ((ParserState s -> Maybe (ParserState s, ())) -> Parser s ())
    -> (ParserState s -> Maybe (ParserState s, ())) -> Parser s ()
    forall a b. (a -> b) -> a -> b
    $ \ParserState s
    st ->
      case ParserState s -> Maybe Char
    forall s. ParserState s -> Maybe Char
    current ParserState s
    st of
        Maybe Char
    Nothing -> (ParserState s, ()) -> Maybe (ParserState s, ())
    forall a. a -> Maybe a
    Just (ParserState s
    st, ())
        Just Char
    _ -> Maybe (ParserState s, ())
    forall a. Maybe a
    Nothing

    -- | Returns current user state.
    getState :: Parser s s
    getState :: forall s. Parser s s
    getState = (ParserState s -> Maybe (ParserState s, s)) -> Parser s s
    forall s a.
    (ParserState s -> Maybe (ParserState s, a)) -> Parser s a
    Parser ((ParserState s -> Maybe (ParserState s, s)) -> Parser s s)
    -> (ParserState s -> Maybe (ParserState s, s)) -> Parser s s
    forall a b. (a -> b) -> a -> b
    $ \ParserState s
    st -> (ParserState s, s) -> Maybe (ParserState s, s)
    forall a. a -> Maybe a
    Just (ParserState s
    st, ParserState s -> s
    forall a. ParserState a -> a
    userState ParserState s
    st)

    -- | Updates user state.
    updateState :: (s -> s) -> Parser s ()
    updateState :: forall s. (s -> s) -> Parser s ()
    updateState s -> s
    f = (ParserState s -> Maybe (ParserState s, ())) -> Parser s ()
    forall s a.
    (ParserState s -> Maybe (ParserState s, a)) -> Parser s a
    Parser ((ParserState s -> Maybe (ParserState s, ())) -> Parser s ())
    -> (ParserState s -> Maybe (ParserState s, ())) -> Parser s ()
    forall a b. (a -> b) -> a -> b
    $ \ParserState s
    st ->
      (ParserState s, ()) -> Maybe (ParserState s, ())
    forall a. a -> Maybe a
    Just (ParserState s
    st{ userState = f (userState st) }, ())

    -- | Apply a parser, returning its result but not changing state
    -- or advancing.
    lookahead :: Parser s a -> Parser s a
    lookahead :: forall s a. Parser s a -> Parser s a
    lookahead Parser s a
    pa = (ParserState s -> Maybe (ParserState s, a)) -> Parser s a
    forall s a.
    (ParserState s -> Maybe (ParserState s, a)) -> Parser s a
    Parser ((ParserState s -> Maybe (ParserState s, a)) -> Parser s a)
    -> (ParserState s -> Maybe (ParserState s, a)) -> Parser s a
    forall a b. (a -> b) -> a -> b
    $ \ParserState s
    st ->
      case Parser s a -> ParserState s -> Maybe (ParserState s, a)
    forall s a. Parser s a -> ParserState s -> Maybe (ParserState s, a)
    runParser Parser s a
    pa ParserState s
    st of
        Just (ParserState s
    _, a
    x) -> (ParserState s, a) -> Maybe (ParserState s, a)
    forall a. a -> Maybe a
    Just (ParserState s
    st, a
    x)
        Maybe (ParserState s, a)
    Nothing -> Maybe (ParserState s, a)
    forall a. Maybe a
    Nothing

    -- | Succeeds if parser fails.
    fails :: Parser s a -> Parser s ()
    fails :: forall s a. Parser s a -> Parser s ()
    fails Parser s a
    pa = (ParserState s -> Maybe (ParserState s, ())) -> Parser s ()
    forall s a.
    (ParserState s -> Maybe (ParserState s, a)) -> Parser s a
    Parser ((ParserState s -> Maybe (ParserState s, ())) -> Parser s ())
    -> (ParserState s -> Maybe (ParserState s, ())) -> Parser s ()
    forall a b. (a -> b) -> a -> b
    $ \ParserState s
    st ->
      case Parser s a -> ParserState s -> Maybe (ParserState s, a)
    forall s a. Parser s a -> ParserState s -> Maybe (ParserState s, a)
    runParser Parser s a
    pa ParserState s
    st of
        Just (ParserState s, a)
    _ -> Maybe (ParserState s, ())
    forall a. Maybe a
    Nothing
        Maybe (ParserState s, a)
    Nothing -> (ParserState s, ()) -> Maybe (ParserState s, ())
    forall a. a -> Maybe a
    Just (ParserState s
    st, ())

    -- | Always fails.
    failed :: Parser s a
    failed :: forall s a. Parser s a
    failed = (ParserState s -> Maybe (ParserState s, a)) -> Parser s a
    forall s a.
    (ParserState s -> Maybe (ParserState s, a)) -> Parser s a
    Parser ((ParserState s -> Maybe (ParserState s, a)) -> Parser s a)
    -> (ParserState s -> Maybe (ParserState s, a)) -> Parser s a
    forall a b. (a -> b) -> a -> b
    $ Maybe (ParserState s, a)
    -> ParserState s -> Maybe (ParserState s, a)
    forall a b. a -> b -> a
    const Maybe (ParserState s, a)
    forall a. Maybe a
    Nothing

    -- | Returns result of parse together with the bytestring
    -- consumed.
    withByteString :: Parser s a -> Parser s (a, ByteString)
    withByteString :: forall s a. Parser s a -> Parser s (a, ByteString)
    withByteString Parser s a
    pa = (ParserState s -> Maybe (ParserState s, (a, ByteString)))
    -> Parser s (a, ByteString)
    forall s a.
    (ParserState s -> Maybe (ParserState s, a)) -> Parser s a
    Parser ((ParserState s -> Maybe (ParserState s, (a, ByteString)))
     -> Parser s (a, ByteString))
    -> (ParserState s -> Maybe (ParserState s, (a, ByteString)))
    -> Parser s (a, ByteString)
    forall a b. (a -> b) -> a -> b
    $ \ParserState s
    st ->
      case Parser s a -> ParserState s -> Maybe (ParserState s, a)
    forall s a. Parser s a -> ParserState s -> Maybe (ParserState s, a)
    runParser Parser s a
    pa ParserState s
    st of
        Just (ParserState s
    st', a
    x) -> (ParserState s, (a, ByteString))
    -> Maybe (ParserState s, (a, ByteString))
    forall a. a -> Maybe a
    Just (ParserState s
    st', (a
    x, Int -> ByteString -> ByteString
    B8.take (ParserState s -> Int
    forall a. ParserState a -> Int
    offset ParserState s
    st' Int -> Int -> Int
    forall a. Num a => a -> a -> a
    - ParserState s -> Int
    forall a. ParserState a -> Int
    offset ParserState s
    st)
                                        (Int -> ByteString -> ByteString
    B8.drop (ParserState s -> Int
    forall a. ParserState a -> Int
    offset ParserState s
    st) (ParserState s -> ByteString
    forall a. ParserState a -> ByteString
    subject ParserState s
    st))))
        Maybe (ParserState s, a)
    Nothing -> Maybe (ParserState s, (a, ByteString))
    forall a. Maybe a
    Nothing

    -- | Returns bytestring consumed by parse.
    byteStringOf :: Parser s a -> Parser s ByteString
    byteStringOf :: forall s a. Parser s a -> Parser s ByteString
    byteStringOf Parser s a
    pa = (ParserState s -> Maybe (ParserState s, ByteString))
    -> Parser s ByteString
    forall s a.
    (ParserState s -> Maybe (ParserState s, a)) -> Parser s a
    Parser ((ParserState s -> Maybe (ParserState s, ByteString))
     -> Parser s ByteString)
    -> (ParserState s -> Maybe (ParserState s, ByteString))
    -> Parser s ByteString
    forall a b. (a -> b) -> a -> b
    $ \ParserState s
    st ->
      case Parser s a -> ParserState s -> Maybe (ParserState s, a)
    forall s a. Parser s a -> ParserState s -> Maybe (ParserState s, a)
    runParser Parser s a
    pa ParserState s
    st of
        Just (ParserState s
    st', a
    _) -> (ParserState s, ByteString) -> Maybe (ParserState s, ByteString)
    forall a. a -> Maybe a
    Just (ParserState s
    st',
           case [Chunk] -> Int
    forall a. [a] -> Int
    forall (t :: * -> *) a. Foldable t => t a -> Int
    length (ParserState s -> [Chunk]
    forall a. ParserState a -> [Chunk]
    chunks ParserState s
    st) Int -> Int -> Int
    forall a. Num a => a -> a -> a
    - [Chunk] -> Int
    forall a. [a] -> Int
    forall (t :: * -> *) a. Foldable t => t a -> Int
    length (ParserState s -> [Chunk]
    forall a. ParserState a -> [Chunk]
    chunks ParserState s
    st') of
             Int
    0 -> Int -> ByteString -> ByteString
    B8.take (ParserState s -> Int
    forall a. ParserState a -> Int
    offset ParserState s
    st' Int -> Int -> Int
    forall a. Num a => a -> a -> a
    - ParserState s -> Int
    forall a. ParserState a -> Int
    offset ParserState s
    st) (Int -> ByteString -> ByteString
    B8.drop (ParserState s -> Int
    forall a. ParserState a -> Int
    offset ParserState s
    st) (ParserState s -> ByteString
    forall a. ParserState a -> ByteString
    subject ParserState s
    st))
             Int
    n ->
               Int -> ByteString -> ByteString
    B8.drop (ParserState s -> Int
    forall a. ParserState a -> Int
    offset ParserState s
    st) (ParserState s -> ByteString
    forall a. ParserState a -> ByteString
    subject ParserState s
    st) ByteString -> ByteString -> ByteString
    forall a. Semigroup a => a -> a -> a
    <>
                (Chunk -> ByteString) -> [Chunk] -> ByteString
    forall m a. Monoid m => (a -> m) -> [a] -> m
    forall (t :: * -> *) m a.
    (Foldable t, Monoid m) =>
    (a -> m) -> t a -> m
    foldMap Chunk -> ByteString
    chunkBytes (Int -> [Chunk] -> [Chunk]
    forall a. Int -> [a] -> [a]
    take (Int
    n Int -> Int -> Int
    forall a. Num a => a -> a -> a
    - Int
    1) (ParserState s -> [Chunk]
    forall a. ParserState a -> [Chunk]
    chunks ParserState s
    st)) ByteString -> ByteString -> ByteString
    forall a. Semigroup a => a -> a -> a
    <>
                Int -> ByteString -> ByteString
    B8.take (ParserState s -> Int
    forall a. ParserState a -> Int
    offset ParserState s
    st') (ParserState s -> ByteString
    forall a. ParserState a -> ByteString
    subject ParserState s
    st'))
        Maybe (ParserState s, a)
    Nothing -> Maybe (ParserState s, ByteString)
    forall a. Maybe a
    Nothing

    -- | Succeeds if first parser succeeds and second fails, returning
    -- first parser's value.
    notFollowedBy :: Parser s a -> Parser s b -> Parser s a
    notFollowedBy :: forall s a b. Parser s a -> Parser s b -> Parser s a
    notFollowedBy Parser s a
    pa Parser s b
    pb = Parser s a
    pa Parser s a -> Parser s () -> Parser s a
    forall a b. Parser s a -> Parser s b -> Parser s a
    forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
    <* Parser s b -> Parser s ()
    forall s a. Parser s a -> Parser s ()
    fails Parser s b
    pb

    -- | Apply parser but still succeed if it doesn't succeed.
    optional_ :: Parser s a -> Parser s ()
    optional_ :: forall s a. Parser s a -> Parser s ()
    optional_ Parser s a
    pa = Parser s a -> Parser s ()
    forall (f :: * -> *) a. Functor f => f a -> f ()
    void Parser s a
    pa Parser s () -> Parser s () -> Parser s ()
    forall a. Parser s a -> Parser s a -> Parser s a
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    <|> () -> Parser s ()
    forall a. a -> Parser s a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure ()

    -- | Parse a bytestring.
    byteString :: ByteString -> Parser s ()
    byteString :: forall s. ByteString -> Parser s ()
    byteString ByteString
    bs = (ParserState s -> Maybe (ParserState s, ())) -> Parser s ()
    forall s a.
    (ParserState s -> Maybe (ParserState s, a)) -> Parser s a
    Parser ((ParserState s -> Maybe (ParserState s, ())) -> Parser s ())
    -> (ParserState s -> Maybe (ParserState s, ())) -> Parser s ()
    forall a b. (a -> b) -> a -> b
    $ \ParserState s
    st ->
      if ByteString
    bs ByteString -> ByteString -> Bool
    `B8.isPrefixOf` Int -> ByteString -> ByteString
    B8.drop (ParserState s -> Int
    forall a. ParserState a -> Int
    offset ParserState s
    st) (ParserState s -> ByteString
    forall a. ParserState a -> ByteString
    subject ParserState s
    st)
         then (ParserState s, ()) -> Maybe (ParserState s, ())
    forall a. a -> Maybe a
    Just (Int -> ParserState s -> ParserState s
    forall s. Int -> ParserState s -> ParserState s
    unsafeAdvance (ByteString -> Int
    B.length ByteString
    bs) ParserState s
    st, ())
         else Maybe (ParserState s, ())
    forall a. Maybe a
    Nothing

    -- | Returns byte offset in input.
    getOffset :: Parser s Int
    getOffset :: forall s. Parser s Int
    getOffset = (ParserState s -> Maybe (ParserState s, Int)) -> Parser s Int
    forall s a.
    (ParserState s -> Maybe (ParserState s, a)) -> Parser s a
    Parser ((ParserState s -> Maybe (ParserState s, Int)) -> Parser s Int)
    -> (ParserState s -> Maybe (ParserState s, Int)) -> Parser s Int
    forall a b. (a -> b) -> a -> b
    $ \ParserState s
    st -> (ParserState s, Int) -> Maybe (ParserState s, Int)
    forall a. a -> Maybe a
    Just (ParserState s
    st, ParserState s -> Int
    forall a. ParserState a -> Int
    offset ParserState s
    st)

    -- | Returns the line number.
    sourceLine :: Parser s Int
    sourceLine :: forall s. Parser s Int
    sourceLine = (ParserState s -> Maybe (ParserState s, Int)) -> Parser s Int
    forall s a.
    (ParserState s -> Maybe (ParserState s, a)) -> Parser s a
    Parser ((ParserState s -> Maybe (ParserState s, Int)) -> Parser s Int)
    -> (ParserState s -> Maybe (ParserState s, Int)) -> Parser s Int
    forall a b. (a -> b) -> a -> b
    $ \ParserState s
    st -> (ParserState s, Int) -> Maybe (ParserState s, Int)
    forall a. a -> Maybe a
    Just (ParserState s
    st, ParserState s -> Int
    forall a. ParserState a -> Int
    line ParserState s
    st)

    -- | Returns the source column number. (Tab stop is computed at 4.)
    sourceColumn :: Parser st Int
    sourceColumn :: forall s. Parser s Int
    sourceColumn = (ParserState st -> Maybe (ParserState st, Int)) -> Parser st Int
    forall s a.
    (ParserState s -> Maybe (ParserState s, a)) -> Parser s a
    Parser ((ParserState st -> Maybe (ParserState st, Int)) -> Parser st Int)
    -> (ParserState st -> Maybe (ParserState st, Int)) -> Parser st Int
    forall a b. (a -> b) -> a -> b
    $ \ParserState st
    st -> (ParserState st, Int) -> Maybe (ParserState st, Int)
    forall a. a -> Maybe a
    Just (ParserState st
    st, ParserState st -> Int
    forall a. ParserState a -> Int
    column ParserState st
    st)

    -- | Try the first parser: if it succeeds, apply the second,
    -- returning its result, otherwise the third.
    branch :: Parser s b -> Parser s a -> Parser s a -> Parser s a
    branch :: forall s b a. Parser s b -> Parser s a -> Parser s a -> Parser s a
    branch Parser s b
    pa Parser s a
    pb Parser s a
    pc = (ParserState s -> Maybe (ParserState s, a)) -> Parser s a
    forall s a.
    (ParserState s -> Maybe (ParserState s, a)) -> Parser s a
    Parser ((ParserState s -> Maybe (ParserState s, a)) -> Parser s a)
    -> (ParserState s -> Maybe (ParserState s, a)) -> Parser s a
    forall a b. (a -> b) -> a -> b
    $ \ParserState s
    st ->
      case Parser s b -> ParserState s -> Maybe (ParserState s, b)
    forall s a. Parser s a -> ParserState s -> Maybe (ParserState s, a)
    runParser Parser s b
    pa ParserState s
    st of
        Just (ParserState s
    st',b
    _) -> Parser s a -> ParserState s -> Maybe (ParserState s, a)
    forall s a. Parser s a -> ParserState s -> Maybe (ParserState s, a)
    runParser Parser s a
    pb ParserState s
    st'
        Maybe (ParserState s, b)
    Nothing -> Parser s a -> ParserState s -> Maybe (ParserState s, a)
    forall s a. Parser s a -> ParserState s -> Maybe (ParserState s, a)
    runParser Parser s a
    pc ParserState s
    st

    -- | Parse an end of line sequence.
    endline :: Parser s ()
    endline :: forall s. Parser s ()
    endline = Parser s () -> Parser s () -> Parser s () -> Parser s ()
    forall s b a. Parser s b -> Parser s a -> Parser s a -> Parser s a
    branch (Char -> Parser s ()
    forall s. Char -> Parser s ()
    asciiChar Char
    '\r') (Parser s () -> Parser s ()
    forall s a. Parser s a -> Parser s ()
    optional_ (Char -> Parser s ()
    forall s. Char -> Parser s ()
    asciiChar Char
    '\n')) (Char -> Parser s ()
    forall s. Char -> Parser s ()
    asciiChar Char
    '\n')

    -- | Return the rest of line (including the end of line).
    restOfLine :: Parser s ByteString
    restOfLine :: forall s. Parser s ByteString
    restOfLine =
      Parser s () -> Parser s ByteString
    forall s a. Parser s a -> Parser s ByteString
    byteStringOf (Parser s () -> Parser s ByteString)
    -> Parser s () -> Parser s ByteString
    forall a b. (a -> b) -> a -> b
    $
        Parser s () -> Parser s ()
    forall s a. Parser s a -> Parser s ()
    skipMany ((Char -> Bool) -> Parser s ()
    forall s. (Char -> Bool) -> Parser s ()
    skipSatisfyByte (\Char
    c -> Char
    c Char -> Char -> Bool
    forall a. Eq a => a -> a -> Bool
    /= Char
    '\n' Bool -> Bool -> Bool
    && Char
    c Char -> Char -> Bool
    forall a. Eq a => a -> a -> Bool
    /= Char
    '\r'))
          Parser s () -> Parser s () -> Parser s ()
    forall a b. Parser s a -> Parser s b -> Parser s a
    forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
    <* Parser s () -> Parser s ()
    forall s a. Parser s a -> Parser s ()
    optional_ Parser s ()
    forall s. Parser s ()
    endline

    {-# INLINE isWs #-}
    -- | Is space, tab, `\r`, or `\n`.
    isWs :: Char -> Bool
    isWs :: Char -> Bool
    isWs Char
    c = Char
    c Char -> Char -> Bool
    forall a. Eq a => a -> a -> Bool
    == Char
    ' ' Bool -> Bool -> Bool
    || Char
    c Char -> Char -> Bool
    forall a. Eq a => a -> a -> Bool
    == Char
    '\t' Bool -> Bool -> Bool
    || Char
    c Char -> Char -> Bool
    forall a. Eq a => a -> a -> Bool
    == Char
    '\r' Bool -> Bool -> Bool
    || Char
    c Char -> Char -> Bool
    forall a. Eq a => a -> a -> Bool
    == Char
    '\n'

    -- | Skip one space or tab.
    spaceOrTab :: Parser s ()
    spaceOrTab :: forall s. Parser s ()
    spaceOrTab = (ParserState s -> Maybe (ParserState s, ())) -> Parser s ()
    forall s a.
    (ParserState s -> Maybe (ParserState s, a)) -> Parser s a
    Parser ((ParserState s -> Maybe (ParserState s, ())) -> Parser s ())
    -> (ParserState s -> Maybe (ParserState s, ())) -> Parser s ()
    forall a b. (a -> b) -> a -> b
    $ \ParserState s
    st ->
      case ParserState s -> Maybe Char
    forall s. ParserState s -> Maybe Char
    current ParserState s
    st of
        Just Char
    ' ' -> (ParserState s, ()) -> Maybe (ParserState s, ())
    forall a. a -> Maybe a
    Just (ParserState s -> ParserState s
    forall s. ParserState s -> ParserState s
    unsafeAdvanceByte ParserState s
    st, ())
        Just Char
    '\t' -> (ParserState s, ()) -> Maybe (ParserState s, ())
    forall a. a -> Maybe a
    Just (ParserState s -> ParserState s
    forall s. ParserState s -> ParserState s
    unsafeAdvanceByte ParserState s
    st, ())
        Maybe Char
    _ -> Maybe (ParserState s, ())
    forall a. Maybe a
    Nothing

    -- | Skip 1 or more ASCII whitespace.
    ws :: Parser s ()
    ws :: forall s. Parser s ()
    ws = Parser s Char -> Parser s ()
    forall s a. Parser s a -> Parser s ()
    skipSome ((Char -> Bool) -> Parser s Char
    forall s. (Char -> Bool) -> Parser s Char
    satisfyByte Char -> Bool
    isWs)

    -- | Next character is ASCII whitespace.
    followedByWhitespace :: Parser s ()
    followedByWhitespace :: forall s. Parser s ()
    followedByWhitespace = (ParserState s -> Maybe (ParserState s, ())) -> Parser s ()
    forall s a.
    (ParserState s -> Maybe (ParserState s, a)) -> Parser s a
    Parser ((ParserState s -> Maybe (ParserState s, ())) -> Parser s ())
    -> (ParserState s -> Maybe (ParserState s, ())) -> Parser s ()
    forall a b. (a -> b) -> a -> b
    $ \ParserState s
    st ->
      case ParserState s -> Maybe Char
    forall s. ParserState s -> Maybe Char
    current ParserState s
    st of
        Just Char
    c | Char -> Bool
    isWs Char
    c -> (ParserState s, ()) -> Maybe (ParserState s, ())
    forall a. a -> Maybe a
    Just (ParserState s
    st, ())
        Maybe Char
    _ -> Maybe (ParserState s, ())
    forall a. Maybe a
    Nothing

    -- | Followed by 0 or more spaces/tabs and endline or eof.
    followedByBlankLine :: Parser s ()
    followedByBlankLine :: forall s. Parser s ()
    followedByBlankLine = (ParserState s -> Maybe (ParserState s, ())) -> Parser s ()
    forall s a.
    (ParserState s -> Maybe (ParserState s, a)) -> Parser s a
    Parser ((ParserState s -> Maybe (ParserState s, ())) -> Parser s ())
    -> (ParserState s -> Maybe (ParserState s, ())) -> Parser s ()
    forall a b. (a -> b) -> a -> b
    $ \ParserState s
    st ->
      let subj :: ByteString
    subj = ParserState s -> ByteString
    forall a. ParserState a -> ByteString
    subject ParserState s
    st
          !len :: Int
    len = ByteString -> Int
    B8.length ByteString
    subj
          go :: Int -> Maybe (ParserState s, ())
    go !Int
    off
            | Int
    off Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    >= Int
    len
              = (ParserState s, ()) -> Maybe (ParserState s, ())
    forall a. a -> Maybe a
    Just (ParserState s
    st, ())
            | Bool
    otherwise
              = case ByteString -> Int -> Char
    B8.index ByteString
    subj Int
    off of
                  Char
    ' ' -> Int -> Maybe (ParserState s, ())
    go (Int
    off Int -> Int -> Int
    forall a. Num a => a -> a -> a
    + Int
    1)
                  Char
    '\r' -> Int -> Maybe (ParserState s, ())
    go (Int
    off Int -> Int -> Int
    forall a. Num a => a -> a -> a
    + Int
    1)
                  Char
    '\t' -> Int -> Maybe (ParserState s, ())
    go (Int
    off Int -> Int -> Int
    forall a. Num a => a -> a -> a
    + Int
    1)
                  Char
    '\n' -> (ParserState s, ()) -> Maybe (ParserState s, ())
    forall a. a -> Maybe a
    Just (ParserState s
    st, ())
                  Char
    _ -> Maybe (ParserState s, ())
    forall a. Maybe a
    Nothing
      in Int -> Maybe (ParserState s, ())
    go (ParserState s -> Int
    forall a. ParserState a -> Int
    offset ParserState s
    st)

    strToUtf8 :: String -> ByteString
    strToUtf8 :: String -> ByteString
    strToUtf8 = Text -> ByteString
    encodeUtf8 (Text -> ByteString) -> (String -> Text) -> String -> ByteString
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . String -> Text
    T.pack

    utf8ToStr :: ByteString -> String
    utf8ToStr :: ByteString -> String
    utf8ToStr = Text -> String
    T.unpack (Text -> String) -> (ByteString -> Text) -> ByteString -> String
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . OnDecodeError -> ByteString -> Text
    decodeUtf8With OnDecodeError
    lenientDecode