summaryrefslogtreecommitdiff
path: root/Haskell-book/24/LearnParsers/src/LearnParsers.hs
blob: 9c349fdb581c42283133a9664bda6b097b127873 (plain)
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
module LearnParsers where

import Text.Trifecta

stop :: Parser a
stop = unexpected "stop"

-- read a single character '1'
one = char '1'

-- read a single character '1', then die
one' = one >> stop
-- equivalent to char '1' >> stop

-- read two characters, '1', and '2'
oneTwo = char '1' >> char '2'

-- read two characters,
-- '1' and '2', then die

oneTwo' = oneTwo >> stop

testParse :: Parser Char -> IO ()
testParse p = print $ parseString p mempty "123"

pNL s = putStrLn ('\n' : s)

oneTwoThree :: Parser String
oneTwoThree = choice
            [ string "123"
            , string "12"
            , string "1"
            ]

oneTwoThree' = oneTwoThree >> stop

testParse' :: Parser String -> IO ()
testParse' p = print $ parseString p mempty "123"

oneTwoThree'' :: Parser Char
oneTwoThree'' = choice
        [ one
        , oneTwo
        , char '1' >> char '2' >> char '3'
        ]