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