| author | wenzelm | 
| Sun, 14 Jun 2015 15:53:13 +0200 | |
| changeset 60469 | d1ea37df7358 | 
| parent 59698 | d4ce901f20c5 | 
| child 61558 | 68b86028e02a | 
| permissions | -rw-r--r-- | 
| 34136 | 1 | /* Title: Pure/General/exn.scala | 
| 45673 
cd41e3903fbf
separate compilation of PIDE vs. Pure sources, which enables independent Scala library;
 wenzelm parents: 
45667diff
changeset | 2 | Module: PIDE | 
| 34136 | 3 | Author: Makarius | 
| 4 | ||
| 43761 
e72ba84ae58f
tuned signature -- corresponding to Scala version;
 wenzelm parents: 
34313diff
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: 
59697diff
changeset | 16 |   {
 | 
| 
d4ce901f20c5
value-oriented user error, for well-defined Thy_Syntax.chop_common;
 wenzelm parents: 
59697diff
changeset | 17 | def user_error: Result[A] = | 
| 
d4ce901f20c5
value-oriented user error, for well-defined Thy_Syntax.chop_common;
 wenzelm parents: 
59697diff
changeset | 18 |       this match {
 | 
| 
d4ce901f20c5
value-oriented user error, for well-defined Thy_Syntax.chop_common;
 wenzelm parents: 
59697diff
changeset | 19 | case Exn(ERROR(msg)) => Exn(ERROR(msg)) | 
| 
d4ce901f20c5
value-oriented user error, for well-defined Thy_Syntax.chop_common;
 wenzelm parents: 
59697diff
changeset | 20 | case _ => this | 
| 
d4ce901f20c5
value-oriented user error, for well-defined Thy_Syntax.chop_common;
 wenzelm parents: 
59697diff
changeset | 21 | } | 
| 
d4ce901f20c5
value-oriented user error, for well-defined Thy_Syntax.chop_common;
 wenzelm parents: 
59697diff
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: 
34300diff
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: 
57831diff
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: 
57831diff
changeset | 41 | |
| 44158 | 42 | |
| 56667 
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
 wenzelm parents: 
52066diff
changeset | 43 | /* interrupts */ | 
| 
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
 wenzelm parents: 
52066diff
changeset | 44 | |
| 
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
 wenzelm parents: 
52066diff
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: 
52066diff
changeset | 55 | |
| 
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
 wenzelm parents: 
52066diff
changeset | 56 | object Interrupt | 
| 
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
 wenzelm parents: 
52066diff
changeset | 57 |   {
 | 
| 
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
 wenzelm parents: 
52066diff
changeset | 58 | def apply(): Throwable = new InterruptedException | 
| 
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
 wenzelm parents: 
52066diff
changeset | 59 | def unapply(exn: Throwable): Boolean = is_interrupt(exn) | 
| 
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
 wenzelm parents: 
52066diff
changeset | 60 | |
| 56861 | 61 |     def expose() { if (Thread.interrupted()) throw apply() }
 | 
| 62 |     def impose() { Thread.currentThread.interrupt }
 | |
| 56860 | 63 | |
| 56862 
e6f7ed54d64e
more robust process kill -- postpone interrupts on current thread;
 wenzelm parents: 
56861diff
changeset | 64 | def postpone[A](body: => A): Option[A] = | 
| 
e6f7ed54d64e
more robust process kill -- postpone interrupts on current thread;
 wenzelm parents: 
56861diff
changeset | 65 |     {
 | 
| 
e6f7ed54d64e
more robust process kill -- postpone interrupts on current thread;
 wenzelm parents: 
56861diff
changeset | 66 | val interrupted = Thread.interrupted | 
| 
e6f7ed54d64e
more robust process kill -- postpone interrupts on current thread;
 wenzelm parents: 
56861diff
changeset | 67 |       val result = capture { body }
 | 
| 
e6f7ed54d64e
more robust process kill -- postpone interrupts on current thread;
 wenzelm parents: 
56861diff
changeset | 68 | if (interrupted) impose() | 
| 
e6f7ed54d64e
more robust process kill -- postpone interrupts on current thread;
 wenzelm parents: 
56861diff
changeset | 69 |       result match {
 | 
| 
e6f7ed54d64e
more robust process kill -- postpone interrupts on current thread;
 wenzelm parents: 
56861diff
changeset | 70 | case Res(x) => Some(x) | 
| 
e6f7ed54d64e
more robust process kill -- postpone interrupts on current thread;
 wenzelm parents: 
56861diff
changeset | 71 | case Exn(e) => | 
| 
e6f7ed54d64e
more robust process kill -- postpone interrupts on current thread;
 wenzelm parents: 
56861diff
changeset | 72 |           if (is_interrupt(e)) { impose(); None }
 | 
| 
e6f7ed54d64e
more robust process kill -- postpone interrupts on current thread;
 wenzelm parents: 
56861diff
changeset | 73 | else throw e | 
| 
e6f7ed54d64e
more robust process kill -- postpone interrupts on current thread;
 wenzelm parents: 
56861diff
changeset | 74 | } | 
| 
e6f7ed54d64e
more robust process kill -- postpone interrupts on current thread;
 wenzelm parents: 
56861diff
changeset | 75 | } | 
| 
e6f7ed54d64e
more robust process kill -- postpone interrupts on current thread;
 wenzelm parents: 
56861diff
changeset | 76 | |
| 56667 
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
 wenzelm parents: 
52066diff
changeset | 77 | val return_code = 130 | 
| 
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
 wenzelm parents: 
52066diff
changeset | 78 | } | 
| 
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
 wenzelm parents: 
