module Drasil.GOOL.CodeAnalysis (
  ExceptionType(..), Exception(loc, exc), printExc, hasLoc, exception, stdExc,
  HasException(..)
) where

-- Eq is needed so that the same exception doesn't get added multiple times 
-- to the list of exceptions for a given method.
data ExceptionType = Standard | FileNotFound | IO deriving ExceptionType -> ExceptionType -> Bool
(ExceptionType -> ExceptionType -> Bool)
-> (ExceptionType -> ExceptionType -> Bool) -> Eq ExceptionType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ExceptionType -> ExceptionType -> Bool
== :: ExceptionType -> ExceptionType -> Bool
$c/= :: ExceptionType -> ExceptionType -> Bool
/= :: ExceptionType -> ExceptionType -> Bool
Eq

-- Stores exception information
data Exception = Exc {
  Exception -> String
loc :: String, -- import string where the exception is defined
  Exception -> String
exc :: String -- name of the exception
}

printExc :: Exception -> String
printExc :: Exception -> String
printExc (Exc String
l String
e) = if String -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
l then String
e else String
l String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"." String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
e

hasLoc :: Exception -> Bool
hasLoc :: Exception -> Bool
hasLoc (Exc [] String
_) = Bool
False
hasLoc (Exc (Char
_:String
_) String
_) = Bool
True

exception :: String -> String -> Exception
exception :: String -> String -> Exception
exception = String -> String -> Exception
Exc

stdExc :: String -> Exception
stdExc :: String -> Exception
stdExc = String -> String -> Exception
Exc String
""

class HasException r where
  toConcreteExc :: ExceptionType -> r Exception