author | wenzelm |
Sun, 30 May 2010 14:14:30 +0200 | |
changeset 37192 | 8cdddd689ea9 |
parent 36816 | da7628b3d231 |
child 37556 | 2bf29095d26f |
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 |
{ |
|
36763 | 16 |
/* spaces */ |
17 |
||
36816 | 18 |
val spc = ' ' |
19 |
val space = " " |
|
20 |
||
21 |
private val static_spaces = space * 4000 |
|
36763 | 22 |
|
23 |
def spaces(k: Int): String = |
|
24 |
{ |
|
25 |
require(k >= 0) |
|
26 |
if (k < static_spaces.length) static_spaces.substring(0, k) |
|
36816 | 27 |
else space * k |
36763 | 28 |
} |
29 |
||
30 |
||
33998 | 31 |
/* Symbol regexps */ |
27901 | 32 |
|
31522 | 33 |
private val plain = new Regex("""(?xs) |
34 |
[^\\ \ud800-\udfff] | [\ud800-\udbff][\udc00-\udfff] """) |
|
27901 | 35 |
|
31522 | 36 |
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
|
37 |
\\ < (?: |
27924 | 38 |
\^? [A-Za-z][A-Za-z0-9_']* | |
39 |
\^raw: [\x20-\x7e\u0100-\uffff && [^.>]]* ) >""") |
|
40 |
||
34316
f879b649ac4c
clarified Symbol.is_plain/is_wellformed -- is_closed was rejecting plain backslashes;
wenzelm
parents:
34193
diff
changeset
|
41 |
// FIXME cover bad surrogates!? |
f879b649ac4c
clarified Symbol.is_plain/is_wellformed -- is_closed was rejecting plain backslashes;
wenzelm
parents:
34193
diff
changeset
|
42 |
// FIXME check wrt. ML version |
31522 | 43 |
private val bad_symbol = new Regex("(?xs) (?!" + symbol + ")" + |
31545
5f1f0a20af4d
discontinued escaped symbols such as \\<forall> -- only one backslash should be used;
wenzelm
parents:
31523
diff
changeset
|
44 |
""" \\ < (?: (?! \s | [\"`\\] | \(\* | \*\) | \{\* | \*\} ) . )*""") |
27924 | 45 |
|
27939 | 46 |
// total pattern |
31522 | 47 |
val regex = new Regex(plain + "|" + symbol + "|" + bad_symbol + "| .") |
27937
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
48 |
|
34137 | 49 |
|
50 |
/* basic matching */ |
|
51 |
||
34316
f879b649ac4c
clarified Symbol.is_plain/is_wellformed -- is_closed was rejecting plain backslashes;
wenzelm
parents:
34193
diff
changeset
|
52 |
def is_plain(c: Char): Boolean = !(c == '\\' || '\ud800' <= c && c <= '\udfff') |
34137 | 53 |
|
34316
f879b649ac4c
clarified Symbol.is_plain/is_wellformed -- is_closed was rejecting plain backslashes;
wenzelm
parents:
34193
diff
changeset
|
54 |
def is_wellformed(s: CharSequence): Boolean = |
f879b649ac4c
clarified Symbol.is_plain/is_wellformed -- is_closed was rejecting plain backslashes;
wenzelm
parents:
34193
diff
changeset
|
55 |
s.length == 1 && is_plain(s.charAt(0)) || !bad_symbol.pattern.matcher(s).matches |
34137 | 56 |
|
57 |
class Matcher(text: CharSequence) |
|
58 |
{ |
|
59 |
private val matcher = regex.pattern.matcher(text) |
|
60 |
def apply(start: Int, end: Int): Int = |
|
61 |
{ |
|
62 |
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
|
63 |
if (is_plain(text.charAt(start))) 1 |
34138 | 64 |
else { |
34137 | 65 |
matcher.region(start, end).lookingAt |
66 |
matcher.group.length |
|
67 |
} |
|
68 |
} |
|
31522 | 69 |
} |
27937
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
70 |
|
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
71 |
|
36011
3ff725ac13a4
adapted to Scala 2.8.0 Beta1 -- with notable changes to scala.collection;
wenzelm
parents:
34316
diff
changeset
|
72 |
/* iterator */ |
33998 | 73 |
|
36011
3ff725ac13a4
adapted to Scala 2.8.0 Beta1 -- with notable changes to scala.collection;
wenzelm
parents:
34316
diff
changeset
|
74 |
def iterator(text: CharSequence) = new Iterator[CharSequence] |
34134 | 75 |
{ |
34137 | 76 |
private val matcher = new Matcher(text) |
33998 | 77 |
private var i = 0 |
78 |
def hasNext = i < text.length |
|
79 |
def next = { |
|
34137 | 80 |
val n = matcher(i, text.length) |
81 |
val s = text.subSequence(i, i + n) |
|
82 |
i += n |
|
83 |
s |
|
33998 | 84 |
} |
85 |
} |
|
86 |
||
87 |
||
88 |
/* decoding offsets */ |
|
89 |
||
90 |
class Index(text: CharSequence) |
|
31929 | 91 |
{ |
92 |
case class Entry(chr: Int, sym: Int) |
|
93 |
val index: Array[Entry] = |
|
94 |
{ |
|
34137 | 95 |
val matcher = new Matcher(text) |
31929 | 96 |
val buf = new mutable.ArrayBuffer[Entry] |
97 |
var chr = 0 |
|
98 |
var sym = 0 |
|
33998 | 99 |
while (chr < text.length) { |
34137 | 100 |
val n = matcher(chr, text.length) |
101 |
chr += n |
|
31929 | 102 |
sym += 1 |
34137 | 103 |
if (n > 1) buf += Entry(chr, sym) |
31929 | 104 |
} |
105 |
buf.toArray |
|
106 |
} |
|
107 |
def decode(sym: Int): Int = |
|
108 |
{ |
|
109 |
val end = index.length |
|
110 |
def bisect(a: Int, b: Int): Int = |
|
111 |
{ |
|
112 |
if (a < b) { |
|
113 |
val c = (a + b) / 2 |
|
114 |
if (sym < index(c).sym) bisect(a, c) |
|
115 |
else if (c + 1 == end || sym < index(c + 1).sym) c |
|
116 |
else bisect(c + 1, b) |
|
117 |
} |
|
118 |
else -1 |
|
119 |
} |
|
120 |
val i = bisect(0, end) |
|
121 |
if (i < 0) sym |
|
122 |
else index(i).chr + sym - index(i).sym |
|
123 |
} |
|
124 |
} |
|
125 |
||
126 |
||
33998 | 127 |
/* recoding text */ |
27937
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
128 |
|
31522 | 129 |
private class Recoder(list: List[(String, String)]) |
130 |
{ |
|
131 |
private val (min, max) = |
|
132 |
{ |
|
27937
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
133 |
var min = '\uffff' |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
134 |
var max = '\u0000' |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
135 |
for ((x, _) <- list) { |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
136 |
val c = x(0) |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
137 |
if (c < min) min = c |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
138 |
if (c > max) max = c |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
139 |
} |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
140 |
(min, max) |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
141 |
} |
36011
3ff725ac13a4
adapted to Scala 2.8.0 Beta1 -- with notable changes to scala.collection;
wenzelm
parents:
34316
diff
changeset
|
142 |
private val table = Map[String, String]() ++ list |
31522 | 143 |
def recode(text: String): String = |
144 |
{ |
|
27937
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
145 |
val len = text.length |
31522 | 146 |
val matcher = regex.pattern.matcher(text) |
27937
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
147 |
val result = new StringBuilder(len) |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
148 |
var i = 0 |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
149 |
while (i < len) { |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
150 |
val c = text(i) |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
151 |
if (min <= c && c <= max) { |
31929 | 152 |
matcher.region(i, len).lookingAt |
27938 | 153 |
val x = matcher.group |
31522 | 154 |
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
|
155 |
i = matcher.end |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
156 |
} |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
157 |
else { result.append(c); i += 1 } |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
158 |
} |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
159 |
result.toString |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
160 |
} |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
161 |
} |
27924 | 162 |
|
27918 | 163 |
|
27923
7ebe9d38743a
use scala.collection.jcl.HashMap, which seems to be more efficient;
wenzelm
parents:
27918
diff
changeset
|
164 |
|
27927 | 165 |
/** Symbol interpretation **/ |
166 |
||
34137 | 167 |
class Interpretation(symbol_decls: List[String]) |
29569
f3f529b5d8fb
more general init of Symbol.Interpretation, independent of IsabelleSystem instance;
wenzelm
parents:
29174
diff
changeset
|
168 |
{ |
31522 | 169 |
/* read symbols */ |
170 |
||
171 |
private val empty = new Regex("""(?xs) ^\s* (?: \#.* )? $ """) |
|
172 |
private val key = new Regex("""(?xs) (.+): """) |
|
173 |
||
174 |
private def read_decl(decl: String): (String, Map[String, String]) = |
|
175 |
{ |
|
176 |
def err() = error("Bad symbol declaration: " + decl) |
|
177 |
||
178 |
def read_props(props: List[String]): Map[String, String] = |
|
179 |
{ |
|
180 |
props match { |
|
181 |
case Nil => Map() |
|
182 |
case _ :: Nil => err() |
|
183 |
case key(x) :: y :: rest => read_props(rest) + (x -> y) |
|
184 |
case _ => err() |
|
185 |
} |
|
186 |
} |
|
187 |
decl.split("\\s+").toList match { |
|
34316
f879b649ac4c
clarified Symbol.is_plain/is_wellformed -- is_closed was rejecting plain backslashes;
wenzelm
parents:
34193
diff
changeset
|
188 |
case sym :: props if sym.length > 1 && is_wellformed(sym) => (sym, read_props(props)) |
34193 | 189 |
case _ => err() |
31522 | 190 |
} |
191 |
} |
|
192 |
||
193 |
private val symbols: List[(String, Map[String, String])] = |
|
34137 | 194 |
for (decl <- symbol_decls if !empty.pattern.matcher(decl).matches) |
31522 | 195 |
yield read_decl(decl) |
196 |
||
197 |
||
31651 | 198 |
/* misc properties */ |
199 |
||
34134 | 200 |
val names: Map[String, String] = |
201 |
{ |
|
31651 | 202 |
val name = new Regex("""\\<([A-Za-z][A-Za-z0-9_']*)>""") |
203 |
Map((for ((sym @ name(a), _) <- symbols) yield (sym -> a)): _*) |
|
204 |
} |
|
205 |
||
206 |
val abbrevs: Map[String, String] = Map(( |
|
207 |
for ((sym, props) <- symbols if props.isDefinedAt("abbrev")) |
|
208 |
yield (sym -> props("abbrev"))): _*) |
|
209 |
||
210 |
||
31522 | 211 |
/* main recoder methods */ |
212 |
||
213 |
private val (decoder, encoder) = |
|
214 |
{ |
|
215 |
val mapping = |
|
216 |
for { |
|
217 |
(sym, props) <- symbols |
|
218 |
val code = |
|
219 |
try { Integer.decode(props("code")).intValue } |
|
220 |
catch { |
|
221 |
case _: NoSuchElementException => error("Missing code for symbol " + sym) |
|
222 |
case _: NumberFormatException => error("Bad code for symbol " + sym) |
|
223 |
} |
|
224 |
val ch = new String(Character.toChars(code)) |
|
34193 | 225 |
} yield { |
226 |
if (code < 128) error("Illegal ASCII code for symbol " + sym) |
|
227 |
else (sym, ch) |
|
228 |
} |
|
31545
5f1f0a20af4d
discontinued escaped symbols such as \\<forall> -- only one backslash should be used;
wenzelm
parents:
31523
diff
changeset
|
229 |
(new Recoder(mapping), |
31548 | 230 |
new Recoder(mapping map { case (x, y) => (y, x) })) |
31522 | 231 |
} |
27918 | 232 |
|
34098 | 233 |
def decode(text: String): String = decoder.recode(text) |
234 |
def encode(text: String): String = encoder.recode(text) |
|
34134 | 235 |
|
236 |
||
237 |
/* classification */ |
|
238 |
||
34138 | 239 |
private object Decode_Set |
240 |
{ |
|
241 |
def apply(elems: String*): Set[String] = |
|
242 |
{ |
|
243 |
val content = elems.toList |
|
244 |
Set((content ::: content.map(decode)): _*) |
|
245 |
} |
|
246 |
} |
|
247 |
||
248 |
private val letters = Decode_Set( |
|
34134 | 249 |
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", |
250 |
"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", |
|
251 |
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", |
|
252 |
"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", |
|
253 |
||
254 |
"\\<A>", "\\<B>", "\\<C>", "\\<D>", "\\<E>", "\\<F>", "\\<G>", |
|
255 |
"\\<H>", "\\<I>", "\\<J>", "\\<K>", "\\<L>", "\\<M>", "\\<N>", |
|
256 |
"\\<O>", "\\<P>", "\\<Q>", "\\<R>", "\\<S>", "\\<T>", "\\<U>", |
|
257 |
"\\<V>", "\\<W>", "\\<X>", "\\<Y>", "\\<Z>", "\\<a>", "\\<b>", |
|
258 |
"\\<c>", "\\<d>", "\\<e>", "\\<f>", "\\<g>", "\\<h>", "\\<i>", |
|
259 |
"\\<j>", "\\<k>", "\\<l>", "\\<m>", "\\<n>", "\\<o>", "\\<p>", |
|
260 |
"\\<q>", "\\<r>", "\\<s>", "\\<t>", "\\<u>", "\\<v>", "\\<w>", |
|
261 |
"\\<x>", "\\<y>", "\\<z>", |
|
262 |
||
263 |
"\\<AA>", "\\<BB>", "\\<CC>", "\\<DD>", "\\<EE>", "\\<FF>", |
|
264 |
"\\<GG>", "\\<HH>", "\\<II>", "\\<JJ>", "\\<KK>", "\\<LL>", |
|
265 |
"\\<MM>", "\\<NN>", "\\<OO>", "\\<PP>", "\\<QQ>", "\\<RR>", |
|
266 |
"\\<SS>", "\\<TT>", "\\<UU>", "\\<VV>", "\\<WW>", "\\<XX>", |
|
267 |
"\\<YY>", "\\<ZZ>", "\\<aa>", "\\<bb>", "\\<cc>", "\\<dd>", |
|
268 |
"\\<ee>", "\\<ff>", "\\<gg>", "\\<hh>", "\\<ii>", "\\<jj>", |
|
269 |
"\\<kk>", "\\<ll>", "\\<mm>", "\\<nn>", "\\<oo>", "\\<pp>", |
|
270 |
"\\<qq>", "\\<rr>", "\\<ss>", "\\<tt>", "\\<uu>", "\\<vv>", |
|
271 |
"\\<ww>", "\\<xx>", "\\<yy>", "\\<zz>", |
|
272 |
||
273 |
"\\<alpha>", "\\<beta>", "\\<gamma>", "\\<delta>", "\\<epsilon>", |
|
274 |
"\\<zeta>", "\\<eta>", "\\<theta>", "\\<iota>", "\\<kappa>", |
|
275 |
"\\<mu>", "\\<nu>", "\\<xi>", "\\<pi>", "\\<rho>", "\\<sigma>", |
|
276 |
"\\<tau>", "\\<upsilon>", "\\<phi>", "\\<chi>", "\\<psi>", |
|
277 |
"\\<omega>", "\\<Gamma>", "\\<Delta>", "\\<Theta>", "\\<Lambda>", |
|
278 |
"\\<Xi>", "\\<Pi>", "\\<Sigma>", "\\<Upsilon>", "\\<Phi>", |
|
279 |
"\\<Psi>", "\\<Omega>", |
|
280 |
||
281 |
"\\<^isub>", "\\<^isup>") |
|
282 |
||
34138 | 283 |
private val blanks = |
36816 | 284 |
Decode_Set(space, "\t", "\n", "\u000B", "\f", "\r", "\\<spacespace>", "\\<^newline>") |
34138 | 285 |
|
286 |
private val sym_chars = |
|
287 |
Set("!", "#", "$", "%", "&", "*", "+", "-", "/", "<", "=", ">", "?", "@", "^", "_", "|", "~") |
|
34134 | 288 |
|
289 |
def is_letter(sym: String): Boolean = letters.contains(sym) |
|
34138 | 290 |
def is_digit(sym: String): Boolean = sym.length == 1 && '0' <= sym(0) && sym(0) <= '9' |
34134 | 291 |
def is_quasi(sym: String): Boolean = sym == "_" || sym == "'" |
34138 | 292 |
def is_letdig(sym: String): Boolean = is_letter(sym) || is_digit(sym) || is_quasi(sym) |
34134 | 293 |
def is_blank(sym: String): Boolean = blanks.contains(sym) |
34138 | 294 |
def is_symbolic_char(sym: String): Boolean = sym_chars.contains(sym) |
295 |
def is_symbolic(sym: String): Boolean = sym.startsWith("\\<") && !sym.startsWith("\\<^") |
|
27918 | 296 |
} |
27901 | 297 |
} |