author | wenzelm |
Wed, 19 Jan 1994 14:21:26 +0100 | |
changeset 237 | a7d3e712767a |
parent 164 | 43506f0a98ae |
child 330 | 2fda15dd1e0f |
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 |
|
18 | 5 |
Scanner combinators and Isabelle's main lexer (used for terms and typs). |
6 |
*) |
|
0 | 7 |
|
18 | 8 |
infix 5 -- ^^; |
9 |
infix 3 >>; |
|
10 |
infix 0 ||; |
|
0 | 11 |
|
164 | 12 |
signature SCANNER = |
13 |
sig |
|
14 |
exception LEXICAL_ERROR |
|
15 |
val >> : ('a -> 'b * 'c) * ('b -> 'd) -> 'a -> 'd * 'c |
|
16 |
val || : ('a -> 'b) * ('a -> 'b) -> 'a -> 'b |
|
17 |
val -- : ('a -> 'b * 'c) * ('c -> 'd * 'e) -> 'a -> ('b * 'd) * 'e |
|
18 |
val ^^ : ('a -> string * 'b) * ('b -> string * 'c) -> 'a -> string * 'c |
|
19 |
val $$ : ''a -> ''a list -> ''a * ''a list |
|
20 |
val scan_empty: 'a list -> 'b list * 'a list |
|
21 |
val scan_one: ('a -> bool) -> 'a list -> 'a * 'a list |
|
22 |
val scan_any: ('a -> bool) -> 'a list -> 'a list * 'a list |
|
23 |
val scan_any1: ('a -> bool) -> 'a list -> 'a list * 'a list |
|
24 |
val scan_end: 'a list -> 'b list * 'a list |
|
25 |
val optional: ('a -> 'b * 'a) -> 'a -> 'b option * 'a |
|
26 |
val repeat: ('a -> 'b * 'a) -> 'a -> 'b list * 'a |
|
27 |
val repeat1: ('a -> 'b * 'a) -> 'a -> 'b list * 'a |
|
28 |
end; |
|
29 |
||
0 | 30 |
signature LEXICON0 = |
31 |
sig |
|
32 |
val is_identifier: string -> bool |
|
18 | 33 |
val string_of_vname: indexname -> string |
0 | 34 |
val scan_varname: string list -> indexname * string list |
18 | 35 |
val scan_var: string -> term |
0 | 36 |
end; |
37 |
||
38 |
signature LEXICON = |
|
39 |
sig |
|
164 | 40 |
include SCANNER |
18 | 41 |
include LEXICON0 |
42 |
val is_xid: string -> bool |
|
43 |
val is_tfree: string -> bool |
|
44 |
type lexicon |
|
45 |
datatype token = |
|
46 |
Token of string | |
|
47 |
IdentSy of string | |
|
48 |
VarSy of string | |
|
49 |
TFreeSy of string | |
|
50 |
TVarSy of string | |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
51 |
EndToken |
18 | 52 |
val id: string |
53 |
val var: string |
|
54 |
val tfree: string |
|
55 |
val tvar: string |
|
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 |
|
61 |
val valued_token: token -> bool |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
62 |
val predef_term: string -> token option |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
63 |
val dest_lexicon: lexicon -> string list |
18 | 64 |
val empty_lexicon: lexicon |
65 |
val extend_lexicon: lexicon -> string list -> lexicon |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
66 |
val merge_lexicons: lexicon -> lexicon -> lexicon |
18 | 67 |
val tokenize: lexicon -> bool -> string -> token list |
0 | 68 |
end; |
69 |
||
18 | 70 |
functor LexiconFun(): LEXICON = |
0 | 71 |
struct |
72 |
||
18 | 73 |
|
74 |
(** is_identifier etc. **) |
|
75 |
||
76 |
fun is_ident [] = false |
|
77 |
| is_ident (c :: cs) = is_letter c andalso forall is_letdig cs; |
|
78 |
||
79 |
val is_identifier = is_ident o explode; |
|
80 |
||
81 |
fun is_xid s = |
|
82 |
(case explode s of |
|
83 |
"_" :: cs => is_ident cs |
|
84 |
| cs => is_ident cs); |
|
85 |
||
86 |
fun is_tfree s = |
|
87 |
(case explode s of |
|
88 |
"'" :: cs => is_ident cs |
|
89 |
| _ => false); |
|
90 |
||
0 | 91 |
|
92 |
||
18 | 93 |
(** string_of_vname **) |
0 | 94 |
|
18 | 95 |
fun string_of_vname (x, i) = |
96 |
let |
|
97 |
val si = string_of_int i; |
|
98 |
in |
|
99 |
if is_digit (last_elem (explode x)) then "?" ^ x ^ "." ^ si |
|
100 |
else if i = 0 then "?" ^ x |
|
101 |
else "?" ^ x ^ si |
|
102 |
end; |
|
0 | 103 |
|
18 | 104 |
|
0 | 105 |
|
18 | 106 |
(** datatype token **) |
0 | 107 |
|
18 | 108 |
datatype token = |
109 |
Token of string | |
|
110 |
IdentSy of string | |
|
111 |
VarSy of string | |
|
112 |
TFreeSy of string | |
|
113 |
TVarSy of string | |
|
114 |
EndToken; |
|
0 | 115 |
|
116 |
||
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
117 |
(* terminal arguments *) |
0 | 118 |
|
18 | 119 |
val id = "id"; |
120 |
val var = "var"; |
|
121 |
val tfree = "tfree"; |
|
122 |
val tvar = "tvar"; |
|
0 | 123 |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
124 |
val terminals = [id, var, tfree, tvar]; |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
125 |
|
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
126 |
fun is_terminal s = s mem terminals; |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
127 |
|
0 | 128 |
|
18 | 129 |
(* str_of_token *) |
0 | 130 |
|
18 | 131 |
fun str_of_token (Token s) = s |
132 |
| str_of_token (IdentSy s) = s |
|
133 |
| str_of_token (VarSy s) = s |
|
134 |
| str_of_token (TFreeSy s) = s |
|
135 |
| str_of_token (TVarSy s) = s |
|
136 |
| str_of_token EndToken = ""; |
|
0 | 137 |
|
18 | 138 |
|
139 |
(* display_token *) |
|
0 | 140 |
|
18 | 141 |
fun display_token (Token s) = quote s |
142 |
| display_token (IdentSy s) = "id(" ^ s ^ ")" |
|
143 |
| display_token (VarSy s) = "var(" ^ s ^ ")" |
|
144 |
| display_token (TFreeSy s) = "tfree(" ^ s ^ ")" |
|
145 |
| display_token (TVarSy s) = "tvar(" ^ s ^ ")" |
|
146 |
| display_token EndToken = ""; |
|
0 | 147 |
|
18 | 148 |
|
149 |
(* matching_tokens *) |
|
0 | 150 |
|
18 | 151 |
fun matching_tokens (Token x, Token y) = (x = y) |
152 |
| matching_tokens (IdentSy _, IdentSy _) = true |
|
153 |
| matching_tokens (VarSy _, VarSy _) = true |
|
154 |
| matching_tokens (TFreeSy _, TFreeSy _) = true |
|
155 |
| matching_tokens (TVarSy _, TVarSy _) = true |
|
156 |
| matching_tokens (EndToken, EndToken) = true |
|
157 |
| matching_tokens _ = false; |
|
0 | 158 |
|
159 |
||
18 | 160 |
(* valued_token *) |
0 | 161 |
|
18 | 162 |
fun valued_token (Token _) = false |
163 |
| valued_token (IdentSy _) = true |
|
164 |
| valued_token (VarSy _) = true |
|
165 |
| valued_token (TFreeSy _) = true |
|
166 |
| valued_token (TVarSy _) = true |
|
167 |
| valued_token EndToken = false; |
|
0 | 168 |
|
169 |
||
18 | 170 |
(* predef_term *) |
0 | 171 |
|
172 |
fun predef_term name = |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
173 |
if name = id then Some (IdentSy name) |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
174 |
else if name = var then Some (VarSy name) |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
175 |
else if name = tfree then Some (TFreeSy name) |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
176 |
else if name = tvar then Some (TVarSy name) |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
177 |
else None; |
0 | 178 |
|
179 |
||
180 |
||
18 | 181 |
(** datatype lexicon **) |
182 |
||
183 |
datatype lexicon = |
|
184 |
Empty | |
|
185 |
Branch of string * string * lexicon * lexicon * lexicon; |
|
186 |
||
187 |
val no_token = ""; |
|
188 |
||
189 |
||
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
190 |
(* dest_lexicon *) |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
191 |
|
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
192 |
fun dest_lexicon Empty = [] |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
193 |
| dest_lexicon (Branch (_, "", lt, eq, gt)) = |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
194 |
dest_lexicon eq @ dest_lexicon lt @ dest_lexicon gt |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
195 |
| dest_lexicon (Branch (_, str, lt, eq, gt)) = |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
196 |
str :: (dest_lexicon eq @ dest_lexicon lt @ dest_lexicon gt); |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
197 |
|
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
198 |
|
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
199 |
(* empty, extend, merge lexicons *) |
18 | 200 |
|
201 |
val empty_lexicon = Empty; |
|
202 |
||
203 |
fun extend_lexicon lexicon strs = |
|
204 |
let |
|
205 |
fun ext (lex, s) = |
|
206 |
let |
|
207 |
fun add (Branch (d, a, lt, eq, gt)) (chs as c :: cs) = |
|
208 |
if c < d then Branch (d, a, add lt chs, eq, gt) |
|
209 |
else if c > d then Branch (d, a, lt, eq, add gt chs) |
|
210 |
else Branch (d, if null cs then s else a, lt, add eq cs, gt) |
|
211 |
| add Empty [c] = |
|
212 |
Branch (c, s, Empty, Empty, Empty) |
|
213 |
| add Empty (c :: cs) = |
|
214 |
Branch (c, no_token, Empty, add Empty cs, Empty) |
|
215 |
| add lex [] = lex; |
|
216 |
||
217 |
val cs = explode s; |
|
218 |
in |
|
219 |
if exists is_blank cs then |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
220 |
sys_error ("extend_lexicon: blank in delimiter " ^ quote s) |
18 | 221 |
else add lex cs |
222 |
end; |
|
223 |
in |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
224 |
foldl ext (lexicon, strs \\ dest_lexicon lexicon) |
18 | 225 |
end; |
226 |
||
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
227 |
fun merge_lexicons lex1 lex2 = |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
228 |
let |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
229 |
val strs1 = dest_lexicon lex1; |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
230 |
val strs2 = dest_lexicon lex2; |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
231 |
in |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
232 |
if strs2 subset strs1 then lex1 |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
233 |
else if strs1 subset strs2 then lex2 |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
234 |
else extend_lexicon lex1 strs2 |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
235 |
end; |
18 | 236 |
|
237 |
||
238 |
||
239 |
(** scanners **) |
|
240 |
||
241 |
exception LEXICAL_ERROR; |
|
242 |
||
0 | 243 |
|
18 | 244 |
(* scanner combinators *) |
245 |
||
246 |
fun (scan >> f) cs = apfst f (scan cs); |
|
247 |
||
248 |
fun (scan1 || scan2) cs = scan1 cs handle LEXICAL_ERROR => scan2 cs; |
|
249 |
||
250 |
fun (scan1 -- scan2) cs = |
|
251 |
let |
|
252 |
val (x, cs') = scan1 cs; |
|
253 |
val (y, cs'') = scan2 cs'; |
|
254 |
in |
|
255 |
((x, y), cs'') |
|
256 |
end; |
|
257 |
||
258 |
fun (scan1 ^^ scan2) = scan1 -- scan2 >> op ^; |
|
259 |
||
260 |
||
261 |
(* generic scanners *) |
|
262 |
||
263 |
fun $$ _ [] = raise LEXICAL_ERROR |
|
264 |
| $$ a (c :: cs) = |
|
265 |
if a = c then (c, cs) else raise LEXICAL_ERROR; |
|
266 |
||
267 |
fun scan_empty cs = ([], cs); |
|
0 | 268 |
|
18 | 269 |
fun scan_one _ [] = raise LEXICAL_ERROR |
270 |
| scan_one pred (c :: cs) = |
|
271 |
if pred c then (c, cs) else raise LEXICAL_ERROR; |
|
272 |
||
164 | 273 |
fun scan_any _ [] = ([], []) |
274 |
| scan_any pred (chs as c :: cs) = |
|
275 |
if pred c then apfst (cons c) (scan_any pred cs) |
|
276 |
else ([], chs); |
|
18 | 277 |
|
278 |
fun scan_any1 pred = scan_one pred -- scan_any pred >> op ::; |
|
279 |
||
280 |
fun scan_rest cs = (cs, []); |
|
281 |
||
282 |
fun scan_end [] = ([], []) |
|
283 |
| scan_end _ = raise LEXICAL_ERROR; |
|
284 |
||
285 |
fun optional scan = scan >> Some || scan_empty >> K None; |
|
286 |
||
287 |
fun repeat scan cs = (scan -- repeat scan >> op :: || scan_empty) cs; |
|
288 |
||
289 |
fun repeat1 scan = scan -- repeat scan >> op ::; |
|
290 |
||
291 |
||
292 |
(* other scanners *) |
|
293 |
||
294 |
val scan_letter_letdigs = scan_one is_letter -- scan_any is_letdig >> op ::; |
|
295 |
||
296 |
val scan_digits1 = scan_any1 is_digit; |
|
297 |
||
298 |
val scan_id = scan_letter_letdigs >> implode; |
|
0 | 299 |
|
18 | 300 |
val scan_id_nat = |
301 |
scan_id ^^ ($$ "." ^^ (scan_digits1 >> implode) || scan_empty >> K ""); |
|
302 |
||
303 |
||
304 |
(* scan_literal *) |
|
305 |
||
306 |
fun scan_literal lex chrs = |
|
307 |
let |
|
308 |
fun scan_lit _ s_cs [] = s_cs |
|
309 |
| scan_lit Empty s_cs _ = s_cs |
|
310 |
| scan_lit (Branch (d, a, lt, eq, gt)) s_cs (chs as c :: cs) = |
|
311 |
if c < d then scan_lit lt s_cs chs |
|
312 |
else if c > d then scan_lit gt s_cs chs |
|
313 |
else scan_lit eq (if a = no_token then s_cs else Some (a, cs)) cs; |
|
314 |
in |
|
315 |
(case scan_lit lex None chrs of |
|
316 |
None => raise LEXICAL_ERROR |
|
317 |
| Some s_cs => s_cs) |
|
318 |
end; |
|
319 |
||
320 |
||
321 |
||
322 |
(** tokenize **) |
|
323 |
||
324 |
fun tokenize lex xids str = |
|
325 |
let |
|
326 |
val scan_xid = |
|
327 |
if xids then $$ "_" ^^ scan_id || scan_id |
|
328 |
else scan_id; |
|
329 |
||
330 |
val scan_lit = scan_literal lex >> pair Token; |
|
331 |
||
332 |
val scan_ident = |
|
333 |
$$ "?" ^^ $$ "'" ^^ scan_id_nat >> pair TVarSy || |
|
334 |
$$ "?" ^^ scan_id_nat >> pair VarSy || |
|
335 |
$$ "'" ^^ scan_id >> pair TFreeSy || |
|
336 |
scan_xid >> pair IdentSy; |
|
337 |
||
338 |
fun scan_max_token cs = |
|
339 |
(case (optional scan_lit cs, optional scan_ident cs) of |
|
340 |
(tok1, (None, _)) => tok1 |
|
341 |
| ((None, _), tok2) => tok2 |
|
342 |
| (tok1 as (Some (_, s1), _), tok2 as (Some (_, s2), _)) => |
|
343 |
if size s1 >= size s2 then tok1 else tok2); |
|
0 | 344 |
|
18 | 345 |
fun scan_tokens [] rev_toks = rev (EndToken :: rev_toks) |
346 |
| scan_tokens (chs as c :: cs) rev_toks = |
|
347 |
if is_blank c then scan_tokens cs rev_toks |
|
348 |
else |
|
349 |
(case scan_max_token chs of |
|
350 |
(None, _) => error ("Lexical error at: " ^ quote (implode chs)) |
|
351 |
| (Some (tk, s), chs') => scan_tokens chs' (tk s :: rev_toks)); |
|
352 |
in |
|
353 |
scan_tokens (explode str) [] |
|
354 |
end; |
|
355 |
||
356 |
||
357 |
||
358 |
(** scan variables **) |
|
359 |
||
360 |
(* scan_vname *) |
|
361 |
||
362 |
fun scan_vname chrs = |
|
363 |
let |
|
364 |
fun nat_of_chs n [] = n |
|
365 |
| nat_of_chs n (c :: cs) = nat_of_chs (n * 10 + (ord c - ord "0")) cs; |
|
366 |
||
367 |
val nat_of = nat_of_chs 0; |
|
368 |
||
369 |
fun split_vname chs = |
|
370 |
let val (cs, ds) = take_suffix is_digit chs |
|
371 |
in (implode cs, nat_of ds) end |
|
372 |
||
373 |
val scan = |
|
374 |
scan_letter_letdigs -- |
|
375 |
($$ "." -- scan_digits1 >> (nat_of o #2) || scan_empty >> K ~1); |
|
376 |
in |
|
377 |
(case scan chrs of |
|
378 |
((cs, ~1), cs') => (split_vname cs, cs') |
|
379 |
| ((cs, i), cs') => ((implode cs, i), cs')) |
|
380 |
end; |
|
381 |
||
382 |
||
383 |
(* scan_varname *) |
|
384 |
||
385 |
fun scan_varname chs = |
|
386 |
scan_vname chs handle LEXICAL_ERROR |
|
387 |
=> error ("scan_varname: bad varname " ^ quote (implode chs)); |
|
388 |
||
389 |
||
390 |
(* scan_var *) |
|
391 |
||
392 |
fun scan_var str = |
|
393 |
let |
|
394 |
fun tvar (x, i) = Var (("'" ^ x, i), dummyT); |
|
395 |
fun var x_i = Var (x_i, dummyT); |
|
396 |
fun free x = Free (x, dummyT); |
|
397 |
||
398 |
val scan = |
|
399 |
$$ "?" -- $$ "'" -- scan_vname -- scan_end >> (tvar o #2 o #1) || |
|
400 |
$$ "?" -- scan_vname -- scan_end >> (var o #2 o #1) || |
|
401 |
scan_rest >> (free o implode); |
|
402 |
in |
|
403 |
#1 (scan (explode str)) |
|
404 |
end; |
|
0 | 405 |
|
406 |
||
407 |
end; |
|
408 |