aboutsummaryrefslogtreecommitdiff
path: root/src/Text/Pandoc/Readers/LaTeX/Parsing.hs
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2021-02-21 10:26:48 -0800
committerJohn MacFarlane <jgm@berkeley.edu>2021-02-21 10:58:42 -0800
commitf43cb5ddcf56ab9387b24ad55c2c30eceb606fad (patch)
treef50903c01c52e38fbe29d9fe6b90395ab360b247 /src/Text/Pandoc/Readers/LaTeX/Parsing.hs
parentc0c8865eaac7089b0304c9b21d981f82ea4c2ebd (diff)
downloadpandoc-f43cb5ddcf56ab9387b24ad55c2c30eceb606fad.tar.gz
LaTeX reader: further performance optimization.
Avoid unnecessary 'doMacros'.
Diffstat (limited to 'src/Text/Pandoc/Readers/LaTeX/Parsing.hs')
-rw-r--r--src/Text/Pandoc/Readers/LaTeX/Parsing.hs42
1 files changed, 19 insertions, 23 deletions
diff --git a/src/Text/Pandoc/Readers/LaTeX/Parsing.hs b/src/Text/Pandoc/Readers/LaTeX/Parsing.hs
index 953747d2f..20311651b 100644
--- a/src/Text/Pandoc/Readers/LaTeX/Parsing.hs
+++ b/src/Text/Pandoc/Readers/LaTeX/Parsing.hs
@@ -446,30 +446,25 @@ satisfyTok f = do
doMacros :: PandocMonad m => LP m ()
doMacros = do
- expanded <- sExpanded <$> getState
- verbatimMode <- sVerbatimMode <$> getState
- unless (expanded || verbatimMode) $ do
- getInput >>= doMacros' 1 >>= setInput
- updateState $ \st -> st{ sExpanded = True }
+ st <- getState
+ unless (sExpanded st || sVerbatimMode st || M.null (sMacros st)) $ do
+ getInput >>= doMacros' 1 >>= setInput
+ updateState $ \s -> s{ sExpanded = True }
doMacros' :: PandocMonad m => Int -> [Tok] -> LP m [Tok]
-doMacros' n inp = do
- macros <- sMacros <$> getState
- if M.null macros
- then return inp
- else
- case inp of
- Tok spos (CtrlSeq "begin") _ : Tok _ Symbol "{" :
- Tok _ Word name : Tok _ Symbol "}" : ts
- -> handleMacros macros n spos name ts <|> return inp
- Tok spos (CtrlSeq "end") _ : Tok _ Symbol "{" :
- Tok _ Word name : Tok _ Symbol "}" : ts
- -> handleMacros macros n spos ("end" <> name) ts <|> return inp
- Tok _ (CtrlSeq "expandafter") _ : t : ts
- -> combineTok t <$> doMacros' n ts
- Tok spos (CtrlSeq name) _ : ts
- -> handleMacros macros n spos name ts <|> return inp
- _ -> return inp
+doMacros' n inp =
+ case inp of
+ Tok spos (CtrlSeq "begin") _ : Tok _ Symbol "{" :
+ Tok _ Word name : Tok _ Symbol "}" : ts
+ -> handleMacros n spos name ts <|> return inp
+ Tok spos (CtrlSeq "end") _ : Tok _ Symbol "{" :
+ Tok _ Word name : Tok _ Symbol "}" : ts
+ -> handleMacros n spos ("end" <> name) ts <|> return inp
+ Tok _ (CtrlSeq "expandafter") _ : t : ts
+ -> combineTok t <$> doMacros' n ts
+ Tok spos (CtrlSeq name) _ : ts
+ -> handleMacros n spos name ts <|> return inp
+ _ -> return inp
where
combineTok (Tok spos (CtrlSeq name) x) (Tok _ Word w : ts)
@@ -510,9 +505,10 @@ doMacros' n inp = do
Tok spos (CtrlSeq x) (txt <> " ") : acc
addTok _ _ spos t acc = setpos spos t : acc
- handleMacros macros n' spos name ts = do
+ handleMacros n' spos name ts = do
when (n' > 20) -- detect macro expansion loops
$ throwError $ PandocMacroLoop name
+ macros <- sMacros <$> getState
case M.lookup name macros of
Nothing -> mzero
Just (Macro expansionPoint argspecs optarg newtoks) -> do