52066diff
changeset | 79 | |
| 
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
 wenzelm parents: 
52066diff
changeset | 80 | |
| 
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
 wenzelm parents: 
52066diff
changeset | 81 | /* POSIX return code */ | 
| 
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
 wenzelm parents: 
52066diff
changeset | 82 | |
| 
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
 wenzelm parents: 
52066diff
changeset | 83 | def return_code(exn: Throwable, rc: Int): Int = | 
| 
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
 wenzelm parents: 
52066diff
changeset | 84 | if (is_interrupt(exn)) Interrupt.return_code else rc | 
| 
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
 wenzelm parents: 
52066diff
changeset | 85 | |
| 
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
 wenzelm parents: 
52066diff
changeset | 86 | |
| 44158 | 87 | /* message */ | 
| 88 | ||
| 48479 
819f7a5f3e7f
more general notion of user ERROR (cf. 44f56fe01528);
 wenzelm parents: 
48476diff
changeset | 89 | def user_message(exn: Throwable): Option[String] = | 
| 59697 
43e14b0e2ef8
more explicit exception User_Error, with value-oriented equality;
 wenzelm parents: 
59138diff
changeset | 90 | if (exn.getClass == classOf[RuntimeException] || | 
| 
43e14b0e2ef8
more explicit exception User_Error, with value-oriented equality;
 wenzelm parents: 
59138diff
changeset | 91 | exn.getClass == classOf[Library.User_Error]) | 
| 
43e14b0e2ef8
more explicit exception User_Error, with value-oriented equality;
 wenzelm parents: 
59138diff
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: 
56862diff
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: 
59138diff
changeset | 96 | else if (exn.isInstanceOf[java.io.IOException]) | 
| 
43e14b0e2ef8
more explicit exception User_Error, with value-oriented equality;
 wenzelm parents: 
59138diff
changeset | 97 |     {
 | 
| 
43e14b0e2ef8
more explicit exception User_Error, with value-oriented equality;
 wenzelm parents: 
59138diff
changeset | 98 | val msg = exn.getMessage | 
| 
43e14b0e2ef8
more explicit exception User_Error, with value-oriented equality;
 wenzelm parents: 
59138diff
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: 
59138diff
changeset | 100 | } | 
| 48479 
819f7a5f3e7f
more general notion of user ERROR (cf. 44f56fe01528);
 wenzelm parents: 
48476diff
changeset | 101 | else if (exn.isInstanceOf[RuntimeException]) Some(exn.toString) | 
| 
819f7a5f3e7f
more general notion of user ERROR (cf. 44f56fe01528);
 wenzelm parents: 
48476diff
changeset | 102 | else None | 
| 
819f7a5f3e7f
more general notion of user ERROR (cf. 44f56fe01528);
 wenzelm parents: 
48476diff
changeset | 103 | |
| 
819f7a5f3e7f
more general notion of user ERROR (cf. 44f56fe01528);
 wenzelm parents: 
48476diff
changeset | 104 | def message(exn: Throwable): String = | 
| 56667 
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
 wenzelm parents: 
52066diff
changeset | 105 | user_message(exn) getOrElse (if (is_interrupt(exn)) "Interrupt" else exn.toString) | 
| 34136 | 106 | } | 
| 107 |