aboutsummaryrefslogtreecommitdiffhomepage
path: root/examples/attoparser.hs
blob: b7d8c11093327b3aa163a06c137309829825288c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import Pipes
import Pipes.Text.IO (fromHandle)
import Pipes.Attoparsec (parsed)
import qualified System.IO as IO
import Data.Attoparsec.Text
import Control.Applicative
data Test = Test {
  a :: Int,
  b :: Int
  } deriving (Show)

testParser :: Parser Test
testParser = do
  a <- decimal
  space
  b <- decimal
  endOfLine
  return $ Test a b
  
main = IO.withFile "./testfile" IO.ReadMode $ \handle -> runEffect $
   do leftover <- for (parsed testParser (fromHandle handle)) 
                   (lift . print)
      return () -- ignore unparsed material
      
-- >>> :! cat testfile
-- 1 1
-- 2 2
-- 3 3
-- 4 4
-- 5 5
-- 6 6
-- 7 7
-- 8 8
-- 9 9
-- 10 10

-- >>> main
-- Test {a = 1, b = 1}
-- Test {a = 2, b = 2}
-- Test {a = 3, b = 3}
-- Test {a = 4, b = 4}
-- Test {a = 5, b = 5}
-- Test {a = 6, b = 6}
-- Test {a = 7, b = 7}
-- Test {a = 8, b = 8}
-- Test {a = 9, b = 9}
-- Test {a = 10, b = 10}