author | blanchet |
Mon, 26 Oct 2009 09:14:29 +0100 | |
changeset 33201 | e3d741e9d2fe |
parent 31929 | ecfc667cac53 |
child 33998 | fc56cfc6906e |
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 |
31929 | 10 |
import scala.collection.{jcl, mutable} |
31522 | 11 |
import scala.util.matching.Regex |
27901 | 12 |
|
13 |
||
31522 | 14 |
object Symbol |
15 |
{ |
|
27901 | 16 |
|
27924 | 17 |
/** Symbol regexps **/ |
27901 | 18 |
|
31522 | 19 |
private val plain = new Regex("""(?xs) |
20 |
[^\\ \ud800-\udfff] | [\ud800-\udbff][\udc00-\udfff] """) |
|
27901 | 21 |
|
31522 | 22 |
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
|
23 |
\\ < (?: |
27924 | 24 |
\^? [A-Za-z][A-Za-z0-9_']* | |
25 |
\^raw: [\x20-\x7e\u0100-\uffff && [^.>]]* ) >""") |
|
26 |
||
31522 | 27 |
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
|
28 |
""" \\ < (?: (?! \s | [\"`\\] | \(\* | \*\) | \{\* | \*\} ) . )*""") |
27924 | 29 |
|
27939 | 30 |
// total pattern |
31522 | 31 |
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
|
32 |
|
31522 | 33 |
// prefix of another symbol |
34 |
def is_open(s: String): Boolean = |
|
35 |
{ |
|
36 |
val len = s.length |
|
31929 | 37 |
len == 1 && Character.isLowSurrogate(s(0)) || |
31522 | 38 |
s == "\\" || |
39 |
s == "\\<" || |
|
40 |
len > 2 && s(len - 1) != '>' |
|
41 |
} |
|
27937
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
42 |
|
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
43 |
|
31929 | 44 |
/** Decoding offsets **/ |
45 |
||
46 |
class Index(text: String) |
|
47 |
{ |
|
48 |
case class Entry(chr: Int, sym: Int) |
|
49 |
val index: Array[Entry] = |
|
50 |
{ |
|
51 |
val length = text.length |
|
52 |
val matcher = regex.pattern.matcher(text) |
|
53 |
val buf = new mutable.ArrayBuffer[Entry] |
|
54 |
var chr = 0 |
|
55 |
var sym = 0 |
|
56 |
while (chr < length) { |
|
57 |
val c = text(chr) |
|
58 |
val len = |
|
59 |
if (c == '\\' || Character.isHighSurrogate(c)) { |
|
60 |
matcher.region(chr, length).lookingAt |
|
61 |
matcher.group.length |
|
62 |
} else 1 |
|
63 |
chr += len |
|
64 |
sym += 1 |
|
65 |
if (len > 1) buf += Entry(chr, sym) |
|
66 |
} |
|
67 |
buf.toArray |
|
68 |
} |
|
69 |
def decode(sym: Int): Int = |
|
70 |
{ |
|
71 |
val end = index.length |
|
72 |
def bisect(a: Int, b: Int): Int = |
|
73 |
{ |
|
74 |
if (a < b) { |
|
75 |
val c = (a + b) / 2 |
|
76 |
if (sym < index(c).sym) bisect(a, c) |
|
77 |
else if (c + 1 == end || sym < index(c + 1).sym) c |
|
78 |
else bisect(c + 1, b) |
|
79 |
} |
|
80 |
else -1 |
|
81 |
} |
|
82 |
val i = bisect(0, end) |
|
83 |
if (i < 0) sym |
|
84 |
else index(i).chr + sym - index(i).sym |
|
85 |
} |
|
86 |
} |
|
87 |
||
88 |
||
89 |
/** Recoding text **/ |
|
27937
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
90 |
|
31522 | 91 |
private class Recoder(list: List[(String, String)]) |
92 |
{ |
|
93 |
private val (min, max) = |
|
94 |
{ |
|
27937
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
95 |
var min = '\uffff' |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
96 |
var max = '\u0000' |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
97 |
for ((x, _) <- list) { |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
98 |
val c = x(0) |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
99 |
if (c < min) min = c |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
100 |
if (c > max) max = c |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
101 |
} |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
102 |
(min, max) |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
103 |
} |
31522 | 104 |
private val table = |
105 |
{ |
|
106 |
val table = new jcl.HashMap[String, String] // reasonably efficient? |
|
27937
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
107 |
for ((x, y) <- list) table + (x -> y) |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
108 |
table |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
109 |
} |
31522 | 110 |
def recode(text: String): String = |
111 |
{ |
|
27937
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
112 |
val len = text.length |
31522 | 113 |
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
|
114 |
val result = new StringBuilder(len) |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
115 |
var i = 0 |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
116 |
while (i < len) { |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
117 |
val c = text(i) |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
118 |
if (min <= c && c <= max) { |
31929 | 119 |
matcher.region(i, len).lookingAt |
27938 | 120 |
val x = matcher.group |
31522 | 121 |
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
|
122 |
i = matcher.end |
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 |
else { result.append(c); i += 1 } |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
125 |
} |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
126 |
result.toString |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
127 |
} |
fdf77e7be01a
more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents:
27935
diff
changeset
|
128 |
} |
27924 | 129 |
|
27918 | 130 |
|
27923
7ebe9d38743a
use scala.collection.jcl.HashMap, which seems to be more efficient;
wenzelm
parents:
27918
diff
changeset
|
131 |
|
27927 | 132 |
/** Symbol interpretation **/ |
133 |
||
29569
f3f529b5d8fb
more general init of Symbol.Interpretation, independent of IsabelleSystem instance;
wenzelm
parents:
29174
diff
changeset
|
134 |
class Interpretation(symbol_decls: Iterator[String]) |
f3f529b5d8fb
more general init of Symbol.Interpretation, independent of IsabelleSystem instance;
wenzelm
parents:
29174
diff
changeset
|
135 |
{ |
31522 | 136 |
/* read symbols */ |
137 |
||
138 |
private val empty = new Regex("""(?xs) ^\s* (?: \#.* )? $ """) |
|
139 |
private val key = new Regex("""(?xs) (.+): """) |
|
140 |
||
141 |
private def read_decl(decl: String): (String, Map[String, String]) = |
|
142 |
{ |
|
143 |
def err() = error("Bad symbol declaration: " + decl) |
|
144 |
||
145 |
def read_props(props: List[String]): Map[String, String] = |
|
146 |
{ |
|
147 |
props match { |
|
148 |
case Nil => Map() |
|
149 |
case _ :: Nil => err() |
|
150 |
case key(x) :: y :: rest => read_props(rest) + (x -> y) |
|
151 |
case _ => err() |
|
152 |
} |
|
153 |
} |
|
154 |
decl.split("\\s+").toList match { |
|
155 |
case Nil => err() |
|
156 |
case sym :: props => (sym, read_props(props)) |
|
157 |
} |
|
158 |
} |
|
159 |
||
160 |
private val symbols: List[(String, Map[String, String])] = |
|
161 |
for (decl <- symbol_decls.toList if !empty.pattern.matcher(decl).matches) |
|
162 |
yield read_decl(decl) |
|
163 |
||
164 |
||
31651 | 165 |
/* misc properties */ |
166 |
||
167 |
val names: Map[String, String] = { |
|
168 |
val name = new Regex("""\\<([A-Za-z][A-Za-z0-9_']*)>""") |
|
169 |
Map((for ((sym @ name(a), _) <- symbols) yield (sym -> a)): _*) |
|
170 |
} |
|
171 |
||
172 |
val abbrevs: Map[String, String] = Map(( |
|
173 |
for ((sym, props) <- symbols if props.isDefinedAt("abbrev")) |
|
174 |
yield (sym -> props("abbrev"))): _*) |
|
175 |
||
176 |
||
31522 | 177 |
/* main recoder methods */ |
178 |
||
179 |
private val (decoder, encoder) = |
|
180 |
{ |
|
181 |
val mapping = |
|
182 |
for { |
|
183 |
(sym, props) <- symbols |
|
184 |
val code = |
|
185 |
try { Integer.decode(props("code")).intValue } |
|
186 |
catch { |
|
187 |
case _: NoSuchElementException => error("Missing code for symbol " + sym) |
|
188 |
case _: NumberFormatException => error("Bad code for symbol " + sym) |
|
189 |
} |
|
190 |
val ch = new String(Character.toChars(code)) |
|
191 |
} yield (sym, ch) |
|
31545
5f1f0a20af4d
discontinued escaped symbols such as \\<forall> -- only one backslash should be used;
wenzelm
parents:
31523
diff
changeset
|
192 |
(new Recoder(mapping), |
31548 | 193 |
new Recoder(mapping map { case (x, y) => (y, x) })) |
31522 | 194 |
} |
27918 | 195 |
|
27924 | 196 |
def decode(text: String) = decoder.recode(text) |
197 |
def encode(text: String) = encoder.recode(text) |
|
27923
7ebe9d38743a
use scala.collection.jcl.HashMap, which seems to be more efficient;
wenzelm
parents:
27918
diff
changeset
|
198 |
|
27918 | 199 |
} |
27901 | 200 |
} |