author | haftmann |
Fri, 09 Jan 2015 08:37:00 +0100 | |
changeset 59324 | f5f9993a168d |
parent 59224 | e3f90d5c0006 |
child 59697 | 43e14b0e2ef8 |
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 |
59224 | 12 |
import scala.util.matching.Regex |
51981
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
13 |
|
37018 | 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 |
||
54548 | 27 |
def cat_message(msg1: String, msg2: String): String = |
28 |
if (msg1 == "") msg2 |
|
57831
885888a880fb
more general notion of "user error" including empty message -- NB: Output.error_message needs non-empty string to emit anything;
wenzelm
parents:
56843
diff
changeset
|
29 |
else if (msg2 == "") msg1 |
54548 | 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 |
||
57909
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
36 |
/* integers */ |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
37 |
|
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
38 |
private val small_int = 10000 |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
39 |
private lazy val small_int_table = |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
40 |
{ |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
41 |
val array = new Array[String](small_int) |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
42 |
for (i <- 0 until small_int) array(i) = i.toString |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
43 |
array |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
44 |
} |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
45 |
|
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
46 |
def is_small_int(s: String): Boolean = |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
47 |
{ |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
48 |
val len = s.length |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
49 |
1 <= len && len <= 4 && |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
50 |
s.forall(c => '0' <= c && c <= '9') && |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
51 |
(len == 1 || s(0) != '0') |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
52 |
} |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
53 |
|
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
54 |
def signed_string_of_long(i: Long): String = |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
55 |
if (0 <= i && i < small_int) small_int_table(i.toInt) |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
56 |
else i.toString |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
57 |
|
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
58 |
def signed_string_of_int(i: Int): String = |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
59 |
if (0 <= i && i < small_int) small_int_table(i) |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
60 |
else i.toString |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
61 |
|
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
62 |
|
48996 | 63 |
/* separated chunks */ |
36688 | 64 |
|
65 |
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
|
66 |
{ |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
67 |
val result = new mutable.ListBuffer[A] |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
68 |
var first = true |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
69 |
for (x <- list) { |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
70 |
if (first) { |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
71 |
first = false |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
72 |
result += x |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
73 |
} |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
74 |
else { |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
75 |
result += s |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
76 |
result += x |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
77 |
} |
36688 | 78 |
} |
51981
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
79 |
result.toList |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
80 |
} |
36688 | 81 |
|
56600 | 82 |
def separated_chunks(sep: Char => Boolean, source: CharSequence): Iterator[CharSequence] = |
48996 | 83 |
new Iterator[CharSequence] { |
84 |
private val end = source.length |
|
85 |
private def next_chunk(i: Int): Option[(CharSequence, Int)] = |
|
86 |
{ |
|
87 |
if (i < end) { |
|
56600 | 88 |
var j = i; do j += 1 while (j < end && !sep(source.charAt(j))) |
48996 | 89 |
Some((source.subSequence(i + 1, j), j)) |
90 |
} |
|
91 |
else None |
|
43598 | 92 |
} |
48996 | 93 |
private var state: Option[(CharSequence, Int)] = if (end == 0) None else next_chunk(-1) |
94 |
||
95 |
def hasNext(): Boolean = state.isDefined |
|
96 |
def next(): CharSequence = |
|
97 |
state match { |
|
98 |
case Some((s, i)) => { state = next_chunk(i); s } |
|
99 |
case None => Iterator.empty.next() |
|
100 |
} |
|
43598 | 101 |
} |
102 |
||
48996 | 103 |
def space_explode(sep: Char, str: String): List[String] = |
56600 | 104 |
separated_chunks(_ == sep, str).map(_.toString).toList |
48996 | 105 |
|
106 |
||
107 |
/* lines */ |
|
108 |
||
51983
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
109 |
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
|
110 |
new Iterable[CharSequence] { |
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
111 |
def iterator: Iterator[CharSequence] = |
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
112 |
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
|
113 |
} |
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
114 |
|
48996 | 115 |
def cat_lines(lines: TraversableOnce[String]): String = lines.mkString("\n") |
116 |
||
43670
7f933761764b
prefer space_explode/split_lines as in Isabelle/ML;
wenzelm
parents:
43652
diff
changeset
|
117 |
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
|
118 |
|
48996 | 119 |
def first_line(source: CharSequence): String = |
120 |
{ |
|
56600 | 121 |
val lines = separated_chunks(_ == '\n', source) |
48996 | 122 |
if (lines.hasNext) lines.next.toString |
123 |
else "" |
|
124 |
} |
|
125 |
||
50847 | 126 |
|
127 |
/* strings */ |
|
128 |
||
129 |
def try_unprefix(prfx: String, s: String): Option[String] = |
|
130 |
if (s.startsWith(prfx)) Some(s.substring(prfx.length)) else None |
|
131 |
||
55033 | 132 |
def try_unsuffix(sffx: String, s: String): Option[String] = |
133 |
if (s.endsWith(sffx)) Some(s.substring(0, s.length - sffx.length)) else None |
|
134 |
||
52444
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
51983
diff
changeset
|
135 |
def trim_line(s: String): String = |
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
51983
diff
changeset
|
136 |
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
|
137 |
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
|
138 |
else s |
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
51983
diff
changeset
|
139 |
|
43598 | 140 |
|
48996 | 141 |
/* quote */ |
46196 | 142 |
|
43598 | 143 |
def quote(s: String): String = "\"" + s + "\"" |
56843
b2bfcd8cda80
support for path completion based on file-system content;
wenzelm
parents:
56730
diff
changeset
|
144 |
|
b2bfcd8cda80
support for path completion based on file-system content;
wenzelm
parents:
56730
diff
changeset
|
145 |
def try_unquote(s: String): Option[String] = |
b2bfcd8cda80
support for path completion based on file-system content;
wenzelm
parents:
56730
diff
changeset
|
146 |
if (s.startsWith("\"") && s.endsWith("\"")) Some(s.substring(1, s.length - 1)) |
b2bfcd8cda80
support for path completion based on file-system content;
wenzelm
parents:
56730
diff
changeset
|
147 |
else None |
b2bfcd8cda80
support for path completion based on file-system content;
wenzelm
parents:
56730
diff
changeset
|
148 |
|
58592 | 149 |
def perhaps_unquote(s: String): String = try_unquote(s) getOrElse s |
150 |
||
43598 | 151 |
def commas(ss: Iterable[String]): String = ss.iterator.mkString(", ") |
48362 | 152 |
def commas_quote(ss: Iterable[String]): String = ss.iterator.map(quote).mkString(", ") |
43598 | 153 |
|
36688 | 154 |
|
51983
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
155 |
/* CharSequence */ |
34141 | 156 |
|
157 |
class Reverse(text: CharSequence, start: Int, end: Int) extends CharSequence |
|
158 |
{ |
|
159 |
require(0 <= start && start <= end && end <= text.length) |
|
160 |
||
161 |
def this(text: CharSequence) = this(text, 0, text.length) |
|
162 |
||
163 |
def length: Int = end - start |
|
164 |
def charAt(i: Int): Char = text.charAt(end - i - 1) |
|
165 |
||
166 |
def subSequence(i: Int, j: Int): CharSequence = |
|
167 |
if (0 <= i && i <= j && j <= length) new Reverse(text, end - j, end - i) |
|
168 |
else throw new IndexOutOfBoundsException |
|
169 |
||
170 |
override def toString: String = |
|
171 |
{ |
|
172 |
val buf = new StringBuilder(length) |
|
173 |
for (i <- 0 until length) |
|
174 |
buf.append(charAt(i)) |
|
175 |
buf.toString |
|
176 |
} |
|
177 |
} |
|
178 |
||
51983
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
179 |
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
|
180 |
{ |
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
181 |
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
|
182 |
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
|
183 |
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
|
184 |
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
|
185 |
else text.subSequence(i, j) |
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
186 |
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
|
187 |
} |
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
188 |
|
34141 | 189 |
|
59224 | 190 |
/* regular expressions */ |
191 |
||
192 |
def make_regex(s: String): Option[Regex] = |
|
193 |
try { Some(new Regex(s)) } catch { case ERROR(_) => None } |
|
194 |
||
195 |
||
56686 | 196 |
/* canonical list operations */ |
197 |
||
56688 | 198 |
def member[A, B](xs: List[A])(x: B): Boolean = xs.exists(_ == x) |
199 |
def insert[A](x: A)(xs: List[A]): List[A] = if (xs.contains(x)) xs else x :: xs |
|
200 |
def remove[A, B](x: B)(xs: List[A]): List[A] = if (member(xs)(x)) xs.filterNot(_ == x) else xs |
|
201 |
def update[A](x: A)(xs: List[A]): List[A] = x :: remove(x)(xs) |
|
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 |
} |