-
Notifications
You must be signed in to change notification settings - Fork 0
/
LearnParsers.hs
51 lines (36 loc) · 943 Bytes
/
LearnParsers.hs
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
50
51
module LearnParsers where
import Control.Applicative
import Text.Trifecta
stop :: Parser a
stop = unexpected "stop"
one :: Parser Char
one = char '1'
one' = one >> stop
oneTwo = char '1' >> char '2'
oneTwo' = oneTwo >> stop
testParse :: Parser Char -> IO ()
testParse p = print $ parseString p mempty "123"
pNL s = putStrLn ('\n' : s)
oneEof = one >> eof
oneTwoEof = oneTwo >> eof
oneString :: Parser String
oneString = string "1"
oneTwoString :: Parser String
oneTwoString = string "12"
testParseString :: Parser String -> IO ()
testParseString p = print $ parseString p mempty "123"
p123 :: String -> IO ()
p123 s = print $ parseString p mempty s
where
p = (try (string "123") <|> try (string "12") <|> try (string "1")) <* eof
main = do
pNL "stop:"
testParse stop
pNL "one:"
testParse one
pNL "one':"
testParse one'
pNL "oneTwo:"
testParse oneTwo
pNL "oneTwo':"
testParse oneTwo'