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

-- Parse the second branch of a conditional.
--
-- The flag argument indicates whether to expand the branch.
condBranches :: HandleTeXIO m => (Bool, Bool) -> [Token] -> LexerT m ([Token], [Token])
condBranches (expandLeft,expandRight) ltoks = do
  t <- tokenCond expandLeft
  s <- getState
  let means = meansPrimitive s
  case t of
    [CtrlSeq n a]
      | (n, a) `means` "fi" ->
          return (ltoks, [])
      | (n, a) `means` "else" ->
          (,) ltoks <$> condRightBranch expandRight []
      | any ((n, a) `means`) conditionalHeads ->
          -- handle embedded conditional in dead branch
          condBranches (False, False) [] *>
            condBranches (expandLeft, expandRight) ltoks
      | otherwise ->
          condBranches (expandLeft, expandRight) (ltoks ++ t)
    _ -> condBranches (expandLeft, expandRight) (ltoks ++ t)

-- Parse the second branch of a conditional.
--
-- The flag argument indicates whether to expand the branch.
condBranches (expandLeft,expandRight) ltoks = do
  t <- tokenCond expandLeft
  s <- getState
  let means = meansPrimitive s
  case t of
    [CtrlSeq n a]
      | (n, a) `means` "fi" ->
          return (ltoks, [])
      | (n, a) `means` "else" ->
          (,) ltoks <$> condRightBranch expandRight []
      | any ((n, a) `means`) conditionalHeads ->
          -- handle embedded conditional in dead branch
          condBranches (False, False) [] *>
            condBranches (expandLeft, expandRight) ltoks
      | otherwise ->
          condBranches (expandLeft, expandRight) (ltoks ++ t)
    _ -> condBranches (expandLeft, expandRight) (ltoks ++ t)

-- Parse the second branch of a conditional.
--
-- The flag argument indicates whether to expand the branch.