1 -----------------------------------------------------------------------------
3 -- Module : Text.Parsec.ByteString.Lazy
4 -- Copyright : (c) Paolo Martini 2007
5 -- License : BSD-style (see the LICENSE file)
7 -- Maintainer : derek.a.elkins@gmail.com
8 -- Stability : provisional
9 -- Portability : portable
11 -- Make lazy ByteStrings an instance of 'Stream' with 'Char' token type.
13 -----------------------------------------------------------------------------
15 {-# LANGUAGE FlexibleInstances #-}
16 {-# OPTIONS_GHC -fno-warn-orphans #-}
18 module Text.Parsec.ByteString.Lazy
19 ( Parser, GenParser, parseFromFile
22 import Text.Parsec.Error
23 import Text.Parsec.Prim
25 import qualified Data.ByteString.Lazy.Char8 as C
27 instance (Monad m) => Stream C.ByteString m Char where
28 uncons = return . C.uncons
30 type Parser = Parsec C.ByteString ()
31 type GenParser t st = Parsec C.ByteString st
33 -- | @parseFromFile p filePath@ runs a lazy bytestring parser @p@ on the
34 -- input read from @filePath@ using 'ByteString.Lazy.Char8.readFile'. Returns either a 'ParseError'
35 -- ('Left') or a value of type @a@ ('Right').
37 -- > main = do{ result <- parseFromFile numbers "digits.txt"
39 -- > Left err -> print err
40 -- > Right xs -> print (sum xs)
42 parseFromFile :: Parser a -> String -> IO (Either ParseError a)
44 = do input <- C.readFile fname
45 return (runP p () fname input)