From d4d7e4faa62bf32ca99e7fe2277bea905cda508d Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Sun, 4 Sep 2022 08:50:55 +0100 Subject: Parse only flag --- src/Main.hs | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'src/Main.hs') diff --git a/src/Main.hs b/src/Main.hs index 8959329..6e57ba4 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -17,23 +17,30 @@ import System.IO (hPutStr, hPutStrLn, stderr, stdout) version = "0.1.0" data Options = Options - { optHelp :: Bool, + { optParse :: Bool, + optHelp :: Bool, optVersion :: Bool } defOptions = Options - { optHelp = False, + { optParse = False, + optHelp = False, optVersion = False } options :: [OptDescr (Options -> Options)] options = [ Option + ['p'] + ["parse"] + (NoArg (\opts -> opts {optParse = True})) + "only parse, reporting any errors", + Option ['h'] ["help"] (NoArg (\opts -> opts {optHelp = True})) - "show help and exit", + "show help", Option ['v'] ["version"] @@ -57,15 +64,15 @@ usage progName errs helpText = usageInfo header options -compileFile :: String -> IO () -compileFile filename = do +compileFile :: String -> Bool -> IO () +compileFile filename onlyParse = do res <- parseFromFile (scan parse) filename case res of Left err -> hPutStrLn stderr (showParserError err) >> exitFailure Right ast -> do res <- return $ evalState (compileAll ast) startState case res of - Right _ -> print ast + Right _ -> if onlyParse then exitSuccess else print ast Left errs -> hPutStr stderr (showErrorList errs) >> exitFailure main :: IO () @@ -78,7 +85,7 @@ main = do when (optHelp opts) $ usage progName [] when (optVersion opts) $ putStrLn (progName ++ " " ++ version) >> exitSuccess case n of - [filename] -> compileFile filename + [filename] -> compileFile filename $ optParse opts _ -> usage progName [] where opts = foldl (flip id) defOptions o -- cgit v1.2.3