author | nipkow |
Mon, 13 May 2002 15:27:28 +0200 | |
changeset 13145 | 59bc43b51aa2 |
parent 12904 | c208d71702d1 |
child 13559 | 51c3ac47d127 |
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 |
|
12116 | 6 |
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
|
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 |
6857 | 14 |
val sync: symbol |
15 |
val is_sync: symbol -> bool |
|
16 |
val not_sync: symbol -> bool |
|
10747 | 17 |
val malformed: symbol |
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 |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
20 |
val not_eof: symbol -> bool |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
21 |
val stopper: symbol * (symbol -> bool) |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
22 |
val is_ascii: symbol -> bool |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
23 |
val is_letter: symbol -> bool |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
24 |
val is_digit: symbol -> bool |
12904 | 25 |
val is_quasi: symbol -> bool |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
26 |
val is_quasi_letter: symbol -> bool |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
27 |
val is_letdig: symbol -> bool |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
28 |
val is_blank: symbol -> bool |
8230 | 29 |
val is_symbolic: symbol -> bool |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
30 |
val is_printable: symbol -> bool |
6272 | 31 |
val length: symbol list -> int |
11010 | 32 |
val strip_blanks: string -> string |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
33 |
val beginning: symbol list -> string |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
34 |
val scan: string list -> symbol * string list |
6640 | 35 |
val scanner: string -> (symbol list -> 'a * symbol list) -> symbol list -> 'a |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
36 |
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
|
37 |
(symbol, (string, 'a) Source.source) Source.source |
6272 | 38 |
val explode: string -> symbol list |
12904 | 39 |
val bump_string: string -> string |
10953 | 40 |
val default_indent: string * int -> string |
41 |
val add_mode: string -> (string -> string * real) * (string * int -> string) -> unit |
|
6692 | 42 |
val symbolsN: string |
43 |
val xsymbolsN: string |
|
10923 | 44 |
val plain_output: string -> string |
6272 | 45 |
val output: string -> string |
46 |
val output_width: string -> string * real |
|
10953 | 47 |
val indent: string * int -> string |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
48 |
end; |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
49 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
50 |
structure Symbol: SYMBOL = |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
51 |
struct |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
52 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
53 |
|
6272 | 54 |
(** generalized characters **) |
55 |
||
56 |
(*symbols, which are considered the smallest entities of any Isabelle |
|
57 |
string, may be of the following form: |
|
58 |
(a) ASCII symbols: a |
|
59 |
(b) printable symbols: \<ident> |
|
60 |
(c) control symbols: \<^ident> |
|
61 |
||
12116 | 62 |
output is subject to the print_mode variable (default: verbatim), |
63 |
actual interpretation in display is up to front-end tools; |
|
6272 | 64 |
*) |
65 |
||
66 |
type symbol = string; |
|
67 |
||
68 |
val space = " "; |
|
10953 | 69 |
fun spaces k = Library.replicate_string k space; |
6857 | 70 |
val sync = "\\<^sync>"; |
10747 | 71 |
val malformed = "\\<^malformed>"; |
6272 | 72 |
val eof = ""; |
73 |
||
74 |
||
75 |
(* kinds *) |
|
76 |
||
6857 | 77 |
fun is_sync s = s = sync; |
78 |
fun not_sync s = s <> sync; |
|
79 |
||
6272 | 80 |
fun is_eof s = s = eof; |
81 |
fun not_eof s = s <> eof; |
|
82 |
val stopper = (eof, is_eof); |
|
83 |
||
84 |
fun is_ascii s = size s = 1 andalso ord s < 128; |
|
85 |
||
86 |
fun is_letter s = |
|
87 |
size s = 1 andalso |
|
88 |
(ord "A" <= ord s andalso ord s <= ord "Z" orelse |
|
89 |
ord "a" <= ord s andalso ord s <= ord "z"); |
|
90 |
||
91 |
fun is_digit s = |
|
92 |
size s = 1 andalso ord "0" <= ord s andalso ord s <= ord "9"; |
|
93 |
||
12904 | 94 |
fun is_quasi "_" = true |
95 |
| is_quasi "'" = true |
|
96 |
| is_quasi _ = false; |
|
97 |
||
98 |
fun is_quasi_letter s = is_quasi s orelse is_letter s; |
|
6272 | 99 |
|
100 |
val is_blank = |
|
101 |
fn " " => true | "\t" => true | "\n" => true | "\^L" => true |
|
102 |
| "\160" => true | "\\<spacespace>" => true |
|
103 |
| _ => false; |
|
104 |
||
12904 | 105 |
fun is_letdig s = is_quasi_letter s orelse is_digit s; |
6272 | 106 |
|
8230 | 107 |
fun is_symbolic s = |
108 |
size s > 2 andalso nth_elem_string (2, s) <> "^"; |
|
109 |
||
6272 | 110 |
fun is_printable s = |
111 |
size s = 1 andalso ord space <= ord s andalso ord s <= ord "~" orelse |
|
8230 | 112 |
is_symbolic s; |
113 |
||
6272 | 114 |
|
10738 | 115 |
fun sym_length ss = foldl (fn (n, s) => |
116 |
(if not (is_printable s) then 0 else |
|
117 |
(case Library.try String.substring (s, 2, 4) of |
|
118 |
Some s' => if s' = "long" orelse s' = "Long" then 2 else 1 |
|
119 |
| None => 1)) + n) (0, ss); |
|
6272 | 120 |
|
11010 | 121 |
fun strip_blanks s = |
122 |
implode (#1 (Library.take_suffix is_blank (#2 (Library.take_prefix is_blank (explode s))))); |
|
123 |
||
6272 | 124 |
|
125 |
(* beginning *) |
|
126 |
||
127 |
val smash_blanks = map (fn s => if is_blank s then space else s); |
|
128 |
||
129 |
fun beginning raw_ss = |
|
130 |
let |
|
131 |
val (all_ss, _) = take_suffix is_blank raw_ss; |
|
132 |
val dots = if length all_ss > 10 then " ..." else ""; |
|
133 |
val (ss, _) = take_suffix is_blank (take (10, all_ss)); |
|
134 |
in implode (smash_blanks ss) ^ dots end; |
|
135 |
||
136 |
||
137 |
||
8998 | 138 |
(** scanning through symbols **) |
6640 | 139 |
|
140 |
fun scanner msg scan chs = |
|
141 |
let |
|
142 |
fun err_msg cs = msg ^ ": " ^ beginning cs; |
|
143 |
val fin_scan = Scan.error (Scan.finite stopper (!! (fn (cs, _) => err_msg cs) scan)); |
|
144 |
in |
|
145 |
(case fin_scan chs of |
|
146 |
(result, []) => result |
|
147 |
| (_, rest) => error (err_msg rest)) |
|
148 |
end; |
|
149 |
||
150 |
||
151 |
||
6272 | 152 |
(** symbol input **) |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
153 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
154 |
(* scan *) |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
155 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
156 |
val scan_id = Scan.one is_letter ^^ (Scan.any is_letdig >> implode); |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
157 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
158 |
val scan = |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
159 |
($$ "\\" --| Scan.optional ($$ "\\") "") ^^ $$ "<" ^^ |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
160 |
!! (fn (cs, _) => "Malformed symbolic character specification: \\" ^ "<" ^ beginning cs) |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
161 |
(Scan.optional ($$ "^") "" ^^ scan_id ^^ $$ ">") || |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
162 |
Scan.one not_eof; |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
163 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
164 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
165 |
(* source *) |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
166 |
|
10747 | 167 |
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
|
168 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
169 |
fun source do_recover src = |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
170 |
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
|
171 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
172 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
173 |
(* explode *) |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
174 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
175 |
fun no_syms [] = true |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
176 |
| no_syms ("\\" :: "<" :: _) = false |
12116 | 177 |
| no_syms (_ :: cs) = no_syms cs; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
178 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
179 |
fun sym_explode str = |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
180 |
let val chs = explode str in |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
181 |
if no_syms chs then chs (*tune trivial case*) |
12116 | 182 |
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
|
183 |
end; |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
184 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
185 |
|
12904 | 186 |
(* bump_string -- increment suffix of lowercase letters like a base 26 number *) |
187 |
||
188 |
fun bump_string str = |
|
189 |
let |
|
190 |
fun bump [] = ["a"] |
|
191 |
| bump ("z" :: ss) = "a" :: bump ss |
|
192 |
| bump (s :: ss) = |
|
193 |
if size s = 1 andalso ord "a" <= ord s andalso ord s < ord "z" |
|
194 |
then chr (ord s + 1) :: ss |
|
195 |
else "a" :: s :: ss; |
|
196 |
val (cs, qs) = Library.take_suffix is_quasi (sym_explode str); |
|
197 |
in implode (rev (bump (rev cs)) @ qs) end; |
|
198 |
||
6272 | 199 |
|
200 |
(** symbol output **) |
|
201 |
||
10953 | 202 |
(* default *) |
6272 | 203 |
|
204 |
fun string_size s = (s, real (size s)); |
|
205 |
||
206 |
fun default_output s = |
|
6320 | 207 |
if not (exists_string (equal "\\") s) then string_size s |
10738 | 208 |
else string_size (implode (map (fn "\\" => "\\\\" | c => c) (explode s))); (*sic!*) |
6272 | 209 |
|
10953 | 210 |
fun default_indent (_: string, k) = spaces k; |
6272 | 211 |
|
10953 | 212 |
|
6272 | 213 |
(* maintain modes *) |
214 |
||
6692 | 215 |
val symbolsN = "symbols"; |
216 |
val xsymbolsN = "xsymbols"; |
|
217 |
||
12116 | 218 |
val modes = |
219 |
ref (Symtab.empty: ((string -> string * real) * (string * int -> string)) Symtab.table); |
|
6272 | 220 |
|
221 |
fun lookup_mode name = Symtab.lookup (! modes, name); |
|
222 |
||
10953 | 223 |
fun add_mode name m = |
6272 | 224 |
(if is_none (lookup_mode name) then () |
6320 | 225 |
else warning ("Redeclaration of symbol print mode " ^ quote name); |
10953 | 226 |
modes := Symtab.update ((name, m), ! modes)); |
227 |
||
228 |
fun get_mode () = |
|
229 |
if_none (get_first lookup_mode (! print_mode)) (default_output, default_indent); |
|
6272 | 230 |
|
231 |
||
232 |
(* mode output *) |
|
233 |
||
10953 | 234 |
fun output_width x = #1 (get_mode ()) x; |
6272 | 235 |
val output = #1 o output_width; |
10923 | 236 |
val plain_output = #1 o default_output; |
6272 | 237 |
|
10953 | 238 |
fun indent x = #2 (get_mode ()) x; |
239 |
||
6272 | 240 |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
241 |
(*final declarations of this structure!*) |
6272 | 242 |
val length = sym_length; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
243 |
val explode = sym_explode; |
6272 | 244 |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
245 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
246 |
end; |