module Text.Madlibs.Cata.SemErr (
SemanticError (..)
, access
, checkSemantics
, head' ) where
import Text.Madlibs.Internal.Types
import Data.Typeable
import Text.PrettyPrint.ANSI.Leijen
import Control.Exception
import qualified Data.Text as T
import Control.Monad
import qualified Data.Set as S
import Text.Megaparsec.Text
import Text.Megaparsec.Prim
import Text.Megaparsec.Error
import Text.Madlibs.Internal.Utils
data SemanticError = NoReturn | CircularFunctionCalls T.Text T.Text | InsufficientArgs Int Int | DoubleDefinition T.Text | NoContext T.Text
deriving (Typeable)
instance Show SemanticError where
show (DoubleDefinition f) = show $ semErrStart <> text "File contains two declarations of:" <> indent 4 (yellow (text' f))
show NoReturn = show $ semErrStart <> text "File must contain exactly one declaration of :return"
show (NoContext f1) = show $ semErrStart <> text "Call in function: " <> indent 4 (yellow (text' f1)) <> "which is not in scope"
show (CircularFunctionCalls f1 f2) = show $ semErrStart <> text "Circular function declaration between:" <> indent 4 (yellow $ (text' f1) <> (text ", ") <> (text' f2))
show (InsufficientArgs i j) = show $ semErrStart <> text "Insufficent arguments from the command line, given " <> (text . show $ i) <> ", expected at least " <> (text . show $ j)
instance Exception SemanticError where
customError :: String -> Parser a
customError = (failure .$ S.empty) . S.singleton . representFail
showCustomError :: (Show a) => a -> Parser b
showCustomError = customError . show
noReturn :: Parser a
noReturn = showCustomError NoReturn
noContext :: T.Text -> Parser a
noContext f1 = showCustomError $ NoContext f1
circularFunctionCalls :: T.Text -> T.Text -> Parser a
circularFunctionCalls f1 f2 = showCustomError $ CircularFunctionCalls f1 f2
doubleDefinition :: T.Text -> Parser a
doubleDefinition f = showCustomError $ DoubleDefinition f
insufficientArgs :: Int -> Int -> Parser a
insufficientArgs i j = showCustomError $ InsufficientArgs i j
semErrStart :: Doc
semErrStart = dullred (text "\n Semantic Error: ")
text' :: T.Text -> Doc
text' = text . T.unpack
checkSemantics :: [(Key, [(Prob, [PreTok])])] -> Parser [(Key, [(Prob, [PreTok])])]
checkSemantics keys = foldr (<=<) pure ((checkKey "Return"):[checkKey key | key <- allKeys keys ]) keys
where allKeys = fmap name . (concatMap snd) . (concatMap snd)--traversal?
name (Name str _) = str
name (PreTok _) = "Return"
sumProb :: [(Prob, [PreTok])] -> Bool
sumProb = ((==1) . sum . (map fst))
head' :: T.Text -> T.Text -> [a] -> a
head' _ _ (x:xs) = x
head' f1 f2 _ = throw (CircularFunctionCalls f1 f2)
access :: [a] -> Int -> a
access xs i = if (i >= length xs) then throw (InsufficientArgs (length xs) (i+1)) else xs !! i
checkKey :: Key -> [(Key, [(Prob, [PreTok])])] -> Parser [(Key, [(Prob, [PreTok])])]
checkKey key keys
| singleInstance key keys = pure keys
| noInstance key keys = pure keys
| key == "Return" && noInstance key keys = noReturn
| otherwise = doubleDefinition key
singleInstance :: Key -> [(Key, [(Prob, [PreTok])])] -> Bool
singleInstance key = singleton . (filter ((==key) . fst))
where singleton [a] = True
singleton _ = False
noInstance :: Key -> [(Key, [(Prob, [PreTok])])] -> Bool
noInstance key = not . any ((== key) . fst)