author | paulson <lp15@cam.ac.uk> |
Wed, 18 Mar 2015 14:13:27 +0000 | |
changeset 59741 | 5b762cd73a8e |
parent 58864 | 505a8150368a |
child 61435 | 636bb75e7683 |
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 |
14678 | 13 |
val is_char: symbol -> bool |
37533
d775bd70f571
explicit treatment of UTF8 character sequences as Isabelle symbols;
wenzelm
parents:
34095
diff
changeset
|
14 |
val is_utf8: symbol -> bool |
14678 | 15 |
val is_symbolic: symbol -> bool |
55033 | 16 |
val is_symbolic_char: symbol -> bool |
14678 | 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 |
40523
1050315f6ee2
simplified/robustified treatment of malformed symbols, which are now fully internalized (total Symbol.explode etc.);
wenzelm
parents:
40509
diff
changeset
|
22 |
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
|
23 |
val malformed_msg: symbol -> string |
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 |
26 |
val is_ascii_digit: symbol -> bool |
|
24580
916259859344
replaced Symbol.is_hex_letter to Symbol.is_ascii_hex;
wenzelm
parents:
23784
diff
changeset
|
27 |
val is_ascii_hex: symbol -> bool |
14678 | 28 |
val is_ascii_quasi: symbol -> bool |
29 |
val is_ascii_blank: symbol -> bool |
|
34095
c2f176a38448
robust representation of low ASCII control characters within XML/YXML text;
wenzelm
parents:
33955
diff
changeset
|
30 |
val is_ascii_control: symbol -> bool |
50236 | 31 |
val is_ascii_letdig: symbol -> bool |
20200 | 32 |
val is_ascii_lower: symbol -> bool |
33 |
val is_ascii_upper: symbol -> bool |
|
34 |
val to_ascii_lower: symbol -> symbol |
|
35 |
val to_ascii_upper: symbol -> symbol |
|
50238
98d35a7368bd
more uniform Symbol.is_ascii_identifier in ML/Scala;
wenzelm
parents:
50237
diff
changeset
|
36 |
val is_ascii_identifier: string -> bool |
50236 | 37 |
val scan_ascii_id: string list -> string * string list |
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 |
14728 | 54 |
val beginning: int -> symbol list -> string |
40523
1050315f6ee2
simplified/robustified treatment of malformed symbols, which are now fully internalized (total Symbol.explode etc.);
wenzelm
parents:
40509
diff
changeset
|
55 |
val source: (string, 'a) Source.source -> (symbol, (string, 'a) Source.source) Source.source |
6272 | 56 |
val explode: string -> symbol list |
50237 | 57 |
val esc: symbol -> string |
58 |
val escape: string -> string |
|
59 |
val scanner: string -> (string list -> 'a * string list) -> symbol list -> 'a |
|
50162 | 60 |
val split_words: symbol list -> string list |
61 |
val explode_words: string -> string list |
|
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 |
|
14678 | 95 |
fun is_char s = size s = 1; |
96 |
||
37533
d775bd70f571
explicit treatment of UTF8 character sequences as Isabelle symbols;
wenzelm
parents:
34095
diff
changeset
|
97 |
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
|
98 |
|
55033 | 99 |
fun raw_symbolic s = |
100 |
String.isPrefix "\\<" s andalso String.isSuffix ">" s andalso not (String.isPrefix "\\<^" s); |
|
101 |
||
14678 | 102 |
fun is_symbolic s = |
55033 | 103 |
s <> "\\<open>" andalso s <> "\\<close>" andalso raw_symbolic s; |
104 |
||
105 |
val is_symbolic_char = member (op =) (raw_explode "!#$%&*+-/<=>?@^_|~"); |
|
14678 | 106 |
|
107 |
fun is_printable s = |
|
108 |
if is_char s then ord space <= ord s andalso ord s <= ord "~" |
|
55033 | 109 |
else is_utf8 s orelse raw_symbolic s; |
26632 | 110 |
|
6272 | 111 |
|
14678 | 112 |
(* input source control *) |
6272 | 113 |
|
14678 | 114 |
val eof = ""; |
6272 | 115 |
fun is_eof s = s = eof; |
116 |
fun not_eof s = s <> eof; |
|
27732 | 117 |
val stopper = Scan.stopper (K eof) is_eof; |
6272 | 118 |
|
48774 | 119 |
fun is_malformed s = |
120 |
String.isPrefix "\\<" s andalso not (String.isSuffix ">" s) |
|
121 |
orelse s = "\\<>" orelse s = "\\<^>"; |
|
122 |
||
40523
1050315f6ee2
simplified/robustified treatment of malformed symbols, which are now fully internalized (total Symbol.explode etc.);
wenzelm
parents:
40509
diff
changeset
|
123 |
fun malformed_msg s = "Malformed symbolic character: " ^ quote s; |
14678 | 124 |
|
125 |
||
43418 | 126 |
(* ASCII symbols *) |
14678 | 127 |
|
128 |
fun is_ascii s = is_char s andalso ord s < 128; |
|
129 |
||
130 |
fun is_ascii_letter s = |
|
131 |
is_char s andalso |
|
132 |
(ord "A" <= ord s andalso ord s <= ord "Z" orelse |
|
133 |
ord "a" <= ord s andalso ord s <= ord "z"); |
|
134 |
||
135 |
fun is_ascii_digit s = |
|
136 |
is_char s andalso ord "0" <= ord s andalso ord s <= ord "9"; |
|
137 |
||
24580
916259859344
replaced Symbol.is_hex_letter to Symbol.is_ascii_hex;
wenzelm
parents:
23784
diff
changeset
|
138 |
fun is_ascii_hex s = |
916259859344
replaced Symbol.is_hex_letter to Symbol.is_ascii_hex;
wenzelm
parents:
23784
diff
changeset
|
139 |
is_char s andalso |
916259859344
replaced Symbol.is_hex_letter to Symbol.is_ascii_hex;
wenzelm
parents:
23784
diff
changeset
|
140 |
(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
|
141 |
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
|
142 |
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
|
143 |
|
14678 | 144 |
fun is_ascii_quasi "_" = true |
145 |
| is_ascii_quasi "'" = true |
|
146 |
| is_ascii_quasi _ = false; |
|
147 |
||
148 |
val is_ascii_blank = |
|
43845
d89353d17f54
added File.fold_pages for streaming of large files;
wenzelm
parents:
43777
diff
changeset
|
149 |
fn " " => true | "\t" => true | "\n" => true | "\^K" => true | "\f" => true | "\^M" => true |
14678 | 150 |
| _ => false; |
151 |
||
34095
c2f176a38448
robust representation of low ASCII control characters within XML/YXML text;
wenzelm
parents:
33955
diff
changeset
|
152 |
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
|
153 |
|
50236 | 154 |
fun is_ascii_letdig s = is_ascii_letter s orelse is_ascii_digit s orelse is_ascii_quasi s; |
155 |
||
20200 | 156 |
fun is_ascii_lower s = is_char s andalso (ord "a" <= ord s andalso ord s <= ord "z"); |
157 |
fun is_ascii_upper s = is_char s andalso (ord "A" <= ord s andalso ord s <= ord "Z"); |
|
158 |
||
159 |
fun to_ascii_lower s = if is_ascii_upper s then chr (ord s + ord "a" - ord "A") else s; |
|
160 |
fun to_ascii_upper s = if is_ascii_lower s then chr (ord s + ord "A" - ord "a") else s; |
|
161 |
||
50238
98d35a7368bd
more uniform Symbol.is_ascii_identifier in ML/Scala;
wenzelm
parents:
50237
diff
changeset
|
162 |
fun is_ascii_identifier s = |
98d35a7368bd
more uniform Symbol.is_ascii_identifier in ML/Scala;
wenzelm
parents:
50237
diff
changeset
|
163 |
size s > 0 andalso is_ascii_letter (String.substring (s, 0, 1)) andalso |
98d35a7368bd
more uniform Symbol.is_ascii_identifier in ML/Scala;
wenzelm
parents:
50237
diff
changeset
|
164 |
forall_string is_ascii_letdig s; |
98d35a7368bd
more uniform Symbol.is_ascii_identifier in ML/Scala;
wenzelm
parents:
50237
diff
changeset
|
165 |
|
50236 | 166 |
val scan_ascii_id = Scan.one is_ascii_letter ^^ (Scan.many is_ascii_letdig >> implode); |
167 |
||
14678 | 168 |
|
14956
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
169 |
(* encode_raw *) |
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
170 |
|
20205
7b2958d3d575
raw symbols: disallow dot to avoid confusion in NameSpace.unpack;
wenzelm
parents:
20200
diff
changeset
|
171 |
fun raw_chr c = |
48773
0e1bab274672
more liberal scanning of potentially malformed symbols;
wenzelm
parents:
48704
diff
changeset
|
172 |
is_char c andalso |
0e1bab274672
more liberal scanning of potentially malformed symbols;
wenzelm
parents:
48704
diff
changeset
|
173 |
(ord space <= ord c andalso ord c <= ord "~" andalso c <> "." andalso c <> ">" |
0e1bab274672
more liberal scanning of potentially malformed symbols;
wenzelm
parents:
48704
diff
changeset
|
174 |
orelse ord c >= 128); |
14956
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
175 |
|
29324 | 176 |
fun encode_raw "" = "" |
177 |
| encode_raw str = |
|
178 |
let |
|
179 |
val raw0 = enclose "\\<^raw:" ">"; |
|
180 |
val raw1 = raw0 o implode; |
|
181 |
val raw2 = enclose "\\<^raw" ">" o string_of_int o ord; |
|
50162 | 182 |
|
33955 | 183 |
fun encode cs = enc (take_prefix raw_chr cs) |
29324 | 184 |
and enc ([], []) = [] |
185 |
| enc (cs, []) = [raw1 cs] |
|
186 |
| enc ([], d :: ds) = raw2 d :: encode ds |
|
187 |
| enc (cs, d :: ds) = raw1 cs :: raw2 d :: encode ds; |
|
188 |
in |
|
40627
becf5d5187cc
renamed raw "explode" function to "raw_explode" to emphasize its meaning;
wenzelm
parents:
40523
diff
changeset
|
189 |
if exists_string (not o raw_chr) str then implode (encode (raw_explode str)) |
29324 | 190 |
else raw0 str |
191 |
end; |
|
14956
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 |
|
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
194 |
(* diagnostics *) |
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
195 |
|
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
196 |
fun beginning n cs = |
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
197 |
let |
33955 | 198 |
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
|
199 |
val all_cs = drop_blanks cs; |
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
200 |
val dots = if length all_cs > n then " ..." else ""; |
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
201 |
in |
33955 | 202 |
(drop_blanks (take n all_cs) |
14956
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
203 |
|> 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
|
204 |
|> implode) ^ dots |
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
205 |
end; |
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
206 |
|
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
207 |
|
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
208 |
(* decode_raw *) |
14834 | 209 |
|
210 |
fun is_raw s = |
|
17063 | 211 |
String.isPrefix "\\<^raw" s andalso String.isSuffix ">" s; |
14834 | 212 |
|
213 |
fun decode_raw s = |
|
23676
ea9b7e9c2301
scan: changed treatment of malformed symbols, passed to next stage;
wenzelm
parents:
23618
diff
changeset
|
214 |
if not (is_raw s) then error (malformed_msg s) |
14834 | 215 |
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
|
216 |
else chr (#1 (Library.read_int (raw_explode (String.substring (s, 6, size s - 7))))); |
14834 | 217 |
|
218 |
||
14873 | 219 |
(* symbol variants *) |
220 |
||
37533
d775bd70f571
explicit treatment of UTF8 character sequences as Isabelle symbols;
wenzelm
parents:
34095
diff
changeset
|
221 |
datatype sym = |
40523
1050315f6ee2
simplified/robustified treatment of malformed symbols, which are now fully internalized (total Symbol.explode etc.);
wenzelm
parents:
40509
diff
changeset
|
222 |
Char of string | UTF8 of string | Sym of string | Ctrl of string | Raw of string | |
43485 | 223 |
Malformed of string | EOF; |
14873 | 224 |
|
225 |
fun decode s = |
|
43485 | 226 |
if s = "" then EOF |
227 |
else if is_char s then Char s |
|
37533
d775bd70f571
explicit treatment of UTF8 character sequences as Isabelle symbols;
wenzelm
parents:
34095
diff
changeset
|
228 |
else if is_utf8 s then UTF8 s |
14873 | 229 |
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
|
230 |
else if is_malformed s then Malformed s |
14873 | 231 |
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
|
232 |
else Sym (String.substring (s, 2, size s - 3)); |
14873 | 233 |
|
234 |
||
14678 | 235 |
(* standard symbol kinds *) |
236 |
||
14171
0cab06e3bbd0
Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents:
13730
diff
changeset
|
237 |
local |
50235 | 238 |
val letter_symbols = |
239 |
Symtab.make_set [ |
|
240 |
"\\<A>", |
|
241 |
"\\<B>", |
|
242 |
"\\<C>", |
|
243 |
"\\<D>", |
|
244 |
"\\<E>", |
|
245 |
"\\<F>", |
|
246 |
"\\<G>", |
|
247 |
"\\<H>", |
|
248 |
"\\<I>", |
|
249 |
"\\<J>", |
|
250 |
"\\<K>", |
|
251 |
"\\<L>", |
|
252 |
"\\<M>", |
|
253 |
"\\<N>", |
|
254 |
"\\<O>", |
|
255 |
"\\<P>", |
|
256 |
"\\<Q>", |
|
257 |
"\\<R>", |
|
258 |
"\\<S>", |
|
259 |
"\\<T>", |
|
260 |
"\\<U>", |
|
261 |
"\\<V>", |
|
262 |
"\\<W>", |
|
263 |
"\\<X>", |
|
264 |
"\\<Y>", |
|
265 |
"\\<Z>", |
|
266 |
"\\<a>", |
|
267 |
"\\<b>", |
|
268 |
"\\<c>", |
|
269 |
"\\<d>", |
|
270 |
"\\<e>", |
|
271 |
"\\<f>", |
|
272 |
"\\<g>", |
|
273 |
"\\<h>", |
|
274 |
"\\<i>", |
|
275 |
"\\<j>", |
|
276 |
"\\<k>", |
|
277 |
"\\<l>", |
|
278 |
"\\<m>", |
|
279 |
"\\<n>", |
|
280 |
"\\<o>", |
|
281 |
"\\<p>", |
|
282 |
"\\<q>", |
|
283 |
"\\<r>", |
|
284 |
"\\<s>", |
|
285 |
"\\<t>", |
|
286 |
"\\<u>", |
|
287 |
"\\<v>", |
|
288 |
"\\<w>", |
|
289 |
"\\<x>", |
|
290 |
"\\<y>", |
|
291 |
"\\<z>", |
|
292 |
"\\<AA>", |
|
293 |
"\\<BB>", |
|
294 |
"\\<CC>", |
|
295 |
"\\<DD>", |
|
296 |
"\\<EE>", |
|
297 |
"\\<FF>", |
|
298 |
"\\<GG>", |
|
299 |
"\\<HH>", |
|
300 |
"\\<II>", |
|
301 |
"\\<JJ>", |
|
302 |
"\\<KK>", |
|
303 |
"\\<LL>", |
|
304 |
"\\<MM>", |
|
305 |
"\\<NN>", |
|
306 |
"\\<OO>", |
|
307 |
"\\<PP>", |
|
308 |
"\\<QQ>", |
|
309 |
"\\<RR>", |
|
310 |
"\\<SS>", |
|
311 |
"\\<TT>", |
|
312 |
"\\<UU>", |
|
313 |
"\\<VV>", |
|
314 |
"\\<WW>", |
|
315 |
"\\<XX>", |
|
316 |
"\\<YY>", |
|
317 |
"\\<ZZ>", |
|
318 |
"\\<aa>", |
|
319 |
"\\<bb>", |
|
320 |
"\\<cc>", |
|
321 |
"\\<dd>", |
|
322 |
"\\<ee>", |
|
323 |
"\\<ff>", |
|
324 |
"\\<gg>", |
|
325 |
"\\<hh>", |
|
326 |
"\\<ii>", |
|
327 |
"\\<jj>", |
|
328 |
"\\<kk>", |
|
329 |
"\\<ll>", |
|
330 |
"\\<mm>", |
|
331 |
"\\<nn>", |
|
332 |
"\\<oo>", |
|
333 |
"\\<pp>", |
|
334 |
"\\<qq>", |
|
335 |
"\\<rr>", |
|
336 |
"\\<ss>", |
|
337 |
"\\<tt>", |
|
338 |
"\\<uu>", |
|
339 |
"\\<vv>", |
|
340 |
"\\<ww>", |
|
341 |
"\\<xx>", |
|
342 |
"\\<yy>", |
|
343 |
"\\<zz>", |
|
344 |
"\\<alpha>", |
|
345 |
"\\<beta>", |
|
346 |
"\\<gamma>", |
|
347 |
"\\<delta>", |
|
348 |
"\\<epsilon>", |
|
349 |
"\\<zeta>", |
|
350 |
"\\<eta>", |
|
351 |
"\\<theta>", |
|
352 |
"\\<iota>", |
|
353 |
"\\<kappa>", |
|
354 |
(*"\\<lambda>", sic!*) |
|
355 |
"\\<mu>", |
|
356 |
"\\<nu>", |
|
357 |
"\\<xi>", |
|
358 |
"\\<pi>", |
|
359 |
"\\<rho>", |
|
360 |
"\\<sigma>", |
|
361 |
"\\<tau>", |
|
362 |
"\\<upsilon>", |
|
363 |
"\\<phi>", |
|
364 |
"\\<chi>", |
|
365 |
"\\<psi>", |
|
366 |
"\\<omega>", |
|
367 |
"\\<Gamma>", |
|
368 |
"\\<Delta>", |
|
369 |
"\\<Theta>", |
|
370 |
"\\<Lambda>", |
|
371 |
"\\<Xi>", |
|
372 |
"\\<Pi>", |
|
373 |
"\\<Sigma>", |
|
374 |
"\\<Upsilon>", |
|
375 |
"\\<Phi>", |
|
376 |
"\\<Psi>", |
|
52616
3ac2878764f9
more robust identifier syntax: sub/superscript counts as modifier of LETDIG part instead of LETTER, both isub/isup and sub/sup are allowed;
wenzelm
parents:
50242
diff
changeset
|
377 |
"\\<Omega>" |
50235 | 378 |
]; |
14171
0cab06e3bbd0
Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents:
13730
diff
changeset
|
379 |
in |
50242
56b9c792a98b
support for sub-structured identifier syntax (inactive);
wenzelm
parents:
50239
diff
changeset
|
380 |
|
56b9c792a98b
support for sub-structured identifier syntax (inactive);
wenzelm
parents:
50239
diff
changeset
|
381 |
val is_letter_symbol = Symtab.defined letter_symbols; |
56b9c792a98b
support for sub-structured identifier syntax (inactive);
wenzelm
parents:
50239
diff
changeset
|
382 |
|
14678 | 383 |
end; |
14173 | 384 |
|
50242
56b9c792a98b
support for sub-structured identifier syntax (inactive);
wenzelm
parents:
50239
diff
changeset
|
385 |
datatype kind = Letter | Digit | Quasi | Blank | Other; |
56b9c792a98b
support for sub-structured identifier syntax (inactive);
wenzelm
parents:
50239
diff
changeset
|
386 |
|
56b9c792a98b
support for sub-structured identifier syntax (inactive);
wenzelm
parents:
50239
diff
changeset
|
387 |
fun kind s = |
56b9c792a98b
support for sub-structured identifier syntax (inactive);
wenzelm
parents:
50239
diff
changeset
|
388 |
if is_ascii_letter s then Letter |
56b9c792a98b
support for sub-structured identifier syntax (inactive);
wenzelm
parents:
50239
diff
changeset
|
389 |
else if is_ascii_digit s then Digit |
56b9c792a98b
support for sub-structured identifier syntax (inactive);
wenzelm
parents:
50239
diff
changeset
|
390 |
else if is_ascii_quasi s then Quasi |
56b9c792a98b
support for sub-structured identifier syntax (inactive);
wenzelm
parents:
50239
diff
changeset
|
391 |
else if is_ascii_blank s then Blank |
56b9c792a98b
support for sub-structured identifier syntax (inactive);
wenzelm
parents:
50239
diff
changeset
|
392 |
else if is_char s then Other |
56b9c792a98b
support for sub-structured identifier syntax (inactive);
wenzelm
parents:
50239
diff
changeset
|
393 |
else if is_letter_symbol s then Letter |
56b9c792a98b
support for sub-structured identifier syntax (inactive);
wenzelm
parents:
50239
diff
changeset
|
394 |
else Other; |
56b9c792a98b
support for sub-structured identifier syntax (inactive);
wenzelm
parents:
50239
diff
changeset
|
395 |
|
14678 | 396 |
fun is_letter s = kind s = Letter; |
397 |
fun is_digit s = kind s = Digit; |
|
398 |
fun is_quasi s = kind s = Quasi; |
|
399 |
fun is_blank s = kind s = Blank; |
|
6272 | 400 |
|
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
|
401 |
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
|
402 |
|
14678 | 403 |
fun is_quasi_letter s = let val k = kind s in k = Letter orelse k = Quasi end; |
404 |
fun is_letdig s = let val k = kind s in k = Letter orelse k = Digit orelse k = Quasi end; |
|
11010 | 405 |
|
6272 | 406 |
|
407 |
||
14678 | 408 |
(** symbol input **) |
409 |
||
23676
ea9b7e9c2301
scan: changed treatment of malformed symbols, passed to next stage;
wenzelm
parents:
23618
diff
changeset
|
410 |
(* source *) |
ea9b7e9c2301
scan: changed treatment of malformed symbols, passed to next stage;
wenzelm
parents:
23618
diff
changeset
|
411 |
|
14678 | 412 |
local |
14561
c53396af770e
* raw control symbols are of the form \<^raw:...> now.
schirmer
parents:
14559
diff
changeset
|
413 |
|
37533
d775bd70f571
explicit treatment of UTF8 character sequences as Isabelle symbols;
wenzelm
parents:
34095
diff
changeset
|
414 |
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
|
415 |
|
d775bd70f571
explicit treatment of UTF8 character sequences as Isabelle symbols;
wenzelm
parents:
34095
diff
changeset
|
416 |
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
|
417 |
|
37728
5d2b3e827371
implode pseudo utf8, i.e. decode byte-stuffed low ASCII characters;
wenzelm
parents:
37534
diff
changeset
|
418 |
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
|
419 |
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
|
420 |
| implode_pseudo_utf8 cs = implode cs; |
5d2b3e827371
implode pseudo utf8, i.e. decode byte-stuffed low ASCII characters;
wenzelm
parents:
37534
diff
changeset
|
421 |
|
14678 | 422 |
val scan_encoded_newline = |
17756 | 423 |
$$ "\^M" -- $$ "\n" >> K "\n" || |
54734
b91afc3aa3e6
clarified Proof General legacy: special treatment of \<^newline> only in TTY mode;
wenzelm
parents:
54732
diff
changeset
|
424 |
$$ "\^M" >> K "\n"; |
14956
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
425 |
|
70ec4b8aef8d
prevent looping of error messages involving malformed symbols;
wenzelm
parents:
14908
diff
changeset
|
426 |
val scan_raw = |
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21495
diff
changeset
|
427 |
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
|
428 |
Scan.this_string "raw" ^^ (Scan.many1 is_ascii_digit >> implode); |
14678 | 429 |
|
40523
1050315f6ee2
simplified/robustified treatment of malformed symbols, which are now fully internalized (total Symbol.explode etc.);
wenzelm
parents:
40509
diff
changeset
|
430 |
val scan_total = |
23676
ea9b7e9c2301
scan: changed treatment of malformed symbols, passed to next stage;
wenzelm
parents:
23618
diff
changeset
|
431 |
Scan.one is_plain || |
37728
5d2b3e827371
implode pseudo utf8, i.e. decode byte-stuffed low ASCII characters;
wenzelm
parents:
37534
diff
changeset
|
432 |
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
|
433 |
scan_encoded_newline || |
48773
0e1bab274672
more liberal scanning of potentially malformed symbols;
wenzelm
parents:
48704
diff
changeset
|
434 |
($$ "\\" ^^ $$ "<" ^^ |
50236 | 435 |
(($$ "^" ^^ Scan.optional (scan_raw || scan_ascii_id) "" || Scan.optional scan_ascii_id "") ^^ |
48774 | 436 |
Scan.optional ($$ ">") "")) || |
23676
ea9b7e9c2301
scan: changed treatment of malformed symbols, passed to next stage;
wenzelm
parents:
23618
diff
changeset
|
437 |
Scan.one not_eof; |
ea9b7e9c2301
scan: changed treatment of malformed symbols, passed to next stage;
wenzelm
parents:
23618
diff
changeset
|
438 |
|
14678 | 439 |
in |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
440 |
|
58864 | 441 |
fun source src = Source.source stopper (Scan.bulk scan_total) src; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
442 |
|
14678 | 443 |
end; |
444 |
||
14562
980da32f4617
proper handling of lines terminated by CRLF or CR;
wenzelm
parents:
14561
diff
changeset
|
445 |
|
23676
ea9b7e9c2301
scan: changed treatment of malformed symbols, passed to next stage;
wenzelm
parents:
23618
diff
changeset
|
446 |
(* explode *) |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
447 |
|
23676
ea9b7e9c2301
scan: changed treatment of malformed symbols, passed to next stage;
wenzelm
parents:
23618
diff
changeset
|
448 |
local |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
449 |
|
14562
980da32f4617
proper handling of lines terminated by CRLF or CR;
wenzelm
parents:
14561
diff
changeset
|
450 |
fun no_explode [] = true |
980da32f4617
proper handling of lines terminated by CRLF or CR;
wenzelm
parents:
14561
diff
changeset
|
451 |
| no_explode ("\\" :: "<" :: _) = false |
17756 | 452 |
| no_explode ("\^M" :: _) = false |
37533
d775bd70f571
explicit treatment of UTF8 character sequences as Isabelle symbols;
wenzelm
parents:
34095
diff
changeset
|
453 |
| 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
|
454 |
|
23676
ea9b7e9c2301
scan: changed treatment of malformed symbols, passed to next stage;
wenzelm
parents:
23618
diff
changeset
|
455 |
in |
ea9b7e9c2301
scan: changed treatment of malformed symbols, passed to next stage;
wenzelm
parents:
23618
diff
changeset
|
456 |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
457 |
fun sym_explode str = |
40627
becf5d5187cc
renamed raw "explode" function to "raw_explode" to emphasize its meaning;
wenzelm
parents:
40523
diff
changeset
|
458 |
let val chs = raw_explode str in |
14562
980da32f4617
proper handling of lines terminated by CRLF or CR;
wenzelm
parents:
14561
diff
changeset
|
459 |
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
|
460 |
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
|
461 |
end; |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
462 |
|
23676
ea9b7e9c2301
scan: changed treatment of malformed symbols, passed to next stage;
wenzelm
parents:
23618
diff
changeset
|
463 |
end; |
14994 | 464 |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
465 |
|
50237 | 466 |
(* escape *) |
467 |
||
468 |
val esc = fn s => |
|
469 |
if is_char s then s |
|
470 |
else if is_utf8 s then translate_string (fn c => "\\" ^ string_of_int (ord c)) s |
|
471 |
else "\\" ^ s; |
|
472 |
||
473 |
val escape = implode o map esc o sym_explode; |
|
474 |
||
475 |
||
476 |
||
477 |
(** scanning through symbols **) |
|
478 |
||
479 |
(* scanner *) |
|
480 |
||
481 |
fun scanner msg scan syms = |
|
482 |
let |
|
483 |
fun message (ss, NONE) = (fn () => msg ^ ": " ^ quote (beginning 10 ss)) |
|
484 |
| message (ss, SOME msg') = (fn () => msg ^ ", " ^ msg' () ^ ": " ^ quote (beginning 10 ss)); |
|
485 |
val finite_scan = Scan.error (Scan.finite stopper (!! message scan)); |
|
486 |
in |
|
487 |
(case finite_scan syms of |
|
488 |
(result, []) => result |
|
489 |
| (_, rest) => error (message (rest, NONE) ())) |
|
490 |
end; |
|
491 |
||
492 |
||
50162 | 493 |
(* space-separated words *) |
494 |
||
495 |
val scan_word = |
|
496 |
Scan.many1 is_ascii_blank >> K NONE || |
|
497 |
Scan.many1 (fn s => not (is_ascii_blank s) andalso not_eof s) >> (SOME o implode); |
|
498 |
||
499 |
val split_words = scanner "Bad text" (Scan.repeat scan_word >> map_filter I); |
|
500 |
||
501 |
val explode_words = split_words o sym_explode; |
|
502 |
||
503 |
||
14678 | 504 |
(* blanks *) |
505 |
||
506 |
fun strip_blanks s = |
|
507 |
sym_explode s |
|
33955 | 508 |
|> take_prefix is_blank |> #2 |
509 |
|> take_suffix is_blank |> #1 |
|
14678 | 510 |
|> implode; |
511 |
||
512 |
||
513 |
(* bump string -- treat as base 26 or base 1 numbers *) |
|
514 |
||
50242
56b9c792a98b
support for sub-structured identifier syntax (inactive);
wenzelm
parents:
50239
diff
changeset
|
515 |
fun symbolic_end (_ :: "\\<^sub>" :: _) = true |
53198
06b096cf89ca
ignore trailing primes, e.g. rename \<alpha>' to \<alpha>'' instead of \<alpha>'a;
wenzelm
parents:
53016
diff
changeset
|
516 |
| symbolic_end ("'" :: ss) = symbolic_end ss |
55033 | 517 |
| symbolic_end (s :: _) = raw_symbolic s |
14908 | 518 |
| symbolic_end [] = false; |
14678 | 519 |
|
520 |
fun bump_init str = |
|
14908 | 521 |
if symbolic_end (rev (sym_explode str)) then str ^ "'" |
14678 | 522 |
else str ^ "a"; |
12904 | 523 |
|
524 |
fun bump_string str = |
|
525 |
let |
|
526 |
fun bump [] = ["a"] |
|
527 |
| bump ("z" :: ss) = "a" :: bump ss |
|
528 |
| bump (s :: ss) = |
|
14678 | 529 |
if is_char s andalso ord "a" <= ord s andalso ord s < ord "z" |
12904 | 530 |
then chr (ord s + 1) :: ss |
531 |
else "a" :: s :: ss; |
|
14678 | 532 |
|
33955 | 533 |
val (ss, qs) = apfst rev (take_suffix is_quasi (sym_explode str)); |
14908 | 534 |
val ss' = if symbolic_end ss then "'" :: ss else bump ss; |
14678 | 535 |
in implode (rev ss' @ qs) end; |
536 |
||
12904 | 537 |
|
6272 | 538 |
|
29324 | 539 |
(** symbol output **) |
14977
77d88064991a
added escape, export encode_raw, default mode now trivial, tuned;
wenzelm
parents:
14961
diff
changeset
|
540 |
|
29324 | 541 |
(* length *) |
6272 | 542 |
|
14678 | 543 |
fun sym_len s = |
24593
1547ea587f5a
added some int constraints (ML_Parse.fix_ints not active here);
wenzelm
parents:
24580
diff
changeset
|
544 |
if not (is_printable s) then (0: int) |
14678 | 545 |
else if String.isPrefix "\\<long" s then 2 |
546 |
else if String.isPrefix "\\<Long" s then 2 |
|
547 |
else 1; |
|
548 |
||
19473 | 549 |
fun sym_length ss = fold (fn s => fn n => sym_len s + n) ss 0; |
14678 | 550 |
|
29324 | 551 |
|
552 |
(* print mode *) |
|
553 |
||
554 |
val xsymbolsN = "xsymbols"; |
|
555 |
||
556 |
fun output s = (s, sym_length (sym_explode s)); |
|
557 |
||
558 |
||
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
559 |
(*final declarations of this structure!*) |
29324 | 560 |
val explode = sym_explode; |
6272 | 561 |
val length = sym_length; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
562 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
563 |
end; |