| 1 |
(* main.sml |
| 2 |
* |
| 3 |
* COPYRIGHT (c) 2005 |
| 4 |
* John Reppy (http://www.cs.uchicago.edu/~jhr) |
| 5 |
* Aaron Turon ([email protected]) |
| 6 |
* All rights reserved. |
| 7 |
* |
| 8 |
* Driver for ml-ulex. |
| 9 |
*) |
| 10 |
|
| 11 |
structure Main = |
| 12 |
struct |
| 13 |
|
| 14 |
structure RE = RegExp |
| 15 |
structure Lex = LexGen |
| 16 |
structure LO = LexOutputSpec |
| 17 |
|
| 18 |
(* print debug messages, etc to stdErr *) |
| 19 |
fun errPrint msg = TextIO.output (TextIO.stdErr, String.concat msg) |
| 20 |
|
| 21 |
val name = "ml-ulex" |
| 22 |
|
| 23 |
(* count the total number of DFA states *) |
| 24 |
fun numStates (LO.Spec{dfa, ...}) = List.length dfa |
| 25 |
|
| 26 |
fun status s = errPrint ["[", name, ": ", s, "]\n"] |
| 27 |
|
| 28 |
fun frontEnd () = let |
| 29 |
val fname = !Options.fname |
| 30 |
val _ = if (String.size fname = 0) |
| 31 |
then ( |
| 32 |
errPrint [ |
| 33 |
"No input file specified\n usage: ", |
| 34 |
name, " ", Options.args, " file\n" |
| 35 |
]; |
| 36 |
OS.Process.exit OS.Process.failure) |
| 37 |
else () |
| 38 |
val _ = status "parsing" |
| 39 |
in |
| 40 |
if !Options.lexCompat |
| 41 |
then MLLexInput.parseFile fname |
| 42 |
else MLULexInput.parseFile fname |
| 43 |
end |
| 44 |
|
| 45 |
fun compile inSpec = let |
| 46 |
val inSpec = if (!Options.beTest) |
| 47 |
then LexSpec.emptyActions inSpec |
| 48 |
else inSpec |
| 49 |
val _ = status "DFA gen" |
| 50 |
val outSpec = Lex.gen inSpec |
| 51 |
val _ = errPrint [" ", Int.toString (numStates outSpec), " states in full DFA\n"] |
| 52 |
val _ = if !Options.dump |
| 53 |
then ( |
| 54 |
status "DFA dump"; |
| 55 |
DumpOutput.output (outSpec, !Options.fname)) |
| 56 |
else () |
| 57 |
in |
| 58 |
outSpec |
| 59 |
end |
| 60 |
|
| 61 |
fun backEnd outSpec = ( |
| 62 |
if !Options.dot |
| 63 |
then ( |
| 64 |
status "DOT gen"; |
| 65 |
DotOutput.output (outSpec, !Options.fname)) |
| 66 |
else (); |
| 67 |
status "SML gen"; |
| 68 |
if (numStates outSpec > 150 andalso !Options.beMode = Options.BySize) |
| 69 |
orelse !Options.beMode = Options.TableBased |
| 70 |
then SMLTblOutput.output (outSpec, !Options.fname) |
| 71 |
else SMLFunOutput.output (outSpec, !Options.fname); |
| 72 |
if !Options.match |
| 73 |
then ( |
| 74 |
errPrint ["-- Interactive matching (blank line to quit) --\n"]; |
| 75 |
Match.output (outSpec, !Options.fname)) |
| 76 |
else ()) |
| 77 |
|
| 78 |
fun mlULex () = (case frontEnd () |
| 79 |
of SOME inSpec => (backEnd (compile inSpec); OS.Process.success) |
| 80 |
| NONE => OS.Process.failure |
| 81 |
(* end case *)) |
| 82 |
|
| 83 |
fun main (_, args) = let |
| 84 |
val _ = List.app Options.procArg args |
| 85 |
in |
| 86 |
mlULex() |
| 87 |
end |
| 88 |
handle Options.Usage msg => ( |
| 89 |
TextIO.output(TextIO.stdErr, concat[ |
| 90 |
"** ", msg, "\nusage: ml-ulex ", Options.args, "\n" |
| 91 |
]); |
| 92 |
OS.Process.exit OS.Process.failure) |
| 93 |
| ex => ( |
| 94 |
TextIO.output(TextIO.stdErr, concat[ |
| 95 |
"** uncaught exception ", General.exnName ex, |
| 96 |
" [", General.exnMessage ex, "]\n" |
| 97 |
]); |
| 98 |
app (fn s => TextIO.output(TextIO.stdErr, concat[ |
| 99 |
" raised at ", s, "\n" |
| 100 |
])) |
| 101 |
(SMLofNJ.exnHistory ex); |
| 102 |
OS.Process.exit OS.Process.failure) |
| 103 |
|
| 104 |
end |