| 1 |
structure MDLError : MDL_ERROR = |
| 2 |
struct |
| 3 |
|
| 4 |
val loc = ref SourceMapping.dummyLoc |
| 5 |
val errorCount = ref 0 |
| 6 |
val warningCount = ref 0 |
| 7 |
|
| 8 |
fun init() = |
| 9 |
(errorCount := 0; warningCount := 0; loc := SourceMapping.dummyLoc) |
| 10 |
|
| 11 |
fun status() = |
| 12 |
let fun pr(0,s,t) = ("no "^s,t) |
| 13 |
| pr(1,s,t) = ("one "^s,t) |
| 14 |
| pr(n,s,t) = (Int.toString n^" "^s^"s","are") |
| 15 |
val (e, t) = pr(!errorCount,"error","is") |
| 16 |
val (w, t) = pr(!warningCount,"warning",t) |
| 17 |
in "There "^t^" "^e^" and "^w |
| 18 |
end |
| 19 |
|
| 20 |
val logFileName = ref "" |
| 21 |
val logFileStream = ref NONE : TextIO.outstream option ref |
| 22 |
|
| 23 |
fun closeLogFile() = |
| 24 |
case !logFileStream of |
| 25 |
SOME s => |
| 26 |
(TextIO.closeOut s; logFileStream := NONE; logFileName := "") |
| 27 |
| NONE => () |
| 28 |
fun openLogFile filename = |
| 29 |
(closeLogFile(); |
| 30 |
logFileName := filename; |
| 31 |
logFileStream := SOME(TextIO.openOut filename) |
| 32 |
) |
| 33 |
fun logfile() = !logFileName |
| 34 |
|
| 35 |
fun printToLog text = |
| 36 |
case !logFileStream of |
| 37 |
NONE => () |
| 38 |
| SOME s => TextIO.output(s,text) |
| 39 |
|
| 40 |
exception Error |
| 41 |
|
| 42 |
fun setLoc l = loc := l |
| 43 |
|
| 44 |
fun withLoc l f x = |
| 45 |
let val p = !loc |
| 46 |
(* val _ = print(SourceMapping.toString l^"\n") *) |
| 47 |
val _ = setLoc l |
| 48 |
val y = f x |
| 49 |
in setLoc p; |
| 50 |
y |
| 51 |
end |
| 52 |
|
| 53 |
fun log msg = |
| 54 |
let val text = msg^"\n" |
| 55 |
in TextIO.output(TextIO.stdErr,text); |
| 56 |
printToLog text |
| 57 |
end |
| 58 |
|
| 59 |
fun error msg = (log(SourceMapping.toString (!loc)^": "^msg); |
| 60 |
errorCount := !errorCount + 1) |
| 61 |
fun errorPos(l, msg) = (setLoc l; error msg) |
| 62 |
fun warning msg = (log(SourceMapping.toString (!loc)^": warning: "^msg); |
| 63 |
warningCount := !warningCount + 1) |
| 64 |
fun warningPos(l, msg) = (setLoc l; warning msg) |
| 65 |
fun fail msg = (error msg; raise Error) |
| 66 |
end |