aboutsummaryrefslogtreecommitdiff
path: root/src/Micro/Error.hs
diff options
context:
space:
mode:
authorJuan J. Martinez <jjm@usebox.net>2022-09-07 16:26:50 +0100
committerJuan J. Martinez <jjm@usebox.net>2022-09-07 16:26:50 +0100
commit65c8beecb14f6d09c49504d74beedd58cc7ddd17 (patch)
tree0a39cc6fc3f78153272c6528300936c039351d3e /src/Micro/Error.hs
parent48896c56c39344fa429260d3969eccc93ef8035c (diff)
downloadmicro-lang-hs-65c8beecb14f6d09c49504d74beedd58cc7ddd17.tar.gz
micro-lang-hs-65c8beecb14f6d09c49504d74beedd58cc7ddd17.zip
Better project layout, removed warnings
Diffstat (limited to 'src/Micro/Error.hs')
-rw-r--r--src/Micro/Error.hs39
1 files changed, 39 insertions, 0 deletions
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)