author | wenzelm |
Tue, 20 Nov 2001 20:56:13 +0100 | |
changeset 12249 | dd9a51255855 |
parent 11697 | 8dd899efbd35 |
child 12785 | 27debaf2112d |
permissions | -rw-r--r-- |
18 | 1 |
(* Title: Pure/Syntax/lexicon.ML |
0 | 2 |
ID: $Id$ |
18 | 3 |
Author: Tobias Nipkow and Markus Wenzel, TU Muenchen |
0 | 4 |
|
4703 | 5 |
Lexer for the inner Isabelle syntax (terms and types). |
18 | 6 |
*) |
0 | 7 |
|
8 |
signature LEXICON0 = |
|
4247
9bba9251bb4d
added implode_xstr: string list -> string, explode_xstr: string -> string list;
wenzelm
parents:
3828
diff
changeset
|
9 |
sig |
0 | 10 |
val is_identifier: string -> bool |
4247
9bba9251bb4d
added implode_xstr: string list -> string, explode_xstr: string -> string list;
wenzelm
parents:
3828
diff
changeset
|
11 |
val implode_xstr: string list -> string |
9bba9251bb4d
added implode_xstr: string list -> string, explode_xstr: string -> string list;
wenzelm
parents:
3828
diff
changeset
|
12 |
val explode_xstr: string -> string list |
4703 | 13 |
val scan_id: string list -> string * string list |
14 |
val scan_longid: string list -> string * string list |
|
15 |
val scan_var: string list -> string * string list |
|
16 |
val scan_tid: string list -> string * string list |
|
4902 | 17 |
val scan_tvar: string list -> string * string list |
4703 | 18 |
val scan_nat: string list -> string * string list |
19 |
val scan_int: string list -> string * string list |
|
18 | 20 |
val string_of_vname: indexname -> string |
2583
690835a06cf2
added string_of_vname' (treats neg. index as free);
wenzelm
parents:
2363
diff
changeset
|
21 |
val string_of_vname': indexname -> string |
4703 | 22 |
val indexname: string list -> indexname |
23 |
val read_var: string -> term |
|
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
24 |
val const: string -> term |
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
25 |
val free: string -> term |
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
26 |
val var: indexname -> term |
9289 | 27 |
val internal: string -> string |
28 |
val dest_internal: string -> string |
|
5260 | 29 |
val skolem: string -> string |
5286 | 30 |
val dest_skolem: string -> string |
5860 | 31 |
val read_nat: string -> int option |
9326 | 32 |
val read_xnum: string -> int |
7784 | 33 |
val read_idents: string -> string list |
4247
9bba9251bb4d
added implode_xstr: string list -> string, explode_xstr: string -> string list;
wenzelm
parents:
3828
diff
changeset
|
34 |
end; |
0 | 35 |
|
36 |
signature LEXICON = |
|
4247
9bba9251bb4d
added implode_xstr: string list -> string, explode_xstr: string -> string list;
wenzelm
parents:
3828
diff
changeset
|
37 |
sig |
18 | 38 |
include LEXICON0 |
39 |
val is_xid: string -> bool |
|
330
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
clasohm
parents:
237
diff
changeset
|
40 |
val is_tid: string -> bool |
18 | 41 |
datatype token = |
42 |
Token of string | |
|
43 |
IdentSy of string | |
|
3828 | 44 |
LongIdentSy of string | |
18 | 45 |
VarSy of string | |
46 |
TFreeSy of string | |
|
47 |
TVarSy of string | |
|
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
48 |
NumSy of string | |
11697 | 49 |
XNumSy of string | |
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
50 |
StrSy of string | |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
51 |
EndToken |
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
52 |
val idT: typ |
3828 | 53 |
val longidT: typ |
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
54 |
val varT: typ |
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
55 |
val tidT: typ |
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
56 |
val tvarT: typ |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
57 |
val terminals: string list |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
58 |
val is_terminal: string -> bool |
18 | 59 |
val str_of_token: token -> string |
60 |
val display_token: token -> string |
|
61 |
val matching_tokens: token * token -> bool |
|
330
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
clasohm
parents:
237
diff
changeset
|
62 |
val token_assoc: (token option * 'a list) list * token -> 'a list |
18 | 63 |
val valued_token: token -> bool |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
64 |
val predef_term: string -> token option |
4703 | 65 |
val tokenize: Scan.lexicon -> bool -> string list -> token list |
4247
9bba9251bb4d
added implode_xstr: string list -> string, explode_xstr: string -> string list;
wenzelm
parents:
3828
diff
changeset
|
66 |
end; |
0 | 67 |
|
1507 | 68 |
structure Lexicon : LEXICON = |
0 | 69 |
struct |
70 |
||
4247
9bba9251bb4d
added implode_xstr: string list -> string, explode_xstr: string -> string list;
wenzelm
parents:
3828
diff
changeset
|
71 |
|
18 | 72 |
(** is_identifier etc. **) |
73 |
||
74 |
fun is_ident [] = false |
|
4703 | 75 |
| is_ident (c :: cs) = Symbol.is_letter c andalso forall Symbol.is_letdig cs; |
18 | 76 |
|
4703 | 77 |
val is_identifier = is_ident o Symbol.explode; |
18 | 78 |
|
79 |
fun is_xid s = |
|
4703 | 80 |
(case Symbol.explode s of |
18 | 81 |
"_" :: cs => is_ident cs |
82 |
| cs => is_ident cs); |
|
83 |
||
330
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
clasohm
parents:
237
diff
changeset
|
84 |
fun is_tid s = |
4703 | 85 |
(case Symbol.explode s of |
18 | 86 |
"'" :: cs => is_ident cs |
87 |
| _ => false); |
|
88 |
||
0 | 89 |
|
90 |
||
4703 | 91 |
(** basic scanners **) |
92 |
||
93 |
val scan_letter_letdigs = Scan.one Symbol.is_letter -- Scan.any Symbol.is_letdig >> op ::; |
|
94 |
val scan_digits1 = Scan.any1 Symbol.is_digit; |
|
95 |
||
96 |
val scan_id = scan_letter_letdigs >> implode; |
|
97 |
val scan_longid = scan_id ^^ (Scan.repeat1 ($$ "." ^^ scan_id) >> implode); |
|
98 |
val scan_tid = $$ "'" ^^ scan_id; |
|
99 |
||
100 |
val scan_nat = scan_digits1 >> implode; |
|
5513 | 101 |
val scan_int = $$ "-" ^^ scan_nat || scan_nat; |
4703 | 102 |
|
103 |
val scan_id_nat = scan_id ^^ Scan.optional ($$ "." ^^ scan_nat) ""; |
|
104 |
val scan_var = $$ "?" ^^ scan_id_nat; |
|
4902 | 105 |
val scan_tvar = $$ "?" ^^ $$ "'" ^^ scan_id_nat; |
4703 | 106 |
|
107 |
||
108 |
||
18 | 109 |
(** string_of_vname **) |
0 | 110 |
|
18 | 111 |
fun string_of_vname (x, i) = |
112 |
let |
|
113 |
val si = string_of_int i; |
|
6962 | 114 |
val dot = if_none (try (Symbol.is_digit o last_elem o Symbol.explode) x) true; |
18 | 115 |
in |
4703 | 116 |
if dot then "?" ^ x ^ "." ^ si |
117 |
else if i = 0 then "?" ^ x |
|
118 |
else "?" ^ x ^ si |
|
18 | 119 |
end; |
0 | 120 |
|
4703 | 121 |
fun string_of_vname' (x, ~1) = x |
122 |
| string_of_vname' xi = string_of_vname xi; |
|
2583
690835a06cf2
added string_of_vname' (treats neg. index as free);
wenzelm
parents:
2363
diff
changeset
|
123 |
|
18 | 124 |
|
0 | 125 |
|
18 | 126 |
(** datatype token **) |
0 | 127 |
|
18 | 128 |
datatype token = |
129 |
Token of string | |
|
130 |
IdentSy of string | |
|
3828 | 131 |
LongIdentSy of string | |
18 | 132 |
VarSy of string | |
133 |
TFreeSy of string | |
|
134 |
TVarSy of string | |
|
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
135 |
NumSy of string | |
11697 | 136 |
XNumSy of string | |
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
137 |
StrSy of string | |
18 | 138 |
EndToken; |
0 | 139 |
|
140 |
||
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
141 |
(* terminal arguments *) |
0 | 142 |
|
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
143 |
val idT = Type ("id", []); |
3828 | 144 |
val longidT = Type ("longid", []); |
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
145 |
val varT = Type ("var", []); |
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
146 |
val tidT = Type ("tid", []); |
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
147 |
val tvarT = Type ("tvar", []); |
0 | 148 |
|
11697 | 149 |
val terminals = ["id", "longid", "var", "tid", "tvar", "num", "xnum", "xstr"]; |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
150 |
|
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
151 |
fun is_terminal s = s mem terminals; |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
152 |
|
0 | 153 |
|
18 | 154 |
(* str_of_token *) |
0 | 155 |
|
18 | 156 |
fun str_of_token (Token s) = s |
157 |
| str_of_token (IdentSy s) = s |
|
3828 | 158 |
| str_of_token (LongIdentSy s) = s |
18 | 159 |
| str_of_token (VarSy s) = s |
160 |
| str_of_token (TFreeSy s) = s |
|
161 |
| str_of_token (TVarSy s) = s |
|
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
162 |
| str_of_token (NumSy s) = s |
11697 | 163 |
| str_of_token (XNumSy s) = s |
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
164 |
| str_of_token (StrSy s) = s |
376
d3d01131470f
extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents:
330
diff
changeset
|
165 |
| str_of_token EndToken = "EOF"; |
0 | 166 |
|
18 | 167 |
|
168 |
(* display_token *) |
|
0 | 169 |
|
18 | 170 |
fun display_token (Token s) = quote s |
171 |
| display_token (IdentSy s) = "id(" ^ s ^ ")" |
|
3828 | 172 |
| display_token (LongIdentSy s) = "longid(" ^ s ^ ")" |
18 | 173 |
| display_token (VarSy s) = "var(" ^ s ^ ")" |
330
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
clasohm
parents:
237
diff
changeset
|
174 |
| display_token (TFreeSy s) = "tid(" ^ s ^ ")" |
18 | 175 |
| display_token (TVarSy s) = "tvar(" ^ s ^ ")" |
11697 | 176 |
| display_token (NumSy s) = "num(" ^ s ^ ")" |
177 |
| display_token (XNumSy s) = "xnum(" ^ s ^ ")" |
|
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
178 |
| display_token (StrSy s) = "xstr(" ^ s ^ ")" |
18 | 179 |
| display_token EndToken = ""; |
0 | 180 |
|
18 | 181 |
|
182 |
(* matching_tokens *) |
|
0 | 183 |
|
18 | 184 |
fun matching_tokens (Token x, Token y) = (x = y) |
185 |
| matching_tokens (IdentSy _, IdentSy _) = true |
|
3828 | 186 |
| matching_tokens (LongIdentSy _, LongIdentSy _) = true |
18 | 187 |
| matching_tokens (VarSy _, VarSy _) = true |
188 |
| matching_tokens (TFreeSy _, TFreeSy _) = true |
|
189 |
| matching_tokens (TVarSy _, TVarSy _) = true |
|
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
190 |
| matching_tokens (NumSy _, NumSy _) = true |
11697 | 191 |
| matching_tokens (XNumSy _, XNumSy _) = true |
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
192 |
| matching_tokens (StrSy _, StrSy _) = true |
18 | 193 |
| matching_tokens (EndToken, EndToken) = true |
194 |
| matching_tokens _ = false; |
|
0 | 195 |
|
196 |
||
376
d3d01131470f
extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents:
330
diff
changeset
|
197 |
(* token_assoc *) |
d3d01131470f
extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents:
330
diff
changeset
|
198 |
|
330
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
clasohm
parents:
237
diff
changeset
|
199 |
fun token_assoc (list, key) = |
376
d3d01131470f
extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents:
330
diff
changeset
|
200 |
let |
d3d01131470f
extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents:
330
diff
changeset
|
201 |
fun assoc [] = [] |
d3d01131470f
extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents:
330
diff
changeset
|
202 |
| assoc ((keyi, xi) :: pairs) = |
d3d01131470f
extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents:
330
diff
changeset
|
203 |
if is_none keyi orelse matching_tokens (the keyi, key) then |
d3d01131470f
extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents:
330
diff
changeset
|
204 |
assoc pairs @ xi |
d3d01131470f
extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents:
330
diff
changeset
|
205 |
else assoc pairs; |
330
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
clasohm
parents:
237
diff
changeset
|
206 |
in assoc list end; |
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
clasohm
parents:
237
diff
changeset
|
207 |
|
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
clasohm
parents:
237
diff
changeset
|
208 |
|
18 | 209 |
(* valued_token *) |
0 | 210 |
|
18 | 211 |
fun valued_token (Token _) = false |
212 |
| valued_token (IdentSy _) = true |
|
3828 | 213 |
| valued_token (LongIdentSy _) = true |
18 | 214 |
| valued_token (VarSy _) = true |
215 |
| valued_token (TFreeSy _) = true |
|
216 |
| valued_token (TVarSy _) = true |
|
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
217 |
| valued_token (NumSy _) = true |
11697 | 218 |
| valued_token (XNumSy _) = true |
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
219 |
| valued_token (StrSy _) = true |
18 | 220 |
| valued_token EndToken = false; |
0 | 221 |
|
222 |
||
18 | 223 |
(* predef_term *) |
0 | 224 |
|
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
225 |
fun predef_term "id" = Some (IdentSy "id") |
3828 | 226 |
| predef_term "longid" = Some (LongIdentSy "longid") |
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
227 |
| predef_term "var" = Some (VarSy "var") |
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
228 |
| predef_term "tid" = Some (TFreeSy "tid") |
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
229 |
| predef_term "tvar" = Some (TVarSy "tvar") |
11697 | 230 |
| predef_term "num" = Some (NumSy "num") |
231 |
| predef_term "xnum" = Some (XNumSy "xnum") |
|
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
232 |
| predef_term "xstr" = Some (StrSy "xstr") |
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
233 |
| predef_term _ = None; |
0 | 234 |
|
235 |
||
4703 | 236 |
(* xstr tokens *) |
18 | 237 |
|
4703 | 238 |
val scan_chr = |
239 |
$$ "\\" |-- Scan.one Symbol.not_eof || |
|
240 |
Scan.one (not_equal "'" andf Symbol.not_eof) || |
|
241 |
$$ "'" --| Scan.ahead (Scan.one (not_equal "'")); |
|
18 | 242 |
|
4703 | 243 |
val scan_str = |
244 |
$$ "'" |-- $$ "'" |-- |
|
4921 | 245 |
!! (fn (cs, _) => "Inner lexical error: malformed literal string at " ^ |
5112 | 246 |
quote ("''" ^ Symbol.beginning cs)) |
4921 | 247 |
(Scan.repeat scan_chr --| $$ "'" --| $$ "'"); |
18 | 248 |
|
0 | 249 |
|
4703 | 250 |
fun implode_xstr cs = enclose "''" "''" (implode (map (fn "'" => "\\'" | c => c) cs)); |
18 | 251 |
|
4703 | 252 |
fun explode_xstr str = |
5868 | 253 |
(case Scan.read Symbol.stopper scan_str (Symbol.explode str) of |
254 |
Some cs => cs |
|
255 |
| _ => error ("Inner lexical error: literal string expected at " ^ quote str)); |
|
18 | 256 |
|
257 |
||
258 |
||
259 |
(** tokenize **) |
|
260 |
||
2363 | 261 |
fun tokenize lex xids chs = |
18 | 262 |
let |
263 |
val scan_xid = |
|
264 |
if xids then $$ "_" ^^ scan_id || scan_id |
|
265 |
else scan_id; |
|
266 |
||
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
267 |
val scan_val = |
4902 | 268 |
scan_tvar >> pair TVarSy || |
4703 | 269 |
scan_var >> pair VarSy || |
270 |
scan_tid >> pair TFreeSy || |
|
11697 | 271 |
scan_int >> pair NumSy || |
272 |
$$ "#" ^^ scan_int >> pair XNumSy || |
|
3828 | 273 |
scan_longid >> pair LongIdentSy || |
18 | 274 |
scan_xid >> pair IdentSy; |
275 |
||
4703 | 276 |
val scan_lit = Scan.literal lex >> (pair Token o implode); |
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
277 |
|
4703 | 278 |
val scan_token = |
279 |
Scan.max (op <= o pairself snd) scan_lit scan_val >> (fn (tk, s) => Some (tk s)) || |
|
280 |
scan_str >> (Some o StrSy o implode_xstr) || |
|
281 |
Scan.one Symbol.is_blank >> K None; |
|
18 | 282 |
in |
4938 | 283 |
(case Scan.error (Scan.finite Symbol.stopper (Scan.repeat scan_token)) chs of |
4703 | 284 |
(toks, []) => mapfilter I toks @ [EndToken] |
285 |
| (_, cs) => error ("Inner lexical error at: " ^ quote (implode cs))) |
|
18 | 286 |
end; |
287 |
||
288 |
||
289 |
||
290 |
(** scan variables **) |
|
291 |
||
292 |
(* scan_vname *) |
|
293 |
||
294 |
fun scan_vname chrs = |
|
295 |
let |
|
296 |
fun nat_of_chs n [] = n |
|
297 |
| nat_of_chs n (c :: cs) = nat_of_chs (n * 10 + (ord c - ord "0")) cs; |
|
298 |
||
4703 | 299 |
val nat = nat_of_chs 0; |
18 | 300 |
|
301 |
fun split_vname chs = |
|
4703 | 302 |
let val (cs, ds) = take_suffix Symbol.is_digit chs |
303 |
in (implode cs, nat ds) end |
|
18 | 304 |
|
305 |
val scan = |
|
4703 | 306 |
scan_letter_letdigs -- Scan.optional ($$ "." |-- scan_digits1 >> nat) ~1; |
18 | 307 |
in |
308 |
(case scan chrs of |
|
309 |
((cs, ~1), cs') => (split_vname cs, cs') |
|
310 |
| ((cs, i), cs') => ((implode cs, i), cs')) |
|
311 |
end; |
|
312 |
||
313 |
||
4703 | 314 |
(* indexname *) |
18 | 315 |
|
4703 | 316 |
fun indexname cs = |
5868 | 317 |
(case Scan.read Symbol.stopper scan_vname cs of |
318 |
Some xi => xi |
|
4703 | 319 |
| _ => error ("Lexical error in variable name: " ^ quote (implode cs))); |
18 | 320 |
|
321 |
||
4703 | 322 |
(* read_var *) |
18 | 323 |
|
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
324 |
fun const c = Const (c, dummyT); |
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
325 |
fun free x = Free (x, dummyT); |
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
326 |
fun var xi = Var (xi, dummyT); |
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
327 |
|
4703 | 328 |
fun read_var str = |
18 | 329 |
let |
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
330 |
fun tvar (x, i) = var ("'" ^ x, i); |
18 | 331 |
|
332 |
val scan = |
|
4703 | 333 |
$$ "?" |-- $$ "'" |-- scan_vname >> tvar || |
334 |
$$ "?" |-- scan_vname >> var || |
|
335 |
Scan.any Symbol.not_eof >> (free o implode); |
|
5868 | 336 |
in the (Scan.read Symbol.stopper scan (Symbol.explode str)) end; |
4587 | 337 |
|
338 |
||
5260 | 339 |
(* variable kinds *) |
340 |
||
9289 | 341 |
val internal = suffix "_"; |
342 |
val dest_internal = unsuffix "_"; |
|
343 |
||
5286 | 344 |
val skolem = suffix "__"; |
345 |
val dest_skolem = unsuffix "__"; |
|
5260 | 346 |
|
347 |
||
5860 | 348 |
(* read_nat *) |
349 |
||
350 |
fun read_nat str = |
|
5868 | 351 |
apsome (#1 o Term.read_int) (Scan.read Symbol.stopper scan_digits1 (Symbol.explode str)); |
5860 | 352 |
|
353 |
||
9326 | 354 |
(* read_xnum *) |
355 |
||
356 |
fun read_xnum str = |
|
357 |
let |
|
358 |
val (sign, digs) = |
|
359 |
(case Symbol.explode str of |
|
360 |
"#" :: "-" :: cs => (~1, cs) |
|
361 |
| "#" :: cs => (1, cs) |
|
11697 | 362 |
| "-" :: cs => (~1, cs) |
363 |
| cs => (1, cs)); |
|
9326 | 364 |
in sign * #1 (Term.read_int digs) end; |
365 |
||
366 |
||
7784 | 367 |
(* read_ident(s) *) |
368 |
||
369 |
fun read_idents str = |
|
370 |
let |
|
371 |
val blanks = Scan.any Symbol.is_blank; |
|
372 |
val junk = Scan.any Symbol.not_eof; |
|
373 |
val idents = Scan.repeat1 (blanks |-- scan_id --| blanks) -- junk; |
|
374 |
in |
|
375 |
(case Scan.read Symbol.stopper idents (Symbol.explode str) of |
|
376 |
Some (ids, []) => ids |
|
377 |
| Some (_, bad) => error ("Bad identifier: " ^ quote (implode bad)) |
|
378 |
| None => error ("No identifier found in: " ^ quote str)) |
|
379 |
end; |
|
380 |
||
381 |
||
0 | 382 |
end; |