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