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