aboutsummaryrefslogtreecommitdiff
path: root/src/Micro/Error.hs
blob: e82c0da1b9d534b9ec1cde46e17c59a772b22e7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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 | InvalidOperation 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
  fromEnum InvalidOperation = 8
  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)