-- | Lazy \"many\" combinator for Parsec.
--   Courtesy of Tomasz Zielonka.

module Bio.Util.Parsex where
import Text.ParserCombinators.Parsec

lazyMany :: GenParser Char () a -> SourceName -> [Char] -> [a]
lazyMany p file contents = lm state0
      where
        Right state0 = parse getParserState file contents

        lm state = case parse p' "" "" of
                        Left err -> error (show err)
                        Right x -> x
          where
            p' = do
              setParserState state
              choice
                [ do
                    eof
                    return []
                , do
                    x <- p
                    state' <- getParserState
                    return (x : lm state')
                ]