git.iko.soy/typeable/pandoc
on master help sign in
LaTeX reader: allow macro definitions inside macros.

Previously we went into an infinite loop with ``` \newcommand{\noop}[1]{#1} \noop{\newcommand{\foo}[1]{#1}} \foo{hi} ``` See #4253.

commit44222e0373f47c986833a609271d495d55ff48de authorJohn MacFarlane <jgm@berkeley.edu> dateSat 13 Jan 2018, 12:10 (9 years ago) parent944ed5e
1 file changed+9−6
src/Text/Pandoc/Readers/LaTeX.hs+9−6
src/Text/Pandoc/Readers/LaTeX.hs+9−6
@@ -442,19 +442,22 @@ doMacros n = do
442442 Just o ->
443443 (:) <$> option o bracketedToks
444444 <*> count (numargs - 1) getarg
445− let addTok (Tok _ (Arg i) _) acc | i > 0
446− , i <= numargs =
447− foldr addTok acc (args !! (i - 1))
445+ -- first boolean param is true if we're tokenizing
446+ -- an argument (in which case we don't want to
447+ -- expand #1 etc.)
448+ let addTok False (Tok _ (Arg i) _) acc | i > 0
449+ , i <= numargs =
450+ foldr (addTok True) acc (args !! (i - 1))
448451 -- add space if needed after control sequence
449452 -- see #4007
450− addTok (Tok _ (CtrlSeq x) txt)
453+ addTok _ (Tok _ (CtrlSeq x) txt)
451454 acc@(Tok _ Word _ : _)
452455 | not (T.null txt) &&
453456 (isLetter (T.last txt)) =
454457 Tok spos (CtrlSeq x) (txt <> " ") : acc
455− addTok t acc = setpos spos t : acc
458+ addTok _ t acc = setpos spos t : acc
456459 ts' <- getInput
457− setInput $ foldr addTok ts' newtoks
460+ setInput $ foldr (addTok False) ts' newtoks
458461 case expansionPoint of
459462 ExpandWhenUsed ->
460463 if n > 20 -- detect macro expansion loops