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