author | wenzelm |
Mon, 25 Mar 2019 16:45:08 +0100 | |
changeset 69980 | f2e3adfd916f |
parent 69891 | def3ec9cdb7e |
child 71383 | 8313dca6dee9 |
permissions | -rw-r--r-- |
27901 | 1 |
/* Title: Pure/General/symbol.scala |
2 |
Author: Makarius |
|
3 |
||
69490 | 4 |
Isabelle text 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 |
||
69448 | 51 |
def is_ascii_line_terminator(c: Char): Boolean = "\r\n".contains(c) |
52 |
||
43418 | 53 |
def is_ascii_letdig(c: Char): Boolean = |
54 |
is_ascii_letter(c) || is_ascii_digit(c) || is_ascii_quasi(c) |
|
55 |
||
56 |
def is_ascii_identifier(s: String): Boolean = |
|
50238
98d35a7368bd
more uniform Symbol.is_ascii_identifier in ML/Scala;
wenzelm
parents:
50233
diff
changeset
|
57 |
s.length > 0 && is_ascii_letter(s(0)) && s.forall(is_ascii_letdig) |
43418 | 58 |
|
62528 | 59 |
def ascii(c: Char): Symbol = |
60 |
{ |
|
61 |
if (c > 127) error("Non-ASCII character: " + quote(c.toString)) |
|
62 |
else char_symbols(c.toInt) |
|
63 |
} |
|
64 |
||
66919 | 65 |
def is_ascii(s: Symbol): Boolean = s.length == 1 && s(0) < 128 |
66 |
||
43418 | 67 |
|
48775 | 68 |
/* symbol matching */ |
27901 | 69 |
|
48775 | 70 |
private val symbol_total = new Regex("""(?xs) |
63936 | 71 |
[\ud800-\udbff][\udc00-\udfff] | \r\n | \\ < \^? ([A-Za-z][A-Za-z0-9_']*)? >? | .""") |
27924 | 72 |
|
48775 | 73 |
private def is_plain(c: Char): Boolean = |
74 |
!(c == '\r' || c == '\\' || Character.isHighSurrogate(c)) |
|
48773
0e1bab274672
more liberal scanning of potentially malformed symbols;
wenzelm
parents:
48704
diff
changeset
|
75 |
|
0e1bab274672
more liberal scanning of potentially malformed symbols;
wenzelm
parents:
48704
diff
changeset
|
76 |
def is_malformed(s: Symbol): Boolean = |
0e1bab274672
more liberal scanning of potentially malformed symbols;
wenzelm
parents:
48704
diff
changeset
|
77 |
s.length match { |
0e1bab274672
more liberal scanning of potentially malformed symbols;
wenzelm
parents:
48704
diff
changeset
|
78 |
case 1 => |
0e1bab274672
more liberal scanning of potentially malformed symbols;
wenzelm
parents:
48704
diff
changeset
|
79 |
val c = s(0) |
0e1bab274672
more liberal scanning of potentially malformed symbols;
wenzelm
parents:
48704
diff
changeset
|
80 |
Character.isHighSurrogate(c) || Character.isLowSurrogate(c) || c == '\ufffd' |
0e1bab274672
more liberal scanning of potentially malformed symbols;
wenzelm
parents:
48704
diff
changeset
|
81 |
case 2 => |
0e1bab274672
more liberal scanning of potentially malformed symbols;
wenzelm
parents:
48704
diff
changeset
|
82 |
val c1 = s(0) |
0e1bab274672
more liberal scanning of potentially malformed symbols;
wenzelm
parents:
48704
diff
changeset
|
83 |
val c2 = s(1) |
0e1bab274672
more liberal scanning of potentially malformed symbols;
wenzelm
parents:
48704
diff
changeset
|
84 |
!(c1 == '\r' && c2 == '\n' || Character.isSurrogatePair(c1, c2)) |
48774 | 85 |
case _ => !s.endsWith(">") || s == "\\<>" || s == "\\<^>" |
48773
0e1bab274672
more liberal scanning of potentially malformed symbols;
wenzelm
parents:
48704
diff
changeset
|
86 |
} |
34137 | 87 |
|
54734
b91afc3aa3e6
clarified Proof General legacy: special treatment of \<^newline> only in TTY mode;
wenzelm
parents:
53400
diff
changeset
|
88 |
def is_newline(s: Symbol): Boolean = |
43675
8252d51d70e2
simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents:
43511
diff
changeset
|
89 |
s == "\n" || s == "\r" || s == "\r\n" |
38877 | 90 |
|
34137 | 91 |
class Matcher(text: CharSequence) |
92 |
{ |
|
48775 | 93 |
private val matcher = symbol_total.pattern.matcher(text) |
34137 | 94 |
def apply(start: Int, end: Int): Int = |
95 |
{ |
|
96 |
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
|
97 |
if (is_plain(text.charAt(start))) 1 |
34138 | 98 |
else { |
34137 | 99 |
matcher.region(start, end).lookingAt |
100 |
matcher.group.length |
|
101 |
} |
|
102 |
} |
|
31522 | 103 |
} |
27937
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
104 |
|
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
105 |
|
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
106 |
/* iterator */ |
33998 | 107 |
|
43696 | 108 |
private val char_symbols: Array[Symbol] = |
43675
8252d51d70e2
simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents:
43511
diff
changeset
|
109 |
(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
|
110 |
|
43696 | 111 |
def iterator(text: CharSequence): Iterator[Symbol] = |
112 |
new Iterator[Symbol] |
|
40522 | 113 |
{ |
43489 | 114 |
private val matcher = new Matcher(text) |
115 |
private var i = 0 |
|
116 |
def hasNext = i < text.length |
|
117 |
def next = |
|
118 |
{ |
|
119 |
val n = matcher(i, text.length) |
|
43675
8252d51d70e2
simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents:
43511
diff
changeset
|
120 |
val s = |
8252d51d70e2
simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents:
43511
diff
changeset
|
121 |
if (n == 0) "" |
8252d51d70e2
simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents:
43511
diff
changeset
|
122 |
else if (n == 1) { |
8252d51d70e2
simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents:
43511
diff
changeset
|
123 |
val c = text.charAt(i) |
8252d51d70e2
simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents:
43511
diff
changeset
|
124 |
if (c < char_symbols.length) char_symbols(c) |
8252d51d70e2
simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents:
43511
diff
changeset
|
125 |
else text.subSequence(i, i + n).toString |
8252d51d70e2
simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents:
43511
diff
changeset
|
126 |
} |
8252d51d70e2
simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents:
43511
diff
changeset
|
127 |
else text.subSequence(i, i + n).toString |
43489 | 128 |
i += n |
129 |
s |
|
130 |
} |
|
33998 | 131 |
} |
43489 | 132 |
|
44949 | 133 |
def explode(text: CharSequence): List[Symbol] = iterator(text).toList |
134 |
||
64615 | 135 |
def length(text: CharSequence): Int = iterator(text).length |
64617 | 136 |
|
67435
f83c1842a559
trim blanks -- more thoroughly than in update_cartouches (for single-line comments);
wenzelm
parents:
67389
diff
changeset
|
137 |
def trim_blanks(text: CharSequence): String = |
f83c1842a559
trim blanks -- more thoroughly than in update_cartouches (for single-line comments);
wenzelm
parents:
67389
diff
changeset
|
138 |
Library.trim(is_blank(_), explode(text)).mkString |
f83c1842a559
trim blanks -- more thoroughly than in update_cartouches (for single-line comments);
wenzelm
parents:
67389
diff
changeset
|
139 |
|
69318 | 140 |
def all_blank(str: String): Boolean = |
141 |
iterator(str).forall(is_blank(_)) |
|
142 |
||
143 |
def trim_blank_lines(text: String): String = |
|
144 |
cat_lines(split_lines(text).dropWhile(all_blank).reverse.dropWhile(all_blank).reverse) |
|
145 |
||
33998 | 146 |
|
147 |
/* decoding offsets */ |
|
148 |
||
52507 | 149 |
object Index |
150 |
{ |
|
56471
2293a4350716
more frugal Symbol.Index -- no need to waste space on mostly empty arrays;
wenzelm
parents:
56338
diff
changeset
|
151 |
private sealed case class Entry(chr: Int, sym: Int) |
52507 | 152 |
|
56472 | 153 |
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
|
154 |
|
2293a4350716
more frugal Symbol.Index -- no need to waste space on mostly empty arrays;
wenzelm
parents:
56338
diff
changeset
|
155 |
def apply(text: CharSequence): Index = |
31929 | 156 |
{ |
34137 | 157 |
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
|
158 |
val buf = new mutable.ListBuffer[Entry] |
31929 | 159 |
var chr = 0 |
160 |
var sym = 0 |
|
33998 | 161 |
while (chr < text.length) { |
34137 | 162 |
val n = matcher(chr, text.length) |
163 |
chr += n |
|
31929 | 164 |
sym += 1 |
34137 | 165 |
if (n > 1) buf += Entry(chr, sym) |
31929 | 166 |
} |
56472 | 167 |
if (buf.isEmpty) empty else new Index(buf.toList) |
31929 | 168 |
} |
56471
2293a4350716
more frugal Symbol.Index -- no need to waste space on mostly empty arrays;
wenzelm
parents:
56338
diff
changeset
|
169 |
} |
55430 | 170 |
|
56472 | 171 |
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
|
172 |
{ |
56472 | 173 |
private val hash: Int = entries.hashCode |
174 |
private val index: Array[Index.Entry] = entries.toArray |
|
175 |
||
55884
f2c0eaedd579
tuned signature -- emphasize symbol positions (prover) vs. decoded text offsets (editor);
wenzelm
parents:
55618
diff
changeset
|
176 |
def decode(symbol_offset: Offset): Text.Offset = |
31929 | 177 |
{ |
55884
f2c0eaedd579
tuned signature -- emphasize symbol positions (prover) vs. decoded text offsets (editor);
wenzelm
parents:
55618
diff
changeset
|
178 |
val sym = symbol_offset - 1 |
31929 | 179 |
val end = index.length |
48922 | 180 |
@tailrec def bisect(a: Int, b: Int): Int = |
31929 | 181 |
{ |
182 |
if (a < b) { |
|
183 |
val c = (a + b) / 2 |
|
184 |
if (sym < index(c).sym) bisect(a, c) |
|
185 |
else if (c + 1 == end || sym < index(c + 1).sym) c |
|
186 |
else bisect(c + 1, b) |
|
187 |
} |
|
188 |
else -1 |
|
189 |
} |
|
190 |
val i = bisect(0, end) |
|
191 |
if (i < 0) sym |
|
192 |
else index(i).chr + sym - index(i).sym |
|
193 |
} |
|
55884
f2c0eaedd579
tuned signature -- emphasize symbol positions (prover) vs. decoded text offsets (editor);
wenzelm
parents:
55618
diff
changeset
|
194 |
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
|
195 |
|
56338
f968f4e3d520
proper structural hashCode, which is required for Command.File equals (NB: Array has physical object identity);
wenzelm
parents:
56335
diff
changeset
|
196 |
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
|
197 |
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
|
198 |
that match { |
8953d4cc060a
store blob content within document node: aux. files that were once open are made persistent;
wenzelm
parents:
55884
diff
changeset
|
199 |
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
|
200 |
case _ => false |
8953d4cc060a
store blob content within document node: aux. files that were once open are made persistent;
wenzelm
parents:
55884
diff
changeset
|
201 |
} |
31929 | 202 |
} |
203 |
||
204 |
||
64477 | 205 |
/* symbolic text chunks -- without actual text */ |
56746 | 206 |
|
207 |
object Text_Chunk |
|
208 |
{ |
|
209 |
sealed abstract class Name |
|
210 |
case object Default extends Name |
|
211 |
case class Id(id: Document_ID.Generic) extends Name |
|
212 |
case class File(name: String) extends Name |
|
213 |
||
65335 | 214 |
val encode_name: XML.Encode.T[Name] = |
215 |
{ |
|
216 |
import XML.Encode._ |
|
217 |
variant(List( |
|
218 |
{ case Default => (Nil, Nil) }, |
|
219 |
{ case Id(a) => (List(long_atom(a)), Nil) }, |
|
220 |
{ case File(a) => (List(a), Nil) })) |
|
221 |
} |
|
222 |
||
223 |
val decode_name: XML.Decode.T[Name] = |
|
224 |
{ |
|
225 |
import XML.Decode._ |
|
226 |
variant(List( |
|
227 |
{ case (Nil, Nil) => Default }, |
|
228 |
{ case (List(a), Nil) => Id(long_atom(a)) }, |
|
229 |
{ case (List(a), Nil) => File(a) })) |
|
230 |
} |
|
231 |
||
56746 | 232 |
def apply(text: CharSequence): Text_Chunk = |
233 |
new Text_Chunk(Text.Range(0, text.length), Index(text)) |
|
234 |
} |
|
235 |
||
236 |
final class Text_Chunk private(val range: Text.Range, private val index: Index) |
|
237 |
{ |
|
238 |
override def hashCode: Int = (range, index).hashCode |
|
239 |
override def equals(that: Any): Boolean = |
|
240 |
that match { |
|
241 |
case other: Text_Chunk => |
|
242 |
range == other.range && |
|
243 |
index == other.index |
|
244 |
case _ => false |
|
245 |
} |
|
246 |
||
57840 | 247 |
override def toString: String = "Text_Chunk" + range.toString |
248 |
||
56746 | 249 |
def decode(symbol_offset: Offset): Text.Offset = index.decode(symbol_offset) |
250 |
def decode(symbol_range: Range): Text.Range = index.decode(symbol_range) |
|
251 |
def incorporate(symbol_range: Range): Option[Text.Range] = |
|
252 |
{ |
|
253 |
def in(r: Range): Option[Text.Range] = |
|
254 |
range.try_restrict(decode(r)) match { |
|
255 |
case Some(r1) if !r1.is_singularity => Some(r1) |
|
256 |
case _ => None |
|
257 |
} |
|
258 |
in(symbol_range) orElse in(symbol_range - 1) |
|
259 |
} |
|
260 |
} |
|
261 |
||
262 |
||
33998 | 263 |
/* recoding text */ |
27937
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
264 |
|
31522 | 265 |
private class Recoder(list: List[(String, String)]) |
266 |
{ |
|
267 |
private val (min, max) = |
|
268 |
{ |
|
27937
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
269 |
var min = '\uffff' |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
270 |
var max = '\u0000' |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
271 |
for ((x, _) <- list) { |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
272 |
val c = x(0) |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
273 |
if (c < min) min = c |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
274 |
if (c > max) max = c |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
275 |
} |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
276 |
(min, max) |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
277 |
} |
40443 | 278 |
private val table = |
279 |
{ |
|
280 |
var tab = Map[String, String]() |
|
281 |
for ((x, y) <- list) { |
|
282 |
tab.get(x) match { |
|
283 |
case None => tab += (x -> y) |
|
284 |
case Some(z) => |
|
62230 | 285 |
error("Duplicate symbol mapping of " + quote(x) + " to " + quote(y) + " vs. " + quote(z)) |
40443 | 286 |
} |
287 |
} |
|
288 |
tab |
|
289 |
} |
|
31522 | 290 |
def recode(text: String): String = |
291 |
{ |
|
27937
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
292 |
val len = text.length |
48775 | 293 |
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
|
294 |
val result = new StringBuilder(len) |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
295 |
var i = 0 |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
296 |
while (i < len) { |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
297 |
val c = text(i) |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
298 |
if (min <= c && c <= max) { |
31929 | 299 |
matcher.region(i, len).lookingAt |
27938 | 300 |
val x = matcher.group |
52888 | 301 |
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
|
302 |
i = matcher.end |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
303 |
} |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
304 |
else { result.append(c); i += 1 } |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
305 |
} |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
306 |
result.toString |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
307 |
} |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
308 |
} |
27924 | 309 |
|
27918 | 310 |
|
27923
7ebe9d38743a
use scala.collection.jcl.HashMap, which seems to be more efficient;
wenzelm
parents:
27918
diff
changeset
|
311 |
|
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
312 |
/** symbol interpretation **/ |
27927 | 313 |
|
67311 | 314 |
val ARGUMENT_CARTOUCHE = "cartouche" |
315 |
val ARGUMENT_SPACE_CARTOUCHE = "space_cartouche" |
|
316 |
||
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
317 |
private lazy val symbols = |
61959 | 318 |
{ |
319 |
val contents = |
|
320 |
for (path <- Path.split(Isabelle_System.getenv("ISABELLE_SYMBOLS")) if path.is_file) |
|
321 |
yield (File.read(path)) |
|
322 |
new Interpretation(cat_lines(contents)) |
|
323 |
} |
|
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
324 |
|
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
325 |
private class Interpretation(symbols_spec: String) |
29569
f3f529b5d8fb
more general init of Symbol.Interpretation, independent of IsabelleSystem instance;
wenzelm
parents:
29174
diff
changeset
|
326 |
{ |
31522 | 327 |
/* read symbols */ |
328 |
||
50136
a96bd08258a2
support for symbol groups, retaining original order of declarations;
wenzelm
parents:
48922
diff
changeset
|
329 |
private val No_Decl = new Regex("""(?xs) ^\s* (?: \#.* )? $ """) |
a96bd08258a2
support for symbol groups, retaining original order of declarations;
wenzelm
parents:
48922
diff
changeset
|
330 |
private val Key = new Regex("""(?xs) (.+): """) |
31522 | 331 |
|
53316
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
332 |
private def read_decl(decl: String): (Symbol, Properties.T) = |
31522 | 333 |
{ |
334 |
def err() = error("Bad symbol declaration: " + decl) |
|
335 |
||
53316
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
336 |
def read_props(props: List[String]): Properties.T = |
31522 | 337 |
{ |
338 |
props match { |
|
53316
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
339 |
case Nil => Nil |
31522 | 340 |
case _ :: Nil => err() |
61174 | 341 |
case Key(x) :: y :: rest => (x -> y.replace('\u2423', ' ')) :: read_props(rest) |
31522 | 342 |
case _ => err() |
343 |
} |
|
344 |
} |
|
345 |
decl.split("\\s+").toList match { |
|
53316
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
346 |
case sym :: props if sym.length > 1 && !is_malformed(sym) => |
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
347 |
(sym, read_props(props)) |
34193 | 348 |
case _ => err() |
31522 | 349 |
} |
350 |
} |
|
351 |
||
53316
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
352 |
private val symbols: List[(Symbol, Properties.T)] = |
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
353 |
(((List.empty[(Symbol, Properties.T)], Set.empty[Symbol]) /: |
50136
a96bd08258a2
support for symbol groups, retaining original order of declarations;
wenzelm
parents:
48922
diff
changeset
|
354 |
split_lines(symbols_spec).reverse) |
a96bd08258a2
support for symbol groups, retaining original order of declarations;
wenzelm
parents:
48922
diff
changeset
|
355 |
{ case (res, No_Decl()) => res |
a96bd08258a2
support for symbol groups, retaining original order of declarations;
wenzelm
parents:
48922
diff
changeset
|
356 |
case ((list, known), decl) => |
a96bd08258a2
support for symbol groups, retaining original order of declarations;
wenzelm
parents:
48922
diff
changeset
|
357 |
val (sym, props) = read_decl(decl) |
a96bd08258a2
support for symbol groups, retaining original order of declarations;
wenzelm
parents:
48922
diff
changeset
|
358 |
if (known(sym)) (list, known) |
a96bd08258a2
support for symbol groups, retaining original order of declarations;
wenzelm
parents:
48922
diff
changeset
|
359 |
else ((sym, props) :: list, known + sym) |
a96bd08258a2
support for symbol groups, retaining original order of declarations;
wenzelm
parents:
48922
diff
changeset
|
360 |
})._1 |
31522 | 361 |
|
362 |
||
53400 | 363 |
/* basic properties */ |
364 |
||
365 |
val properties: Map[Symbol, Properties.T] = Map(symbols: _*) |
|
31651 | 366 |
|
67311 | 367 |
val names: Map[Symbol, (String, String)] = |
34134 | 368 |
{ |
67311 | 369 |
val Name = new Regex("""\\<\^?([A-Za-z][A-Za-z0-9_']*)>""") |
370 |
val Argument = new Properties.String("argument") |
|
371 |
def argument(sym: Symbol, props: Properties.T): String = |
|
372 |
props match { |
|
373 |
case Argument(arg) => |
|
374 |
if (arg == ARGUMENT_CARTOUCHE || arg == ARGUMENT_SPACE_CARTOUCHE) arg |
|
375 |
else error("Bad argument: " + quote(arg) + " for symbol " + quote(sym)) |
|
376 |
case _ => "" |
|
377 |
} |
|
378 |
Map((for ((sym @ Name(a), props) <- symbols) yield sym -> (a, argument(sym, props))): _*) |
|
31651 | 379 |
} |
380 |
||
50136
a96bd08258a2
support for symbol groups, retaining original order of declarations;
wenzelm
parents:
48922
diff
changeset
|
381 |
val groups: List[(String, List[Symbol])] = |
53316
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
382 |
symbols.map({ case (sym, props) => |
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
383 |
val gs = for (("group", g) <- props) yield g |
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
384 |
if (gs.isEmpty) List(sym -> "unsorted") else gs.map(sym -> _) |
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
385 |
}).flatten |
50136
a96bd08258a2
support for symbol groups, retaining original order of declarations;
wenzelm
parents:
48922
diff
changeset
|
386 |
.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
|
387 |
.sortBy(_._1) |
a96bd08258a2
support for symbol groups, retaining original order of declarations;
wenzelm
parents:
48922
diff
changeset
|
388 |
|
53316
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
389 |
val abbrevs: Multi_Map[Symbol, String] = |
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
390 |
Multi_Map(( |
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
391 |
for { |
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
392 |
(sym, props) <- symbols |
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
393 |
("abbrev", a) <- props.reverse |
60215 | 394 |
} yield sym -> a): _*) |
43488 | 395 |
|
66051 | 396 |
val codes: List[(Symbol, Int)] = |
61376
93224745477f
output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents:
61174
diff
changeset
|
397 |
{ |
93224745477f
output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents:
61174
diff
changeset
|
398 |
val Code = new Properties.String("code") |
93224745477f
output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents:
61174
diff
changeset
|
399 |
for { |
93224745477f
output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents:
61174
diff
changeset
|
400 |
(sym, props) <- symbols |
67311 | 401 |
code <- |
61376
93224745477f
output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents:
61174
diff
changeset
|
402 |
props match { |
93224745477f
output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents:
61174
diff
changeset
|
403 |
case Code(s) => |
67311 | 404 |
try { Some(Integer.decode(s).intValue) } |
61376
93224745477f
output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents:
61174
diff
changeset
|
405 |
catch { case _: NumberFormatException => error("Bad code for symbol " + sym) } |
67311 | 406 |
case _ => None |
61376
93224745477f
output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents:
61174
diff
changeset
|
407 |
} |
93224745477f
output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents:
61174
diff
changeset
|
408 |
} yield { |
93224745477f
output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents:
61174
diff
changeset
|
409 |
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
|
410 |
else (sym, code) |
93224745477f
output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents:
61174
diff
changeset
|
411 |
} |
93224745477f
output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents:
61174
diff
changeset
|
412 |
} |
93224745477f
output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents:
61174
diff
changeset
|
413 |
|
43488 | 414 |
|
43490 | 415 |
/* recoding */ |
31522 | 416 |
|
417 |
private val (decoder, encoder) = |
|
418 |
{ |
|
419 |
val mapping = |
|
61376
93224745477f
output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents:
61174
diff
changeset
|
420 |
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
|
421 |
(new Recoder(mapping), new Recoder(for ((x, y) <- mapping) yield (y, x))) |
31522 | 422 |
} |
27918 | 423 |
|
34098 | 424 |
def decode(text: String): String = decoder.recode(text) |
425 |
def encode(text: String): String = encoder.recode(text) |
|
34134 | 426 |
|
43490 | 427 |
private def recode_set(elems: String*): Set[String] = |
428 |
{ |
|
429 |
val content = elems.toList |
|
430 |
Set((content ::: content.map(decode)): _*) |
|
431 |
} |
|
432 |
||
433 |
private def recode_map[A](elems: (String, A)*): Map[String, A] = |
|
434 |
{ |
|
435 |
val content = elems.toList |
|
436 |
Map((content ::: content.map({ case (sym, a) => (decode(sym), a) })): _*) |
|
437 |
} |
|
438 |
||
439 |
||
440 |
/* user fonts */ |
|
441 |
||
53316
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
442 |
private val Font = new Properties.String("font") |
43696 | 443 |
val fonts: Map[Symbol, String] = |
60215 | 444 |
recode_map((for ((sym, Font(font)) <- symbols) yield sym -> font): _*) |
43490 | 445 |
|
446 |
val font_names: List[String] = Set(fonts.toList.map(_._2): _*).toList |
|
447 |
val font_index: Map[String, Int] = Map((font_names zip (0 until font_names.length).toList): _*) |
|
448 |
||
34134 | 449 |
|
450 |
/* classification */ |
|
451 |
||
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
452 |
val letters = recode_set( |
34134 | 453 |
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", |
454 |
"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", |
|
455 |
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", |
|
456 |
"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", |
|
457 |
||
458 |
"\\<A>", "\\<B>", "\\<C>", "\\<D>", "\\<E>", "\\<F>", "\\<G>", |
|
459 |
"\\<H>", "\\<I>", "\\<J>", "\\<K>", "\\<L>", "\\<M>", "\\<N>", |
|
460 |
"\\<O>", "\\<P>", "\\<Q>", "\\<R>", "\\<S>", "\\<T>", "\\<U>", |
|
461 |
"\\<V>", "\\<W>", "\\<X>", "\\<Y>", "\\<Z>", "\\<a>", "\\<b>", |
|
462 |
"\\<c>", "\\<d>", "\\<e>", "\\<f>", "\\<g>", "\\<h>", "\\<i>", |
|
463 |
"\\<j>", "\\<k>", "\\<l>", "\\<m>", "\\<n>", "\\<o>", "\\<p>", |
|
464 |
"\\<q>", "\\<r>", "\\<s>", "\\<t>", "\\<u>", "\\<v>", "\\<w>", |
|
465 |
"\\<x>", "\\<y>", "\\<z>", |
|
466 |
||
467 |
"\\<AA>", "\\<BB>", "\\<CC>", "\\<DD>", "\\<EE>", "\\<FF>", |
|
468 |
"\\<GG>", "\\<HH>", "\\<II>", "\\<JJ>", "\\<KK>", "\\<LL>", |
|
469 |
"\\<MM>", "\\<NN>", "\\<OO>", "\\<PP>", "\\<QQ>", "\\<RR>", |
|
470 |
"\\<SS>", "\\<TT>", "\\<UU>", "\\<VV>", "\\<WW>", "\\<XX>", |
|
471 |
"\\<YY>", "\\<ZZ>", "\\<aa>", "\\<bb>", "\\<cc>", "\\<dd>", |
|
472 |
"\\<ee>", "\\<ff>", "\\<gg>", "\\<hh>", "\\<ii>", "\\<jj>", |
|
473 |
"\\<kk>", "\\<ll>", "\\<mm>", "\\<nn>", "\\<oo>", "\\<pp>", |
|
474 |
"\\<qq>", "\\<rr>", "\\<ss>", "\\<tt>", "\\<uu>", "\\<vv>", |
|
475 |
"\\<ww>", "\\<xx>", "\\<yy>", "\\<zz>", |
|
476 |
||
477 |
"\\<alpha>", "\\<beta>", "\\<gamma>", "\\<delta>", "\\<epsilon>", |
|
478 |
"\\<zeta>", "\\<eta>", "\\<theta>", "\\<iota>", "\\<kappa>", |
|
479 |
"\\<mu>", "\\<nu>", "\\<xi>", "\\<pi>", "\\<rho>", "\\<sigma>", |
|
480 |
"\\<tau>", "\\<upsilon>", "\\<phi>", "\\<chi>", "\\<psi>", |
|
481 |
"\\<omega>", "\\<Gamma>", "\\<Delta>", "\\<Theta>", "\\<Lambda>", |
|
482 |
"\\<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
|
483 |
"\\<Psi>", "\\<Omega>") |
34134 | 484 |
|
61865 | 485 |
val blanks = recode_set(space, "\t", "\n", "\u000B", "\f", "\r", "\r\n") |
34138 | 486 |
|
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
487 |
val sym_chars = |
34138 | 488 |
Set("!", "#", "$", "%", "&", "*", "+", "-", "/", "<", "=", ">", "?", "@", "^", "_", "|", "~") |
34134 | 489 |
|
44992
aa34d2d049ce
refined Symbol.is_symbolic -- cover recoded versions as well;
wenzelm
parents:
44949
diff
changeset
|
490 |
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
|
491 |
|
43455 | 492 |
|
63528
0f39f59317c1
completion templates for commands involving "begin ... end" blocks;
wenzelm
parents:
62528
diff
changeset
|
493 |
/* misc symbols */ |
61579 | 494 |
|
63528
0f39f59317c1
completion templates for commands involving "begin ... end" blocks;
wenzelm
parents:
62528
diff
changeset
|
495 |
val newline_decoded = decode(newline) |
61579 | 496 |
val comment_decoded = decode(comment) |
67438 | 497 |
val cancel_decoded = decode(cancel) |
498 |
val latex_decoded = decode(latex) |
|
69891
def3ec9cdb7e
document markers are formal comments, and may thus occur anywhere in the command-span;
wenzelm
parents:
69887
diff
changeset
|
499 |
val marker_decoded = decode(marker) |
55033 | 500 |
val open_decoded = decode(open) |
501 |
val close_decoded = decode(close) |
|
502 |
||
503 |
||
43488 | 504 |
/* control symbols */ |
505 |
||
59107 | 506 |
val control_decoded: Set[Symbol] = |
43488 | 507 |
Set((for ((sym, _) <- symbols if sym.startsWith("\\<^")) yield decode(sym)): _*) |
508 |
||
62103 | 509 |
val sub_decoded = decode(sub) |
510 |
val sup_decoded = decode(sup) |
|
511 |
val bold_decoded = decode(bold) |
|
62104
fb73c0d7bb37
clarified symbol insertion, depending on buffer encoding;
wenzelm
parents:
62103
diff
changeset
|
512 |
val emph_decoded = decode(emph) |
65997
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65521
diff
changeset
|
513 |
val bsub_decoded = decode(bsub) |
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65521
diff
changeset
|
514 |
val esub_decoded = decode(esub) |
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65521
diff
changeset
|
515 |
val bsup_decoded = decode(bsup) |
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65521
diff
changeset
|
516 |
val esup_decoded = decode(esup) |
27918 | 517 |
} |
43695
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 |
|
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
520 |
/* tables */ |
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
521 |
|
53400 | 522 |
def properties: Map[Symbol, Properties.T] = symbols.properties |
67311 | 523 |
def names: Map[Symbol, (String, String)] = symbols.names |
50136
a96bd08258a2
support for symbol groups, retaining original order of declarations;
wenzelm
parents:
48922
diff
changeset
|
524 |
def groups: List[(String, List[Symbol])] = symbols.groups |
53316
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
525 |
def abbrevs: Multi_Map[Symbol, String] = symbols.abbrevs |
66051 | 526 |
def codes: List[(Symbol, Int)] = symbols.codes |
67389 | 527 |
def groups_code: List[(String, List[Symbol])] = |
528 |
{ |
|
529 |
val has_code = codes.iterator.map(_._1).toSet |
|
530 |
groups.flatMap({ case (group, symbols) => |
|
531 |
val symbols1 = symbols.filter(has_code) |
|
532 |
if (symbols1.isEmpty) None else Some((group, symbols1)) |
|
533 |
}) |
|
534 |
} |
|
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
535 |
|
67304
3cf05d7cf174
more robust treatment of conflicts with existing Unicode text;
wenzelm
parents:
67255
diff
changeset
|
536 |
lazy val is_code: Int => Boolean = codes.map(_._2).toSet |
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
537 |
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
|
538 |
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
|
539 |
|
65344
b99283eed13c
clarified YXML vs. symbol encoding: operate on whole message;
wenzelm
parents:
65335
diff
changeset
|
540 |
def decode_yxml(text: String): XML.Body = YXML.parse_body(decode(text)) |
b99283eed13c
clarified YXML vs. symbol encoding: operate on whole message;
wenzelm
parents:
65335
diff
changeset
|
541 |
def decode_yxml_failsafe(text: String): XML.Body = YXML.parse_body_failsafe(decode(text)) |
b99283eed13c
clarified YXML vs. symbol encoding: operate on whole message;
wenzelm
parents:
65335
diff
changeset
|
542 |
def encode_yxml(body: XML.Body): String = encode(YXML.string_of_body(body)) |
53337
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53316
diff
changeset
|
543 |
|
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
|
544 |
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
|
545 |
{ |
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
|
546 |
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
|
547 |
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
|
548 |
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
|
549 |
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
|
550 |
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
|
551 |
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
|
552 |
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
|
553 |
} |
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
|
554 |
} |
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
|
555 |
|
43696 | 556 |
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
|
557 |
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
|
558 |
def font_index: Map[String, Int] = symbols.font_index |
43696 | 559 |
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
|
560 |
|
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
561 |
|
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
562 |
/* classification */ |
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
563 |
|
43696 | 564 |
def is_letter(sym: Symbol): Boolean = symbols.letters.contains(sym) |
565 |
def is_digit(sym: Symbol): Boolean = sym.length == 1 && '0' <= sym(0) && sym(0) <= '9' |
|
566 |
def is_quasi(sym: Symbol): Boolean = sym == "_" || sym == "'" |
|
567 |
def is_letdig(sym: Symbol): Boolean = is_letter(sym) || is_digit(sym) || is_quasi(sym) |
|
568 |
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
|
569 |
|
55033 | 570 |
|
67438 | 571 |
/* symbolic newline */ |
63528
0f39f59317c1
completion templates for commands involving "begin ... end" blocks;
wenzelm
parents:
62528
diff
changeset
|
572 |
|
0f39f59317c1
completion templates for commands involving "begin ... end" blocks;
wenzelm
parents:
62528
diff
changeset
|
573 |
val newline: Symbol = "\\<newline>" |
0f39f59317c1
completion templates for commands involving "begin ... end" blocks;
wenzelm
parents:
62528
diff
changeset
|
574 |
def newline_decoded: Symbol = symbols.newline_decoded |
0f39f59317c1
completion templates for commands involving "begin ... end" blocks;
wenzelm
parents:
62528
diff
changeset
|
575 |
|
0f39f59317c1
completion templates for commands involving "begin ... end" blocks;
wenzelm
parents:
62528
diff
changeset
|
576 |
def print_newlines(str: String): String = |
0f39f59317c1
completion templates for commands involving "begin ... end" blocks;
wenzelm
parents:
62528
diff
changeset
|
577 |
if (str.contains('\n')) |
0f39f59317c1
completion templates for commands involving "begin ... end" blocks;
wenzelm
parents:
62528
diff
changeset
|
578 |
(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
|
579 |
else str |
61579 | 580 |
|
67438 | 581 |
|
582 |
/* formal comments */ |
|
583 |
||
61579 | 584 |
val comment: Symbol = "\\<comment>" |
67449 | 585 |
val cancel: Symbol = "\\<^cancel>" |
586 |
val latex: Symbol = "\\<^latex>" |
|
69891
def3ec9cdb7e
document markers are formal comments, and may thus occur anywhere in the command-span;
wenzelm
parents:
69887
diff
changeset
|
587 |
val marker: Symbol = "\\<^marker>" |
67449 | 588 |
|
61579 | 589 |
def comment_decoded: Symbol = symbols.comment_decoded |
67438 | 590 |
def cancel_decoded: Symbol = symbols.cancel_decoded |
591 |
def latex_decoded: Symbol = symbols.latex_decoded |
|
69891
def3ec9cdb7e
document markers are formal comments, and may thus occur anywhere in the command-span;
wenzelm
parents:
69887
diff
changeset
|
592 |
def marker_decoded: Symbol = symbols.marker_decoded |
61579 | 593 |
|
594 |
||
55033 | 595 |
/* cartouches */ |
596 |
||
61579 | 597 |
val open: Symbol = "\\<open>" |
598 |
val close: Symbol = "\\<close>" |
|
55033 | 599 |
|
600 |
def open_decoded: Symbol = symbols.open_decoded |
|
601 |
def close_decoded: Symbol = symbols.close_decoded |
|
602 |
||
603 |
def is_open(sym: Symbol): Boolean = sym == open_decoded || sym == open |
|
604 |
def is_close(sym: Symbol): Boolean = sym == close_decoded || sym == close |
|
605 |
||
67131 | 606 |
def cartouche(s: String): String = open + s + close |
607 |
def cartouche_decoded(s: String): String = open_decoded + s + close_decoded |
|
608 |
||
55033 | 609 |
|
610 |
/* symbols for symbolic identifiers */ |
|
44992
aa34d2d049ce
refined Symbol.is_symbolic -- cover recoded versions as well;
wenzelm
parents:
44949
diff
changeset
|
611 |
|
aa34d2d049ce
refined Symbol.is_symbolic -- cover recoded versions as well;
wenzelm
parents:
44949
diff
changeset
|
612 |
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
|
613 |
sym.startsWith("\\<") && sym.endsWith(">") && !sym.startsWith("\\<^") |
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
614 |
|
55033 | 615 |
def is_symbolic(sym: Symbol): Boolean = |
616 |
!is_open(sym) && !is_close(sym) && (raw_symbolic(sym) || symbols.symbolic.contains(sym)) |
|
617 |
||
618 |
def is_symbolic_char(sym: Symbol): Boolean = symbols.sym_chars.contains(sym) |
|
619 |
||
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
620 |
|
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
621 |
/* control symbols */ |
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
622 |
|
67127
cf111622c9f8
font style for literal control symbols, notably for antiquotations;
wenzelm
parents:
66919
diff
changeset
|
623 |
val control_prefix = "\\<^" |
cf111622c9f8
font style for literal control symbols, notably for antiquotations;
wenzelm
parents:
66919
diff
changeset
|
624 |
val control_suffix = ">" |
cf111622c9f8
font style for literal control symbols, notably for antiquotations;
wenzelm
parents:
66919
diff
changeset
|
625 |
|
67255
f1f983484878
HTML rendering of \<^control> as in Isabelle/jEdit;
wenzelm
parents:
67131
diff
changeset
|
626 |
def control_name(sym: Symbol): Option[String] = |
f1f983484878
HTML rendering of \<^control> as in Isabelle/jEdit;
wenzelm
parents:
67131
diff
changeset
|
627 |
if (is_control_encoded(sym)) |
f1f983484878
HTML rendering of \<^control> as in Isabelle/jEdit;
wenzelm
parents:
67131
diff
changeset
|
628 |
Some(sym.substring(control_prefix.length, sym.length - control_suffix.length)) |
f1f983484878
HTML rendering of \<^control> as in Isabelle/jEdit;
wenzelm
parents:
67131
diff
changeset
|
629 |
else None |
f1f983484878
HTML rendering of \<^control> as in Isabelle/jEdit;
wenzelm
parents:
67131
diff
changeset
|
630 |
|
67127
cf111622c9f8
font style for literal control symbols, notably for antiquotations;
wenzelm
parents:
66919
diff
changeset
|
631 |
def is_control_encoded(sym: Symbol): Boolean = |
cf111622c9f8
font style for literal control symbols, notably for antiquotations;
wenzelm
parents:
66919
diff
changeset
|
632 |
sym.startsWith(control_prefix) && sym.endsWith(control_suffix) |
cf111622c9f8
font style for literal control symbols, notably for antiquotations;
wenzelm
parents:
66919
diff
changeset
|
633 |
|
59107 | 634 |
def is_control(sym: Symbol): Boolean = |
67127
cf111622c9f8
font style for literal control symbols, notably for antiquotations;
wenzelm
parents:
66919
diff
changeset
|
635 |
is_control_encoded(sym) || symbols.control_decoded.contains(sym) |
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
636 |
|
43696 | 637 |
def is_controllable(sym: Symbol): Boolean = |
66006
cec184536dfd
uniform notion of Symbol.is_controllable (see also 265d9300d523);
wenzelm
parents:
65997
diff
changeset
|
638 |
!is_blank(sym) && !is_control(sym) && !is_open(sym) && !is_close(sym) && |
cec184536dfd
uniform notion of Symbol.is_controllable (see also 265d9300d523);
wenzelm
parents:
65997
diff
changeset
|
639 |
!is_malformed(sym) && sym != "\"" |
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
640 |
|
62103 | 641 |
val sub = "\\<^sub>" |
642 |
val sup = "\\<^sup>" |
|
643 |
val bold = "\\<^bold>" |
|
62104
fb73c0d7bb37
clarified symbol insertion, depending on buffer encoding;
wenzelm
parents:
62103
diff
changeset
|
644 |
val emph = "\\<^emph>" |
65997
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65521
diff
changeset
|
645 |
val bsub = "\\<^bsub>" |
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65521
diff
changeset
|
646 |
val esub = "\\<^esub>" |
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65521
diff
changeset
|
647 |
val bsup = "\\<^bsup>" |
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65521
diff
changeset
|
648 |
val esup = "\\<^esup>" |
62103 | 649 |
|
44238
36120feb70ed
some convenience actions/shortcuts for control symbols;
wenzelm
parents:
44181
diff
changeset
|
650 |
def sub_decoded: Symbol = symbols.sub_decoded |
36120feb70ed
some convenience actions/shortcuts for control symbols;
wenzelm
parents:
44181
diff
changeset
|
651 |
def sup_decoded: Symbol = symbols.sup_decoded |
62103 | 652 |
def bold_decoded: Symbol = symbols.bold_decoded |
653 |
def emph_decoded: Symbol = symbols.emph_decoded |
|
44238
36120feb70ed
some convenience actions/shortcuts for control symbols;
wenzelm
parents:
44181
diff
changeset
|
654 |
def bsub_decoded: Symbol = symbols.bsub_decoded |
36120feb70ed
some convenience actions/shortcuts for control symbols;
wenzelm
parents:
44181
diff
changeset
|
655 |
def esub_decoded: Symbol = symbols.esub_decoded |
36120feb70ed
some convenience actions/shortcuts for control symbols;
wenzelm
parents:
44181
diff
changeset
|
656 |
def bsup_decoded: Symbol = symbols.bsup_decoded |
36120feb70ed
some convenience actions/shortcuts for control symbols;
wenzelm
parents:
44181
diff
changeset
|
657 |
def esup_decoded: Symbol = symbols.esup_decoded |
27901 | 658 |
} |