source: proto/RE/Haskell/simplify.hs @ 4223

Last change on this file since 4223 was 3888, checked in by cameron, 5 years ago

removeNullablePrefix, removeNullableSuffix optimizations

File size: 1.7 KB
Line 
1--
2-- Regular Expression Simplifier
3
4module Main (main) where 
5
6import CanonicalRE
7import REparse
8import Nullable
9import Data.List
10
11import System.Environment
12import System.Console.GetOpt
13import System.IO (stdout,stderr,hPutStr,hPutStrLn)
14import System.Exit (ExitCode(..),exitWith)
15import Control.Monad
16--
17
18header = "Usage: simplify (-h | -v | -n regexp)"
19version = "simplify 0.2"
20
21data SimplifyFlags = Help | Version | Nullable deriving Eq 
22simplifyOptions :: [OptDescr SimplifyFlags]
23simplifyOptions =
24       [Option ['h'] ["help"]     (NoArg Help)        "Display help and exit.",
25        Option ['v'] ["version"]  (NoArg Version)     "Show the version number and exit.",
26        Option ['n'] ["nullable"] (NoArg Nullable)    "Remove nullable prefixes and suffixes."]
27
28usageMsg = usageInfo header simplifyOptions
29
30main = do
31  simplifyArgs <- getArgs
32  let (opts, args, errs) = getOpt Permute simplifyOptions simplifyArgs
33
34  when (not (null errs)) $ do
35       hPutStrLn stderr "Errors:"
36       mapM_ (hPutStrLn stderr) errs
37       exitWith (ExitFailure 1)
38  when (opts == [Version]) $ do
39       hPutStrLn stderr version
40       exitWith ExitSuccess
41  when (opts == [Help]) $ do
42       hPutStrLn stderr usageMsg
43       exitWith ExitSuccess
44  when (length(args) /= 1) $ do
45       hPutStrLn stderr usageMsg
46       exitWith (ExitFailure 1)
47  let [regexp] = args
48  case parseRE(regexp) of
49         (ParseSuccess r) -> do
50            if (opts == [Nullable]) then do
51                hPutStrLn stdout (show (simplify (removeNullableSuffix (removeNullablePrefix r))))
52            else do
53                hPutStrLn stdout (show (simplify r))
54            exitWith ExitSuccess
55         (ParseFailure m) -> do
56            hPutStrLn stderr ("Bad regexp: " ++ m)
57            exitWith (ExitFailure 1)
58
Note: See TracBrowser for help on using the repository browser.