author | wenzelm |
Thu, 07 Jul 2011 23:55:15 +0200 | |
changeset 43698 | 91c4d7397f0e |
parent 43670 | 7f933761764b |
child 43750 | 390dbda4f3d7 |
permissions | -rw-r--r-- |
34136 | 1 |
/* Title: Pure/library.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Basic library. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle |
|
8 |
||
38258 | 9 |
|
38636
b7647ca7de5a
module for simplified thread operations (Scala version);
wenzelm
parents:
38583
diff
changeset
|
10 |
import java.lang.System |
34216 | 11 |
import java.awt.Component |
12 |
import javax.swing.JOptionPane |
|
34136 | 13 |
|
37018 | 14 |
import scala.swing.ComboBox |
15 |
import scala.swing.event.SelectionChanged |
|
43598 | 16 |
import scala.collection.mutable |
37018 | 17 |
|
18 |
||
34136 | 19 |
object Library |
20 |
{ |
|
43652 | 21 |
/* user errors */ |
22 |
||
23 |
object ERROR |
|
24 |
{ |
|
25 |
def apply(message: String): Throwable = new RuntimeException(message) |
|
26 |
def unapply(exn: Throwable): Option[String] = |
|
27 |
exn match { |
|
28 |
case e: RuntimeException => |
|
29 |
val msg = e.getMessage |
|
30 |
Some(if (msg == null) "" else msg) |
|
31 |
case _ => None |
|
32 |
} |
|
33 |
} |
|
34 |
||
35 |
def error(message: String): Nothing = throw ERROR(message) |
|
36 |
||
37 |
def cat_error(msg1: String, msg2: String): Nothing = |
|
38 |
if (msg1 == "") error(msg1) |
|
39 |
else error(msg1 + "\n" + msg2) |
|
40 |
||
41 |
||
43598 | 42 |
/* lists */ |
36688 | 43 |
|
44 |
def separate[A](s: A, list: List[A]): List[A] = |
|
45 |
list match { |
|
46 |
case x :: xs if !xs.isEmpty => x :: s :: separate(s, xs) |
|
47 |
case _ => list |
|
48 |
} |
|
49 |
||
43598 | 50 |
def space_explode(sep: Char, str: String): List[String] = |
51 |
if (str.isEmpty) Nil |
|
52 |
else { |
|
53 |
val result = new mutable.ListBuffer[String] |
|
54 |
var start = 0 |
|
55 |
var finished = false |
|
56 |
while (!finished) { |
|
57 |
val i = str.indexOf(sep, start) |
|
58 |
if (i == -1) { result += str.substring(start); finished = true } |
|
59 |
else { result += str.substring(start, i); start = i + 1 } |
|
60 |
} |
|
61 |
result.toList |
|
62 |
} |
|
63 |
||
43670
7f933761764b
prefer space_explode/split_lines as in Isabelle/ML;
wenzelm
parents:
43652
diff
changeset
|
64 |
def split_lines(str: String): List[String] = space_explode('\n', str) |
7f933761764b
prefer space_explode/split_lines as in Isabelle/ML;
wenzelm
parents:
43652
diff
changeset
|
65 |
|
43598 | 66 |
|
43652 | 67 |
/* iterate over chunks (cf. space_explode) */ |
68 |
||
69 |
def chunks(source: CharSequence, sep: Char = '\n') = new Iterator[CharSequence] |
|
70 |
{ |
|
71 |
private val end = source.length |
|
72 |
private def next_chunk(i: Int): Option[(CharSequence, Int)] = |
|
73 |
{ |
|
74 |
if (i < end) { |
|
75 |
var j = i; do j += 1 while (j < end && source.charAt(j) != sep) |
|
76 |
Some((source.subSequence(i + 1, j), j)) |
|
77 |
} |
|
78 |
else None |
|
79 |
} |
|
80 |
private var state: Option[(CharSequence, Int)] = if (end == 0) None else next_chunk(-1) |
|
81 |
||
82 |
def hasNext(): Boolean = state.isDefined |
|
83 |
def next(): CharSequence = |
|
84 |
state match { |
|
85 |
case Some((s, i)) => { state = next_chunk(i); s } |
|
86 |
case None => Iterator.empty.next() |
|
87 |
} |
|
88 |
} |
|
89 |
||
90 |
def first_line(source: CharSequence): String = |
|
91 |
{ |
|
92 |
val lines = chunks(source) |
|
93 |
if (lines.hasNext) lines.next.toString |
|
94 |
else "" |
|
95 |
} |
|
96 |
||
97 |
||
43598 | 98 |
/* strings */ |
99 |
||
100 |
def quote(s: String): String = "\"" + s + "\"" |
|
101 |
def commas(ss: Iterable[String]): String = ss.iterator.mkString(", ") |
|
102 |
def commas_quote(ss: Iterable[String]): String = ss.iterator.mkString("\"", ", ", "\"") |
|
103 |
||
36688 | 104 |
|
34141 | 105 |
/* reverse CharSequence */ |
106 |
||
107 |
class Reverse(text: CharSequence, start: Int, end: Int) extends CharSequence |
|
108 |
{ |
|
109 |
require(0 <= start && start <= end && end <= text.length) |
|
110 |
||
111 |
def this(text: CharSequence) = this(text, 0, text.length) |
|
112 |
||
113 |
def length: Int = end - start |
|
114 |
def charAt(i: Int): Char = text.charAt(end - i - 1) |
|
115 |
||
116 |
def subSequence(i: Int, j: Int): CharSequence = |
|
117 |
if (0 <= i && i <= j && j <= length) new Reverse(text, end - j, end - i) |
|
118 |
else throw new IndexOutOfBoundsException |
|
119 |
||
120 |
override def toString: String = |
|
121 |
{ |
|
122 |
val buf = new StringBuilder(length) |
|
123 |
for (i <- 0 until length) |
|
124 |
buf.append(charAt(i)) |
|
125 |
buf.toString |
|
126 |
} |
|
127 |
} |
|
128 |
||
129 |
||
34216 | 130 |
/* simple dialogs */ |
131 |
||
132 |
private def simple_dialog(kind: Int, default_title: String) |
|
133 |
(parent: Component, title: String, message: Any*) |
|
134 |
{ |
|
36791 | 135 |
Swing_Thread.now { |
38232
00b72526dc64
simple_dialog: allow scala.swing.Component as well;
wenzelm
parents:
37686
diff
changeset
|
136 |
val java_message = message map { case x: scala.swing.Component => x.peer case x => x } |
36791 | 137 |
JOptionPane.showMessageDialog(parent, |
38232
00b72526dc64
simple_dialog: allow scala.swing.Component as well;
wenzelm
parents:
37686
diff
changeset
|
138 |
java_message.toArray.asInstanceOf[Array[AnyRef]], |
36791 | 139 |
if (title == null) default_title else title, kind) |
140 |
} |
|
34216 | 141 |
} |
142 |
||
143 |
def dialog = simple_dialog(JOptionPane.PLAIN_MESSAGE, null) _ |
|
144 |
def warning_dialog = simple_dialog(JOptionPane.WARNING_MESSAGE, "Warning") _ |
|
145 |
def error_dialog = simple_dialog(JOptionPane.ERROR_MESSAGE, "Error") _ |
|
146 |
||
147 |
||
37018 | 148 |
/* zoom box */ |
149 |
||
37048 | 150 |
class Zoom_Box(apply_factor: Int => Unit) extends ComboBox[String]( |
151 |
List("50%", "70%", "85%", "100%", "125%", "150%", "175%", "200%", "300%", "400%")) |
|
152 |
{ |
|
153 |
val Factor = "([0-9]+)%?"r |
|
154 |
def parse(text: String): Int = |
|
155 |
text match { |
|
156 |
case Factor(s) => |
|
157 |
val i = Integer.parseInt(s) |
|
158 |
if (10 <= i && i <= 1000) i else 100 |
|
159 |
case _ => 100 |
|
160 |
} |
|
161 |
def print(i: Int): String = i.toString + "%" |
|
37018 | 162 |
|
37048 | 163 |
makeEditable()(c => new ComboBox.BuiltInEditor(c)(text => print(parse(text)), x => x)) |
164 |
reactions += { |
|
165 |
case SelectionChanged(_) => apply_factor(parse(selection.item)) |
|
37018 | 166 |
} |
37048 | 167 |
listenTo(selection) |
168 |
selection.index = 3 |
|
169 |
prototypeDisplayValue = Some("00000%") |
|
170 |
} |
|
37018 | 171 |
|
172 |
||
34136 | 173 |
/* timing */ |
174 |
||
34314 | 175 |
def timeit[A](message: String)(e: => A) = |
34136 | 176 |
{ |
40848
8662b9b1f123
more abstract/uniform handling of time, preferring seconds as Double;
wenzelm
parents:
40475
diff
changeset
|
177 |
val start = System.currentTimeMillis() |
34136 | 178 |
val result = Exn.capture(e) |
40848
8662b9b1f123
more abstract/uniform handling of time, preferring seconds as Double;
wenzelm
parents:
40475
diff
changeset
|
179 |
val stop = System.currentTimeMillis() |
34314 | 180 |
System.err.println( |
37686 | 181 |
(if (message == null || message.isEmpty) "" else message + ": ") + |
40848
8662b9b1f123
more abstract/uniform handling of time, preferring seconds as Double;
wenzelm
parents:
40475
diff
changeset
|
182 |
new Time(stop - start).message + " elapsed time") |
34136 | 183 |
Exn.release(result) |
184 |
} |
|
185 |
} |
|
43652 | 186 |
|
187 |
||
188 |
class Basic_Library |
|
189 |
{ |
|
43670
7f933761764b
prefer space_explode/split_lines as in Isabelle/ML;
wenzelm
parents:
43652
diff
changeset
|
190 |
val ERROR = Library.ERROR |
7f933761764b
prefer space_explode/split_lines as in Isabelle/ML;
wenzelm
parents:
43652
diff
changeset
|
191 |
val error = Library.error _ |
7f933761764b
prefer space_explode/split_lines as in Isabelle/ML;
wenzelm
parents:
43652
diff
changeset
|
192 |
val cat_error = Library.cat_error _ |
7f933761764b
prefer space_explode/split_lines as in Isabelle/ML;
wenzelm
parents:
43652
diff
changeset
|
193 |
|
43652 | 194 |
val space_explode = Library.space_explode _ |
43670
7f933761764b
prefer space_explode/split_lines as in Isabelle/ML;
wenzelm
parents:
43652
diff
changeset
|
195 |
val split_lines = Library.split_lines _ |
43652 | 196 |
|
197 |
val quote = Library.quote _ |
|
198 |
val commas = Library.commas _ |
|
199 |
val commas_quote = Library.commas_quote _ |
|
200 |
} |