author | obua |
Sun, 09 May 2004 23:04:36 +0200 | |
changeset 14722 | 8e739a6eaf11 |
parent 14679 | 6ed90bd68eda |
child 14730 | 59ab60c6fcc6 |
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 |
12785 | 4 |
License: GPL (GNU GENERAL PUBLIC LICENSE) |
0 | 5 |
|
4703 | 6 |
Lexer for the inner Isabelle syntax (terms and types). |
18 | 7 |
*) |
0 | 8 |
|
9 |
signature LEXICON0 = |
|
4247
9bba9251bb4d
added implode_xstr: string list -> string, explode_xstr: string -> string list;
wenzelm
parents:
3828
diff
changeset
|
10 |
sig |
0 | 11 |
val is_identifier: string -> bool |
14679 | 12 |
val is_ascii_identifier: string -> bool |
4247
9bba9251bb4d
added implode_xstr: string list -> string, explode_xstr: string -> string list;
wenzelm
parents:
3828
diff
changeset
|
13 |
val implode_xstr: string list -> string |
9bba9251bb4d
added implode_xstr: string list -> string, explode_xstr: string -> string list;
wenzelm
parents:
3828
diff
changeset
|
14 |
val explode_xstr: string -> string list |
4703 | 15 |
val scan_id: string list -> string * string list |
16 |
val scan_longid: string list -> string * string list |
|
17 |
val scan_var: string list -> string * string list |
|
18 |
val scan_tid: string list -> string * string list |
|
4902 | 19 |
val scan_tvar: string list -> string * string list |
4703 | 20 |
val scan_nat: string list -> string * string list |
21 |
val scan_int: string list -> string * string list |
|
18 | 22 |
val string_of_vname: indexname -> string |
2583
690835a06cf2
added string_of_vname' (treats neg. index as free);
wenzelm
parents:
2363
diff
changeset
|
23 |
val string_of_vname': indexname -> string |
4703 | 24 |
val indexname: string list -> indexname |
25 |
val read_var: string -> term |
|
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
26 |
val const: string -> term |
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
27 |
val free: string -> term |
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
28 |
val var: indexname -> term |
9289 | 29 |
val internal: string -> string |
30 |
val dest_internal: string -> string |
|
5260 | 31 |
val skolem: string -> string |
5286 | 32 |
val dest_skolem: string -> string |
5860 | 33 |
val read_nat: string -> int option |
9326 | 34 |
val read_xnum: string -> int |
7784 | 35 |
val read_idents: string -> string list |
4247
9bba9251bb4d
added implode_xstr: string list -> string, explode_xstr: string -> string list;
wenzelm
parents:
3828
diff
changeset
|
36 |
end; |
0 | 37 |
|
38 |
signature LEXICON = |
|
4247
9bba9251bb4d
added implode_xstr: string list -> string, explode_xstr: string -> string list;
wenzelm
parents:
3828
diff
changeset
|
39 |
sig |
18 | 40 |
include LEXICON0 |
41 |
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
|
42 |
val is_tid: string -> bool |
18 | 43 |
datatype token = |
44 |
Token of string | |
|
45 |
IdentSy of string | |
|
3828 | 46 |
LongIdentSy of string | |
18 | 47 |
VarSy of string | |
48 |
TFreeSy of string | |
|
49 |
TVarSy of string | |
|
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
50 |
NumSy of string | |
11697 | 51 |
XNumSy of string | |
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
52 |
StrSy of string | |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
53 |
EndToken |
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
54 |
val idT: typ |
3828 | 55 |
val longidT: typ |
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
56 |
val varT: typ |
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
57 |
val tidT: typ |
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
58 |
val tvarT: typ |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
59 |
val terminals: string list |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
60 |
val is_terminal: string -> bool |
18 | 61 |
val str_of_token: token -> string |
62 |
val display_token: token -> string |
|
63 |
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
|
64 |
val token_assoc: (token option * 'a list) list * token -> 'a list |
18 | 65 |
val valued_token: token -> bool |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
66 |
val predef_term: string -> token option |
4703 | 67 |
val tokenize: Scan.lexicon -> bool -> string list -> token list |
4247
9bba9251bb4d
added implode_xstr: string list -> string, explode_xstr: string -> string list;
wenzelm
parents:
3828
diff
changeset
|
68 |
end; |
0 | 69 |
|
14679 | 70 |
structure Lexicon: LEXICON = |
0 | 71 |
struct |
72 |
||
4247
9bba9251bb4d
added implode_xstr: string list -> string, explode_xstr: string -> string list;
wenzelm
parents:
3828
diff
changeset
|
73 |
|
18 | 74 |
(** is_identifier etc. **) |
75 |
||
76 |
fun is_ident [] = false |
|
4703 | 77 |
| is_ident (c :: cs) = Symbol.is_letter c andalso forall Symbol.is_letdig cs; |
18 | 78 |
|
4703 | 79 |
val is_identifier = is_ident o Symbol.explode; |
18 | 80 |
|
14679 | 81 |
fun is_ascii_identifier s = |
82 |
let val cs = Symbol.explode s |
|
83 |
in forall Symbol.is_ascii cs andalso is_ident cs end; |
|
84 |
||
18 | 85 |
fun is_xid s = |
4703 | 86 |
(case Symbol.explode s of |
18 | 87 |
"_" :: cs => is_ident cs |
88 |
| cs => is_ident cs); |
|
89 |
||
330
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
clasohm
parents:
237
diff
changeset
|
90 |
fun is_tid s = |
4703 | 91 |
(case Symbol.explode s of |
18 | 92 |
"'" :: cs => is_ident cs |
93 |
| _ => false); |
|
94 |
||
0 | 95 |
|
96 |
||
4703 | 97 |
(** basic scanners **) |
98 |
||
99 |
val scan_letter_letdigs = Scan.one Symbol.is_letter -- Scan.any Symbol.is_letdig >> op ::; |
|
100 |
val scan_digits1 = Scan.any1 Symbol.is_digit; |
|
101 |
||
102 |
val scan_id = scan_letter_letdigs >> implode; |
|
103 |
val scan_longid = scan_id ^^ (Scan.repeat1 ($$ "." ^^ scan_id) >> implode); |
|
104 |
val scan_tid = $$ "'" ^^ scan_id; |
|
105 |
||
106 |
val scan_nat = scan_digits1 >> implode; |
|
5513 | 107 |
val scan_int = $$ "-" ^^ scan_nat || scan_nat; |
4703 | 108 |
|
109 |
val scan_id_nat = scan_id ^^ Scan.optional ($$ "." ^^ scan_nat) ""; |
|
110 |
val scan_var = $$ "?" ^^ scan_id_nat; |
|
4902 | 111 |
val scan_tvar = $$ "?" ^^ $$ "'" ^^ scan_id_nat; |
4703 | 112 |
|
113 |
||
114 |
||
18 | 115 |
(** string_of_vname **) |
0 | 116 |
|
18 | 117 |
fun string_of_vname (x, i) = |
118 |
let |
|
119 |
val si = string_of_int i; |
|
6962 | 120 |
val dot = if_none (try (Symbol.is_digit o last_elem o Symbol.explode) x) true; |
18 | 121 |
in |
4703 | 122 |
if dot then "?" ^ x ^ "." ^ si |
123 |
else if i = 0 then "?" ^ x |
|
124 |
else "?" ^ x ^ si |
|
18 | 125 |
end; |
0 | 126 |
|
4703 | 127 |
fun string_of_vname' (x, ~1) = x |
128 |
| string_of_vname' xi = string_of_vname xi; |
|
2583
690835a06cf2
added string_of_vname' (treats neg. index as free);
wenzelm
parents:
2363
diff
changeset
|
129 |
|
18 | 130 |
|
0 | 131 |
|
18 | 132 |
(** datatype token **) |
0 | 133 |
|
18 | 134 |
datatype token = |
135 |
Token of string | |
|
136 |
IdentSy of string | |
|
3828 | 137 |
LongIdentSy of string | |
18 | 138 |
VarSy of string | |
139 |
TFreeSy of string | |
|
140 |
TVarSy of string | |
|
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
141 |
NumSy of string | |
11697 | 142 |
XNumSy of string | |
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
143 |
StrSy of string | |
18 | 144 |
EndToken; |
0 | 145 |
|
146 |
||
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
147 |
(* terminal arguments *) |
0 | 148 |
|
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
149 |
val idT = Type ("id", []); |
3828 | 150 |
val longidT = Type ("longid", []); |
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
151 |
val varT = Type ("var", []); |
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
152 |
val tidT = Type ("tid", []); |
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
153 |
val tvarT = Type ("tvar", []); |
0 | 154 |
|
11697 | 155 |
val terminals = ["id", "longid", "var", "tid", "tvar", "num", "xnum", "xstr"]; |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
156 |
|
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
157 |
fun is_terminal s = s mem terminals; |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
158 |
|
0 | 159 |
|
18 | 160 |
(* str_of_token *) |
0 | 161 |
|
18 | 162 |
fun str_of_token (Token s) = s |
163 |
| str_of_token (IdentSy s) = s |
|
3828 | 164 |
| str_of_token (LongIdentSy s) = s |
18 | 165 |
| str_of_token (VarSy s) = s |
166 |
| str_of_token (TFreeSy s) = s |
|
167 |
| str_of_token (TVarSy s) = s |
|
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
168 |
| str_of_token (NumSy s) = s |
11697 | 169 |
| str_of_token (XNumSy s) = s |
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
170 |
| str_of_token (StrSy s) = s |
376
d3d01131470f
extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents:
330
diff
changeset
|
171 |
| str_of_token EndToken = "EOF"; |
0 | 172 |
|
18 | 173 |
|
174 |
(* display_token *) |
|
0 | 175 |
|
18 | 176 |
fun display_token (Token s) = quote s |
177 |
| display_token (IdentSy s) = "id(" ^ s ^ ")" |
|
3828 | 178 |
| display_token (LongIdentSy s) = "longid(" ^ s ^ ")" |
18 | 179 |
| 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
|
180 |
| display_token (TFreeSy s) = "tid(" ^ s ^ ")" |
18 | 181 |
| display_token (TVarSy s) = "tvar(" ^ s ^ ")" |
11697 | 182 |
| display_token (NumSy s) = "num(" ^ s ^ ")" |
183 |
| display_token (XNumSy s) = "xnum(" ^ s ^ ")" |
|
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
184 |
| display_token (StrSy s) = "xstr(" ^ s ^ ")" |
18 | 185 |
| display_token EndToken = ""; |
0 | 186 |
|
18 | 187 |
|
188 |
(* matching_tokens *) |
|
0 | 189 |
|
18 | 190 |
fun matching_tokens (Token x, Token y) = (x = y) |
191 |
| matching_tokens (IdentSy _, IdentSy _) = true |
|
3828 | 192 |
| matching_tokens (LongIdentSy _, LongIdentSy _) = true |
18 | 193 |
| matching_tokens (VarSy _, VarSy _) = true |
194 |
| matching_tokens (TFreeSy _, TFreeSy _) = true |
|
195 |
| matching_tokens (TVarSy _, TVarSy _) = true |
|
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
196 |
| matching_tokens (NumSy _, NumSy _) = true |
11697 | 197 |
| matching_tokens (XNumSy _, XNumSy _) = true |
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
198 |
| matching_tokens (StrSy _, StrSy _) = true |
18 | 199 |
| matching_tokens (EndToken, EndToken) = true |
200 |
| matching_tokens _ = false; |
|
0 | 201 |
|
202 |
||
376
d3d01131470f
extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents:
330
diff
changeset
|
203 |
(* token_assoc *) |
d3d01131470f
extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents:
330
diff
changeset
|
204 |
|
330
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
clasohm
parents:
237
diff
changeset
|
205 |
fun token_assoc (list, key) = |
376
d3d01131470f
extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents:
330
diff
changeset
|
206 |
let |
d3d01131470f
extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents:
330
diff
changeset
|
207 |
fun assoc [] = [] |
d3d01131470f
extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents:
330
diff
changeset
|
208 |
| assoc ((keyi, xi) :: pairs) = |
d3d01131470f
extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents:
330
diff
changeset
|
209 |
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
|
210 |
assoc pairs @ xi |
d3d01131470f
extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents:
330
diff
changeset
|
211 |
else assoc pairs; |
330
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
clasohm
parents:
237
diff
changeset
|
212 |
in assoc list end; |
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
clasohm
parents:
237
diff
changeset
|
213 |
|
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
clasohm
parents:
237
diff
changeset
|
214 |
|
18 | 215 |
(* valued_token *) |
0 | 216 |
|
18 | 217 |
fun valued_token (Token _) = false |
218 |
| valued_token (IdentSy _) = true |
|
3828 | 219 |
| valued_token (LongIdentSy _) = true |
18 | 220 |
| valued_token (VarSy _) = true |
221 |
| valued_token (TFreeSy _) = true |
|
222 |
| valued_token (TVarSy _) = true |
|
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
223 |
| valued_token (NumSy _) = true |
11697 | 224 |
| valued_token (XNumSy _) = true |
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
225 |
| valued_token (StrSy _) = true |
18 | 226 |
| valued_token EndToken = false; |
0 | 227 |
|
228 |
||
18 | 229 |
(* predef_term *) |
0 | 230 |
|
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
231 |
fun predef_term "id" = Some (IdentSy "id") |
3828 | 232 |
| predef_term "longid" = Some (LongIdentSy "longid") |
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
233 |
| predef_term "var" = Some (VarSy "var") |
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
234 |
| predef_term "tid" = Some (TFreeSy "tid") |
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
235 |
| predef_term "tvar" = Some (TVarSy "tvar") |
11697 | 236 |
| predef_term "num" = Some (NumSy "num") |
237 |
| predef_term "xnum" = Some (XNumSy "xnum") |
|
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
238 |
| predef_term "xstr" = Some (StrSy "xstr") |
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
239 |
| predef_term _ = None; |
0 | 240 |
|
241 |
||
13808 | 242 |
fun scan_enclosed(a1,a2,z1,z2,kind) = |
243 |
let val scan_chr = |
|
244 |
$$ "\\" |-- Scan.one Symbol.not_eof || |
|
245 |
Scan.one (not_equal z1 andf Symbol.not_eof) || |
|
246 |
$$ z1 --| Scan.ahead (Scan.one (not_equal z2)); |
|
247 |
in $$ a1 |-- $$ a2 |-- |
|
248 |
!! (fn (cs, _) => "Inner lexical error: missing end of " ^ kind ^ " at " ^ |
|
249 |
quote (a1 ^ a2 ^ Symbol.beginning cs)) |
|
250 |
(Scan.repeat scan_chr --| $$ z1 --| $$ z2) |
|
251 |
end; |
|
252 |
||
4703 | 253 |
(* xstr tokens *) |
18 | 254 |
|
13808 | 255 |
val scan_str = scan_enclosed("'","'","'","'","string") |
0 | 256 |
|
4703 | 257 |
fun implode_xstr cs = enclose "''" "''" (implode (map (fn "'" => "\\'" | c => c) cs)); |
18 | 258 |
|
4703 | 259 |
fun explode_xstr str = |
5868 | 260 |
(case Scan.read Symbol.stopper scan_str (Symbol.explode str) of |
261 |
Some cs => cs |
|
262 |
| _ => error ("Inner lexical error: literal string expected at " ^ quote str)); |
|
18 | 263 |
|
264 |
||
13808 | 265 |
val scan_comment = scan_enclosed("(","*","*",")","comment") |
18 | 266 |
|
267 |
(** tokenize **) |
|
268 |
||
2363 | 269 |
fun tokenize lex xids chs = |
18 | 270 |
let |
271 |
val scan_xid = |
|
272 |
if xids then $$ "_" ^^ scan_id || scan_id |
|
273 |
else scan_id; |
|
274 |
||
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
275 |
val scan_val = |
4902 | 276 |
scan_tvar >> pair TVarSy || |
4703 | 277 |
scan_var >> pair VarSy || |
278 |
scan_tid >> pair TFreeSy || |
|
11697 | 279 |
scan_int >> pair NumSy || |
280 |
$$ "#" ^^ scan_int >> pair XNumSy || |
|
3828 | 281 |
scan_longid >> pair LongIdentSy || |
18 | 282 |
scan_xid >> pair IdentSy; |
283 |
||
4703 | 284 |
val scan_lit = Scan.literal lex >> (pair Token o implode); |
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
285 |
|
4703 | 286 |
val scan_token = |
13808 | 287 |
scan_comment >> K None || |
4703 | 288 |
Scan.max (op <= o pairself snd) scan_lit scan_val >> (fn (tk, s) => Some (tk s)) || |
289 |
scan_str >> (Some o StrSy o implode_xstr) || |
|
290 |
Scan.one Symbol.is_blank >> K None; |
|
18 | 291 |
in |
4938 | 292 |
(case Scan.error (Scan.finite Symbol.stopper (Scan.repeat scan_token)) chs of |
4703 | 293 |
(toks, []) => mapfilter I toks @ [EndToken] |
294 |
| (_, cs) => error ("Inner lexical error at: " ^ quote (implode cs))) |
|
18 | 295 |
end; |
296 |
||
297 |
||
298 |
||
299 |
(** scan variables **) |
|
300 |
||
301 |
(* scan_vname *) |
|
302 |
||
303 |
fun scan_vname chrs = |
|
304 |
let |
|
305 |
fun nat_of_chs n [] = n |
|
306 |
| nat_of_chs n (c :: cs) = nat_of_chs (n * 10 + (ord c - ord "0")) cs; |
|
307 |
||
4703 | 308 |
val nat = nat_of_chs 0; |
18 | 309 |
|
310 |
fun split_vname chs = |
|
4703 | 311 |
let val (cs, ds) = take_suffix Symbol.is_digit chs |
312 |
in (implode cs, nat ds) end |
|
18 | 313 |
|
314 |
val scan = |
|
4703 | 315 |
scan_letter_letdigs -- Scan.optional ($$ "." |-- scan_digits1 >> nat) ~1; |
18 | 316 |
in |
317 |
(case scan chrs of |
|
318 |
((cs, ~1), cs') => (split_vname cs, cs') |
|
319 |
| ((cs, i), cs') => ((implode cs, i), cs')) |
|
320 |
end; |
|
321 |
||
322 |
||
4703 | 323 |
(* indexname *) |
18 | 324 |
|
4703 | 325 |
fun indexname cs = |
5868 | 326 |
(case Scan.read Symbol.stopper scan_vname cs of |
327 |
Some xi => xi |
|
4703 | 328 |
| _ => error ("Lexical error in variable name: " ^ quote (implode cs))); |
18 | 329 |
|
330 |
||
4703 | 331 |
(* read_var *) |
18 | 332 |
|
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
333 |
fun const c = Const (c, dummyT); |
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
334 |
fun free x = Free (x, dummyT); |
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
335 |
fun var xi = Var (xi, dummyT); |
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
336 |
|
4703 | 337 |
fun read_var str = |
18 | 338 |
let |
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
339 |
fun tvar (x, i) = var ("'" ^ x, i); |
18 | 340 |
|
341 |
val scan = |
|
4703 | 342 |
$$ "?" |-- $$ "'" |-- scan_vname >> tvar || |
343 |
$$ "?" |-- scan_vname >> var || |
|
344 |
Scan.any Symbol.not_eof >> (free o implode); |
|
5868 | 345 |
in the (Scan.read Symbol.stopper scan (Symbol.explode str)) end; |
4587 | 346 |
|
347 |
||
5260 | 348 |
(* variable kinds *) |
349 |
||
9289 | 350 |
val internal = suffix "_"; |
351 |
val dest_internal = unsuffix "_"; |
|
352 |
||
5286 | 353 |
val skolem = suffix "__"; |
354 |
val dest_skolem = unsuffix "__"; |
|
5260 | 355 |
|
356 |
||
5860 | 357 |
(* read_nat *) |
358 |
||
359 |
fun read_nat str = |
|
5868 | 360 |
apsome (#1 o Term.read_int) (Scan.read Symbol.stopper scan_digits1 (Symbol.explode str)); |
5860 | 361 |
|
362 |
||
9326 | 363 |
(* read_xnum *) |
364 |
||
365 |
fun read_xnum str = |
|
366 |
let |
|
367 |
val (sign, digs) = |
|
368 |
(case Symbol.explode str of |
|
369 |
"#" :: "-" :: cs => (~1, cs) |
|
370 |
| "#" :: cs => (1, cs) |
|
11697 | 371 |
| "-" :: cs => (~1, cs) |
372 |
| cs => (1, cs)); |
|
9326 | 373 |
in sign * #1 (Term.read_int digs) end; |
374 |
||
375 |
||
7784 | 376 |
(* read_ident(s) *) |
377 |
||
378 |
fun read_idents str = |
|
379 |
let |
|
380 |
val blanks = Scan.any Symbol.is_blank; |
|
381 |
val junk = Scan.any Symbol.not_eof; |
|
382 |
val idents = Scan.repeat1 (blanks |-- scan_id --| blanks) -- junk; |
|
383 |
in |
|
384 |
(case Scan.read Symbol.stopper idents (Symbol.explode str) of |
|
385 |
Some (ids, []) => ids |
|
386 |
| Some (_, bad) => error ("Bad identifier: " ^ quote (implode bad)) |
|
387 |
| None => error ("No identifier found in: " ^ quote str)) |
|
388 |
end; |
|
389 |
||
390 |
||
0 | 391 |
end; |