module RPGEngine.Parse.Core ( parseWith , parseWithRest , ignoreWS ) where import Text.Parsec ( ParseError, anyChar, endOfLine, spaces, string, anyToken, choice, eof, manyTill, parse ) import Text.Parsec.String ( Parser ) ------------------------------ Exported ------------------------------ -- A wrapper, which takes a parser and some input and returns a -- parsed output. parseWith :: Parser a -> String -> Either ParseError a parseWith parser = parse parser "" -- Also return anything that has not yet been parsed parseWithRest :: Parser a -> String -> Either ParseError (a, String) -- fmap (,) over Parser monad and apply to rest parseWithRest parser = parse ((,) <$> parser <*> rest) "" where rest = manyTill anyToken eof -- Ignore all kinds of whitespace ignoreWS :: Parser a -> Parser a ignoreWS parser = choice [skipComment, spaces] >> parser where skipComment = do{ string "#"; manyTill anyChar endOfLine; return ()}