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