From 65c8beecb14f6d09c49504d74beedd58cc7ddd17 Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Wed, 7 Sep 2022 16:26:50 +0100 Subject: Better project layout, removed warnings --- src/Micro/Error.hs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/Micro/Error.hs (limited to 'src/Micro/Error.hs') diff --git a/src/Micro/Error.hs b/src/Micro/Error.hs new file mode 100644 index 0000000..ced1318 --- /dev/null +++ b/src/Micro/Error.hs @@ -0,0 +1,39 @@ +module Micro.Error where + +import Data.List (sort) +import Text.Parsec (SourcePos, errorPos) +import Text.Parsec.Error (ParseError, errorMessages, showErrorMessages) + +data ErrorType = GenericError | TypeError | UnexpectedReturn | AlreadyDefined | NonCallable | Undefined | UndefinedType | InvalidTarget deriving (Show) + +instance Enum ErrorType where + fromEnum GenericError = 0 + fromEnum TypeError = 1 + fromEnum UnexpectedReturn = 2 + fromEnum AlreadyDefined = 3 + fromEnum NonCallable = 4 + fromEnum Undefined = 5 + fromEnum UndefinedType = 6 + fromEnum InvalidTarget = 7 + 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 + +showParserError :: ParseError -> String +showParserError err = + show (errorPos err) ++ " error: syntax error" + ++ showErrorMessages "or" "unknown parser error" " expected:" " found:" "end of input" (errorMessages err) + +instance Ord Error where + 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