13 import System.CommandLine
15 from Text import class Text(concat), instance Text String
27 derive gPrint TokenValue
29 Start :: *World -> *World
31 # (args, w) = parseArgs w
34 # out = out <<< "Usage: " <<< args.program <<< " [opts] [FILENAME]\n"
35 <<< "\t--help Show this help\n"
36 <<< "\t--[no-]lex Lexer output(default: disabled)\n"
37 <<< "\t--[no-]parse Parser output(default: enabled)\n\n"
38 <<< "\tFILENAME File to parse, when unspecified stdin is parsed\n"
40 # (stdin, w) = stdio w
41 # (contents, stdin, w) = readFileOrStdin stdin args.fp w
43 (Left cs) = snd $ fclose (stdin <<< cs) w
46 # stdin = if (not args.lex) stdin (case lexOut of
47 (Left lex) = stdin <<< toString lex
48 (Right toks) = stdin <<<
49 concat (map (\(_, _, t)->printToString t +++ "\n") toks))
50 # parseOut = parser lexOut
51 # stdin = if (not args.parse) stdin (case parser lexOut of
52 (Left parse) = stdin <<< toString parse
53 (Right ast) = stdin <<< toString ast)
54 = snd $ fclose stdin w
56 parseArgs :: *World -> (Opts, *World)
58 # ([p:args], w) = getCommandLine w
59 = (pa args {program=p, lex=False, parse=True, fp=Nothing, help=False}, w)
61 pa :: [String] Opts -> Opts
63 pa ["--help":r] o = pa r {o & help=True}
64 pa ["--lex":r] o = pa r {o & lex=True}
65 pa ["--no-lex":r] o = pa r {o & lex=False}
66 pa ["--parse":r] o = pa r {o & parse=True}
67 pa ["--no-parse":r] o = pa r {o & parse=False}
68 pa [x:r] o = pa r {o & fp=Just x}
70 readFileOrStdin :: *File (Maybe String) *World -> *(Either String [Char], *File, *World)
71 readFileOrStdin stdin Nothing w
72 # (cs, stdin) = readEntireFile stdin
73 = (Right cs, stdin, w)
74 readFileOrStdin stdin (Just fp) w
75 # (b, fin, w) = fopen fp FReadText w
76 | not b = (Left "Unable to open file", stdin, w)
77 # (cs, fin) = readEntireFile fin
78 # (b, w) = fclose fin w
79 | not b = (Left "Unable to close file", stdin, w)
80 = (Right cs, stdin, w)
82 readEntireFile :: *File -> *([Char], *File)
84 # (b, c, f) = freadc f
86 # (cs, f) = readEntireFile f