author | skalberg |
Thu, 03 Mar 2005 12:43:01 +0100 | |
changeset 15570 | 8d8c70b41bab |
parent 15531 | 08c8dad8e399 |
child 15965 | f422f8283491 |
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 |
14679 | 11 |
val is_ascii_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 |
15443
07f78cc82a73
indexname function now parses type variables as well; changed input
berghofe
parents:
14981
diff
changeset
|
23 |
val indexname: string -> indexname |
4703 | 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 |
|
14679 | 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 |
|
14679 | 80 |
fun is_ascii_identifier s = |
81 |
let val cs = Symbol.explode s |
|
82 |
in forall Symbol.is_ascii cs andalso is_ident cs end; |
|
83 |
||
18 | 84 |
fun is_xid s = |
4703 | 85 |
(case Symbol.explode s of |
18 | 86 |
"_" :: cs => is_ident cs |
87 |
| cs => is_ident cs); |
|
88 |
||
330
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
clasohm
parents:
237
diff
changeset
|
89 |
fun is_tid s = |
4703 | 90 |
(case Symbol.explode s of |
18 | 91 |
"'" :: cs => is_ident cs |
92 |
| _ => false); |
|
93 |
||
0 | 94 |
|
95 |
||
4703 | 96 |
(** basic scanners **) |
97 |
||
98 |
val scan_letter_letdigs = Scan.one Symbol.is_letter -- Scan.any Symbol.is_letdig >> op ::; |
|
99 |
val scan_digits1 = Scan.any1 Symbol.is_digit; |
|
100 |
||
101 |
val scan_id = scan_letter_letdigs >> implode; |
|
102 |
val scan_longid = scan_id ^^ (Scan.repeat1 ($$ "." ^^ scan_id) >> implode); |
|
103 |
val scan_tid = $$ "'" ^^ scan_id; |
|
104 |
||
105 |
val scan_nat = scan_digits1 >> implode; |
|
5513 | 106 |
val scan_int = $$ "-" ^^ scan_nat || scan_nat; |
4703 | 107 |
|
108 |
val scan_id_nat = scan_id ^^ Scan.optional ($$ "." ^^ scan_nat) ""; |
|
109 |
val scan_var = $$ "?" ^^ scan_id_nat; |
|
4902 | 110 |
val scan_tvar = $$ "?" ^^ $$ "'" ^^ scan_id_nat; |
4703 | 111 |
|
112 |
||
113 |
||
18 | 114 |
(** string_of_vname **) |
0 | 115 |
|
14783 | 116 |
val string_of_vname = Term.string_of_vname; |
117 |
val string_of_vname' = Term.string_of_vname'; |
|
2583
690835a06cf2
added string_of_vname' (treats neg. index as free);
wenzelm
parents:
2363
diff
changeset
|
118 |
|
18 | 119 |
|
0 | 120 |
|
18 | 121 |
(** datatype token **) |
0 | 122 |
|
18 | 123 |
datatype token = |
124 |
Token of string | |
|
125 |
IdentSy of string | |
|
3828 | 126 |
LongIdentSy of string | |
18 | 127 |
VarSy of string | |
128 |
TFreeSy of string | |
|
129 |
TVarSy of string | |
|
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
130 |
NumSy of string | |
11697 | 131 |
XNumSy of string | |
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
132 |
StrSy of string | |
18 | 133 |
EndToken; |
0 | 134 |
|
135 |
||
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
136 |
(* terminal arguments *) |
0 | 137 |
|
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
138 |
val idT = Type ("id", []); |
3828 | 139 |
val longidT = Type ("longid", []); |
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
140 |
val varT = Type ("var", []); |
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
141 |
val tidT = Type ("tid", []); |
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
142 |
val tvarT = Type ("tvar", []); |
0 | 143 |
|
11697 | 144 |
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
|
145 |
|
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
146 |
fun is_terminal s = s mem terminals; |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
147 |
|
0 | 148 |
|
18 | 149 |
(* str_of_token *) |
0 | 150 |
|
18 | 151 |
fun str_of_token (Token s) = s |
152 |
| str_of_token (IdentSy s) = s |
|
3828 | 153 |
| str_of_token (LongIdentSy s) = s |
18 | 154 |
| str_of_token (VarSy s) = s |
155 |
| str_of_token (TFreeSy s) = s |
|
156 |
| str_of_token (TVarSy s) = s |
|
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
157 |
| str_of_token (NumSy s) = s |
11697 | 158 |
| str_of_token (XNumSy s) = s |
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
159 |
| str_of_token (StrSy s) = s |
376
d3d01131470f
extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents:
330
diff
changeset
|
160 |
| str_of_token EndToken = "EOF"; |
0 | 161 |
|
18 | 162 |
|
163 |
(* display_token *) |
|
0 | 164 |
|
18 | 165 |
fun display_token (Token s) = quote s |
166 |
| display_token (IdentSy s) = "id(" ^ s ^ ")" |
|
3828 | 167 |
| display_token (LongIdentSy s) = "longid(" ^ s ^ ")" |
18 | 168 |
| 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
|
169 |
| display_token (TFreeSy s) = "tid(" ^ s ^ ")" |
18 | 170 |
| display_token (TVarSy s) = "tvar(" ^ s ^ ")" |
11697 | 171 |
| display_token (NumSy s) = "num(" ^ s ^ ")" |
172 |
| display_token (XNumSy s) = "xnum(" ^ s ^ ")" |
|
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
173 |
| display_token (StrSy s) = "xstr(" ^ s ^ ")" |
18 | 174 |
| display_token EndToken = ""; |
0 | 175 |
|
18 | 176 |
|
177 |
(* matching_tokens *) |
|
0 | 178 |
|
18 | 179 |
fun matching_tokens (Token x, Token y) = (x = y) |
180 |
| matching_tokens (IdentSy _, IdentSy _) = true |
|
3828 | 181 |
| matching_tokens (LongIdentSy _, LongIdentSy _) = true |
18 | 182 |
| matching_tokens (VarSy _, VarSy _) = true |
183 |
| matching_tokens (TFreeSy _, TFreeSy _) = true |
|
184 |
| matching_tokens (TVarSy _, TVarSy _) = true |
|
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
185 |
| matching_tokens (NumSy _, NumSy _) = true |
11697 | 186 |
| matching_tokens (XNumSy _, XNumSy _) = true |
550
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) = |
15570 | 198 |
if is_none keyi orelse matching_tokens (valOf keyi, key) then |
376
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 |
11697 | 213 |
| valued_token (XNumSy _) = true |
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
214 |
| valued_token (StrSy _) = true |
18 | 215 |
| valued_token EndToken = false; |
0 | 216 |
|
217 |
||
18 | 218 |
(* predef_term *) |
0 | 219 |
|
15531 | 220 |
fun predef_term "id" = SOME (IdentSy "id") |
221 |
| predef_term "longid" = SOME (LongIdentSy "longid") |
|
222 |
| predef_term "var" = SOME (VarSy "var") |
|
223 |
| predef_term "tid" = SOME (TFreeSy "tid") |
|
224 |
| predef_term "tvar" = SOME (TVarSy "tvar") |
|
225 |
| predef_term "num" = SOME (NumSy "num") |
|
226 |
| predef_term "xnum" = SOME (XNumSy "xnum") |
|
227 |
| predef_term "xstr" = SOME (StrSy "xstr") |
|
228 |
| predef_term _ = NONE; |
|
0 | 229 |
|
230 |
||
4703 | 231 |
(* xstr tokens *) |
18 | 232 |
|
14730 | 233 |
fun lex_err msg prfx (cs, _) = |
234 |
"Inner lexical error: " ^ msg ^ " at " ^ quote (prfx ^ Symbol.beginning 10 cs); |
|
235 |
||
236 |
val scan_chr = |
|
237 |
$$ "\\" |-- Scan.one Symbol.not_eof || |
|
238 |
Scan.one (not_equal "'" andf Symbol.not_eof) || |
|
239 |
$$ "'" --| Scan.ahead (Scan.one (not_equal "'")); |
|
240 |
||
241 |
val scan_str = |
|
242 |
$$ "'" |-- $$ "'" |-- !! (lex_err "missing end of string" "''") |
|
243 |
(Scan.repeat scan_chr --| $$ "'" --| $$ "'"); |
|
244 |
||
0 | 245 |
|
4703 | 246 |
fun implode_xstr cs = enclose "''" "''" (implode (map (fn "'" => "\\'" | c => c) cs)); |
18 | 247 |
|
4703 | 248 |
fun explode_xstr str = |
5868 | 249 |
(case Scan.read Symbol.stopper scan_str (Symbol.explode str) of |
15531 | 250 |
SOME cs => cs |
5868 | 251 |
| _ => error ("Inner lexical error: literal string expected at " ^ quote str)); |
18 | 252 |
|
253 |
||
14730 | 254 |
(* nested comments *) |
255 |
||
256 |
val scan_cmt = |
|
257 |
Scan.depend (fn d => $$ "(" ^^ $$ "*" >> pair (d + 1)) || |
|
258 |
Scan.depend (fn 0 => Scan.fail | d => $$ "*" ^^ $$ ")" >> pair (d - 1)) || |
|
259 |
Scan.lift ($$ "*" --| Scan.ahead (Scan.one (not_equal ")"))) || |
|
260 |
Scan.lift (Scan.one (not_equal "*" andf Symbol.not_eof)); |
|
261 |
||
14783 | 262 |
val scan_comment = |
263 |
$$ "(" -- $$ "*" -- !! (lex_err "missing end of comment" "(*") |
|
14730 | 264 |
(Scan.pass 0 (Scan.repeat scan_cmt) -- $$ "*" -- $$ ")") |
14783 | 265 |
>> K (); |
14730 | 266 |
|
267 |
||
18 | 268 |
|
269 |
(** tokenize **) |
|
270 |
||
2363 | 271 |
fun tokenize lex xids chs = |
18 | 272 |
let |
273 |
val scan_xid = |
|
274 |
if xids then $$ "_" ^^ scan_id || scan_id |
|
275 |
else scan_id; |
|
276 |
||
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
277 |
val scan_val = |
4902 | 278 |
scan_tvar >> pair TVarSy || |
4703 | 279 |
scan_var >> pair VarSy || |
280 |
scan_tid >> pair TFreeSy || |
|
11697 | 281 |
scan_int >> pair NumSy || |
282 |
$$ "#" ^^ scan_int >> pair XNumSy || |
|
3828 | 283 |
scan_longid >> pair LongIdentSy || |
18 | 284 |
scan_xid >> pair IdentSy; |
285 |
||
4703 | 286 |
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
|
287 |
|
4703 | 288 |
val scan_token = |
15531 | 289 |
scan_comment >> K NONE || |
290 |
Scan.max (op <= o pairself snd) scan_lit scan_val >> (fn (tk, s) => SOME (tk s)) || |
|
291 |
scan_str >> (SOME o StrSy o implode_xstr) || |
|
292 |
Scan.one Symbol.is_blank >> K NONE; |
|
18 | 293 |
in |
4938 | 294 |
(case Scan.error (Scan.finite Symbol.stopper (Scan.repeat scan_token)) chs of |
15570 | 295 |
(toks, []) => List.mapPartial I toks @ [EndToken] |
4703 | 296 |
| (_, cs) => error ("Inner lexical error at: " ^ quote (implode cs))) |
18 | 297 |
end; |
298 |
||
299 |
||
300 |
||
301 |
(** scan variables **) |
|
302 |
||
303 |
(* scan_vname *) |
|
304 |
||
305 |
fun scan_vname chrs = |
|
306 |
let |
|
307 |
fun nat_of_chs n [] = n |
|
308 |
| nat_of_chs n (c :: cs) = nat_of_chs (n * 10 + (ord c - ord "0")) cs; |
|
309 |
||
4703 | 310 |
val nat = nat_of_chs 0; |
18 | 311 |
|
312 |
fun split_vname chs = |
|
4703 | 313 |
let val (cs, ds) = take_suffix Symbol.is_digit chs |
314 |
in (implode cs, nat ds) end |
|
18 | 315 |
|
316 |
val scan = |
|
4703 | 317 |
scan_letter_letdigs -- Scan.optional ($$ "." |-- scan_digits1 >> nat) ~1; |
18 | 318 |
in |
319 |
(case scan chrs of |
|
320 |
((cs, ~1), cs') => (split_vname cs, cs') |
|
321 |
| ((cs, i), cs') => ((implode cs, i), cs')) |
|
322 |
end; |
|
323 |
||
324 |
||
4703 | 325 |
(* indexname *) |
18 | 326 |
|
15443
07f78cc82a73
indexname function now parses type variables as well; changed input
berghofe
parents:
14981
diff
changeset
|
327 |
val scan_indexname = |
07f78cc82a73
indexname function now parses type variables as well; changed input
berghofe
parents:
14981
diff
changeset
|
328 |
$$ "'" |-- scan_vname >> (fn (x, i) => ("'" ^ x, i)) |
07f78cc82a73
indexname function now parses type variables as well; changed input
berghofe
parents:
14981
diff
changeset
|
329 |
|| scan_vname; |
07f78cc82a73
indexname function now parses type variables as well; changed input
berghofe
parents:
14981
diff
changeset
|
330 |
|
4703 | 331 |
fun indexname cs = |
15443
07f78cc82a73
indexname function now parses type variables as well; changed input
berghofe
parents:
14981
diff
changeset
|
332 |
(case Scan.read Symbol.stopper scan_indexname (Symbol.explode cs) of |
15531 | 333 |
SOME xi => xi |
15443
07f78cc82a73
indexname function now parses type variables as well; changed input
berghofe
parents:
14981
diff
changeset
|
334 |
| _ => error ("Lexical error in variable name: " ^ quote cs)); |
18 | 335 |
|
336 |
||
4703 | 337 |
(* read_var *) |
18 | 338 |
|
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
339 |
fun const c = Const (c, dummyT); |
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
340 |
fun free x = Free (x, dummyT); |
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
341 |
fun var xi = Var (xi, dummyT); |
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
342 |
|
4703 | 343 |
fun read_var str = |
18 | 344 |
let |
345 |
val scan = |
|
15443
07f78cc82a73
indexname function now parses type variables as well; changed input
berghofe
parents:
14981
diff
changeset
|
346 |
$$ "?" |-- scan_indexname >> var || |
4703 | 347 |
Scan.any Symbol.not_eof >> (free o implode); |
15570 | 348 |
in valOf (Scan.read Symbol.stopper scan (Symbol.explode str)) end; |
4587 | 349 |
|
350 |
||
5260 | 351 |
(* variable kinds *) |
352 |
||
9289 | 353 |
val internal = suffix "_"; |
354 |
val dest_internal = unsuffix "_"; |
|
355 |
||
5286 | 356 |
val skolem = suffix "__"; |
357 |
val dest_skolem = unsuffix "__"; |
|
5260 | 358 |
|
359 |
||
5860 | 360 |
(* read_nat *) |
361 |
||
362 |
fun read_nat str = |
|
15570 | 363 |
Option.map (#1 o Library.read_int) (Scan.read Symbol.stopper scan_digits1 (Symbol.explode str)); |
5860 | 364 |
|
365 |
||
9326 | 366 |
(* read_xnum *) |
367 |
||
368 |
fun read_xnum str = |
|
369 |
let |
|
370 |
val (sign, digs) = |
|
371 |
(case Symbol.explode str of |
|
372 |
"#" :: "-" :: cs => (~1, cs) |
|
373 |
| "#" :: cs => (1, cs) |
|
11697 | 374 |
| "-" :: cs => (~1, cs) |
375 |
| cs => (1, cs)); |
|
14835 | 376 |
in sign * #1 (Library.read_int digs) end; |
9326 | 377 |
|
378 |
||
7784 | 379 |
(* read_ident(s) *) |
380 |
||
381 |
fun read_idents str = |
|
382 |
let |
|
383 |
val blanks = Scan.any Symbol.is_blank; |
|
384 |
val junk = Scan.any Symbol.not_eof; |
|
385 |
val idents = Scan.repeat1 (blanks |-- scan_id --| blanks) -- junk; |
|
386 |
in |
|
387 |
(case Scan.read Symbol.stopper idents (Symbol.explode str) of |
|
15531 | 388 |
SOME (ids, []) => ids |
389 |
| SOME (_, bad) => error ("Bad identifier: " ^ quote (implode bad)) |
|
390 |
| NONE => error ("No identifier found in: " ^ quote str)) |
|
7784 | 391 |
end; |
392 |
||
0 | 393 |
end; |