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