ignoreUntil :: TeXParser Token -> TeXParser ()
ignoreUntil stop =
  ignoreUntilRec 0
  where
    stopLevel :: Integer -> TeXParser ()
    stopLevel 0 = return ()
    stopLevel _ = fail "not at level 0"

    ignoreUntilRec :: Integer -> TeXParser ()
    ignoreUntilRec level =
      (stopLevel level >> (lookAhead stop) >> return ()) <|>
      (ifToken >> ignoreUntilRec (level + 1)) <|>
      (fiToken >> ignoreUntilRec (level - 1)) <|>
      (anyToken >> ignoreUntilRec level)
ignoreUntil :: TeXParser Token -> TeXParser ()
ignoreUntil stop =
  ignoreUntilRec 0
  where
    stopLevel :: Integer -> TeXParser ()
    stopLevel 0 = return ()
    stopLevel _ = fail "not at level 0"

    ignoreUntilRec :: Integer -> TeXParser ()
    ignoreUntilRec level =
      (stopLevel level >> (lookAhead stop) >> return ()) <|>
      (ifToken >> ignoreUntilRec (level + 1)) <|>
      (fiToken >> ignoreUntilRec (level - 1)) <|>
      (anyToken >> ignoreUntilRec level)
ignoreUntil stop =
  ignoreUntilRec 0
  where
    stopLevel :: Integer -> TeXParser ()
    stopLevel 0 = return ()
    stopLevel _ = fail "not at level 0"

    ignoreUntilRec :: Integer -> TeXParser ()
    ignoreUntilRec level =
      (stopLevel level >> (lookAhead stop) >> return ()) <|>
      (ifToken >> ignoreUntilRec (level + 1)) <|>
      (fiToken >> ignoreUntilRec (level - 1)) <|>
      (anyToken >> ignoreUntilRec level)