Skip to content

Commit 742803d

Browse files
committed
More work on parsing
1 parent c37f674 commit 742803d

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

parsing.purs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,14 @@ fail message = Parser $ \s -> Left (parseError message)
3434

3535
string :: String -> Parser String String
3636
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
3838
_ -> Left $ parseError $ "Expected \"" ++ s ++ "\""
3939

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+
4045
instance Prelude.Monad (Parser s) where
4146
return a = Parser $ \s -> Right (parseResult s a)
4247
(>>=) (Parser p) f = Parser $ \s -> case p s of

test.purs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,23 @@ import Parsing
77

88
parser :: Parser String String
99
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
1225

1326
main = do
14-
case runParser parser "ad" of
27+
case runParser (nested {}) "(((a)))" of
1528
Left (ParseError err) -> Trace.print err.message
1629
Right (ParseResult res) -> Trace.print res.result

0 commit comments

Comments
 (0)