author | wenzelm |
Sun, 21 Feb 2010 22:35:02 +0100 | |
changeset 35262 | 9ea4445d2ccf |
parent 30573 | 49899f26fbd1 |
child 35428 | bd7d6f65976e |
permissions | -rw-r--r-- |
18 | 1 |
(* Title: Pure/Syntax/lexicon.ML |
2 |
Author: Tobias Nipkow and Markus Wenzel, TU Muenchen |
|
0 | 3 |
|
4703 | 4 |
Lexer for the inner Isabelle syntax (terms and types). |
18 | 5 |
*) |
0 | 6 |
|
7 |
signature LEXICON0 = |
|
4247
9bba9251bb4d
added implode_xstr: string list -> string, explode_xstr: string -> string list;
wenzelm
parents:
3828
diff
changeset
|
8 |
sig |
0 | 9 |
val is_identifier: string -> bool |
14679 | 10 |
val is_ascii_identifier: string -> bool |
20165 | 11 |
val is_tid: string -> bool |
30573 | 12 |
val scan_id: Symbol_Pos.T list -> Symbol_Pos.T list * Symbol_Pos.T list |
13 |
val scan_longid: Symbol_Pos.T list -> Symbol_Pos.T list * Symbol_Pos.T list |
|
14 |
val scan_tid: Symbol_Pos.T list -> Symbol_Pos.T list * Symbol_Pos.T list |
|
15 |
val scan_nat: Symbol_Pos.T list -> Symbol_Pos.T list * Symbol_Pos.T list |
|
16 |
val scan_int: Symbol_Pos.T list -> Symbol_Pos.T list * Symbol_Pos.T list |
|
17 |
val scan_hex: Symbol_Pos.T list -> Symbol_Pos.T list * Symbol_Pos.T list |
|
18 |
val scan_bin: Symbol_Pos.T list -> Symbol_Pos.T list * Symbol_Pos.T list |
|
19 |
val scan_var: Symbol_Pos.T list -> Symbol_Pos.T list * Symbol_Pos.T list |
|
20 |
val scan_tvar: Symbol_Pos.T list -> Symbol_Pos.T list * Symbol_Pos.T list |
|
4247
9bba9251bb4d
added implode_xstr: string list -> string, explode_xstr: string -> string list;
wenzelm
parents:
3828
diff
changeset
|
21 |
val implode_xstr: string list -> string |
9bba9251bb4d
added implode_xstr: string list -> string, explode_xstr: string -> string list;
wenzelm
parents:
3828
diff
changeset
|
22 |
val explode_xstr: string -> string list |
20313
bf9101cc4385
renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents:
20165
diff
changeset
|
23 |
val read_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 |
20313
bf9101cc4385
renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents:
20165
diff
changeset
|
30 |
val read_int: string -> int option |
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
24583
diff
changeset
|
31 |
val read_xnum: string -> {radix: int, leading_zeros: int, value: int} |
28904 | 32 |
val read_float: string -> {mant: int, exp: int} |
19002 | 33 |
val fixedN: string |
35262
9ea4445d2ccf
slightly more abstract syntax mark/unmark operations;
wenzelm
parents:
30573
diff
changeset
|
34 |
val mark_fixed: string -> string |
9ea4445d2ccf
slightly more abstract syntax mark/unmark operations;
wenzelm
parents:
30573
diff
changeset
|
35 |
val unmark_fixed: string -> string |
19002 | 36 |
val constN: string |
35262
9ea4445d2ccf
slightly more abstract syntax mark/unmark operations;
wenzelm
parents:
30573
diff
changeset
|
37 |
val mark_const: string -> string |
9ea4445d2ccf
slightly more abstract syntax mark/unmark operations;
wenzelm
parents:
30573
diff
changeset
|
38 |
val unmark_const: string -> string |
4247
9bba9251bb4d
added implode_xstr: string list -> string, explode_xstr: string -> string list;
wenzelm
parents:
3828
diff
changeset
|
39 |
end; |
0 | 40 |
|
41 |
signature LEXICON = |
|
4247
9bba9251bb4d
added implode_xstr: string list -> string, explode_xstr: string -> string list;
wenzelm
parents:
3828
diff
changeset
|
42 |
sig |
18 | 43 |
include LEXICON0 |
44 |
val is_xid: string -> bool |
|
27800
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
45 |
datatype token_kind = |
27887 | 46 |
Literal | IdentSy | LongIdentSy | VarSy | TFreeSy | TVarSy | |
28904 | 47 |
NumSy | FloatSy | XNumSy | StrSy | Space | Comment | EOF |
27800
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
48 |
datatype token = Token of token_kind * string * Position.range |
27806 | 49 |
val str_of_token: token -> string |
50 |
val pos_of_token: token -> Position.T |
|
27887 | 51 |
val is_proper: token -> bool |
27800
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
52 |
val mk_eof: Position.T -> token |
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
53 |
val eof: token |
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
54 |
val is_eof: token -> bool |
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
55 |
val stopper: token Scan.stopper |
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
56 |
val idT: typ |
3828 | 57 |
val longidT: typ |
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
58 |
val varT: typ |
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
59 |
val tidT: typ |
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
60 |
val tvarT: typ |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
61 |
val terminals: string list |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
62 |
val is_terminal: string -> bool |
28413
ee73353fb87c
report_token/parse_token: back to context-less version;
wenzelm
parents:
28407
diff
changeset
|
63 |
val report_token: token -> unit |
18 | 64 |
val matching_tokens: token * token -> bool |
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 |
30573 | 67 |
val tokenize: Scan.lexicon -> bool -> Symbol_Pos.T 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 |
||
18 | 73 |
(** is_identifier etc. **) |
74 |
||
16150 | 75 |
val is_identifier = Symbol.is_ident o Symbol.explode; |
18 | 76 |
|
14679 | 77 |
fun is_ascii_identifier s = |
78 |
let val cs = Symbol.explode s |
|
16150 | 79 |
in forall Symbol.is_ascii cs andalso Symbol.is_ident cs end; |
14679 | 80 |
|
18 | 81 |
fun is_xid s = |
4703 | 82 |
(case Symbol.explode s of |
16150 | 83 |
"_" :: cs => Symbol.is_ident cs |
84 |
| cs => Symbol.is_ident cs); |
|
18 | 85 |
|
330
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
clasohm
parents:
237
diff
changeset
|
86 |
fun is_tid s = |
4703 | 87 |
(case Symbol.explode s of |
16150 | 88 |
"'" :: cs => Symbol.is_ident cs |
18 | 89 |
| _ => false); |
90 |
||
0 | 91 |
|
92 |
||
4703 | 93 |
(** basic scanners **) |
94 |
||
30573 | 95 |
open Basic_Symbol_Pos; |
27773 | 96 |
|
30573 | 97 |
fun !!! msg = Symbol_Pos.!!! ("Inner lexical error: " ^ msg); |
27773 | 98 |
|
99 |
val scan_id = Scan.one (Symbol.is_letter o symbol) ::: Scan.many (Symbol.is_letdig o symbol); |
|
100 |
val scan_longid = scan_id @@@ (Scan.repeat1 ($$$ "." @@@ scan_id) >> flat); |
|
101 |
val scan_tid = $$$ "'" @@@ scan_id; |
|
4703 | 102 |
|
27773 | 103 |
val scan_nat = Scan.many1 (Symbol.is_digit o symbol); |
104 |
val scan_int = $$$ "-" @@@ scan_nat || scan_nat; |
|
28904 | 105 |
val scan_natdot = scan_nat @@@ $$$ "." @@@ scan_nat; |
106 |
val scan_float = $$$ "-" @@@ scan_natdot || scan_natdot; |
|
27773 | 107 |
val scan_hex = $$$ "0" @@@ $$$ "x" @@@ Scan.many1 (Symbol.is_ascii_hex o symbol); |
108 |
val scan_bin = $$$ "0" @@@ $$$ "b" @@@ Scan.many1 (fn (s, _) => s = "0" orelse s = "1"); |
|
4703 | 109 |
|
27773 | 110 |
val scan_id_nat = scan_id @@@ Scan.optional ($$$ "." @@@ scan_nat) []; |
111 |
val scan_var = $$$ "?" @@@ scan_id_nat; |
|
112 |
val scan_tvar = $$$ "?" @@@ $$$ "'" @@@ scan_id_nat; |
|
4703 | 113 |
|
114 |
||
115 |
||
18 | 116 |
(** datatype token **) |
0 | 117 |
|
27800
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
118 |
datatype token_kind = |
27887 | 119 |
Literal | IdentSy | LongIdentSy | VarSy | TFreeSy | TVarSy | |
28904 | 120 |
NumSy | FloatSy | XNumSy | StrSy | Space | Comment | EOF; |
27800
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
121 |
|
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
122 |
datatype token = Token of token_kind * string * Position.range; |
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
123 |
|
27806 | 124 |
fun str_of_token (Token (_, s, _)) = s; |
125 |
fun pos_of_token (Token (_, _, (pos, _))) = pos; |
|
126 |
||
27887 | 127 |
fun is_proper (Token (Space, _, _)) = false |
128 |
| is_proper (Token (Comment, _, _)) = false |
|
129 |
| is_proper _ = true; |
|
130 |
||
27800
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
131 |
|
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
132 |
(* stopper *) |
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
133 |
|
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
134 |
fun mk_eof pos = Token (EOF, "", (pos, Position.none)); |
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
135 |
val eof = mk_eof Position.none; |
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
136 |
|
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
137 |
fun is_eof (Token (EOF, _, _)) = true |
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
138 |
| is_eof _ = false; |
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
139 |
|
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
140 |
val stopper = Scan.stopper (K eof) is_eof; |
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
141 |
|
0 | 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 |
|
29156
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
151 |
val terminal_kinds = |
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
152 |
[("id", IdentSy), |
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
153 |
("longid", LongIdentSy), |
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
154 |
("var", VarSy), |
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
155 |
("tid", TFreeSy), |
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
156 |
("tvar", TVarSy), |
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
157 |
("num", NumSy), |
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
158 |
("float_token", FloatSy), |
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
159 |
("xnum", XNumSy), |
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
160 |
("xstr", StrSy)]; |
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
161 |
|
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
162 |
val terminals = map #1 terminal_kinds; |
20664 | 163 |
val is_terminal = member (op =) terminals; |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
164 |
|
0 | 165 |
|
27887 | 166 |
(* markup *) |
167 |
||
168 |
val token_kind_markup = |
|
169 |
fn Literal => Markup.literal |
|
170 |
| IdentSy => Markup.ident |
|
171 |
| LongIdentSy => Markup.ident |
|
172 |
| VarSy => Markup.var |
|
173 |
| TFreeSy => Markup.tfree |
|
174 |
| TVarSy => Markup.tvar |
|
29318
6337d1cb2ba0
added numeral, which supercedes num, xnum, float;
wenzelm
parents:
29156
diff
changeset
|
175 |
| NumSy => Markup.numeral |
6337d1cb2ba0
added numeral, which supercedes num, xnum, float;
wenzelm
parents:
29156
diff
changeset
|
176 |
| FloatSy => Markup.numeral |
6337d1cb2ba0
added numeral, which supercedes num, xnum, float;
wenzelm
parents:
29156
diff
changeset
|
177 |
| XNumSy => Markup.numeral |
6337d1cb2ba0
added numeral, which supercedes num, xnum, float;
wenzelm
parents:
29156
diff
changeset
|
178 |
| StrSy => Markup.inner_string |
27887 | 179 |
| Space => Markup.none |
180 |
| Comment => Markup.inner_comment |
|
181 |
| EOF => Markup.none; |
|
182 |
||
28413
ee73353fb87c
report_token/parse_token: back to context-less version;
wenzelm
parents:
28407
diff
changeset
|
183 |
fun report_token (Token (kind, _, (pos, _))) = |
ee73353fb87c
report_token/parse_token: back to context-less version;
wenzelm
parents:
28407
diff
changeset
|
184 |
Position.report (token_kind_markup kind) pos; |
27887 | 185 |
|
186 |
||
18 | 187 |
(* matching_tokens *) |
0 | 188 |
|
27800
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
189 |
fun matching_tokens (Token (Literal, x, _), Token (Literal, y, _)) = x = y |
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
190 |
| matching_tokens (Token (k, _, _), Token (k', _, _)) = k = k'; |
0 | 191 |
|
192 |
||
18 | 193 |
(* valued_token *) |
0 | 194 |
|
27800
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
195 |
fun valued_token (Token (Literal, _, _)) = false |
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
196 |
| valued_token (Token (EOF, _, _)) = false |
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
197 |
| valued_token _ = true; |
0 | 198 |
|
199 |
||
18 | 200 |
(* predef_term *) |
0 | 201 |
|
29156
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
202 |
fun predef_term s = |
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
203 |
(case AList.lookup (op =) terminal_kinds s of |
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
204 |
SOME sy => SOME (Token (sy, s, Position.no_range)) |
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
205 |
| NONE => NONE); |
0 | 206 |
|
207 |
||
4703 | 208 |
(* xstr tokens *) |
18 | 209 |
|
14730 | 210 |
val scan_chr = |
27773 | 211 |
$$$ "\\" |-- $$$ "'" || |
212 |
Scan.one ((fn s => s <> "\\" andalso s <> "'" andalso Symbol.is_regular s) o symbol) >> single || |
|
213 |
$$$ "'" --| Scan.ahead (~$$$ "'"); |
|
14730 | 214 |
|
215 |
val scan_str = |
|
27800
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
216 |
$$$ "'" @@@ $$$ "'" @@@ !!! "missing end of string" |
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
217 |
((Scan.repeat scan_chr >> flat) @@@ $$$ "'" @@@ $$$ "'"); |
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
218 |
|
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
219 |
val scan_str_body = |
27773 | 220 |
$$$ "'" |-- $$$ "'" |-- !!! "missing end of string" |
221 |
((Scan.repeat scan_chr >> flat) --| $$$ "'" --| $$$ "'"); |
|
14730 | 222 |
|
0 | 223 |
|
4703 | 224 |
fun implode_xstr cs = enclose "''" "''" (implode (map (fn "'" => "\\'" | c => c) cs)); |
18 | 225 |
|
4703 | 226 |
fun explode_xstr str = |
30573 | 227 |
(case Scan.read Symbol_Pos.stopper scan_str_body (Symbol_Pos.explode (str, Position.none)) of |
27773 | 228 |
SOME cs => map symbol cs |
5868 | 229 |
| _ => error ("Inner lexical error: literal string expected at " ^ quote str)); |
18 | 230 |
|
231 |
||
27806 | 232 |
|
18 | 233 |
(** tokenize **) |
234 |
||
27800
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
235 |
fun token_leq (Token (_, s1, _), Token (_, s2, _)) = s1 <= s2; |
30573 | 236 |
fun token kind ss = Token (kind, Symbol_Pos.content ss, Symbol_Pos.range ss); |
27800
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
237 |
|
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
238 |
fun tokenize lex xids syms = |
18 | 239 |
let |
240 |
val scan_xid = |
|
27773 | 241 |
if xids then $$$ "_" @@@ scan_id || scan_id |
18 | 242 |
else scan_id; |
243 |
||
20096 | 244 |
val scan_num = scan_hex || scan_bin || scan_int; |
245 |
||
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
246 |
val scan_val = |
26007
3760d3ff4cce
basic scanners: produce symbol list instead of imploded string;
wenzelm
parents:
24630
diff
changeset
|
247 |
scan_tvar >> token TVarSy || |
3760d3ff4cce
basic scanners: produce symbol list instead of imploded string;
wenzelm
parents:
24630
diff
changeset
|
248 |
scan_var >> token VarSy || |
3760d3ff4cce
basic scanners: produce symbol list instead of imploded string;
wenzelm
parents:
24630
diff
changeset
|
249 |
scan_tid >> token TFreeSy || |
28904 | 250 |
scan_float >> token FloatSy || |
26007
3760d3ff4cce
basic scanners: produce symbol list instead of imploded string;
wenzelm
parents:
24630
diff
changeset
|
251 |
scan_num >> token NumSy || |
27773 | 252 |
$$$ "#" @@@ scan_num >> token XNumSy || |
26007
3760d3ff4cce
basic scanners: produce symbol list instead of imploded string;
wenzelm
parents:
24630
diff
changeset
|
253 |
scan_longid >> token LongIdentSy || |
3760d3ff4cce
basic scanners: produce symbol list instead of imploded string;
wenzelm
parents:
24630
diff
changeset
|
254 |
scan_xid >> token IdentSy; |
18 | 255 |
|
27800
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
256 |
val scan_lit = Scan.literal lex >> token Literal; |
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
257 |
|
4703 | 258 |
val scan_token = |
30573 | 259 |
Symbol_Pos.scan_comment !!! >> token Comment || |
27887 | 260 |
Scan.max token_leq scan_lit scan_val || |
261 |
scan_str >> token StrSy || |
|
262 |
Scan.many1 (Symbol.is_blank o symbol) >> token Space; |
|
18 | 263 |
in |
27773 | 264 |
(case Scan.error |
30573 | 265 |
(Scan.finite Symbol_Pos.stopper (Scan.repeat scan_token)) syms of |
27887 | 266 |
(toks, []) => toks |
30573 | 267 |
| (_, ss) => error ("Inner lexical error at: " ^ Symbol_Pos.content ss ^ |
268 |
Position.str_of (#1 (Symbol_Pos.range ss)))) |
|
18 | 269 |
end; |
270 |
||
271 |
||
272 |
||
273 |
(** scan variables **) |
|
274 |
||
15991
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
275 |
(* scan_indexname *) |
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
276 |
|
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
277 |
local |
18 | 278 |
|
27773 | 279 |
val scan_vname = |
18 | 280 |
let |
15991
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
281 |
fun nat n [] = n |
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
282 |
| nat n (c :: cs) = nat (n * 10 + (ord c - ord "0")) cs; |
18 | 283 |
|
15991
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
284 |
fun idxname cs ds = (implode (rev cs), nat 0 ds); |
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
285 |
fun chop_idx [] ds = idxname [] ds |
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
286 |
| chop_idx (cs as (_ :: "\\<^isub>" :: _)) ds = idxname cs ds |
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
287 |
| chop_idx (cs as (_ :: "\\<^isup>" :: _)) ds = idxname cs ds |
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
288 |
| chop_idx (c :: cs) ds = |
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
289 |
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
|
290 |
else idxname (c :: cs) ds; |
18 | 291 |
|
27773 | 292 |
val scan = (scan_id >> map symbol) -- |
293 |
Scan.optional ($$$ "." |-- scan_nat >> (nat 0 o map symbol)) ~1; |
|
18 | 294 |
in |
27773 | 295 |
scan >> |
296 |
(fn (cs, ~1) => chop_idx (rev cs) [] |
|
297 |
| (cs, i) => (implode cs, i)) |
|
18 | 298 |
end; |
299 |
||
15991
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
300 |
in |
18 | 301 |
|
27773 | 302 |
val scan_indexname = $$$ "'" |-- scan_vname >> (fn (x, i) => ("'" ^ x, i)) || scan_vname; |
15443
07f78cc82a73
indexname function now parses type variables as well; changed input
berghofe
parents:
14981
diff
changeset
|
303 |
|
15991
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
304 |
end; |
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
305 |
|
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
306 |
|
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
307 |
(* indexname *) |
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
308 |
|
20313
bf9101cc4385
renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents:
20165
diff
changeset
|
309 |
fun read_indexname s = |
30573 | 310 |
(case Scan.read Symbol_Pos.stopper scan_indexname (Symbol_Pos.explode (s, Position.none)) of |
15531 | 311 |
SOME xi => xi |
20313
bf9101cc4385
renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents:
20165
diff
changeset
|
312 |
| _ => error ("Lexical error in variable name: " ^ quote s)); |
18 | 313 |
|
314 |
||
4703 | 315 |
(* read_var *) |
18 | 316 |
|
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
317 |
fun const c = Const (c, dummyT); |
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
318 |
fun free x = Free (x, dummyT); |
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
319 |
fun var xi = Var (xi, dummyT); |
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
320 |
|
4703 | 321 |
fun read_var str = |
18 | 322 |
let |
323 |
val scan = |
|
30573 | 324 |
$$$ "?" |-- scan_indexname --| Scan.ahead (Scan.one Symbol_Pos.is_eof) >> var || |
27773 | 325 |
Scan.many (Symbol.is_regular o symbol) >> (free o implode o map symbol); |
30573 | 326 |
in the (Scan.read Symbol_Pos.stopper scan (Symbol_Pos.explode (str, Position.none))) end; |
15991
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
327 |
|
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
328 |
|
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
329 |
(* read_variable *) |
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
330 |
|
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
331 |
fun read_variable str = |
27773 | 332 |
let val scan = $$$ "?" |-- scan_indexname || scan_indexname |
30573 | 333 |
in Scan.read Symbol_Pos.stopper scan (Symbol_Pos.explode (str, Position.none)) end; |
4587 | 334 |
|
335 |
||
19002 | 336 |
(* specific identifiers *) |
5260 | 337 |
|
35262
9ea4445d2ccf
slightly more abstract syntax mark/unmark operations;
wenzelm
parents:
30573
diff
changeset
|
338 |
val fixedN = "\\<^fixed>"; |
9ea4445d2ccf
slightly more abstract syntax mark/unmark operations;
wenzelm
parents:
30573
diff
changeset
|
339 |
val mark_fixed = prefix fixedN; |
9ea4445d2ccf
slightly more abstract syntax mark/unmark operations;
wenzelm
parents:
30573
diff
changeset
|
340 |
val unmark_fixed = unprefix fixedN; |
9ea4445d2ccf
slightly more abstract syntax mark/unmark operations;
wenzelm
parents:
30573
diff
changeset
|
341 |
|
19002 | 342 |
val constN = "\\<^const>"; |
35262
9ea4445d2ccf
slightly more abstract syntax mark/unmark operations;
wenzelm
parents:
30573
diff
changeset
|
343 |
val mark_const = prefix constN; |
9ea4445d2ccf
slightly more abstract syntax mark/unmark operations;
wenzelm
parents:
30573
diff
changeset
|
344 |
val unmark_const = unprefix constN; |
19002 | 345 |
|
5260 | 346 |
|
20313
bf9101cc4385
renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents:
20165
diff
changeset
|
347 |
(* read numbers *) |
bf9101cc4385
renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents:
20165
diff
changeset
|
348 |
|
bf9101cc4385
renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents:
20165
diff
changeset
|
349 |
local |
bf9101cc4385
renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents:
20165
diff
changeset
|
350 |
|
bf9101cc4385
renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents:
20165
diff
changeset
|
351 |
fun nat cs = |
27773 | 352 |
Option.map (#1 o Library.read_int o map symbol) |
30573 | 353 |
(Scan.read Symbol_Pos.stopper scan_nat cs); |
5860 | 354 |
|
20313
bf9101cc4385
renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents:
20165
diff
changeset
|
355 |
in |
bf9101cc4385
renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents:
20165
diff
changeset
|
356 |
|
30573 | 357 |
fun read_nat s = nat (Symbol_Pos.explode (s, Position.none)); |
20313
bf9101cc4385
renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents:
20165
diff
changeset
|
358 |
|
bf9101cc4385
renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents:
20165
diff
changeset
|
359 |
fun read_int s = |
30573 | 360 |
(case Symbol_Pos.explode (s, Position.none) of |
27773 | 361 |
("-", _) :: cs => Option.map ~ (nat cs) |
20313
bf9101cc4385
renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents:
20165
diff
changeset
|
362 |
| cs => nat cs); |
bf9101cc4385
renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents:
20165
diff
changeset
|
363 |
|
bf9101cc4385
renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents:
20165
diff
changeset
|
364 |
end; |
5860 | 365 |
|
366 |
||
20096 | 367 |
(* read_xnum: hex/bin/decimal *) |
9326 | 368 |
|
15991
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
369 |
local |
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
370 |
|
20096 | 371 |
val ten = ord "0" + 10; |
372 |
val a = ord "a"; |
|
373 |
val A = ord "A"; |
|
23802 | 374 |
val _ = a > A orelse sys_error "Bad ASCII"; |
20096 | 375 |
|
376 |
fun remap_hex c = |
|
377 |
let val x = ord c in |
|
378 |
if x >= a then chr (x - a + ten) |
|
379 |
else if x >= A then chr (x - A + ten) |
|
380 |
else c |
|
381 |
end; |
|
15991
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
382 |
|
21781 | 383 |
fun leading_zeros ["0"] = 0 |
384 |
| leading_zeros ("0" :: cs) = 1 + leading_zeros cs |
|
385 |
| leading_zeros _ = 0; |
|
386 |
||
15991
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
387 |
in |
15965
f422f8283491
Use of IntInf.int instead of int in most numeric simprocs; avoids
paulson
parents:
15570
diff
changeset
|
388 |
|
9326 | 389 |
fun read_xnum str = |
390 |
let |
|
20096 | 391 |
val (sign, radix, digs) = |
392 |
(case Symbol.explode (perhaps (try (unprefix "#")) str) of |
|
393 |
"0" :: "x" :: cs => (1, 16, map remap_hex cs) |
|
394 |
| "0" :: "b" :: cs => (1, 2, cs) |
|
395 |
| "-" :: cs => (~1, 10, cs) |
|
396 |
| cs => (1, 10, cs)); |
|
29156
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
397 |
in |
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
398 |
{radix = radix, |
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
399 |
leading_zeros = leading_zeros digs, |
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
400 |
value = sign * #1 (Library.read_radix_int radix digs)} |
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
401 |
end; |
9326 | 402 |
|
15991
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
403 |
end; |
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
404 |
|
28904 | 405 |
fun read_float str = |
406 |
let |
|
407 |
val (sign, cs) = |
|
29156
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
408 |
(case Symbol.explode str of |
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
409 |
"-" :: cs => (~1, cs) |
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
410 |
| cs => (1, cs)); |
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
411 |
val (intpart, fracpart) = |
28904 | 412 |
(case take_prefix Symbol.is_digit cs of |
29156
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
413 |
(intpart, "." :: fracpart) => (intpart, fracpart) |
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
414 |
| _ => raise Fail "read_float"); |
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
415 |
in |
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
416 |
{mant = sign * #1 (Library.read_int (intpart @ fracpart)), |
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
417 |
exp = length fracpart} |
28904 | 418 |
end |
419 |
||
0 | 420 |
end; |