author | wenzelm |
Sat, 22 Feb 2014 20:52:43 +0100 | |
changeset 55672 | 5e25cc741ab9 |
parent 55618 | 995162143ef4 |
child 55884 | f2c0eaedd579 |
permissions | -rw-r--r-- |
27901 | 1 |
/* Title: Pure/General/symbol.scala |
2 |
Author: Makarius |
|
3 |
||
27924 | 4 |
Detecting and recoding Isabelle symbols. |
27901 | 5 |
*/ |
6 |
||
7 |
package isabelle |
|
8 |
||
55618 | 9 |
|
36011
3ff725ac13a4
adapted to Scala 2.8.0 Beta1 -- with notable changes to scala.collection;
wenzelm
parents:
34316
diff
changeset
|
10 |
import scala.collection.mutable |
31522 | 11 |
import scala.util.matching.Regex |
48922 | 12 |
import scala.annotation.tailrec |
27901 | 13 |
|
14 |
||
31522 | 15 |
object Symbol |
16 |
{ |
|
43696 | 17 |
type Symbol = String |
18 |
||
19 |
||
43418 | 20 |
/* ASCII characters */ |
21 |
||
22 |
def is_ascii_letter(c: Char): Boolean = 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' |
|
55497 | 23 |
|
43418 | 24 |
def is_ascii_digit(c: Char): Boolean = '0' <= c && c <= '9' |
55497 | 25 |
|
26 |
def is_ascii_hex(c: Char): Boolean = |
|
27 |
'0' <= c && c <= '9' || 'A' <= c && c <= 'F' || 'a' <= c && c <= 'f' |
|
28 |
||
43418 | 29 |
def is_ascii_quasi(c: Char): Boolean = c == '_' || c == '\'' |
30 |
||
55497 | 31 |
def is_ascii_blank(c: Char): Boolean = " \t\n\u000b\f\r".contains(c) |
32 |
||
43418 | 33 |
def is_ascii_letdig(c: Char): Boolean = |
34 |
is_ascii_letter(c) || is_ascii_digit(c) || is_ascii_quasi(c) |
|
35 |
||
36 |
def is_ascii_identifier(s: String): Boolean = |
|
50238
98d35a7368bd
more uniform Symbol.is_ascii_identifier in ML/Scala;
wenzelm
parents:
50233
diff
changeset
|
37 |
s.length > 0 && is_ascii_letter(s(0)) && s.forall(is_ascii_letdig) |
43418 | 38 |
|
39 |
||
48775 | 40 |
/* symbol matching */ |
27901 | 41 |
|
48775 | 42 |
private val symbol_total = new Regex("""(?xs) |
43 |
[\ud800-\udbff][\udc00-\udfff] | \r\n | |
|
44 |
\\ < (?: \^raw: [\x20-\x7e\u0100-\uffff && [^.>]]* | \^? ([A-Za-z][A-Za-z0-9_']*)? ) >? | |
|
45 |
.""") |
|
27924 | 46 |
|
48775 | 47 |
private def is_plain(c: Char): Boolean = |
48 |
!(c == '\r' || c == '\\' || Character.isHighSurrogate(c)) |
|
48773
0e1bab274672
more liberal scanning of potentially malformed symbols;
wenzelm
parents:
48704
diff
changeset
|
49 |
|
0e1bab274672
more liberal scanning of potentially malformed symbols;
wenzelm
parents:
48704
diff
changeset
|
50 |
def is_malformed(s: Symbol): Boolean = |
0e1bab274672
more liberal scanning of potentially malformed symbols;
wenzelm
parents:
48704
diff
changeset
|
51 |
s.length match { |
0e1bab274672
more liberal scanning of potentially malformed symbols;
wenzelm
parents:
48704
diff
changeset
|
52 |
case 1 => |
0e1bab274672
more liberal scanning of potentially malformed symbols;
wenzelm
parents:
48704
diff
changeset
|
53 |
val c = s(0) |
0e1bab274672
more liberal scanning of potentially malformed symbols;
wenzelm
parents:
48704
diff
changeset
|
54 |
Character.isHighSurrogate(c) || Character.isLowSurrogate(c) || c == '\ufffd' |
0e1bab274672
more liberal scanning of potentially malformed symbols;
wenzelm
parents:
48704
diff
changeset
|
55 |
case 2 => |
0e1bab274672
more liberal scanning of potentially malformed symbols;
wenzelm
parents:
48704
diff
changeset
|
56 |
val c1 = s(0) |
0e1bab274672
more liberal scanning of potentially malformed symbols;
wenzelm
parents:
48704
diff
changeset
|
57 |
val c2 = s(1) |
0e1bab274672
more liberal scanning of potentially malformed symbols;
wenzelm
parents:
48704
diff
changeset
|
58 |
!(c1 == '\r' && c2 == '\n' || Character.isSurrogatePair(c1, c2)) |
48774 | 59 |
case _ => !s.endsWith(">") || s == "\\<>" || s == "\\<^>" |
48773
0e1bab274672
more liberal scanning of potentially malformed symbols;
wenzelm
parents:
48704
diff
changeset
|
60 |
} |
34137 | 61 |
|
54734
b91afc3aa3e6
clarified Proof General legacy: special treatment of \<^newline> only in TTY mode;
wenzelm
parents:
53400
diff
changeset
|
62 |
def is_newline(s: Symbol): Boolean = |
43675
8252d51d70e2
simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents:
43511
diff
changeset
|
63 |
s == "\n" || s == "\r" || s == "\r\n" |
38877 | 64 |
|
34137 | 65 |
class Matcher(text: CharSequence) |
66 |
{ |
|
48775 | 67 |
private val matcher = symbol_total.pattern.matcher(text) |
34137 | 68 |
def apply(start: Int, end: Int): Int = |
69 |
{ |
|
70 |
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
|
71 |
if (is_plain(text.charAt(start))) 1 |
34138 | 72 |
else { |
34137 | 73 |
matcher.region(start, end).lookingAt |
74 |
matcher.group.length |
|
75 |
} |
|
76 |
} |
|
31522 | 77 |
} |
27937
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
78 |
|
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
79 |
|
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
80 |
/* iterator */ |
33998 | 81 |
|
43696 | 82 |
private val char_symbols: Array[Symbol] = |
43675
8252d51d70e2
simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents:
43511
diff
changeset
|
83 |
(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
|
84 |
|
43696 | 85 |
def iterator(text: CharSequence): Iterator[Symbol] = |
86 |
new Iterator[Symbol] |
|
40522 | 87 |
{ |
43489 | 88 |
private val matcher = new Matcher(text) |
89 |
private var i = 0 |
|
90 |
def hasNext = i < text.length |
|
91 |
def next = |
|
92 |
{ |
|
93 |
val n = matcher(i, text.length) |
|
43675
8252d51d70e2
simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents:
43511
diff
changeset
|
94 |
val s = |
8252d51d70e2
simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents:
43511
diff
changeset
|
95 |
if (n == 0) "" |
8252d51d70e2
simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents:
43511
diff
changeset
|
96 |
else if (n == 1) { |
8252d51d70e2
simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents:
43511
diff
changeset
|
97 |
val c = text.charAt(i) |
8252d51d70e2
simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents:
43511
diff
changeset
|
98 |
if (c < char_symbols.length) char_symbols(c) |
8252d51d70e2
simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents:
43511
diff
changeset
|
99 |
else text.subSequence(i, i + n).toString |
8252d51d70e2
simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents:
43511
diff
changeset
|
100 |
} |
8252d51d70e2
simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents:
43511
diff
changeset
|
101 |
else text.subSequence(i, i + n).toString |
43489 | 102 |
i += n |
103 |
s |
|
104 |
} |
|
33998 | 105 |
} |
43489 | 106 |
|
44949 | 107 |
def explode(text: CharSequence): List[Symbol] = iterator(text).toList |
108 |
||
48922 | 109 |
def advance_line_column(pos: (Int, Int), text: CharSequence): (Int, Int) = |
110 |
{ |
|
111 |
var (line, column) = pos |
|
112 |
for (sym <- iterator(text)) { |
|
54734
b91afc3aa3e6
clarified Proof General legacy: special treatment of \<^newline> only in TTY mode;
wenzelm
parents:
53400
diff
changeset
|
113 |
if (is_newline(sym)) { line += 1; column = 1 } |
48922 | 114 |
else column += 1 |
115 |
} |
|
116 |
(line, column) |
|
117 |
} |
|
118 |
||
33998 | 119 |
|
120 |
/* decoding offsets */ |
|
121 |
||
52507 | 122 |
object Index |
123 |
{ |
|
124 |
def apply(text: CharSequence): Index = new Index(text) |
|
125 |
} |
|
126 |
||
127 |
final class Index private(text: CharSequence) |
|
31929 | 128 |
{ |
55430 | 129 |
private sealed case class Entry(chr: Int, sym: Int) |
130 |
private val index: Array[Entry] = |
|
31929 | 131 |
{ |
34137 | 132 |
val matcher = new Matcher(text) |
31929 | 133 |
val buf = new mutable.ArrayBuffer[Entry] |
134 |
var chr = 0 |
|
135 |
var sym = 0 |
|
33998 | 136 |
while (chr < text.length) { |
34137 | 137 |
val n = matcher(chr, text.length) |
138 |
chr += n |
|
31929 | 139 |
sym += 1 |
34137 | 140 |
if (n > 1) buf += Entry(chr, sym) |
31929 | 141 |
} |
142 |
buf.toArray |
|
143 |
} |
|
55430 | 144 |
|
38479
e628da370072
more efficient Markup_Tree, based on branches sorted by quasi-order;
wenzelm
parents:
37556
diff
changeset
|
145 |
def decode(sym1: Int): Int = |
31929 | 146 |
{ |
38479
e628da370072
more efficient Markup_Tree, based on branches sorted by quasi-order;
wenzelm
parents:
37556
diff
changeset
|
147 |
val sym = sym1 - 1 |
31929 | 148 |
val end = index.length |
48922 | 149 |
@tailrec def bisect(a: Int, b: Int): Int = |
31929 | 150 |
{ |
151 |
if (a < b) { |
|
152 |
val c = (a + b) / 2 |
|
153 |
if (sym < index(c).sym) bisect(a, c) |
|
154 |
else if (c + 1 == end || sym < index(c + 1).sym) c |
|
155 |
else bisect(c + 1, b) |
|
156 |
} |
|
157 |
else -1 |
|
158 |
} |
|
159 |
val i = bisect(0, end) |
|
160 |
if (i < 0) sym |
|
161 |
else index(i).chr + sym - index(i).sym |
|
162 |
} |
|
38479
e628da370072
more efficient Markup_Tree, based on branches sorted by quasi-order;
wenzelm
parents:
37556
diff
changeset
|
163 |
def decode(range: Text.Range): Text.Range = range.map(decode(_)) |
31929 | 164 |
} |
165 |
||
166 |
||
33998 | 167 |
/* recoding text */ |
27937
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
168 |
|
31522 | 169 |
private class Recoder(list: List[(String, String)]) |
170 |
{ |
|
171 |
private val (min, max) = |
|
172 |
{ |
|
27937
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
173 |
var min = '\uffff' |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
174 |
var max = '\u0000' |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
175 |
for ((x, _) <- list) { |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
176 |
val c = x(0) |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
177 |
if (c < min) min = c |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
178 |
if (c > max) max = c |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
179 |
} |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
180 |
(min, max) |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
181 |
} |
40443 | 182 |
private val table = |
183 |
{ |
|
184 |
var tab = Map[String, String]() |
|
185 |
for ((x, y) <- list) { |
|
186 |
tab.get(x) match { |
|
187 |
case None => tab += (x -> y) |
|
188 |
case Some(z) => |
|
44181 | 189 |
error("Duplicate mapping of " + quote(x) + " to " + quote(y) + " vs. " + quote(z)) |
40443 | 190 |
} |
191 |
} |
|
192 |
tab |
|
193 |
} |
|
31522 | 194 |
def recode(text: String): String = |
195 |
{ |
|
27937
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
196 |
val len = text.length |
48775 | 197 |
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
|
198 |
val result = new StringBuilder(len) |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
199 |
var i = 0 |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
200 |
while (i < len) { |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
201 |
val c = text(i) |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
202 |
if (min <= c && c <= max) { |
31929 | 203 |
matcher.region(i, len).lookingAt |
27938 | 204 |
val x = matcher.group |
52888 | 205 |
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
|
206 |
i = matcher.end |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
207 |
} |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
208 |
else { result.append(c); i += 1 } |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
209 |
} |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
210 |
result.toString |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
211 |
} |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
212 |
} |
27924 | 213 |
|
27918 | 214 |
|
27923
7ebe9d38743a
use scala.collection.jcl.HashMap, which seems to be more efficient;
wenzelm
parents:
27918
diff
changeset
|
215 |
|
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
216 |
/** symbol interpretation **/ |
27927 | 217 |
|
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
218 |
private lazy val symbols = |
50564
c6fde2fc4217
allow to suppress ISABELLE_SYMBOLS for experiments;
wenzelm
parents:
50291
diff
changeset
|
219 |
new Interpretation(File.try_read(Path.split(Isabelle_System.getenv("ISABELLE_SYMBOLS")))) |
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
220 |
|
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
221 |
private class Interpretation(symbols_spec: String) |
29569
f3f529b5d8fb
more general init of Symbol.Interpretation, independent of IsabelleSystem instance;
wenzelm
parents:
29174
diff
changeset
|
222 |
{ |
31522 | 223 |
/* read symbols */ |
224 |
||
50136
a96bd08258a2
support for symbol groups, retaining original order of declarations;
wenzelm
parents:
48922
diff
changeset
|
225 |
private val No_Decl = new Regex("""(?xs) ^\s* (?: \#.* )? $ """) |
a96bd08258a2
support for symbol groups, retaining original order of declarations;
wenzelm
parents:
48922
diff
changeset
|
226 |
private val Key = new Regex("""(?xs) (.+): """) |
31522 | 227 |
|
53316
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
228 |
private def read_decl(decl: String): (Symbol, Properties.T) = |
31522 | 229 |
{ |
230 |
def err() = error("Bad symbol declaration: " + decl) |
|
231 |
||
53316
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
232 |
def read_props(props: List[String]): Properties.T = |
31522 | 233 |
{ |
234 |
props match { |
|
53316
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
235 |
case Nil => Nil |
31522 | 236 |
case _ :: Nil => err() |
53316
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
237 |
case Key(x) :: y :: rest => (x -> y) :: read_props(rest) |
31522 | 238 |
case _ => err() |
239 |
} |
|
240 |
} |
|
241 |
decl.split("\\s+").toList match { |
|
53316
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
242 |
case sym :: props if sym.length > 1 && !is_malformed(sym) => |
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
243 |
(sym, read_props(props)) |
34193 | 244 |
case _ => err() |
31522 | 245 |
} |
246 |
} |
|
247 |
||
53316
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
248 |
private val symbols: List[(Symbol, Properties.T)] = |
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
249 |
(((List.empty[(Symbol, Properties.T)], Set.empty[Symbol]) /: |
50136
a96bd08258a2
support for symbol groups, retaining original order of declarations;
wenzelm
parents:
48922
diff
changeset
|
250 |
split_lines(symbols_spec).reverse) |
a96bd08258a2
support for symbol groups, retaining original order of declarations;
wenzelm
parents:
48922
diff
changeset
|
251 |
{ case (res, No_Decl()) => res |
a96bd08258a2
support for symbol groups, retaining original order of declarations;
wenzelm
parents:
48922
diff
changeset
|
252 |
case ((list, known), decl) => |
a96bd08258a2
support for symbol groups, retaining original order of declarations;
wenzelm
parents:
48922
diff
changeset
|
253 |
val (sym, props) = read_decl(decl) |
a96bd08258a2
support for symbol groups, retaining original order of declarations;
wenzelm
parents:
48922
diff
changeset
|
254 |
if (known(sym)) (list, known) |
a96bd08258a2
support for symbol groups, retaining original order of declarations;
wenzelm
parents:
48922
diff
changeset
|
255 |
else ((sym, props) :: list, known + sym) |
a96bd08258a2
support for symbol groups, retaining original order of declarations;
wenzelm
parents:
48922
diff
changeset
|
256 |
})._1 |
31522 | 257 |
|
258 |
||
53400 | 259 |
/* basic properties */ |
260 |
||
261 |
val properties: Map[Symbol, Properties.T] = Map(symbols: _*) |
|
31651 | 262 |
|
43696 | 263 |
val names: Map[Symbol, String] = |
34134 | 264 |
{ |
43456
8a6de1a6e1dc
names for control symbols without "^", which is relevant for completion;
wenzelm
parents:
43455
diff
changeset
|
265 |
val name = new Regex("""\\<\^?([A-Za-z][A-Za-z0-9_']*)>""") |
31651 | 266 |
Map((for ((sym @ name(a), _) <- symbols) yield (sym -> a)): _*) |
267 |
} |
|
268 |
||
50136
a96bd08258a2
support for symbol groups, retaining original order of declarations;
wenzelm
parents:
48922
diff
changeset
|
269 |
val groups: List[(String, List[Symbol])] = |
53316
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
270 |
symbols.map({ case (sym, props) => |
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
271 |
val gs = for (("group", g) <- props) yield g |
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
272 |
if (gs.isEmpty) List(sym -> "unsorted") else gs.map(sym -> _) |
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
273 |
}).flatten |
50136
a96bd08258a2
support for symbol groups, retaining original order of declarations;
wenzelm
parents:
48922
diff
changeset
|
274 |
.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
|
275 |
.sortBy(_._1) |
a96bd08258a2
support for symbol groups, retaining original order of declarations;
wenzelm
parents:
48922
diff
changeset
|
276 |
|
53316
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
277 |
val abbrevs: Multi_Map[Symbol, String] = |
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
278 |
Multi_Map(( |
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
279 |
for { |
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
280 |
(sym, props) <- symbols |
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
281 |
("abbrev", a) <- props.reverse |
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
282 |
} yield (sym -> a)): _*) |
43488 | 283 |
|
284 |
||
43490 | 285 |
/* recoding */ |
31522 | 286 |
|
53316
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
287 |
private val Code = new Properties.String("code") |
31522 | 288 |
private val (decoder, encoder) = |
289 |
{ |
|
290 |
val mapping = |
|
291 |
for { |
|
292 |
(sym, props) <- symbols |
|
46997 | 293 |
code = |
53316
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
294 |
props match { |
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
295 |
case Code(s) => |
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
296 |
try { Integer.decode(s).intValue } |
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
297 |
catch { case _: NumberFormatException => error("Bad code for symbol " + sym) } |
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
298 |
case _ => error("Missing code for symbol " + sym) |
31522 | 299 |
} |
46997 | 300 |
ch = new String(Character.toChars(code)) |
34193 | 301 |
} yield { |
302 |
if (code < 128) error("Illegal ASCII code for symbol " + sym) |
|
303 |
else (sym, ch) |
|
304 |
} |
|
31545
5f1f0a20af4d
discontinued escaped symbols such as \\<forall> -- only one backslash should be used;
wenzelm
parents:
31523
diff
changeset
|
305 |
(new Recoder(mapping), |
31548 | 306 |
new Recoder(mapping map { case (x, y) => (y, x) })) |
31522 | 307 |
} |
27918 | 308 |
|
34098 | 309 |
def decode(text: String): String = decoder.recode(text) |
310 |
def encode(text: String): String = encoder.recode(text) |
|
34134 | 311 |
|
43490 | 312 |
private def recode_set(elems: String*): Set[String] = |
313 |
{ |
|
314 |
val content = elems.toList |
|
315 |
Set((content ::: content.map(decode)): _*) |
|
316 |
} |
|
317 |
||
318 |
private def recode_map[A](elems: (String, A)*): Map[String, A] = |
|
319 |
{ |
|
320 |
val content = elems.toList |
|
321 |
Map((content ::: content.map({ case (sym, a) => (decode(sym), a) })): _*) |
|
322 |
} |
|
323 |
||
324 |
||
325 |
/* user fonts */ |
|
326 |
||
53316
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
327 |
private val Font = new Properties.String("font") |
43696 | 328 |
val fonts: Map[Symbol, String] = |
53316
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
329 |
recode_map((for ((sym, Font(font)) <- symbols) yield (sym -> font)): _*) |
43490 | 330 |
|
331 |
val font_names: List[String] = Set(fonts.toList.map(_._2): _*).toList |
|
332 |
val font_index: Map[String, Int] = Map((font_names zip (0 until font_names.length).toList): _*) |
|
333 |
||
34134 | 334 |
|
335 |
/* classification */ |
|
336 |
||
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
337 |
val letters = recode_set( |
34134 | 338 |
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", |
339 |
"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", |
|
340 |
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", |
|
341 |
"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", |
|
342 |
||
343 |
"\\<A>", "\\<B>", "\\<C>", "\\<D>", "\\<E>", "\\<F>", "\\<G>", |
|
344 |
"\\<H>", "\\<I>", "\\<J>", "\\<K>", "\\<L>", "\\<M>", "\\<N>", |
|
345 |
"\\<O>", "\\<P>", "\\<Q>", "\\<R>", "\\<S>", "\\<T>", "\\<U>", |
|
346 |
"\\<V>", "\\<W>", "\\<X>", "\\<Y>", "\\<Z>", "\\<a>", "\\<b>", |
|
347 |
"\\<c>", "\\<d>", "\\<e>", "\\<f>", "\\<g>", "\\<h>", "\\<i>", |
|
348 |
"\\<j>", "\\<k>", "\\<l>", "\\<m>", "\\<n>", "\\<o>", "\\<p>", |
|
349 |
"\\<q>", "\\<r>", "\\<s>", "\\<t>", "\\<u>", "\\<v>", "\\<w>", |
|
350 |
"\\<x>", "\\<y>", "\\<z>", |
|
351 |
||
352 |
"\\<AA>", "\\<BB>", "\\<CC>", "\\<DD>", "\\<EE>", "\\<FF>", |
|
353 |
"\\<GG>", "\\<HH>", "\\<II>", "\\<JJ>", "\\<KK>", "\\<LL>", |
|
354 |
"\\<MM>", "\\<NN>", "\\<OO>", "\\<PP>", "\\<QQ>", "\\<RR>", |
|
355 |
"\\<SS>", "\\<TT>", "\\<UU>", "\\<VV>", "\\<WW>", "\\<XX>", |
|
356 |
"\\<YY>", "\\<ZZ>", "\\<aa>", "\\<bb>", "\\<cc>", "\\<dd>", |
|
357 |
"\\<ee>", "\\<ff>", "\\<gg>", "\\<hh>", "\\<ii>", "\\<jj>", |
|
358 |
"\\<kk>", "\\<ll>", "\\<mm>", "\\<nn>", "\\<oo>", "\\<pp>", |
|
359 |
"\\<qq>", "\\<rr>", "\\<ss>", "\\<tt>", "\\<uu>", "\\<vv>", |
|
360 |
"\\<ww>", "\\<xx>", "\\<yy>", "\\<zz>", |
|
361 |
||
362 |
"\\<alpha>", "\\<beta>", "\\<gamma>", "\\<delta>", "\\<epsilon>", |
|
363 |
"\\<zeta>", "\\<eta>", "\\<theta>", "\\<iota>", "\\<kappa>", |
|
364 |
"\\<mu>", "\\<nu>", "\\<xi>", "\\<pi>", "\\<rho>", "\\<sigma>", |
|
365 |
"\\<tau>", "\\<upsilon>", "\\<phi>", "\\<chi>", "\\<psi>", |
|
366 |
"\\<omega>", "\\<Gamma>", "\\<Delta>", "\\<Theta>", "\\<Lambda>", |
|
367 |
"\\<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
|
368 |
"\\<Psi>", "\\<Omega>") |
34134 | 369 |
|
54734
b91afc3aa3e6
clarified Proof General legacy: special treatment of \<^newline> only in TTY mode;
wenzelm
parents:
53400
diff
changeset
|
370 |
val blanks = recode_set(" ", "\t", "\n", "\u000B", "\f", "\r", "\r\n") |
34138 | 371 |
|
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
372 |
val sym_chars = |
34138 | 373 |
Set("!", "#", "$", "%", "&", "*", "+", "-", "/", "<", "=", ">", "?", "@", "^", "_", "|", "~") |
34134 | 374 |
|
44992
aa34d2d049ce
refined Symbol.is_symbolic -- cover recoded versions as well;
wenzelm
parents:
44949
diff
changeset
|
375 |
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
|
376 |
|
43455 | 377 |
|
55033 | 378 |
/* cartouches */ |
379 |
||
380 |
val open_decoded = decode(open) |
|
381 |
val close_decoded = decode(close) |
|
382 |
||
383 |
||
43488 | 384 |
/* control symbols */ |
385 |
||
43696 | 386 |
val ctrl_decoded: Set[Symbol] = |
43488 | 387 |
Set((for ((sym, _) <- symbols if sym.startsWith("\\<^")) yield decode(sym)): _*) |
388 |
||
44238
36120feb70ed
some convenience actions/shortcuts for control symbols;
wenzelm
parents:
44181
diff
changeset
|
389 |
val sub_decoded = decode("\\<^sub>") |
36120feb70ed
some convenience actions/shortcuts for control symbols;
wenzelm
parents:
44181
diff
changeset
|
390 |
val sup_decoded = decode("\\<^sup>") |
43511 | 391 |
val bsub_decoded = decode("\\<^bsub>") |
392 |
val esub_decoded = decode("\\<^esub>") |
|
393 |
val bsup_decoded = decode("\\<^bsup>") |
|
394 |
val esup_decoded = decode("\\<^esup>") |
|
44238
36120feb70ed
some convenience actions/shortcuts for control symbols;
wenzelm
parents:
44181
diff
changeset
|
395 |
val bold_decoded = decode("\\<^bold>") |
27918 | 396 |
} |
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
397 |
|
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
398 |
|
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
399 |
/* tables */ |
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
400 |
|
53400 | 401 |
def properties: Map[Symbol, Properties.T] = symbols.properties |
43696 | 402 |
def names: Map[Symbol, String] = symbols.names |
50136
a96bd08258a2
support for symbol groups, retaining original order of declarations;
wenzelm
parents:
48922
diff
changeset
|
403 |
def groups: List[(String, List[Symbol])] = symbols.groups |
53316
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53021
diff
changeset
|
404 |
def abbrevs: Multi_Map[Symbol, String] = symbols.abbrevs |
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
405 |
|
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
406 |
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
|
407 |
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
|
408 |
|
53337
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53316
diff
changeset
|
409 |
def decode_string: XML.Decode.T[String] = (x => decode(XML.Decode.string(x))) |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53316
diff
changeset
|
410 |
def encode_string: XML.Encode.T[String] = (x => XML.Encode.string(encode(x))) |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53316
diff
changeset
|
411 |
|
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
|
412 |
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
|
413 |
{ |
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
|
414 |
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
|
415 |
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
|
416 |
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
|
417 |
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
|
418 |
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
|
419 |
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
|
420 |
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
|
421 |
} |
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
|
422 |
} |
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
|
423 |
|
43696 | 424 |
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
|
425 |
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
|
426 |
def font_index: Map[String, Int] = symbols.font_index |
43696 | 427 |
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
|
428 |
|
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
429 |
|
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
430 |
/* classification */ |
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
431 |
|
43696 | 432 |
def is_letter(sym: Symbol): Boolean = symbols.letters.contains(sym) |
433 |
def is_digit(sym: Symbol): Boolean = sym.length == 1 && '0' <= sym(0) && sym(0) <= '9' |
|
434 |
def is_quasi(sym: Symbol): Boolean = sym == "_" || sym == "'" |
|
435 |
def is_letdig(sym: Symbol): Boolean = is_letter(sym) || is_digit(sym) || is_quasi(sym) |
|
436 |
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
|
437 |
|
55033 | 438 |
|
439 |
/* cartouches */ |
|
440 |
||
441 |
val open = "\\<open>" |
|
442 |
val close = "\\<close>" |
|
443 |
||
444 |
def open_decoded: Symbol = symbols.open_decoded |
|
445 |
def close_decoded: Symbol = symbols.close_decoded |
|
446 |
||
447 |
def is_open(sym: Symbol): Boolean = sym == open_decoded || sym == open |
|
448 |
def is_close(sym: Symbol): Boolean = sym == close_decoded || sym == close |
|
449 |
||
450 |
||
451 |
/* symbols for symbolic identifiers */ |
|
44992
aa34d2d049ce
refined Symbol.is_symbolic -- cover recoded versions as well;
wenzelm
parents:
44949
diff
changeset
|
452 |
|
aa34d2d049ce
refined Symbol.is_symbolic -- cover recoded versions as well;
wenzelm
parents:
44949
diff
changeset
|
453 |
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
|
454 |
sym.startsWith("\\<") && sym.endsWith(">") && !sym.startsWith("\\<^") |
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
455 |
|
55033 | 456 |
def is_symbolic(sym: Symbol): Boolean = |
457 |
!is_open(sym) && !is_close(sym) && (raw_symbolic(sym) || symbols.symbolic.contains(sym)) |
|
458 |
||
459 |
def is_symbolic_char(sym: Symbol): Boolean = symbols.sym_chars.contains(sym) |
|
460 |
||
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
461 |
|
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
462 |
/* control symbols */ |
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
463 |
|
43696 | 464 |
def is_ctrl(sym: Symbol): Boolean = |
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
465 |
sym.startsWith("\\<^") || symbols.ctrl_decoded.contains(sym) |
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
466 |
|
43696 | 467 |
def is_controllable(sym: Symbol): Boolean = |
55033 | 468 |
!is_blank(sym) && !is_ctrl(sym) && !is_open(sym) && !is_close(sym) && !is_malformed(sym) |
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
469 |
|
44238
36120feb70ed
some convenience actions/shortcuts for control symbols;
wenzelm
parents:
44181
diff
changeset
|
470 |
def sub_decoded: Symbol = symbols.sub_decoded |
36120feb70ed
some convenience actions/shortcuts for control symbols;
wenzelm
parents:
44181
diff
changeset
|
471 |
def sup_decoded: Symbol = symbols.sup_decoded |
36120feb70ed
some convenience actions/shortcuts for control symbols;
wenzelm
parents:
44181
diff
changeset
|
472 |
def bsub_decoded: Symbol = symbols.bsub_decoded |
36120feb70ed
some convenience actions/shortcuts for control symbols;
wenzelm
parents:
44181
diff
changeset
|
473 |
def esub_decoded: Symbol = symbols.esub_decoded |
36120feb70ed
some convenience actions/shortcuts for control symbols;
wenzelm
parents:
44181
diff
changeset
|
474 |
def bsup_decoded: Symbol = symbols.bsup_decoded |
36120feb70ed
some convenience actions/shortcuts for control symbols;
wenzelm
parents:
44181
diff
changeset
|
475 |
def esup_decoded: Symbol = symbols.esup_decoded |
36120feb70ed
some convenience actions/shortcuts for control symbols;
wenzelm
parents:
44181
diff
changeset
|
476 |
def bold_decoded: Symbol = symbols.bold_decoded |
27901 | 477 |
} |