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