module Graphics.Implicit.ExtOpenScad.Util.ArgParser where
import Graphics.Implicit.Definitions
import Graphics.Implicit.ExtOpenScad.Definitions
import Graphics.Implicit.ExtOpenScad.Util.OVal
import qualified Control.Exception as Ex
import qualified Data.Map as Map
import qualified Data.Maybe as Maybe
import Control.Monad
instance Monad ArgParser where
return a = APTerminator a
(AP str fallback doc f) >>= g = AP str fallback doc (\a -> (f a) >>= g)
(APFailIf b errmsg child) >>= g = APFailIf b errmsg (child >>= g)
(APExample str child) >>= g = APExample str (child >>= g)
(APTest str tests child) >>= g = APTest str tests (child >>= g)
(APTerminator a) >>= g = g a
(APBranch bs) >>= g = APBranch $ map (>>= g) bs
instance MonadPlus ArgParser where
mzero = APFailIf True "" undefined
mplus (APBranch as) (APBranch bs) = APBranch ( as ++ bs )
mplus (APBranch as) b = APBranch ( as ++ [b] )
mplus a (APBranch bs) = APBranch ( [a] ++ bs )
mplus a b = APBranch [ a , b ]
argument :: forall desiredType. (OTypeMirror desiredType) => String -> ArgParser desiredType
argument name =
AP name Nothing "" $ \oObjVal -> do
let
val = fromOObj oObjVal :: Maybe desiredType
errmsg = case oObjVal of
OError errs -> "error in computing value for arugment " ++ name
++ ": " ++ concat errs
_ -> "arg " ++ show oObjVal ++ " not compatible with " ++ name
APFailIf (Maybe.isNothing val) errmsg $ APTerminator $ (\(Just a) -> a) val
doc (AP name defMaybeVal _ next) newDoc = AP name defMaybeVal newDoc next
defaultTo :: forall a. (OTypeMirror a) => ArgParser a -> a -> ArgParser a
defaultTo (AP name oldDefMaybeVal doc next) newDefVal =
AP name (Just $ toOObj newDefVal) doc next
example :: String -> ArgParser ()
example str = APExample str (return ())
test :: String -> ArgParser ()
test str = APTest str [] (return ())
eulerCharacteristic :: ArgParser a -> Int -> ArgParser a
eulerCharacteristic (APTest str tests child) χ =
APTest str ((EulerCharacteristic χ) : tests) child
argMap ::
[(Maybe String, OVal)]
-> ArgParser a
-> (Maybe a, [String])
argMap args = argMap2 unnamedArgs (Map.fromList namedArgs) where
unnamedArgs = map snd $ filter (Maybe.isNothing . fst) args
namedArgs = map (\(a,b) -> (Maybe.fromJust a, b)) $ filter (Maybe.isJust . fst) args
argMap2 :: [OVal] -> Map.Map String OVal -> ArgParser a -> (Maybe a, [String])
argMap2 uArgs nArgs (APBranch branches) =
foldl1 merge solutions where
solutions = map (argMap2 uArgs nArgs) branches
merge a@(Just _, []) _ = a
merge _ b@(Just _, []) = b
merge a@(Just _, _) _ = a
merge (Nothing, _) a = a
argMap2 unnamedArgs namedArgs (AP name fallback _ f) =
case Map.lookup name namedArgs of
Just a -> argMap2
unnamedArgs
(Map.delete name namedArgs)
(f a)
Nothing -> case unnamedArgs of
x:xs -> argMap2 xs namedArgs (f x)
[] -> case fallback of
Just b -> argMap2 [] namedArgs (f b)
Nothing -> (Nothing, ["No value and no default for argument " ++ name])
argMap2 a b (APTerminator val) =
(Just val,
if not (null a && Map.null b)
then ["unused arguments"]
else []
)
argMap2 a b (APFailIf test err child) =
if test
then (Nothing, [err])
else argMap2 a b child
argMap2 a b (APExample str child) = argMap2 a b child
argMap2 a b (APTest str tests child) = argMap2 a b child