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