condRightBranch :: HandleTeXIO m => Bool -> [Token] -> LexerT m [Token]
condRightBranch expandMode toks = do
  t <- tokenCond expandMode
  s <- getState
  let means = meansPrimitive s
  case t of
    [CtrlSeq n a]
      | (n, a) `means` "fi" ->
          return toks
      | any ((n, a) `means`) conditionalHeads ->
        -- handle embedded conditional in dead branch
          condBranches (False, False) [] *>
            condRightBranch expandMode toks
      | otherwise ->
          condRightBranch expandMode (toks ++ t)
    _ -> condRightBranch expandMode (toks ++ t)

-- Parse a token in a conditional.
--
-- Note: Grouping characters are parsed literally.
condRightBranch :: HandleTeXIO m => Bool -> [Token] -> LexerT m [Token]
condRightBranch expandMode toks = do
  t <- tokenCond expandMode
  s <- getState
  let means = meansPrimitive s
  case t of
    [CtrlSeq n a]
      | (n, a) `means` "fi" ->
          return toks
      | any ((n, a) `means`) conditionalHeads ->
        -- handle embedded conditional in dead branch
          condBranches (False, False) [] *>
            condRightBranch expandMode toks
      | otherwise ->
          condRightBranch expandMode (toks ++ t)
    _ -> condRightBranch expandMode (toks ++ t)

-- Parse a token in a conditional.
--
-- Note: Grouping characters are parsed literally.
condRightBranch expandMode toks = do
  t <- tokenCond expandMode
  s <- getState
  let means = meansPrimitive s
  case t of
    [CtrlSeq n a]
      | (n, a) `means` "fi" ->
          return toks
      | any ((n, a) `means`) conditionalHeads ->
        -- handle embedded conditional in dead branch
          condBranches (False, False) [] *>
            condRightBranch expandMode toks
      | otherwise ->
          condRightBranch expandMode (toks ++ t)
    _ -> condRightBranch expandMode (toks ++ t)

-- Parse a token in a conditional.
--
-- Note: Grouping characters are parsed literally.