mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-23 19:42:50 -06:00
made command table independent of options
This commit is contained in:
@@ -5,6 +5,7 @@ module GF.Command.Commands (
|
|||||||
isOpt,
|
isOpt,
|
||||||
options,
|
options,
|
||||||
flags,
|
flags,
|
||||||
|
CommandInfo,
|
||||||
CommandOutput
|
CommandOutput
|
||||||
) where
|
) where
|
||||||
|
|
||||||
@@ -22,7 +23,7 @@ import qualified Data.Map as Map
|
|||||||
type CommandOutput = ([Tree],String) ---- errors, etc
|
type CommandOutput = ([Tree],String) ---- errors, etc
|
||||||
|
|
||||||
data CommandInfo = CommandInfo {
|
data CommandInfo = CommandInfo {
|
||||||
exec :: [Tree] -> IO CommandOutput,
|
exec :: [Option] -> [Tree] -> IO CommandOutput,
|
||||||
synopsis :: String,
|
synopsis :: String,
|
||||||
explanation :: String,
|
explanation :: String,
|
||||||
longname :: String,
|
longname :: String,
|
||||||
@@ -32,7 +33,7 @@ data CommandInfo = CommandInfo {
|
|||||||
|
|
||||||
emptyCommandInfo :: CommandInfo
|
emptyCommandInfo :: CommandInfo
|
||||||
emptyCommandInfo = CommandInfo {
|
emptyCommandInfo = CommandInfo {
|
||||||
exec = \ts -> return (ts,[]), ----
|
exec = \_ ts -> return (ts,[]), ----
|
||||||
synopsis = "synopsis",
|
synopsis = "synopsis",
|
||||||
explanation = "explanation",
|
explanation = "explanation",
|
||||||
longname = "longname",
|
longname = "longname",
|
||||||
@@ -46,7 +47,7 @@ lookCommand = Map.lookup
|
|||||||
commandHelpAll :: MultiGrammar -> [Option] -> String
|
commandHelpAll :: MultiGrammar -> [Option] -> String
|
||||||
commandHelpAll mgr opts = unlines
|
commandHelpAll mgr opts = unlines
|
||||||
[commandHelp (isOpt "full" opts) (co,info)
|
[commandHelp (isOpt "full" opts) (co,info)
|
||||||
| (co,info) <- Map.assocs (allCommands mgr opts)]
|
| (co,info) <- Map.assocs (allCommands mgr)]
|
||||||
|
|
||||||
commandHelp :: Bool -> (String,CommandInfo) -> String
|
commandHelp :: Bool -> (String,CommandInfo) -> String
|
||||||
commandHelp full (co,info) = unlines $ [
|
commandHelp full (co,info) = unlines $ [
|
||||||
@@ -78,45 +79,45 @@ isOpt :: String -> [Option] -> Bool
|
|||||||
isOpt o opts = elem o [x | OOpt (Ident x) <- opts]
|
isOpt o opts = elem o [x | OOpt (Ident x) <- opts]
|
||||||
|
|
||||||
|
|
||||||
allCommands :: MultiGrammar -> [Option] -> Map.Map String CommandInfo
|
allCommands :: MultiGrammar -> Map.Map String CommandInfo
|
||||||
allCommands mgr opts = Map.fromAscList [
|
allCommands mgr = Map.fromAscList [
|
||||||
("gr", emptyCommandInfo {
|
("gr", emptyCommandInfo {
|
||||||
longname = "generate_random",
|
longname = "generate_random",
|
||||||
synopsis = "generates a list of random trees, by default one tree",
|
synopsis = "generates a list of random trees, by default one tree",
|
||||||
flags = ["number"],
|
flags = ["cat","number"],
|
||||||
exec = \_ -> do
|
exec = \opts _ -> do
|
||||||
ts <- generateRandom mgr optCat
|
ts <- generateRandom mgr (optCat opts)
|
||||||
return $ fromTrees $ take optNum ts
|
return $ fromTrees $ take (optNum opts) ts
|
||||||
}),
|
}),
|
||||||
("h", emptyCommandInfo {
|
("h", emptyCommandInfo {
|
||||||
longname = "help",
|
longname = "help",
|
||||||
synopsis = "get description of a command, or a the full list of commands",
|
synopsis = "get description of a command, or a the full list of commands",
|
||||||
options = ["full"],
|
options = ["full"],
|
||||||
exec = \ts -> return ([], case ts of
|
exec = \opts ts -> return ([], case ts of
|
||||||
[t] -> let co = (showTree t) in
|
[t] -> let co = (showTree t) in
|
||||||
case lookCommand co (allCommands mgr opts) of
|
case lookCommand co (allCommands mgr) of ---- new map ??!!
|
||||||
Just info -> commandHelp True (co,info)
|
Just info -> commandHelp True (co,info)
|
||||||
_ -> "command not found"
|
_ -> "command not found"
|
||||||
_ -> commandHelpAll mgr opts)
|
_ -> commandHelpAll mgr opts)
|
||||||
}),
|
}),
|
||||||
("l", emptyCommandInfo {
|
("l", emptyCommandInfo {
|
||||||
exec = return . fromStrings . map lin,
|
exec = \opts -> return . fromStrings . map (lin opts),
|
||||||
flags = ["lang"]
|
flags = ["lang"]
|
||||||
}),
|
}),
|
||||||
("p", emptyCommandInfo {
|
("p", emptyCommandInfo {
|
||||||
exec = return . fromTrees . concatMap par . toStrings,
|
exec = \opts -> return . fromTrees . concatMap (par opts). toStrings,
|
||||||
flags = ["cat","lang"]
|
flags = ["cat","lang"]
|
||||||
})
|
})
|
||||||
]
|
]
|
||||||
where
|
where
|
||||||
lin t = unlines [linearize mgr lang t | lang <- optLangs]
|
lin opts t = unlines [linearize mgr lang t | lang <- optLangs opts]
|
||||||
par s = concat [parse mgr lang optCat s | lang <- optLangs]
|
par opts s = concat [parse mgr lang (optCat opts) s | lang <- optLangs opts]
|
||||||
|
|
||||||
optLangs = case valIdOpts "lang" "" opts of
|
optLangs opts = case valIdOpts "lang" "" opts of
|
||||||
"" -> languages mgr
|
"" -> languages mgr
|
||||||
lang -> [lang]
|
lang -> [lang]
|
||||||
optCat = valIdOpts "cat" (lookAbsFlag gr (cid "startcat")) opts
|
optCat opts = valIdOpts "cat" (lookAbsFlag gr (cid "startcat")) opts
|
||||||
optNum = valIntOpts "number" 1 opts
|
optNum opts = valIntOpts "number" 1 opts
|
||||||
|
|
||||||
gr = gfcc mgr
|
gr = gfcc mgr
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
module GF.Command.Interpreter (
|
module GF.Command.Interpreter (
|
||||||
|
CommandEnv (..),
|
||||||
interpretCommandLine
|
interpretCommandLine
|
||||||
) where
|
) where
|
||||||
|
|
||||||
@@ -14,8 +15,13 @@ import GF.Command.ErrM ----
|
|||||||
|
|
||||||
import qualified Data.Map as Map
|
import qualified Data.Map as Map
|
||||||
|
|
||||||
interpretCommandLine :: MultiGrammar -> String -> IO ()
|
data CommandEnv = CommandEnv {
|
||||||
interpretCommandLine gr line = case (pCommandLine (myLexer line)) of
|
multigrammar :: MultiGrammar,
|
||||||
|
commands :: Map.Map String CommandInfo
|
||||||
|
}
|
||||||
|
|
||||||
|
interpretCommandLine :: CommandEnv -> String -> IO ()
|
||||||
|
interpretCommandLine env line = case (pCommandLine (myLexer line)) of
|
||||||
Ok CEmpty -> return ()
|
Ok CEmpty -> return ()
|
||||||
Ok (CLine pipes) -> mapM_ interPipe pipes
|
Ok (CLine pipes) -> mapM_ interPipe pipes
|
||||||
_ -> putStrLn "command not parsed"
|
_ -> putStrLn "command not parsed"
|
||||||
@@ -27,14 +33,14 @@ interpretCommandLine gr line = case (pCommandLine (myLexer line)) of
|
|||||||
intercs (trees,_) (c:cs) = do
|
intercs (trees,_) (c:cs) = do
|
||||||
treess2 <- interc trees c
|
treess2 <- interc trees c
|
||||||
intercs treess2 cs
|
intercs treess2 cs
|
||||||
interc = interpret gr
|
interc = interpret env
|
||||||
|
|
||||||
-- return the trees to be sent in pipe, and the output possibly printed
|
-- return the trees to be sent in pipe, and the output possibly printed
|
||||||
interpret :: MultiGrammar -> [Tree] -> Command -> IO CommandOutput
|
interpret :: CommandEnv -> [Tree] -> Command -> IO CommandOutput
|
||||||
interpret mgr trees0 comm = case lookCommand co commands of
|
interpret env trees0 comm = case lookCommand co comms of
|
||||||
Just info -> do
|
Just info -> do
|
||||||
checkOpts info
|
checkOpts info
|
||||||
tss@(_,s) <- exec info trees
|
tss@(_,s) <- exec info opts trees
|
||||||
optTrace s
|
optTrace s
|
||||||
return tss
|
return tss
|
||||||
_ -> do
|
_ -> do
|
||||||
@@ -43,7 +49,7 @@ interpret mgr trees0 comm = case lookCommand co commands of
|
|||||||
where
|
where
|
||||||
optTrace = if isOpt "tr" opts then putStrLn else const (return ())
|
optTrace = if isOpt "tr" opts then putStrLn else const (return ())
|
||||||
(co,opts,trees) = getCommand comm trees0
|
(co,opts,trees) = getCommand comm trees0
|
||||||
commands = allCommands mgr opts
|
comms = commands env
|
||||||
checkOpts info =
|
checkOpts info =
|
||||||
case
|
case
|
||||||
[o | OOpt (Ident o) <- opts, notElem o (options info)] ++
|
[o | OOpt (Ident o) <- opts, notElem o (options info)] ++
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
module Main where
|
module Main where
|
||||||
|
|
||||||
import GF.Command.Interpreter
|
import GF.Command.Interpreter
|
||||||
|
import GF.Command.Commands
|
||||||
import GF.GFCC.API
|
import GF.GFCC.API
|
||||||
import System (getArgs)
|
import System (getArgs)
|
||||||
import Data.Char (isDigit)
|
import Data.Char (isDigit)
|
||||||
@@ -11,15 +12,16 @@ main :: IO ()
|
|||||||
main = do
|
main = do
|
||||||
file:_ <- getArgs
|
file:_ <- getArgs
|
||||||
grammar <- file2grammar file
|
grammar <- file2grammar file
|
||||||
|
let env = CommandEnv grammar (allCommands grammar)
|
||||||
printHelp grammar
|
printHelp grammar
|
||||||
loop grammar
|
loop env
|
||||||
|
|
||||||
loop :: MultiGrammar -> IO ()
|
loop :: CommandEnv -> IO ()
|
||||||
loop grammar = do
|
loop env = do
|
||||||
s <- getLine
|
s <- getLine
|
||||||
if s == "q" then return () else do
|
if s == "q" then return () else do
|
||||||
interpretCommandLine grammar s
|
interpretCommandLine env s
|
||||||
loop grammar
|
loop env
|
||||||
|
|
||||||
printHelp grammar = do
|
printHelp grammar = do
|
||||||
putStrLn $ "languages: " ++ unwords (languages grammar)
|
putStrLn $ "languages: " ++ unwords (languages grammar)
|
||||||
|
|||||||
Reference in New Issue
Block a user