author | wenzelm |
Sat, 07 Jul 2007 00:14:59 +0200 | |
changeset 23618 | 32ee8cac5c02 |
parent 21897 | d0c67d715deb |
child 23676 | ea9b7e9c2301 |
permissions | -rw-r--r-- |
6118 | 1 |
(* Title: Pure/General/symbol.ML |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
2 |
ID: $Id$ |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
3 |
Author: Markus Wenzel, TU Muenchen |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
4 |
|
21897 | 5 |
Generalized characters with infinitely many named symbols. |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
6 |
*) |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
7 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
8 |
signature SYMBOL = |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
9 |
sig |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
10 |
type symbol |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
11 |
val space: symbol |
23618 | 12 |
val spaces: int -> string |
14678 | 13 |
val is_char: symbol -> bool |
14 |
val is_symbolic: symbol -> bool |
|
15 |
val is_printable: symbol -> bool |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
16 |
val eof: symbol |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
17 |
val is_eof: symbol -> bool |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
18 |
val not_eof: symbol -> bool |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
19 |
val stopper: symbol * (symbol -> bool) |
14678 | 20 |
val sync: symbol |
21 |
val is_sync: symbol -> bool |
|
22 |
val not_sync: symbol -> bool |
|
23 |
val malformed: symbol |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
24 |
val is_ascii: symbol -> bool |
14678 | 25 |
val is_ascii_letter: symbol -> bool |
20067
26bac504ef90
hex and binary numerals (contributed by Rafal Kolanski)
kleing
parents:
19473
diff
changeset
|
26 |
val is_hex_letter: symbol -> bool |
14678 | 27 |
val is_ascii_digit: symbol -> bool |
28 |
val is_ascii_quasi: symbol -> bool |
|
29 |
val is_ascii_blank: symbol -> bool |
|
20200 | 30 |
val is_ascii_lower: symbol -> bool |
31 |
val is_ascii_upper: symbol -> bool |
|
32 |
val to_ascii_lower: symbol -> symbol |
|
33 |
val to_ascii_upper: symbol -> symbol |
|
14834 | 34 |
val is_raw: symbol -> bool |
35 |
val decode_raw: symbol -> string |
|
14977
77d88064991a
added escape, export encode_raw, default mode now trivial, tuned;
wenzelm
parents:
14961
diff
changeset
|
36 |
val encode_raw: string -> string |
14873 | 37 |
datatype sym = Char of string | Sym of string | Ctrl of string | Raw of string |
38 |
val decode: symbol -> sym |
|
14678 | 39 |
datatype kind = Letter | Digit | Quasi | Blank | Other |
40 |
val kind: symbol -> kind |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
41 |
val is_letter: symbol -> bool |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
42 |
val is_digit: symbol -> bool |
12904 | 43 |
val is_quasi: symbol -> bool |
14678 | 44 |
val is_blank: symbol -> bool |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
45 |
val is_quasi_letter: symbol -> bool |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
46 |
val is_letdig: symbol -> bool |
16138 | 47 |
val is_ident: symbol list -> bool |
14728 | 48 |
val beginning: int -> symbol list -> string |
14678 | 49 |
val scanner: string -> (string list -> 'a * string list) -> symbol list -> 'a |
13730 | 50 |
val scan_id: string list -> string * string list |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
51 |
val scan: string list -> symbol * string list |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
52 |
val source: bool -> (string, 'a) Source.source -> |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
53 |
(symbol, (string, 'a) Source.source) Source.source |
6272 | 54 |
val explode: string -> symbol list |
14994 | 55 |
val chars_only: string -> bool |
14977
77d88064991a
added escape, export encode_raw, default mode now trivial, tuned;
wenzelm
parents:
14961
diff
changeset
|
56 |
val escape: string -> string |
14678 | 57 |
val strip_blanks: string -> string |
58 |
val bump_init: string -> string |
|
12904 | 59 |
val bump_string: string -> string |
14678 | 60 |
val length: symbol list -> int |
6692 | 61 |
val xsymbolsN: string |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
62 |
end; |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
63 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
64 |
structure Symbol: SYMBOL = |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
65 |
struct |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
66 |
|
14678 | 67 |
(** type symbol **) |
6272 | 68 |
|
14678 | 69 |
(*Symbols, which are considered the smallest entities of any Isabelle |
6272 | 70 |
string, may be of the following form: |
14678 | 71 |
|
14834 | 72 |
(1) ASCII symbols: a |
17823 | 73 |
(2) regular symbols: \<ident> |
14834 | 74 |
(3) control symbols: \<^ident> |
75 |
(4) raw control symbols: \<^raw:...>, where "..." may be any printable |
|
20205
7b2958d3d575
raw symbols: disallow dot to avoid confusion in NameSpace.unpack;
wenzelm
parents:
20200
diff
changeset
|
76 |
character (excluding ".", ">"), or \<^raw000> |
6272 | 77 |
|
14678 | 78 |
Output is subject to the print_mode variable (default: verbatim), |
79 |
actual interpretation in display is up to front-end tools. |
|
6272 | 80 |
*) |
81 |
||
82 |
type symbol = string; |
|
83 |
||
84 |
val space = " "; |
|
17063 | 85 |
|
86 |
local |
|
87 |
val small_spaces = Vector.tabulate (65, fn i => Library.replicate_string i space); |
|
88 |
in |
|
89 |
fun spaces k = |
|
90 |
if k < 64 then Vector.sub (small_spaces, k) |
|
91 |
else Library.replicate_string (k div 64) (Vector.sub (small_spaces, 64)) ^ |
|
92 |
Vector.sub (small_spaces, k mod 64); |
|
93 |
end; |
|
14678 | 94 |
|
95 |
fun is_char s = size s = 1; |
|
96 |
||
97 |
fun is_symbolic s = |
|
98 |
String.isPrefix "\\<" s andalso not (String.isPrefix "\\<^" s); |
|
99 |
||
100 |
fun is_printable s = |
|
101 |
if is_char s then ord space <= ord s andalso ord s <= ord "~" |
|
102 |
else not (String.isPrefix "\\<^" s); |
|
6272 | 103 |
|
104 |
||
14678 | 105 |
(* input source control *) |
6272 | 106 |
|
14678 | 107 |
val eof = ""; |
6272 | 108 |
fun is_eof s = s = eof; |
109 |
fun not_eof s = s <> eof; |
|
110 |
val stopper = (eof, is_eof); |
|
111 |
||
14678 | 112 |
val sync = "\\<^sync>"; |
113 |
fun is_sync s = s = sync; |
|
114 |
fun not_sync s = s <> sync; |
|
115 |
||
116 |
val malformed = "\\<^malformed>"; |
|
117 |
||
118 |
||
119 |
(* ascii symbols *) |
|
120 |
||
121 |
fun is_ascii s = is_char s andalso ord s < 128; |
|
122 |
||
123 |
fun is_ascii_letter s = |
|
124 |
is_char s andalso |
|
125 |
(ord "A" <= ord s andalso ord s <= ord "Z" orelse |
|
126 |
ord "a" <= ord s andalso ord s <= ord "z"); |
|
127 |
||
20067
26bac504ef90
hex and binary numerals (contributed by Rafal Kolanski)
kleing
parents:
19473
diff
changeset
|
128 |
fun is_hex_letter s = |
26bac504ef90
hex and binary numerals (contributed by Rafal Kolanski)
kleing
parents:
19473
diff
changeset
|
129 |
is_char s andalso |
26bac504ef90
hex and binary numerals (contributed by Rafal Kolanski)
kleing
parents:
19473
diff
changeset
|
130 |
(ord "A" <= ord s andalso ord s <= ord "F" orelse |
26bac504ef90
hex and binary numerals (contributed by Rafal Kolanski)
kleing
parents:
19473
diff
changeset
|
131 |
ord "a" <= ord s andalso ord s <= ord "f"); |
26bac504ef90
hex and binary numerals (contributed by Rafal Kolanski)
kleing
parents:
19473
diff
changeset
|
132 |
|
14678 | 133 |
fun is_ascii_digit s = |
134 |
is_char s andalso ord "0" <= ord s andalso ord s <= ord "9"; |
|
135 |
||
136 |
fun is_ascii_quasi "_" = true |
|
137 |
| is_ascii_quasi "'" = true |
|
138 |
| is_ascii_quasi _ = false; |
|
139 |
||
140 |
val is_ascii_blank = |
|
17756 | 141 |
fn " " => true | "\t" => true | "\n" => true | "\^L" => true | "\^M" => true |
14678 | 142 |
| _ => false; |
143 |
||
20200 | 144 |
fun is_ascii_lower s = is_char s andalso (ord "a" <= ord s andalso ord s <= ord "z"); |
145 |
fun is_ascii_upper s = is_char s andalso (ord "A" <= ord s andalso ord s <= ord "Z"); |
|
146 |
||
147 |
fun to_ascii_lower s = if is_ascii_upper s then chr (ord s + ord "a" - ord "A") else s; |
|
148 |
fun to_ascii_upper s = if is_ascii_lower s then chr (ord s + ord "A" - ord "a") else s; |
|
149 |
||
14678 | 150 |
|
14956
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
151 |
(* encode_raw *) |
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
152 |
|
20205
7b2958d3d575
raw symbols: disallow dot to avoid confusion in NameSpace.unpack;
wenzelm
parents:
20200
diff
changeset
|
153 |
fun raw_chr c = |
7b2958d3d575
raw symbols: disallow dot to avoid confusion in NameSpace.unpack;
wenzelm
parents:
20200
diff
changeset
|
154 |
ord space <= ord c andalso ord c <= ord "~" andalso c <> "." andalso c <> ">" |
17823 | 155 |
orelse ord c >= 128; |
14956
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
156 |
|
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
157 |
fun encode_raw str = |
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
158 |
let |
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
159 |
val raw1 = enclose "\\<^raw:" ">" o implode; |
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
160 |
val raw2 = enclose "\\<^raw" ">" o string_of_int o ord; |
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
161 |
|
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
162 |
fun encode cs = enc (Library.take_prefix raw_chr cs) |
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
163 |
and enc ([], []) = [] |
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
164 |
| enc (cs, []) = [raw1 cs] |
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
165 |
| enc ([], d :: ds) = raw2 d :: encode ds |
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
166 |
| enc (cs, d :: ds) = raw1 cs :: raw2 d :: encode ds; |
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
167 |
in |
14977
77d88064991a
added escape, export encode_raw, default mode now trivial, tuned;
wenzelm
parents:
14961
diff
changeset
|
168 |
if exists_string (not o raw_chr) str then implode (encode (explode str)) |
77d88064991a
added escape, export encode_raw, default mode now trivial, tuned;
wenzelm
parents:
14961
diff
changeset
|
169 |
else enclose "\\<^raw:" ">" str |
14956
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
170 |
end; |
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
171 |
|
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
172 |
|
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
173 |
(* diagnostics *) |
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
174 |
|
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
175 |
fun beginning n cs = |
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
176 |
let |
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
177 |
val drop_blanks = #1 o Library.take_suffix is_ascii_blank; |
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
178 |
val all_cs = drop_blanks cs; |
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
179 |
val dots = if length all_cs > n then " ..." else ""; |
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
180 |
in |
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
181 |
(drop_blanks (Library.take (n, all_cs)) |
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
182 |
|> map (fn c => if is_ascii_blank c then space else c) |
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
183 |
|> implode) ^ dots |
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
184 |
end; |
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
185 |
|
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
186 |
(*raw encoding avoids looping errors!*) |
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
187 |
fun malformed_symbol s = |
23618 | 188 |
"Malformed symbolic character specification: " ^ quote (Output.escape s); |
14956
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
189 |
|
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
190 |
|
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
191 |
(* decode_raw *) |
14834 | 192 |
|
193 |
fun is_raw s = |
|
17063 | 194 |
String.isPrefix "\\<^raw" s andalso String.isSuffix ">" s; |
14834 | 195 |
|
196 |
fun decode_raw s = |
|
14956
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
197 |
if not (is_raw s) then error (malformed_symbol s) |
14834 | 198 |
else if String.isPrefix "\\<^raw:" s then String.substring (s, 7, size s - 8) |
199 |
else chr (#1 (Library.read_int (explode (String.substring (s, 6, size s - 7))))); |
|
200 |
||
201 |
||
14873 | 202 |
(* symbol variants *) |
203 |
||
204 |
datatype sym = Char of string | Sym of string | Ctrl of string | Raw of string; |
|
205 |
||
206 |
fun decode s = |
|
207 |
if is_char s then Char s |
|
208 |
else if is_raw s then Raw (decode_raw s) |
|
209 |
else if String.isPrefix "\\<^" s then Ctrl (String.substring (s, 3, size s - 4)) |
|
210 |
else if String.isPrefix "\\<" s then Sym (String.substring (s, 2, size s - 3)) |
|
14956
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
211 |
else error (malformed_symbol s); |
14873 | 212 |
|
213 |
||
14678 | 214 |
(* standard symbol kinds *) |
215 |
||
216 |
datatype kind = Letter | Digit | Quasi | Blank | Other; |
|
6272 | 217 |
|
14171
0cab06e3bbd0
Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents:
13730
diff
changeset
|
218 |
local |
14678 | 219 |
val symbol_kinds = Symtab.make |
220 |
[("\\<A>", Letter), |
|
221 |
("\\<B>", Letter), |
|
222 |
("\\<C>", Letter), |
|
223 |
("\\<D>", Letter), |
|
224 |
("\\<E>", Letter), |
|
225 |
("\\<F>", Letter), |
|
226 |
("\\<G>", Letter), |
|
227 |
("\\<H>", Letter), |
|
228 |
("\\<I>", Letter), |
|
229 |
("\\<J>", Letter), |
|
230 |
("\\<K>", Letter), |
|
231 |
("\\<L>", Letter), |
|
232 |
("\\<M>", Letter), |
|
233 |
("\\<N>", Letter), |
|
234 |
("\\<O>", Letter), |
|
235 |
("\\<P>", Letter), |
|
236 |
("\\<Q>", Letter), |
|
237 |
("\\<R>", Letter), |
|
238 |
("\\<S>", Letter), |
|
239 |
("\\<T>", Letter), |
|
240 |
("\\<U>", Letter), |
|
241 |
("\\<V>", Letter), |
|
242 |
("\\<W>", Letter), |
|
243 |
("\\<X>", Letter), |
|
244 |
("\\<Y>", Letter), |
|
245 |
("\\<Z>", Letter), |
|
246 |
("\\<a>", Letter), |
|
247 |
("\\<b>", Letter), |
|
248 |
("\\<c>", Letter), |
|
249 |
("\\<d>", Letter), |
|
250 |
("\\<e>", Letter), |
|
251 |
("\\<f>", Letter), |
|
252 |
("\\<g>", Letter), |
|
253 |
("\\<h>", Letter), |
|
254 |
("\\<i>", Letter), |
|
255 |
("\\<j>", Letter), |
|
256 |
("\\<k>", Letter), |
|
257 |
("\\<l>", Letter), |
|
258 |
("\\<m>", Letter), |
|
259 |
("\\<n>", Letter), |
|
260 |
("\\<o>", Letter), |
|
261 |
("\\<p>", Letter), |
|
262 |
("\\<q>", Letter), |
|
263 |
("\\<r>", Letter), |
|
264 |
("\\<s>", Letter), |
|
265 |
("\\<t>", Letter), |
|
266 |
("\\<u>", Letter), |
|
267 |
("\\<v>", Letter), |
|
268 |
("\\<w>", Letter), |
|
269 |
("\\<x>", Letter), |
|
270 |
("\\<y>", Letter), |
|
271 |
("\\<z>", Letter), |
|
272 |
("\\<AA>", Letter), |
|
273 |
("\\<BB>", Letter), |
|
274 |
("\\<CC>", Letter), |
|
275 |
("\\<DD>", Letter), |
|
276 |
("\\<EE>", Letter), |
|
277 |
("\\<FF>", Letter), |
|
278 |
("\\<GG>", Letter), |
|
279 |
("\\<HH>", Letter), |
|
280 |
("\\<II>", Letter), |
|
281 |
("\\<JJ>", Letter), |
|
282 |
("\\<KK>", Letter), |
|
283 |
("\\<LL>", Letter), |
|
284 |
("\\<MM>", Letter), |
|
285 |
("\\<NN>", Letter), |
|
286 |
("\\<OO>", Letter), |
|
287 |
("\\<PP>", Letter), |
|
288 |
("\\<QQ>", Letter), |
|
289 |
("\\<RR>", Letter), |
|
290 |
("\\<SS>", Letter), |
|
291 |
("\\<TT>", Letter), |
|
292 |
("\\<UU>", Letter), |
|
293 |
("\\<VV>", Letter), |
|
294 |
("\\<WW>", Letter), |
|
295 |
("\\<XX>", Letter), |
|
296 |
("\\<YY>", Letter), |
|
297 |
("\\<ZZ>", Letter), |
|
298 |
("\\<aa>", Letter), |
|
299 |
("\\<bb>", Letter), |
|
300 |
("\\<cc>", Letter), |
|
301 |
("\\<dd>", Letter), |
|
302 |
("\\<ee>", Letter), |
|
303 |
("\\<ff>", Letter), |
|
304 |
("\\<gg>", Letter), |
|
305 |
("\\<hh>", Letter), |
|
306 |
("\\<ii>", Letter), |
|
307 |
("\\<jj>", Letter), |
|
308 |
("\\<kk>", Letter), |
|
309 |
("\\<ll>", Letter), |
|
310 |
("\\<mm>", Letter), |
|
311 |
("\\<nn>", Letter), |
|
312 |
("\\<oo>", Letter), |
|
313 |
("\\<pp>", Letter), |
|
314 |
("\\<qq>", Letter), |
|
315 |
("\\<rr>", Letter), |
|
316 |
("\\<ss>", Letter), |
|
317 |
("\\<tt>", Letter), |
|
318 |
("\\<uu>", Letter), |
|
319 |
("\\<vv>", Letter), |
|
320 |
("\\<ww>", Letter), |
|
321 |
("\\<xx>", Letter), |
|
322 |
("\\<yy>", Letter), |
|
323 |
("\\<zz>", Letter), |
|
324 |
("\\<alpha>", Letter), |
|
325 |
("\\<beta>", Letter), |
|
326 |
("\\<gamma>", Letter), |
|
327 |
("\\<delta>", Letter), |
|
328 |
("\\<epsilon>", Letter), |
|
329 |
("\\<zeta>", Letter), |
|
330 |
("\\<eta>", Letter), |
|
331 |
("\\<theta>", Letter), |
|
332 |
("\\<iota>", Letter), |
|
333 |
("\\<kappa>", Letter), |
|
334 |
("\\<lambda>", Other), (*sic!*) |
|
335 |
("\\<mu>", Letter), |
|
336 |
("\\<nu>", Letter), |
|
337 |
("\\<xi>", Letter), |
|
338 |
("\\<pi>", Letter), |
|
339 |
("\\<rho>", Letter), |
|
340 |
("\\<sigma>", Letter), |
|
341 |
("\\<tau>", Letter), |
|
342 |
("\\<upsilon>", Letter), |
|
343 |
("\\<phi>", Letter), |
|
344 |
("\\<psi>", Letter), |
|
345 |
("\\<omega>", Letter), |
|
346 |
("\\<Gamma>", Letter), |
|
347 |
("\\<Delta>", Letter), |
|
348 |
("\\<Theta>", Letter), |
|
349 |
("\\<Lambda>", Letter), |
|
350 |
("\\<Xi>", Letter), |
|
351 |
("\\<Pi>", Letter), |
|
352 |
("\\<Sigma>", Letter), |
|
353 |
("\\<Upsilon>", Letter), |
|
354 |
("\\<Phi>", Letter), |
|
355 |
("\\<Psi>", Letter), |
|
356 |
("\\<Omega>", Letter), |
|
14961 | 357 |
("\\<^isub>", Letter), |
358 |
("\\<^isup>", Letter), |
|
14678 | 359 |
("\\<spacespace>", Blank)]; |
14171
0cab06e3bbd0
Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents:
13730
diff
changeset
|
360 |
in |
14678 | 361 |
fun kind s = |
362 |
if is_ascii_letter s then Letter |
|
363 |
else if is_ascii_digit s then Digit |
|
364 |
else if is_ascii_quasi s then Quasi |
|
365 |
else if is_ascii_blank s then Blank |
|
366 |
else if is_char s then Other |
|
18939 | 367 |
else the_default Other (Symtab.lookup symbol_kinds s); |
14678 | 368 |
end; |
14173 | 369 |
|
14678 | 370 |
fun is_letter s = kind s = Letter; |
371 |
fun is_digit s = kind s = Digit; |
|
372 |
fun is_quasi s = kind s = Quasi; |
|
373 |
fun is_blank s = kind s = Blank; |
|
6272 | 374 |
|
14678 | 375 |
fun is_quasi_letter s = let val k = kind s in k = Letter orelse k = Quasi end; |
376 |
fun is_letdig s = let val k = kind s in k = Letter orelse k = Digit orelse k = Quasi end; |
|
11010 | 377 |
|
16138 | 378 |
fun is_ident [] = false |
379 |
| is_ident (c :: cs) = is_letter c andalso forall is_letdig cs; |
|
380 |
||
6272 | 381 |
|
382 |
||
14678 | 383 |
(** symbol input **) |
384 |
||
385 |
(* scanning through symbols *) |
|
6272 | 386 |
|
6640 | 387 |
fun scanner msg scan chs = |
388 |
let |
|
15531 | 389 |
fun message (cs, NONE) = msg ^ ": " ^ quote (beginning 10 cs) |
390 |
| message (cs, SOME msg') = msg ^ ", " ^ msg' ^ ": " ^ quote (beginning 10 cs); |
|
14961 | 391 |
val fin_scan = Scan.error (Scan.finite stopper (!! message scan)); |
6640 | 392 |
in |
393 |
(case fin_scan chs of |
|
394 |
(result, []) => result |
|
15531 | 395 |
| (_, rest) => error (message (rest, NONE))) |
6640 | 396 |
end; |
397 |
||
398 |
||
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
399 |
(* scan *) |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
400 |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21495
diff
changeset
|
401 |
val scan_id = Scan.one is_letter ^^ (Scan.many is_letdig >> implode); |
14678 | 402 |
|
403 |
local |
|
14561
c53396af770e
* raw control symbols are of the form \<^raw:...> now.
schirmer
parents:
14559
diff
changeset
|
404 |
|
14678 | 405 |
val scan_encoded_newline = |
17756 | 406 |
$$ "\^M" -- $$ "\n" >> K "\n" || |
407 |
$$ "\^M" >> K "\n" || |
|
14956
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
408 |
$$ "\\" -- Scan.optional ($$ "\\") "" -- Scan.this_string "<^newline>" >> K "\n"; |
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
409 |
|
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
410 |
val scan_raw = |
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21495
diff
changeset
|
411 |
Scan.this_string "raw:" ^^ (Scan.many raw_chr >> implode) || |
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21495
diff
changeset
|
412 |
Scan.this_string "raw" ^^ (Scan.many1 is_ascii_digit >> implode); |
14678 | 413 |
|
414 |
in |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
415 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
416 |
val scan = |
14678 | 417 |
scan_encoded_newline || |
14956
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
418 |
(($$ "\\" --| Scan.optional ($$ "\\") "") ^^ $$ "<" ^^ |
14961 | 419 |
!! (fn (cs, _) => malformed_symbol (quote (beginning 10 ("\\" :: "<" :: cs)))) |
14956
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
420 |
(($$ "^" ^^ (scan_raw || scan_id) || scan_id) ^^ $$ ">")) || |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
421 |
Scan.one not_eof; |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
422 |
|
14678 | 423 |
end; |
424 |
||
14562
980da32f4617
proper handling of lines terminated by CRLF or CR;
wenzelm
parents:
14561
diff
changeset
|
425 |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
426 |
(* source *) |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
427 |
|
14956
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
428 |
val recover = |
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21495
diff
changeset
|
429 |
Scan.many (fn s => not (is_blank s) andalso s <> "\"" andalso not_eof s) >> K [malformed]; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
430 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
431 |
fun source do_recover src = |
15531 | 432 |
Source.source stopper (Scan.bulk scan) (if do_recover then SOME recover else NONE) src; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
433 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
434 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
435 |
(* explode *) |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
436 |
|
14562
980da32f4617
proper handling of lines terminated by CRLF or CR;
wenzelm
parents:
14561
diff
changeset
|
437 |
fun no_explode [] = true |
980da32f4617
proper handling of lines terminated by CRLF or CR;
wenzelm
parents:
14561
diff
changeset
|
438 |
| no_explode ("\\" :: "<" :: _) = false |
17756 | 439 |
| no_explode ("\^M" :: _) = false |
14562
980da32f4617
proper handling of lines terminated by CRLF or CR;
wenzelm
parents:
14561
diff
changeset
|
440 |
| no_explode (_ :: cs) = no_explode cs; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
441 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
442 |
fun sym_explode str = |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
443 |
let val chs = explode str in |
14562
980da32f4617
proper handling of lines terminated by CRLF or CR;
wenzelm
parents:
14561
diff
changeset
|
444 |
if no_explode chs then chs |
16002 | 445 |
else the (Scan.read stopper (Scan.repeat scan) chs) |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
446 |
end; |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
447 |
|
19305 | 448 |
val chars_only = not o exists_string (fn s => s = "\\"); |
14994 | 449 |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
450 |
|
14977
77d88064991a
added escape, export encode_raw, default mode now trivial, tuned;
wenzelm
parents:
14961
diff
changeset
|
451 |
(* escape *) |
77d88064991a
added escape, export encode_raw, default mode now trivial, tuned;
wenzelm
parents:
14961
diff
changeset
|
452 |
|
77d88064991a
added escape, export encode_raw, default mode now trivial, tuned;
wenzelm
parents:
14961
diff
changeset
|
453 |
val escape = implode o map (fn s => if is_char s then s else "\\" ^ s) o sym_explode; |
77d88064991a
added escape, export encode_raw, default mode now trivial, tuned;
wenzelm
parents:
14961
diff
changeset
|
454 |
|
77d88064991a
added escape, export encode_raw, default mode now trivial, tuned;
wenzelm
parents:
14961
diff
changeset
|
455 |
|
14678 | 456 |
(* blanks *) |
457 |
||
458 |
fun strip_blanks s = |
|
459 |
sym_explode s |
|
460 |
|> Library.take_prefix is_blank |> #2 |
|
461 |
|> Library.take_suffix is_blank |> #1 |
|
462 |
|> implode; |
|
463 |
||
464 |
||
465 |
(* bump string -- treat as base 26 or base 1 numbers *) |
|
466 |
||
15979 | 467 |
fun symbolic_end (_ :: "\\<^isub>" :: _) = true |
468 |
| symbolic_end (_ :: "\\<^isup>" :: _) = true |
|
14908 | 469 |
| symbolic_end (s :: _) = is_symbolic s |
470 |
| symbolic_end [] = false; |
|
14678 | 471 |
|
472 |
fun bump_init str = |
|
14908 | 473 |
if symbolic_end (rev (sym_explode str)) then str ^ "'" |
14678 | 474 |
else str ^ "a"; |
12904 | 475 |
|
476 |
fun bump_string str = |
|
477 |
let |
|
478 |
fun bump [] = ["a"] |
|
479 |
| bump ("z" :: ss) = "a" :: bump ss |
|
480 |
| bump (s :: ss) = |
|
14678 | 481 |
if is_char s andalso ord "a" <= ord s andalso ord s < ord "z" |
12904 | 482 |
then chr (ord s + 1) :: ss |
483 |
else "a" :: s :: ss; |
|
14678 | 484 |
|
485 |
val (ss, qs) = apfst rev (Library.take_suffix is_quasi (sym_explode str)); |
|
14908 | 486 |
val ss' = if symbolic_end ss then "'" :: ss else bump ss; |
14678 | 487 |
in implode (rev ss' @ qs) end; |
488 |
||
12904 | 489 |
|
6272 | 490 |
|
23618 | 491 |
(** xsymbols **) |
14977
77d88064991a
added escape, export encode_raw, default mode now trivial, tuned;
wenzelm
parents:
14961
diff
changeset
|
492 |
|
77d88064991a
added escape, export encode_raw, default mode now trivial, tuned;
wenzelm
parents:
14961
diff
changeset
|
493 |
val xsymbolsN = "xsymbols"; |
6272 | 494 |
|
14678 | 495 |
fun sym_len s = |
496 |
if not (is_printable s) then 0 |
|
497 |
else if String.isPrefix "\\<long" s then 2 |
|
498 |
else if String.isPrefix "\\<Long" s then 2 |
|
499 |
else if s = "\\<spacespace>" then 2 |
|
500 |
else 1; |
|
501 |
||
19473 | 502 |
fun sym_length ss = fold (fn s => fn n => sym_len s + n) ss 0; |
14678 | 503 |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
504 |
(*final declarations of this structure!*) |
6272 | 505 |
val length = sym_length; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
506 |
val explode = sym_explode; |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
507 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
508 |
end; |