author | wenzelm |
Fri, 16 Apr 2004 21:03:40 +0200 | |
changeset 14609 | 663e0e435866 |
parent 14595 | 2df717e26035 |
child 14632 | 805fa01ac233 |
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 |
13559
51c3ac47d127
added checking so that (rename_tac "x y") is rejected, since
paulson
parents:
12904
diff
changeset
|
29 |
val is_identifier: symbol -> bool |
8230 | 30 |
val is_symbolic: symbol -> bool |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
31 |
val is_printable: symbol -> bool |
6272 | 32 |
val length: symbol list -> int |
11010 | 33 |
val strip_blanks: string -> string |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
34 |
val beginning: symbol list -> string |
13730 | 35 |
val scan_id: string list -> string * string list |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
36 |
val scan: string list -> symbol * string list |
6640 | 37 |
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
|
38 |
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
|
39 |
(symbol, (string, 'a) Source.source) Source.source |
14561
c53396af770e
* raw control symbols are of the form \<^raw:...> now.
schirmer
parents:
14559
diff
changeset
|
40 |
val escape: string -> string |
6272 | 41 |
val explode: string -> symbol list |
12904 | 42 |
val bump_string: string -> string |
10953 | 43 |
val default_indent: string * int -> string |
44 |
val add_mode: string -> (string -> string * real) * (string * int -> string) -> unit |
|
6692 | 45 |
val symbolsN: string |
46 |
val xsymbolsN: string |
|
10923 | 47 |
val plain_output: string -> string |
6272 | 48 |
val output: string -> string |
49 |
val output_width: string -> string * real |
|
10953 | 50 |
val indent: string * int -> string |
14595
2df717e26035
Added variants of functions quote and commas_quote that allow escaping
berghofe
parents:
14562
diff
changeset
|
51 |
val quote: string -> string |
2df717e26035
Added variants of functions quote and commas_quote that allow escaping
berghofe
parents:
14562
diff
changeset
|
52 |
val commas_quote: string list -> string |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
53 |
end; |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
54 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
55 |
structure Symbol: SYMBOL = |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
56 |
struct |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
57 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
58 |
|
6272 | 59 |
(** generalized characters **) |
60 |
||
61 |
(*symbols, which are considered the smallest entities of any Isabelle |
|
62 |
string, may be of the following form: |
|
63 |
(a) ASCII symbols: a |
|
64 |
(b) printable symbols: \<ident> |
|
14557
31ae4a47267c
* cleaner distinction between control symbols "\<^...>" and "\<^raw...>" in
schirmer
parents:
14361
diff
changeset
|
65 |
(c) control symbols: \<^ident> |
14561
c53396af770e
* raw control symbols are of the form \<^raw:...> now.
schirmer
parents:
14559
diff
changeset
|
66 |
(d) raw control symbols: \<^raw:...>, where "..." may be any printable |
14557
31ae4a47267c
* cleaner distinction between control symbols "\<^...>" and "\<^raw...>" in
schirmer
parents:
14361
diff
changeset
|
67 |
character excluding ">" |
6272 | 68 |
|
12116 | 69 |
output is subject to the print_mode variable (default: verbatim), |
70 |
actual interpretation in display is up to front-end tools; |
|
14561
c53396af770e
* raw control symbols are of the form \<^raw:...> now.
schirmer
parents:
14559
diff
changeset
|
71 |
|
c53396af770e
* raw control symbols are of the form \<^raw:...> now.
schirmer
parents:
14559
diff
changeset
|
72 |
Symbols (b),(c) and (d) may optionally start with "\\" instead of just "\" |
c53396af770e
* raw control symbols are of the form \<^raw:...> now.
schirmer
parents:
14559
diff
changeset
|
73 |
for compatibility with ML-strings of old style theory and ML-files and |
c53396af770e
* raw control symbols are of the form \<^raw:...> now.
schirmer
parents:
14559
diff
changeset
|
74 |
isa-ProofGeneral. The default output of these symbols will also start with "\\". |
c53396af770e
* raw control symbols are of the form \<^raw:...> now.
schirmer
parents:
14559
diff
changeset
|
75 |
This is used by the Isar ML-command to convert Isabelle-strings to ML-strings, |
c53396af770e
* raw control symbols are of the form \<^raw:...> now.
schirmer
parents:
14559
diff
changeset
|
76 |
before evaluation. |
6272 | 77 |
*) |
78 |
||
79 |
type symbol = string; |
|
80 |
||
81 |
val space = " "; |
|
10953 | 82 |
fun spaces k = Library.replicate_string k space; |
6857 | 83 |
val sync = "\\<^sync>"; |
10747 | 84 |
val malformed = "\\<^malformed>"; |
6272 | 85 |
val eof = ""; |
86 |
||
87 |
||
88 |
(* kinds *) |
|
89 |
||
6857 | 90 |
fun is_sync s = s = sync; |
91 |
fun not_sync s = s <> sync; |
|
92 |
||
6272 | 93 |
fun is_eof s = s = eof; |
94 |
fun not_eof s = s <> eof; |
|
95 |
val stopper = (eof, is_eof); |
|
96 |
||
97 |
fun is_ascii s = size s = 1 andalso ord s < 128; |
|
98 |
||
14171
0cab06e3bbd0
Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents:
13730
diff
changeset
|
99 |
local |
0cab06e3bbd0
Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents:
13730
diff
changeset
|
100 |
fun wrap s = "\\<" ^ s ^ ">" |
0cab06e3bbd0
Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents:
13730
diff
changeset
|
101 |
|
0cab06e3bbd0
Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents:
13730
diff
changeset
|
102 |
val cal_letters = |
0cab06e3bbd0
Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents:
13730
diff
changeset
|
103 |
["A","B","C","D","E","F","G","H","I","J","K","L","M", |
0cab06e3bbd0
Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents:
13730
diff
changeset
|
104 |
"N","O","P","Q","R","S","T","U","V","W","X","Y","Z"] |
0cab06e3bbd0
Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents:
13730
diff
changeset
|
105 |
|
0cab06e3bbd0
Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents:
13730
diff
changeset
|
106 |
val small_letters = |
0cab06e3bbd0
Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents:
13730
diff
changeset
|
107 |
["a","b","c","d","e","f","g","h","i","j","k","l","m", |
0cab06e3bbd0
Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents:
13730
diff
changeset
|
108 |
"n","o","p","q","r","s","t","u","v","w","x","y","z"] |
0cab06e3bbd0
Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents:
13730
diff
changeset
|
109 |
|
0cab06e3bbd0
Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents:
13730
diff
changeset
|
110 |
val goth_letters = |
0cab06e3bbd0
Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents:
13730
diff
changeset
|
111 |
["AA","BB","CC","DD","EE","FF","GG","HH","II","JJ","KK","LL","MM", |
0cab06e3bbd0
Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents:
13730
diff
changeset
|
112 |
"NN","OO","PP","QQ","RR","SS","TT","UU","VV","WW","XX","YY","ZZ", |
0cab06e3bbd0
Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents:
13730
diff
changeset
|
113 |
"aa","bb","cc","dd","ee","ff","gg","hh","ii","jj","kk","ll","mm", |
0cab06e3bbd0
Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents:
13730
diff
changeset
|
114 |
"nn","oo","pp","qq","rr","ss","tt","uu","vv","ww","xx","yy","zz"] |
0cab06e3bbd0
Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents:
13730
diff
changeset
|
115 |
|
0cab06e3bbd0
Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents:
13730
diff
changeset
|
116 |
val greek_letters = |
0cab06e3bbd0
Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents:
13730
diff
changeset
|
117 |
["alpha","beta","gamma","delta","epsilon","zeta","eta","theta", |
0cab06e3bbd0
Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents:
13730
diff
changeset
|
118 |
"iota","kappa",(*"lambda",*)"mu","nu","xi","pi","rho","sigma","tau", |
0cab06e3bbd0
Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents:
13730
diff
changeset
|
119 |
"upsilon","phi","psi","omega","Gamma","Delta","Theta","Lambda", |
0cab06e3bbd0
Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents:
13730
diff
changeset
|
120 |
"Xi","Pi","Sigma","Upsilon","Phi","Psi","Omega"] |
0cab06e3bbd0
Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents:
13730
diff
changeset
|
121 |
|
0cab06e3bbd0
Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents:
13730
diff
changeset
|
122 |
val bbb_letters = ["bool","complex","nat","rat","real","int"] |
0cab06e3bbd0
Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents:
13730
diff
changeset
|
123 |
|
14234
9590df3c5f2a
use \<^isub> and \<^isup> in identifiers instead of just \<^sub> (avoid
kleing
parents:
14232
diff
changeset
|
124 |
val control_letters = ["^isub", "^isup"] |
14232 | 125 |
|
14171
0cab06e3bbd0
Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents:
13730
diff
changeset
|
126 |
val pre_letters = |
0cab06e3bbd0
Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents:
13730
diff
changeset
|
127 |
cal_letters @ |
0cab06e3bbd0
Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents:
13730
diff
changeset
|
128 |
small_letters @ |
0cab06e3bbd0
Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents:
13730
diff
changeset
|
129 |
goth_letters @ |
14232 | 130 |
greek_letters @ |
131 |
control_letters |
|
132 |
||
14171
0cab06e3bbd0
Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents:
13730
diff
changeset
|
133 |
in |
14232 | 134 |
val ext_letters = map wrap pre_letters |
14173 | 135 |
|
14609 | 136 |
fun is_ext_letter s = String.isPrefix "\\<" s andalso s mem_string ext_letters |
14171
0cab06e3bbd0
Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents:
13730
diff
changeset
|
137 |
end |
0cab06e3bbd0
Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents:
13730
diff
changeset
|
138 |
|
6272 | 139 |
fun is_letter s = |
14171
0cab06e3bbd0
Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents:
13730
diff
changeset
|
140 |
(size s = 1 andalso |
0cab06e3bbd0
Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents:
13730
diff
changeset
|
141 |
(ord "A" <= ord s andalso ord s <= ord "Z" orelse |
0cab06e3bbd0
Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents:
13730
diff
changeset
|
142 |
ord "a" <= ord s andalso ord s <= ord "z")) |
0cab06e3bbd0
Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents:
13730
diff
changeset
|
143 |
orelse is_ext_letter s |
6272 | 144 |
|
145 |
fun is_digit s = |
|
14173 | 146 |
size s = 1 andalso ord "0" <= ord s andalso ord s <= ord "9" |
6272 | 147 |
|
12904 | 148 |
fun is_quasi "_" = true |
149 |
| is_quasi "'" = true |
|
150 |
| is_quasi _ = false; |
|
151 |
||
152 |
fun is_quasi_letter s = is_quasi s orelse is_letter s; |
|
6272 | 153 |
|
154 |
val is_blank = |
|
14221 | 155 |
fn " " => true | "\t" => true | "\r" => true | "\n" => true | "\^L" => true |
6272 | 156 |
| "\160" => true | "\\<spacespace>" => true |
157 |
| _ => false; |
|
158 |
||
12904 | 159 |
fun is_letdig s = is_quasi_letter s orelse is_digit s; |
6272 | 160 |
|
8230 | 161 |
fun is_symbolic s = |
14171
0cab06e3bbd0
Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents:
13730
diff
changeset
|
162 |
size s > 2 andalso nth_elem_string (2, s) <> "^" |
14173 | 163 |
andalso not (is_ext_letter s); |
8230 | 164 |
|
6272 | 165 |
fun is_printable s = |
166 |
size s = 1 andalso ord space <= ord s andalso ord s <= ord "~" orelse |
|
14173 | 167 |
is_ext_letter s orelse |
8230 | 168 |
is_symbolic s; |
169 |
||
14361
ad2f5da643b4
* Support for raw latex output in control symbols: \<^raw...>
schirmer
parents:
14234
diff
changeset
|
170 |
fun is_ctrl_letter s = |
ad2f5da643b4
* Support for raw latex output in control symbols: \<^raw...>
schirmer
parents:
14234
diff
changeset
|
171 |
size s = 1 andalso ord space <= ord s andalso ord s <= ord "~" andalso s <> ">"; |
ad2f5da643b4
* Support for raw latex output in control symbols: \<^raw...>
schirmer
parents:
14234
diff
changeset
|
172 |
|
13559
51c3ac47d127
added checking so that (rename_tac "x y") is rejected, since
paulson
parents:
12904
diff
changeset
|
173 |
fun is_identifier s = |
51c3ac47d127
added checking so that (rename_tac "x y") is rejected, since
paulson
parents:
12904
diff
changeset
|
174 |
case (explode s) of |
51c3ac47d127
added checking so that (rename_tac "x y") is rejected, since
paulson
parents:
12904
diff
changeset
|
175 |
[] => false |
51c3ac47d127
added checking so that (rename_tac "x y") is rejected, since
paulson
parents:
12904
diff
changeset
|
176 |
| c::cs => is_letter c andalso forall is_letdig cs; |
6272 | 177 |
|
10738 | 178 |
fun sym_length ss = foldl (fn (n, s) => |
179 |
(if not (is_printable s) then 0 else |
|
180 |
(case Library.try String.substring (s, 2, 4) of |
|
181 |
Some s' => if s' = "long" orelse s' = "Long" then 2 else 1 |
|
182 |
| None => 1)) + n) (0, ss); |
|
6272 | 183 |
|
11010 | 184 |
fun strip_blanks s = |
185 |
implode (#1 (Library.take_suffix is_blank (#2 (Library.take_prefix is_blank (explode s))))); |
|
186 |
||
6272 | 187 |
|
188 |
(* beginning *) |
|
189 |
||
190 |
val smash_blanks = map (fn s => if is_blank s then space else s); |
|
191 |
||
192 |
fun beginning raw_ss = |
|
193 |
let |
|
194 |
val (all_ss, _) = take_suffix is_blank raw_ss; |
|
195 |
val dots = if length all_ss > 10 then " ..." else ""; |
|
196 |
val (ss, _) = take_suffix is_blank (take (10, all_ss)); |
|
197 |
in implode (smash_blanks ss) ^ dots end; |
|
198 |
||
199 |
||
200 |
||
8998 | 201 |
(** scanning through symbols **) |
6640 | 202 |
|
203 |
fun scanner msg scan chs = |
|
204 |
let |
|
205 |
fun err_msg cs = msg ^ ": " ^ beginning cs; |
|
206 |
val fin_scan = Scan.error (Scan.finite stopper (!! (fn (cs, _) => err_msg cs) scan)); |
|
207 |
in |
|
208 |
(case fin_scan chs of |
|
209 |
(result, []) => result |
|
210 |
| (_, rest) => error (err_msg rest)) |
|
211 |
end; |
|
212 |
||
213 |
||
214 |
||
6272 | 215 |
(** symbol input **) |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
216 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
217 |
(* scan *) |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
218 |
|
14562
980da32f4617
proper handling of lines terminated by CRLF or CR;
wenzelm
parents:
14561
diff
changeset
|
219 |
val scan_newline = ($$ "\r" ^^ $$ "\n" || $$ "\r") >> K "\n"; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
220 |
val scan_id = Scan.one is_letter ^^ (Scan.any is_letdig >> implode); |
14559 | 221 |
val scan_rawctrlid = |
14561
c53396af770e
* raw control symbols are of the form \<^raw:...> now.
schirmer
parents:
14559
diff
changeset
|
222 |
$$ "r" ^^ $$ "a" ^^ $$ "w" ^^ $$ ":" ^^ (Scan.any is_ctrl_letter >> implode); |
c53396af770e
* raw control symbols are of the form \<^raw:...> now.
schirmer
parents:
14559
diff
changeset
|
223 |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
224 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
225 |
val scan = |
14561
c53396af770e
* raw control symbols are of the form \<^raw:...> now.
schirmer
parents:
14559
diff
changeset
|
226 |
($$ "\\" --| Scan.optional ($$ "\\") "") ^^ $$ "<" ^^ |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
227 |
!! (fn (cs, _) => "Malformed symbolic character specification: \\" ^ "<" ^ beginning cs) |
14557
31ae4a47267c
* cleaner distinction between control symbols "\<^...>" and "\<^raw...>" in
schirmer
parents:
14361
diff
changeset
|
228 |
((($$ "^" ^^ (scan_rawctrlid || scan_id)) || scan_id) ^^ $$ ">") || |
14562
980da32f4617
proper handling of lines terminated by CRLF or CR;
wenzelm
parents:
14561
diff
changeset
|
229 |
scan_newline || |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
230 |
Scan.one not_eof; |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
231 |
|
14562
980da32f4617
proper handling of lines terminated by CRLF or CR;
wenzelm
parents:
14561
diff
changeset
|
232 |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
233 |
(* source *) |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
234 |
|
10747 | 235 |
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
|
236 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
237 |
fun source do_recover src = |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
238 |
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
|
239 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
240 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
241 |
(* explode *) |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
242 |
|
14562
980da32f4617
proper handling of lines terminated by CRLF or CR;
wenzelm
parents:
14561
diff
changeset
|
243 |
fun no_explode [] = true |
980da32f4617
proper handling of lines terminated by CRLF or CR;
wenzelm
parents:
14561
diff
changeset
|
244 |
| no_explode ("\\" :: "<" :: _) = false |
980da32f4617
proper handling of lines terminated by CRLF or CR;
wenzelm
parents:
14561
diff
changeset
|
245 |
| no_explode ("\r" :: _) = false |
980da32f4617
proper handling of lines terminated by CRLF or CR;
wenzelm
parents:
14561
diff
changeset
|
246 |
| no_explode (_ :: cs) = no_explode cs; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
247 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
248 |
fun sym_explode str = |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
249 |
let val chs = explode str in |
14562
980da32f4617
proper handling of lines terminated by CRLF or CR;
wenzelm
parents:
14561
diff
changeset
|
250 |
if no_explode chs then chs |
12116 | 251 |
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
|
252 |
end; |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
253 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
254 |
|
12904 | 255 |
(* bump_string -- increment suffix of lowercase letters like a base 26 number *) |
256 |
||
257 |
fun bump_string str = |
|
258 |
let |
|
259 |
fun bump [] = ["a"] |
|
260 |
| bump ("z" :: ss) = "a" :: bump ss |
|
261 |
| bump (s :: ss) = |
|
262 |
if size s = 1 andalso ord "a" <= ord s andalso ord s < ord "z" |
|
263 |
then chr (ord s + 1) :: ss |
|
264 |
else "a" :: s :: ss; |
|
265 |
val (cs, qs) = Library.take_suffix is_quasi (sym_explode str); |
|
266 |
in implode (rev (bump (rev cs)) @ qs) end; |
|
267 |
||
6272 | 268 |
|
269 |
(** symbol output **) |
|
270 |
||
10953 | 271 |
(* default *) |
6272 | 272 |
|
273 |
fun string_size s = (s, real (size s)); |
|
274 |
||
14561
c53396af770e
* raw control symbols are of the form \<^raw:...> now.
schirmer
parents:
14559
diff
changeset
|
275 |
fun sym_escape s = if size s = 1 then s else "\\" ^ s; |
13730 | 276 |
|
14561
c53396af770e
* raw control symbols are of the form \<^raw:...> now.
schirmer
parents:
14559
diff
changeset
|
277 |
fun default_output s = |
c53396af770e
* raw control symbols are of the form \<^raw:...> now.
schirmer
parents:
14559
diff
changeset
|
278 |
if not (exists_string (equal "\\") s) then string_size s |
c53396af770e
* raw control symbols are of the form \<^raw:...> now.
schirmer
parents:
14559
diff
changeset
|
279 |
else string_size (implode (map sym_escape (sym_explode s))); |
6272 | 280 |
|
10953 | 281 |
fun default_indent (_: string, k) = spaces k; |
6272 | 282 |
|
10953 | 283 |
|
6272 | 284 |
(* maintain modes *) |
285 |
||
6692 | 286 |
val symbolsN = "symbols"; |
287 |
val xsymbolsN = "xsymbols"; |
|
288 |
||
12116 | 289 |
val modes = |
290 |
ref (Symtab.empty: ((string -> string * real) * (string * int -> string)) Symtab.table); |
|
6272 | 291 |
|
292 |
fun lookup_mode name = Symtab.lookup (! modes, name); |
|
293 |
||
10953 | 294 |
fun add_mode name m = |
6272 | 295 |
(if is_none (lookup_mode name) then () |
6320 | 296 |
else warning ("Redeclaration of symbol print mode " ^ quote name); |
10953 | 297 |
modes := Symtab.update ((name, m), ! modes)); |
298 |
||
299 |
fun get_mode () = |
|
300 |
if_none (get_first lookup_mode (! print_mode)) (default_output, default_indent); |
|
6272 | 301 |
|
302 |
||
303 |
(* mode output *) |
|
304 |
||
10953 | 305 |
fun output_width x = #1 (get_mode ()) x; |
6272 | 306 |
val output = #1 o output_width; |
14561
c53396af770e
* raw control symbols are of the form \<^raw:...> now.
schirmer
parents:
14559
diff
changeset
|
307 |
val plain_output = #1 o default_output; |
c53396af770e
* raw control symbols are of the form \<^raw:...> now.
schirmer
parents:
14559
diff
changeset
|
308 |
|
10953 | 309 |
fun indent x = #2 (get_mode ()) x; |
310 |
||
6272 | 311 |
|
14595
2df717e26035
Added variants of functions quote and commas_quote that allow escaping
berghofe
parents:
14562
diff
changeset
|
312 |
(* these variants allow escaping of quotes depending on mode *) |
2df717e26035
Added variants of functions quote and commas_quote that allow escaping
berghofe
parents:
14562
diff
changeset
|
313 |
|
2df717e26035
Added variants of functions quote and commas_quote that allow escaping
berghofe
parents:
14562
diff
changeset
|
314 |
fun quote s = output "\"" ^ s ^ output "\""; |
2df717e26035
Added variants of functions quote and commas_quote that allow escaping
berghofe
parents:
14562
diff
changeset
|
315 |
val commas_quote = space_implode (output ", ") o map quote; |
2df717e26035
Added variants of functions quote and commas_quote that allow escaping
berghofe
parents:
14562
diff
changeset
|
316 |
|
2df717e26035
Added variants of functions quote and commas_quote that allow escaping
berghofe
parents:
14562
diff
changeset
|
317 |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
318 |
(*final declarations of this structure!*) |
6272 | 319 |
val length = sym_length; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
320 |
val explode = sym_explode; |
14561
c53396af770e
* raw control symbols are of the form \<^raw:...> now.
schirmer
parents:
14559
diff
changeset
|
321 |
val escape = sym_escape; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
322 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
323 |
end; |
14595
2df717e26035
Added variants of functions quote and commas_quote that allow escaping
berghofe
parents:
14562
diff
changeset
|
324 |
|
2df717e26035
Added variants of functions quote and commas_quote that allow escaping
berghofe
parents:
14562
diff
changeset
|
325 |
|
2df717e26035
Added variants of functions quote and commas_quote that allow escaping
berghofe
parents:
14562
diff
changeset
|
326 |
(* Overwrites versions in Library *) |
2df717e26035
Added variants of functions quote and commas_quote that allow escaping
berghofe
parents:
14562
diff
changeset
|
327 |
|
2df717e26035
Added variants of functions quote and commas_quote that allow escaping
berghofe
parents:
14562
diff
changeset
|
328 |
val quote = Symbol.quote; |
14609 | 329 |
val commas_quote = Symbol.commas_quote; |