author | wenzelm |
Thu, 22 Sep 2016 11:25:27 +0200 | |
changeset 63936 | b87784e19a77 |
parent 63528 | 0f39f59317c1 |
child 64477 | 8be21ca788ca |
permissions | -rw-r--r-- |
27901 | 1 |
/* Title: Pure/General/symbol.scala |
2 |
Author: Makarius |
|
3 |
||
27924 | 4 |
Detecting and recoding Isabelle symbols. |
27901 | 5 |
*/ |
6 |
||
7 |
package isabelle |
|
8 |
||
55618 | 9 |
|
36011
3ff725ac13a4
adapted to Scala 2.8.0 Beta1 -- with notable changes to scala.collection;
wenzelm
parents:
34316
diff
changeset
|
10 |
import scala.collection.mutable |
31522 | 11 |
import scala.util.matching.Regex |
48922 | 12 |
import scala.annotation.tailrec |
27901 | 13 |
|
14 |
||
31522 | 15 |
object Symbol |
16 |
{ |
|
43696 | 17 |
type Symbol = String |
18 |
||
55884
f2c0eaedd579
tuned signature -- emphasize symbol positions (prover) vs. decoded text offsets (editor);
wenzelm
parents:
55618
diff
changeset
|
19 |
// counting Isabelle symbols, starting from 1 |
f2c0eaedd579
tuned signature -- emphasize symbol positions (prover) vs. decoded text offsets (editor);
wenzelm
parents:
55618
diff
changeset
|
20 |
type Offset = Text.Offset |
f2c0eaedd579
tuned signature -- emphasize symbol positions (prover) vs. decoded text offsets (editor);
wenzelm
parents:
55618
diff
changeset
|
21 |
type Range = Text.Range |
f2c0eaedd579
tuned signature -- emphasize symbol positions (prover) vs. decoded text offsets (editor);
wenzelm
parents:
55618
diff
changeset
|
22 |
|
43696 | 23 |
|
61865 | 24 |
/* spaces */ |
25 |
||
26 |
val space = " " |
|
27 |
||
28 |
private val static_spaces = space * 4000 |
|
29 |
||
30 |
def spaces(n: Int): String = |
|
31 |
{ |
|
32 |
require(n >= 0) |
|
33 |
if (n < static_spaces.length) static_spaces.substring(0, n) |
|
34 |
else space * n |
|
35 |
} |
|
36 |
||
37 |
||
43418 | 38 |
/* ASCII characters */ |
39 |
||
40 |
def is_ascii_letter(c: Char): Boolean = 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' |
|
55497 | 41 |
|
43418 | 42 |
def is_ascii_digit(c: Char): Boolean = '0' <= c && c <= '9' |
55497 | 43 |
|
44 |
def is_ascii_hex(c: Char): Boolean = |
|
45 |
'0' <= c && c <= '9' || 'A' <= c && c <= 'F' || 'a' <= c && c <= 'f' |
|
46 |
||
43418 | 47 |
def is_ascii_quasi(c: Char): Boolean = c == '_' || c == '\'' |
48 |
||
55497 | 49 |
def is_ascii_blank(c: Char): Boolean = " \t\n\u000b\f\r".contains(c) |
50 |
||
43418 | 51 |
def is_ascii_letdig(c: Char): Boolean = |
52 |
is_ascii_letter(c) || is_ascii_digit(c) || is_ascii_quasi(c) |
|
53 |
||
54 |
def is_ascii_identifier(s: String): Boolean = |
|
50238
98d35a7368bd
more uniform Symbol.is_ascii_identifier in ML/Scala;
wenzelm
parents:
50233
diff
changeset
|
55 |
s.length > 0 && is_ascii_letter(s(0)) && s.forall(is_ascii_letdig) |
43418 | 56 |
|
62528 | 57 |
def ascii(c: Char): Symbol = |
58 |
{ |
|
59 |
if (c > 127) error("Non-ASCII character: " + quote(c.toString)) |
|
60 |
else char_symbols(c.toInt) |
|
61 |
} |
|
62 |
||
43418 | 63 |
|
48775 | 64 |
/* symbol matching */ |
27901 | 65 |
|
48775 | 66 |
private val symbol_total = new Regex("""(?xs) |
63936 | 67 |
[\ud800-\udbff][\udc00-\udfff] | \r\n | \\ < \^? ([A-Za-z][A-Za-z0-9_']*)? >? | .""") |
27924 | 68 |
|
48775 | 69 |
private def is_plain(c: Char): Boolean = |
70 |
!(c == '\r' || c == '\\' || Character.isHighSurrogate(c)) |
|
48773
0e1bab274672
more liberal scanning of potentially malformed symbols;
wenzelm
parents:
48704
diff
changeset
|
71 |
|
0e1bab274672
more liberal scanning of potentially malformed symbols;
wenzelm
parents:
48704
diff
changeset
|
72 |
def is_malformed(s: Symbol): Boolean = |
0e1bab274672
more liberal scanning of potentially malformed symbols;
wenzelm
parents:
48704
diff
changeset
|
73 |
s.length match { |
0e1bab274672
more liberal scanning of potentially malformed symbols;
wenzelm
parents:
48704
diff
changeset
|
74 |
case 1 => |
0e1bab274672
more liberal scanning of potentially malformed symbols;
wenzelm
parents:
48704
diff
changeset
|
75 |
val c = s(0) |
0e1bab274672
more liberal scanning of potentially malformed symbols;
wenzelm
parents:
48704
diff
changeset
|
76 |
Character.isHighSurrogate(c) || Character.isLowSurrogate(c) || c == '\ufffd' |
0e1bab274672
more liberal scanning of potentially malformed symbols;
wenzelm
parents:
48704
diff
changeset
|
77 |
case 2 => |
0e1bab274672
more liberal scanning of potentially malformed symbols;
wenzelm
parents:
48704
diff
changeset
|
78 |
val c1 = s(0) |
0e1bab274672
more liberal scanning of potentially malformed symbols;
wenzelm
parents:
48704
diff
changeset
|
79 |
val c2 = s(1) |
0e1bab274672
more liberal scanning of potentially malformed symbols;
wenzelm
parents:
48704
diff
changeset
|
80 |
!(c1 == '\r' && c2 == '\n' || Character.isSurrogatePair(c1, c2)) |
48774 | 81 |
case _ => !s.endsWith(">") || s == "\\<>" || s == "\\<^>" |
48773
0e1bab274672
more liberal scanning of potentially malformed symbols;
wenzelm
parents:
48704
diff
changeset
|
82 |
} |
34137 | 83 |
|
54734
b91afc3aa3e6
clarified Proof General legacy: special treatment of \<^newline> only in TTY mode;
wenzelm
parents:
53400
diff
changeset
|
84 |
def is_newline(s: Symbol): Boolean = |
43675
8252d51d70e2
simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents:
43511
diff
changeset
|
85 |
s == "\n" || s == "\r" || s == "\r\n" |
38877 | 86 |
|
34137 | 87 |
class Matcher(text: CharSequence) |
88 |
{ |
|
48775 | 89 |
private val matcher = symbol_total.pattern.matcher(text) |
34137 | 90 |
def apply(start: Int, end: Int): Int = |
91 |
{ |
|
92 |
require(0 <= start && start < end && end <= text.length) |
|
34316
f879b649ac4c
clarified Symbol.is_plain/is_wellformed -- is_closed was rejecting plain backslashes;
wenzelm
parents:
34193
diff
changeset
|
93 |
if (is_plain(text.charAt(start))) 1 |
34138 | 94 |
else { |
34137 | 95 |
matcher.region(start, end).lookingAt |
96 |
matcher.group.length |
|
97 |
} |
|
98 |
} |
|
31522 | 99 |
} |
27937
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
100 |
|
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
101 |
|
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
102 |
/* iterator */ |
33998 | 103 |
|
43696 | 104 |
private val char_symbols: Array[Symbol] = |
43675
8252d51d70e2
simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents:
43511
diff
changeset
|
105 |
(0 until 256).iterator.map(i => new String(Array(i.toChar))).toArray |
8252d51d70e2
simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents:
43511
diff
changeset
|
106 |
|
43696 | 107 |
def iterator(text: CharSequence): Iterator[Symbol] = |
108 |
new Iterator[Symbol] |
|
40522 | 109 |
{ |
43489 | 110 |
private val matcher = new Matcher(text) |
111 |
private var i = 0 |
|
112 |
def hasNext = i < text.length |
|
113 |
def next = |
|
114 |
{ |
|
115 |
val n = matcher(i, text.length) |
|
43675
8252d51d70e2
simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents:
43511
diff
changeset
|
116 |
val s = |
8252d51d70e2
simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents:
43511
diff
changeset
|
117 |
if (n == 0) "" |
8252d51d70e2
simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents:
43511
diff
changeset
|
118 |
else if (n == 1) { |
8252d51d70e2
simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents:
43511
diff
changeset
|
119 |
val c = text.charAt(i) |
8252d51d70e2
simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents:
43511
diff
changeset
|
120 |
if (c < char_symbols.length) char_symbols(c) |
8252d51d70e2
simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents:
43511
diff
changeset
|
121 |
else text.subSequence(i, i + n).toString |
8252d51d70e2
simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents:
43511
diff
changeset
|
122 |
} |
8252d51d70e2
simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents:
43511
diff
changeset
|
123 |
else text.subSequence(i, i + n).toString |
43489 | 124 |
i += n |
125 |
s |
|
126 |
} |
|
33998 | 127 |
} |
43489 | 128 |
|
44949 | 129 |
def explode(text: CharSequence): List[Symbol] = iterator(text).toList |
130 |
||
48922 | 131 |
def advance_line_column(pos: (Int, Int), text: CharSequence): (Int, Int) = |
132 |
{ |
|
133 |
var (line, column) = pos |
|
134 |
for (sym <- iterator(text)) { |
|
54734
b91afc3aa3e6
clarified Proof General legacy: special treatment of \<^newline> only in TTY mode;
wenzelm
parents:
53400
diff
changeset
|
135 |
if (is_newline(sym)) { line += 1; column = 1 } |
48922 | 136 |
else column += 1 |
137 |
} |
|
138 |
(line, column) |
|
139 |
} |
|
140 |
||
33998 | 141 |
|
142 |
/* decoding offsets */ |
|
143 |
||
52507 | 144 |
object Index |
145 |
{ |
|
56471
2293a4350716
more frugal Symbol.Index -- no need to waste space on mostly empty arrays;
wenzelm
parents:
56338
diff
changeset
|
146 |
private sealed case class Entry(chr: Int, sym: Int) |
52507 | 147 |
|
56472 | 148 |
val empty: Index = new Index(Nil) |
56471
2293a4350716
more frugal Symbol.Index -- no need to waste space on mostly empty arrays;
wenzelm
parents:
56338
diff
changeset
|
149 |
|
2293a4350716
more frugal Symbol.Index -- no need to waste space on mostly empty arrays;
wenzelm
parents:
56338
diff
changeset
|
150 |
def apply(text: CharSequence): Index = |
31929 | 151 |
{ |
34137 | 152 |
val matcher = new Matcher(text) |
56471
2293a4350716
more frugal Symbol.Index -- no need to waste space on mostly empty arrays;
wenzelm
parents:
56338
diff
changeset
|
153 |
val buf = new mutable.ListBuffer[Entry] |
31929 | 154 |
var chr = 0 |
155 |
var sym = 0 |
|
33998 | 156 |
while (chr < text.length) { |
34137 | 157 |
val n = matcher(chr, text.length) |
158 |
chr += n |
|
31929 | 159 |
sym += 1 |
34137 | 160 |
if (n > 1) buf += Entry(chr, sym) |
31929 | 161 |
} |
56472 | 162 |
if (buf.isEmpty) empty else new Index(buf.toList) |
31929 | 163 |
} |
56471
2293a4350716
more frugal Symbol.Index -- no need to waste space on mostly empty arrays;
wenzelm
parents:
56338
diff
changeset
|
164 |
} |
55430 | 165 |
|
56472 | 166 |
final class Index private(entries: List[Index.Entry]) |
56471
2293a4350716
more frugal Symbol.Index -- no need to waste space on mostly empty arrays;
wenzelm
parents:
56338
diff
changeset
|
167 |
{ |
56472 | 168 |
private val hash: Int = entries.hashCode |
169 |
private val index: Array[Index.Entry] = entries.toArray |
|
170 |
||
55884
f2c0eaedd579
tuned signature -- emphasize symbol positions (prover) vs. decoded text offsets (editor);
wenzelm
parents:
55618
diff
changeset
|
171 |
def decode(symbol_offset: Offset): Text.Offset = |
31929 | 172 |
{ |
55884
f2c0eaedd579
tuned signature -- emphasize symbol positions (prover) vs. decoded text offsets (editor);
wenzelm
parents:
55618
diff
changeset
|
173 |
val sym = symbol_offset - 1 |
31929 | 174 |
val end = index.length |
48922 | 175 |
@tailrec def bisect(a: Int, b: Int): Int = |
31929 | 176 |
{ |
177 |
if (a < b) { |
|
178 |
val c = (a + b) / 2 |
|
179 |
if (sym < index(c).sym) bisect(a, c) |
|
180 |
else if (c + 1 == end || sym < index(c + 1).sym) c |
|
181 |
else bisect(c + 1, b) |
|
182 |
} |
|
183 |
else -1 |
|
184 |
} |
|
185 |
val i = bisect(0, end) |
|
186 |
if (i < 0) sym |
|
187 |
else index(i).chr + sym - index(i).sym |
|
188 |
} |
|
55884
f2c0eaedd579
tuned signature -- emphasize symbol positions (prover) vs. decoded text offsets (editor);
wenzelm
parents:
55618
diff
changeset
|
189 |
def decode(symbol_range: Range): Text.Range = symbol_range.map(decode(_)) |
56335
8953d4cc060a
store blob content within document node: aux. files that were once open are made persistent;
wenzelm
parents:
55884
diff
changeset
|
190 |
|
56338
f968f4e3d520
proper structural hashCode, which is required for Command.File equals (NB: Array has physical object identity);
wenzelm
parents:
56335
diff
changeset
|
191 |
override def hashCode: Int = hash |
56335
8953d4cc060a
store blob content within document node: aux. files that were once open are made persistent;
wenzelm
parents:
55884
diff
changeset
|
192 |
override def equals(that: Any): Boolean = |
8953d4cc060a
store blob content within document node: aux. files that were once open are made persistent;
wenzelm
parents:
55884
diff
changeset
|
193 |
that match { |
8953d4cc060a
store blob content within document node: aux. files that were once open are made persistent;
wenzelm
parents:
55884
diff
changeset
|
194 |
case other: Index => index.sameElements(other.index) |
8953d4cc060a
store blob content within document node: aux. files that were once open are made persistent;
wenzelm
parents:
55884
diff
changeset
|
195 |
case _ => false |
8953d4cc060a
store blob content within document node: aux. files that were once open are made persistent;
wenzelm
parents:
55884
diff
changeset
|
196 |
} |
31929 | 197 |
} |
198 |
||
199 |
||
56746 | 200 |
/* text chunks */ |
201 |
||
202 |
object Text_Chunk |
|
203 |
{ |
|
204 |
sealed abstract class Name |
|
205 |
case object Default extends Name |
|
206 |
case class Id(id: Document_ID.Generic) extends Name |
|
207 |
case class File(name: String) extends Name |
|
208 |
||
209 |
def apply(text: CharSequence): Text_Chunk = |
|
210 |
new Text_Chunk(Text.Range(0, text.length), Index(text)) |
|
211 |
} |
|
212 |
||
213 |
final class Text_Chunk private(val range: Text.Range, private val index: Index) |
|
214 |
{ |
|
215 |
override def hashCode: Int = (range, index).hashCode |
|
216 |
override def equals(that: Any): Boolean = |
|
217 |
that match { |
|
218 |
case other: Text_Chunk => |
|
219 |
range == other.range && |
|
220 |
index == other.index |
|
221 |
case _ => false |
|
222 |
} |
|
223 |
||
57840 | 224 |
override def toString: String = "Text_Chunk" + range.toString |
225 |
||
56746 | 226 |
def decode(symbol_offset: Offset): Text.Offset = index.decode(symbol_offset) |
227 |
def decode(symbol_range: Range): Text.Range = index.decode(symbol_range) |
|
228 |
def incorporate(symbol_range: Range): Option[Text.Range] = |
|
229 |
{ |
|
230 |
def in(r: Range): Option[Text.Range] = |
|
231 |
range.try_restrict(decode(r)) match { |
|
232 |
case Some(r1) if !r1.is_singularity => Some(r1) |
|
233 |
case _ => None |
|
234 |
} |
|
235 |
in(symbol_range) orElse in(symbol_range - 1) |
|
236 |
} |
|
237 |
} |
|
238 |
||
239 |
||
33998 | 240 |
/* recoding text */ |
27937
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
241 |
|
31522 | 242 |
private class Recoder(list: List[(String, String)]) |
243 |
{ |
|
244 |
private val (min, max) = |
|
245 |
{ |
|
27937
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
246 |
var min = '\uffff' |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
247 |
var max = '\u0000' |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
248 |
for ((x, _) <- list) { |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
249 |
val c = x(0) |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
250 |
if (c < min) min = c |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
251 |
if (c > max) max = c |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
252 |
} |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
253 |
(min, max) |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
254 |
} |
40443 | 255 |
private val table = |
256 |
{ |
|
257 |
var tab = Map[String, String]() |
|
258 |
for ((x, y) <- list) { |
|
259 |
tab.get(x) match { |
|
260 |
case None => tab += (x -> y) |
|
261 |
case Some(z) => |
|
62230 | 262 |
error("Duplicate symbol mapping of " + quote(x) + " to " + quote(y) + " vs. " + quote(z)) |
40443 | 263 |
} |
264 |
} |
|
265 |
tab |
|
266 |
} |
|
31522 | 267 |
def recode(text: String): String = |
268 |
{ |
|
27937
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
269 |
val len = text.length |
48775 | 270 |
val matcher = symbol_total.pattern.matcher(text) |
27937
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
271 |
val result = new StringBuilder(len) |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
272 |
var i = 0 |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
273 |
while (i < len) { |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
274 |
val c = text(i) |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
275 |
if (min <= c && c <= max) { |
31929 | 276 |
matcher.region(i, len).lookingAt |
27938 | 277 |
val x = matcher.group |
52888 | 278 |
result.append(table.getOrElse(x, x)) |
27937
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
279 |
i = matcher.end |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
280 |
} |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
281 |
else { result.append(c); i += 1 } |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
282 |
} |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
283 |
result.toString |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
284 |
} |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
285 |
} |
27924 | 286 |
|
27918 | 287 |
|
27923
7ebe9d38743a
use scala.collection.jcl.HashMap, which seems to be more efficient;
wenzelm
parents:
27918
diff
changeset
|
288 |
|
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
289 |
/** symbol interpretation **/ |
27927 | 290 |
|
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
291 |
private lazy val symbols = |
61959 | 292 |
{ |
293 |
val contents = |
|
294 |
for (path <- Path.split(Isabelle_System.getenv("ISABELLE_SYMBOLS")) if path.is_file) |
|
295 |
yield (File.read(path)) |
|
296 |
new Interpretation(cat_lines(contents)) |
|
297 |
} |
|
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
298 |
|
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
299 |
private class Interpretation(symbols_spec: String) |
29569
f3f529b5d8fb
more general init of Symbol.Interpretation, independent of IsabelleSystem instance;
wenzelm
parents:
29174
diff
changeset
|
300 |
{ |
31522 | 301 |
/* read symbols */ |
302 |
||
50136
a96bd08258a2
support for symbol groups, retaining original order of declarations;
wenzelm
parents:
48922
diff
changeset
|
303 |
private val No_Decl = new Regex("""(?xs) ^\s* (?: \#.* )? $ """) |
a96bd08258a2
support for symbol groups, retaining original order of declarations;
wenzelm
parents:
48922
diff
changeset
|
304 |
private val Key = new Regex("""(?xs) (.+): """) |
31522 | 305 |
|
53316
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
306 |
private def read_decl(decl: String): (Symbol, Properties.T) = |
31522 | 307 |
{ |
308 |
def err() = error("Bad symbol declaration: " + decl) |
|
309 |
||
53316
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
310 |
def read_props(props: List[String]): Properties.T = |
31522 | 311 |
{ |
312 |
props match { |
|
53316
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
313 |
case Nil => Nil |
31522 | 314 |
case _ :: Nil => err() |
61174 | 315 |
case Key(x) :: y :: rest => (x -> y.replace('\u2423', ' ')) :: read_props(rest) |
31522 | 316 |
case _ => err() |
317 |
} |
|
318 |
} |
|
319 |
decl.split("\\s+").toList match { |
|
53316
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
320 |
case sym :: props if sym.length > 1 && !is_malformed(sym) => |
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
321 |
(sym, read_props(props)) |
34193 | 322 |
case _ => err() |
31522 | 323 |
} |
324 |
} |
|
325 |
||
53316
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
326 |
private val symbols: List[(Symbol, Properties.T)] = |
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
327 |
(((List.empty[(Symbol, Properties.T)], Set.empty[Symbol]) /: |
50136
a96bd08258a2
support for symbol groups, retaining original order of declarations;
wenzelm
parents:
48922
diff
changeset
|
328 |
split_lines(symbols_spec).reverse) |
a96bd08258a2
support for symbol groups, retaining original order of declarations;
wenzelm
parents:
48922
diff
changeset
|
329 |
{ case (res, No_Decl()) => res |
a96bd08258a2
support for symbol groups, retaining original order of declarations;
wenzelm
parents:
48922
diff
changeset
|
330 |
case ((list, known), decl) => |
a96bd08258a2
support for symbol groups, retaining original order of declarations;
wenzelm
parents:
48922
diff
changeset
|
331 |
val (sym, props) = read_decl(decl) |
a96bd08258a2
support for symbol groups, retaining original order of declarations;
wenzelm
parents:
48922
diff
changeset
|
332 |
if (known(sym)) (list, known) |
a96bd08258a2
support for symbol groups, retaining original order of declarations;
wenzelm
parents:
48922
diff
changeset
|
333 |
else ((sym, props) :: list, known + sym) |
a96bd08258a2
support for symbol groups, retaining original order of declarations;
wenzelm
parents:
48922
diff
changeset
|
334 |
})._1 |
31522 | 335 |
|
336 |
||
53400 | 337 |
/* basic properties */ |
338 |
||
339 |
val properties: Map[Symbol, Properties.T] = Map(symbols: _*) |
|
31651 | 340 |
|
43696 | 341 |
val names: Map[Symbol, String] = |
34134 | 342 |
{ |
43456
8a6de1a6e1dc
names for control symbols without "^", which is relevant for completion;
wenzelm
parents:
43455
diff
changeset
|
343 |
val name = new Regex("""\\<\^?([A-Za-z][A-Za-z0-9_']*)>""") |
60215 | 344 |
Map((for ((sym @ name(a), _) <- symbols) yield sym -> a): _*) |
31651 | 345 |
} |
346 |
||
50136
a96bd08258a2
support for symbol groups, retaining original order of declarations;
wenzelm
parents:
48922
diff
changeset
|
347 |
val groups: List[(String, List[Symbol])] = |
53316
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
348 |
symbols.map({ case (sym, props) => |
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
349 |
val gs = for (("group", g) <- props) yield g |
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
350 |
if (gs.isEmpty) List(sym -> "unsorted") else gs.map(sym -> _) |
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
351 |
}).flatten |
50136
a96bd08258a2
support for symbol groups, retaining original order of declarations;
wenzelm
parents:
48922
diff
changeset
|
352 |
.groupBy(_._2).toList.map({ case (group, list) => (group, list.map(_._1)) }) |
a96bd08258a2
support for symbol groups, retaining original order of declarations;
wenzelm
parents:
48922
diff
changeset
|
353 |
.sortBy(_._1) |
a96bd08258a2
support for symbol groups, retaining original order of declarations;
wenzelm
parents:
48922
diff
changeset
|
354 |
|
53316
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
355 |
val abbrevs: Multi_Map[Symbol, String] = |
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
356 |
Multi_Map(( |
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
357 |
for { |
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
358 |
(sym, props) <- symbols |
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
359 |
("abbrev", a) <- props.reverse |
60215 | 360 |
} yield sym -> a): _*) |
43488 | 361 |
|
61376
93224745477f
output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents:
61174
diff
changeset
|
362 |
val codes: List[(String, Int)] = |
93224745477f
output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents:
61174
diff
changeset
|
363 |
{ |
93224745477f
output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents:
61174
diff
changeset
|
364 |
val Code = new Properties.String("code") |
93224745477f
output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents:
61174
diff
changeset
|
365 |
for { |
93224745477f
output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents:
61174
diff
changeset
|
366 |
(sym, props) <- symbols |
93224745477f
output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents:
61174
diff
changeset
|
367 |
code = |
93224745477f
output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents:
61174
diff
changeset
|
368 |
props match { |
93224745477f
output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents:
61174
diff
changeset
|
369 |
case Code(s) => |
93224745477f
output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents:
61174
diff
changeset
|
370 |
try { Integer.decode(s).intValue } |
93224745477f
output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents:
61174
diff
changeset
|
371 |
catch { case _: NumberFormatException => error("Bad code for symbol " + sym) } |
93224745477f
output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents:
61174
diff
changeset
|
372 |
case _ => error("Missing code for symbol " + sym) |
93224745477f
output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents:
61174
diff
changeset
|
373 |
} |
93224745477f
output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents:
61174
diff
changeset
|
374 |
} yield { |
93224745477f
output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents:
61174
diff
changeset
|
375 |
if (code < 128) error("Illegal ASCII code for symbol " + sym) |
93224745477f
output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents:
61174
diff
changeset
|
376 |
else (sym, code) |
93224745477f
output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents:
61174
diff
changeset
|
377 |
} |
93224745477f
output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents:
61174
diff
changeset
|
378 |
} |
93224745477f
output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents:
61174
diff
changeset
|
379 |
|
43488 | 380 |
|
43490 | 381 |
/* recoding */ |
31522 | 382 |
|
383 |
private val (decoder, encoder) = |
|
384 |
{ |
|
385 |
val mapping = |
|
61376
93224745477f
output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents:
61174
diff
changeset
|
386 |
for ((sym, code) <- codes) yield (sym, new String(Character.toChars(code))) |
93224745477f
output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents:
61174
diff
changeset
|
387 |
(new Recoder(mapping), new Recoder(for ((x, y) <- mapping) yield (y, x))) |
31522 | 388 |
} |
27918 | 389 |
|
34098 | 390 |
def decode(text: String): String = decoder.recode(text) |
391 |
def encode(text: String): String = encoder.recode(text) |
|
34134 | 392 |
|
43490 | 393 |
private def recode_set(elems: String*): Set[String] = |
394 |
{ |
|
395 |
val content = elems.toList |
|
396 |
Set((content ::: content.map(decode)): _*) |
|
397 |
} |
|
398 |
||
399 |
private def recode_map[A](elems: (String, A)*): Map[String, A] = |
|
400 |
{ |
|
401 |
val content = elems.toList |
|
402 |
Map((content ::: content.map({ case (sym, a) => (decode(sym), a) })): _*) |
|
403 |
} |
|
404 |
||
405 |
||
406 |
/* user fonts */ |
|
407 |
||
53316
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
408 |
private val Font = new Properties.String("font") |
43696 | 409 |
val fonts: Map[Symbol, String] = |
60215 | 410 |
recode_map((for ((sym, Font(font)) <- symbols) yield sym -> font): _*) |
43490 | 411 |
|
412 |
val font_names: List[String] = Set(fonts.toList.map(_._2): _*).toList |
|
413 |
val font_index: Map[String, Int] = Map((font_names zip (0 until font_names.length).toList): _*) |
|
414 |
||
34134 | 415 |
|
416 |
/* classification */ |
|
417 |
||
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
418 |
val letters = recode_set( |
34134 | 419 |
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", |
420 |
"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", |
|
421 |
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", |
|
422 |
"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", |
|
423 |
||
424 |
"\\<A>", "\\<B>", "\\<C>", "\\<D>", "\\<E>", "\\<F>", "\\<G>", |
|
425 |
"\\<H>", "\\<I>", "\\<J>", "\\<K>", "\\<L>", "\\<M>", "\\<N>", |
|
426 |
"\\<O>", "\\<P>", "\\<Q>", "\\<R>", "\\<S>", "\\<T>", "\\<U>", |
|
427 |
"\\<V>", "\\<W>", "\\<X>", "\\<Y>", "\\<Z>", "\\<a>", "\\<b>", |
|
428 |
"\\<c>", "\\<d>", "\\<e>", "\\<f>", "\\<g>", "\\<h>", "\\<i>", |
|
429 |
"\\<j>", "\\<k>", "\\<l>", "\\<m>", "\\<n>", "\\<o>", "\\<p>", |
|
430 |
"\\<q>", "\\<r>", "\\<s>", "\\<t>", "\\<u>", "\\<v>", "\\<w>", |
|
431 |
"\\<x>", "\\<y>", "\\<z>", |
|
432 |
||
433 |
"\\<AA>", "\\<BB>", "\\<CC>", "\\<DD>", "\\<EE>", "\\<FF>", |
|
434 |
"\\<GG>", "\\<HH>", "\\<II>", "\\<JJ>", "\\<KK>", "\\<LL>", |
|
435 |
"\\<MM>", "\\<NN>", "\\<OO>", "\\<PP>", "\\<QQ>", "\\<RR>", |
|
436 |
"\\<SS>", "\\<TT>", "\\<UU>", "\\<VV>", "\\<WW>", "\\<XX>", |
|
437 |
"\\<YY>", "\\<ZZ>", "\\<aa>", "\\<bb>", "\\<cc>", "\\<dd>", |
|
438 |
"\\<ee>", "\\<ff>", "\\<gg>", "\\<hh>", "\\<ii>", "\\<jj>", |
|
439 |
"\\<kk>", "\\<ll>", "\\<mm>", "\\<nn>", "\\<oo>", "\\<pp>", |
|
440 |
"\\<qq>", "\\<rr>", "\\<ss>", "\\<tt>", "\\<uu>", "\\<vv>", |
|
441 |
"\\<ww>", "\\<xx>", "\\<yy>", "\\<zz>", |
|
442 |
||
443 |
"\\<alpha>", "\\<beta>", "\\<gamma>", "\\<delta>", "\\<epsilon>", |
|
444 |
"\\<zeta>", "\\<eta>", "\\<theta>", "\\<iota>", "\\<kappa>", |
|
445 |
"\\<mu>", "\\<nu>", "\\<xi>", "\\<pi>", "\\<rho>", "\\<sigma>", |
|
446 |
"\\<tau>", "\\<upsilon>", "\\<phi>", "\\<chi>", "\\<psi>", |
|
447 |
"\\<omega>", "\\<Gamma>", "\\<Delta>", "\\<Theta>", "\\<Lambda>", |
|
448 |
"\\<Xi>", "\\<Pi>", "\\<Sigma>", "\\<Upsilon>", "\\<Phi>", |
|
52616
3ac2878764f9
more robust identifier syntax: sub/superscript counts as modifier of LETDIG part instead of LETTER, both isub/isup and sub/sup are allowed;
wenzelm
parents:
52507
diff
changeset
|
449 |
"\\<Psi>", "\\<Omega>") |
34134 | 450 |
|
61865 | 451 |
val blanks = recode_set(space, "\t", "\n", "\u000B", "\f", "\r", "\r\n") |
34138 | 452 |
|
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
453 |
val sym_chars = |
34138 | 454 |
Set("!", "#", "$", "%", "&", "*", "+", "-", "/", "<", "=", ">", "?", "@", "^", "_", "|", "~") |
34134 | 455 |
|
44992
aa34d2d049ce
refined Symbol.is_symbolic -- cover recoded versions as well;
wenzelm
parents:
44949
diff
changeset
|
456 |
val symbolic = recode_set((for { (sym, _) <- symbols; if raw_symbolic(sym) } yield sym): _*) |
aa34d2d049ce
refined Symbol.is_symbolic -- cover recoded versions as well;
wenzelm
parents:
44949
diff
changeset
|
457 |
|
43455 | 458 |
|
63528
0f39f59317c1
completion templates for commands involving "begin ... end" blocks;
wenzelm
parents:
62528
diff
changeset
|
459 |
/* misc symbols */ |
61579 | 460 |
|
63528
0f39f59317c1
completion templates for commands involving "begin ... end" blocks;
wenzelm
parents:
62528
diff
changeset
|
461 |
val newline_decoded = decode(newline) |
61579 | 462 |
val comment_decoded = decode(comment) |
463 |
||
464 |
||
55033 | 465 |
/* cartouches */ |
466 |
||
467 |
val open_decoded = decode(open) |
|
468 |
val close_decoded = decode(close) |
|
469 |
||
470 |
||
43488 | 471 |
/* control symbols */ |
472 |
||
59107 | 473 |
val control_decoded: Set[Symbol] = |
43488 | 474 |
Set((for ((sym, _) <- symbols if sym.startsWith("\\<^")) yield decode(sym)): _*) |
475 |
||
62103 | 476 |
val sub_decoded = decode(sub) |
477 |
val sup_decoded = decode(sup) |
|
478 |
val bold_decoded = decode(bold) |
|
62104
fb73c0d7bb37
clarified symbol insertion, depending on buffer encoding;
wenzelm
parents:
62103
diff
changeset
|
479 |
val emph_decoded = decode(emph) |
43511 | 480 |
val bsub_decoded = decode("\\<^bsub>") |
481 |
val esub_decoded = decode("\\<^esub>") |
|
482 |
val bsup_decoded = decode("\\<^bsup>") |
|
483 |
val esup_decoded = decode("\\<^esup>") |
|
27918 | 484 |
} |
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
485 |
|
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
486 |
|
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
487 |
/* tables */ |
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
488 |
|
53400 | 489 |
def properties: Map[Symbol, Properties.T] = symbols.properties |
43696 | 490 |
def names: Map[Symbol, String] = symbols.names |
50136
a96bd08258a2
support for symbol groups, retaining original order of declarations;
wenzelm
parents:
48922
diff
changeset
|
491 |
def groups: List[(String, List[Symbol])] = symbols.groups |
53316
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
492 |
def abbrevs: Multi_Map[Symbol, String] = symbols.abbrevs |
61376
93224745477f
output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents:
61174
diff
changeset
|
493 |
def codes: List[(String, Int)] = symbols.codes |
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
494 |
|
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
495 |
def decode(text: String): String = symbols.decode(text) |
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
496 |
def encode(text: String): String = symbols.encode(text) |
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
497 |
|
53337
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53316
diff
changeset
|
498 |
def decode_string: XML.Decode.T[String] = (x => decode(XML.Decode.string(x))) |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53316
diff
changeset
|
499 |
def encode_string: XML.Encode.T[String] = (x => XML.Encode.string(encode(x))) |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53316
diff
changeset
|
500 |
|
50291
674893679352
prefer Symbol.decode_strict in batch mode, to avoid files with spurious Unicode symbols that clash with Isabelle symbol interpretation;
wenzelm
parents:
50238
diff
changeset
|
501 |
def decode_strict(text: String): String = |
674893679352
prefer Symbol.decode_strict in batch mode, to avoid files with spurious Unicode symbols that clash with Isabelle symbol interpretation;
wenzelm
parents:
50238
diff
changeset
|
502 |
{ |
674893679352
prefer Symbol.decode_strict in batch mode, to avoid files with spurious Unicode symbols that clash with Isabelle symbol interpretation;
wenzelm
parents:
50238
diff
changeset
|
503 |
val decoded = decode(text) |
674893679352
prefer Symbol.decode_strict in batch mode, to avoid files with spurious Unicode symbols that clash with Isabelle symbol interpretation;
wenzelm
parents:
50238
diff
changeset
|
504 |
if (encode(decoded) == text) decoded |
674893679352
prefer Symbol.decode_strict in batch mode, to avoid files with spurious Unicode symbols that clash with Isabelle symbol interpretation;
wenzelm
parents:
50238
diff
changeset
|
505 |
else { |
674893679352
prefer Symbol.decode_strict in batch mode, to avoid files with spurious Unicode symbols that clash with Isabelle symbol interpretation;
wenzelm
parents:
50238
diff
changeset
|
506 |
val bad = new mutable.ListBuffer[Symbol] |
674893679352
prefer Symbol.decode_strict in batch mode, to avoid files with spurious Unicode symbols that clash with Isabelle symbol interpretation;
wenzelm
parents:
50238
diff
changeset
|
507 |
for (s <- iterator(text) if encode(decode(s)) != s && !bad.contains(s)) |
674893679352
prefer Symbol.decode_strict in batch mode, to avoid files with spurious Unicode symbols that clash with Isabelle symbol interpretation;
wenzelm
parents:
50238
diff
changeset
|
508 |
bad += s |
674893679352
prefer Symbol.decode_strict in batch mode, to avoid files with spurious Unicode symbols that clash with Isabelle symbol interpretation;
wenzelm
parents:
50238
diff
changeset
|
509 |
error("Bad Unicode symbols in text: " + commas_quote(bad)) |
674893679352
prefer Symbol.decode_strict in batch mode, to avoid files with spurious Unicode symbols that clash with Isabelle symbol interpretation;
wenzelm
parents:
50238
diff
changeset
|
510 |
} |
674893679352
prefer Symbol.decode_strict in batch mode, to avoid files with spurious Unicode symbols that clash with Isabelle symbol interpretation;
wenzelm
parents:
50238
diff
changeset
|
511 |
} |
674893679352
prefer Symbol.decode_strict in batch mode, to avoid files with spurious Unicode symbols that clash with Isabelle symbol interpretation;
wenzelm
parents:
50238
diff
changeset
|
512 |
|
43696 | 513 |
def fonts: Map[Symbol, String] = symbols.fonts |
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
514 |
def font_names: List[String] = symbols.font_names |
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
515 |
def font_index: Map[String, Int] = symbols.font_index |
43696 | 516 |
def lookup_font(sym: Symbol): Option[Int] = symbols.fonts.get(sym).map(font_index(_)) |
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
517 |
|
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
518 |
|
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
519 |
/* classification */ |
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
520 |
|
43696 | 521 |
def is_letter(sym: Symbol): Boolean = symbols.letters.contains(sym) |
522 |
def is_digit(sym: Symbol): Boolean = sym.length == 1 && '0' <= sym(0) && sym(0) <= '9' |
|
523 |
def is_quasi(sym: Symbol): Boolean = sym == "_" || sym == "'" |
|
524 |
def is_letdig(sym: Symbol): Boolean = is_letter(sym) || is_digit(sym) || is_quasi(sym) |
|
525 |
def is_blank(sym: Symbol): Boolean = symbols.blanks.contains(sym) |
|
44992
aa34d2d049ce
refined Symbol.is_symbolic -- cover recoded versions as well;
wenzelm
parents:
44949
diff
changeset
|
526 |
|
55033 | 527 |
|
63528
0f39f59317c1
completion templates for commands involving "begin ... end" blocks;
wenzelm
parents:
62528
diff
changeset
|
528 |
/* misc symbols */ |
0f39f59317c1
completion templates for commands involving "begin ... end" blocks;
wenzelm
parents:
62528
diff
changeset
|
529 |
|
0f39f59317c1
completion templates for commands involving "begin ... end" blocks;
wenzelm
parents:
62528
diff
changeset
|
530 |
val newline: Symbol = "\\<newline>" |
0f39f59317c1
completion templates for commands involving "begin ... end" blocks;
wenzelm
parents:
62528
diff
changeset
|
531 |
def newline_decoded: Symbol = symbols.newline_decoded |
0f39f59317c1
completion templates for commands involving "begin ... end" blocks;
wenzelm
parents:
62528
diff
changeset
|
532 |
|
0f39f59317c1
completion templates for commands involving "begin ... end" blocks;
wenzelm
parents:
62528
diff
changeset
|
533 |
def print_newlines(str: String): String = |
0f39f59317c1
completion templates for commands involving "begin ... end" blocks;
wenzelm
parents:
62528
diff
changeset
|
534 |
if (str.contains('\n')) |
0f39f59317c1
completion templates for commands involving "begin ... end" blocks;
wenzelm
parents:
62528
diff
changeset
|
535 |
(for (s <- iterator(str)) yield { if (s == "\n") newline_decoded else s }).mkString |
0f39f59317c1
completion templates for commands involving "begin ... end" blocks;
wenzelm
parents:
62528
diff
changeset
|
536 |
else str |
61579 | 537 |
|
538 |
val comment: Symbol = "\\<comment>" |
|
539 |
def comment_decoded: Symbol = symbols.comment_decoded |
|
540 |
||
541 |
||
55033 | 542 |
/* cartouches */ |
543 |
||
61579 | 544 |
val open: Symbol = "\\<open>" |
545 |
val close: Symbol = "\\<close>" |
|
55033 | 546 |
|
547 |
def open_decoded: Symbol = symbols.open_decoded |
|
548 |
def close_decoded: Symbol = symbols.close_decoded |
|
549 |
||
550 |
def is_open(sym: Symbol): Boolean = sym == open_decoded || sym == open |
|
551 |
def is_close(sym: Symbol): Boolean = sym == close_decoded || sym == close |
|
552 |
||
553 |
||
554 |
/* symbols for symbolic identifiers */ |
|
44992
aa34d2d049ce
refined Symbol.is_symbolic -- cover recoded versions as well;
wenzelm
parents:
44949
diff
changeset
|
555 |
|
aa34d2d049ce
refined Symbol.is_symbolic -- cover recoded versions as well;
wenzelm
parents:
44949
diff
changeset
|
556 |
private def raw_symbolic(sym: Symbol): Boolean = |
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
557 |
sym.startsWith("\\<") && sym.endsWith(">") && !sym.startsWith("\\<^") |
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
558 |
|
55033 | 559 |
def is_symbolic(sym: Symbol): Boolean = |
560 |
!is_open(sym) && !is_close(sym) && (raw_symbolic(sym) || symbols.symbolic.contains(sym)) |
|
561 |
||
562 |
def is_symbolic_char(sym: Symbol): Boolean = symbols.sym_chars.contains(sym) |
|
563 |
||
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
564 |
|
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
565 |
/* control symbols */ |
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
566 |
|
59107 | 567 |
def is_control(sym: Symbol): Boolean = |
61470 | 568 |
(sym.startsWith("\\<^") && sym.endsWith(">")) || symbols.control_decoded.contains(sym) |
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
569 |
|
43696 | 570 |
def is_controllable(sym: Symbol): Boolean = |
59107 | 571 |
!is_blank(sym) && !is_control(sym) && !is_open(sym) && !is_close(sym) && !is_malformed(sym) |
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
572 |
|
62103 | 573 |
val sub = "\\<^sub>" |
574 |
val sup = "\\<^sup>" |
|
575 |
val bold = "\\<^bold>" |
|
62104
fb73c0d7bb37
clarified symbol insertion, depending on buffer encoding;
wenzelm
parents:
62103
diff
changeset
|
576 |
val emph = "\\<^emph>" |
62103 | 577 |
|
44238
36120feb70ed
some convenience actions/shortcuts for control symbols;
wenzelm
parents:
44181
diff
changeset
|
578 |
def sub_decoded: Symbol = symbols.sub_decoded |
36120feb70ed
some convenience actions/shortcuts for control symbols;
wenzelm
parents:
44181
diff
changeset
|
579 |
def sup_decoded: Symbol = symbols.sup_decoded |
62103 | 580 |
def bold_decoded: Symbol = symbols.bold_decoded |
581 |
def emph_decoded: Symbol = symbols.emph_decoded |
|
44238
36120feb70ed
some convenience actions/shortcuts for control symbols;
wenzelm
parents:
44181
diff
changeset
|
582 |
def bsub_decoded: Symbol = symbols.bsub_decoded |
36120feb70ed
some convenience actions/shortcuts for control symbols;
wenzelm
parents:
44181
diff
changeset
|
583 |
def esub_decoded: Symbol = symbols.esub_decoded |
36120feb70ed
some convenience actions/shortcuts for control symbols;
wenzelm
parents:
44181
diff
changeset
|
584 |
def bsup_decoded: Symbol = symbols.bsup_decoded |
36120feb70ed
some convenience actions/shortcuts for control symbols;
wenzelm
parents:
44181
diff
changeset
|
585 |
def esup_decoded: Symbol = symbols.esup_decoded |
27901 | 586 |
} |