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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
(*
Title: Install a pretty printer for the exn type
Author: David Matthews
Copyright David Matthews 2009
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*)
local
open PolyML
open RunCall
(* Print exception packet. Run-time system exceptions have
to be processed specially because the IDs don't have printer functions. *)
fun exnPrint depth _ exn =
let
val (exnId, exnName, exnArg, _) = unsafeCast exn
(* This parenthesis code is used in various places and probably should be centralised. *)
fun parenthesise(s as PrettyBlock(_, _, _, [ _ ])) = s
| parenthesise(s as PrettyBlock(_, _, _, (PrettyString("(")::_ ))) = s
| parenthesise(s as PrettyBlock(_, _, _, (PrettyString("{")::_ ))) = s
| parenthesise(s as PrettyBlock(_, _, _, (PrettyString("[")::_ ))) = s
| parenthesise(s as PrettyBlock _) =
PrettyBlock(3, true, [], [ PrettyString "(", s, PrettyString ")" ])
| parenthesise s = s (* String or Break *)
fun nullaryException s = PrettyString s
and parameterException(s, param) =
PrettyBlock(1, false, [],
[
PrettyString s,
PrettyBreak(1, 1),
parenthesise param
])
(* Use prettyRepresentation because this correctly quotes the string. *)
fun stringException(s, arg: string) =
parameterException(s, PolyML.prettyRepresentation(arg, depth-1))
in
if run_call1 RuntimeCalls.POLY_SYS_is_short exnId
then
case exn of
Conversion s => stringException(exnName, s)
| Fail s => stringException(exnName, s)
| Foreign s => stringException(exnName, s)
| Thread s => stringException(exnName, s)
| XWindows s => stringException(exnName, s)
| OS.SysErr param =>
parameterException("SysErr",
if depth <= 1 then PrettyString "..." else PolyML.prettyRepresentation(param, depth-1))
| _ => (* Anything else is nullary. *)
nullaryException exnName
else
(
(* Exceptions generated within ML contain a printer function. *)
case !exnId of
NONE => nullaryException exnName
| SOME printFn => parameterException(exnName, printFn(exnArg, depth-1))
)
end
in
val () = addPrettyPrinter exnPrint
end;
(* Print a ref. Because refs can form circular structures we include a check for a loop here. *)
local
open PolyML
(* If we have an expression as the argument we parenthesise it unless it is
a simple string, a tuple, a record or a list. *)
fun parenthesise(s as PrettyBlock(_, _, _, [ _ ])) = s
| parenthesise(s as PrettyBlock(_, _, _, (PrettyString("(")::_ ))) = s
| parenthesise(s as PrettyBlock(_, _, _, (PrettyString("{")::_ ))) = s
| parenthesise(s as PrettyBlock(_, _, _, (PrettyString("[")::_ ))) = s
| parenthesise(s as PrettyBlock _) =
PrettyBlock(3, true, [], [ PrettyString "(", s, PrettyString ")" ])
| parenthesise s = s (* String or Break *)
val printLimit: word ref list Universal.tag = Universal.tag()
fun print_ref depth doArg (r as ref x) =
if depth <= 0
then PrettyString "..."
else
let
(* We keep a list in thread-local storage of refs we're currently printing.
This is thread-local to avoid interference between different threads. *)
val currentRefs =
case Thread.Thread.getLocal printLimit of
NONE => []
| SOME limit => limit
val thisRef: word ref = RunCall.unsafeCast r
in
if List.exists(fn x => x = thisRef) currentRefs
then PrettyString "..." (* We've already seen this ref. *)
else
(
(* Add this to the list. *)
Thread.Thread.setLocal (printLimit, thisRef :: currentRefs);
(* Print it and reset the list*)
(PrettyBlock(3, false, [],
[ PrettyString "ref", PrettyBreak(1, 0), parenthesise(doArg(x, depth-1)) ]))
before (Thread.Thread.setLocal (printLimit, currentRefs))
) handle exn =>
(
(* Reset the list if there's been an exception. *)
Thread.Thread.setLocal (printLimit, currentRefs);
raise exn
)
end
in
val () = addPrettyPrinter print_ref
end;
|