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