{-# LANGUAGE OverloadedStrings #-}

module Data.Attoparsec.Text.Machine where

import Data.Attoparsec.Machine (processParserWith, streamParserWith)
import Data.Attoparsec.Text (Parser, parse, takeWhile)
import Data.Machine (ProcessT, asParts, auto, (<~))
import Data.Text (Text)

asLines :: Monad m => ProcessT m Text Text
asLines :: ProcessT m Text Text
asLines = MachineT m (Is [Text]) Text
forall (f :: * -> *) a. Foldable f => Process (f a) a
asParts MachineT m (Is [Text]) Text
-> MachineT m (Is Text) [Text] -> ProcessT m Text Text
forall (m :: * -> *) b c (k :: * -> *).
Monad m =>
ProcessT m b c -> MachineT m k b -> MachineT m k c
<~ (Either String Text -> [Text])
-> Process (Either String Text) [Text]
forall (k :: * -> * -> *) a b. Automaton k => k a b -> Process a b
auto Either String Text -> [Text]
forall a a. Either a a -> [a]
unpackLine MachineT m (Is (Either String Text)) [Text]
-> MachineT m (Is Text) (Either String Text)
-> MachineT m (Is Text) [Text]
forall (m :: * -> *) b c (k :: * -> *).
Monad m =>
ProcessT m b c -> MachineT m k b -> MachineT m k c
<~ Parser Text -> MachineT m (Is Text) (Either String Text)
forall (m :: * -> *) a.
Monad m =>
Parser a -> ProcessT m Text (Either String a)
streamParser (((Char -> Bool) -> Parser Text
Data.Attoparsec.Text.takeWhile ((Char -> Bool) -> Parser Text) -> (Char -> Bool) -> Parser Text
forall a b. (a -> b) -> a -> b
$ \Char
w -> Char
w Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'\n') Parser Text -> Parser Text -> Parser Text
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Parser Text
"\n")
  where
    unpackLine :: Either a a -> [a]
unpackLine (Right a
txt) = [a
txt]
    unpackLine (Left a
_) = []

processParser :: Monad m => Parser a -> ProcessT m Text (Either String (Text, a))
processParser :: Parser a -> ProcessT m Text (Either String (Text, a))
processParser Parser a
p = (Text -> IResult Text a)
-> ProcessT m Text (Either String (Text, a))
forall i (m :: * -> *) a.
(Monoid i, Monad m) =>
(i -> IResult i a) -> ProcessT m i (Either String (i, a))
processParserWith ((Text -> IResult Text a)
 -> ProcessT m Text (Either String (Text, a)))
-> (Text -> IResult Text a)
-> ProcessT m Text (Either String (Text, a))
forall a b. (a -> b) -> a -> b
$ Parser a -> Text -> IResult Text a
forall a. Parser a -> Text -> Result a
parse Parser a
p

streamParser :: Monad m => Parser a -> ProcessT m Text (Either String a)
streamParser :: Parser a -> ProcessT m Text (Either String a)
streamParser Parser a
p = (Text -> IResult Text a) -> ProcessT m Text (Either String a)
forall i (m :: * -> *) a.
(Monoid i, Monad m) =>
(i -> IResult i a) -> ProcessT m i (Either String a)
streamParserWith ((Text -> IResult Text a) -> ProcessT m Text (Either String a))
-> (Text -> IResult Text a) -> ProcessT m Text (Either String a)
forall a b. (a -> b) -> a -> b
$ Parser a -> Text -> IResult Text a
forall a. Parser a -> Text -> Result a
parse Parser a
p