author | wenzelm |
Wed, 16 Apr 2014 09:38:40 +0200 | |
changeset 56599 | c4424d8c890f |
parent 56568 | 6e4f2d4215b0 |
child 56600 | 628e039cc34d |
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 |
|
51981
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
11 |
import scala.collection.mutable |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
12 |
|
50414
e17a1f179bb0
explore theory_body_files via future, for improved performance;
wenzelm
parents:
50299
diff
changeset
|
13 |
import java.util.concurrent.{Future => JFuture, TimeUnit} |
37018 | 14 |
|
15 |
||
34136 | 16 |
object Library |
17 |
{ |
|
43652 | 18 |
/* user errors */ |
19 |
||
20 |
object ERROR |
|
21 |
{ |
|
22 |
def apply(message: String): Throwable = new RuntimeException(message) |
|
48479
819f7a5f3e7f
more general notion of user ERROR (cf. 44f56fe01528);
wenzelm
parents:
48425
diff
changeset
|
23 |
def unapply(exn: Throwable): Option[String] = Exn.user_message(exn) |
43652 | 24 |
} |
25 |
||
26 |
def error(message: String): Nothing = throw ERROR(message) |
|
27 |
||
54548 | 28 |
def cat_message(msg1: String, msg2: String): String = |
29 |
if (msg1 == "") msg2 |
|
30 |
else msg1 + "\n" + msg2 |
|
31 |
||
43652 | 32 |
def cat_error(msg1: String, msg2: String): Nothing = |
54548 | 33 |
error(cat_message(msg1, msg2)) |
43652 | 34 |
|
35 |
||
48996 | 36 |
/* separated chunks */ |
36688 | 37 |
|
38 |
def separate[A](s: A, list: List[A]): List[A] = |
|
51981
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
39 |
{ |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
40 |
val result = new mutable.ListBuffer[A] |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
41 |
var first = true |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
42 |
for (x <- list) { |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
43 |
if (first) { |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
44 |
first = false |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
45 |
result += x |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
46 |
} |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
47 |
else { |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
48 |
result += s |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
49 |
result += x |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
50 |
} |
36688 | 51 |
} |
51981
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
52 |
result.toList |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
53 |
} |
36688 | 54 |
|
48996 | 55 |
def separated_chunks(sep: Char, source: CharSequence): Iterator[CharSequence] = |
56 |
new Iterator[CharSequence] { |
|
57 |
private val end = source.length |
|
58 |
private def next_chunk(i: Int): Option[(CharSequence, Int)] = |
|
59 |
{ |
|
60 |
if (i < end) { |
|
61 |
var j = i; do j += 1 while (j < end && source.charAt(j) != sep) |
|
62 |
Some((source.subSequence(i + 1, j), j)) |
|
63 |
} |
|
64 |
else None |
|
43598 | 65 |
} |
48996 | 66 |
private var state: Option[(CharSequence, Int)] = if (end == 0) None else next_chunk(-1) |
67 |
||
68 |
def hasNext(): Boolean = state.isDefined |
|
69 |
def next(): CharSequence = |
|
70 |
state match { |
|
71 |
case Some((s, i)) => { state = next_chunk(i); s } |
|
72 |
case None => Iterator.empty.next() |
|
73 |
} |
|
43598 | 74 |
} |
75 |
||
48996 | 76 |
def space_explode(sep: Char, str: String): List[String] = |
77 |
separated_chunks(sep, str).map(_.toString).toList |
|
78 |
||
79 |
||
80 |
/* lines */ |
|
81 |
||
51983
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
82 |
def terminate_lines(lines: Iterable[CharSequence]): Iterable[CharSequence] = |
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
83 |
new Iterable[CharSequence] { |
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
84 |
def iterator: Iterator[CharSequence] = |
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
85 |
lines.iterator.map(line => new Line_Termination(line)) |
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
86 |
} |
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
87 |
|
48996 | 88 |
def cat_lines(lines: TraversableOnce[String]): String = lines.mkString("\n") |
89 |
||
43670
7f933761764b
prefer space_explode/split_lines as in Isabelle/ML;
wenzelm
parents:
43652
diff
changeset
|
90 |
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
|
91 |
|
48996 | 92 |
def first_line(source: CharSequence): String = |
93 |
{ |
|
94 |
val lines = separated_chunks('\n', source) |
|
95 |
if (lines.hasNext) lines.next.toString |
|
96 |
else "" |
|
97 |
} |
|
98 |
||
50847 | 99 |
|
100 |
/* strings */ |
|
101 |
||
102 |
def try_unprefix(prfx: String, s: String): Option[String] = |
|
103 |
if (s.startsWith(prfx)) Some(s.substring(prfx.length)) else None |
|
104 |
||
55033 | 105 |
def try_unsuffix(sffx: String, s: String): Option[String] = |
106 |
if (s.endsWith(sffx)) Some(s.substring(0, s.length - sffx.length)) else None |
|
107 |
||
52444
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
51983
diff
changeset
|
108 |
def trim_line(s: String): String = |
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
51983
diff
changeset
|
109 |
if (s.endsWith("\r\n")) s.substring(0, s.length - 2) |
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
51983
diff
changeset
|
110 |
else if (s.endsWith("\r") || s.endsWith("\n")) s.substring(0, s.length - 1) |
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
51983
diff
changeset
|
111 |
else s |
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
51983
diff
changeset
|
112 |
|
43598 | 113 |
|
48996 | 114 |
/* quote */ |
46196 | 115 |
|
43598 | 116 |
def quote(s: String): String = "\"" + s + "\"" |
117 |
def commas(ss: Iterable[String]): String = ss.iterator.mkString(", ") |
|
48362 | 118 |
def commas_quote(ss: Iterable[String]): String = ss.iterator.map(quote).mkString(", ") |
43598 | 119 |
|
36688 | 120 |
|
51983
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
121 |
/* CharSequence */ |
34141 | 122 |
|
123 |
class Reverse(text: CharSequence, start: Int, end: Int) extends CharSequence |
|
124 |
{ |
|
125 |
require(0 <= start && start <= end && end <= text.length) |
|
126 |
||
127 |
def this(text: CharSequence) = this(text, 0, text.length) |
|
128 |
||
129 |
def length: Int = end - start |
|
130 |
def charAt(i: Int): Char = text.charAt(end - i - 1) |
|
131 |
||
132 |
def subSequence(i: Int, j: Int): CharSequence = |
|
133 |
if (0 <= i && i <= j && j <= length) new Reverse(text, end - j, end - i) |
|
134 |
else throw new IndexOutOfBoundsException |
|
135 |
||
136 |
override def toString: String = |
|
137 |
{ |
|
138 |
val buf = new StringBuilder(length) |
|
139 |
for (i <- 0 until length) |
|
140 |
buf.append(charAt(i)) |
|
141 |
buf.toString |
|
142 |
} |
|
143 |
} |
|
144 |
||
51983
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
145 |
class Line_Termination(text: CharSequence) extends CharSequence |
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
146 |
{ |
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
147 |
def length: Int = text.length + 1 |
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
148 |
def charAt(i: Int): Char = if (i == text.length) '\n' else text.charAt(i) |
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
149 |
def subSequence(i: Int, j: Int): CharSequence = |
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
150 |
if (j == text.length + 1) new Line_Termination(text.subSequence(i, j - 1)) |
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
151 |
else text.subSequence(i, j) |
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
152 |
override def toString: String = text.toString + "\n" |
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
153 |
} |
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
154 |
|
34141 | 155 |
|
50414
e17a1f179bb0
explore theory_body_files via future, for improved performance;
wenzelm
parents:
50299
diff
changeset
|
156 |
/* Java futures */ |
e17a1f179bb0
explore theory_body_files via future, for improved performance;
wenzelm
parents:
50299
diff
changeset
|
157 |
|
e17a1f179bb0
explore theory_body_files via future, for improved performance;
wenzelm
parents:
50299
diff
changeset
|
158 |
def future_value[A](x: A) = new JFuture[A] |
e17a1f179bb0
explore theory_body_files via future, for improved performance;
wenzelm
parents:
50299
diff
changeset
|
159 |
{ |
e17a1f179bb0
explore theory_body_files via future, for improved performance;
wenzelm
parents:
50299
diff
changeset
|
160 |
def cancel(may_interrupt: Boolean): Boolean = false |
e17a1f179bb0
explore theory_body_files via future, for improved performance;
wenzelm
parents:
50299
diff
changeset
|
161 |
def isCancelled(): Boolean = false |
e17a1f179bb0
explore theory_body_files via future, for improved performance;
wenzelm
parents:
50299
diff
changeset
|
162 |
def isDone(): Boolean = true |
e17a1f179bb0
explore theory_body_files via future, for improved performance;
wenzelm
parents:
50299
diff
changeset
|
163 |
def get(): A = x |
e17a1f179bb0
explore theory_body_files via future, for improved performance;
wenzelm
parents:
50299
diff
changeset
|
164 |
def get(timeout: Long, time_unit: TimeUnit): A = x |
e17a1f179bb0
explore theory_body_files via future, for improved performance;
wenzelm
parents:
50299
diff
changeset
|
165 |
} |
34136 | 166 |
} |
43652 | 167 |
|
168 |
||
169 |
class Basic_Library |
|
170 |
{ |
|
43670
7f933761764b
prefer space_explode/split_lines as in Isabelle/ML;
wenzelm
parents:
43652
diff
changeset
|
171 |
val ERROR = Library.ERROR |
7f933761764b
prefer space_explode/split_lines as in Isabelle/ML;
wenzelm
parents:
43652
diff
changeset
|
172 |
val error = Library.error _ |
7f933761764b
prefer space_explode/split_lines as in Isabelle/ML;
wenzelm
parents:
43652
diff
changeset
|
173 |
val cat_error = Library.cat_error _ |
7f933761764b
prefer space_explode/split_lines as in Isabelle/ML;
wenzelm
parents:
43652
diff
changeset
|
174 |
|
43652 | 175 |
val space_explode = Library.space_explode _ |
43670
7f933761764b
prefer space_explode/split_lines as in Isabelle/ML;
wenzelm
parents:
43652
diff
changeset
|
176 |
val split_lines = Library.split_lines _ |
46196 | 177 |
val cat_lines = Library.cat_lines _ |
43652 | 178 |
val quote = Library.quote _ |
179 |
val commas = Library.commas _ |
|
180 |
val commas_quote = Library.commas_quote _ |
|
49470 | 181 |
|
182 |
||
183 |
/* parallel tasks */ |
|
184 |
||
185 |
implicit def function_as_callable[A](f: () => A) = |
|
186 |
new java.util.concurrent.Callable[A] { def call = f() } |
|
187 |
||
188 |
val default_thread_pool = |
|
189 |
scala.collection.parallel.ThreadPoolTasks.defaultThreadPool |
|
43652 | 190 |
} |