-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRispError.hs
More file actions
35 lines (31 loc) · 1.48 KB
/
Copy pathRispError.hs
File metadata and controls
35 lines (31 loc) · 1.48 KB
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
module RispError where
import Risp
import Text.ParserCombinators.Parsec hiding (State)
data RispError = NumArgs Integer [Risp]
| TypeMismatch String Risp
| Parser ParseError
| BadSpecialForm String Risp
| NotFunction String String
| UnboundVar String -- the variable has not been declared
| VarAlreadyExists String
| ReservedKeyword String
| EmptyCharSet
| UnboundCaptureGroupName String
| Default String
showError :: RispError -> String
showError (NumArgs expected found) = "Expected " ++ show expected ++ " args; found values " ++ unwordsList found
showError (TypeMismatch expected found) = "Invalid type: expected " ++ expected ++ ", found " ++ show found
showError (Parser parseErr) = "Parse error at " ++ show parseErr
showError (BadSpecialForm message form) = message ++ ": " ++ show form
showError (NotFunction message func) = message ++ ": " ++ show func
showError (UnboundVar varname) = "Getting an unbound variable: " ++ varname
showError (UnboundCaptureGroupName name) = "Getting an unbound capture group name: " ++ name
showError (VarAlreadyExists varname) = "Already existed var: " ++ varname
showError (ReservedKeyword varname) = "Illegal use of reserved keyword: " ++ varname
showError EmptyCharSet = "CharSet matches 0 characters"
showError (Default message) = show message
instance Show RispError where show = showError
type EitherError = Either RispError
-- get the right value of Either
extractValue :: EitherError a -> a
extractValue (Right val) = val