-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ch24_LearnParsers.hs
50 lines (40 loc) · 1.02 KB
/
Ch24_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
module Ch24_LearnParsers where
import Text.Trifecta
stop :: Parser a
stop = unexpected "stop"
-- read a single character '1'
one :: Parser Char
one = char '1'
-- read a single character '1', then die
-- equivalent to char '1' >> stop
one' :: Parser b
one' = one >> stop
-- 1. Await a string value
-- 2. Produce a result which may or may not succeed.
-- (A Nothing value means the parse failed.)
-- 3. returns (value produced, remaining string to be parse)
type Parzer a = String -> Maybe (a, String)
-- read two characters, '1', and '2'
oneTwo :: Parser Char
oneTwo = char '1' >> char '2'
-- read two characters,
-- '1' and '2', then die
oneTwo' :: Parser b
oneTwo' = oneTwo >> stop
testParse :: Parser Char -> IO ()
testParse p =
print $ parseString p mempty "123"
println :: String -> IO ()
println s = putStrLn ('\n' : s)
test :: IO ()
test = do
println "stop:"
testParse stop
println "one:"
testParse one
println "one':"
testParse one'
println "oneTwo:"
testParse oneTwo
println "oneTwo':"
testParse oneTwo'