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