| 1 |
(* main.sml |
| 2 |
* |
| 3 |
* COPYRIGHT (c) 2006 |
| 4 |
* John Reppy (http://www.cs.uchicago.edu/~jhr) |
| 5 |
* Aaron Turon (http://www.cs.uchicago.edu/~adrassi) |
| 6 |
* All rights reserved. |
| 7 |
* |
| 8 |
* The driver. |
| 9 |
*) |
| 10 |
|
| 11 |
|
| 12 |
structure Main : sig |
| 13 |
|
| 14 |
val main : (string * string list) -> OS.Process.status |
| 15 |
|
| 16 |
val load : string -> LLKSpec.grammar * GLA.gla |
| 17 |
val getNT : LLKSpec.grammar -> string -> LLKSpec.nonterm |
| 18 |
|
| 19 |
end = struct |
| 20 |
|
| 21 |
(* check a parse tree, returning a grammar *) |
| 22 |
fun checkPT parseTree = let |
| 23 |
val grm = CheckGrammar.check parseTree |
| 24 |
val LLKSpec.Grammar {nterms, prods, ...} = grm |
| 25 |
val _ = Err.debugs [" ", Int.toString (List.length nterms), |
| 26 |
" nonterminals"] |
| 27 |
val _ = Err.debugs [" ", Int.toString (List.length prods), |
| 28 |
" productions"] |
| 29 |
(* |
| 30 |
val _ = app (Err.debug o Prod.toString) prods |
| 31 |
*) |
| 32 |
in |
| 33 |
grm |
| 34 |
end |
| 35 |
|
| 36 |
fun process () = let |
| 37 |
val _ = Err.anyErrors := false |
| 38 |
val file = !Options.fname |
| 39 |
(* check that the input file exists *) |
| 40 |
val _ = if OS.FileSys.access(file, [OS.FileSys.A_READ]) |
| 41 |
then () |
| 42 |
else ( |
| 43 |
Err.errMsg [ |
| 44 |
"ml-antlr: file \"", String.toString file, |
| 45 |
"\" does not exist or is unreadable" |
| 46 |
]; |
| 47 |
raise Err.Abort) |
| 48 |
(* process the grammar *) |
| 49 |
val grm = checkPT (ParseFile.parse file) |
| 50 |
val gla = GLA.mkGLA grm |
| 51 |
val pm = ComputePredict.mkPM (grm, gla) |
| 52 |
val outspec = (grm, pm, file) |
| 53 |
in |
| 54 |
SMLOutput.output outspec; |
| 55 |
if !Options.dotOutput then GLA.dumpGraph (file, grm, gla) else (); |
| 56 |
if !Options.texOutput then LaTeXOutput.output outspec else (); |
| 57 |
if !Err.anyErrors |
| 58 |
then OS.Process.failure |
| 59 |
else OS.Process.success |
| 60 |
end |
| 61 |
handle Err.Abort => OS.Process.failure |
| 62 |
| ex => ( |
| 63 |
Err.errMsg [ |
| 64 |
"uncaught exception ", General.exnName ex, |
| 65 |
" [", exnMessage ex, "]" |
| 66 |
]; |
| 67 |
List.app (fn s => Err.errMsg [" raised at ", s]) (SMLofNJ.exnHistory ex); |
| 68 |
OS.Process.failure) |
| 69 |
|
| 70 |
fun main (_, args) = |
| 71 |
if Options.processArgs args |
| 72 |
then ( |
| 73 |
Err.errMsg [Options.usage]; |
| 74 |
OS.Process.failure) |
| 75 |
else process() |
| 76 |
|
| 77 |
(* these functions are for debugging in the interactive loop *) |
| 78 |
fun load file = let |
| 79 |
val grm = checkPT (ParseFile.parse file) |
| 80 |
val gla = GLA.mkGLA grm |
| 81 |
in |
| 82 |
(grm, gla) |
| 83 |
end |
| 84 |
|
| 85 |
fun getNT (LLKSpec.Grammar {nterms, ...}) name = |
| 86 |
hd (List.filter (fn nt => Nonterm.qualName nt = name) nterms) |
| 87 |
|
| 88 |
end |