author | Manuel Eberl <eberlm@in.tum.de> |
Tue, 19 Jan 2016 11:19:25 +0100 | |
changeset 62201 | eca7b38c8ee5 |
parent 61925 | ab52f183f020 |
child 62492 | 0e53fade87fe |
permissions | -rw-r--r-- |
61925 | 1 |
/* Title: Pure/RAW/exn.scala |
45673
cd41e3903fbf
separate compilation of PIDE vs. Pure sources, which enables independent Scala library;
wenzelm
parents:
45667
diff
changeset
|
2 |
Module: PIDE |
34136 | 3 |
Author: Makarius |
4 |
||
43761
e72ba84ae58f
tuned signature -- corresponding to Scala version;
wenzelm
parents:
34313
diff
changeset
|
5 |
Support for exceptions (arbitrary throwables). |
34136 | 6 |
*/ |
7 |
||
8 |
package isabelle |
|
9 |
||
10 |
||
11 |
object Exn |
|
12 |
{ |
|
44158 | 13 |
/* exceptions as values */ |
34136 | 14 |
|
15 |
sealed abstract class Result[A] |
|
59698
d4ce901f20c5
value-oriented user error, for well-defined Thy_Syntax.chop_common;
wenzelm
parents:
59697
diff
changeset
|
16 |
{ |
d4ce901f20c5
value-oriented user error, for well-defined Thy_Syntax.chop_common;
wenzelm
parents:
59697
diff
changeset
|
17 |
def user_error: Result[A] = |
d4ce901f20c5
value-oriented user error, for well-defined Thy_Syntax.chop_common;
wenzelm
parents:
59697
diff
changeset
|
18 |
this match { |
d4ce901f20c5
value-oriented user error, for well-defined Thy_Syntax.chop_common;
wenzelm
parents:
59697
diff
changeset
|
19 |
case Exn(ERROR(msg)) => Exn(ERROR(msg)) |
d4ce901f20c5
value-oriented user error, for well-defined Thy_Syntax.chop_common;
wenzelm
parents:
59697
diff
changeset
|
20 |
case _ => this |
d4ce901f20c5
value-oriented user error, for well-defined Thy_Syntax.chop_common;
wenzelm
parents:
59697
diff
changeset
|
21 |
} |
d4ce901f20c5
value-oriented user error, for well-defined Thy_Syntax.chop_common;
wenzelm
parents:
59697
diff
changeset
|
22 |
} |
43762 | 23 |
case class Res[A](res: A) extends Result[A] |
24 |
case class Exn[A](exn: Throwable) extends Result[A] |
|
34136 | 25 |
|
26 |
def capture[A](e: => A): Result[A] = |
|
27 |
try { Res(e) } |
|
34313
2f890016afab
treat *all* JVM throwables as "exceptions", cf. ML version;
wenzelm
parents:
34300
diff
changeset
|
28 |
catch { case exn: Throwable => Exn[A](exn) } |
34136 | 29 |
|
30 |
def release[A](result: Result[A]): A = |
|
31 |
result match { |
|
32 |
case Res(x) => x |
|
33 |
case Exn(exn) => throw exn |
|
34 |
} |
|
44158 | 35 |
|
59136
c2b23cb8a677
added Par_List in Scala, in accordance to ML version;
wenzelm
parents:
57831
diff
changeset
|
36 |
def release_first[A](results: List[Result[A]]): List[A] = |
59138 | 37 |
results.find({ case Exn(exn) => !is_interrupt(exn) case _ => false }) match { |
38 |
case Some(Exn(exn)) => throw exn |
|
39 |
case _ => results.map(release(_)) |
|
40 |
} |
|
59136
c2b23cb8a677
added Par_List in Scala, in accordance to ML version;
wenzelm
parents:
57831
diff
changeset
|
41 |
|
44158 | 42 |
|
56667
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
52066
diff
changeset
|
43 |
/* interrupts */ |
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
52066
diff
changeset
|
44 |
|
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
52066
diff
changeset
|
45 |
def is_interrupt(exn: Throwable): Boolean = |
56670 | 46 |
{ |
47 |
var found_interrupt = false |
|
48 |
var e = exn |
|
49 |
while (!found_interrupt && e != null) { |
|
50 |
found_interrupt |= e.isInstanceOf[InterruptedException] |
|
51 |
e = e.getCause |
|
52 |
} |
|
53 |
found_interrupt |
|
54 |
} |
|
56667
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
52066
diff
changeset
|
55 |
|
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
52066
diff
changeset
|
56 |
object Interrupt |
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
52066
diff
changeset
|
57 |
{ |
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
52066
diff
changeset
|
58 |
def apply(): Throwable = new InterruptedException |
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
52066
diff
changeset
|
59 |
def unapply(exn: Throwable): Boolean = is_interrupt(exn) |
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
52066
diff
changeset
|
60 |
|
61558 | 61 |
def expose() { if (Thread.interrupted) throw apply() } |
56861 | 62 |
def impose() { Thread.currentThread.interrupt } |
56860 | 63 |
|
56862
e6f7ed54d64e
more robust process kill -- postpone interrupts on current thread;
wenzelm
parents:
56861
diff
changeset
|
64 |
def postpone[A](body: => A): Option[A] = |
e6f7ed54d64e
more robust process kill -- postpone interrupts on current thread;
wenzelm
parents:
56861
diff
changeset
|
65 |
{ |
e6f7ed54d64e
more robust process kill -- postpone interrupts on current thread;
wenzelm
parents:
56861
diff
changeset
|
66 |
val interrupted = Thread.interrupted |
e6f7ed54d64e
more robust process kill -- postpone interrupts on current thread;
wenzelm
parents:
56861
diff
changeset
|
67 |
val result = capture { body } |
e6f7ed54d64e
more robust process kill -- postpone interrupts on current thread;
wenzelm
parents:
56861
diff
changeset
|
68 |
if (interrupted) impose() |
e6f7ed54d64e
more robust process kill -- postpone interrupts on current thread;
wenzelm
parents:
56861
diff
changeset
|
69 |
result match { |
e6f7ed54d64e
more robust process kill -- postpone interrupts on current thread;
wenzelm
parents:
56861
diff
changeset
|
70 |
case Res(x) => Some(x) |
e6f7ed54d64e
more robust process kill -- postpone interrupts on current thread;
wenzelm
parents:
56861
diff
changeset
|
71 |
case Exn(e) => |
e6f7ed54d64e
more robust process kill -- postpone interrupts on current thread;
wenzelm
parents:
56861
diff
changeset
|
72 |
if (is_interrupt(e)) { impose(); None } |
e6f7ed54d64e
more robust process kill -- postpone interrupts on current thread;
wenzelm
parents:
56861
diff
changeset
|
73 |
else throw e |
e6f7ed54d64e
more robust process kill -- postpone interrupts on current thread;
wenzelm
parents:
56861
diff
changeset
|
74 |
} |
e6f7ed54d64e
more robust process kill -- postpone interrupts on current thread;
wenzelm
parents:
56861
diff
changeset
|
75 |
} |
e6f7ed54d64e
more robust process kill -- postpone interrupts on current thread;
wenzelm
parents:
56861
diff
changeset
|
76 |
|
56667
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
52066
diff
changeset
|
77 |
val return_code = 130 |
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
52066
diff
changeset
|
78 |
} |
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
52066
diff
changeset
|
79 |
|
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
52066
diff
changeset
|
80 |
|
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
52066
diff
changeset
|
81 |
/* POSIX return code */ |
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
52066
diff
changeset
|
82 |
|
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
52066
diff
changeset
|
83 |
def return_code(exn: Throwable, rc: Int): Int = |
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
52066
diff
changeset
|
84 |
if (is_interrupt(exn)) Interrupt.return_code else rc |
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
52066
diff
changeset
|
85 |
|
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
52066
diff
changeset
|
86 |
|
44158 | 87 |
/* message */ |
88 |
||
48479
819f7a5f3e7f
more general notion of user ERROR (cf. 44f56fe01528);
wenzelm
parents:
48476
diff
changeset
|
89 |
def user_message(exn: Throwable): Option[String] = |
59697
43e14b0e2ef8
more explicit exception User_Error, with value-oriented equality;
wenzelm
parents:
59138
diff
changeset
|
90 |
if (exn.getClass == classOf[RuntimeException] || |
43e14b0e2ef8
more explicit exception User_Error, with value-oriented equality;
wenzelm
parents:
59138
diff
changeset
|
91 |
exn.getClass == classOf[Library.User_Error]) |
43e14b0e2ef8
more explicit exception User_Error, with value-oriented equality;
wenzelm
parents:
59138
diff
changeset
|
92 |
{ |
44158 | 93 |
val msg = exn.getMessage |
57831
885888a880fb
more general notion of "user error" including empty message -- NB: Output.error_message needs non-empty string to emit anything;
wenzelm
parents:
56862
diff
changeset
|
94 |
Some(if (msg == null || msg == "") "Error" else msg) |
44158 | 95 |
} |
59697
43e14b0e2ef8
more explicit exception User_Error, with value-oriented equality;
wenzelm
parents:
59138
diff
changeset
|
96 |
else if (exn.isInstanceOf[java.io.IOException]) |
43e14b0e2ef8
more explicit exception User_Error, with value-oriented equality;
wenzelm
parents:
59138
diff
changeset
|
97 |
{ |
43e14b0e2ef8
more explicit exception User_Error, with value-oriented equality;
wenzelm
parents:
59138
diff
changeset
|
98 |
val msg = exn.getMessage |
43e14b0e2ef8
more explicit exception User_Error, with value-oriented equality;
wenzelm
parents:
59138
diff
changeset
|
99 |
Some(if (msg == null || msg == "") "I/O error" else "I/O error: " + msg) |
43e14b0e2ef8
more explicit exception User_Error, with value-oriented equality;
wenzelm
parents:
59138
diff
changeset
|
100 |
} |
48479
819f7a5f3e7f
more general notion of user ERROR (cf. 44f56fe01528);
wenzelm
parents:
48476
diff
changeset
|
101 |
else if (exn.isInstanceOf[RuntimeException]) Some(exn.toString) |
819f7a5f3e7f
more general notion of user ERROR (cf. 44f56fe01528);
wenzelm
parents:
48476
diff
changeset
|
102 |
else None |
819f7a5f3e7f
more general notion of user ERROR (cf. 44f56fe01528);
wenzelm
parents:
48476
diff
changeset
|
103 |
|
819f7a5f3e7f
more general notion of user ERROR (cf. 44f56fe01528);
wenzelm
parents:
48476
diff
changeset
|
104 |
def message(exn: Throwable): String = |
56667
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
52066
diff
changeset
|
105 |
user_message(exn) getOrElse (if (is_interrupt(exn)) "Interrupt" else exn.toString) |
34136 | 106 |
} |