File: error.sml

package info (click to toggle)
mlton 20210117%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 58,464 kB
  • sloc: ansic: 27,682; sh: 4,455; asm: 3,569; lisp: 2,879; makefile: 2,347; perl: 1,169; python: 191; pascal: 68; javascript: 7
file content (66 lines) | stat: -rw-r--r-- 1,823 bytes parent folder | download | duplicates (5)
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