[hs] Move parsing stuff to Aoc.Parse

This commit is contained in:
Joscha 2020-12-03 01:35:01 +00:00
parent b87c8e4555
commit 320fc25e06
2 changed files with 23 additions and 4 deletions

View file

@ -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

21
hs/src/Aoc/Parse.hs Normal file
View file

@ -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