From 593d78ebd7e2d10c609a4e5e363ed89386ab27cf Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Wed, 17 Aug 2022 22:30:42 +0100 Subject: More sensible error types --- src/Error.hs | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) (limited to 'src/Error.hs') diff --git a/src/Error.hs b/src/Error.hs index 1363120..14b4875 100644 --- a/src/Error.hs +++ b/src/Error.hs @@ -4,19 +4,20 @@ import Data.List (sort) import Text.Parsec (SourcePos, errorPos) import Text.Parsec.Error (ParseError, errorMessages, showErrorMessages) -data Error - = TypeError String SourcePos - | UnexpectedReturn String SourcePos - | Error String SourcePos +data ErrorType = GenericError | TypeError | UnexpectedReturn | AlreadyDefined + +instance Enum ErrorType where + fromEnum GenericError = 0 + fromEnum TypeError = 1 + fromEnum UnexpectedReturn = 2 + fromEnum AlreadyDefined = 3 + toEnum _ = error "toEnum is undefined for Error" + +data Error = Error ErrorType String SourcePos deriving (Eq) instance Show Error where - show (Error message pos) = - show pos ++ " error: " ++ message - -- XXX: can we do this differently? - show (TypeError message pos) = - show pos ++ " error: " ++ message - show (UnexpectedReturn message pos) = + show (Error _ message pos) = show pos ++ " error: " ++ message showParserError :: ParseError -> String @@ -25,7 +26,10 @@ showParserError error = ++ showErrorMessages "or" "unknown parser error" " expecting" " unexpected" "end of input" (errorMessages error) instance Ord Error where - compare (Error _ pos1) (Error _ pos2) = compare pos1 pos2 + compare (Error _ _ pos1) (Error _ _ pos2) = compare pos1 pos2 + +instance Eq ErrorType where + e1 == e2 = fromEnum e1 == fromEnum e2 showErrorList :: [Error] -> String showErrorList errs = unlines $ map show (sort errs) -- cgit v1.2.3