From: pimjager Date: Mon, 29 Feb 2016 19:25:54 +0000 (+0100) Subject: Added eof parser X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=3275e6545ebf0901e962065416cf95f5b35fa828;p=cc1516.git Added eof parser --- diff --git a/src/yard.dcl b/src/yard.dcl index cedf0df..a029c4b 100644 --- a/src/yard.dcl +++ b/src/yard.dcl @@ -6,6 +6,7 @@ from StdClass import class Eq from Data.Functor import class Functor from Control.Monad import class Monad from Control.Applicative import class Applicative, class Alternative +import Data.Void :: Error = ParseError | LexError String | Unexpected String Int :: Parser a b = Parser ([a] -> (Either Error b, [a])) @@ -26,3 +27,4 @@ satisfy :: (a -> Bool) -> Parser a a check :: (a -> Bool) -> Parser a a item :: a -> Parser a a | Eq a list :: [a] -> Parser a [a] | Eq a +eof :: Parser a Void diff --git a/src/yard.icl b/src/yard.icl index 81e5883..582ea1d 100644 --- a/src/yard.icl +++ b/src/yard.icl @@ -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" @@ -65,6 +66,11 @@ 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 @@ -75,4 +81,4 @@ 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