author | Fabian Huch <huch@in.tum.de> |
Thu, 18 Jul 2024 13:08:11 +0200 | |
changeset 80574 | 90493e889dff |
parent 80548 | d1662f1296db |
child 81339 | e181259e539b |
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 |
||
75393 | 15 |
object Symbol { |
43696 | 16 |
type Symbol = String |
17 |
||
55884
f2c0eaedd579
tuned signature -- emphasize symbol positions (prover) vs. decoded text offsets (editor);
wenzelm
parents:
55618
diff
changeset
|
18 |
// counting Isabelle symbols, starting from 1 |
f2c0eaedd579
tuned signature -- emphasize symbol positions (prover) vs. decoded text offsets (editor);
wenzelm
parents:
55618
diff
changeset
|
19 |
type Offset = Text.Offset |
f2c0eaedd579
tuned signature -- emphasize symbol positions (prover) vs. decoded text offsets (editor);
wenzelm
parents:
55618
diff
changeset
|
20 |
type Range = Text.Range |
f2c0eaedd579
tuned signature -- emphasize symbol positions (prover) vs. decoded text offsets (editor);
wenzelm
parents:
55618
diff
changeset
|
21 |
|
43696 | 22 |
|
61865 | 23 |
/* spaces */ |
24 |
||
71649 | 25 |
val space_char = ' ' |
61865 | 26 |
val space = " " |
27 |
||
28 |
private val static_spaces = space * 4000 |
|
29 |
||
75393 | 30 |
def spaces(n: Int): String = { |
73120
c3589f2dff31
more informative errors: simplify diagnosis of spurious failures reported by users;
wenzelm
parents:
73030
diff
changeset
|
31 |
require(n >= 0, "negative spaces") |
61865 | 32 |
if (n < static_spaces.length) static_spaces.substring(0, n) |
33 |
else space * n |
|
34 |
} |
|
35 |
||
36 |
||
75238
e74d162ddf9f
clarified char symbols: cover most European languages;
wenzelm
parents:
75237
diff
changeset
|
37 |
/* char symbols */ |
e74d162ddf9f
clarified char symbols: cover most European languages;
wenzelm
parents:
75237
diff
changeset
|
38 |
|
e74d162ddf9f
clarified char symbols: cover most European languages;
wenzelm
parents:
75237
diff
changeset
|
39 |
private val char_symbols: Array[Symbol] = |
e74d162ddf9f
clarified char symbols: cover most European languages;
wenzelm
parents:
75237
diff
changeset
|
40 |
(0 until 0x500).iterator.map(i => new String(Array(i.toChar))).toArray |
e74d162ddf9f
clarified char symbols: cover most European languages;
wenzelm
parents:
75237
diff
changeset
|
41 |
|
e74d162ddf9f
clarified char symbols: cover most European languages;
wenzelm
parents:
75237
diff
changeset
|
42 |
def char_symbol(c: Char): String = |
e74d162ddf9f
clarified char symbols: cover most European languages;
wenzelm
parents:
75237
diff
changeset
|
43 |
if (c < char_symbols.length) char_symbols(c) |
e74d162ddf9f
clarified char symbols: cover most European languages;
wenzelm
parents:
75237
diff
changeset
|
44 |
else c.toString |
e74d162ddf9f
clarified char symbols: cover most European languages;
wenzelm
parents:
75237
diff
changeset
|
45 |
|
e74d162ddf9f
clarified char symbols: cover most European languages;
wenzelm
parents:
75237
diff
changeset
|
46 |
|
43418 | 47 |
/* ASCII characters */ |
48 |
||
71649 | 49 |
def is_ascii_printable(c: Char): Boolean = space_char <= c && c <= '~' |
50 |
||
43418 | 51 |
def is_ascii_letter(c: Char): Boolean = 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' |
55497 | 52 |
|
43418 | 53 |
def is_ascii_digit(c: Char): Boolean = '0' <= c && c <= '9' |
80548 | 54 |
def is_ascii_digit(b: Byte): Boolean = is_ascii_digit(b.toChar) |
55497 | 55 |
|
56 |
def is_ascii_hex(c: Char): Boolean = |
|
57 |
'0' <= c && c <= '9' || 'A' <= c && c <= 'F' || 'a' <= c && c <= 'f' |
|
58 |
||
43418 | 59 |
def is_ascii_quasi(c: Char): Boolean = c == '_' || c == '\'' |
60 |
||
55497 | 61 |
def is_ascii_blank(c: Char): Boolean = " \t\n\u000b\f\r".contains(c) |
62 |
||
69448 | 63 |
def is_ascii_line_terminator(c: Char): Boolean = "\r\n".contains(c) |
64 |
||
43418 | 65 |
def is_ascii_letdig(c: Char): Boolean = |
66 |
is_ascii_letter(c) || is_ascii_digit(c) || is_ascii_quasi(c) |
|
67 |
||
68 |
def is_ascii_identifier(s: String): Boolean = |
|
75192
7d680dcd69b1
misc tuning, based on suggestions by IntelliJ IDEA;
wenzelm
parents:
73359
diff
changeset
|
69 |
s.nonEmpty && is_ascii_letter(s(0)) && s.forall(is_ascii_letdig) |
43418 | 70 |
|
75393 | 71 |
def ascii(c: Char): Symbol = { |
62528 | 72 |
if (c > 127) error("Non-ASCII character: " + quote(c.toString)) |
75238
e74d162ddf9f
clarified char symbols: cover most European languages;
wenzelm
parents:
75237
diff
changeset
|
73 |
else char_symbol(c) |
62528 | 74 |
} |
75 |
||
66919 | 76 |
def is_ascii(s: Symbol): Boolean = s.length == 1 && s(0) < 128 |
77 |
||
43418 | 78 |
|
48775 | 79 |
/* symbol matching */ |
27901 | 80 |
|
48773
0e1bab274672
more liberal scanning of potentially malformed symbols;
wenzelm
parents:
48704
diff
changeset
|
81 |
def is_malformed(s: Symbol): Boolean = |
0e1bab274672
more liberal scanning of potentially malformed symbols;
wenzelm
parents:
48704
diff
changeset
|
82 |
s.length match { |
0e1bab274672
more liberal scanning of potentially malformed symbols;
wenzelm
parents:
48704
diff
changeset
|
83 |
case 1 => |
0e1bab274672
more liberal scanning of potentially malformed symbols;
wenzelm
parents:
48704
diff
changeset
|
84 |
val c = s(0) |
0e1bab274672
more liberal scanning of potentially malformed symbols;
wenzelm
parents:
48704
diff
changeset
|
85 |
Character.isHighSurrogate(c) || Character.isLowSurrogate(c) || c == '\ufffd' |
0e1bab274672
more liberal scanning of potentially malformed symbols;
wenzelm
parents:
48704
diff
changeset
|
86 |
case 2 => |
0e1bab274672
more liberal scanning of potentially malformed symbols;
wenzelm
parents:
48704
diff
changeset
|
87 |
val c1 = s(0) |
0e1bab274672
more liberal scanning of potentially malformed symbols;
wenzelm
parents:
48704
diff
changeset
|
88 |
val c2 = s(1) |
0e1bab274672
more liberal scanning of potentially malformed symbols;
wenzelm
parents:
48704
diff
changeset
|
89 |
!(c1 == '\r' && c2 == '\n' || Character.isSurrogatePair(c1, c2)) |
48774 | 90 |
case _ => !s.endsWith(">") || s == "\\<>" || s == "\\<^>" |
48773
0e1bab274672
more liberal scanning of potentially malformed symbols;
wenzelm
parents:
48704
diff
changeset
|
91 |
} |
34137 | 92 |
|
54734
b91afc3aa3e6
clarified Proof General legacy: special treatment of \<^newline> only in TTY mode;
wenzelm
parents:
53400
diff
changeset
|
93 |
def is_newline(s: Symbol): Boolean = |
43675
8252d51d70e2
simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents:
43511
diff
changeset
|
94 |
s == "\n" || s == "\r" || s == "\r\n" |
38877 | 95 |
|
75393 | 96 |
class Matcher(text: CharSequence) { |
75237
90eaac98b3fa
more elementary Symbol.Matcher without detour via Regex (see also Pure/General/symbol_explode.ML);
wenzelm
parents:
75199
diff
changeset
|
97 |
private def ok(i: Int): Boolean = 0 <= i && i < text.length |
90eaac98b3fa
more elementary Symbol.Matcher without detour via Regex (see also Pure/General/symbol_explode.ML);
wenzelm
parents:
75199
diff
changeset
|
98 |
private def char(i: Int): Char = if (ok(i)) text.charAt(i) else 0 |
90eaac98b3fa
more elementary Symbol.Matcher without detour via Regex (see also Pure/General/symbol_explode.ML);
wenzelm
parents:
75199
diff
changeset
|
99 |
private def maybe_char(c: Char, i: Int): Int = if (char(i) == c) i + 1 else i |
90eaac98b3fa
more elementary Symbol.Matcher without detour via Regex (see also Pure/General/symbol_explode.ML);
wenzelm
parents:
75199
diff
changeset
|
100 |
|
90eaac98b3fa
more elementary Symbol.Matcher without detour via Regex (see also Pure/General/symbol_explode.ML);
wenzelm
parents:
75199
diff
changeset
|
101 |
@tailrec private def many_ascii_letdig(i: Int): Int = |
90eaac98b3fa
more elementary Symbol.Matcher without detour via Regex (see also Pure/General/symbol_explode.ML);
wenzelm
parents:
75199
diff
changeset
|
102 |
if (is_ascii_letdig(char(i))) many_ascii_letdig(i + 1) else i |
90eaac98b3fa
more elementary Symbol.Matcher without detour via Regex (see also Pure/General/symbol_explode.ML);
wenzelm
parents:
75199
diff
changeset
|
103 |
private def maybe_ascii_id(i: Int): Int = |
90eaac98b3fa
more elementary Symbol.Matcher without detour via Regex (see also Pure/General/symbol_explode.ML);
wenzelm
parents:
75199
diff
changeset
|
104 |
if (is_ascii_letter(char(i))) many_ascii_letdig(i + 1) else i |
90eaac98b3fa
more elementary Symbol.Matcher without detour via Regex (see also Pure/General/symbol_explode.ML);
wenzelm
parents:
75199
diff
changeset
|
105 |
|
75393 | 106 |
def match_length(i: Int): Int = { |
75237
90eaac98b3fa
more elementary Symbol.Matcher without detour via Regex (see also Pure/General/symbol_explode.ML);
wenzelm
parents:
75199
diff
changeset
|
107 |
val a = char(i) |
90eaac98b3fa
more elementary Symbol.Matcher without detour via Regex (see also Pure/General/symbol_explode.ML);
wenzelm
parents:
75199
diff
changeset
|
108 |
val b = char(i + 1) |
90eaac98b3fa
more elementary Symbol.Matcher without detour via Regex (see also Pure/General/symbol_explode.ML);
wenzelm
parents:
75199
diff
changeset
|
109 |
|
90eaac98b3fa
more elementary Symbol.Matcher without detour via Regex (see also Pure/General/symbol_explode.ML);
wenzelm
parents:
75199
diff
changeset
|
110 |
if (Character.isHighSurrogate(a) && Character.isLowSurrogate(b) || a == '\r' && b == '\n') 2 |
90eaac98b3fa
more elementary Symbol.Matcher without detour via Regex (see also Pure/General/symbol_explode.ML);
wenzelm
parents:
75199
diff
changeset
|
111 |
else if (a == '\\' && b == '<') maybe_char('>', maybe_ascii_id(maybe_char('^', i + 2))) - i |
90eaac98b3fa
more elementary Symbol.Matcher without detour via Regex (see also Pure/General/symbol_explode.ML);
wenzelm
parents:
75199
diff
changeset
|
112 |
else if (ok(i)) 1 |
90eaac98b3fa
more elementary Symbol.Matcher without detour via Regex (see also Pure/General/symbol_explode.ML);
wenzelm
parents:
75199
diff
changeset
|
113 |
else 0 |
90eaac98b3fa
more elementary Symbol.Matcher without detour via Regex (see also Pure/General/symbol_explode.ML);
wenzelm
parents:
75199
diff
changeset
|
114 |
} |
90eaac98b3fa
more elementary Symbol.Matcher without detour via Regex (see also Pure/General/symbol_explode.ML);
wenzelm
parents:
75199
diff
changeset
|
115 |
|
90eaac98b3fa
more elementary Symbol.Matcher without detour via Regex (see also Pure/General/symbol_explode.ML);
wenzelm
parents:
75199
diff
changeset
|
116 |
def match_symbol(i: Int): String = |
90eaac98b3fa
more elementary Symbol.Matcher without detour via Regex (see also Pure/General/symbol_explode.ML);
wenzelm
parents:
75199
diff
changeset
|
117 |
match_length(i) match { |
90eaac98b3fa
more elementary Symbol.Matcher without detour via Regex (see also Pure/General/symbol_explode.ML);
wenzelm
parents:
75199
diff
changeset
|
118 |
case 0 => "" |
75238
e74d162ddf9f
clarified char symbols: cover most European languages;
wenzelm
parents:
75237
diff
changeset
|
119 |
case 1 => char_symbol(text.charAt(i)) |
e74d162ddf9f
clarified char symbols: cover most European languages;
wenzelm
parents:
75237
diff
changeset
|
120 |
case n => text.subSequence(i, i + n).toString |
34137 | 121 |
} |
31522 | 122 |
} |
27937
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
123 |
|
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
124 |
|
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
125 |
/* iterator */ |
33998 | 126 |
|
43696 | 127 |
def iterator(text: CharSequence): Iterator[Symbol] = |
75393 | 128 |
new Iterator[Symbol] { |
43489 | 129 |
private val matcher = new Matcher(text) |
130 |
private var i = 0 |
|
71601 | 131 |
def hasNext: Boolean = i < text.length |
75393 | 132 |
def next(): Symbol = { |
75237
90eaac98b3fa
more elementary Symbol.Matcher without detour via Regex (see also Pure/General/symbol_explode.ML);
wenzelm
parents:
75199
diff
changeset
|
133 |
val s = matcher.match_symbol(i) |
90eaac98b3fa
more elementary Symbol.Matcher without detour via Regex (see also Pure/General/symbol_explode.ML);
wenzelm
parents:
75199
diff
changeset
|
134 |
i += s.length |
43489 | 135 |
s |
136 |
} |
|
33998 | 137 |
} |
43489 | 138 |
|
44949 | 139 |
def explode(text: CharSequence): List[Symbol] = iterator(text).toList |
140 |
||
64615 | 141 |
def length(text: CharSequence): Int = iterator(text).length |
64617 | 142 |
|
67435
f83c1842a559
trim blanks -- more thoroughly than in update_cartouches (for single-line comments);
wenzelm
parents:
67389
diff
changeset
|
143 |
def trim_blanks(text: CharSequence): String = |
71601 | 144 |
Library.trim(is_blank, explode(text)).mkString |
67435
f83c1842a559
trim blanks -- more thoroughly than in update_cartouches (for single-line comments);
wenzelm
parents:
67389
diff
changeset
|
145 |
|
69318 | 146 |
def all_blank(str: String): Boolean = |
71601 | 147 |
iterator(str).forall(is_blank) |
69318 | 148 |
|
149 |
def trim_blank_lines(text: String): String = |
|
150 |
cat_lines(split_lines(text).dropWhile(all_blank).reverse.dropWhile(all_blank).reverse) |
|
151 |
||
33998 | 152 |
|
153 |
/* decoding offsets */ |
|
154 |
||
75393 | 155 |
object Index { |
56471
2293a4350716
more frugal Symbol.Index -- no need to waste space on mostly empty arrays;
wenzelm
parents:
56338
diff
changeset
|
156 |
private sealed case class Entry(chr: Int, sym: Int) |
52507 | 157 |
|
56472 | 158 |
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
|
159 |
|
75393 | 160 |
def apply(text: CharSequence): Index = { |
34137 | 161 |
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
|
162 |
val buf = new mutable.ListBuffer[Entry] |
31929 | 163 |
var chr = 0 |
164 |
var sym = 0 |
|
33998 | 165 |
while (chr < text.length) { |
75237
90eaac98b3fa
more elementary Symbol.Matcher without detour via Regex (see also Pure/General/symbol_explode.ML);
wenzelm
parents:
75199
diff
changeset
|
166 |
val n = matcher.match_length(chr) |
34137 | 167 |
chr += n |
31929 | 168 |
sym += 1 |
34137 | 169 |
if (n > 1) buf += Entry(chr, sym) |
31929 | 170 |
} |
56472 | 171 |
if (buf.isEmpty) empty else new Index(buf.toList) |
31929 | 172 |
} |
56471
2293a4350716
more frugal Symbol.Index -- no need to waste space on mostly empty arrays;
wenzelm
parents:
56338
diff
changeset
|
173 |
} |
55430 | 174 |
|
75393 | 175 |
final class Index private(entries: List[Index.Entry]) { |
56472 | 176 |
private val hash: Int = entries.hashCode |
177 |
private val index: Array[Index.Entry] = entries.toArray |
|
178 |
||
75393 | 179 |
def decode(symbol_offset: Offset): Text.Offset = { |
55884
f2c0eaedd579
tuned signature -- emphasize symbol positions (prover) vs. decoded text offsets (editor);
wenzelm
parents:
55618
diff
changeset
|
180 |
val sym = symbol_offset - 1 |
31929 | 181 |
val end = index.length |
75393 | 182 |
@tailrec def bisect(a: Int, b: Int): Int = { |
31929 | 183 |
if (a < b) { |
184 |
val c = (a + b) / 2 |
|
185 |
if (sym < index(c).sym) bisect(a, c) |
|
186 |
else if (c + 1 == end || sym < index(c + 1).sym) c |
|
187 |
else bisect(c + 1, b) |
|
188 |
} |
|
189 |
else -1 |
|
190 |
} |
|
191 |
val i = bisect(0, end) |
|
192 |
if (i < 0) sym |
|
193 |
else index(i).chr + sym - index(i).sym |
|
194 |
} |
|
71601 | 195 |
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
|
196 |
|
56338
f968f4e3d520
proper structural hashCode, which is required for Command.File equals (NB: Array has physical object identity);
wenzelm
parents:
56335
diff
changeset
|
197 |
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
|
198 |
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
|
199 |
that match { |
8953d4cc060a
store blob content within document node: aux. files that were once open are made persistent;
wenzelm
parents:
55884
diff
changeset
|
200 |
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
|
201 |
case _ => false |
8953d4cc060a
store blob content within document node: aux. files that were once open are made persistent;
wenzelm
parents:
55884
diff
changeset
|
202 |
} |
31929 | 203 |
} |
204 |
||
205 |
||
64477 | 206 |
/* symbolic text chunks -- without actual text */ |
56746 | 207 |
|
75393 | 208 |
object Text_Chunk { |
56746 | 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 |
||
214 |
def apply(text: CharSequence): Text_Chunk = |
|
76235 | 215 |
new Text_Chunk(Text.Range.length(text), Index(text)) |
56746 | 216 |
} |
217 |
||
75393 | 218 |
final class Text_Chunk private(val range: Text.Range, private val index: Index) { |
56746 | 219 |
override def hashCode: Int = (range, index).hashCode |
220 |
override def equals(that: Any): Boolean = |
|
221 |
that match { |
|
222 |
case other: Text_Chunk => |
|
223 |
range == other.range && |
|
224 |
index == other.index |
|
225 |
case _ => false |
|
226 |
} |
|
227 |
||
57840 | 228 |
override def toString: String = "Text_Chunk" + range.toString |
229 |
||
56746 | 230 |
def decode(symbol_offset: Offset): Text.Offset = index.decode(symbol_offset) |
231 |
def decode(symbol_range: Range): Text.Range = index.decode(symbol_range) |
|
75393 | 232 |
def incorporate(symbol_range: Range): Option[Text.Range] = { |
75659
9bd92ac9328f
more robust Scala 3 indentation, for the sake of IntelliJ IDEA;
wenzelm
parents:
75393
diff
changeset
|
233 |
def in(r: Range): Option[Text.Range] = { |
56746 | 234 |
range.try_restrict(decode(r)) match { |
235 |
case Some(r1) if !r1.is_singularity => Some(r1) |
|
236 |
case _ => None |
|
237 |
} |
|
75659
9bd92ac9328f
more robust Scala 3 indentation, for the sake of IntelliJ IDEA;
wenzelm
parents:
75393
diff
changeset
|
238 |
} |
9bd92ac9328f
more robust Scala 3 indentation, for the sake of IntelliJ IDEA;
wenzelm
parents:
75393
diff
changeset
|
239 |
in(symbol_range) orElse in(symbol_range - 1) |
56746 | 240 |
} |
241 |
} |
|
242 |
||
243 |
||
33998 | 244 |
/* recoding text */ |
27937
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
245 |
|
75393 | 246 |
private class Recoder(list: List[(String, String)]) { |
247 |
private val (min, max) = { |
|
27937
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
248 |
var min = '\uffff' |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
249 |
var max = '\u0000' |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
250 |
for ((x, _) <- list) { |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
251 |
val c = x(0) |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
252 |
if (c < min) min = c |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
253 |
if (c > max) max = c |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
254 |
} |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
255 |
(min, max) |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
256 |
} |
75393 | 257 |
private val table = { |
40443 | 258 |
var tab = Map[String, String]() |
259 |
for ((x, y) <- list) { |
|
260 |
tab.get(x) match { |
|
261 |
case None => tab += (x -> y) |
|
262 |
case Some(z) => |
|
62230 | 263 |
error("Duplicate symbol mapping of " + quote(x) + " to " + quote(y) + " vs. " + quote(z)) |
40443 | 264 |
} |
265 |
} |
|
266 |
tab |
|
267 |
} |
|
75393 | 268 |
def recode(text: String): String = { |
80443 | 269 |
val n = text.length |
80445 | 270 |
val relevant = { |
80441 | 271 |
var i = 0 |
80445 | 272 |
var found = false |
273 |
while (i < n && !found) { |
|
80441 | 274 |
val c = text(i) |
80445 | 275 |
if (min <= c && c <= max) { found = true } |
276 |
i += 1 |
|
277 |
} |
|
278 |
found |
|
279 |
} |
|
280 |
if (relevant) { |
|
281 |
val matcher = new Symbol.Matcher(text) |
|
282 |
Library.string_builder(hint = n) { result => |
|
283 |
var i = 0 |
|
284 |
while (i < n) { |
|
285 |
val c = text(i) |
|
286 |
if (min <= c && c <= max) { |
|
287 |
val s = matcher.match_symbol(i) |
|
288 |
result.append(table.getOrElse(s, s)) |
|
289 |
i += s.length |
|
290 |
} |
|
291 |
else { result.append(c); i += 1 } |
|
80441 | 292 |
} |
27937
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
293 |
} |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
294 |
} |
80445 | 295 |
else text |
27937
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
296 |
} |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
297 |
} |
27924 | 298 |
|
27918 | 299 |
|
27923
7ebe9d38743a
use scala.collection.jcl.HashMap, which seems to be more efficient;
wenzelm
parents:
27918
diff
changeset
|
300 |
|
75194 | 301 |
/** defined symbols **/ |
27927 | 302 |
|
78599 | 303 |
object Argument { |
304 |
def unapply(s: String): Option[Argument] = |
|
305 |
try { Some(valueOf(s)) } |
|
306 |
catch { case _: IllegalArgumentException => None} |
|
307 |
} |
|
67311 | 308 |
|
78599 | 309 |
enum Argument { case none, cartouche, space_cartouche } |
75193 | 310 |
|
75393 | 311 |
object Entry { |
75197 | 312 |
private val Name = new Regex("""\\<\^?([A-Za-z][A-Za-z0-9_']*)>""") |
313 |
private val Argument = new Properties.String("argument") |
|
314 |
private val Abbrev = new Properties.String("abbrev") |
|
315 |
private val Code = new Properties.String("code") |
|
316 |
private val Font = new Properties.String("font") |
|
317 |
private val Group = new Properties.String("group") |
|
318 |
||
75393 | 319 |
def apply(symbol: Symbol, props: Properties.T): Entry = { |
75197 | 320 |
def err(msg: String): Nothing = error(msg + " for symbol " + quote(symbol)) |
321 |
||
322 |
val name = |
|
323 |
symbol match { case Name(a) => a case _ => err("Cannot determine name") } |
|
324 |
||
325 |
val argument = |
|
326 |
props match { |
|
327 |
case Argument(arg) => |
|
328 |
Symbol.Argument.unapply(arg) getOrElse |
|
329 |
error("Bad argument: " + quote(arg) + " for symbol " + quote(symbol)) |
|
330 |
case _ => Symbol.Argument.none |
|
331 |
} |
|
332 |
||
333 |
val code = |
|
334 |
props match { |
|
335 |
case Code(s) => |
|
336 |
try { |
|
337 |
val code = Integer.decode(s).intValue |
|
338 |
if (code >= 128) Some(code) else err("Illegal ASCII code") |
|
339 |
} |
|
340 |
catch { case _: NumberFormatException => err("Bad code") } |
|
341 |
case _ => None |
|
342 |
} |
|
343 |
||
78592 | 344 |
val groups = |
345 |
proper_list(for (case (Group.name, a) <- props) yield a).getOrElse(List("unsorted")) |
|
75197 | 346 |
|
78592 | 347 |
val abbrevs = for (case (Abbrev.name, a) <- props) yield a |
75197 | 348 |
|
349 |
new Entry(symbol, name, argument, code, Font.unapply(props), groups, abbrevs) |
|
350 |
} |
|
351 |
} |
|
352 |
||
353 |
class Entry private( |
|
354 |
val symbol: Symbol, |
|
355 |
val name: String, |
|
78599 | 356 |
val argument: Symbol.Argument, |
75197 | 357 |
val code: Option[Int], |
358 |
val font: Option[String], |
|
359 |
val groups: List[String], |
|
75393 | 360 |
val abbrevs: List[String] |
361 |
) { |
|
75197 | 362 |
override def toString: String = symbol |
363 |
||
364 |
val decode: Option[String] = |
|
365 |
code.map(c => new String(Character.toChars(c))) |
|
75194 | 366 |
} |
367 |
||
78938
7774e1372476
clarified loading of symbols: permissive entries in $ISABELLE_SYMBOLS require explicit "?";
wenzelm
parents:
78599
diff
changeset
|
368 |
lazy val symbols: Symbols = |
7774e1372476
clarified loading of symbols: permissive entries in $ISABELLE_SYMBOLS require explicit "?";
wenzelm
parents:
78599
diff
changeset
|
369 |
Symbols.make(cat_lines(Symbols.files().map(File.read))) |
75194 | 370 |
|
75393 | 371 |
object Symbols { |
78938
7774e1372476
clarified loading of symbols: permissive entries in $ISABELLE_SYMBOLS require explicit "?";
wenzelm
parents:
78599
diff
changeset
|
372 |
def files(): List[Path] = |
7774e1372476
clarified loading of symbols: permissive entries in $ISABELLE_SYMBOLS require explicit "?";
wenzelm
parents:
78599
diff
changeset
|
373 |
Path.split_permissive_files(Isabelle_System.getenv("ISABELLE_SYMBOLS")) |
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
374 |
|
75393 | 375 |
def make(symbols_spec: String): Symbols = { |
75193 | 376 |
val No_Decl = new Regex("""(?xs) ^\s* (?: \#.* )? $ """) |
377 |
val Key = new Regex("""(?xs) (.+): """) |
|
31522 | 378 |
|
75393 | 379 |
def read_decl(decl: String): (Symbol, Properties.T) = { |
75193 | 380 |
def err() = error("Bad symbol declaration: " + decl) |
31522 | 381 |
|
75393 | 382 |
def read_props(props: List[String]): Properties.T = { |
75193 | 383 |
props match { |
384 |
case Nil => Nil |
|
385 |
case _ :: Nil => err() |
|
386 |
case Key(x) :: y :: rest => (x -> y.replace('\u2423', ' ')) :: read_props(rest) |
|
387 |
case _ => err() |
|
388 |
} |
|
389 |
} |
|
390 |
decl.split("\\s+").toList match { |
|
391 |
case sym :: props if sym.length > 1 && !is_malformed(sym) => |
|
392 |
(sym, read_props(props)) |
|
31522 | 393 |
case _ => err() |
394 |
} |
|
395 |
} |
|
396 |
||
75197 | 397 |
new Symbols( |
75193 | 398 |
split_lines(symbols_spec).reverse. |
75197 | 399 |
foldLeft((List.empty[Entry], Set.empty[Symbol])) { |
75193 | 400 |
case (res, No_Decl()) => res |
401 |
case ((list, known), decl) => |
|
402 |
val (sym, props) = read_decl(decl) |
|
403 |
if (known(sym)) (list, known) |
|
75197 | 404 |
else (Entry(sym, props) :: list, known + sym) |
405 |
}._1) |
|
75193 | 406 |
} |
407 |
} |
|
31522 | 408 |
|
75393 | 409 |
class Symbols(val entries: List[Entry]) { |
75197 | 410 |
override def toString: String = entries.mkString("Symbols(", ", ", ")") |
411 |
||
412 |
||
53400 | 413 |
/* basic properties */ |
414 |
||
75197 | 415 |
private val entries_map: Map[Symbol, Entry] = |
416 |
(for (entry <- entries.iterator) yield entry.symbol -> entry).toMap |
|
31651 | 417 |
|
75197 | 418 |
def get(sym: Symbol): Option[Entry] = entries_map.get(sym) |
419 |
def defined(sym: Symbol): Boolean = entries_map.isDefinedAt(sym) |
|
420 |
||
421 |
def defined_code(sym: Symbol): Boolean = |
|
422 |
get(sym) match { case Some(entry) => entry.code.isDefined case _ => false } |
|
31651 | 423 |
|
75197 | 424 |
val code_defined: Int => Boolean = |
425 |
(for (entry <- entries.iterator; code <- entry.code) yield code).toSet |
|
50136
a96bd08258a2
support for symbol groups, retaining original order of declarations;
wenzelm
parents:
48922
diff
changeset
|
426 |
|
75197 | 427 |
val groups_code: List[(String, List[Symbol])] = |
428 |
(for { |
|
429 |
entry <- entries.iterator if entry.code.isDefined |
|
430 |
group <- entry.groups.iterator |
|
431 |
} yield entry.symbol -> group) |
|
432 |
.toList.groupBy(_._2).toList.map({ case (g, list) => (g, list.map(_._1)) }).sortBy(_._1) |
|
75195 | 433 |
|
75199 | 434 |
def get_abbrevs(sym: Symbol): List[String] = |
435 |
get(sym) match { case Some(entry) => entry.abbrevs case _ => Nil } |
|
75195 | 436 |
|
43488 | 437 |
|
43490 | 438 |
/* recoding */ |
31522 | 439 |
|
75393 | 440 |
private val (decoder, encoder) = { |
31522 | 441 |
val mapping = |
75197 | 442 |
for (entry <- entries; s <- entry.decode) yield entry.symbol -> s |
61376
93224745477f
output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents:
61174
diff
changeset
|
443 |
(new Recoder(mapping), new Recoder(for ((x, y) <- mapping) yield (y, x))) |
31522 | 444 |
} |
27918 | 445 |
|
34098 | 446 |
def decode(text: String): String = decoder.recode(text) |
447 |
def encode(text: String): String = encoder.recode(text) |
|
34134 | 448 |
|
75198 | 449 |
private def recode_set(elems: Iterable[String]): Set[String] = |
450 |
(elems.iterator ++ elems.iterator.map(decode)).toSet |
|
43490 | 451 |
|
75198 | 452 |
private def recode_map[A](elems: Iterable[(String, A)]): Map[String, A] = |
453 |
(elems.iterator ++ elems.iterator.map({ case (sym, a) => (decode(sym), a) })).toMap |
|
43490 | 454 |
|
455 |
||
456 |
/* user fonts */ |
|
457 |
||
43696 | 458 |
val fonts: Map[Symbol, String] = |
75198 | 459 |
recode_map(for (entry <- entries; font <- entry.font) yield entry.symbol -> font) |
43490 | 460 |
|
75192
7d680dcd69b1
misc tuning, based on suggestions by IntelliJ IDEA;
wenzelm
parents:
73359
diff
changeset
|
461 |
val font_names: List[String] = fonts.iterator.map(_._2).toSet.toList |
7d680dcd69b1
misc tuning, based on suggestions by IntelliJ IDEA;
wenzelm
parents:
73359
diff
changeset
|
462 |
val font_index: Map[String, Int] = (font_names zip font_names.indices.toList).toMap |
43490 | 463 |
|
75195 | 464 |
def lookup_font(sym: Symbol): Option[Int] = fonts.get(sym).map(font_index(_)) |
465 |
||
34134 | 466 |
|
467 |
/* classification */ |
|
468 |
||
75198 | 469 |
val letters: Set[String] = recode_set(List( |
34134 | 470 |
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", |
471 |
"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", |
|
472 |
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", |
|
473 |
"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", |
|
474 |
||
475 |
"\\<A>", "\\<B>", "\\<C>", "\\<D>", "\\<E>", "\\<F>", "\\<G>", |
|
476 |
"\\<H>", "\\<I>", "\\<J>", "\\<K>", "\\<L>", "\\<M>", "\\<N>", |
|
477 |
"\\<O>", "\\<P>", "\\<Q>", "\\<R>", "\\<S>", "\\<T>", "\\<U>", |
|
478 |
"\\<V>", "\\<W>", "\\<X>", "\\<Y>", "\\<Z>", "\\<a>", "\\<b>", |
|
479 |
"\\<c>", "\\<d>", "\\<e>", "\\<f>", "\\<g>", "\\<h>", "\\<i>", |
|
480 |
"\\<j>", "\\<k>", "\\<l>", "\\<m>", "\\<n>", "\\<o>", "\\<p>", |
|
481 |
"\\<q>", "\\<r>", "\\<s>", "\\<t>", "\\<u>", "\\<v>", "\\<w>", |
|
482 |
"\\<x>", "\\<y>", "\\<z>", |
|
483 |
||
484 |
"\\<AA>", "\\<BB>", "\\<CC>", "\\<DD>", "\\<EE>", "\\<FF>", |
|
485 |
"\\<GG>", "\\<HH>", "\\<II>", "\\<JJ>", "\\<KK>", "\\<LL>", |
|
486 |
"\\<MM>", "\\<NN>", "\\<OO>", "\\<PP>", "\\<QQ>", "\\<RR>", |
|
487 |
"\\<SS>", "\\<TT>", "\\<UU>", "\\<VV>", "\\<WW>", "\\<XX>", |
|
488 |
"\\<YY>", "\\<ZZ>", "\\<aa>", "\\<bb>", "\\<cc>", "\\<dd>", |
|
489 |
"\\<ee>", "\\<ff>", "\\<gg>", "\\<hh>", "\\<ii>", "\\<jj>", |
|
490 |
"\\<kk>", "\\<ll>", "\\<mm>", "\\<nn>", "\\<oo>", "\\<pp>", |
|
491 |
"\\<qq>", "\\<rr>", "\\<ss>", "\\<tt>", "\\<uu>", "\\<vv>", |
|
492 |
"\\<ww>", "\\<xx>", "\\<yy>", "\\<zz>", |
|
493 |
||
494 |
"\\<alpha>", "\\<beta>", "\\<gamma>", "\\<delta>", "\\<epsilon>", |
|
495 |
"\\<zeta>", "\\<eta>", "\\<theta>", "\\<iota>", "\\<kappa>", |
|
496 |
"\\<mu>", "\\<nu>", "\\<xi>", "\\<pi>", "\\<rho>", "\\<sigma>", |
|
497 |
"\\<tau>", "\\<upsilon>", "\\<phi>", "\\<chi>", "\\<psi>", |
|
498 |
"\\<omega>", "\\<Gamma>", "\\<Delta>", "\\<Theta>", "\\<Lambda>", |
|
499 |
"\\<Xi>", "\\<Pi>", "\\<Sigma>", "\\<Upsilon>", "\\<Phi>", |
|
75198 | 500 |
"\\<Psi>", "\\<Omega>")) |
34134 | 501 |
|
75198 | 502 |
val blanks: Set[String] = recode_set(List(space, "\t", "\n", "\u000B", "\f", "\r", "\r\n")) |
34138 | 503 |
|
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
504 |
val sym_chars = |
34138 | 505 |
Set("!", "#", "$", "%", "&", "*", "+", "-", "/", "<", "=", ">", "?", "@", "^", "_", "|", "~") |
34134 | 506 |
|
75197 | 507 |
val symbolic: Set[String] = |
75198 | 508 |
recode_set(for (entry <- entries if raw_symbolic(entry.symbol)) yield entry.symbol) |
44992
aa34d2d049ce
refined Symbol.is_symbolic -- cover recoded versions as well;
wenzelm
parents:
44949
diff
changeset
|
509 |
|
43455 | 510 |
|
63528
0f39f59317c1
completion templates for commands involving "begin ... end" blocks;
wenzelm
parents:
62528
diff
changeset
|
511 |
/* misc symbols */ |
61579 | 512 |
|
75192
7d680dcd69b1
misc tuning, based on suggestions by IntelliJ IDEA;
wenzelm
parents:
73359
diff
changeset
|
513 |
val newline_decoded: Symbol = decode(newline) |
7d680dcd69b1
misc tuning, based on suggestions by IntelliJ IDEA;
wenzelm
parents:
73359
diff
changeset
|
514 |
val comment_decoded: Symbol = decode(comment) |
7d680dcd69b1
misc tuning, based on suggestions by IntelliJ IDEA;
wenzelm
parents:
73359
diff
changeset
|
515 |
val cancel_decoded: Symbol = decode(cancel) |
7d680dcd69b1
misc tuning, based on suggestions by IntelliJ IDEA;
wenzelm
parents:
73359
diff
changeset
|
516 |
val latex_decoded: Symbol = decode(latex) |
7d680dcd69b1
misc tuning, based on suggestions by IntelliJ IDEA;
wenzelm
parents:
73359
diff
changeset
|
517 |
val marker_decoded: Symbol = decode(marker) |
7d680dcd69b1
misc tuning, based on suggestions by IntelliJ IDEA;
wenzelm
parents:
73359
diff
changeset
|
518 |
val open_decoded: Symbol = decode(open) |
7d680dcd69b1
misc tuning, based on suggestions by IntelliJ IDEA;
wenzelm
parents:
73359
diff
changeset
|
519 |
val close_decoded: Symbol = decode(close) |
55033 | 520 |
|
521 |
||
43488 | 522 |
/* control symbols */ |
523 |
||
59107 | 524 |
val control_decoded: Set[Symbol] = |
75197 | 525 |
(for (entry <- entries.iterator if entry.symbol.startsWith("\\<^"); s <- entry.decode) |
526 |
yield s).toSet |
|
43488 | 527 |
|
75192
7d680dcd69b1
misc tuning, based on suggestions by IntelliJ IDEA;
wenzelm
parents:
73359
diff
changeset
|
528 |
val sub_decoded: Symbol = decode(sub) |
7d680dcd69b1
misc tuning, based on suggestions by IntelliJ IDEA;
wenzelm
parents:
73359
diff
changeset
|
529 |
val sup_decoded: Symbol = decode(sup) |
7d680dcd69b1
misc tuning, based on suggestions by IntelliJ IDEA;
wenzelm
parents:
73359
diff
changeset
|
530 |
val bold_decoded: Symbol = decode(bold) |
7d680dcd69b1
misc tuning, based on suggestions by IntelliJ IDEA;
wenzelm
parents:
73359
diff
changeset
|
531 |
val emph_decoded: Symbol = decode(emph) |
7d680dcd69b1
misc tuning, based on suggestions by IntelliJ IDEA;
wenzelm
parents:
73359
diff
changeset
|
532 |
val bsub_decoded: Symbol = decode(bsub) |
7d680dcd69b1
misc tuning, based on suggestions by IntelliJ IDEA;
wenzelm
parents:
73359
diff
changeset
|
533 |
val esub_decoded: Symbol = decode(esub) |
7d680dcd69b1
misc tuning, based on suggestions by IntelliJ IDEA;
wenzelm
parents:
73359
diff
changeset
|
534 |
val bsup_decoded: Symbol = decode(bsup) |
7d680dcd69b1
misc tuning, based on suggestions by IntelliJ IDEA;
wenzelm
parents:
73359
diff
changeset
|
535 |
val esup_decoded: Symbol = decode(esup) |
27918 | 536 |
} |
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
537 |
|
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
538 |
|
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
539 |
/* tables */ |
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
540 |
|
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
541 |
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
|
542 |
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
|
543 |
|
73030 | 544 |
def decode_yxml(text: String, cache: XML.Cache = XML.Cache.none): XML.Body = |
80480
972f7a4cdc0e
clarified YXML.Source: more direct support for String and Bytes, instead of CharSequence;
wenzelm
parents:
80447
diff
changeset
|
545 |
YXML.parse_body(YXML.Source(text), recode = decode, cache = cache) |
73030 | 546 |
|
547 |
def decode_yxml_failsafe(text: String, cache: XML.Cache = XML.Cache.none): XML.Body = |
|
80480
972f7a4cdc0e
clarified YXML.Source: more direct support for String and Bytes, instead of CharSequence;
wenzelm
parents:
80447
diff
changeset
|
548 |
YXML.parse_body_failsafe(YXML.Source(text), recode = decode, cache = cache) |
73030 | 549 |
|
80437
2c07b9b2f9f4
minor performance tuning: more direct Bytes with Symbol.encode;
wenzelm
parents:
78938
diff
changeset
|
550 |
def encode_yxml(body: XML.Body): String = |
2c07b9b2f9f4
minor performance tuning: more direct Bytes with Symbol.encode;
wenzelm
parents:
78938
diff
changeset
|
551 |
YXML.string_of_body(body, recode = encode) |
53337
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53316
diff
changeset
|
552 |
|
75393 | 553 |
def decode_strict(text: String): String = { |
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
|
554 |
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
|
555 |
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
|
556 |
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
|
557 |
val bad = new mutable.ListBuffer[Symbol] |
80444 | 558 |
for (s <- iterator(text) if encode(decode(s)) != s && !bad.contains(s)) bad += s |
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
|
559 |
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
|
560 |
} |
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
|
561 |
} |
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
|
562 |
|
72866 | 563 |
def output(unicode_symbols: Boolean, text: String): String = |
564 |
if (unicode_symbols) Symbol.decode(text) else Symbol.encode(text) |
|
565 |
||
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
566 |
|
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
567 |
/* classification */ |
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
568 |
|
43696 | 569 |
def is_letter(sym: Symbol): Boolean = symbols.letters.contains(sym) |
570 |
def is_digit(sym: Symbol): Boolean = sym.length == 1 && '0' <= sym(0) && sym(0) <= '9' |
|
571 |
def is_quasi(sym: Symbol): Boolean = sym == "_" || sym == "'" |
|
572 |
def is_letdig(sym: Symbol): Boolean = is_letter(sym) || is_digit(sym) || is_quasi(sym) |
|
573 |
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
|
574 |
|
55033 | 575 |
|
67438 | 576 |
/* symbolic newline */ |
63528
0f39f59317c1
completion templates for commands involving "begin ... end" blocks;
wenzelm
parents:
62528
diff
changeset
|
577 |
|
0f39f59317c1
completion templates for commands involving "begin ... end" blocks;
wenzelm
parents:
62528
diff
changeset
|
578 |
val newline: Symbol = "\\<newline>" |
0f39f59317c1
completion templates for commands involving "begin ... end" blocks;
wenzelm
parents:
62528
diff
changeset
|
579 |
def newline_decoded: Symbol = symbols.newline_decoded |
0f39f59317c1
completion templates for commands involving "begin ... end" blocks;
wenzelm
parents:
62528
diff
changeset
|
580 |
|
0f39f59317c1
completion templates for commands involving "begin ... end" blocks;
wenzelm
parents:
62528
diff
changeset
|
581 |
def print_newlines(str: String): String = |
0f39f59317c1
completion templates for commands involving "begin ... end" blocks;
wenzelm
parents:
62528
diff
changeset
|
582 |
if (str.contains('\n')) |
0f39f59317c1
completion templates for commands involving "begin ... end" blocks;
wenzelm
parents:
62528
diff
changeset
|
583 |
(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
|
584 |
else str |
61579 | 585 |
|
67438 | 586 |
|
587 |
/* formal comments */ |
|
588 |
||
61579 | 589 |
val comment: Symbol = "\\<comment>" |
67449 | 590 |
val cancel: Symbol = "\\<^cancel>" |
591 |
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
|
592 |
val marker: Symbol = "\\<^marker>" |
67449 | 593 |
|
61579 | 594 |
def comment_decoded: Symbol = symbols.comment_decoded |
67438 | 595 |
def cancel_decoded: Symbol = symbols.cancel_decoded |
596 |
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
|
597 |
def marker_decoded: Symbol = symbols.marker_decoded |
61579 | 598 |
|
599 |
||
55033 | 600 |
/* cartouches */ |
601 |
||
61579 | 602 |
val open: Symbol = "\\<open>" |
603 |
val close: Symbol = "\\<close>" |
|
55033 | 604 |
|
605 |
def open_decoded: Symbol = symbols.open_decoded |
|
606 |
def close_decoded: Symbol = symbols.close_decoded |
|
607 |
||
608 |
def is_open(sym: Symbol): Boolean = sym == open_decoded || sym == open |
|
609 |
def is_close(sym: Symbol): Boolean = sym == close_decoded || sym == close |
|
610 |
||
67131 | 611 |
def cartouche(s: String): String = open + s + close |
612 |
def cartouche_decoded(s: String): String = open_decoded + s + close_decoded |
|
613 |
||
55033 | 614 |
|
615 |
/* symbols for symbolic identifiers */ |
|
44992
aa34d2d049ce
refined Symbol.is_symbolic -- cover recoded versions as well;
wenzelm
parents:
44949
diff
changeset
|
616 |
|
aa34d2d049ce
refined Symbol.is_symbolic -- cover recoded versions as well;
wenzelm
parents:
44949
diff
changeset
|
617 |
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
|
618 |
sym.startsWith("\\<") && sym.endsWith(">") && !sym.startsWith("\\<^") |
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
619 |
|
55033 | 620 |
def is_symbolic(sym: Symbol): Boolean = |
621 |
!is_open(sym) && !is_close(sym) && (raw_symbolic(sym) || symbols.symbolic.contains(sym)) |
|
622 |
||
623 |
def is_symbolic_char(sym: Symbol): Boolean = symbols.sym_chars.contains(sym) |
|
624 |
||
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
625 |
|
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
626 |
/* control symbols */ |
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
627 |
|
67127
cf111622c9f8
font style for literal control symbols, notably for antiquotations;
wenzelm
parents:
66919
diff
changeset
|
628 |
val control_prefix = "\\<^" |
cf111622c9f8
font style for literal control symbols, notably for antiquotations;
wenzelm
parents:
66919
diff
changeset
|
629 |
val control_suffix = ">" |
cf111622c9f8
font style for literal control symbols, notably for antiquotations;
wenzelm
parents:
66919
diff
changeset
|
630 |
|
67255
f1f983484878
HTML rendering of \<^control> as in Isabelle/jEdit;
wenzelm
parents:
67131
diff
changeset
|
631 |
def control_name(sym: Symbol): Option[String] = |
f1f983484878
HTML rendering of \<^control> as in Isabelle/jEdit;
wenzelm
parents:
67131
diff
changeset
|
632 |
if (is_control_encoded(sym)) |
f1f983484878
HTML rendering of \<^control> as in Isabelle/jEdit;
wenzelm
parents:
67131
diff
changeset
|
633 |
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
|
634 |
else None |
f1f983484878
HTML rendering of \<^control> as in Isabelle/jEdit;
wenzelm
parents:
67131
diff
changeset
|
635 |
|
67127
cf111622c9f8
font style for literal control symbols, notably for antiquotations;
wenzelm
parents:
66919
diff
changeset
|
636 |
def is_control_encoded(sym: Symbol): Boolean = |
cf111622c9f8
font style for literal control symbols, notably for antiquotations;
wenzelm
parents:
66919
diff
changeset
|
637 |
sym.startsWith(control_prefix) && sym.endsWith(control_suffix) |
cf111622c9f8
font style for literal control symbols, notably for antiquotations;
wenzelm
parents:
66919
diff
changeset
|
638 |
|
59107 | 639 |
def is_control(sym: Symbol): Boolean = |
67127
cf111622c9f8
font style for literal control symbols, notably for antiquotations;
wenzelm
parents:
66919
diff
changeset
|
640 |
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
|
641 |
|
43696 | 642 |
def is_controllable(sym: Symbol): Boolean = |
66006
cec184536dfd
uniform notion of Symbol.is_controllable (see also 265d9300d523);
wenzelm
parents:
65997
diff
changeset
|
643 |
!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
|
644 |
!is_malformed(sym) && sym != "\"" |
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
645 |
|
73208 | 646 |
val sub: Symbol = "\\<^sub>" |
647 |
val sup: Symbol = "\\<^sup>" |
|
648 |
val bold: Symbol = "\\<^bold>" |
|
649 |
val emph: Symbol = "\\<^emph>" |
|
650 |
val bsub: Symbol = "\\<^bsub>" |
|
651 |
val esub: Symbol = "\\<^esub>" |
|
652 |
val bsup: Symbol = "\\<^bsup>" |
|
653 |
val esup: Symbol = "\\<^esup>" |
|
62103 | 654 |
|
44238
36120feb70ed
some convenience actions/shortcuts for control symbols;
wenzelm
parents:
44181
diff
changeset
|
655 |
def sub_decoded: Symbol = symbols.sub_decoded |
36120feb70ed
some convenience actions/shortcuts for control symbols;
wenzelm
parents:
44181
diff
changeset
|
656 |
def sup_decoded: Symbol = symbols.sup_decoded |
62103 | 657 |
def bold_decoded: Symbol = symbols.bold_decoded |
658 |
def emph_decoded: Symbol = symbols.emph_decoded |
|
44238
36120feb70ed
some convenience actions/shortcuts for control symbols;
wenzelm
parents:
44181
diff
changeset
|
659 |
def bsub_decoded: Symbol = symbols.bsub_decoded |
36120feb70ed
some convenience actions/shortcuts for control symbols;
wenzelm
parents:
44181
diff
changeset
|
660 |
def esub_decoded: Symbol = symbols.esub_decoded |
36120feb70ed
some convenience actions/shortcuts for control symbols;
wenzelm
parents:
44181
diff
changeset
|
661 |
def bsup_decoded: Symbol = symbols.bsup_decoded |
36120feb70ed
some convenience actions/shortcuts for control symbols;
wenzelm
parents:
44181
diff
changeset
|
662 |
def esup_decoded: Symbol = symbols.esup_decoded |
71649 | 663 |
|
664 |
||
665 |
/* metric */ |
|
666 |
||
667 |
def is_printable(sym: Symbol): Boolean = |
|
668 |
if (is_ascii(sym)) is_ascii_printable(sym(0)) |
|
669 |
else !is_control(sym) |
|
670 |
||
75393 | 671 |
object Metric extends Pretty.Metric { |
71649 | 672 |
val unit = 1.0 |
673 |
def apply(str: String): Double = |
|
674 |
(for (s <- iterator(str)) yield { |
|
675 |
val sym = encode(s) |
|
676 |
if (sym.startsWith("\\<long") || sym.startsWith("\\<Long")) 2 |
|
677 |
else if (is_printable(sym)) 1 |
|
678 |
else 0 |
|
679 |
}).sum |
|
680 |
} |
|
27901 | 681 |
} |