summaryrefslogtreecommitdiff
path: root/src/compiler/GF/Command/CommandInfo.hs
diff options
context:
space:
mode:
authorhallgren <hallgren@chalmers.se>2015-08-10 13:01:02 +0000
committerhallgren <hallgren@chalmers.se>2015-08-10 13:01:02 +0000
commitd38efbaa6a2c94218bb65925bd9ad6c028dfbfd6 (patch)
tree6b6f5b8d34e9d5daa8d021d0d748d64cb993b188 /src/compiler/GF/Command/CommandInfo.hs
parent20644d02990f7510812fcb47d33ad20a27be48ab (diff)
Refactor GF shell modules to improve modularity and reusability
+ Move type CommandInfo from GF.Command.Commands to a new module GF.Commands.CommandInfo and make it independent of the PGF type. + Make the module GF.Command.Interpreter independent of the PGF type and eliminate the import of GF.Command.Commands. + Move the implementation of the "help" command to its own module GF.Command.Help
Diffstat (limited to 'src/compiler/GF/Command/CommandInfo.hs')
-rw-r--r--src/compiler/GF/Command/CommandInfo.hs57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/compiler/GF/Command/CommandInfo.hs b/src/compiler/GF/Command/CommandInfo.hs
new file mode 100644
index 000000000..bffb452ce
--- /dev/null
+++ b/src/compiler/GF/Command/CommandInfo.hs
@@ -0,0 +1,57 @@
+module GF.Command.CommandInfo where
+import GF.Command.Abstract(Option,Expr)
+import GF.Infra.SIO(SIO)
+import qualified PGF as H(showExpr)
+import qualified PGF.Internal as H(Literal(LStr),Expr(ELit)) ----
+import GF.Text.Pretty(Doc)
+
+data CommandInfo env = CommandInfo {
+ exec :: env -> [Option] -> [Expr] -> SIO CommandOutput,
+ synopsis :: String,
+ syntax :: String,
+ explanation :: String,
+ longname :: String,
+ options :: [(String,String)],
+ flags :: [(String,String)],
+ examples :: [(String,String)],
+ needsTypeCheck :: Bool
+ }
+
+emptyCommandInfo :: CommandInfo env
+emptyCommandInfo = CommandInfo {
+ exec = \_ _ ts -> return $ pipeExprs ts, ----
+ synopsis = "",
+ syntax = "",
+ explanation = "",
+ longname = "",
+ options = [],
+ flags = [],
+ examples = [],
+ needsTypeCheck = True
+ }
+--------------------------------------------------------------------------------
+
+class TypeCheckArg env where typeCheckArg :: env -> Expr -> Either Doc 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