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