mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-13 14:59:32 -06:00
+ Generalize the CommandInfo type by parameterizing it on the monad
instead of just the environment.
+ Generalize the commands defined in
GF.Command.{Commands,Commands2,CommonCommands,SourceCommands,HelpCommand}
to work in any monad that supports the needed operations.
+ Liberate GF.Command.Interpreter from the IO monad.
Also, move the current PGF from CommandEnv to GFEnv in
GF.Interactive, making the command interpreter even more generic.
+ Use a state monad to maintain the state of the interpreter in
GF.{Interactive,Interactive2}.
62 lines
1.9 KiB
Haskell
62 lines
1.9 KiB
Haskell
module GF.Command.CommandInfo where
|
|
import GF.Command.Abstract(Option,Expr)
|
|
import qualified PGF as H(showExpr)
|
|
import qualified PGF.Internal as H(Literal(LStr),Expr(ELit)) ----
|
|
|
|
data CommandInfo m = CommandInfo {
|
|
exec :: [Option] -> [Expr] -> m CommandOutput,
|
|
synopsis :: String,
|
|
syntax :: String,
|
|
explanation :: String,
|
|
longname :: String,
|
|
options :: [(String,String)],
|
|
flags :: [(String,String)],
|
|
examples :: [(String,String)],
|
|
needsTypeCheck :: Bool
|
|
}
|
|
|
|
mapCommandExec f c = c { exec = \ opts ts -> f (exec c opts ts) }
|
|
|
|
--emptyCommandInfo :: CommandInfo env
|
|
emptyCommandInfo = CommandInfo {
|
|
exec = error "command not implemented",
|
|
synopsis = "",
|
|
syntax = "",
|
|
explanation = "",
|
|
longname = "",
|
|
options = [],
|
|
flags = [],
|
|
examples = [],
|
|
needsTypeCheck = True
|
|
}
|
|
--------------------------------------------------------------------------------
|
|
|
|
class Monad m => TypeCheckArg m where typeCheckArg :: Expr -> m Expr
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
newtype CommandOutput = Piped {fromPipe :: ([Expr],String)} ---- errors, etc
|
|
|
|
-- ** Converting command output
|
|
fromStrings ss = Piped (map stringAsExpr ss, unlines ss)
|
|
fromExprs es = Piped (es,unlines (map (H.showExpr []) es))
|
|
fromString s = Piped ([stringAsExpr s], s)
|
|
pipeWithMessage es msg = Piped (es,msg)
|
|
pipeMessage msg = Piped ([],msg)
|
|
pipeExprs es = Piped (es,[]) -- only used in emptyCommandInfo
|
|
void = Piped ([],"")
|
|
|
|
stringAsExpr = H.ELit . H.LStr -- should be a pattern macro
|
|
|
|
-- ** Converting command input
|
|
toString = unwords . toStrings
|
|
toStrings = map showAsString
|
|
where
|
|
showAsString t = case t of
|
|
H.ELit (H.LStr s) -> s
|
|
_ -> "\n" ++ H.showExpr [] t ---newline needed in other cases than the first
|
|
|
|
-- ** Creating documentation
|
|
|
|
mkEx s = let (command,expl) = break (=="--") (words s) in (unwords command, unwords (drop 1 expl))
|