Permalink
Browse files

The Parser lib

  • Loading branch information...
1 parent 98102da commit 0fd44ecf7f75462f3bf6e5bd3bb707639f6a2a1e @tianyicui committed Nov 29, 2010
Showing with 26 additions and 0 deletions.
  1. +26 −0 ch08/Parser.hs
View
@@ -86,3 +86,29 @@ space :: Parser ()
space = do
many (sat isSpace)
return ()
+
+token :: Parser a -> Parser a
+token p = do
+ space
+ v <- p
+ space
+ return v
+
+identifier :: Parser String
+identifier = token ident
+
+natural :: Parser Int
+natural = token nat
+
+symbol :: String -> Parser String
+symbol xs = token (string xs)
+
+-- soooo powerful!
+intListParser :: Parser [Int]
+intListParser = do
+ symbol "["
+ n <- natural
+ ns <- many (do symbol ","
+ natural)
+ symbol "]"
+ return (n:ns)

0 comments on commit 0fd44ec

Please sign in to comment.