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