    {-# LANGUAGE FlexibleInstances     #-}
    {-# LANGUAGE MultiParamTypeClasses #-}
    {-# LANGUAGE OverloadedStrings     #-}
    {-# LANGUAGE ScopedTypeVariables   #-}
    {- |
       Module      : Text.Pandoc.Readers.LaTeX.Parsing
       Copyright   : Copyright (C) 2006-2020 John MacFarlane
       License     : GNU GPL, version 2 or above

       Maintainer  : John MacFarlane <jgm@berkeley.edu>
       Stability   : alpha
       Portability : portable

    General parsing types and functions for LaTeX.
    -}
    module Text.Pandoc.Readers.LaTeX.Parsing
      ( DottedNum(..)
      , renderDottedNum
      , incrementDottedNum
      , LaTeXState(..)
      , defaultLaTeXState
      , LP
      , withVerbatimMode
      , rawLaTeXParser
      , applyMacros
      , tokenize
      , untokenize
      , untoken
      , totoks
      , toksToString
      , satisfyTok
      , doMacros
      , doMacros'
      , setpos
      , anyControlSeq
      , anySymbol
      , isNewlineTok
      , isWordTok
      , isArgTok
      , spaces
      , spaces1
      , tokTypeIn
      , controlSeq
      , symbol
      , symbolIn
      , sp
      , whitespace
      , newlineTok
      , comment
      , anyTok
      , singleChar
      , specialChars
      , endline
      , blankline
      , primEscape
      , bgroup
      , egroup
      , grouped
      , braced
      , braced'
      , bracedUrl
      , bracedOrToken
      , bracketed
      , bracketedToks
      , parenWrapped
      , dimenarg
      , ignore
      , withRaw
      ) where

    import Control.Applicative (many, (<|>))
    import Control.Monad
    import Control.Monad.Except (throwError)
    import Control.Monad.Trans (lift)
    import Data.Char (chr, isAlphaNum, isDigit, isLetter, ord)
    import Data.Default
    import Data.List (intercalate)
    import qualified Data.Map as M
    import qualified Data.Set as Set
    import Data.Text (Text)
    import qualified Data.Text as T
    import Text.Pandoc.Builder
    import Text.Pandoc.Class.PandocMonad (PandocMonad, report)
    import Text.Pandoc.Error (PandocError (PandocMacroLoop))
    import Text.Pandoc.Logging
    import Text.Pandoc.Options
    import Text.Pandoc.Parsing hiding (blankline, many, mathDisplay, mathInline,
                                space, spaces, withRaw, (<|>))
    import Text.Pandoc.Readers.LaTeX.Types (ExpansionPoint (..), Macro (..),
                                            ArgSpec (..), Tok (..), TokType (..))
    import Text.Pandoc.Shared
    import Text.Parsec.Pos
    -- import Debug.Trace

    newtype DottedNum = DottedNum [Int]
      deriving (Int -> DottedNum -> ShowS
    [DottedNum] -> ShowS
    DottedNum -> String
    (Int -> DottedNum -> ShowS)
    -> (DottedNum -> String)
    -> ([DottedNum] -> ShowS)
    -> Show DottedNum
    forall a.
    (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
    showList :: [DottedNum] -> ShowS
    $cshowList :: [DottedNum] -> ShowS
    show :: DottedNum -> String
    $cshow :: DottedNum -> String
    showsPrec :: Int -> DottedNum -> ShowS
    $cshowsPrec :: Int -> DottedNum -> ShowS
    Show)

    renderDottedNum :: DottedNum -> T.Text
    renderDottedNum :: DottedNum -> Text
    renderDottedNum (DottedNum xs :: [Int]
    xs) = String -> Text
    T.pack (String -> Text) -> String -> Text
    forall a b. (a -> b) -> a -> b
    $
      String -> [String] -> String
    forall a. [a] -> [[a]] -> [a]
    intercalate "." ((Int -> String) -> [Int] -> [String]
    forall a b. (a -> b) -> [a] -> [b]
    map Int -> String
    forall a. Show a => a -> String
    show [Int]
    xs)

    incrementDottedNum :: Int -> DottedNum -> DottedNum
    incrementDottedNum :: Int -> DottedNum -> DottedNum
    incrementDottedNum level :: Int
    level (DottedNum ns :: [Int]
    ns) = [Int] -> DottedNum
    DottedNum ([Int] -> DottedNum) -> [Int] -> DottedNum
    forall a b. (a -> b) -> a -> b
    $
      case [Int] -> [Int]
    forall a. [a] -> [a]
    reverse (Int -> [Int] -> [Int]
    forall a. Int -> [a] -> [a]
    take Int
    level ([Int]
    ns [Int] -> [Int] -> [Int]
    forall a. [a] -> [a] -> [a]
    ++ Int -> [Int]
    forall a. a -> [a]
    repeat 0)) of
           (x :: Int
    x:xs :: [Int]
    xs) -> [Int] -> [Int]
    forall a. [a] -> [a]
    reverse (Int
    xInt -> Int -> Int
    forall a. Num a => a -> a -> a
    +1 Int -> [Int] -> [Int]
    forall a. a -> [a] -> [a]
    : [Int]
    xs)
           []     -> []  -- shouldn't happen

    data LaTeXState = LaTeXState{ LaTeXState -> ReaderOptions
    sOptions       :: ReaderOptions
                                , LaTeXState -> Meta
    sMeta          :: Meta
                                , LaTeXState -> QuoteContext
    sQuoteContext  :: QuoteContext
                                , LaTeXState -> Map Text Macro
    sMacros        :: M.Map Text Macro
                                , LaTeXState -> [Text]
    sContainers    :: [Text]
                                , LaTeXState -> [LogMessage]
    sLogMessages   :: [LogMessage]
                                , LaTeXState -> Set Text
    sIdentifiers   :: Set.Set Text
                                , LaTeXState -> Bool
    sVerbatimMode  :: Bool
                                , LaTeXState -> Maybe Inlines
    sCaption       :: Maybe Inlines
                                , LaTeXState -> Bool
    sInListItem    :: Bool
                                , LaTeXState -> Bool
    sInTableCell   :: Bool
                                , LaTeXState -> DottedNum
    sLastHeaderNum :: DottedNum
                                , LaTeXState -> DottedNum
    sLastFigureNum :: DottedNum
                                , LaTeXState -> DottedNum
    sLastTableNum  :: DottedNum
                                , LaTeXState -> Maybe Text
    sLastLabel     :: Maybe Text
                                , LaTeXState -> Map Text [Inline]
    sLabels        :: M.Map Text [Inline]
                                , LaTeXState -> Bool
    sHasChapters   :: Bool
                                , LaTeXState -> Map Text Bool
    sToggles       :: M.Map Text Bool
                                , LaTeXState -> Bool
    sExpanded      :: Bool
                                }
         deriving Int -> LaTeXState -> ShowS
    [LaTeXState] -> ShowS
    LaTeXState -> String
    (Int -> LaTeXState -> ShowS)
    -> (LaTeXState -> String)
    -> ([LaTeXState] -> ShowS)
    -> Show LaTeXState
    forall a.
    (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
    showList :: [LaTeXState] -> ShowS
    $cshowList :: [LaTeXState] -> ShowS
    show :: LaTeXState -> String
    $cshow :: LaTeXState -> String
    showsPrec :: Int -> LaTeXState -> ShowS
    $cshowsPrec :: Int -> LaTeXState -> ShowS
    Show

    defaultLaTeXState :: LaTeXState
    defaultLaTeXState :: LaTeXState
    defaultLaTeXState = LaTeXState :: ReaderOptions
    -> Meta
    -> QuoteContext
    -> Map Text Macro
    -> [Text]
    -> [LogMessage]
    -> Set Text
    -> Bool
    -> Maybe Inlines
    -> Bool
    -> Bool
    -> DottedNum
    -> DottedNum
    -> DottedNum
    -> Maybe Text
    -> Map Text [Inline]
    -> Bool
    -> Map Text Bool
    -> Bool
    -> LaTeXState
    LaTeXState{ sOptions :: ReaderOptions
    sOptions       = ReaderOptions
    forall a. Default a => a
    def
                                  , sMeta :: Meta
    sMeta          = Meta
    nullMeta
                                  , sQuoteContext :: QuoteContext
    sQuoteContext  = QuoteContext
    NoQuote
                                  , sMacros :: Map Text Macro
    sMacros        = Map Text Macro
    forall k a. Map k a
    M.empty
                                  , sContainers :: [Text]
    sContainers    = []
                                  , sLogMessages :: [LogMessage]
    sLogMessages   = []
                                  , sIdentifiers :: Set Text
    sIdentifiers   = Set Text
    forall a. Set a
    Set.empty
                                  , sVerbatimMode :: Bool
    sVerbatimMode  = Bool
    False
                                  , sCaption :: Maybe Inlines
    sCaption       = Maybe Inlines
    forall a. Maybe a
    Nothing
                                  , sInListItem :: Bool
    sInListItem    = Bool
    False
                                  , sInTableCell :: Bool
    sInTableCell   = Bool
    False
                                  , sLastHeaderNum :: DottedNum
    sLastHeaderNum = [Int] -> DottedNum
    DottedNum []
                                  , sLastFigureNum :: DottedNum
    sLastFigureNum = [Int] -> DottedNum
    DottedNum []
                                  , sLastTableNum :: DottedNum
    sLastTableNum  = [Int] -> DottedNum
    DottedNum []
                                  , sLastLabel :: Maybe Text
    sLastLabel     = Maybe Text
    forall a. Maybe a
    Nothing
                                  , sLabels :: Map Text [Inline]
    sLabels        = Map Text [Inline]
    forall k a. Map k a
    M.empty
                                  , sHasChapters :: Bool
    sHasChapters   = Bool
    False
                                  , sToggles :: Map Text Bool
    sToggles       = Map Text Bool
    forall k a. Map k a
    M.empty
                                  , sExpanded :: Bool
    sExpanded      = Bool
    False
                                  }

    instance PandocMonad m => HasQuoteContext LaTeXState m where
      getQuoteContext :: ParsecT s LaTeXState m QuoteContext
    getQuoteContext = LaTeXState -> QuoteContext
    sQuoteContext (LaTeXState -> QuoteContext)
    -> ParsecT s LaTeXState m LaTeXState
    -> ParsecT s LaTeXState m QuoteContext
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> ParsecT s LaTeXState m LaTeXState
    forall (m :: * -> *) s u. Monad m => ParsecT s u m u
    getState
      withQuoteContext :: QuoteContext
    -> ParsecT s LaTeXState m a -> ParsecT s LaTeXState m a
    withQuoteContext context :: QuoteContext
    context parser :: ParsecT s LaTeXState m a
    parser = do
        LaTeXState
    oldState <- ParsecT s LaTeXState m LaTeXState
    forall (m :: * -> *) s u. Monad m => ParsecT s u m u
    getState
        let oldQuoteContext :: QuoteContext
    oldQuoteContext = LaTeXState -> QuoteContext
    sQuoteContext LaTeXState
    oldState
        LaTeXState -> ParsecT s LaTeXState m ()
    forall (m :: * -> *) u s. Monad m => u -> ParsecT s u m ()
    setState LaTeXState
    oldState { sQuoteContext :: QuoteContext
    sQuoteContext = QuoteContext
    context }
        a
    result <- ParsecT s LaTeXState m a
    parser
        LaTeXState
    newState <- ParsecT s LaTeXState m LaTeXState
    forall (m :: * -> *) s u. Monad m => ParsecT s u m u
    getState
        LaTeXState -> ParsecT s LaTeXState m ()
    forall (m :: * -> *) u s. Monad m => u -> ParsecT s u m ()
    setState LaTeXState
    newState { sQuoteContext :: QuoteContext
    sQuoteContext = QuoteContext
    oldQuoteContext }
        a -> ParsecT s LaTeXState m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return a
    result

    instance HasLogMessages LaTeXState where
      addLogMessage :: LogMessage -> LaTeXState -> LaTeXState
    addLogMessage msg :: LogMessage
    msg st :: LaTeXState
    st = LaTeXState
    st{ sLogMessages :: [LogMessage]
    sLogMessages = LogMessage
    msg LogMessage -> [LogMessage] -> [LogMessage]
    forall a. a -> [a] -> [a]
    : LaTeXState -> [LogMessage]
    sLogMessages LaTeXState
    st }
      getLogMessages :: LaTeXState -> [LogMessage]
    getLogMessages st :: LaTeXState
    st = [LogMessage] -> [LogMessage]
    forall a. [a] -> [a]
    reverse ([LogMessage] -> [LogMessage]) -> [LogMessage] -> [LogMessage]
    forall a b. (a -> b) -> a -> b
    $ LaTeXState -> [LogMessage]
    sLogMessages LaTeXState
    st

    instance HasIdentifierList LaTeXState where
      extractIdentifierList :: LaTeXState -> Set Text
    extractIdentifierList     = LaTeXState -> Set Text
    sIdentifiers
      updateIdentifierList :: (Set Text -> Set Text) -> LaTeXState -> LaTeXState
    updateIdentifierList f :: Set Text -> Set Text
    f st :: LaTeXState
    st = LaTeXState
    st{ sIdentifiers :: Set Text
    sIdentifiers = Set Text -> Set Text
    f (Set Text -> Set Text) -> Set Text -> Set Text
    forall a b. (a -> b) -> a -> b
    $ LaTeXState -> Set Text
    sIdentifiers LaTeXState
    st }

    instance HasIncludeFiles LaTeXState where
      getIncludeFiles :: LaTeXState -> [Text]
    getIncludeFiles = LaTeXState -> [Text]
    sContainers
      addIncludeFile :: Text -> LaTeXState -> LaTeXState
    addIncludeFile f :: Text
    f s :: LaTeXState
    s = LaTeXState
    s{ sContainers :: [Text]
    sContainers = Text
    f Text -> [Text] -> [Text]
    forall a. a -> [a] -> [a]
    : LaTeXState -> [Text]
    sContainers LaTeXState
    s }
      dropLatestIncludeFile :: LaTeXState -> LaTeXState
    dropLatestIncludeFile s :: LaTeXState
    s = LaTeXState
    s { sContainers :: [Text]
    sContainers = Int -> [Text] -> [Text]
    forall a. Int -> [a] -> [a]
    drop 1 ([Text] -> [Text]) -> [Text] -> [Text]
    forall a b. (a -> b) -> a -> b
    $ LaTeXState -> [Text]
    sContainers LaTeXState
    s }

    instance HasMacros LaTeXState where
      extractMacros :: LaTeXState -> Map Text Macro
    extractMacros  st :: LaTeXState
    st  = LaTeXState -> Map Text Macro
    sMacros LaTeXState
    st
      updateMacros :: (Map Text Macro -> Map Text Macro) -> LaTeXState -> LaTeXState
    updateMacros f :: Map Text Macro -> Map Text Macro
    f st :: LaTeXState
    st  = LaTeXState
    st{ sMacros :: Map Text Macro
    sMacros = Map Text Macro -> Map Text Macro
    f (LaTeXState -> Map Text Macro
    sMacros LaTeXState
    st) }

    instance HasReaderOptions LaTeXState where
      extractReaderOptions :: LaTeXState -> ReaderOptions
    extractReaderOptions = LaTeXState -> ReaderOptions
    sOptions

    instance HasMeta LaTeXState where
      setMeta :: Text -> b -> LaTeXState -> LaTeXState
    setMeta field :: Text
    field val :: b
    val st :: LaTeXState
    st =
        LaTeXState
    st{ sMeta :: Meta
    sMeta = Text -> b -> Meta -> Meta
    forall a b. (HasMeta a, ToMetaValue b) => Text -> b -> a -> a
    setMeta Text
    field b
    val (Meta -> Meta) -> Meta -> Meta
    forall a b. (a -> b) -> a -> b
    $ LaTeXState -> Meta
    sMeta LaTeXState
    st }
      deleteMeta :: Text -> LaTeXState -> LaTeXState
    deleteMeta field :: Text
    field st :: LaTeXState
    st =
        LaTeXState
    st{ sMeta :: Meta
    sMeta = Text -> Meta -> Meta
    forall a. HasMeta a => Text -> a -> a
    deleteMeta Text
    field (Meta -> Meta) -> Meta -> Meta
    forall a b. (a -> b) -> a -> b
    $ LaTeXState -> Meta
    sMeta LaTeXState
    st }

    instance Default LaTeXState where
      def :: LaTeXState
    def = LaTeXState
    defaultLaTeXState

    type LP m = ParserT [Tok] LaTeXState m

    withVerbatimMode :: PandocMonad m => LP m a -> LP m a
    withVerbatimMode :: LP m a -> LP m a
    withVerbatimMode parser :: LP m a
    parser = do
      Bool
    alreadyVerbatimMode <- LaTeXState -> Bool
    sVerbatimMode (LaTeXState -> Bool)
    -> ParsecT [Tok] LaTeXState m LaTeXState
    -> ParsecT [Tok] LaTeXState m Bool
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> ParsecT [Tok] LaTeXState m LaTeXState
    forall (m :: * -> *) s u. Monad m => ParsecT s u m u
    getState
      if Bool
    alreadyVerbatimMode
         then LP m a
    parser
         else do
           (LaTeXState -> LaTeXState) -> ParsecT [Tok] LaTeXState m ()
    forall (m :: * -> *) u s. Monad m => (u -> u) -> ParsecT s u m ()
    updateState ((LaTeXState -> LaTeXState) -> ParsecT [Tok] LaTeXState m ())
    -> (LaTeXState -> LaTeXState) -> ParsecT [Tok] LaTeXState m ()
    forall a b. (a -> b) -> a -> b
    $ \st :: LaTeXState
    st -> LaTeXState
    st{ sVerbatimMode :: Bool
    sVerbatimMode = Bool
    True }
           a
    result <- LP m a
    parser
           (LaTeXState -> LaTeXState) -> ParsecT [Tok] LaTeXState m ()
    forall (m :: * -> *) u s. Monad m => (u -> u) -> ParsecT s u m ()
    updateState ((LaTeXState -> LaTeXState) -> ParsecT [Tok] LaTeXState m ())
    -> (LaTeXState -> LaTeXState) -> ParsecT [Tok] LaTeXState m ()
    forall a b. (a -> b) -> a -> b
    $ \st :: LaTeXState
    st -> LaTeXState
    st{ sVerbatimMode :: Bool
    sVerbatimMode = Bool
    False }
           a -> LP m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return a
    result

    rawLaTeXParser :: (PandocMonad m, HasMacros s, HasReaderOptions s)
                   => [Tok] -> Bool -> LP m a -> LP m a
                   -> ParserT Text s m (a, Text)
    rawLaTeXParser :: [Tok] -> Bool -> LP m a -> LP m a -> ParserT Text s m (a, Text)
    rawLaTeXParser toks :: [Tok]
    toks retokenize :: Bool
    retokenize parser :: LP m a
    parser valParser :: LP m a
    valParser = do
      s
    pstate <- ParsecT Text s m s
    forall (m :: * -> *) s u. Monad m => ParsecT s u m u
    getState
      let lstate :: LaTeXState
    lstate = LaTeXState
    forall a. Default a => a
    def{ sOptions :: ReaderOptions
    sOptions = s -> ReaderOptions
    forall st. HasReaderOptions st => st -> ReaderOptions
    extractReaderOptions s
    pstate }
      let lstate' :: LaTeXState
    lstate' = LaTeXState
    lstate { sMacros :: Map Text Macro
    sMacros = s -> Map Text Macro
    forall st. HasMacros st => st -> Map Text Macro
    extractMacros s
    pstate }
      let rawparser :: ParsecT [Tok] LaTeXState m ((a, [Tok]), LaTeXState)
    rawparser = (,) ((a, [Tok]) -> LaTeXState -> ((a, [Tok]), LaTeXState))
    -> ParsecT [Tok] LaTeXState m (a, [Tok])
    -> ParsecT
         [Tok] LaTeXState m (LaTeXState -> ((a, [Tok]), LaTeXState))
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> LP m a -> ParsecT [Tok] LaTeXState m (a, [Tok])
    forall (m :: * -> *) a. PandocMonad m => LP m a -> LP m (a, [Tok])
    withRaw LP m a
    valParser ParsecT [Tok] LaTeXState m (LaTeXState -> ((a, [Tok]), LaTeXState))
    -> ParsecT [Tok] LaTeXState m LaTeXState
    -> ParsecT [Tok] LaTeXState m ((a, [Tok]), LaTeXState)
    forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
    <*> ParsecT [Tok] LaTeXState m LaTeXState
    forall (m :: * -> *) s u. Monad m => ParsecT s u m u
    getState
      Either ParseError [Tok]
    res' <- m (Either ParseError [Tok])
    -> ParsecT Text s m (Either ParseError [Tok])
    forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
    (MonadTrans t, Monad m) =>
    m a -> t m a
    lift (m (Either ParseError [Tok])
     -> ParsecT Text s m (Either ParseError [Tok]))
    -> m (Either ParseError [Tok])
    -> ParsecT Text s m (Either ParseError [Tok])
    forall a b. (a -> b) -> a -> b
    $ ParsecT [Tok] LaTeXState m [Tok]
    -> LaTeXState -> String -> [Tok] -> m (Either ParseError [Tok])
    forall s (m :: * -> *) t u a.
    Stream s m t =>
    ParsecT s u m a -> u -> String -> s -> m (Either ParseError a)
    runParserT ((a, [Tok]) -> [Tok]
    forall a b. (a, b) -> b
    snd ((a, [Tok]) -> [Tok])
    -> ParsecT [Tok] LaTeXState m (a, [Tok])
    -> ParsecT [Tok] LaTeXState m [Tok]
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> LP m a -> ParsecT [Tok] LaTeXState m (a, [Tok])
    forall (m :: * -> *) a. PandocMonad m => LP m a -> LP m (a, [Tok])
    withRaw LP m a
    parser) LaTeXState
    lstate "chunk" [Tok]
    toks
      case Either ParseError [Tok]
    res' of
           Left _    -> ParserT Text s m (a, Text)
    forall (m :: * -> *) a. MonadPlus m => m a
    mzero
           Right toks' :: [Tok]
    toks' -> do
             Either ParseError ((a, [Tok]), LaTeXState)
    res <- m (Either ParseError ((a, [Tok]), LaTeXState))
    -> ParsecT Text s m (Either ParseError ((a, [Tok]), LaTeXState))
    forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
    (MonadTrans t, Monad m) =>
    m a -> t m a
    lift (m (Either ParseError ((a, [Tok]), LaTeXState))
     -> ParsecT Text s m (Either ParseError ((a, [Tok]), LaTeXState)))
    -> m (Either ParseError ((a, [Tok]), LaTeXState))
    -> ParsecT Text s m (Either ParseError ((a, [Tok]), LaTeXState))
    forall a b. (a -> b) -> a -> b
    $ ParsecT [Tok] LaTeXState m ((a, [Tok]), LaTeXState)
    -> LaTeXState
    -> String
    -> [Tok]
    -> m (Either ParseError ((a, [Tok]), LaTeXState))
    forall s (m :: * -> *) t u a.
    Stream s m t =>
    ParsecT s u m a -> u -> String -> s -> m (Either ParseError a)
    runParserT (do Bool
    -> ParsecT [Tok] LaTeXState m () -> ParsecT [Tok] LaTeXState m ()
    forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
    when Bool
    retokenize (ParsecT [Tok] LaTeXState m () -> ParsecT [Tok] LaTeXState m ())
    -> ParsecT [Tok] LaTeXState m () -> ParsecT [Tok] LaTeXState m ()
    forall a b. (a -> b) -> a -> b
    $ do
                                            -- retokenize, applying macros
                                            ParsecT [Tok] LaTeXState m ()
    forall (m :: * -> *). PandocMonad m => LP m ()
    doMacros
                                            [Tok]
    ts <- ParsecT [Tok] LaTeXState m Tok -> ParsecT [Tok] LaTeXState m [Tok]
    forall (f :: * -> *) a. Alternative f => f a -> f [a]
    many ((Tok -> Bool) -> ParsecT [Tok] LaTeXState m Tok
    forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
    satisfyTok (Bool -> Tok -> Bool
    forall a b. a -> b -> a
    const Bool
    True))
                                            [Tok] -> ParsecT [Tok] LaTeXState m ()
    forall (m :: * -> *) s u. Monad m => s -> ParsecT s u m ()
    setInput [Tok]
    ts
                                          ParsecT [Tok] LaTeXState m ((a, [Tok]), LaTeXState)
    rawparser)
                            LaTeXState
    lstate' "chunk" [Tok]
    toks'
             case Either ParseError ((a, [Tok]), LaTeXState)
    res of
                  Left _    -> ParserT Text s m (a, Text)
    forall (m :: * -> *) a. MonadPlus m => m a
    mzero
                  Right ((val :: a
    val, raw :: [Tok]
    raw), st :: LaTeXState
    st) -> do
                    (s -> s) -> ParsecT Text s m ()
    forall (m :: * -> *) u s. Monad m => (u -> u) -> ParsecT s u m ()
    updateState ((Map Text Macro -> Map Text Macro) -> s -> s
    forall st.
    HasMacros st =>
    (Map Text Macro -> Map Text Macro) -> st -> st
    updateMacros (LaTeXState -> Map Text Macro
    sMacros LaTeXState
    st Map Text Macro -> Map Text Macro -> Map Text Macro
    forall a. Semigroup a => a -> a -> a
    <>))
                    Text
    _ <- Int -> ParserT Text s m Text
    forall (m :: * -> *) st. Monad m => Int -> ParserT Text st m Text
    takeP (Text -> Int
    T.length ([Tok] -> Text
    untokenize [Tok]
    toks'))
                    let result :: Text
    result = [Tok] -> Text
    untokenize [Tok]
    raw
                    -- ensure we end with space if input did, see #4442
                    let result' :: Text
    result' =
                          case [Tok] -> [Tok]
    forall a. [a] -> [a]
    reverse [Tok]
    toks' of
                            (Tok _ (CtrlSeq _) t :: Text
    t : _)
                             | " " Text -> Text -> Bool
    `T.isSuffixOf` Text
    t
                             , Bool -> Bool
    not (" " Text -> Text -> Bool
    `T.isSuffixOf` Text
    result)
                              -> Text
    result Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> " "
                            _ -> Text
    result
                    (a, Text) -> ParserT Text s m (a, Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (a
    val, Text
    result')

    applyMacros :: (PandocMonad m, HasMacros s, HasReaderOptions s)
                => Text -> ParserT Text s m Text
    applyMacros :: Text -> ParserT Text s m Text
    applyMacros s :: Text
    s = (Extension -> ParserT Text s m ()
    forall s (m :: * -> *) a st.
    (Stream s m a, HasReaderOptions st) =>
    Extension -> ParserT s st m ()
    guardDisabled Extension
    Ext_latex_macros ParserT Text s m ()
    -> ParserT Text s m Text -> ParserT Text s m Text
    forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
    >> Text -> ParserT Text s m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return Text
    s) ParserT Text s m Text
    -> ParserT Text s m Text -> ParserT Text s m Text
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    <|>
       do let retokenize :: ParsecT [Tok] LaTeXState (ParsecT Text s m) Text
    retokenize = [Tok] -> Text
    untokenize ([Tok] -> Text)
    -> ParsecT [Tok] LaTeXState (ParsecT Text s m) [Tok]
    -> ParsecT [Tok] LaTeXState (ParsecT Text s m) Text
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> ParsecT [Tok] LaTeXState (ParsecT Text s m) Tok
    -> ParsecT [Tok] LaTeXState (ParsecT Text s m) [Tok]
    forall (f :: * -> *) a. Alternative f => f a -> f [a]
    many ((Tok -> Bool) -> ParsecT [Tok] LaTeXState (ParsecT Text s m) Tok
    forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
    satisfyTok (Bool -> Tok -> Bool
    forall a b. a -> b -> a
    const Bool
    True))
          s
    pstate <- ParsecT Text s m s
    forall (m :: * -> *) s u. Monad m => ParsecT s u m u
    getState
          let lstate :: LaTeXState
    lstate = LaTeXState
    forall a. Default a => a
    def{ sOptions :: ReaderOptions
    sOptions = s -> ReaderOptions
    forall st. HasReaderOptions st => st -> ReaderOptions
    extractReaderOptions s
    pstate
                          , sMacros :: Map Text Macro
    sMacros  = s -> Map Text Macro
    forall st. HasMacros st => st -> Map Text Macro
    extractMacros s
    pstate }
          Either ParseError Text
    res <- ParsecT [Tok] LaTeXState (ParsecT Text s m) Text
    -> LaTeXState
    -> String
    -> [Tok]
    -> ParsecT Text s m (Either ParseError Text)
    forall s (m :: * -> *) t u a.
    Stream s m t =>
    ParsecT s u m a -> u -> String -> s -> m (Either ParseError a)
    runParserT ParsecT [Tok] LaTeXState (ParsecT Text s m) Text
    retokenize LaTeXState
    lstate "math" (String -> Text -> [Tok]
    tokenize "math" Text
    s)
          case Either ParseError Text
    res of
               Left e :: ParseError
    e   -> String -> ParserT Text s m Text
    forall (m :: * -> *) a. MonadFail m => String -> m a
    Prelude.fail (ParseError -> String
    forall a. Show a => a -> String
    show ParseError
    e)
               Right s' :: Text
    s' -> Text -> ParserT Text s m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return Text
    s'

    tokenize :: SourceName -> Text -> [Tok]
    tokenize :: String -> Text -> [Tok]
    tokenize sourcename :: String
    sourcename = SourcePos -> Text -> [Tok]
    totoks (String -> SourcePos
    initialPos String
    sourcename)

    totoks :: SourcePos -> Text -> [Tok]
    totoks :: SourcePos -> Text -> [Tok]
    totoks pos :: SourcePos
    pos t :: Text
    t =
      case Text -> Maybe (Char, Text)
    T.uncons Text
    t of
           Nothing        -> []
           Just (c :: Char
    c, rest :: Text
    rest)
             | Char
    c Char -> Char -> Bool
    forall a. Eq a => a -> a -> Bool
    == '\n' ->
               SourcePos -> TokType -> Text -> Tok
    Tok SourcePos
    pos TokType
    Newline "\n"
               Tok -> [Tok] -> [Tok]
    forall a. a -> [a] -> [a]
    : SourcePos -> Text -> [Tok]
    totoks (SourcePos -> Int -> SourcePos
    setSourceColumn (SourcePos -> Int -> SourcePos
    incSourceLine SourcePos
    pos 1) 1) Text
    rest
             | Char -> Bool
    isSpaceOrTab Char
    c ->
               let (sps :: Text
    sps, rest' :: Text
    rest') = (Char -> Bool) -> Text -> (Text, Text)
    T.span Char -> Bool
    isSpaceOrTab Text
    t
               in  SourcePos -> TokType -> Text -> Tok
    Tok SourcePos
    pos TokType
    Spaces Text
    sps
                   Tok -> [Tok] -> [Tok]
    forall a. a -> [a] -> [a]
    : SourcePos -> Text -> [Tok]
    totoks (SourcePos -> Int -> SourcePos
    incSourceColumn SourcePos
    pos (Text -> Int
    T.length Text
    sps))
                     Text
    rest'
             | Char -> Bool
    isAlphaNum Char
    c ->
               let (ws :: Text
    ws, rest' :: Text
    rest') = (Char -> Bool) -> Text -> (Text, Text)
    T.span Char -> Bool
    isAlphaNum Text
    t
               in  SourcePos -> TokType -> Text -> Tok
    Tok SourcePos
    pos TokType
    Word Text
    ws
                   Tok -> [Tok] -> [Tok]
    forall a. a -> [a] -> [a]
    : SourcePos -> Text -> [Tok]
    totoks (SourcePos -> Int -> SourcePos
    incSourceColumn SourcePos
    pos (Text -> Int
    T.length Text
    ws)) Text
    rest'
             | Char
    c Char -> Char -> Bool
    forall a. Eq a => a -> a -> Bool
    == '%' ->
               let (cs :: Text
    cs, rest' :: Text
    rest') = (Char -> Bool) -> Text -> (Text, Text)
    T.break (Char -> Char -> Bool
    forall a. Eq a => a -> a -> Bool
    == '\n') Text
    rest
               in  SourcePos -> TokType -> Text -> Tok
    Tok SourcePos
    pos TokType
    Comment ("%" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    cs)
                   Tok -> [Tok] -> [Tok]
    forall a. a -> [a] -> [a]
    : SourcePos -> Text -> [Tok]
    totoks (SourcePos -> Int -> SourcePos
    incSourceColumn SourcePos
    pos (1 Int -> Int -> Int
    forall a. Num a => a -> a -> a
    + Text -> Int
    T.length Text
    cs)) Text
    rest'
             | Char
    c Char -> Char -> Bool
    forall a. Eq a => a -> a -> Bool
    == '\\' ->
               case Text -> Maybe (Char, Text)
    T.uncons Text
    rest of
                    Nothing -> [SourcePos -> TokType -> Text -> Tok
    Tok SourcePos
    pos (Text -> TokType
    CtrlSeq " ") "\\"]
                    Just (d :: Char
    d, rest' :: Text
    rest')
                      | Char -> Bool
    isLetterOrAt Char
    d ->
                          -- \makeatletter is common in macro defs;
                          -- ideally we should make tokenization sensitive
                          -- to \makeatletter and \makeatother, but this is
                          -- probably best for now
                          let (ws :: Text
    ws, rest'' :: Text
    rest'') = (Char -> Bool) -> Text -> (Text, Text)
    T.span Char -> Bool
    isLetterOrAt Text
    rest
                              (ss :: Text
    ss, rest''' :: Text
    rest''') = (Char -> Bool) -> Text -> (Text, Text)
    T.span Char -> Bool
    isSpaceOrTab Text
    rest''
                          in  SourcePos -> TokType -> Text -> Tok
    Tok SourcePos
    pos (Text -> TokType
    CtrlSeq Text
    ws) ("\\" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    ws Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    ss)
                              Tok -> [Tok] -> [Tok]
    forall a. a -> [a] -> [a]
    : SourcePos -> Text -> [Tok]
    totoks (SourcePos -> Int -> SourcePos
    incSourceColumn SourcePos
    pos
                                   (1 Int -> Int -> Int
    forall a. Num a => a -> a -> a
    + Text -> Int
    T.length Text
    ws Int -> Int -> Int
    forall a. Num a => a -> a -> a
    + Text -> Int
    T.length Text
    ss)) Text
    rest'''
                      | Char -> Bool
    isSpaceOrTab Char
    d Bool -> Bool -> Bool
    || Char
    d Char -> Char -> Bool
    forall a. Eq a => a -> a -> Bool
    == '\n' ->
                          let (w1 :: Text
    w1, r1 :: Text
    r1) = (Char -> Bool) -> Text -> (Text, Text)
    T.span Char -> Bool
    isSpaceOrTab Text
    rest
                              (w2 :: Text
    w2, (w3 :: Text
    w3, r3 :: Text
    r3)) = case Text -> Maybe (Char, Text)
    T.uncons Text
    r1 of
                                              Just ('\n', r2 :: Text
    r2)
                                                      -> (String -> Text
    T.pack "\n",
                                                            (Char -> Bool) -> Text -> (Text, Text)
    T.span Char -> Bool
    isSpaceOrTab Text
    r2)
                                              _ -> (Text
    forall a. Monoid a => a
    mempty, (Text
    forall a. Monoid a => a
    mempty, Text
    r1))
                              ws :: Text
    ws = "\\" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    w1 Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    w2 Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    w3
                          in  case Text -> Maybe (Char, Text)
    T.uncons Text
    r3 of
                                   Just ('\n', _) ->
                                     SourcePos -> TokType -> Text -> Tok
    Tok SourcePos
    pos (Text -> TokType
    CtrlSeq " ") ("\\" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    w1)
                                     Tok -> [Tok] -> [Tok]
    forall a. a -> [a] -> [a]
    : SourcePos -> Text -> [Tok]
    totoks (SourcePos -> Int -> SourcePos
    incSourceColumn SourcePos
    pos (Text -> Int
    T.length Text
    ws))
                                       Text
    r1
                                   _ ->
                                     SourcePos -> TokType -> Text -> Tok
    Tok SourcePos
    pos (Text -> TokType
    CtrlSeq " ") Text
    ws
                                     Tok -> [Tok] -> [Tok]
    forall a. a -> [a] -> [a]
    : SourcePos -> Text -> [Tok]
    totoks (SourcePos -> Int -> SourcePos
    incSourceColumn SourcePos
    pos (Text -> Int
    T.length Text
    ws))
                                       Text
    r3
                      | Bool
    otherwise  ->
                          SourcePos -> TokType -> Text -> Tok
    Tok SourcePos
    pos (Text -> TokType
    CtrlSeq (Char -> Text
    T.singleton Char
    d)) (String -> Text
    T.pack [Char
    c,Char
    d])
                          Tok -> [Tok] -> [Tok]
    forall a. a -> [a] -> [a]
    : SourcePos -> Text -> [Tok]
    totoks (SourcePos -> Int -> SourcePos
    incSourceColumn SourcePos
    pos 2) Text
    rest'
             | Char
    c Char -> Char -> Bool
    forall a. Eq a => a -> a -> Bool
    == '#' ->
               let (t1 :: Text
    t1, t2 :: Text
    t2) = (Char -> Bool) -> Text -> (Text, Text)
    T.span (\d :: Char
    d -> Char
    d Char -> Char -> Bool
    forall a. Ord a => a -> a -> Bool
    >= '0' Bool -> Bool -> Bool
    && Char
    d Char -> Char -> Bool
    forall a. Ord a => a -> a -> Bool
    <= '9') Text
    rest
               in  case Text -> Maybe Int
    forall (m :: * -> *) a. (MonadPlus m, Read a) => Text -> m a
    safeRead Text
    t1 of
                        Just i :: Int
    i ->
                           SourcePos -> TokType -> Text -> Tok
    Tok SourcePos
    pos (Int -> TokType
    Arg Int
    i) ("#" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    t1)
                           Tok -> [Tok] -> [Tok]
    forall a. a -> [a] -> [a]
    : SourcePos -> Text -> [Tok]
    totoks (SourcePos -> Int -> SourcePos
    incSourceColumn SourcePos
    pos (1 Int -> Int -> Int
    forall a. Num a => a -> a -> a
    + Text -> Int
    T.length Text
    t1)) Text
    t2
                        Nothing ->
                           SourcePos -> TokType -> Text -> Tok
    Tok SourcePos
    pos TokType
    Symbol "#"
                           Tok -> [Tok] -> [Tok]
    forall a. a -> [a] -> [a]
    : SourcePos -> Text -> [Tok]
    totoks (SourcePos -> Int -> SourcePos
    incSourceColumn SourcePos
    pos 1) Text
    t2
             | Char
    c Char -> Char -> Bool
    forall a. Eq a => a -> a -> Bool
    == '^' ->
               case Text -> Maybe (Char, Text)
    T.uncons Text
    rest of
                    Just ('^', rest' :: Text
    rest') ->
                      case Text -> Maybe (Char, Text)
    T.uncons Text
    rest' of
                           Just (d :: Char
    d, rest'' :: Text
    rest'')
                             | Char -> Bool
    isLowerHex Char
    d ->
                               case Text -> Maybe (Char, Text)
    T.uncons Text
    rest'' of
                                    Just (e :: Char
    e, rest''' :: Text
    rest''') | Char -> Bool
    isLowerHex Char
    e ->
                                      SourcePos -> TokType -> Text -> Tok
    Tok SourcePos
    pos TokType
    Esc2 (String -> Text
    T.pack ['^','^',Char
    d,Char
    e])
                                      Tok -> [Tok] -> [Tok]
    forall a. a -> [a] -> [a]
    : SourcePos -> Text -> [Tok]
    totoks (SourcePos -> Int -> SourcePos
    incSourceColumn SourcePos
    pos 4) Text
    rest'''
                                    _ ->
                                      SourcePos -> TokType -> Text -> Tok
    Tok SourcePos
    pos TokType
    Esc1 (String -> Text
    T.pack ['^','^',Char
    d])
                                      Tok -> [Tok] -> [Tok]
    forall a. a -> [a] -> [a]
    : SourcePos -> Text -> [Tok]
    totoks (SourcePos -> Int -> SourcePos
    incSourceColumn SourcePos
    pos 3) Text
    rest''
                             | Char
    d Char -> Char -> Bool
    forall a. Ord a => a -> a -> Bool
    < '\128' ->
                                      SourcePos -> TokType -> Text -> Tok
    Tok SourcePos
    pos TokType
    Esc1 (String -> Text
    T.pack ['^','^',Char
    d])
                                      Tok -> [Tok] -> [Tok]
    forall a. a -> [a] -> [a]
    : SourcePos -> Text -> [Tok]
    totoks (SourcePos -> Int -> SourcePos
    incSourceColumn SourcePos
    pos 3) Text
    rest''
                           _ -> SourcePos -> TokType -> Text -> Tok
    Tok SourcePos
    pos TokType
    Symbol "^" Tok -> [Tok] -> [Tok]
    forall a. a -> [a] -> [a]
    :
                                SourcePos -> TokType -> Text -> Tok
    Tok (SourcePos -> Int -> SourcePos
    incSourceColumn SourcePos
    pos 1) TokType
    Symbol "^" Tok -> [Tok] -> [Tok]
    forall a. a -> [a] -> [a]
    :
                                SourcePos -> Text -> [Tok]
    totoks (SourcePos -> Int -> SourcePos
    incSourceColumn SourcePos
    pos 2) Text
    rest'
                    _ -> SourcePos -> TokType -> Text -> Tok
    Tok SourcePos
    pos TokType
    Symbol "^"
                         Tok -> [Tok] -> [Tok]
    forall a. a -> [a] -> [a]
    : SourcePos -> Text -> [Tok]
    totoks (SourcePos -> Int -> SourcePos
    incSourceColumn SourcePos
    pos 1) Text
    rest
             | Bool
    otherwise ->
               SourcePos -> TokType -> Text -> Tok
    Tok SourcePos
    pos TokType
    Symbol (Char -> Text
    T.singleton Char
    c) Tok -> [Tok] -> [Tok]
    forall a. a -> [a] -> [a]
    : SourcePos -> Text -> [Tok]
    totoks (SourcePos -> Int -> SourcePos
    incSourceColumn SourcePos
    pos 1) Text
    rest

    isSpaceOrTab :: Char -> Bool
    isSpaceOrTab :: Char -> Bool
    isSpaceOrTab ' '  = Bool
    True
    isSpaceOrTab '\t' = Bool
    True
    isSpaceOrTab _    = Bool
    False

    isLetterOrAt :: Char -> Bool
    isLetterOrAt :: Char -> Bool
    isLetterOrAt '@' = Bool
    True
    isLetterOrAt c :: Char
    c   = Char -> Bool
    isLetter Char
    c

    isLowerHex :: Char -> Bool
    isLowerHex :: Char -> Bool
    isLowerHex x :: Char
    x = Char
    x Char -> Char -> Bool
    forall a. Ord a => a -> a -> Bool
    >= '0' Bool -> Bool -> Bool
    && Char
    x Char -> Char -> Bool
    forall a. Ord a => a -> a -> Bool
    <= '9' Bool -> Bool -> Bool
    || Char
    x Char -> Char -> Bool
    forall a. Ord a => a -> a -> Bool
    >= 'a' Bool -> Bool -> Bool
    && Char
    x Char -> Char -> Bool
    forall a. Ord a => a -> a -> Bool
    <= 'f'

    untokenize :: [Tok] -> Text
    untokenize :: [Tok] -> Text
    untokenize = (Tok -> Text -> Text) -> Text -> [Tok] -> Text
    forall (t :: * -> *) a b.
    Foldable t =>
    (a -> b -> b) -> b -> t a -> b
    foldr Tok -> Text -> Text
    untokenAccum Text
    forall a. Monoid a => a
    mempty

    untokenAccum :: Tok -> Text -> Text
    untokenAccum :: Tok -> Text -> Text
    untokenAccum (Tok _ (CtrlSeq _) t :: Text
    t) accum :: Text
    accum =
      -- insert space to prevent breaking a control sequence; see #5836
      case (Text -> Maybe (Text, Char)
    T.unsnoc Text
    t, Text -> Maybe (Char, Text)
    T.uncons Text
    accum) of
        (Just (_,c :: Char
    c), Just (d :: Char
    d,_))
          | Char -> Bool
    isLetter Char
    c
          , Char -> Bool
    isLetter Char
    d
          -> Text
    t Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> " " Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    accum
        _ -> Text
    t Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    accum
    untokenAccum (Tok _ _ t :: Text
    t) accum :: Text
    accum = Text
    t Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    accum

    untoken :: Tok -> Text
    untoken :: Tok -> Text
    untoken t :: Tok
    t = Tok -> Text -> Text
    untokenAccum Tok
    t Text
    forall a. Monoid a => a
    mempty

    toksToString :: [Tok] -> String
    toksToString :: [Tok] -> String
    toksToString = Text -> String
    T.unpack (Text -> String) -> ([Tok] -> Text) -> [Tok] -> String
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [Tok] -> Text
    untokenize

    satisfyTok :: PandocMonad m => (Tok -> Bool) -> LP m Tok
    satisfyTok :: (Tok -> Bool) -> LP m Tok
    satisfyTok f :: Tok -> Bool
    f = do
        LP m ()
    forall (m :: * -> *). PandocMonad m => LP m ()
    doMacros -- apply macros on remaining input stream
        Tok
    res <- (Tok -> String)
    -> (SourcePos -> Tok -> [Tok] -> SourcePos)
    -> (Tok -> Maybe Tok)
    -> LP m Tok
    forall s (m :: * -> *) t a u.
    Stream s m t =>
    (t -> String)
    -> (SourcePos -> t -> s -> SourcePos)
    -> (t -> Maybe a)
    -> ParsecT s u m a
    tokenPrim (Text -> String
    T.unpack (Text -> String) -> (Tok -> Text) -> Tok -> String
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Tok -> Text
    untoken) SourcePos -> Tok -> [Tok] -> SourcePos
    updatePos Tok -> Maybe Tok
    matcher
        (LaTeXState -> LaTeXState) -> LP m ()
    forall (m :: * -> *) u s. Monad m => (u -> u) -> ParsecT s u m ()
    updateState ((LaTeXState -> LaTeXState) -> LP m ())
    -> (LaTeXState -> LaTeXState) -> LP m ()
    forall a b. (a -> b) -> a -> b
    $ \st :: LaTeXState
    st -> LaTeXState
    st{ sExpanded :: Bool
    sExpanded = Bool
    False }
        Tok -> LP m Tok
    forall (m :: * -> *) a. Monad m => a -> m a
    return Tok
    res
      where matcher :: Tok -> Maybe Tok
    matcher t :: Tok
    t | Tok -> Bool
    f Tok
    t       = Tok -> Maybe Tok
    forall a. a -> Maybe a
    Just Tok
    t
                      | Bool
    otherwise = Maybe Tok
    forall a. Maybe a
    Nothing
            updatePos :: SourcePos -> Tok -> [Tok] -> SourcePos
            updatePos :: SourcePos -> Tok -> [Tok] -> SourcePos
    updatePos _spos :: SourcePos
    _spos _ (Tok pos :: SourcePos
    pos _ _ : _) = SourcePos
    pos
            updatePos spos :: SourcePos
    spos _ []                 = SourcePos -> Int -> SourcePos
    incSourceColumn SourcePos
    spos 1

    doMacros :: PandocMonad m => LP m ()
    doMacros :: LP m ()
    doMacros = do
      Bool
    expanded <- LaTeXState -> Bool
    sExpanded (LaTeXState -> Bool)
    -> ParsecT [Tok] LaTeXState m LaTeXState
    -> ParsecT [Tok] LaTeXState m Bool
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> ParsecT [Tok] LaTeXState m LaTeXState
    forall (m :: * -> *) s u. Monad m => ParsecT s u m u
    getState
      Bool
    verbatimMode <- LaTeXState -> Bool
    sVerbatimMode (LaTeXState -> Bool)
    -> ParsecT [Tok] LaTeXState m LaTeXState
    -> ParsecT [Tok] LaTeXState m Bool
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> ParsecT [Tok] LaTeXState m LaTeXState
    forall (m :: * -> *) s u. Monad m => ParsecT s u m u
    getState
      Bool -> LP m () -> LP m ()
    forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
    unless (Bool
    expanded Bool -> Bool -> Bool
    || Bool
    verbatimMode) (LP m () -> LP m ()) -> LP m () -> LP m ()
    forall a b. (a -> b) -> a -> b
    $ do
          ParsecT [Tok] LaTeXState m [Tok]
    forall (m :: * -> *) s u. Monad m => ParsecT s u m s
    getInput ParsecT [Tok] LaTeXState m [Tok]
    -> ([Tok] -> ParsecT [Tok] LaTeXState m [Tok])
    -> ParsecT [Tok] LaTeXState m [Tok]
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= Int -> [Tok] -> ParsecT [Tok] LaTeXState m [Tok]
    forall (m :: * -> *). PandocMonad m => Int -> [Tok] -> LP m [Tok]
    doMacros' 1 ParsecT [Tok] LaTeXState m [Tok] -> ([Tok] -> LP m ()) -> LP m ()
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [Tok] -> LP m ()
    forall (m :: * -> *) s u. Monad m => s -> ParsecT s u m ()
    setInput
          (LaTeXState -> LaTeXState) -> LP m ()
    forall (m :: * -> *) u s. Monad m => (u -> u) -> ParsecT s u m ()
    updateState ((LaTeXState -> LaTeXState) -> LP m ())
    -> (LaTeXState -> LaTeXState) -> LP m ()
    forall a b. (a -> b) -> a -> b
    $ \st :: LaTeXState
    st -> LaTeXState
    st{ sExpanded :: Bool
    sExpanded = Bool
    True }

    doMacros' :: PandocMonad m => Int -> [Tok] -> LP m [Tok]
    doMacros' :: Int -> [Tok] -> LP m [Tok]
    doMacros' n :: Int
    n inp :: [Tok]
    inp =
      case [Tok]
    inp of
         Tok spos :: SourcePos
    spos (CtrlSeq "begin") _ : Tok _ Symbol "{" :
          Tok _ Word name :: Text
    name : Tok _ Symbol "}" : ts :: [Tok]
    ts
            -> Int -> SourcePos -> Text -> [Tok] -> LP m [Tok]
    forall (m :: * -> *).
    PandocMonad m =>
    Int
    -> SourcePos -> Text -> [Tok] -> ParsecT [Tok] LaTeXState m [Tok]
    handleMacros Int
    n SourcePos
    spos Text
    name [Tok]
    ts
         Tok spos :: SourcePos
    spos (CtrlSeq "end") _ : Tok _ Symbol "{" :
          Tok _ Word name :: Text
    name : Tok _ Symbol "}" : ts :: [Tok]
    ts
            -> Int -> SourcePos -> Text -> [Tok] -> LP m [Tok]
    forall (m :: * -> *).
    PandocMonad m =>
    Int
    -> SourcePos -> Text -> [Tok] -> ParsecT [Tok] LaTeXState m [Tok]
    handleMacros Int
    n SourcePos
    spos ("end" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    name) [Tok]
    ts
         Tok _ (CtrlSeq "expandafter") _ : t :: Tok
    t : ts :: [Tok]
    ts
            -> Tok -> [Tok] -> [Tok]
    combineTok Tok
    t ([Tok] -> [Tok]) -> LP m [Tok] -> LP m [Tok]
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Int -> [Tok] -> LP m [Tok]
    forall (m :: * -> *). PandocMonad m => Int -> [Tok] -> LP m [Tok]
    doMacros' Int
    n [Tok]
    ts
         Tok spos :: SourcePos
    spos (CtrlSeq name :: Text
    name) _ : ts :: [Tok]
    ts
            -> Int -> SourcePos -> Text -> [Tok] -> LP m [Tok]
    forall (m :: * -> *).
    PandocMonad m =>
    Int
    -> SourcePos -> Text -> [Tok] -> ParsecT [Tok] LaTeXState m [Tok]
    handleMacros Int
    n SourcePos
    spos Text
    name [Tok]
    ts
         _ -> [Tok] -> LP m [Tok]
    forall (m :: * -> *) a. Monad m => a -> m a
    return [Tok]
    inp
       LP m [Tok] -> LP m [Tok] -> LP m [Tok]
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    <|> [Tok] -> LP m [Tok]
    forall (m :: * -> *) a. Monad m => a -> m a
    return [Tok]
    inp

      where
        combineTok :: Tok -> [Tok] -> [Tok]
    combineTok (Tok spos :: SourcePos
    spos (CtrlSeq name :: Text
    name) x :: Text
    x) (Tok _ Word w :: Text
    w : ts :: [Tok]
    ts)
          | (Char -> Bool) -> Text -> Bool
    T.all Char -> Bool
    isLetterOrAt Text
    w =
            SourcePos -> TokType -> Text -> Tok
    Tok SourcePos
    spos (Text -> TokType
    CtrlSeq (Text
    name Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    w)) (Text
    x1 Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    w Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    x2) Tok -> [Tok] -> [Tok]
    forall a. a -> [a] -> [a]
    : [Tok]
    ts
              where (x1 :: Text
    x1, x2 :: Text
    x2) = (Char -> Bool) -> Text -> (Text, Text)
    T.break Char -> Bool
    isSpaceOrTab Text
    x
        combineTok t :: Tok
    t ts :: [Tok]
    ts = Tok
    tTok -> [Tok] -> [Tok]
    forall a. a -> [a] -> [a]
    :[Tok]
    ts

        matchTok :: Tok -> LP m Tok
    matchTok (Tok _ toktype :: TokType
    toktype txt :: Text
    txt) =
          (Tok -> Bool) -> LP m Tok
    forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
    satisfyTok (\(Tok _ toktype' :: TokType
    toktype' txt' :: Text
    txt') ->
                        TokType
    toktype TokType -> TokType -> Bool
    forall a. Eq a => a -> a -> Bool
    == TokType
    toktype' Bool -> Bool -> Bool
    &&
                        Text
    txt Text -> Text -> Bool
    forall a. Eq a => a -> a -> Bool
    == Text
    txt')

        matchPattern :: t Tok -> ParsecT [Tok] LaTeXState m ()
    matchPattern toks :: t Tok
    toks = ParsecT [Tok] LaTeXState m () -> ParsecT [Tok] LaTeXState m ()
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
    try (ParsecT [Tok] LaTeXState m () -> ParsecT [Tok] LaTeXState m ())
    -> ParsecT [Tok] LaTeXState m () -> ParsecT [Tok] LaTeXState m ()
    forall a b. (a -> b) -> a -> b
    $ (Tok -> ParsecT [Tok] LaTeXState m Tok)
    -> t Tok -> ParsecT [Tok] LaTeXState m ()
    forall (t :: * -> *) (m :: * -> *) a b.
    (Foldable t, Monad m) =>
    (a -> m b) -> t a -> m ()
    mapM_ Tok -> ParsecT [Tok] LaTeXState m Tok
    forall (m :: * -> *). PandocMonad m => Tok -> LP m Tok
    matchTok t Tok
    toks

        getargs :: Map Int [Tok]
    -> [ArgSpec] -> ParsecT [Tok] LaTeXState m (Map Int [Tok])
    getargs argmap :: Map Int [Tok]
    argmap [] = Map Int [Tok] -> ParsecT [Tok] LaTeXState m (Map Int [Tok])
    forall (m :: * -> *) a. Monad m => a -> m a
    return Map Int [Tok]
    argmap
        getargs argmap :: Map Int [Tok]
    argmap (Pattern toks :: [Tok]
    toks : rest :: [ArgSpec]
    rest) = ParsecT [Tok] LaTeXState m (Map Int [Tok])
    -> ParsecT [Tok] LaTeXState m (Map Int [Tok])
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
    try (ParsecT [Tok] LaTeXState m (Map Int [Tok])
     -> ParsecT [Tok] LaTeXState m (Map Int [Tok]))
    -> ParsecT [Tok] LaTeXState m (Map Int [Tok])
    -> ParsecT [Tok] LaTeXState m (Map Int [Tok])
    forall a b. (a -> b) -> a -> b
    $ do
           [Tok] -> ParsecT [Tok] LaTeXState m ()
    forall (t :: * -> *) (m :: * -> *).
    (Foldable t, PandocMonad m) =>
    t Tok -> ParsecT [Tok] LaTeXState m ()
    matchPattern [Tok]
    toks
           Map Int [Tok]
    -> [ArgSpec] -> ParsecT [Tok] LaTeXState m (Map Int [Tok])
    getargs Map Int [Tok]
    argmap [ArgSpec]
    rest
        getargs argmap :: Map Int [Tok]
    argmap (ArgNum i :: Int
    i : Pattern toks :: [Tok]
    toks : rest :: [ArgSpec]
    rest) =
          ParsecT [Tok] LaTeXState m (Map Int [Tok])
    -> ParsecT [Tok] LaTeXState m (Map Int [Tok])
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
    try (ParsecT [Tok] LaTeXState m (Map Int [Tok])
     -> ParsecT [Tok] LaTeXState m (Map Int [Tok]))
    -> ParsecT [Tok] LaTeXState m (Map Int [Tok])
    -> ParsecT [Tok] LaTeXState m (Map Int [Tok])
    forall a b. (a -> b) -> a -> b
    $ do
            [Tok]
    x <- [[Tok]] -> [Tok]
    forall a. Monoid a => [a] -> a
    mconcat ([[Tok]] -> [Tok])
    -> ParsecT [Tok] LaTeXState m [[Tok]]
    -> ParsecT [Tok] LaTeXState m [Tok]
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> ParsecT [Tok] LaTeXState m [Tok]
    -> ParsecT [Tok] LaTeXState m ()
    -> ParsecT [Tok] LaTeXState m [[Tok]]
    forall s (m :: * -> *) t u a end.
    Stream s m t =>
    ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m [a]
    manyTill (ParsecT [Tok] LaTeXState m [Tok]
    forall (m :: * -> *). PandocMonad m => LP m [Tok]
    braced ParsecT [Tok] LaTeXState m [Tok]
    -> ParsecT [Tok] LaTeXState m [Tok]
    -> ParsecT [Tok] LaTeXState m [Tok]
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    <|> ((Tok -> [Tok] -> [Tok]
    forall a. a -> [a] -> [a]
    :[]) (Tok -> [Tok])
    -> ParsecT [Tok] LaTeXState m Tok
    -> ParsecT [Tok] LaTeXState m [Tok]
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> ParsecT [Tok] LaTeXState m Tok
    forall (m :: * -> *). PandocMonad m => LP m Tok
    anyTok))
                      ([Tok] -> ParsecT [Tok] LaTeXState m ()
    forall (t :: * -> *) (m :: * -> *).
    (Foldable t, PandocMonad m) =>
    t Tok -> ParsecT [Tok] LaTeXState m ()
    matchPattern [Tok]
    toks)
            Map Int [Tok]
    -> [ArgSpec] -> ParsecT [Tok] LaTeXState m (Map Int [Tok])
    getargs (Int -> [Tok] -> Map Int [Tok] -> Map Int [Tok]
    forall k a. Ord k => k -> a -> Map k a -> Map k a
    M.insert Int
    i [Tok]
    x Map Int [Tok]
    argmap) [ArgSpec]
    rest
        getargs argmap :: Map Int [Tok]
    argmap (ArgNum i :: Int
    i : rest :: [ArgSpec]
    rest) = do
          [Tok]
    x <- ParsecT [Tok] LaTeXState m [Tok]
    -> ParsecT [Tok] LaTeXState m [Tok]
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
    try (ParsecT [Tok] LaTeXState m [Tok]
     -> ParsecT [Tok] LaTeXState m [Tok])
    -> ParsecT [Tok] LaTeXState m [Tok]
    -> ParsecT [Tok] LaTeXState m [Tok]
    forall a b. (a -> b) -> a -> b
    $ ParsecT [Tok] LaTeXState m ()
    forall (m :: * -> *). PandocMonad m => LP m ()
    spaces ParsecT [Tok] LaTeXState m ()
    -> ParsecT [Tok] LaTeXState m [Tok]
    -> ParsecT [Tok] LaTeXState m [Tok]
    forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
    >> ParsecT [Tok] LaTeXState m [Tok]
    forall (m :: * -> *). PandocMonad m => LP m [Tok]
    bracedOrToken
          Map Int [Tok]
    -> [ArgSpec] -> ParsecT [Tok] LaTeXState m (Map Int [Tok])
    getargs (Int -> [Tok] -> Map Int [Tok] -> Map Int [Tok]
    forall k a. Ord k => k -> a -> Map k a -> Map k a
    M.insert Int
    i [Tok]
    x Map Int [Tok]
    argmap) [ArgSpec]
    rest

        addTok :: Bool -> Map Int (t Tok) -> SourcePos -> Tok -> [Tok] -> [Tok]
    addTok False args :: Map Int (t Tok)
    args spos :: SourcePos
    spos (Tok _ (Arg i :: Int
    i) _) acc :: [Tok]
    acc =
           case Int -> Map Int (t Tok) -> Maybe (t Tok)
    forall k a. Ord k => k -> Map k a -> Maybe a
    M.lookup Int
    i Map Int (t Tok)
    args of
                Nothing -> [Tok]
    forall (m :: * -> *) a. MonadPlus m => m a
    mzero
                Just xs :: t Tok
    xs -> (Tok -> [Tok] -> [Tok]) -> [Tok] -> t Tok -> [Tok]
    forall (t :: * -> *) a b.
    Foldable t =>
    (a -> b -> b) -> b -> t a -> b
    foldr (Bool -> Map Int (t Tok) -> SourcePos -> Tok -> [Tok] -> [Tok]
    addTok Bool
    True Map Int (t Tok)
    args SourcePos
    spos) [Tok]
    acc t Tok
    xs
        -- see #4007
        addTok _ _ spos :: SourcePos
    spos (Tok _ (CtrlSeq x :: Text
    x) txt :: Text
    txt)
               acc :: [Tok]
    acc@(Tok _ Word _ : _)
          | Bool -> Bool
    not (Text -> Bool
    T.null Text
    txt)
          , Char -> Bool
    isLetter (Text -> Char
    T.last Text
    txt) =
            SourcePos -> TokType -> Text -> Tok
    Tok SourcePos
    spos (Text -> TokType
    CtrlSeq Text
    x) (Text
    txt Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> " ") Tok -> [Tok] -> [Tok]
    forall a. a -> [a] -> [a]
    : [Tok]
    acc
        addTok _ _ spos :: SourcePos
    spos t :: Tok
    t acc :: [Tok]
    acc = SourcePos -> Tok -> Tok
    setpos SourcePos
    spos Tok
    t Tok -> [Tok] -> [Tok]
    forall a. a -> [a] -> [a]
    : [Tok]
    acc

        handleMacros :: Int
    -> SourcePos -> Text -> [Tok] -> ParsecT [Tok] LaTeXState m [Tok]
    handleMacros n' :: Int
    n' spos :: SourcePos
    spos name :: Text
    name ts :: [Tok]
    ts = do
          Bool
    -> ParsecT [Tok] LaTeXState m () -> ParsecT [Tok] LaTeXState m ()
    forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
    when (Int
    n' Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    > 20)  -- detect macro expansion loops
            (ParsecT [Tok] LaTeXState m () -> ParsecT [Tok] LaTeXState m ())
    -> ParsecT [Tok] LaTeXState m () -> ParsecT [Tok] LaTeXState m ()
    forall a b. (a -> b) -> a -> b
    $ PandocError -> ParsecT [Tok] LaTeXState m ()
    forall e (m :: * -> *) a. MonadError e m => e -> m a
    throwError (PandocError -> ParsecT [Tok] LaTeXState m ())
    -> PandocError -> ParsecT [Tok] LaTeXState m ()
    forall a b. (a -> b) -> a -> b
    $ Text -> PandocError
    PandocMacroLoop Text
    name
          Map Text Macro
    macros <- LaTeXState -> Map Text Macro
    sMacros (LaTeXState -> Map Text Macro)
    -> ParsecT [Tok] LaTeXState m LaTeXState
    -> ParsecT [Tok] LaTeXState m (Map Text Macro)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> ParsecT [Tok] LaTeXState m LaTeXState
    forall (m :: * -> *) s u. Monad m => ParsecT s u m u
    getState
          case Text -> Map Text Macro -> Maybe Macro
    forall k a. Ord k => k -> Map k a -> Maybe a
    M.lookup Text
    name Map Text Macro
    macros of
               Nothing -> ParsecT [Tok] LaTeXState m [Tok]
    forall (m :: * -> *) a. MonadPlus m => m a
    mzero
               Just (Macro expansionPoint :: ExpansionPoint
    expansionPoint argspecs :: [ArgSpec]
    argspecs optarg :: Maybe [Tok]
    optarg newtoks :: [Tok]
    newtoks) -> do
                 let getargs' :: ParsecT [Tok] LaTeXState m (Map Int [Tok], [Tok])
    getargs' = do
                       Map Int [Tok]
    args <- case Maybe [Tok]
    optarg of
                                 Nothing -> Map Int [Tok]
    -> [ArgSpec] -> ParsecT [Tok] LaTeXState m (Map Int [Tok])
    forall (m :: * -> *).
    PandocMonad m =>
    Map Int [Tok]
    -> [ArgSpec] -> ParsecT [Tok] LaTeXState m (Map Int [Tok])
    getargs Map Int [Tok]
    forall k a. Map k a
    M.empty [ArgSpec]
    argspecs
                                 Just o :: [Tok]
    o  -> do
                                    [Tok]
    x <- [Tok]
    -> ParsecT [Tok] LaTeXState m [Tok]
    -> ParsecT [Tok] LaTeXState m [Tok]
    forall s (m :: * -> *) t a u.
    Stream s m t =>
    a -> ParsecT s u m a -> ParsecT s u m a
    option [Tok]
    o ParsecT [Tok] LaTeXState m [Tok]
    forall (m :: * -> *). PandocMonad m => LP m [Tok]
    bracketedToks
                                    Map Int [Tok]
    -> [ArgSpec] -> ParsecT [Tok] LaTeXState m (Map Int [Tok])
    forall (m :: * -> *).
    PandocMonad m =>
    Map Int [Tok]
    -> [ArgSpec] -> ParsecT [Tok] LaTeXState m (Map Int [Tok])
    getargs (Int -> [Tok] -> Map Int [Tok]
    forall k a. k -> a -> Map k a
    M.singleton 1 [Tok]
    x) ([ArgSpec] -> ParsecT [Tok] LaTeXState m (Map Int [Tok]))
    -> [ArgSpec] -> ParsecT [Tok] LaTeXState m (Map Int [Tok])
    forall a b. (a -> b) -> a -> b
    $ Int -> [ArgSpec] -> [ArgSpec]
    forall a. Int -> [a] -> [a]
    drop 1 [ArgSpec]
    argspecs
                       [Tok]
    rest <- ParsecT [Tok] LaTeXState m [Tok]
    forall (m :: * -> *) s u. Monad m => ParsecT s u m s
    getInput
                       (Map Int [Tok], [Tok])
    -> ParsecT [Tok] LaTeXState m (Map Int [Tok], [Tok])
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Map Int [Tok]
    args, [Tok]
    rest)
                 LaTeXState
    lstate <- ParsecT [Tok] LaTeXState m LaTeXState
    forall (m :: * -> *) s u. Monad m => ParsecT s u m u
    getState
                 Either ParseError (Map Int [Tok], [Tok])
    res <- m (Either ParseError (Map Int [Tok], [Tok]))
    -> ParsecT
         [Tok] LaTeXState m (Either ParseError (Map Int [Tok], [Tok]))
    forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
    (MonadTrans t, Monad m) =>
    m a -> t m a
    lift (m (Either ParseError (Map Int [Tok], [Tok]))
     -> ParsecT
          [Tok] LaTeXState m (Either ParseError (Map Int [Tok], [Tok])))
    -> m (Either ParseError (Map Int [Tok], [Tok]))
    -> ParsecT
         [Tok] LaTeXState m (Either ParseError (Map Int [Tok], [Tok]))
    forall a b. (a -> b) -> a -> b
    $ ParsecT [Tok] LaTeXState m (Map Int [Tok], [Tok])
    -> LaTeXState
    -> String
    -> [Tok]
    -> m (Either ParseError (Map Int [Tok], [Tok]))
    forall s (m :: * -> *) t u a.
    Stream s m t =>
    ParsecT s u m a -> u -> String -> s -> m (Either ParseError a)
    runParserT ParsecT [Tok] LaTeXState m (Map Int [Tok], [Tok])
    getargs' LaTeXState
    lstate "args" [Tok]
    ts
                 case Either ParseError (Map Int [Tok], [Tok])
    res of
                   Left _ -> String -> ParsecT [Tok] LaTeXState m [Tok]
    forall (m :: * -> *) a. MonadFail m => String -> m a
    Prelude.fail (String -> ParsecT [Tok] LaTeXState m [Tok])
    -> String -> ParsecT [Tok] LaTeXState m [Tok]
    forall a b. (a -> b) -> a -> b
    $ "Could not parse arguments for " String -> ShowS
    forall a. [a] -> [a] -> [a]
    ++
                                    Text -> String
    T.unpack Text
    name
                   Right (args :: Map Int [Tok]
    args, rest :: [Tok]
    rest) -> do
                     -- first boolean param is true if we're tokenizing
                     -- an argument (in which case we don't want to
                     -- expand #1 etc.)
                     let result :: [Tok]
    result = (Tok -> [Tok] -> [Tok]) -> [Tok] -> [Tok] -> [Tok]
    forall (t :: * -> *) a b.
    Foldable t =>
    (a -> b -> b) -> b -> t a -> b
    foldr (Bool -> Map Int [Tok] -> SourcePos -> Tok -> [Tok] -> [Tok]
    forall (t :: * -> *).
    Foldable t =>
    Bool -> Map Int (t Tok) -> SourcePos -> Tok -> [Tok] -> [Tok]
    addTok Bool
    False Map Int [Tok]
    args SourcePos
    spos) [Tok]
    rest [Tok]
    newtoks
                     case ExpansionPoint
    expansionPoint of
                       ExpandWhenUsed    -> Int -> [Tok] -> ParsecT [Tok] LaTeXState m [Tok]
    forall (m :: * -> *). PandocMonad m => Int -> [Tok] -> LP m [Tok]
    doMacros' (Int
    n' Int -> Int -> Int
    forall a. Num a => a -> a -> a
    + 1) [Tok]
    result
                       ExpandWhenDefined -> [Tok] -> ParsecT [Tok] LaTeXState m [Tok]
    forall (m :: * -> *) a. Monad m => a -> m a
    return [Tok]
    result

    setpos :: SourcePos -> Tok -> Tok
    setpos :: SourcePos -> Tok -> Tok
    setpos spos :: SourcePos
    spos (Tok _ tt :: TokType
    tt txt :: Text
    txt) = SourcePos -> TokType -> Text -> Tok
    Tok SourcePos
    spos TokType
    tt Text
    txt

    anyControlSeq :: PandocMonad m => LP m Tok
    anyControlSeq :: LP m Tok
    anyControlSeq = (Tok -> Bool) -> LP m Tok
    forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
    satisfyTok Tok -> Bool
    isCtrlSeq

    isCtrlSeq :: Tok -> Bool
    isCtrlSeq :: Tok -> Bool
    isCtrlSeq (Tok _ (CtrlSeq _) _) = Bool
    True
    isCtrlSeq _                     = Bool
    False

    anySymbol :: PandocMonad m => LP m Tok
    anySymbol :: LP m Tok
    anySymbol = (Tok -> Bool) -> LP m Tok
    forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
    satisfyTok Tok -> Bool
    isSymbolTok

    isSymbolTok :: Tok -> Bool
    isSymbolTok :: Tok -> Bool
    isSymbolTok (Tok _ Symbol _) = Bool
    True
    isSymbolTok _                = Bool
    False

    isWordTok :: Tok -> Bool
    isWordTok :: Tok -> Bool
    isWordTok (Tok _ Word _) = Bool
    True
    isWordTok _              = Bool
    False

    isArgTok :: Tok -> Bool
    isArgTok :: Tok -> Bool
    isArgTok (Tok _ (Arg _) _) = Bool
    True
    isArgTok _                 = Bool
    False

    spaces :: PandocMonad m => LP m ()
    spaces :: LP m ()
    spaces = ParsecT [Tok] LaTeXState m Tok -> LP m ()
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m ()
    skipMany ((Tok -> Bool) -> ParsecT [Tok] LaTeXState m Tok
    forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
    satisfyTok ([TokType] -> Tok -> Bool
    tokTypeIn [TokType
    Comment, TokType
    Spaces, TokType
    Newline]))

    spaces1 :: PandocMonad m => LP m ()
    spaces1 :: LP m ()
    spaces1 = ParsecT [Tok] LaTeXState m Tok -> LP m ()
    forall s (m :: * -> *) t u a.
    Stream s m t =>
    ParsecT s u m a -> ParsecT s u m ()
    skipMany1 ((Tok -> Bool) -> ParsecT [Tok] LaTeXState m Tok
    forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
    satisfyTok ([TokType] -> Tok -> Bool
    tokTypeIn [TokType
    Comment, TokType
    Spaces, TokType
    Newline]))

    tokTypeIn :: [TokType] -> Tok -> Bool
    tokTypeIn :: [TokType] -> Tok -> Bool
    tokTypeIn toktypes :: [TokType]
    toktypes (Tok _ tt :: TokType
    tt _) = TokType
    tt TokType -> [TokType] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [TokType]
    toktypes

    controlSeq :: PandocMonad m => Text -> LP m Tok
    controlSeq :: Text -> LP m Tok
    controlSeq name :: Text
    name = (Tok -> Bool) -> LP m Tok
    forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
    satisfyTok Tok -> Bool
    isNamed
      where isNamed :: Tok -> Bool
    isNamed (Tok _ (CtrlSeq n :: Text
    n) _) = Text
    n Text -> Text -> Bool
    forall a. Eq a => a -> a -> Bool
    == Text
    name
            isNamed _                     = Bool
    False

    symbol :: PandocMonad m => Char -> LP m Tok
    symbol :: Char -> LP m Tok
    symbol c :: Char
    c = (Tok -> Bool) -> LP m Tok
    forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
    satisfyTok Tok -> Bool
    isc
      where isc :: Tok -> Bool
    isc (Tok _ Symbol d :: Text
    d) = case Text -> Maybe (Char, Text)
    T.uncons Text
    d of
                                        Just (c' :: Char
    c',_) -> Char
    c Char -> Char -> Bool
    forall a. Eq a => a -> a -> Bool
    == Char
    c'
                                        _           -> Bool
    False
            isc _ = Bool
    False

    symbolIn :: PandocMonad m => [Char] -> LP m Tok
    symbolIn :: String -> LP m Tok
    symbolIn cs :: String
    cs = (Tok -> Bool) -> LP m Tok
    forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
    satisfyTok Tok -> Bool
    isInCs
      where isInCs :: Tok -> Bool
    isInCs (Tok _ Symbol d :: Text
    d) = case Text -> Maybe (Char, Text)
    T.uncons Text
    d of
                                           Just (c :: Char
    c,_) -> Char
    c Char -> String -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` String
    cs
                                           _          -> Bool
    False
            isInCs _ = Bool
    False

    sp :: PandocMonad m => LP m ()
    sp :: LP m ()
    sp = do
      LP m () -> LP m ()
    forall s (m :: * -> *) t u a.
    Stream s m t =>
    ParsecT s u m a -> ParsecT s u m ()
    optional (LP m () -> LP m ()) -> LP m () -> LP m ()
    forall a b. (a -> b) -> a -> b
    $ LP m () -> LP m ()
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m ()
    skipMany (LP m ()
    forall (m :: * -> *). PandocMonad m => LP m ()
    whitespace LP m () -> LP m () -> LP m ()
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    <|> LP m ()
    forall (m :: * -> *). PandocMonad m => LP m ()
    comment)
      LP m () -> LP m ()
    forall s (m :: * -> *) t u a.
    Stream s m t =>
    ParsecT s u m a -> ParsecT s u m ()
    optional (LP m () -> LP m ()) -> LP m () -> LP m ()
    forall a b. (a -> b) -> a -> b
    $ LP m ()
    forall (m :: * -> *). PandocMonad m => LP m ()
    endline  LP m () -> LP m () -> LP m ()
    forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
    *> LP m () -> LP m ()
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m ()
    skipMany (LP m ()
    forall (m :: * -> *). PandocMonad m => LP m ()
    whitespace LP m () -> LP m () -> LP m ()
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    <|> LP m ()
    forall (m :: * -> *). PandocMonad m => LP m ()
    comment)

    whitespace :: PandocMonad m => LP m ()
    whitespace :: LP m ()
    whitespace = () () -> ParsecT [Tok] LaTeXState m Tok -> LP m ()
    forall (f :: * -> *) a b. Functor f => a -> f b -> f a
    <$ (Tok -> Bool) -> ParsecT [Tok] LaTeXState m Tok
    forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
    satisfyTok Tok -> Bool
    isSpaceTok

    isSpaceTok :: Tok -> Bool
    isSpaceTok :: Tok -> Bool
    isSpaceTok (Tok _ Spaces _) = Bool
    True
    isSpaceTok _                = Bool
    False

    newlineTok :: PandocMonad m => LP m ()
    newlineTok :: LP m ()
    newlineTok = () () -> ParsecT [Tok] LaTeXState m Tok -> LP m ()
    forall (f :: * -> *) a b. Functor f => a -> f b -> f a
    <$ (Tok -> Bool) -> ParsecT [Tok] LaTeXState m Tok
    forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
    satisfyTok Tok -> Bool
    isNewlineTok

    isNewlineTok :: Tok -> Bool
    isNewlineTok :: Tok -> Bool
    isNewlineTok (Tok _ Newline _) = Bool
    True
    isNewlineTok _                 = Bool
    False

    comment :: PandocMonad m => LP m ()
    comment :: LP m ()
    comment = () () -> ParsecT [Tok] LaTeXState m Tok -> LP m ()
    forall (f :: * -> *) a b. Functor f => a -> f b -> f a
    <$ (Tok -> Bool) -> ParsecT [Tok] LaTeXState m Tok
    forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
    satisfyTok Tok -> Bool
    isCommentTok

    isCommentTok :: Tok -> Bool
    isCommentTok :: Tok -> Bool
    isCommentTok (Tok _ Comment _) = Bool
    True
    isCommentTok _                 = Bool
    False

    anyTok :: PandocMonad m => LP m Tok
    anyTok :: LP m Tok
    anyTok = (Tok -> Bool) -> LP m Tok
    forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
    satisfyTok (Bool -> Tok -> Bool
    forall a b. a -> b -> a
    const Bool
    True)

    singleChar :: PandocMonad m => LP m Tok
    singleChar :: LP m Tok
    singleChar = LP m Tok -> LP m Tok
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
    try (LP m Tok -> LP m Tok) -> LP m Tok -> LP m Tok
    forall a b. (a -> b) -> a -> b
    $ do
      Tok pos :: SourcePos
    pos toktype :: TokType
    toktype t :: Text
    t <- (Tok -> Bool) -> LP m Tok
    forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
    satisfyTok ([TokType] -> Tok -> Bool
    tokTypeIn [TokType
    Word, TokType
    Symbol])
      Bool -> ParsecT [Tok] LaTeXState m ()
    forall (f :: * -> *). Alternative f => Bool -> f ()
    guard (Bool -> ParsecT [Tok] LaTeXState m ())
    -> Bool -> ParsecT [Tok] LaTeXState m ()
    forall a b. (a -> b) -> a -> b
    $ Bool -> Bool
    not (Bool -> Bool) -> Bool -> Bool
    forall a b. (a -> b) -> a -> b
    $ TokType
    toktype TokType -> TokType -> Bool
    forall a. Eq a => a -> a -> Bool
    == TokType
    Symbol Bool -> Bool -> Bool
    &&
                    (Char -> Bool) -> Text -> Bool
    T.any (Char -> Set Char -> Bool
    forall a. Ord a => a -> Set a -> Bool
    `Set.member` Set Char
    specialChars) Text
    t
      if Text -> Int
    T.length Text
    t Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    > 1
         then do
           let (t1 :: Text
    t1, t2 :: Text
    t2) = (Int -> Text -> Text
    T.take 1 Text
    t, Int -> Text -> Text
    T.drop 1 Text
    t)
           [Tok]
    inp <- ParsecT [Tok] LaTeXState m [Tok]
    forall (m :: * -> *) s u. Monad m => ParsecT s u m s
    getInput
           [Tok] -> ParsecT [Tok] LaTeXState m ()
    forall (m :: * -> *) s u. Monad m => s -> ParsecT s u m ()
    setInput ([Tok] -> ParsecT [Tok] LaTeXState m ())
    -> [Tok] -> ParsecT [Tok] LaTeXState m ()
    forall a b. (a -> b) -> a -> b
    $ SourcePos -> TokType -> Text -> Tok
    Tok (SourcePos -> Int -> SourcePos
    incSourceColumn SourcePos
    pos 1) TokType
    toktype Text
    t2 Tok -> [Tok] -> [Tok]
    forall a. a -> [a] -> [a]
    : [Tok]
    inp
           Tok -> LP m Tok
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Tok -> LP m Tok) -> Tok -> LP m Tok
    forall a b. (a -> b) -> a -> b
    $ SourcePos -> TokType -> Text -> Tok
    Tok SourcePos
    pos TokType
    toktype Text
    t1
         else Tok -> LP m Tok
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Tok -> LP m Tok) -> Tok -> LP m Tok
    forall a b. (a -> b) -> a -> b
    $ SourcePos -> TokType -> Text -> Tok
    Tok SourcePos
    pos TokType
    toktype Text
    t

    specialChars :: Set.Set Char
    specialChars :: Set Char
    specialChars = String -> Set Char
    forall a. Ord a => [a] -> Set a
    Set.fromList "#$%&~_^\\{}"

    endline :: PandocMonad m => LP m ()
    endline :: LP m ()
    endline = LP m () -> LP m ()
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
    try (LP m () -> LP m ()) -> LP m () -> LP m ()
    forall a b. (a -> b) -> a -> b
    $ do
      LP m ()
    forall (m :: * -> *). PandocMonad m => LP m ()
    newlineTok
      ParsecT [Tok] LaTeXState m Tok -> ParsecT [Tok] LaTeXState m Tok
    forall s (m :: * -> *) t u a.
    Stream s m t =>
    ParsecT s u m a -> ParsecT s u m a
    lookAhead ParsecT [Tok] LaTeXState m Tok
    forall (m :: * -> *). PandocMonad m => LP m Tok
    anyTok
      LP m () -> LP m ()
    forall s (m :: * -> *) t a u.
    (Stream s m t, Show a) =>
    ParsecT s u m a -> ParsecT s u m ()
    notFollowedBy LP m ()
    forall (m :: * -> *). PandocMonad m => LP m ()
    blankline

    blankline :: PandocMonad m => LP m ()
    blankline :: LP m ()
    blankline = LP m () -> LP m ()
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
    try (LP m () -> LP m ()) -> LP m () -> LP m ()
    forall a b. (a -> b) -> a -> b
    $ LP m () -> LP m ()
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m ()
    skipMany LP m ()
    forall (m :: * -> *). PandocMonad m => LP m ()
    whitespace LP m () -> LP m () -> LP m ()
    forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
    *> LP m ()
    forall (m :: * -> *). PandocMonad m => LP m ()
    newlineTok

    primEscape :: PandocMonad m => LP m Char
    primEscape :: LP m Char
    primEscape = do
      Tok _ toktype :: TokType
    toktype t :: Text
    t <- (Tok -> Bool) -> LP m Tok
    forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
    satisfyTok ([TokType] -> Tok -> Bool
    tokTypeIn [TokType
    Esc1, TokType
    Esc2])
      case TokType
    toktype of
           Esc1 -> case Text -> Maybe (Char, Text)
    T.uncons (Int -> Text -> Text
    T.drop 2 Text
    t) of
                        Just (c :: Char
    c, _)
                          | Char
    c Char -> Char -> Bool
    forall a. Ord a => a -> a -> Bool
    >= '\64' Bool -> Bool -> Bool
    && Char
    c Char -> Char -> Bool
    forall a. Ord a => a -> a -> Bool
    <= '\127' -> Char -> LP m Char
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Int -> Char
    chr (Char -> Int
    ord Char
    c Int -> Int -> Int
    forall a. Num a => a -> a -> a
    - 64))
                          | Bool
    otherwise                 -> Char -> LP m Char
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Int -> Char
    chr (Char -> Int
    ord Char
    c Int -> Int -> Int
    forall a. Num a => a -> a -> a
    + 64))
                        Nothing -> String -> LP m Char
    forall (m :: * -> *) a. MonadFail m => String -> m a
    Prelude.fail "Empty content of Esc1"
           Esc2 -> case Text -> Maybe Int
    forall (m :: * -> *) a. (MonadPlus m, Read a) => Text -> m a
    safeRead ("0x" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Int -> Text -> Text
    T.drop 2 Text
    t) of
                        Just x :: Int
    x  -> Char -> LP m Char
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Int -> Char
    chr Int
    x)
                        Nothing -> String -> LP m Char
    forall (m :: * -> *) a. MonadFail m => String -> m a
    Prelude.fail (String -> LP m Char) -> String -> LP m Char
    forall a b. (a -> b) -> a -> b
    $ "Could not read: " String -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ Text -> String
    T.unpack Text
    t
           _    -> String -> LP m Char
    forall (m :: * -> *) a. MonadFail m => String -> m a
    Prelude.fail "Expected an Esc1 or Esc2 token" -- should not happen

    bgroup :: PandocMonad m => LP m Tok
    bgroup :: LP m Tok
    bgroup = LP m Tok -> LP m Tok
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
    try (LP m Tok -> LP m Tok) -> LP m Tok -> LP m Tok
    forall a b. (a -> b) -> a -> b
    $ do
      ParsecT [Tok] LaTeXState m () -> ParsecT [Tok] LaTeXState m ()
    forall s (m :: * -> *) t u a.
    Stream s m t =>
    ParsecT s u m a -> ParsecT s u m ()
    optional ParsecT [Tok] LaTeXState m ()
    forall (m :: * -> *). PandocMonad m => LP m ()
    sp
      Char -> LP m Tok
    forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
    symbol '{' LP m Tok -> LP m Tok -> LP m Tok
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    <|> Text -> LP m Tok
    forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
    controlSeq "bgroup" LP m Tok -> LP m Tok -> LP m Tok
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    <|> Text -> LP m Tok
    forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
    controlSeq "begingroup"

    egroup :: PandocMonad m => LP m Tok
    egroup :: LP m Tok
    egroup = Char -> LP m Tok
    forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
    symbol '}' LP m Tok -> LP m Tok -> LP m Tok
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    <|> Text -> LP m Tok
    forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
    controlSeq "egroup" LP m Tok -> LP m Tok -> LP m Tok
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    <|> Text -> LP m Tok
    forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
    controlSeq "endgroup"

    grouped :: (PandocMonad m,  Monoid a) => LP m a -> LP m a
    grouped :: LP m a -> LP m a
    grouped parser :: LP m a
    parser = LP m a -> LP m a
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
    try (LP m a -> LP m a) -> LP m a -> LP m a
    forall a b. (a -> b) -> a -> b
    $ do
      LP m Tok
    forall (m :: * -> *). PandocMonad m => LP m Tok
    bgroup
      -- first we check for an inner 'grouped', because
      -- {{a,b}} should be parsed the same as {a,b}
      LP m a -> LP m a
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
    try (LP m a -> LP m a
    forall (m :: * -> *) a.
    (PandocMonad m, Monoid a) =>
    LP m a -> LP m a
    grouped LP m a
    parser LP m a -> LP m Tok -> LP m a
    forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
    <* LP m Tok
    forall (m :: * -> *). PandocMonad m => LP m Tok
    egroup) LP m a -> LP m a -> LP m a
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    <|> ([a] -> a
    forall a. Monoid a => [a] -> a
    mconcat ([a] -> a) -> ParsecT [Tok] LaTeXState m [a] -> LP m a
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> LP m a -> LP m Tok -> ParsecT [Tok] LaTeXState m [a]
    forall s (m :: * -> *) t u a end.
    Stream s m t =>
    ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m [a]
    manyTill LP m a
    parser LP m Tok
    forall (m :: * -> *). PandocMonad m => LP m Tok
    egroup)

    braced' :: PandocMonad m => LP m Tok -> Int -> LP m [Tok]
    braced' :: LP m Tok -> Int -> LP m [Tok]
    braced' getTok :: LP m Tok
    getTok n :: Int
    n =
      LP m [Tok]
    handleEgroup LP m [Tok] -> LP m [Tok] -> LP m [Tok]
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    <|> LP m [Tok]
    handleBgroup LP m [Tok] -> LP m [Tok] -> LP m [Tok]
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    <|> LP m [Tok]
    handleOther
      where handleEgroup :: LP m [Tok]
    handleEgroup = do
              Tok
    t <- Char -> LP m Tok
    forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
    symbol '}'
              if Int
    n Int -> Int -> Bool
    forall a. Eq a => a -> a -> Bool
    == 1
                 then [Tok] -> LP m [Tok]
    forall (m :: * -> *) a. Monad m => a -> m a
    return []
                 else (Tok
    tTok -> [Tok] -> [Tok]
    forall a. a -> [a] -> [a]
    :) ([Tok] -> [Tok]) -> LP m [Tok] -> LP m [Tok]
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> LP m Tok -> Int -> LP m [Tok]
    forall (m :: * -> *).
    PandocMonad m =>
    LP m Tok -> Int -> LP m [Tok]
    braced' LP m Tok
    getTok (Int
    n Int -> Int -> Int
    forall a. Num a => a -> a -> a
    - 1)
            handleBgroup :: LP m [Tok]
    handleBgroup = do
              Tok
    t <- Char -> LP m Tok
    forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
    symbol '{'
              (Tok
    tTok -> [Tok] -> [Tok]
    forall a. a -> [a] -> [a]
    :) ([Tok] -> [Tok]) -> LP m [Tok] -> LP m [Tok]
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> LP m Tok -> Int -> LP m [Tok]
    forall (m :: * -> *).
    PandocMonad m =>
    LP m Tok -> Int -> LP m [Tok]
    braced' LP m Tok
    getTok (Int
    n Int -> Int -> Int
    forall a. Num a => a -> a -> a
    + 1)
            handleOther :: LP m [Tok]
    handleOther = do
              Tok
    t <- LP m Tok
    getTok
              (Tok
    tTok -> [Tok] -> [Tok]
    forall a. a -> [a] -> [a]
    :) ([Tok] -> [Tok]) -> LP m [Tok] -> LP m [Tok]
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> LP m Tok -> Int -> LP m [Tok]
    forall (m :: * -> *).
    PandocMonad m =>
    LP m Tok -> Int -> LP m [Tok]
    braced' LP m Tok
    getTok Int
    n

    braced :: PandocMonad m => LP m [Tok]
    braced :: LP m [Tok]
    braced = Char -> LP m Tok
    forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
    symbol '{' LP m Tok -> LP m [Tok] -> LP m [Tok]
    forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
    *> LP m Tok -> Int -> LP m [Tok]
    forall (m :: * -> *).
    PandocMonad m =>
    LP m Tok -> Int -> LP m [Tok]
    braced' LP m Tok
    forall (m :: * -> *). PandocMonad m => LP m Tok
    anyTok 1

    -- URLs require special handling, because they can contain %
    -- characters.  So we retonenize comments as we go...
    bracedUrl :: PandocMonad m => LP m [Tok]
    bracedUrl :: LP m [Tok]
    bracedUrl = LP m Tok
    forall (m :: * -> *). PandocMonad m => LP m Tok
    bgroup LP m Tok -> LP m [Tok] -> LP m [Tok]
    forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
    *> LP m Tok -> Int -> LP m [Tok]
    forall (m :: * -> *).
    PandocMonad m =>
    LP m Tok -> Int -> LP m [Tok]
    braced' (LP m ()
    forall (m :: * -> *). PandocMonad m => LP m ()
    retokenizeComment LP m () -> LP m Tok -> LP m Tok
    forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
    >> LP m Tok
    forall (m :: * -> *). PandocMonad m => LP m Tok
    anyTok) 1

    -- For handling URLs, which allow literal % characters...
    retokenizeComment :: PandocMonad m => LP m ()
    retokenizeComment :: LP m ()
    retokenizeComment = (do
      Tok pos :: SourcePos
    pos Comment txt :: Text
    txt <- (Tok -> Bool) -> LP m Tok
    forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
    satisfyTok Tok -> Bool
    isCommentTok
      let updPos :: Tok -> Tok
    updPos (Tok pos' :: SourcePos
    pos' toktype' :: TokType
    toktype' txt' :: Text
    txt') =
            SourcePos -> TokType -> Text -> Tok
    Tok (SourcePos -> Int -> SourcePos
    incSourceColumn (SourcePos -> Int -> SourcePos
    incSourceLine SourcePos
    pos' (SourcePos -> Int
    sourceLine SourcePos
    pos Int -> Int -> Int
    forall a. Num a => a -> a -> a
    - 1))
                 (SourcePos -> Int
    sourceColumn SourcePos
    pos)) TokType
    toktype' Text
    txt'
      let newtoks :: [Tok]
    newtoks = (Tok -> Tok) -> [Tok] -> [Tok]
    forall a b. (a -> b) -> [a] -> [b]
    map Tok -> Tok
    updPos ([Tok] -> [Tok]) -> [Tok] -> [Tok]
    forall a b. (a -> b) -> a -> b
    $ String -> Text -> [Tok]
    tokenize (SourcePos -> String
    sourceName SourcePos
    pos) (Text -> [Tok]) -> Text -> [Tok]
    forall a b. (a -> b) -> a -> b
    $ Text -> Text
    T.tail Text
    txt
      ParsecT [Tok] LaTeXState m [Tok]
    forall (m :: * -> *) s u. Monad m => ParsecT s u m s
    getInput ParsecT [Tok] LaTeXState m [Tok] -> ([Tok] -> LP m ()) -> LP m ()
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [Tok] -> LP m ()
    forall (m :: * -> *) s u. Monad m => s -> ParsecT s u m ()
    setInput ([Tok] -> LP m ()) -> ([Tok] -> [Tok]) -> [Tok] -> LP m ()
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . ((SourcePos -> TokType -> Text -> Tok
    Tok SourcePos
    pos TokType
    Symbol "%" Tok -> [Tok] -> [Tok]
    forall a. a -> [a] -> [a]
    : [Tok]
    newtoks) [Tok] -> [Tok] -> [Tok]
    forall a. [a] -> [a] -> [a]
    ++))
        LP m () -> LP m () -> LP m ()
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    <|> () -> LP m ()
    forall (m :: * -> *) a. Monad m => a -> m a
    return ()

    bracedOrToken :: PandocMonad m => LP m [Tok]
    bracedOrToken :: LP m [Tok]
    bracedOrToken = LP m [Tok]
    forall (m :: * -> *). PandocMonad m => LP m [Tok]
    braced LP m [Tok] -> LP m [Tok] -> LP m [Tok]
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    <|> ((Tok -> [Tok] -> [Tok]
    forall a. a -> [a] -> [a]
    :[]) (Tok -> [Tok]) -> ParsecT [Tok] LaTeXState m Tok -> LP m [Tok]
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> (ParsecT [Tok] LaTeXState m Tok
    forall (m :: * -> *). PandocMonad m => LP m Tok
    anyControlSeq ParsecT [Tok] LaTeXState m Tok
    -> ParsecT [Tok] LaTeXState m Tok -> ParsecT [Tok] LaTeXState m Tok
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    <|> ParsecT [Tok] LaTeXState m Tok
    forall (m :: * -> *). PandocMonad m => LP m Tok
    singleChar))

    bracketed :: PandocMonad m => Monoid a => LP m a -> LP m a
    bracketed :: LP m a -> LP m a
    bracketed parser :: LP m a
    parser = LP m a -> LP m a
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
    try (LP m a -> LP m a) -> LP m a -> LP m a
    forall a b. (a -> b) -> a -> b
    $ do
      Char -> LP m Tok
    forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
    symbol '['
      [a] -> a
    forall a. Monoid a => [a] -> a
    mconcat ([a] -> a) -> ParsecT [Tok] LaTeXState m [a] -> LP m a
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> LP m a -> LP m Tok -> ParsecT [Tok] LaTeXState m [a]
    forall s (m :: * -> *) t u a end.
    Stream s m t =>
    ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m [a]
    manyTill LP m a
    parser (Char -> LP m Tok
    forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
    symbol ']')

    bracketedToks :: PandocMonad m => LP m [Tok]
    bracketedToks :: LP m [Tok]
    bracketedToks = do
      Char -> LP m Tok
    forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
    symbol '['
      [[Tok]] -> [Tok]
    forall (t :: * -> *) a. Foldable t => t [a] -> [a]
    concat ([[Tok]] -> [Tok])
    -> ParsecT [Tok] LaTeXState m [[Tok]] -> LP m [Tok]
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> LP m [Tok] -> LP m Tok -> ParsecT [Tok] LaTeXState m [[Tok]]
    forall s (m :: * -> *) t u a end.
    Stream s m t =>
    ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m [a]
    manyTill ((([Tok], [Tok]) -> [Tok]
    forall a b. (a, b) -> b
    snd (([Tok], [Tok]) -> [Tok])
    -> ParsecT [Tok] LaTeXState m ([Tok], [Tok]) -> LP m [Tok]
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> LP m [Tok] -> ParsecT [Tok] LaTeXState m ([Tok], [Tok])
    forall (m :: * -> *) a. PandocMonad m => LP m a -> LP m (a, [Tok])
    withRaw (LP m [Tok] -> LP m [Tok]
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
    try LP m [Tok]
    forall (m :: * -> *). PandocMonad m => LP m [Tok]
    braced)) LP m [Tok] -> LP m [Tok] -> LP m [Tok]
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    <|> Int -> LP m Tok -> LP m [Tok]
    forall s (m :: * -> *) t u a.
    Stream s m t =>
    Int -> ParsecT s u m a -> ParsecT s u m [a]
    count 1 LP m Tok
    forall (m :: * -> *). PandocMonad m => LP m Tok
    anyTok)
                          (Char -> LP m Tok
    forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
    symbol ']')

    parenWrapped :: PandocMonad m => Monoid a => LP m a -> LP m a
    parenWrapped :: LP m a -> LP m a
    parenWrapped parser :: LP m a
    parser = LP m a -> LP m a
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
    try (LP m a -> LP m a) -> LP m a -> LP m a
    forall a b. (a -> b) -> a -> b
    $ do
      Char -> LP m Tok
    forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
    symbol '('
      [a] -> a
    forall a. Monoid a => [a] -> a
    mconcat ([a] -> a) -> ParsecT [Tok] LaTeXState m [a] -> LP m a
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> LP m a -> LP m Tok -> ParsecT [Tok] LaTeXState m [a]
    forall s (m :: * -> *) t u a end.
    Stream s m t =>
    ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m [a]
    manyTill LP m a
    parser (Char -> LP m Tok
    forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
    symbol ')')

    dimenarg :: PandocMonad m => LP m Text
    dimenarg :: LP m Text
    dimenarg = LP m Text -> LP m Text
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
    try (LP m Text -> LP m Text) -> LP m Text -> LP m Text
    forall a b. (a -> b) -> a -> b
    $ do
      ParsecT [Tok] LaTeXState m () -> ParsecT [Tok] LaTeXState m ()
    forall s (m :: * -> *) t u a.
    Stream s m t =>
    ParsecT s u m a -> ParsecT s u m ()
    optional ParsecT [Tok] LaTeXState m ()
    forall (m :: * -> *). PandocMonad m => LP m ()
    sp
      Bool
    ch  <- Bool
    -> ParsecT [Tok] LaTeXState m Bool
    -> ParsecT [Tok] LaTeXState m Bool
    forall s (m :: * -> *) t a u.
    Stream s m t =>
    a -> ParsecT s u m a -> ParsecT s u m a
    option Bool
    False (ParsecT [Tok] LaTeXState m Bool
     -> ParsecT [Tok] LaTeXState m Bool)
    -> ParsecT [Tok] LaTeXState m Bool
    -> ParsecT [Tok] LaTeXState m Bool
    forall a b. (a -> b) -> a -> b
    $ Bool
    True Bool
    -> ParsecT [Tok] LaTeXState m Tok
    -> ParsecT [Tok] LaTeXState m Bool
    forall (f :: * -> *) a b. Functor f => a -> f b -> f a
    <$ Char -> ParsecT [Tok] LaTeXState m Tok
    forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
    symbol '='
      Text
    minus <- Text -> LP m Text -> LP m Text
    forall s (m :: * -> *) t a u.
    Stream s m t =>
    a -> ParsecT s u m a -> ParsecT s u m a
    option "" (LP m Text -> LP m Text) -> LP m Text -> LP m Text
    forall a b. (a -> b) -> a -> b
    $ "-" Text -> ParsecT [Tok] LaTeXState m Tok -> LP m Text
    forall (f :: * -> *) a b. Functor f => a -> f b -> f a
    <$ Char -> ParsecT [Tok] LaTeXState m Tok
    forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
    symbol '-'
      Tok _ _ s1 :: Text
    s1 <- (Tok -> Bool) -> ParsecT [Tok] LaTeXState m Tok
    forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
    satisfyTok Tok -> Bool
    isWordTok
      Text
    s2 <- Text -> LP m Text -> LP m Text
    forall s (m :: * -> *) t a u.
    Stream s m t =>
    a -> ParsecT s u m a -> ParsecT s u m a
    option "" (LP m Text -> LP m Text) -> LP m Text -> LP m Text
    forall a b. (a -> b) -> a -> b
    $ LP m Text -> LP m Text
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
    try (LP m Text -> LP m Text) -> LP m Text -> LP m Text
    forall a b. (a -> b) -> a -> b
    $ do
              Char -> ParsecT [Tok] LaTeXState m Tok
    forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
    symbol '.'
              Tok _ _ t :: Text
    t <-  (Tok -> Bool) -> ParsecT [Tok] LaTeXState m Tok
    forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
    satisfyTok Tok -> Bool
    isWordTok
              Text -> LP m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return ("." Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    t)
      let s :: Text
    s = Text
    s1 Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    s2
      let (num :: Text
    num, rest :: Text
    rest) = (Char -> Bool) -> Text -> (Text, Text)
    T.span (\c :: Char
    c -> Char -> Bool
    isDigit Char
    c Bool -> Bool -> Bool
    || Char
    c Char -> Char -> Bool
    forall a. Eq a => a -> a -> Bool
    == '.') Text
    s
      Bool -> ParsecT [Tok] LaTeXState m ()
    forall (f :: * -> *). Alternative f => Bool -> f ()
    guard (Bool -> ParsecT [Tok] LaTeXState m ())
    -> Bool -> ParsecT [Tok] LaTeXState m ()
    forall a b. (a -> b) -> a -> b
    $ Text -> Int
    T.length Text
    num Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    > 0
      Bool -> ParsecT [Tok] LaTeXState m ()
    forall (f :: * -> *). Alternative f => Bool -> f ()
    guard (Bool -> ParsecT [Tok] LaTeXState m ())
    -> Bool -> ParsecT [Tok] LaTeXState m ()
    forall a b. (a -> b) -> a -> b
    $ Text
    rest Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` ["", "pt","pc","in","bp","cm","mm","dd","cc","sp"]
      Text -> LP m Text
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Text -> LP m Text) -> Text -> LP m Text
    forall a b. (a -> b) -> a -> b
    $ String -> Text
    T.pack ['=' | Bool
    ch] Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    minus Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    s

    ignore :: (Monoid a, PandocMonad m) => Text -> ParserT s u m a
    ignore :: Text -> ParserT s u m a
    ignore raw :: Text
    raw = do
      SourcePos
    pos <- ParsecT s u m SourcePos
    forall (m :: * -> *) s u. Monad m => ParsecT s u m SourcePos
    getPosition
      LogMessage -> ParsecT s u m ()
    forall (m :: * -> *). PandocMonad m => LogMessage -> m ()
    report (LogMessage -> ParsecT s u m ()) -> LogMessage -> ParsecT s u m ()
    forall a b. (a -> b) -> a -> b
    $ Text -> SourcePos -> LogMessage
    SkippedContent Text
    raw SourcePos
    pos
      a -> ParserT s u m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return a
    forall a. Monoid a => a
    mempty

    withRaw :: PandocMonad m => LP m a -> LP m (a, [Tok])
    withRaw :: LP m a -> LP m (a, [Tok])
    withRaw parser :: LP m a
    parser = do
      [Tok]
    inp <- ParsecT [Tok] LaTeXState m [Tok]
    forall (m :: * -> *) s u. Monad m => ParsecT s u m s
    getInput
      a
    result <- LP m a
    parser
      Maybe SourcePos
    nxtpos <- Maybe SourcePos
    -> ParsecT [Tok] LaTeXState m (Maybe SourcePos)
    -> ParsecT [Tok] LaTeXState m (Maybe SourcePos)
    forall s (m :: * -> *) t a u.
    Stream s m t =>
    a -> ParsecT s u m a -> ParsecT s u m a
    option Maybe SourcePos
    forall a. Maybe a
    Nothing ((\(Tok pos' :: SourcePos
    pos' _ _) -> SourcePos -> Maybe SourcePos
    forall a. a -> Maybe a
    Just SourcePos
    pos') (Tok -> Maybe SourcePos)
    -> ParsecT [Tok] LaTeXState m Tok
    -> ParsecT [Tok] LaTeXState m (Maybe SourcePos)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> ParsecT [Tok] LaTeXState m Tok -> ParsecT [Tok] LaTeXState m Tok
    forall s (m :: * -> *) t u a.
    Stream s m t =>
    ParsecT s u m a -> ParsecT s u m a
    lookAhead ParsecT [Tok] LaTeXState m Tok
    forall (m :: * -> *). PandocMonad m => LP m Tok
    anyTok)
      let raw :: [Tok]
    raw = (Tok -> Bool) -> [Tok] -> [Tok]
    forall a. (a -> Bool) -> [a] -> [a]
    takeWhile (\(Tok pos :: SourcePos
    pos _ _) -> Bool -> (SourcePos -> Bool) -> Maybe SourcePos -> Bool
    forall b a. b -> (a -> b) -> Maybe a -> b
    maybe Bool
    True
                      (\p :: SourcePos
    p -> SourcePos -> String
    sourceName SourcePos
    p String -> String -> Bool
    forall a. Eq a => a -> a -> Bool
    /= SourcePos -> String
    sourceName SourcePos
    pos Bool -> Bool -> Bool
    || SourcePos
    pos SourcePos -> SourcePos -> Bool
    forall a. Ord a => a -> a -> Bool
    < SourcePos
    p) Maybe SourcePos
    nxtpos) [Tok]
    inp
      (a, [Tok]) -> LP m (a, [Tok])
    forall (m :: * -> *) a. Monad m => a -> m a
    return (a
    result, [Tok]
    raw)