11 import System.CommandLine
23 :: *StartType :== (LexerOutput, ParserOutput, *World)
25 Start :: *World -> *StartType
27 # (args, w) = parseArgs w
30 # out = out <<< "\nUsage: " <<< args.program <<< " [opts] [FILENAME]\n"
31 <<< "\t--help Show this help\n"
32 <<< "\t--lex Lex only, is mutually exclusive with --parse\n"
33 <<< "\t--parse Lex & Parse only\n\n"
34 <<< "\tFILENAME File to parse, when unspecified stdin is parsed\n"
35 # (_, w) = fclose out w
36 = (Left "", Left "", w)
37 # (stdin, w) = stdio w
38 # (contents, stdin, w) = readFileOrStdin stdin args.fp w
39 | args.lex = case contents of
40 (Right cs) = (lexer cs, Left "Parsing Disabled", w)
41 (Left e) = (Left e, Left "Parsing disabled", w)
43 (Left e) = (Left e, Left "", w)
44 (Right cs) = let lexOut = lexer cs in (lexOut, parser lexOut, w)
46 parseArgs :: *World -> (Opts, *World)
48 # ([p:args], w) = getCommandLine w
49 = (pa args {program=p, lex=False, parse=False, fp=Nothing, help=False}, w)
51 pa :: [String] -> (Opts -> Opts)
53 pa ["--help":r] = \o.pa r {o & help=True}
54 pa ["--lex":r] = \o.pa r {o & lex=True, parse=False}
55 pa ["--parse":r] = \o.pa r {o & lex=False, parse=True}
56 pa [x:r] = \o.pa r {o & fp=Just x}
58 readFileOrStdin :: *File (Maybe String) *World -> *(Either String [Char], *File, *World)
59 readFileOrStdin stdin Nothing w
60 # (cs, stdin) = readEntireFile stdin
61 = (Right cs, stdin, w)
62 readFileOrStdin stdin (Just fp) w
63 # (b, fin, w) = fopen fp FReadText w
64 | not b = (Left "Unable to open file", stdin, w)
65 # (cs, fin) = readEntireFile fin
66 # (b, w) = fclose fin w
67 | not b = (Left "Unable to close file", stdin, w)
68 = (Right cs, stdin, w)
70 readEntireFile :: *File -> *([Char], *File)
72 # (b, c, f) = freadc f
74 # (cs, f) = readEntireFile f