module Language.Qux.Annotated.Exception (
TypeException(..),
pos, message,
) where
import Data.List (intercalate)
import Language.Qux.Annotated.Parser (SourcePos)
import Language.Qux.Syntax
data TypeException = TypeException SourcePos String
| DuplicateFunctionName SourcePos Id
| DuplicateParameterName SourcePos Id
| InvalidFunctionCall SourcePos Int Int
| MismatchedType SourcePos Id [Id]
deriving Eq
instance Show TypeException where
show e = show (pos e) ++ ":\n" ++ message e
pos :: TypeException -> SourcePos
pos (TypeException p _) = p
pos (DuplicateFunctionName p _) = p
pos (DuplicateParameterName p _) = p
pos (InvalidFunctionCall p _ _) = p
pos (MismatchedType p _ _) = p
message :: TypeException -> String
message (TypeException _ m) = m
message (DuplicateFunctionName _ name) = "duplicate function name \"" ++ name ++ "\""
message (DuplicateParameterName _ name) = "duplicate parameter name \"" ++ name ++ "\""
message (InvalidFunctionCall _ received expected) = intercalate " " [
"invalid arguments count", show received,
"\nexpecting", show expected
]
message (MismatchedType _ received expects) = intercalate " " [
"unexpected type", "\"" ++ received ++ "\"",
"\nexpecting", sentence "or" expects
]
where
sentence _ [x] = x
sentence sep xs = intercalate " " [intercalate ", " (map show $ init xs), sep, show $ last xs]