13 import System.CommandLine
15 from Text import class Text(concat), instance Text String
20 from yard import :: Error, instance toString Error
30 derive gPrint TokenValue
32 Start :: *World -> *World
34 # (args, w) = parseArgs w
35 # (stdin, w) = stdio w
38 <<< "spl 0.1 (17 march 2016)\n"
39 <<< "Copyright Pim Jager and Mart Lubbers\n"
40 = snd $ fclose stdin w
43 <<< "Usage: " <<< args.program <<< " [OPTION] [FILE]\n"
44 <<< "<spl> ::= <spl> <parser> <lexer>\n"
45 <<< "Lex parse and either FILE or stdin\n"
48 <<< " --help Show this help\n"
49 <<< " --version Show the version\n"
50 <<< " --[no-]lex Lexer output(default: disabled)\n"
51 <<< " --[no-]parse Parser output(default: enabled)\n"
52 = snd $ fclose stdin w
53 # (contents, stdin, w) = readFileOrStdin stdin args.fp w
55 (Left cs) = snd $ fclose (stdin <<< cs) w
58 # stdin = if (not args.lex) stdin (case lexOut of
60 stdin <<< "//LEXER\n" <<< printTokens toks <<< "//LEXER\n"
62 # parseOut = parser lexOut
63 # stdin = if (not args.parse) stdin (case parser lexOut of
65 stdin <<< "//PARSER\n" <<< toString ast <<< "//PARSER\n"
66 (Left parse) = stdin <<< toString parse)
67 = snd $ fclose stdin w
69 printTokens :: [Token] -> String
70 printTokens ts = concat $ flatten $ map pt ts
72 pt ({line,col},token) = [toString line, ":",
73 toString col, ": ", printToString token, "\n"]
75 parseArgs :: *World -> (Opts, *World)
77 # ([p:args], w) = getCommandLine w
86 pa :: [String] Opts -> Opts
88 pa ["--help":r] o = pa r {o & help=True}
89 pa ["--version":r] o = pa r {o & version=True}
90 pa ["--lex":r] o = pa r {o & lex=True}
91 pa ["--no-lex":r] o = pa r {o & lex=False}
92 pa ["--parse":r] o = pa r {o & parse=True}
93 pa ["--no-parse":r] o = pa r {o & parse=False}
94 pa [x:r] o = pa r {o & fp=Just x}
96 readFileOrStdin :: *File (Maybe String) *World -> *(Either String [Char], *File, *World)
97 readFileOrStdin stdin Nothing w
98 # (cs, stdin) = readEntireFile stdin
99 = (Right cs, stdin, w)
100 readFileOrStdin stdin (Just fp) w
101 # (b, fin, w) = fopen fp FReadText w
102 | not b = (Left "Unable to open file", stdin, w)
103 # (cs, fin) = readEntireFile fin
104 # (b, w) = fclose fin w
105 | not b = (Left "Unable to close file", stdin, w)
106 = (Right cs, stdin, w)
108 readEntireFile :: *File -> *([Char], *File)
110 # (b, c, f) = freadc f
112 # (cs, f) = readEntireFile f