defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
1 (* Title: Pure/General/symbol.ML
2 Author: Markus Wenzel, TU Muenchen
4 Generalized characters with infinitely many named symbols.
13 val spaces: int -> string
14 val is_char: symbol -> bool
15 val is_utf8: symbol -> bool
16 val is_symbolic: symbol -> bool
17 val is_printable: symbol -> bool
19 val is_eof: symbol -> bool
20 val not_eof: symbol -> bool
21 val stopper: symbol Scan.stopper
23 val is_sync: symbol -> bool
24 val is_regular: symbol -> bool
25 val is_malformed: symbol -> bool
26 val malformed_msg: symbol -> string
27 val is_ascii: symbol -> bool
28 val is_ascii_letter: symbol -> bool
29 val is_ascii_digit: symbol -> bool
30 val is_ascii_hex: symbol -> bool
31 val is_ascii_quasi: symbol -> bool
32 val is_ascii_blank: symbol -> bool
33 val is_ascii_control: symbol -> bool
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
38 val is_raw: symbol -> bool
39 val decode_raw: symbol -> string
40 val encode_raw: string -> string
42 Char of string | UTF8 of string | Sym of string | Ctrl of string | Raw of string |
43 Malformed of string | EOF
44 val decode: symbol -> sym
45 datatype kind = Letter | Digit | Quasi | Blank | Other
46 val kind: symbol -> kind
47 val is_letter: symbol -> bool
48 val is_digit: symbol -> bool
49 val is_quasi: symbol -> bool
50 val is_blank: symbol -> bool
51 val is_quasi_letter: symbol -> bool
52 val is_letdig: symbol -> bool
53 val is_ident: symbol list -> bool
54 val beginning: int -> symbol list -> string
55 val scanner: string -> (string list -> 'a * string list) -> symbol list -> 'a
56 val scan_id: string list -> string * string list
57 val source: (string, 'a) Source.source -> (symbol, (string, 'a) Source.source) Source.source
58 val explode: string -> symbol list
59 val esc: symbol -> string
60 val escape: string -> string
61 val strip_blanks: string -> string
62 val bump_init: string -> string
63 val bump_string: string -> string
64 val length: symbol list -> int
66 val output: string -> Output.output * int
69 structure Symbol: SYMBOL =
74 (*Symbols, which are considered the smallest entities of any Isabelle
75 string, may be of the following form:
78 (2) regular symbols: \<ident>
79 (3) control symbols: \<^ident>
80 (4) raw control symbols: \<^raw:...>, where "..." may be any printable
81 character (excluding ".", ">"), or \<^raw000>
83 Output is subject to the print_mode variable (default: verbatim),
84 actual interpretation in display is up to front-end tools.
95 val small_spaces = Vector.tabulate (65, fn i => Library.replicate_string i space);
98 if k < 64 then Vector.sub (small_spaces, k)
99 else Library.replicate_string (k div 64) (Vector.sub (small_spaces, 64)) ^
100 Vector.sub (small_spaces, k mod 64);
103 fun is_char s = size s = 1;
105 fun is_utf8 s = size s > 0 andalso forall_string (fn c => ord c >= 128) s;
108 String.isPrefix "\\<" s andalso String.isSuffix ">" s andalso not (String.isPrefix "\\<^" s);
111 if is_char s then ord space <= ord s andalso ord s <= ord "~"
112 else is_utf8 s orelse is_symbolic s;
115 (* input source control *)
118 fun is_eof s = s = eof;
119 fun not_eof s = s <> eof;
120 val stopper = Scan.stopper (K eof) is_eof;
122 val sync = "\\<^sync>";
123 fun is_sync s = s = sync;
125 fun is_regular s = not_eof s andalso s <> sync;
127 fun is_malformed s = String.isPrefix "\\<" s andalso not (String.isSuffix ">" s);
128 fun malformed_msg s = "Malformed symbolic character: " ^ quote s;
133 fun is_ascii s = is_char s andalso ord s < 128;
135 fun is_ascii_letter s =
137 (ord "A" <= ord s andalso ord s <= ord "Z" orelse
138 ord "a" <= ord s andalso ord s <= ord "z");
140 fun is_ascii_digit s =
141 is_char s andalso ord "0" <= ord s andalso ord s <= ord "9";
145 (ord "0" <= ord s andalso ord s <= ord "9" orelse
146 ord "A" <= ord s andalso ord s <= ord "F" orelse
147 ord "a" <= ord s andalso ord s <= ord "f");
149 fun is_ascii_quasi "_" = true
150 | is_ascii_quasi "'" = true
151 | is_ascii_quasi _ = false;
154 fn " " => true | "\t" => true | "\n" => true | "\^K" => true | "\f" => true | "\^M" => true
157 fun is_ascii_control s = is_char s andalso ord s < 32 andalso not (is_ascii_blank s);
159 fun is_ascii_lower s = is_char s andalso (ord "a" <= ord s andalso ord s <= ord "z");
160 fun is_ascii_upper s = is_char s andalso (ord "A" <= ord s andalso ord s <= ord "Z");
162 fun to_ascii_lower s = if is_ascii_upper s then chr (ord s + ord "a" - ord "A") else s;
163 fun to_ascii_upper s = if is_ascii_lower s then chr (ord s + ord "A" - ord "a") else s;
169 ord space <= ord c andalso ord c <= ord "~" andalso c <> "." andalso c <> ">"
172 fun encode_raw "" = ""
175 val raw0 = enclose "\\<^raw:" ">";
176 val raw1 = raw0 o implode;
177 val raw2 = enclose "\\<^raw" ">" o string_of_int o ord;
179 fun encode cs = enc (take_prefix raw_chr cs)
180 and enc ([], []) = []
181 | enc (cs, []) = [raw1 cs]
182 | enc ([], d :: ds) = raw2 d :: encode ds
183 | enc (cs, d :: ds) = raw1 cs :: raw2 d :: encode ds;
185 if exists_string (not o raw_chr) str then implode (encode (raw_explode str))
194 val drop_blanks = #1 o take_suffix is_ascii_blank;
195 val all_cs = drop_blanks cs;
196 val dots = if length all_cs > n then " ..." else "";
198 (drop_blanks (take n all_cs)
199 |> map (fn c => if is_ascii_blank c then space else c)
207 String.isPrefix "\\<^raw" s andalso String.isSuffix ">" s;
210 if not (is_raw s) then error (malformed_msg s)
211 else if String.isPrefix "\\<^raw:" s then String.substring (s, 7, size s - 8)
212 else chr (#1 (Library.read_int (raw_explode (String.substring (s, 6, size s - 7)))));
215 (* symbol variants *)
218 Char of string | UTF8 of string | Sym of string | Ctrl of string | Raw of string |
219 Malformed of string | EOF;
223 else if is_char s then Char s
224 else if is_utf8 s then UTF8 s
225 else if is_raw s then Raw (decode_raw s)
226 else if is_malformed s then Malformed s
227 else if String.isPrefix "\\<^" s then Ctrl (String.substring (s, 3, size s - 4))
228 else Sym (String.substring (s, 2, size s - 3));
231 (* standard symbol kinds *)
233 datatype kind = Letter | Digit | Quasi | Blank | Other;
236 val symbol_kinds = Symtab.make
341 ("\\<alpha>", Letter),
342 ("\\<beta>", Letter),
343 ("\\<gamma>", Letter),
344 ("\\<delta>", Letter),
345 ("\\<epsilon>", Letter),
346 ("\\<zeta>", Letter),
348 ("\\<theta>", Letter),
349 ("\\<iota>", Letter),
350 ("\\<kappa>", Letter),
351 ("\\<lambda>", Other), (*sic!*)
357 ("\\<sigma>", Letter),
359 ("\\<upsilon>", Letter),
363 ("\\<omega>", Letter),
364 ("\\<Gamma>", Letter),
365 ("\\<Delta>", Letter),
366 ("\\<Theta>", Letter),
367 ("\\<Lambda>", Letter),
370 ("\\<Sigma>", Letter),
371 ("\\<Upsilon>", Letter),
374 ("\\<Omega>", Letter),
375 ("\\<^isub>", Letter),
376 ("\\<^isup>", Letter),
377 ("\\<spacespace>", Blank)];
380 if is_ascii_letter s then Letter
381 else if is_ascii_digit s then Digit
382 else if is_ascii_quasi s then Quasi
383 else if is_ascii_blank s then Blank
384 else if is_char s then Other
385 else the_default Other (Symtab.lookup symbol_kinds s);
388 fun is_letter s = kind s = Letter;
389 fun is_digit s = kind s = Digit;
390 fun is_quasi s = kind s = Quasi;
391 fun is_blank s = kind s = Blank;
393 fun is_quasi_letter s = let val k = kind s in k = Letter orelse k = Quasi end;
394 fun is_letdig s = let val k = kind s in k = Letter orelse k = Digit orelse k = Quasi end;
396 fun is_ident [] = false
397 | is_ident (c :: cs) = is_letter c andalso forall is_letdig cs;
403 (* scanning through symbols *)
405 fun scanner msg scan chs =
407 fun message (cs, NONE) = (fn () => msg ^ ": " ^ quote (beginning 10 cs))
408 | message (cs, SOME msg') = (fn () => msg ^ ", " ^ msg' () ^ ": " ^ quote (beginning 10 cs));
409 val fin_scan = Scan.error (Scan.finite stopper (!! message scan));
411 (case fin_scan chs of
412 (result, []) => result
413 | (_, rest) => error (message (rest, NONE) ()))
416 val scan_id = Scan.one is_letter ^^ (Scan.many is_letdig >> implode);
423 fun is_plain s = is_ascii s andalso s <> "\^M" andalso s <> "\\";
425 fun is_utf8_trailer s = is_char s andalso 128 <= ord s andalso ord s < 192;
427 fun implode_pseudo_utf8 (cs as ["\192", c]) =
428 if ord c < 160 then chr (ord c - 128) else implode cs
429 | implode_pseudo_utf8 cs = implode cs;
431 val scan_encoded_newline =
432 $$ "\^M" -- $$ "\n" >> K "\n" ||
433 $$ "\^M" >> K "\n" ||
434 Scan.this_string "\\<^newline>" >> K "\n";
437 Scan.this_string "raw:" ^^ (Scan.many raw_chr >> implode) ||
438 Scan.this_string "raw" ^^ (Scan.many1 is_ascii_digit >> implode);
442 Scan.one is_utf8 ::: Scan.many is_utf8_trailer >> implode_pseudo_utf8 ||
443 scan_encoded_newline ||
444 ($$ "\\" ^^ $$ "<" ^^ (($$ "^" ^^ (scan_raw || scan_id) || scan_id) ^^ $$ ">")) ||
445 Scan.this_string "\\<^" ||
446 Scan.this_string "\\<" ||
451 fun source src = Source.source stopper (Scan.bulk scan_total) NONE src;
460 fun no_explode [] = true
461 | no_explode ("\\" :: "<" :: _) = false
462 | no_explode ("\^M" :: _) = false
463 | no_explode (c :: cs) = is_ascii c andalso no_explode cs;
467 fun sym_explode str =
468 let val chs = raw_explode str in
469 if no_explode chs then chs
470 else Source.exhaust (source (Source.of_list chs))
480 else if is_utf8 s then translate_string (fn c => "\\" ^ string_of_int (ord c)) s
483 val escape = implode o map esc o sym_explode;
490 |> take_prefix is_blank |> #2
491 |> take_suffix is_blank |> #1
495 (* bump string -- treat as base 26 or base 1 numbers *)
497 fun symbolic_end (_ :: "\\<^isub>" :: _) = true
498 | symbolic_end (_ :: "\\<^isup>" :: _) = true
499 | symbolic_end (s :: _) = is_symbolic s
500 | symbolic_end [] = false;
503 if symbolic_end (rev (sym_explode str)) then str ^ "'"
506 fun bump_string str =
509 | bump ("z" :: ss) = "a" :: bump ss
511 if is_char s andalso ord "a" <= ord s andalso ord s < ord "z"
512 then chr (ord s + 1) :: ss
515 val (ss, qs) = apfst rev (take_suffix is_quasi (sym_explode str));
516 val ss' = if symbolic_end ss then "'" :: ss else bump ss;
517 in implode (rev ss' @ qs) end;
521 (** symbol output **)
526 if not (is_printable s) then (0: int)
527 else if String.isPrefix "\\<long" s then 2
528 else if String.isPrefix "\\<Long" s then 2
529 else if s = "\\<spacespace>" then 2
532 fun sym_length ss = fold (fn s => fn n => sym_len s + n) ss 0;
537 val xsymbolsN = "xsymbols";
539 fun output s = (s, sym_length (sym_explode s));
542 (*final declarations of this structure!*)
543 val explode = sym_explode;
544 val length = sym_length;