]> git.immae.eu Git - github/fretlink/text-pipes.git/blob - examples/attoparser.hs
encoding documentation
[github/fretlink/text-pipes.git] / examples / attoparser.hs
1 import Pipes
2 import Pipes.Text.IO (fromHandle)
3 import Pipes.Attoparsec (parsed)
4 import qualified System.IO as IO
5 import Data.Attoparsec.Text
6 import Control.Applicative
7 data Test = Test {
8 a :: Int,
9 b :: Int
10 } deriving (Show)
11
12 testParser :: Parser Test
13 testParser = do
14 a <- decimal
15 space
16 b <- decimal
17 endOfLine
18 return $ Test a b
19
20 main = IO.withFile "./testfile" IO.ReadMode $ \handle -> runEffect $
21 do leftover <- for (parsed testParser (fromHandle handle))
22 (lift . print)
23 return () -- ignore unparsed material
24
25 -- >>> :! cat testfile
26 -- 1 1
27 -- 2 2
28 -- 3 3
29 -- 4 4
30 -- 5 5
31 -- 6 6
32 -- 7 7
33 -- 8 8
34 -- 9 9
35 -- 10 10
36
37 -- >>> main
38 -- Test {a = 1, b = 1}
39 -- Test {a = 2, b = 2}
40 -- Test {a = 3, b = 3}
41 -- Test {a = 4, b = 4}
42 -- Test {a = 5, b = 5}
43 -- Test {a = 6, b = 6}
44 -- Test {a = 7, b = 7}
45 -- Test {a = 8, b = 8}
46 -- Test {a = 9, b = 9}
47 -- Test {a = 10, b = 10}
48
49