X-Git-Url: https://git.martlubbers.net/?a=blobdiff_plain;f=src%2Fyard.icl;h=9b2ec23ec1e1a9a9cf8fdd534b0b72be538b2320;hb=fa2b27e75142781978149dc3e906e8b5aae8e603;hp=7b74454c8b203d1a55d73693084dc4e5c299fa7c;hpb=0deec3b5e205945dc8cf8a00ca8cff4c96f42552;p=cc1516.git diff --git a/src/yard.icl b/src/yard.icl index 7b74454..9b2ec23 100644 --- a/src/yard.icl +++ b/src/yard.icl @@ -15,18 +15,15 @@ from Data.Func import $ import Data.Void instance toString Error where - toString ParseError = "General parse error" - toString (LexError e) = "Lexer error: " +++ e - toString (Unexpected e (l,c)) = "Unexpected " +++ e +++ " at " - +++ (toString l) +++ ":" +++ (toString c) + toString (PositionalError l c e) = + concat [toString l,":",toString c,": ",e, "\n"] + toString (Error e) = concat ["-:-: ", e, "\n"] runParser :: (Parser a b) [a] -> (Either Error b, [a]) runParser (Parser f) i = f i instance + Error where - (+) ParseError r = r - (+) r ParseError = r - (+) r _ = r + (+) r1 r2 = r2 //TODO instance Functor (Parser a) where fmap f m = liftM f m @@ -41,7 +38,7 @@ instance Monad (Parser a) where (Left e, _) = (Left e, i) instance Alternative (Parser a) where - empty = Parser \i -> (Left ParseError, i) + empty = Parser \i -> (Left $ Error "" , i) (<|>) p1 p2 = Parser \i -> case runParser p1 i of (Right r, rest) = (Right r, rest) (Left e1, rest) = case runParser p2 i of @@ -59,12 +56,12 @@ fail = empty top :: Parser a a top = Parser \i -> case i of - [] = (Left ParseError, []) + [] = (Left $ Error "", []) [x:xs] = (Right x, xs) peek :: Parser a a peek = Parser \i -> case i of - [] = (Left ParseError, []) + [] = (Left $ Error "", []) [x:xs] = (Right x, [x:xs]) //runs the left parser until the right parser succeeds. Returns the result of the left parser @@ -88,7 +85,7 @@ peek = Parser \i -> case i of eof :: Parser a Void eof = Parser \i -> case i of [] = (Right Void, []) - _ = (Left ParseError, i) + _ = (Left $ Error "", i) satisfy :: (a -> Bool) -> Parser a a satisfy f = top >>= \r -> if (f r) (return r) fail