Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Complete library from http://www.cs.nott.ac.uk/~gmh/Parsing.lhs

  • Loading branch information...
commit 7c13a31679217daff03f992649e08e8d6db9c13a 1 parent 0fd44ec
@tianyicui authored
Showing with 14 additions and 2 deletions.
  1. +14 −2 ch08/Parser.hs
View
16 ch08/Parser.hs
@@ -11,6 +11,10 @@ instance Monad Parser where
[] -> []
[(v,out)] -> parse (f v) out)
+instance MonadPlus Parser where
+ mzero = failure
+ mplus = (+++)
+
failure :: Parser a
failure = P (\inp -> [])
@@ -82,6 +86,13 @@ nat = do
xs <- many1 digit
return (read xs)
+int :: Parser Int
+int = do
+ char '-'
+ n <- nat
+ return (-n)
+ +++ nat
+
space :: Parser ()
space = do
many (sat isSpace)
@@ -97,8 +108,9 @@ token p = do
identifier :: Parser String
identifier = token ident
-natural :: Parser Int
+natural, integer :: Parser Int
natural = token nat
+integer = token int
symbol :: String -> Parser String
symbol xs = token (string xs)
@@ -111,4 +123,4 @@ intListParser = do
ns <- many (do symbol ","
natural)
symbol "]"
- return (n:ns)
+ return (n : ns)
Please sign in to comment.
Something went wrong with that request. Please try again.