From 320fc25e0621b3b550a4c2ad2b9b19137d3fc5d9 Mon Sep 17 00:00:00 2001 From: Joscha Date: Thu, 3 Dec 2020 01:35:01 +0000 Subject: [PATCH] [hs] Move parsing stuff to Aoc.Parse --- hs/src/Aoc/Day.hs | 6 ++---- hs/src/Aoc/Parse.hs | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 hs/src/Aoc/Parse.hs diff --git a/hs/src/Aoc/Day.hs b/hs/src/Aoc/Day.hs index aa4494e..229345c 100644 --- a/hs/src/Aoc/Day.hs +++ b/hs/src/Aoc/Day.hs @@ -4,17 +4,17 @@ module Aoc.Day , dayFile , dayString , dayText - , Parser , dayParser ) where import Control.Monad -import Data.Void import qualified Data.Text as T import qualified Data.Text.IO as T import Text.Megaparsec +import Aoc.Parse + data Day = DayPure String (IO ()) | DayFile String (FilePath -> IO ()) @@ -31,8 +31,6 @@ dayString name f = dayFile name $ f <=< readFile dayText :: String -> (T.Text -> IO ()) -> Day dayText name f = dayFile name $ f <=< T.readFile -type Parser = Parsec Void T.Text - dayParser :: String -> Parser a -> (a -> IO ()) -> Day dayParser name p f = dayFile name $ \path -> do text <- T.readFile path diff --git a/hs/src/Aoc/Parse.hs b/hs/src/Aoc/Parse.hs new file mode 100644 index 0000000..d6d62d7 --- /dev/null +++ b/hs/src/Aoc/Parse.hs @@ -0,0 +1,21 @@ +module Aoc.Parse + ( module Text.Megaparsec + , module Text.Megaparsec.Char + , module Text.Megaparsec.Char.Lexer + , Parser + , manyLines + ) where + +import Data.Void + +import qualified Data.Text as T +import Text.Megaparsec +import Text.Megaparsec.Char +import Text.Megaparsec.Char.Lexer (binary, decimal, float, + hexadecimal, octal, scientific, + signed) + +type Parser = Parsec Void T.Text + +manyLines :: Parser a -> Parser [a] +manyLines p = sepBy p newline <* optional newline <* eof