author | wenzelm |
Thu, 07 Jul 2011 14:10:50 +0200 | |
changeset 43696 | 58bb7ca5c7a2 |
parent 43695 | 5130dfe1b7be |
child 43714 | 3749d1e6dde9 |
permissions | -rw-r--r-- |
27901 | 1 |
/* Title: Pure/General/symbol.scala |
2 |
Author: Makarius |
|
3 |
||
27924 | 4 |
Detecting and recoding Isabelle symbols. |
27901 | 5 |
*/ |
6 |
||
7 |
package isabelle |
|
8 |
||
27918 | 9 |
import scala.io.Source |
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 |
27901 | 12 |
|
13 |
||
31522 | 14 |
object Symbol |
15 |
{ |
|
43696 | 16 |
type Symbol = String |
17 |
||
18 |
||
36763 | 19 |
/* spaces */ |
20 |
||
36816 | 21 |
val spc = ' ' |
22 |
val space = " " |
|
23 |
||
24 |
private val static_spaces = space * 4000 |
|
36763 | 25 |
|
26 |
def spaces(k: Int): String = |
|
27 |
{ |
|
28 |
require(k >= 0) |
|
29 |
if (k < static_spaces.length) static_spaces.substring(0, k) |
|
36816 | 30 |
else space * k |
36763 | 31 |
} |
32 |
||
33 |
||
43418 | 34 |
/* ASCII characters */ |
35 |
||
36 |
def is_ascii_letter(c: Char): Boolean = 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' |
|
37 |
def is_ascii_digit(c: Char): Boolean = '0' <= c && c <= '9' |
|
38 |
def is_ascii_quasi(c: Char): Boolean = c == '_' || c == '\'' |
|
39 |
||
40 |
def is_ascii_letdig(c: Char): Boolean = |
|
41 |
is_ascii_letter(c) || is_ascii_digit(c) || is_ascii_quasi(c) |
|
42 |
||
43 |
def is_ascii_identifier(s: String): Boolean = |
|
44 |
s.length > 0 && is_ascii_letter(s(0)) && s.substring(1).forall(is_ascii_letdig) |
|
45 |
||
46 |
||
33998 | 47 |
/* Symbol regexps */ |
27901 | 48 |
|
31522 | 49 |
private val plain = new Regex("""(?xs) |
40524
6131d7a78ad3
treat Unicode "replacement character" (i.e. decoding error) is malformed;
wenzelm
parents:
40523
diff
changeset
|
50 |
[^\r\\\ud800-\udfff\ufffd] | [\ud800-\udbff][\udc00-\udfff] """) |
37556
2bf29095d26f
treat alternative newline symbols as in Isabelle/ML;
wenzelm
parents:
36816
diff
changeset
|
51 |
|
40522 | 52 |
private val physical_newline = new Regex("""(?xs) \n | \r\n | \r """) |
27901 | 53 |
|
31522 | 54 |
private val symbol = new Regex("""(?xs) |
31545
5f1f0a20af4d
discontinued escaped symbols such as \\<forall> -- only one backslash should be used;
wenzelm
parents:
31523
diff
changeset
|
55 |
\\ < (?: |
27924 | 56 |
\^? [A-Za-z][A-Za-z0-9_']* | |
57 |
\^raw: [\x20-\x7e\u0100-\uffff && [^.>]]* ) >""") |
|
58 |
||
40523
1050315f6ee2
simplified/robustified treatment of malformed symbols, which are now fully internalized (total Symbol.explode etc.);
wenzelm
parents:
40522
diff
changeset
|
59 |
private val malformed_symbol = new Regex("(?xs) (?!" + symbol + ")" + |
40529 | 60 |
""" [\ud800-\udbff\ufffd] | \\<\^? """) |
27924 | 61 |
|
40523
1050315f6ee2
simplified/robustified treatment of malformed symbols, which are now fully internalized (total Symbol.explode etc.);
wenzelm
parents:
40522
diff
changeset
|
62 |
val regex_total = |
1050315f6ee2
simplified/robustified treatment of malformed symbols, which are now fully internalized (total Symbol.explode etc.);
wenzelm
parents:
40522
diff
changeset
|
63 |
new Regex(plain + "|" + physical_newline + "|" + symbol + "|" + malformed_symbol + "| .") |
27937
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
64 |
|
34137 | 65 |
|
66 |
/* basic matching */ |
|
67 |
||
37556
2bf29095d26f
treat alternative newline symbols as in Isabelle/ML;
wenzelm
parents:
36816
diff
changeset
|
68 |
def is_plain(c: Char): Boolean = !(c == '\r' || c == '\\' || '\ud800' <= c && c <= '\udfff') |
34137 | 69 |
|
43696 | 70 |
def is_physical_newline(s: Symbol): Boolean = |
43675
8252d51d70e2
simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents:
43511
diff
changeset
|
71 |
s == "\n" || s == "\r" || s == "\r\n" |
38877 | 72 |
|
43696 | 73 |
def is_malformed(s: Symbol): Boolean = |
43675
8252d51d70e2
simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents:
43511
diff
changeset
|
74 |
!(s.length == 1 && is_plain(s(0))) && malformed_symbol.pattern.matcher(s).matches |
34137 | 75 |
|
76 |
class Matcher(text: CharSequence) |
|
77 |
{ |
|
40523
1050315f6ee2
simplified/robustified treatment of malformed symbols, which are now fully internalized (total Symbol.explode etc.);
wenzelm
parents:
40522
diff
changeset
|
78 |
private val matcher = regex_total.pattern.matcher(text) |
34137 | 79 |
def apply(start: Int, end: Int): Int = |
80 |
{ |
|
81 |
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
|
82 |
if (is_plain(text.charAt(start))) 1 |
34138 | 83 |
else { |
34137 | 84 |
matcher.region(start, end).lookingAt |
85 |
matcher.group.length |
|
86 |
} |
|
87 |
} |
|
31522 | 88 |
} |
27937
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
89 |
|
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
90 |
|
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
91 |
/* iterator */ |
33998 | 92 |
|
43696 | 93 |
private val char_symbols: Array[Symbol] = |
43675
8252d51d70e2
simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents:
43511
diff
changeset
|
94 |
(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
|
95 |
|
43696 | 96 |
def iterator(text: CharSequence): Iterator[Symbol] = |
97 |
new Iterator[Symbol] |
|
40522 | 98 |
{ |
43489 | 99 |
private val matcher = new Matcher(text) |
100 |
private var i = 0 |
|
101 |
def hasNext = i < text.length |
|
102 |
def next = |
|
103 |
{ |
|
104 |
val n = matcher(i, text.length) |
|
43675
8252d51d70e2
simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents:
43511
diff
changeset
|
105 |
val s = |
8252d51d70e2
simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents:
43511
diff
changeset
|
106 |
if (n == 0) "" |
8252d51d70e2
simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents:
43511
diff
changeset
|
107 |
else if (n == 1) { |
8252d51d70e2
simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents:
43511
diff
changeset
|
108 |
val c = text.charAt(i) |
8252d51d70e2
simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents:
43511
diff
changeset
|
109 |
if (c < char_symbols.length) char_symbols(c) |
8252d51d70e2
simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents:
43511
diff
changeset
|
110 |
else text.subSequence(i, i + n).toString |
8252d51d70e2
simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents:
43511
diff
changeset
|
111 |
} |
8252d51d70e2
simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents:
43511
diff
changeset
|
112 |
else text.subSequence(i, i + n).toString |
43489 | 113 |
i += n |
114 |
s |
|
115 |
} |
|
33998 | 116 |
} |
43489 | 117 |
|
33998 | 118 |
|
119 |
/* decoding offsets */ |
|
120 |
||
121 |
class Index(text: CharSequence) |
|
31929 | 122 |
{ |
123 |
case class Entry(chr: Int, sym: Int) |
|
124 |
val index: Array[Entry] = |
|
125 |
{ |
|
34137 | 126 |
val matcher = new Matcher(text) |
31929 | 127 |
val buf = new mutable.ArrayBuffer[Entry] |
128 |
var chr = 0 |
|
129 |
var sym = 0 |
|
33998 | 130 |
while (chr < text.length) { |
34137 | 131 |
val n = matcher(chr, text.length) |
132 |
chr += n |
|
31929 | 133 |
sym += 1 |
34137 | 134 |
if (n > 1) buf += Entry(chr, sym) |
31929 | 135 |
} |
136 |
buf.toArray |
|
137 |
} |
|
38479
e628da370072
more efficient Markup_Tree, based on branches sorted by quasi-order;
wenzelm
parents:
37556
diff
changeset
|
138 |
def decode(sym1: Int): Int = |
31929 | 139 |
{ |
38479
e628da370072
more efficient Markup_Tree, based on branches sorted by quasi-order;
wenzelm
parents:
37556
diff
changeset
|
140 |
val sym = sym1 - 1 |
31929 | 141 |
val end = index.length |
142 |
def bisect(a: Int, b: Int): Int = |
|
143 |
{ |
|
144 |
if (a < b) { |
|
145 |
val c = (a + b) / 2 |
|
146 |
if (sym < index(c).sym) bisect(a, c) |
|
147 |
else if (c + 1 == end || sym < index(c + 1).sym) c |
|
148 |
else bisect(c + 1, b) |
|
149 |
} |
|
150 |
else -1 |
|
151 |
} |
|
152 |
val i = bisect(0, end) |
|
153 |
if (i < 0) sym |
|
154 |
else index(i).chr + sym - index(i).sym |
|
155 |
} |
|
38479
e628da370072
more efficient Markup_Tree, based on branches sorted by quasi-order;
wenzelm
parents:
37556
diff
changeset
|
156 |
def decode(range: Text.Range): Text.Range = range.map(decode(_)) |
31929 | 157 |
} |
158 |
||
159 |
||
33998 | 160 |
/* recoding text */ |
27937
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
161 |
|
31522 | 162 |
private class Recoder(list: List[(String, String)]) |
163 |
{ |
|
164 |
private val (min, max) = |
|
165 |
{ |
|
27937
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
166 |
var min = '\uffff' |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
167 |
var max = '\u0000' |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
168 |
for ((x, _) <- list) { |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
169 |
val c = x(0) |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
170 |
if (c < min) min = c |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
171 |
if (c > max) max = c |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
172 |
} |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
173 |
(min, max) |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
174 |
} |
40443 | 175 |
private val table = |
176 |
{ |
|
177 |
var tab = Map[String, String]() |
|
178 |
for ((x, y) <- list) { |
|
179 |
tab.get(x) match { |
|
180 |
case None => tab += (x -> y) |
|
181 |
case Some(z) => |
|
182 |
error("Duplicate mapping of \"" + x + "\" to \"" + y + "\" vs. \"" + z + "\"") |
|
183 |
} |
|
184 |
} |
|
185 |
tab |
|
186 |
} |
|
31522 | 187 |
def recode(text: String): String = |
188 |
{ |
|
27937
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
189 |
val len = text.length |
40523
1050315f6ee2
simplified/robustified treatment of malformed symbols, which are now fully internalized (total Symbol.explode etc.);
wenzelm
parents:
40522
diff
changeset
|
190 |
val matcher = regex_total.pattern.matcher(text) |
27937
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
191 |
val result = new StringBuilder(len) |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
192 |
var i = 0 |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
193 |
while (i < len) { |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
194 |
val c = text(i) |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
195 |
if (min <= c && c <= max) { |
31929 | 196 |
matcher.region(i, len).lookingAt |
27938 | 197 |
val x = matcher.group |
31522 | 198 |
result.append(table.get(x) getOrElse x) |
27937
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
199 |
i = matcher.end |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
200 |
} |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
201 |
else { result.append(c); i += 1 } |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
202 |
} |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
203 |
result.toString |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
204 |
} |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
205 |
} |
27924 | 206 |
|
27918 | 207 |
|
27923
7ebe9d38743a
use scala.collection.jcl.HashMap, which seems to be more efficient;
wenzelm
parents:
27918
diff
changeset
|
208 |
|
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
209 |
/** symbol interpretation **/ |
27927 | 210 |
|
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
211 |
private lazy val symbols = |
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
212 |
new Interpretation( |
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
213 |
Isabelle_System.try_read(Path.split(Isabelle_System.getenv_strict("ISABELLE_SYMBOLS")))) |
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
214 |
|
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
215 |
private class Interpretation(symbols_spec: String) |
29569
f3f529b5d8fb
more general init of Symbol.Interpretation, independent of IsabelleSystem instance;
wenzelm
parents:
29174
diff
changeset
|
216 |
{ |
31522 | 217 |
/* read symbols */ |
218 |
||
219 |
private val empty = new Regex("""(?xs) ^\s* (?: \#.* )? $ """) |
|
220 |
private val key = new Regex("""(?xs) (.+): """) |
|
221 |
||
43696 | 222 |
private def read_decl(decl: String): (Symbol, Map[String, String]) = |
31522 | 223 |
{ |
224 |
def err() = error("Bad symbol declaration: " + decl) |
|
225 |
||
226 |
def read_props(props: List[String]): Map[String, String] = |
|
227 |
{ |
|
228 |
props match { |
|
229 |
case Nil => Map() |
|
230 |
case _ :: Nil => err() |
|
231 |
case key(x) :: y :: rest => read_props(rest) + (x -> y) |
|
232 |
case _ => err() |
|
233 |
} |
|
234 |
} |
|
235 |
decl.split("\\s+").toList match { |
|
40523
1050315f6ee2
simplified/robustified treatment of malformed symbols, which are now fully internalized (total Symbol.explode etc.);
wenzelm
parents:
40522
diff
changeset
|
236 |
case sym :: props if sym.length > 1 && !is_malformed(sym) => (sym, read_props(props)) |
34193 | 237 |
case _ => err() |
31522 | 238 |
} |
239 |
} |
|
240 |
||
43696 | 241 |
private val symbols: List[(Symbol, Map[String, String])] = |
40443 | 242 |
Map(( |
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
243 |
for (decl <- split_lines(symbols_spec) if !empty.pattern.matcher(decl).matches) |
40443 | 244 |
yield read_decl(decl)): _*) toList |
31522 | 245 |
|
246 |
||
31651 | 247 |
/* misc properties */ |
248 |
||
43696 | 249 |
val names: Map[Symbol, String] = |
34134 | 250 |
{ |
43456
8a6de1a6e1dc
names for control symbols without "^", which is relevant for completion;
wenzelm
parents:
43455
diff
changeset
|
251 |
val name = new Regex("""\\<\^?([A-Za-z][A-Za-z0-9_']*)>""") |
31651 | 252 |
Map((for ((sym @ name(a), _) <- symbols) yield (sym -> a)): _*) |
253 |
} |
|
254 |
||
43696 | 255 |
val abbrevs: Map[Symbol, String] = |
43488 | 256 |
Map(( |
257 |
for ((sym, props) <- symbols if props.isDefinedAt("abbrev")) |
|
258 |
yield (sym -> props("abbrev"))): _*) |
|
259 |
||
260 |
||
43490 | 261 |
/* recoding */ |
31522 | 262 |
|
263 |
private val (decoder, encoder) = |
|
264 |
{ |
|
265 |
val mapping = |
|
266 |
for { |
|
267 |
(sym, props) <- symbols |
|
268 |
val code = |
|
269 |
try { Integer.decode(props("code")).intValue } |
|
270 |
catch { |
|
271 |
case _: NoSuchElementException => error("Missing code for symbol " + sym) |
|
272 |
case _: NumberFormatException => error("Bad code for symbol " + sym) |
|
273 |
} |
|
274 |
val ch = new String(Character.toChars(code)) |
|
34193 | 275 |
} yield { |
276 |
if (code < 128) error("Illegal ASCII code for symbol " + sym) |
|
277 |
else (sym, ch) |
|
278 |
} |
|
31545
5f1f0a20af4d
discontinued escaped symbols such as \\<forall> -- only one backslash should be used;
wenzelm
parents:
31523
diff
changeset
|
279 |
(new Recoder(mapping), |
31548 | 280 |
new Recoder(mapping map { case (x, y) => (y, x) })) |
31522 | 281 |
} |
27918 | 282 |
|
34098 | 283 |
def decode(text: String): String = decoder.recode(text) |
284 |
def encode(text: String): String = encoder.recode(text) |
|
34134 | 285 |
|
43490 | 286 |
private def recode_set(elems: String*): Set[String] = |
287 |
{ |
|
288 |
val content = elems.toList |
|
289 |
Set((content ::: content.map(decode)): _*) |
|
290 |
} |
|
291 |
||
292 |
private def recode_map[A](elems: (String, A)*): Map[String, A] = |
|
293 |
{ |
|
294 |
val content = elems.toList |
|
295 |
Map((content ::: content.map({ case (sym, a) => (decode(sym), a) })): _*) |
|
296 |
} |
|
297 |
||
298 |
||
299 |
/* user fonts */ |
|
300 |
||
43696 | 301 |
val fonts: Map[Symbol, String] = |
43490 | 302 |
recode_map(( |
303 |
for ((sym, props) <- symbols if props.isDefinedAt("font")) |
|
304 |
yield (sym -> props("font"))): _*) |
|
305 |
||
306 |
val font_names: List[String] = Set(fonts.toList.map(_._2): _*).toList |
|
307 |
val font_index: Map[String, Int] = Map((font_names zip (0 until font_names.length).toList): _*) |
|
308 |
||
34134 | 309 |
|
310 |
/* classification */ |
|
311 |
||
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
312 |
val letters = recode_set( |
34134 | 313 |
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", |
314 |
"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", |
|
315 |
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", |
|
316 |
"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", |
|
317 |
||
318 |
"\\<A>", "\\<B>", "\\<C>", "\\<D>", "\\<E>", "\\<F>", "\\<G>", |
|
319 |
"\\<H>", "\\<I>", "\\<J>", "\\<K>", "\\<L>", "\\<M>", "\\<N>", |
|
320 |
"\\<O>", "\\<P>", "\\<Q>", "\\<R>", "\\<S>", "\\<T>", "\\<U>", |
|
321 |
"\\<V>", "\\<W>", "\\<X>", "\\<Y>", "\\<Z>", "\\<a>", "\\<b>", |
|
322 |
"\\<c>", "\\<d>", "\\<e>", "\\<f>", "\\<g>", "\\<h>", "\\<i>", |
|
323 |
"\\<j>", "\\<k>", "\\<l>", "\\<m>", "\\<n>", "\\<o>", "\\<p>", |
|
324 |
"\\<q>", "\\<r>", "\\<s>", "\\<t>", "\\<u>", "\\<v>", "\\<w>", |
|
325 |
"\\<x>", "\\<y>", "\\<z>", |
|
326 |
||
327 |
"\\<AA>", "\\<BB>", "\\<CC>", "\\<DD>", "\\<EE>", "\\<FF>", |
|
328 |
"\\<GG>", "\\<HH>", "\\<II>", "\\<JJ>", "\\<KK>", "\\<LL>", |
|
329 |
"\\<MM>", "\\<NN>", "\\<OO>", "\\<PP>", "\\<QQ>", "\\<RR>", |
|
330 |
"\\<SS>", "\\<TT>", "\\<UU>", "\\<VV>", "\\<WW>", "\\<XX>", |
|
331 |
"\\<YY>", "\\<ZZ>", "\\<aa>", "\\<bb>", "\\<cc>", "\\<dd>", |
|
332 |
"\\<ee>", "\\<ff>", "\\<gg>", "\\<hh>", "\\<ii>", "\\<jj>", |
|
333 |
"\\<kk>", "\\<ll>", "\\<mm>", "\\<nn>", "\\<oo>", "\\<pp>", |
|
334 |
"\\<qq>", "\\<rr>", "\\<ss>", "\\<tt>", "\\<uu>", "\\<vv>", |
|
335 |
"\\<ww>", "\\<xx>", "\\<yy>", "\\<zz>", |
|
336 |
||
337 |
"\\<alpha>", "\\<beta>", "\\<gamma>", "\\<delta>", "\\<epsilon>", |
|
338 |
"\\<zeta>", "\\<eta>", "\\<theta>", "\\<iota>", "\\<kappa>", |
|
339 |
"\\<mu>", "\\<nu>", "\\<xi>", "\\<pi>", "\\<rho>", "\\<sigma>", |
|
340 |
"\\<tau>", "\\<upsilon>", "\\<phi>", "\\<chi>", "\\<psi>", |
|
341 |
"\\<omega>", "\\<Gamma>", "\\<Delta>", "\\<Theta>", "\\<Lambda>", |
|
342 |
"\\<Xi>", "\\<Pi>", "\\<Sigma>", "\\<Upsilon>", "\\<Phi>", |
|
343 |
"\\<Psi>", "\\<Omega>", |
|
344 |
||
345 |
"\\<^isub>", "\\<^isup>") |
|
346 |
||
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
347 |
val blanks = |
43490 | 348 |
recode_set(space, "\t", "\n", "\u000B", "\f", "\r", "\\<spacespace>", "\\<^newline>") |
34138 | 349 |
|
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
350 |
val sym_chars = |
34138 | 351 |
Set("!", "#", "$", "%", "&", "*", "+", "-", "/", "<", "=", ">", "?", "@", "^", "_", "|", "~") |
34134 | 352 |
|
43455 | 353 |
|
43488 | 354 |
/* control symbols */ |
355 |
||
43696 | 356 |
val ctrl_decoded: Set[Symbol] = |
43488 | 357 |
Set((for ((sym, _) <- symbols if sym.startsWith("\\<^")) yield decode(sym)): _*) |
358 |
||
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
359 |
val subscript_decoded = Set(decode("\\<^sub>"), decode("\\<^isub>")) |
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
360 |
val superscript_decoded = Set(decode("\\<^sup>"), decode("\\<^isup>")) |
43511 | 361 |
|
362 |
val bold_decoded = decode("\\<^bold>") |
|
363 |
val bsub_decoded = decode("\\<^bsub>") |
|
364 |
val esub_decoded = decode("\\<^esub>") |
|
365 |
val bsup_decoded = decode("\\<^bsup>") |
|
366 |
val esup_decoded = decode("\\<^esup>") |
|
27918 | 367 |
} |
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
368 |
|
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
369 |
|
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
370 |
/* tables */ |
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
371 |
|
43696 | 372 |
def names: Map[Symbol, String] = symbols.names |
373 |
def abbrevs: Map[Symbol, String] = symbols.abbrevs |
|
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
374 |
|
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
375 |
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
|
376 |
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
|
377 |
|
43696 | 378 |
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
|
379 |
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
|
380 |
def font_index: Map[String, Int] = symbols.font_index |
43696 | 381 |
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
|
382 |
|
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
383 |
|
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
384 |
/* classification */ |
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
385 |
|
43696 | 386 |
def is_letter(sym: Symbol): Boolean = symbols.letters.contains(sym) |
387 |
def is_digit(sym: Symbol): Boolean = sym.length == 1 && '0' <= sym(0) && sym(0) <= '9' |
|
388 |
def is_quasi(sym: Symbol): Boolean = sym == "_" || sym == "'" |
|
389 |
def is_letdig(sym: Symbol): Boolean = is_letter(sym) || is_digit(sym) || is_quasi(sym) |
|
390 |
def is_blank(sym: Symbol): Boolean = symbols.blanks.contains(sym) |
|
391 |
def is_symbolic_char(sym: Symbol): Boolean = symbols.sym_chars.contains(sym) |
|
392 |
def is_symbolic(sym: Symbol): Boolean = |
|
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
393 |
sym.startsWith("\\<") && sym.endsWith(">") && !sym.startsWith("\\<^") |
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
394 |
|
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
395 |
|
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
396 |
/* control symbols */ |
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
397 |
|
43696 | 398 |
def is_ctrl(sym: Symbol): Boolean = |
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
399 |
sym.startsWith("\\<^") || symbols.ctrl_decoded.contains(sym) |
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
400 |
|
43696 | 401 |
def is_controllable(sym: Symbol): Boolean = |
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
402 |
!is_blank(sym) && !is_ctrl(sym) && !is_malformed(sym) |
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
403 |
|
43696 | 404 |
def is_subscript_decoded(sym: Symbol): Boolean = symbols.subscript_decoded.contains(sym) |
405 |
def is_superscript_decoded(sym: Symbol): Boolean = symbols.superscript_decoded.contains(sym) |
|
406 |
def is_bold_decoded(sym: Symbol): Boolean = sym == symbols.bold_decoded |
|
407 |
def is_bsub_decoded(sym: Symbol): Boolean = sym == symbols.bsub_decoded |
|
408 |
def is_esub_decoded(sym: Symbol): Boolean = sym == symbols.esub_decoded |
|
409 |
def is_bsup_decoded(sym: Symbol): Boolean = sym == symbols.bsup_decoded |
|
410 |
def is_esup_decoded(sym: Symbol): Boolean = sym == symbols.esup_decoded |
|
27901 | 411 |
} |