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