File tree Expand file tree Collapse file tree 2 files changed +22
-4
lines changed Expand file tree Collapse file tree 2 files changed +22
-4
lines changed Original file line number Diff line number Diff line change @@ -34,9 +34,14 @@ fail message = Parser $ \s -> Left (parseError message)
34
34
35
35
string :: String -> Parser String String
36
36
string s = Parser $ \s' -> case indexOfS s' s of
37
- 0 -> Right $ parseResult (substr 0 (lengthS s) s') s
37
+ 0 -> Right $ parseResult (substring (lengthS s) (lengthS s' ) s') s
38
38
_ -> Left $ parseError $ " Expected \" " ++ s ++ " \" "
39
39
40
+ char :: Parser String String
41
+ char = Parser $ \s -> case s of
42
+ " " -> Left $ parseError $ " Unexpected EOF"
43
+ _ -> Right $ parseResult (substring 1 (lengthS s) s) (substr 0 1 s)
44
+
40
45
instance Prelude.Monad (Parser s ) where
41
46
return a = Parser $ \s -> Right (parseResult s a)
42
47
(>>=) (Parser p) f = Parser $ \s -> case p s of
Original file line number Diff line number Diff line change @@ -7,10 +7,23 @@ import Parsing
7
7
8
8
parser :: Parser String String
9
9
parser = do
10
- string " a" <|> string " b"
11
- string " c" <|> string " d"
10
+ s1 <- string " a" <|> string " b"
11
+ s2 <- string s1
12
+ return $ s1 ++ s2
13
+
14
+ parens :: forall a . ({ } -> Parser String a ) -> Parser String a
15
+ parens p = do
16
+ string " ("
17
+ a <- p {}
18
+ string " )"
19
+ return a
20
+
21
+ nested :: { } -> Parser String Number
22
+ nested _ = (do
23
+ string " a"
24
+ return 0 ) <|> ((+) 1 ) <$> parens nested
12
25
13
26
main = do
14
- case runParser parser " ad " of
27
+ case runParser (nested {}) " (((a))) " of
15
28
Left (ParseError err) -> Trace .print err.message
16
29
Right (ParseResult res) -> Trace .print res.result
You can’t perform that action at this time.
0 commit comments