Skip to content

Commit

Permalink
LaTeX reader: allow macro definitions inside macros.
Browse files Browse the repository at this point in the history
Previously we went into an infinite loop with

```
\newcommand{\noop}[1]{#1}
\noop{\newcommand{\foo}[1]{#1}}
\foo{hi}
```

See #4253.
  • Loading branch information
jgm committed Jan 13, 2018
1 parent 944ed5e commit 44222e0
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/Text/Pandoc/Readers/LaTeX.hs
Original file line number Diff line number Diff line change
Expand Up @@ -442,19 +442,22 @@ doMacros n = do
Just o ->
(:) <$> option o bracketedToks
<*> count (numargs - 1) getarg
let addTok (Tok _ (Arg i) _) acc | i > 0
, i <= numargs =
foldr addTok acc (args !! (i - 1))
-- first boolean param is true if we're tokenizing
-- an argument (in which case we don't want to
-- expand #1 etc.)
let addTok False (Tok _ (Arg i) _) acc | i > 0
, i <= numargs =
foldr (addTok True) acc (args !! (i - 1))
-- add space if needed after control sequence
-- see #4007
addTok (Tok _ (CtrlSeq x) txt)
addTok _ (Tok _ (CtrlSeq x) txt)
acc@(Tok _ Word _ : _)
| not (T.null txt) &&
(isLetter (T.last txt)) =
Tok spos (CtrlSeq x) (txt <> " ") : acc
addTok t acc = setpos spos t : acc
addTok _ t acc = setpos spos t : acc
ts' <- getInput
setInput $ foldr addTok ts' newtoks
setInput $ foldr (addTok False) ts' newtoks
case expansionPoint of
ExpandWhenUsed ->
if n > 20 -- detect macro expansion loops
Expand Down

0 comments on commit 44222e0

Please sign in to comment.