| 1 |
(* err.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 |
* Global error handling, including printing of error messages |
| 9 |
* and a flag to trigger halting of the tool. |
| 10 |
*) |
| 11 |
|
| 12 |
structure Err = |
| 13 |
struct |
| 14 |
|
| 15 |
(* signal that the program should be aborted with no further messages printed *) |
| 16 |
exception Abort |
| 17 |
|
| 18 |
type pos = string * int * int |
| 19 |
(* type pos = StreamPos.pos *) |
| 20 |
type span = pos * pos |
| 21 |
|
| 22 |
(* |
| 23 |
val emptySpan = let val npos = 0 in (npos, npos) end |
| 24 |
*) |
| 25 |
val emptySpan = let val npos = ("",0,0) in (npos, npos) end |
| 26 |
|
| 27 |
(* global flag to record the existance of errors *) |
| 28 |
val anyErrors = ref false |
| 29 |
val leftRecurs : string list ref = ref [] |
| 30 |
|
| 31 |
fun abortIfErr() = if !anyErrors then raise Abort else () |
| 32 |
|
| 33 |
fun errMsg l = ( |
| 34 |
anyErrors := true; |
| 35 |
TextIO.output(TextIO.stdErr, String.concat l ^ "\n")) |
| 36 |
|
| 37 |
fun warning l = |
| 38 |
TextIO.output(TextIO.stdErr, String.concat l ^ "\n") |
| 39 |
|
| 40 |
local |
| 41 |
fun lc2str (l, c) = Int.toString l ^ "." ^ Int.toString c |
| 42 |
in |
| 43 |
|
| 44 |
fun pos2str (fname, l, c) = "[" ^ fname ^ ":" ^ lc2str (l, c) ^ "]" |
| 45 |
fun span2str ((fname, l1, c1), (_, l2, c2)) = |
| 46 |
if l1 = l2 andalso c1 = c2 |
| 47 |
then pos2str (fname, l1, c1) |
| 48 |
else |
| 49 |
"[" ^ fname ^ ":" ^ lc2str (l1, c1) ^ "-" ^ lc2str (l2, c2) ^ "]" |
| 50 |
(* |
| 51 |
fun pos2str _ = "" |
| 52 |
fun span2str _ = "" |
| 53 |
*) |
| 54 |
|
| 55 |
(* error function at a single position *) |
| 56 |
fun posErr (pos, msg) = errMsg (["Error ", pos2str pos, ": "]@msg) |
| 57 |
(* error function over a position span *) |
| 58 |
fun spanErr (span, msg) = errMsg (["Error ", span2str span, ": "]@msg) |
| 59 |
|
| 60 |
end |
| 61 |
|
| 62 |
(* left recursion detected *) |
| 63 |
fun leftRecur name = |
| 64 |
if List.exists (fn n => (n = name)) (!leftRecurs) |
| 65 |
then () |
| 66 |
else ( |
| 67 |
leftRecurs := name::(!leftRecurs); |
| 68 |
warning ["Left recursion detected: ", name, " -> ", name, " ..."]) |
| 69 |
|
| 70 |
val printDebug = ref true |
| 71 |
|
| 72 |
(* print a debugging message *) |
| 73 |
fun debug s = if !printDebug |
| 74 |
then (TextIO.output(TextIO.stdErr, s); TextIO.output(TextIO.stdErr, "\n")) |
| 75 |
else () |
| 76 |
fun debugs ss = debug (concat ss) |
| 77 |
|
| 78 |
(* print a status message *) |
| 79 |
fun status s = TextIO.output(TextIO.stdErr, concat ["[ml-antlr: ", s, "]\n"]) |
| 80 |
fun statuss ss = status (concat ss) |
| 81 |
|
| 82 |
end |