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