module FST.Arguments ( parseInteractive,
InteractiveCommand(..),
isFST,
isDAT,
isNET,
isTHIS,
parseBatch,
inputB,
outputB,
isUpB
) where
import FST.GetOpt
data InteractiveCommand = BuildTransducer |
BuildNTransducer |
Minimize |
Determinize |
StdInReg String |
Load FilePath |
LUnion FilePath FilePath |
LProduct FilePath FilePath |
LStar FilePath |
LComposition FilePath FilePath |
Save FilePath |
ApplyDown |
ApplyUp |
ApplyD [String] |
ApplyU [String] |
ViewReg |
ViewInput |
ViewOutput |
ViewTransducer |
Help |
ClearMemory |
Quit |
NoCommand
parseInteractive :: [String] -> InteractiveCommand
parseInteractive ["b"] = BuildTransducer
parseInteractive ["bn"] = BuildNTransducer
parseInteractive ["m"] = Minimize
parseInteractive ["det"] = Determinize
parseInteractive ("r":xs) = StdInReg (unwords xs)
parseInteractive ["d"] = ApplyDown
parseInteractive ["u"] = ApplyUp
parseInteractive ("d":xs) = ApplyD xs
parseInteractive ("u":xs) = ApplyU xs
parseInteractive ["l",file] = Load file
parseInteractive ["l",file1,"|",file2] = LUnion file1 file2
parseInteractive ["l",file1," ",file2] = LProduct file1 file2
parseInteractive ["l",file, "*"] = LStar file
parseInteractive ["l",file1,".o.",file2] = LComposition file1 file2
parseInteractive ["s",file] = Save file
parseInteractive ["vt"] = ViewTransducer
parseInteractive ["vi"] = ViewInput
parseInteractive ["vo"] = ViewOutput
parseInteractive ["vr"] = ViewReg
parseInteractive ["h"] = Help
parseInteractive ["q"] = Quit
parseInteractive ["c"] = ClearMemory
parseInteractive _ = NoCommand
isFST :: String -> Bool
isFST str = case (reverse str) of
('t':'s':'f':'.':_) -> True
_ -> False
isDAT :: String -> Bool
isDAT str = case (reverse str) of
('t':'a':'d':'.':_) -> True
_ -> False
isNET :: String -> Bool
isNET str = case (reverse str) of
('t':'e':'n':'.':_) -> True
_ -> False
isTHIS :: String -> Bool
isTHIS = (== "*")
isApplyUp :: [String] -> Bool
isApplyUp = elem "-u"
data BatchCommand = DownB |
UpB |
InvalidCommand |
Input String |
Output String |
HelpB
deriving Show
batchOptions :: [OptDescr BatchCommand]
batchOptions = [Option ['u'] ["up"] (NoArg UpB) "apply the transducer up (default is down)",
Option ['d'] ["down"] (NoArg DownB) "apply the transducer down (default)",
Option ['i'] ["input"] (ReqArg Input "FILE") "read input from FILE",
Option ['o'] ["output"] (ReqArg Output "FILE") "write output to FILE"]
parseBatch :: [String] -> Either String (FilePath,[BatchCommand])
parseBatch cmdline = case getOpt Permute batchOptions cmdline of
(o,[file],[]) -> Right (file,o)
(_,_,errs) -> Left $ concat errs ++ usageInfo header batchOptions
where header = "Usage: fst [FILE.net or FILE.fst] [OPTIONS...]"
inputB :: [BatchCommand] -> Maybe FilePath
inputB [] = Nothing
inputB ((Input file):_) = return file
inputB (_:xs) = inputB xs
outputB :: [BatchCommand] -> Maybe FilePath
outputB [] = Nothing
outputB ((Output file):_) = return file
outputB (_:xs) = outputB xs
isUpB :: [BatchCommand] -> Bool
isUpB [] = False
isUpB (UpB:_) = True
isUpB (_:xs) = isUpB xs