diff options
author | Juan J. Martinez <jjm@usebox.net> | 2022-08-17 22:30:42 +0100 |
---|---|---|
committer | Juan J. Martinez <jjm@usebox.net> | 2022-08-17 22:30:42 +0100 |
commit | 593d78ebd7e2d10c609a4e5e363ed89386ab27cf (patch) | |
tree | af46824bc65ae1fdc4dd25a9e6c395a92ba9479f /src/Error.hs | |
parent | acbe62d01a76e5adb29ff479cca01588c65cf561 (diff) | |
download | micro-lang-hs-593d78ebd7e2d10c609a4e5e363ed89386ab27cf.tar.gz micro-lang-hs-593d78ebd7e2d10c609a4e5e363ed89386ab27cf.zip |
More sensible error types
Diffstat (limited to 'src/Error.hs')
-rw-r--r-- | src/Error.hs | 26 |
1 files changed, 15 insertions, 11 deletions
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) |