{-# LANGUAGE OverloadedStrings #-}
module Main where
import qualified Data.ByteString as B
import Pipes
import qualified Pipes.Prelude as P
import qualified Pipes.Attoparsec as P
import Data.Attoparsec.ByteString.Char8 as A
data Foo = Foo deriving Show
data Bar = Bar deriving Show
data Baz = Baz deriving Show
data Command = Command Foo Bar Baz deriving Show
data Escape = Escape deriving Show
fooParser :: Parser Foo
fooParser = string "foo" >> return Foo
barParser :: Parser Bar
barParser = string "bar" >> return Bar
bazParser :: Parser Baz
bazParser = string "baz" >> return Baz
exitParser :: Parser Escape
exitParser = string "escape" >> return Escape
commandParser :: Parser Command
commandParser =
Command <$> fooParser
<*> barParser
<*> bazParser
example :: Producer B.ByteString IO ()
example = do
yield "foobarbaz"
yield "foo"
yield "bar"
yield "invalid" -- skip over invalid input
yield "baz"
yield "foo"
yield "escape" -- exit early from parse based on specific value
yield "foobarbaz"
main :: IO ()
main = do
runEffect $ P.parsed commandParser example >-> P.print
return ()