@@ -403,30 +403,30 @@ doMacros n = do
403403 Tok spos (CtrlSeq (name <> w)) (x1 <> w <> x2) : ts
404404 where (x1, x2) = T.break isSpaceOrTab x
405405 combineTok t ts = t:ts
406+ matchTok (Tok _ toktype txt) =
407+ satisfyTok (\(Tok _ toktype' txt') ->
408+ toktype == toktype' &&
409+ txt == txt')
410+ matchPattern toks = try $ mapM_ matchTok toks
411+ getargs argmap [] = return argmap
412+ getargs argmap (Pattern toks : rest) = try $ do
413+ matchPattern toks
414+ getargs argmap rest
415+ getargs argmap (ArgNum i : Pattern toks : rest) =
416+ try $ do
417+ x <- mconcat <$> manyTill
418+ (braced <|> ((:[]) <$> anyTok))
419+ (matchPattern toks)
420+ getargs (M.insert i x argmap) rest
421+ getargs argmap (ArgNum i : rest) = do
422+ x <- try $ spaces >> bracedOrToken
423+ getargs (M.insert i x argmap) rest
406424 handleMacros spos name ts = do
407425 macros <- sMacros <$> getState
408426 case M.lookup name macros of
409427 Nothing -> return ()
410428 Just (Macro expansionPoint argspecs optarg newtoks) -> do
411429 setInput ts
412− let matchTok (Tok _ toktype txt) =
413− satisfyTok (\(Tok _ toktype' txt') ->
414− toktype == toktype' &&
415− txt == txt')
416− let matchPattern toks = try $ mapM_ matchTok toks
417− let getargs argmap [] = return argmap
418− getargs argmap (Pattern toks : rest) = try $ do
419− matchPattern toks
420− getargs argmap rest
421− getargs argmap (ArgNum i : Pattern toks : rest) =
422− try $ do
423− x <- mconcat <$> manyTill
424− (braced <|> ((:[]) <$> anyTok))
425− (matchPattern toks)
426− getargs (M.insert i x argmap) rest
427− getargs argmap (ArgNum i : rest) = do
428− x <- try $ spaces >> bracedOrToken
429− getargs (M.insert i x argmap) rest
430430 args <- case optarg of
431431 Nothing -> getargs M.empty argspecs
432432 Just o -> do
@@ -442,12 +442,13 @@ doMacros n = do
442442 -- see #4007
443443 addTok _ (Tok _ (CtrlSeq x) txt)
444444 acc@(Tok _ Word _ : _)
445− | not (T.null txt) &&
446− isLetter (T.last txt) =
445+ | not (T.null txt)
446+ , isLetter (T.last txt) =
447447 Tok spos (CtrlSeq x) (txt <> " ") : acc
448448 addTok _ t acc = setpos spos t : acc
449449 ts' <- getInput
450450 setInput $ foldr (addTok False) ts' newtoks
451+
451452 case expansionPoint of
452453 ExpandWhenUsed ->
453454 if n > 20 -- detect macro expansion loops