From 93052875e06a4e73989a5c1eb75292d14330ac98 Mon Sep 17 00:00:00 2001 From: Willem Van Onsem Date: Tue, 29 Dec 2020 17:32:22 +0100 Subject: [PATCH 1/2] make functions to parse files with stateful parsers --- src/Text/Parsec/ByteString.hs | 24 ++++++++++++++++++------ src/Text/Parsec/ByteString/Lazy.hs | 26 +++++++++++++++++++------- src/Text/Parsec/String.hs | 25 +++++++++++++++++++------ src/Text/Parsec/Text.hs | 28 +++++++++++++++++++--------- src/Text/Parsec/Text/Lazy.hs | 28 +++++++++++++++++++--------- 5 files changed, 94 insertions(+), 37 deletions(-) diff --git a/src/Text/Parsec/ByteString.hs b/src/Text/Parsec/ByteString.hs index e65dee14..2840937c 100644 --- a/src/Text/Parsec/ByteString.hs +++ b/src/Text/Parsec/ByteString.hs @@ -15,7 +15,7 @@ ----------------------------------------------------------------------------- module Text.Parsec.ByteString - ( Parser, GenParser, parseFromFile + ( Parser, ParserU, GenParser, parseFromFile, parseFromFile' ) where import Text.Parsec.Error @@ -23,9 +23,24 @@ import Text.Parsec.Prim import qualified Data.ByteString.Char8 as C -type Parser = Parsec C.ByteString () +type ParserU u = Parsec C.ByteString u +type Parser = ParserU () type GenParser t st = Parsec C.ByteString st + +-- | @parseFromFile' p u filePath@ runs a strict bytestring parser @p@ on the +-- input read from @filePath@ using 'ByteString.Char8.readFile' with start state @u@. +-- Returns either a 'ParseError' ('Left') or a value of type @a@ ('Right'). +-- +-- > main = do{ result <- parseFromFile' numbers () "digits.txt" +-- > ; case result of +-- > Left err -> print err +-- > Right xs -> print (sum xs) +-- > } +parseFromFile' :: ParserU u a -> u -> FilePath -> IO (Either ParseError a) +parseFromFile' p u fname = runP p u fname <$> C.readFile fname + + -- | @parseFromFile p filePath@ runs a strict bytestring parser @p@ on the -- input read from @filePath@ using 'ByteString.Char8.readFile'. Returns either a 'ParseError' -- ('Left') or a value of type @a@ ('Right'). @@ -35,8 +50,5 @@ type GenParser t st = Parsec C.ByteString st -- > Left err -> print err -- > Right xs -> print (sum xs) -- > } - parseFromFile :: Parser a -> FilePath -> IO (Either ParseError a) -parseFromFile p fname - = do input <- C.readFile fname - return (runP p () fname input) +parseFromFile = (`parseFromFile'` ()) diff --git a/src/Text/Parsec/ByteString/Lazy.hs b/src/Text/Parsec/ByteString/Lazy.hs index 2a457814..5dfcfd10 100644 --- a/src/Text/Parsec/ByteString/Lazy.hs +++ b/src/Text/Parsec/ByteString/Lazy.hs @@ -15,7 +15,7 @@ ----------------------------------------------------------------------------- module Text.Parsec.ByteString.Lazy - ( Parser, GenParser, parseFromFile + ( Parser, ParserU, GenParser, parseFromFile, parseFromFile' ) where import Text.Parsec.Error @@ -23,10 +23,25 @@ import Text.Parsec.Prim import qualified Data.ByteString.Lazy.Char8 as C -type Parser = Parsec C.ByteString () +type ParserU u = Parsec C.ByteString u +type Parser = ParserU () type GenParser t st = Parsec C.ByteString st --- | @parseFromFile p filePath@ runs a lazy bytestring parser @p@ on the + +-- | @parseFromFile' p u filePath@ runs a strict bytestring parser @p@ on the +-- input read from @filePath@ using 'ByteString.Lazy.Char8.readFile' with start state @u@. +-- Returns either a 'ParseError' ('Left') or a value of type @a@ ('Right'). +-- +-- > main = do{ result <- parseFromFile' numbers () "digits.txt" +-- > ; case result of +-- > Left err -> print err +-- > Right xs -> print (sum xs) +-- > } +parseFromFile' :: ParserU u a -> u -> FilePath -> IO (Either ParseError a) +parseFromFile' p u fname = runP p u fname <$> C.readFile fname + + +-- | @parseFromFile p filePath@ runs a strict bytestring parser @p@ on the -- input read from @filePath@ using 'ByteString.Lazy.Char8.readFile'. Returns either a 'ParseError' -- ('Left') or a value of type @a@ ('Right'). -- @@ -35,8 +50,5 @@ type GenParser t st = Parsec C.ByteString st -- > Left err -> print err -- > Right xs -> print (sum xs) -- > } - parseFromFile :: Parser a -> FilePath -> IO (Either ParseError a) -parseFromFile p fname - = do input <- C.readFile fname - return (runP p () fname input) +parseFromFile = (`parseFromFile'` ()) diff --git a/src/Text/Parsec/String.hs b/src/Text/Parsec/String.hs index 67efa537..e1ecc396 100644 --- a/src/Text/Parsec/String.hs +++ b/src/Text/Parsec/String.hs @@ -15,16 +15,31 @@ ----------------------------------------------------------------------------- module Text.Parsec.String - ( Parser, GenParser, parseFromFile + ( Parser, ParserU, GenParser, parseFromFile, parseFromFile' ) where import Text.Parsec.Error import Text.Parsec.Prim -type Parser = Parsec String () +type ParserU u = Parsec String u +type Parser = ParserU () type GenParser tok st = Parsec [tok] st --- | @parseFromFile p filePath@ runs a string parser @p@ on the + +-- | @parseFromFile' p u filePath@ runs a strict bytestring parser @p@ on the +-- input read from @filePath@ using 'Prelude.readFile' with start state @u@. +-- Returns either a 'ParseError' ('Left') or a value of type @a@ ('Right'). +-- +-- > main = do{ result <- parseFromFile' numbers () "digits.txt" +-- > ; case result of +-- > Left err -> print err +-- > Right xs -> print (sum xs) +-- > } +parseFromFile' :: ParserU u a -> u -> FilePath -> IO (Either ParseError a) +parseFromFile' p u fname = runP p u fname <$> readFile fname + + +-- | @parseFromFile p filePath@ runs a strict bytestring parser @p@ on the -- input read from @filePath@ using 'Prelude.readFile'. Returns either a 'ParseError' -- ('Left') or a value of type @a@ ('Right'). -- @@ -34,6 +49,4 @@ type GenParser tok st = Parsec [tok] st -- > Right xs -> print (sum xs) -- > } parseFromFile :: Parser a -> FilePath -> IO (Either ParseError a) -parseFromFile p fname - = do input <- readFile fname - return (runP p () fname input) +parseFromFile = (`parseFromFile'` ()) diff --git a/src/Text/Parsec/Text.hs b/src/Text/Parsec/Text.hs index 0e609686..b346d7bd 100644 --- a/src/Text/Parsec/Text.hs +++ b/src/Text/Parsec/Text.hs @@ -15,7 +15,7 @@ ----------------------------------------------------------------------------- module Text.Parsec.Text - ( Parser, GenParser, parseFromFile + ( Parser, ParserU, GenParser, parseFromFile, parseFromFile' ) where import qualified Data.Text as Text @@ -23,10 +23,25 @@ import Text.Parsec.Prim import Text.Parsec.Error import Data.Text.IO as T -type Parser = Parsec Text.Text () +type ParserU u = Parsec Text.Text u +type Parser = ParserU () type GenParser st = Parsec Text.Text st --- | @parseFromFile p filePath@ runs a strict text parser @p@ on the + +-- | @parseFromFile' p u filePath@ runs a strict bytestring parser @p@ on the +-- input read from @filePath@ using 'Data.Text.IO.readFile' with start state @u@. +-- Returns either a 'ParseError' ('Left') or a value of type @a@ ('Right'). +-- +-- > main = do{ result <- parseFromFile' numbers () "digits.txt" +-- > ; case result of +-- > Left err -> print err +-- > Right xs -> print (sum xs) +-- > } +parseFromFile' :: ParserU u a -> u -> FilePath -> IO (Either ParseError a) +parseFromFile' p u fname = runP p u fname <$> T.readFile fname + + +-- | @parseFromFile p filePath@ runs a strict bytestring parser @p@ on the -- input read from @filePath@ using 'Data.Text.IO.readFile'. Returns either a 'ParseError' -- ('Left') or a value of type @a@ ('Right'). -- @@ -35,10 +50,5 @@ type GenParser st = Parsec Text.Text st -- > Left err -> print err -- > Right xs -> print (sum xs) -- > } --- --- @since 3.1.14.0 - parseFromFile :: Parser a -> FilePath -> IO (Either ParseError a) -parseFromFile p fname - = do input <- T.readFile fname - return (runP p () fname input) +parseFromFile = (`parseFromFile'` ()) diff --git a/src/Text/Parsec/Text/Lazy.hs b/src/Text/Parsec/Text/Lazy.hs index 3cddb675..789881bd 100644 --- a/src/Text/Parsec/Text/Lazy.hs +++ b/src/Text/Parsec/Text/Lazy.hs @@ -15,7 +15,7 @@ ----------------------------------------------------------------------------- module Text.Parsec.Text.Lazy - ( Parser, GenParser, parseFromFile + ( Parser, ParserU, GenParser, parseFromFile, parseFromFile' ) where import qualified Data.Text.Lazy as Text @@ -23,10 +23,25 @@ import Text.Parsec.Prim import Text.Parsec.Error import Data.Text.Lazy.IO as TL -type Parser = Parsec Text.Text () +type ParserU u = Parsec Text.Text u +type Parser = ParserU () type GenParser st = Parsec Text.Text st --- | @parseFromFile p filePath@ runs a strict text parser @p@ on the + +-- | @parseFromFile' p u filePath@ runs a strict bytestring parser @p@ on the +-- input read from @filePath@ using 'Data.Text.Lazy.IO.readFile' with start state @u@. +-- Returns either a 'ParseError' ('Left') or a value of type @a@ ('Right'). +-- +-- > main = do{ result <- parseFromFile' numbers () "digits.txt" +-- > ; case result of +-- > Left err -> print err +-- > Right xs -> print (sum xs) +-- > } +parseFromFile' :: ParserU u a -> u -> FilePath -> IO (Either ParseError a) +parseFromFile' p u fname = runP p u fname <$> TL.readFile fname + + +-- | @parseFromFile p filePath@ runs a strict bytestring parser @p@ on the -- input read from @filePath@ using 'Data.Text.Lazy.IO.readFile'. Returns either a 'ParseError' -- ('Left') or a value of type @a@ ('Right'). -- @@ -35,10 +50,5 @@ type GenParser st = Parsec Text.Text st -- > Left err -> print err -- > Right xs -> print (sum xs) -- > } --- --- @since 3.1.14.0 - parseFromFile :: Parser a -> FilePath -> IO (Either ParseError a) -parseFromFile p fname - = do input <- TL.readFile fname - return (runP p () fname input) +parseFromFile = (`parseFromFile'` ()) From c874511df6c50e5f99bd021d4b81ada5e9a4fc94 Mon Sep 17 00:00:00 2001 From: Willem Van Onsem Date: Tue, 29 Dec 2020 20:09:33 +0100 Subject: [PATCH 2/2] conditional import of (<$>) --- src/Text/Parsec/ByteString.hs | 5 +++++ src/Text/Parsec/ByteString/Lazy.hs | 5 +++++ src/Text/Parsec/String.hs | 5 +++++ src/Text/Parsec/Text.hs | 5 +++++ src/Text/Parsec/Text/Lazy.hs | 5 +++++ 5 files changed, 25 insertions(+) diff --git a/src/Text/Parsec/ByteString.hs b/src/Text/Parsec/ByteString.hs index 2840937c..74936e45 100644 --- a/src/Text/Parsec/ByteString.hs +++ b/src/Text/Parsec/ByteString.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE CPP #-} {-# LANGUAGE Safe #-} ----------------------------------------------------------------------------- @@ -18,6 +19,10 @@ module Text.Parsec.ByteString ( Parser, ParserU, GenParser, parseFromFile, parseFromFile' ) where +#if __GLASGOW_HASKELL__ < 710 +import Data.Functor((<$>)) +#endif + import Text.Parsec.Error import Text.Parsec.Prim diff --git a/src/Text/Parsec/ByteString/Lazy.hs b/src/Text/Parsec/ByteString/Lazy.hs index 5dfcfd10..f1d1f28d 100644 --- a/src/Text/Parsec/ByteString/Lazy.hs +++ b/src/Text/Parsec/ByteString/Lazy.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE CPP #-} {-# LANGUAGE Safe #-} ----------------------------------------------------------------------------- @@ -18,6 +19,10 @@ module Text.Parsec.ByteString.Lazy ( Parser, ParserU, GenParser, parseFromFile, parseFromFile' ) where +#if __GLASGOW_HASKELL__ < 710 +import Data.Functor((<$>)) +#endif + import Text.Parsec.Error import Text.Parsec.Prim diff --git a/src/Text/Parsec/String.hs b/src/Text/Parsec/String.hs index e1ecc396..c1f5de08 100644 --- a/src/Text/Parsec/String.hs +++ b/src/Text/Parsec/String.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE CPP #-} {-# LANGUAGE Safe #-} ----------------------------------------------------------------------------- @@ -18,6 +19,10 @@ module Text.Parsec.String ( Parser, ParserU, GenParser, parseFromFile, parseFromFile' ) where +#if __GLASGOW_HASKELL__ < 710 +import Data.Functor((<$>)) +#endif + import Text.Parsec.Error import Text.Parsec.Prim diff --git a/src/Text/Parsec/Text.hs b/src/Text/Parsec/Text.hs index b346d7bd..04de919f 100644 --- a/src/Text/Parsec/Text.hs +++ b/src/Text/Parsec/Text.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE CPP #-} {-# LANGUAGE Safe #-} ----------------------------------------------------------------------------- @@ -18,6 +19,10 @@ module Text.Parsec.Text ( Parser, ParserU, GenParser, parseFromFile, parseFromFile' ) where +#if __GLASGOW_HASKELL__ < 710 +import Data.Functor((<$>)) +#endif + import qualified Data.Text as Text import Text.Parsec.Prim import Text.Parsec.Error diff --git a/src/Text/Parsec/Text/Lazy.hs b/src/Text/Parsec/Text/Lazy.hs index 789881bd..bc50c409 100644 --- a/src/Text/Parsec/Text/Lazy.hs +++ b/src/Text/Parsec/Text/Lazy.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE CPP #-} {-# LANGUAGE Safe #-} ----------------------------------------------------------------------------- @@ -18,6 +19,10 @@ module Text.Parsec.Text.Lazy ( Parser, ParserU, GenParser, parseFromFile, parseFromFile' ) where +#if __GLASGOW_HASKELL__ < 710 +import Data.Functor((<$>)) +#endif + import qualified Data.Text.Lazy as Text import Text.Parsec.Prim import Text.Parsec.Error