manySep :: [Token] -> ([Token] -> ParserResult) -> Token -> ParserResultList
manySep tokens parsingFunc separator = do
    (aa, ta) <- parsingFunc tokens -- we assume at least one match
    (ab, tb) <- many ta consumingParsingFunc sepCond
    return (aa : ab, tb)
    where
        -- Consume a separator and then parse an element
        consumingParsingFunc partialTokens =
            consume partialTokens separator >>= parsingFunc
        -- Stop when the separator is no longer matched or on EOF
        sepCond (t:_) = t /= separator -- the separator is no longer matched
        sepCond _     = True           -- we hit an EOF


-- For simple left-recursive grammars such as
-- Ta -> Ta 'aug' Tc
--    -> Tc
-- It first generates a flat list of the subtrees, and then forms a recursive
-- tree with the Ast constructor
manySep :: [Token] -> ([Token] -> ParserResult) -> Token -> ParserResultList
manySep tokens parsingFunc separator = do
    (aa, ta) <- parsingFunc tokens -- we assume at least one match
    (ab, tb) <- many ta consumingParsingFunc sepCond
    return (aa : ab, tb)
    where
        -- Consume a separator and then parse an element
        consumingParsingFunc partialTokens =
            consume partialTokens separator >>= parsingFunc
        -- Stop when the separator is no longer matched or on EOF
        sepCond (t:_) = t /= separator -- the separator is no longer matched
        sepCond _     = True           -- we hit an EOF


-- For simple left-recursive grammars such as
-- Ta -> Ta 'aug' Tc
--    -> Tc
-- It first generates a flat list of the subtrees, and then forms a recursive
-- tree with the Ast constructor
manySep tokens parsingFunc separator = do
    (aa, ta) <- parsingFunc tokens -- we assume at least one match
    (ab, tb) <- many ta consumingParsingFunc sepCond
    return (aa : ab, tb)
    where
        -- Consume a separator and then parse an element
        consumingParsingFunc partialTokens =
            consume partialTokens separator >>= parsingFunc
        -- Stop when the separator is no longer matched or on EOF
        sepCond (t:_) = t /= separator -- the separator is no longer matched
        sepCond _     = True           -- we hit an EOF


-- For simple left-recursive grammars such as
-- Ta -> Ta 'aug' Tc
--    -> Tc
-- It first generates a flat list of the subtrees, and then forms a recursive
-- tree with the Ast constructor