Added eof parser
[cc1516.git] / src / yard.icl
index bded7fb..582ea1d 100644 (file)
@@ -12,6 +12,7 @@ import Data.Either
 import Control.Monad
 import Control.Applicative
 from Data.Func import $
+import Data.Void
 
 instance toString Error where
        toString ParseError = "General parse error"
@@ -24,7 +25,7 @@ runParser (Parser f) i = f i
 instance + Error where
     (+) ParseError r = r 
     (+) r ParseError = r
-    (+) _ r          = r
+    (+) r _          = r
  
 instance Functor (Parser a) where
     fmap f m = liftM f m
@@ -60,11 +61,24 @@ top = Parser \i -> case i of
     []      = (Left ParseError, [])
     [x:xs]  = (Right x, xs)
 
+peek :: Parser a a
+peek = Parser \i -> case i of
+    []      = (Left ParseError, [])
+    [x:xs]  = (Right x, [x:xs])
+
+eof :: Parser a Void
+eof = Parser \i -> case i of 
+    []      = (Right Void, [])
+    _       = (Left ParseError, i)
+
 satisfy :: (a -> Bool) -> Parser a a
 satisfy f = top >>= \r -> if (f r) (return r) fail
 
+check :: (a -> Bool) -> Parser a a
+check f = peek >>= \r -> if (f r) (return r) fail
+
 item :: a -> Parser a a | Eq a
 item a  = satisfy ((==)a)
 
 list :: [a] -> Parser a [a] | Eq a
-list as = mapM item as
+list as = mapM item as
\ No newline at end of file