on master help sign in
Removed textbook-code comments, beautified headings
02c94c3Melvar Chen
contribs/lib/SimpleParser/SimpleParser.idr+16−24
@@ -14,10 +14,9 @@ import Builtins
14%access public
14%access public
15
15
16
16
17−{-
17+--------------------------------------------------------------------------------
18−The monad of parsers
18+-- The monad of parsers
19−--------------------
19+--------------------------------------------------------------------------------
20−-}
21
20
22data Parser a = P (String -> Either String (a, String))
21data Parser a = P (String -> Either String (a, String))
23
22
@@ -25,23 +24,17 @@ parse : Parser a -> String -> Either String (a, String)
25parse (P p) inp = p inp
24parse (P p) inp = p inp
26
25
27instance Functor Parser where
26instance Functor Parser where
28− -- fmap : (a -> b) -> f a -> f b
29− -- fmap : (a -> b) -> Parser a -> Parser b
30− -- Given a function (a -> b), and a parser a, make a parser b
31 fmap f p = P (\inp => case parse p inp of
27 fmap f p = P (\inp => case parse p inp of
32 Left err => Left err
28 Left err => Left err
33− -- Apply f to the v that we got from parsing
34 Right (v, rest) => Right ((f v), rest))
29 Right (v, rest) => Right ((f v), rest))
35
30
36instance Applicative Parser where
31instance Applicative Parser where
37 pure v = P (\inp => Right (v, inp))
32 pure v = P (\inp => Right (v, inp))
38− -- Parse to get the function, then parse to get the first argument
39 a <$> b = P (\inp => do (f, rest) <- parse a inp
33 a <$> b = P (\inp => do (f, rest) <- parse a inp
40 (x, rest') <- parse b rest
34 (x, rest') <- parse b rest
41 pure ((f x), rest'))
35 pure ((f x), rest'))
42
36
43instance Monad Parser where
37instance Monad Parser where
44− -- m a -> (a -> m b) -> m b
45 p >>= f = P (\inp => case parse p inp of
38 p >>= f = P (\inp => case parse p inp of
46 Left err => Left err
39 Left err => Left err
47 Right (v,rest) => parse (f v) rest)
40 Right (v,rest) => parse (f v) rest)
@@ -53,10 +46,9 @@ instance Alternative Parser where
53 Right (v,out) => Right (v,out))
46 Right (v,out) => Right (v,out))
54
47
55
48
56−{-
49+--------------------------------------------------------------------------------
57−Basic parsers
50+-- Basic parsers
58−-------------
51+--------------------------------------------------------------------------------
59−-}
60
52
61failure : String -> Parser a
53failure : String -> Parser a
62failure msg = P (\inp => Left msg)
54failure msg = P (\inp => Left msg)
@@ -67,9 +59,9 @@ item = P (\inp => case unpack inp of
67 (x::xs) => Right (x, pack xs))
59 (x::xs) => Right (x, pack xs))
68
60
69
61
70−--{-
62+--------------------------------------------------------------------------------
71−--Derived primitives
63+-- Derived primitives
72−---}
64+--------------------------------------------------------------------------------
73
65
74sat : (Char -> Bool) -> Parser Char
66sat : (Char -> Bool) -> Parser Char
75sat p = item >>= (\x => if p x then pure x else failure "failed")
67sat p = item >>= (\x => if p x then pure x else failure "failed")
@@ -132,9 +124,11 @@ int = neg <|> nat
132space : Parser ()
124space : Parser ()
133space = do many (sat isSpace)
125space = do many (sat isSpace)
134 pure ()
126 pure ()
135−--{-
127+
128+
129+--------------------------------------------------------------------------------
136−--Ignoring spacing
130+-- Ignoring spacing
137−---}
131+--------------------------------------------------------------------------------
138
132
139token : Parser a -> Parser a
133token : Parser a -> Parser a
140token p = do space
134token p = do space
@@ -157,12 +151,10 @@ symbol xs = token (string xs)
157strToken : Parser String
151strToken : Parser String
158strToken = fmap pack (token (many1 alphanum))
152strToken = fmap pack (token (many1 alphanum))
159
153
160−--apply : Parser a -> String -> List (a,String)
161−--apply p = parse (space >>= (\_ => p))
162
154
163−--{-
155+--------------------------------------------------------------------------------
164-- Expressions
156-- Expressions
165−---}
157+--------------------------------------------------------------------------------
166
158
167expr : Parser Int
159expr : Parser Int
168factor : Parser Int
160factor : Parser Int