author | wenzelm |
Wed, 31 Jan 2018 11:23:53 +0100 | |
changeset 67550 | 3b666615e3ce |
parent 67549 | 7b399522d6c1 |
child 67551 | 4a087b9a29c5 |
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 |
|
42290
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
7 |
signature LEXICON = |
4247
9bba9251bb4d
added implode_xstr: string list -> string, explode_xstr: string -> string list;
wenzelm
parents:
3828
diff
changeset
|
8 |
sig |
42476 | 9 |
structure Syntax: |
10 |
sig |
|
11 |
val const: string -> term |
|
12 |
val free: string -> term |
|
13 |
val var: indexname -> term |
|
14 |
end |
|
67426 | 15 |
val scan_id: Symbol_Pos.T list scanner |
16 |
val scan_longid: Symbol_Pos.T list scanner |
|
17 |
val scan_tid: Symbol_Pos.T list scanner |
|
18 |
val scan_hex: Symbol_Pos.T list scanner |
|
19 |
val scan_bin: Symbol_Pos.T list scanner |
|
20 |
val scan_var: Symbol_Pos.T list scanner |
|
21 |
val scan_tvar: Symbol_Pos.T list scanner |
|
50239 | 22 |
val is_tid: string -> bool |
27800
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
23 |
datatype token_kind = |
67548 | 24 |
Literal | Ident | Long_Ident | Var | Type_Ident | Type_Var | Num | Float | Str | |
67550 | 25 |
String | Cartouche | Space | Comment of Comment.kind option | Dummy | EOF |
67539 | 26 |
val token_kind_ord: token_kind * token_kind -> order |
27800
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
27 |
datatype token = Token of token_kind * string * Position.range |
67412 | 28 |
val kind_of_token: token -> token_kind |
27806 | 29 |
val str_of_token: token -> string |
30 |
val pos_of_token: token -> Position.T |
|
67549 | 31 |
val tokens_match_ord: token * token -> order |
27887 | 32 |
val is_proper: token -> bool |
67550 | 33 |
val dummy: token |
27800
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
34 |
val mk_eof: Position.T -> token |
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
35 |
val eof: token |
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
36 |
val is_eof: token -> bool |
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
37 |
val stopper: token Scan.stopper |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
38 |
val terminals: string list |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
39 |
val is_terminal: string -> bool |
49821
d15fe10593ff
clarified output token markup (see also bc22daeed49e);
wenzelm
parents:
48992
diff
changeset
|
40 |
val literal_markup: string -> Markup.T |
44736 | 41 |
val report_of_token: token -> Position.report |
39510
d9f5f01faa1b
Syntax.read_asts error: report token ranges within message -- no side-effect here;
wenzelm
parents:
39507
diff
changeset
|
42 |
val reported_token_range: Proof.context -> token -> string |
18 | 43 |
val valued_token: token -> bool |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
44 |
val predef_term: string -> token option |
55108
0b7a0c1fdf7e
inner syntax token language allows regular quoted strings;
wenzelm
parents:
55107
diff
changeset
|
45 |
val implode_string: Symbol.symbol list -> string |
0b7a0c1fdf7e
inner syntax token language allows regular quoted strings;
wenzelm
parents:
55107
diff
changeset
|
46 |
val explode_string: string * Position.T -> Symbol_Pos.T list |
0b7a0c1fdf7e
inner syntax token language allows regular quoted strings;
wenzelm
parents:
55107
diff
changeset
|
47 |
val implode_str: Symbol.symbol list -> string |
0b7a0c1fdf7e
inner syntax token language allows regular quoted strings;
wenzelm
parents:
55107
diff
changeset
|
48 |
val explode_str: string * Position.T -> Symbol_Pos.T list |
30573 | 49 |
val tokenize: Scan.lexicon -> bool -> Symbol_Pos.T list -> token list |
42290
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
50 |
val read_indexname: string -> indexname |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
51 |
val read_var: string -> term |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
52 |
val read_variable: string -> indexname option |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
53 |
val read_nat: string -> int option |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
54 |
val read_int: string -> int option |
58421 | 55 |
val read_num: string -> {radix: int, leading_zeros: int, value: int} |
42290
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
56 |
val read_float: string -> {mant: int, exp: int} |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
57 |
val mark_class: string -> string val unmark_class: string -> string |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
58 |
val mark_type: string -> string val unmark_type: string -> string |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
59 |
val mark_const: string -> string val unmark_const: string -> string |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
60 |
val mark_fixed: string -> string val unmark_fixed: string -> string |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
61 |
val unmark: |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
62 |
{case_class: string -> 'a, |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
63 |
case_type: string -> 'a, |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
64 |
case_const: string -> 'a, |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
65 |
case_fixed: string -> 'a, |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
66 |
case_default: string -> 'a} -> string -> 'a |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
67 |
val is_marked: string -> bool |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
68 |
val dummy_type: term |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
69 |
val fun_type: term |
4247
9bba9251bb4d
added implode_xstr: string list -> string, explode_xstr: string -> string list;
wenzelm
parents:
3828
diff
changeset
|
70 |
end; |
0 | 71 |
|
14679 | 72 |
structure Lexicon: LEXICON = |
0 | 73 |
struct |
74 |
||
42476 | 75 |
(** syntaxtic terms **) |
76 |
||
77 |
structure Syntax = |
|
78 |
struct |
|
79 |
||
80 |
fun const c = Const (c, dummyT); |
|
81 |
fun free x = Free (x, dummyT); |
|
82 |
fun var xi = Var (xi, dummyT); |
|
83 |
||
84 |
end; |
|
85 |
||
86 |
||
87 |
||
4703 | 88 |
(** basic scanners **) |
89 |
||
30573 | 90 |
open Basic_Symbol_Pos; |
27773 | 91 |
|
55105 | 92 |
val err_prefix = "Inner lexical error: "; |
93 |
||
94 |
fun !!! msg = Symbol_Pos.!!! (fn () => err_prefix ^ msg); |
|
27773 | 95 |
|
50239 | 96 |
val scan_id = Symbol_Pos.scan_ident; |
61476 | 97 |
val scan_longid = scan_id @@@ Scan.repeats1 ($$$ "." @@@ scan_id); |
27773 | 98 |
val scan_tid = $$$ "'" @@@ scan_id; |
4703 | 99 |
|
40525
14a2e686bdac
eliminated slightly odd pervasive Symbol_Pos.symbol;
wenzelm
parents:
40290
diff
changeset
|
100 |
val scan_hex = $$$ "0" @@@ $$$ "x" @@@ Scan.many1 (Symbol.is_ascii_hex o Symbol_Pos.symbol); |
27773 | 101 |
val scan_bin = $$$ "0" @@@ $$$ "b" @@@ Scan.many1 (fn (s, _) => s = "0" orelse s = "1"); |
62782 | 102 |
val scan_num = scan_hex || scan_bin || Symbol_Pos.scan_nat; |
4703 | 103 |
|
62782 | 104 |
val scan_id_nat = scan_id @@@ Scan.optional ($$$ "." @@@ Symbol_Pos.scan_nat) []; |
27773 | 105 |
val scan_var = $$$ "?" @@@ scan_id_nat; |
106 |
val scan_tvar = $$$ "?" @@@ $$$ "'" @@@ scan_id_nat; |
|
4703 | 107 |
|
50239 | 108 |
fun is_tid s = |
109 |
(case try (unprefix "'") s of |
|
110 |
SOME s' => Symbol_Pos.is_identifier s' |
|
111 |
| NONE => false); |
|
112 |
||
4703 | 113 |
|
114 |
||
18 | 115 |
(** datatype token **) |
0 | 116 |
|
27800
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
117 |
datatype token_kind = |
67548 | 118 |
Literal | Ident | Long_Ident | Var | Type_Ident | Type_Var | Num | Float | Str | |
67550 | 119 |
String | Cartouche | Space | Comment of Comment.kind option | Dummy | EOF; |
27800
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
120 |
|
67539 | 121 |
val token_kind_index = |
122 |
fn Literal => 0 |
|
67548 | 123 |
| Ident => 1 |
124 |
| Long_Ident => 2 |
|
125 |
| Var => 3 |
|
126 |
| Type_Ident => 4 |
|
127 |
| Type_Var => 5 |
|
128 |
| Num => 6 |
|
129 |
| Float => 7 |
|
130 |
| Str => 8 |
|
131 |
| String => 9 |
|
67539 | 132 |
| Cartouche => 10 |
133 |
| Space => 11 |
|
134 |
| Comment NONE => 12 |
|
135 |
| Comment (SOME Comment.Comment) => 13 |
|
136 |
| Comment (SOME Comment.Cancel) => 14 |
|
137 |
| Comment (SOME Comment.Latex) => 15 |
|
67550 | 138 |
| Dummy => 16 |
139 |
| EOF => 17; |
|
67539 | 140 |
|
141 |
val token_kind_ord = int_ord o apply2 token_kind_index; |
|
142 |
||
143 |
||
27800
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
144 |
datatype token = Token of token_kind * string * Position.range; |
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
145 |
|
67412 | 146 |
fun kind_of_token (Token (k, _, _)) = k; |
27806 | 147 |
fun str_of_token (Token (_, s, _)) = s; |
148 |
fun pos_of_token (Token (_, _, (pos, _))) = pos; |
|
149 |
||
67549 | 150 |
fun tokens_match_ord toks = |
151 |
(case apply2 kind_of_token toks of |
|
152 |
(Literal, Literal) => fast_string_ord (apply2 str_of_token toks) |
|
153 |
| kinds => token_kind_ord kinds); |
|
154 |
||
67425
7d4a088dbc0e
clarified modules: uniform notion of formal comments;
wenzelm
parents:
67413
diff
changeset
|
155 |
val is_proper = kind_of_token #> (fn Space => false | Comment _ => false | _ => true); |
27887 | 156 |
|
67550 | 157 |
val dummy = Token (Dummy, "", Position.no_range); |
158 |
||
27800
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
159 |
|
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
160 |
(* stopper *) |
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
161 |
|
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
162 |
fun mk_eof pos = Token (EOF, "", (pos, Position.none)); |
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
163 |
val eof = mk_eof Position.none; |
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
164 |
|
67412 | 165 |
fun is_eof tok = kind_of_token tok = EOF; |
27800
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
166 |
val stopper = Scan.stopper (K eof) is_eof; |
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
167 |
|
0 | 168 |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
169 |
(* terminal arguments *) |
0 | 170 |
|
29156
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
171 |
val terminal_kinds = |
67548 | 172 |
[("id", Ident), |
173 |
("longid", Long_Ident), |
|
174 |
("var", Var), |
|
175 |
("tid", Type_Ident), |
|
176 |
("tvar", Type_Var), |
|
177 |
("num_token", Num), |
|
178 |
("float_token", Float), |
|
179 |
("str_token", Str), |
|
180 |
("string_token", String), |
|
55033 | 181 |
("cartouche", Cartouche)]; |
29156
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
182 |
|
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
183 |
val terminals = map #1 terminal_kinds; |
20664 | 184 |
val is_terminal = member (op =) terminals; |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
185 |
|
0 | 186 |
|
27887 | 187 |
(* markup *) |
188 |
||
49821
d15fe10593ff
clarified output token markup (see also bc22daeed49e);
wenzelm
parents:
48992
diff
changeset
|
189 |
fun literal_markup s = |
52189 | 190 |
if Symbol.is_ascii_identifier s orelse exists Symbol.is_letter (Symbol.explode s) |
191 |
then Markup.literal |
|
192 |
else Markup.delimiter; |
|
49821
d15fe10593ff
clarified output token markup (see also bc22daeed49e);
wenzelm
parents:
48992
diff
changeset
|
193 |
|
27887 | 194 |
val token_kind_markup = |
67548 | 195 |
fn Type_Ident => Markup.tfree |
196 |
| Type_Var => Markup.tvar |
|
197 |
| Num => Markup.numeral |
|
198 |
| Float => Markup.numeral |
|
199 |
| Str => Markup.inner_string |
|
200 |
| String => Markup.inner_string |
|
55033 | 201 |
| Cartouche => Markup.inner_cartouche |
67440 | 202 |
| Comment _ => Markup.inner_comment |
55033 | 203 |
| _ => Markup.empty; |
27887 | 204 |
|
44736 | 205 |
fun report_of_token (Token (kind, s, (pos, _))) = |
49821
d15fe10593ff
clarified output token markup (see also bc22daeed49e);
wenzelm
parents:
48992
diff
changeset
|
206 |
let val markup = if kind = Literal then literal_markup s else token_kind_markup kind |
44736 | 207 |
in (pos, markup) end; |
27887 | 208 |
|
39510
d9f5f01faa1b
Syntax.read_asts error: report token ranges within message -- no side-effect here;
wenzelm
parents:
39507
diff
changeset
|
209 |
fun reported_token_range ctxt tok = |
39168
e3ac771235f7
report token range after inner parse error -- often provides important clues about misunderstanding concerning lexical phase;
wenzelm
parents:
38474
diff
changeset
|
210 |
if is_proper tok |
50201
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents:
49821
diff
changeset
|
211 |
then Context_Position.reported_text ctxt (pos_of_token tok) Markup.token_range "" |
39510
d9f5f01faa1b
Syntax.read_asts error: report token ranges within message -- no side-effect here;
wenzelm
parents:
39507
diff
changeset
|
212 |
else ""; |
39168
e3ac771235f7
report token range after inner parse error -- often provides important clues about misunderstanding concerning lexical phase;
wenzelm
parents:
38474
diff
changeset
|
213 |
|
27887 | 214 |
|
18 | 215 |
(* valued_token *) |
0 | 216 |
|
67412 | 217 |
val valued_token = |
218 |
kind_of_token #> (fn Literal => false | EOF => false | _ => true); |
|
0 | 219 |
|
220 |
||
18 | 221 |
(* predef_term *) |
0 | 222 |
|
29156
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
223 |
fun predef_term s = |
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
224 |
(case AList.lookup (op =) terminal_kinds s of |
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
225 |
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
|
226 |
| NONE => NONE); |
0 | 227 |
|
228 |
||
55108
0b7a0c1fdf7e
inner syntax token language allows regular quoted strings;
wenzelm
parents:
55107
diff
changeset
|
229 |
|
0b7a0c1fdf7e
inner syntax token language allows regular quoted strings;
wenzelm
parents:
55107
diff
changeset
|
230 |
(** string literals **) |
0b7a0c1fdf7e
inner syntax token language allows regular quoted strings;
wenzelm
parents:
55107
diff
changeset
|
231 |
|
0b7a0c1fdf7e
inner syntax token language allows regular quoted strings;
wenzelm
parents:
55107
diff
changeset
|
232 |
fun explode_literal scan_body (str, pos) = |
0b7a0c1fdf7e
inner syntax token language allows regular quoted strings;
wenzelm
parents:
55107
diff
changeset
|
233 |
(case Scan.read Symbol_Pos.stopper scan_body (Symbol_Pos.explode (str, pos)) of |
0b7a0c1fdf7e
inner syntax token language allows regular quoted strings;
wenzelm
parents:
55107
diff
changeset
|
234 |
SOME ss => ss |
0b7a0c1fdf7e
inner syntax token language allows regular quoted strings;
wenzelm
parents:
55107
diff
changeset
|
235 |
| _ => error (err_prefix ^ "malformed string literal " ^ quote str ^ Position.here pos)); |
0b7a0c1fdf7e
inner syntax token language allows regular quoted strings;
wenzelm
parents:
55107
diff
changeset
|
236 |
|
0b7a0c1fdf7e
inner syntax token language allows regular quoted strings;
wenzelm
parents:
55107
diff
changeset
|
237 |
|
0b7a0c1fdf7e
inner syntax token language allows regular quoted strings;
wenzelm
parents:
55107
diff
changeset
|
238 |
(* string *) |
0b7a0c1fdf7e
inner syntax token language allows regular quoted strings;
wenzelm
parents:
55107
diff
changeset
|
239 |
|
0b7a0c1fdf7e
inner syntax token language allows regular quoted strings;
wenzelm
parents:
55107
diff
changeset
|
240 |
val scan_string = Scan.trace (Symbol_Pos.scan_string_qq err_prefix) >> #2; |
0b7a0c1fdf7e
inner syntax token language allows regular quoted strings;
wenzelm
parents:
55107
diff
changeset
|
241 |
val scan_string_body = Symbol_Pos.scan_string_qq err_prefix >> (#1 o #2); |
0b7a0c1fdf7e
inner syntax token language allows regular quoted strings;
wenzelm
parents:
55107
diff
changeset
|
242 |
|
0b7a0c1fdf7e
inner syntax token language allows regular quoted strings;
wenzelm
parents:
55107
diff
changeset
|
243 |
fun implode_string ss = quote (implode (map (fn "\"" => "\\\"" | s => s) ss)); |
0b7a0c1fdf7e
inner syntax token language allows regular quoted strings;
wenzelm
parents:
55107
diff
changeset
|
244 |
val explode_string = explode_literal scan_string_body; |
0b7a0c1fdf7e
inner syntax token language allows regular quoted strings;
wenzelm
parents:
55107
diff
changeset
|
245 |
|
0b7a0c1fdf7e
inner syntax token language allows regular quoted strings;
wenzelm
parents:
55107
diff
changeset
|
246 |
|
0b7a0c1fdf7e
inner syntax token language allows regular quoted strings;
wenzelm
parents:
55107
diff
changeset
|
247 |
(* str *) |
18 | 248 |
|
14730 | 249 |
val scan_chr = |
55107 | 250 |
$$ "\\" |-- $$$ "'" || |
40525
14a2e686bdac
eliminated slightly odd pervasive Symbol_Pos.symbol;
wenzelm
parents:
40290
diff
changeset
|
251 |
Scan.one |
58854 | 252 |
((fn s => s <> "\\" andalso s <> "'" andalso Symbol.not_eof s) o |
40525
14a2e686bdac
eliminated slightly odd pervasive Symbol_Pos.symbol;
wenzelm
parents:
40290
diff
changeset
|
253 |
Symbol_Pos.symbol) >> single || |
55107 | 254 |
$$$ "'" --| Scan.ahead (~$$ "'"); |
14730 | 255 |
|
256 |
val scan_str = |
|
55106 | 257 |
Scan.ahead ($$ "'" -- $$ "'") |-- |
258 |
!!! "unclosed string literal" |
|
61476 | 259 |
($$$ "'" @@@ $$$ "'" @@@ Scan.repeats scan_chr @@@ $$$ "'" @@@ $$$ "'"); |
27800
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
260 |
|
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
261 |
val scan_str_body = |
55107 | 262 |
Scan.ahead ($$ "'" |-- $$ "'") |-- |
55106 | 263 |
!!! "unclosed string literal" |
61476 | 264 |
($$ "'" |-- $$ "'" |-- Scan.repeats scan_chr --| $$ "'" --| $$ "'"); |
14730 | 265 |
|
55108
0b7a0c1fdf7e
inner syntax token language allows regular quoted strings;
wenzelm
parents:
55107
diff
changeset
|
266 |
fun implode_str ss = enclose "''" "''" (implode (map (fn "'" => "\\'" | s => s) ss)); |
0b7a0c1fdf7e
inner syntax token language allows regular quoted strings;
wenzelm
parents:
55107
diff
changeset
|
267 |
val explode_str = explode_literal scan_str_body; |
18 | 268 |
|
269 |
||
27806 | 270 |
|
18 | 271 |
(** tokenize **) |
272 |
||
67412 | 273 |
val token_leq = op <= o apply2 str_of_token; |
30573 | 274 |
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
|
275 |
|
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
276 |
fun tokenize lex xids syms = |
18 | 277 |
let |
278 |
val scan_xid = |
|
55962
fbd0e768bc8f
special identifier "__" (i.e. empty name with internal suffix) serves as wild-card for completion;
wenzelm
parents:
55624
diff
changeset
|
279 |
(if xids then $$$ "_" @@@ scan_id || scan_id else scan_id) || |
fbd0e768bc8f
special identifier "__" (i.e. empty name with internal suffix) serves as wild-card for completion;
wenzelm
parents:
55624
diff
changeset
|
280 |
$$$ "_" @@@ $$$ "_"; |
18 | 281 |
|
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
282 |
val scan_val = |
67548 | 283 |
scan_tvar >> token Type_Var || |
284 |
scan_var >> token Var || |
|
285 |
scan_tid >> token Type_Ident || |
|
286 |
Symbol_Pos.scan_float >> token Float || |
|
287 |
scan_num >> token Num || |
|
288 |
scan_longid >> token Long_Ident || |
|
289 |
scan_xid >> token Ident; |
|
18 | 290 |
|
27800
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
291 |
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
|
292 |
|
4703 | 293 |
val scan_token = |
55105 | 294 |
Symbol_Pos.scan_cartouche err_prefix >> token Cartouche || |
67425
7d4a088dbc0e
clarified modules: uniform notion of formal comments;
wenzelm
parents:
67413
diff
changeset
|
295 |
Symbol_Pos.scan_comment err_prefix >> token (Comment NONE) || |
7d4a088dbc0e
clarified modules: uniform notion of formal comments;
wenzelm
parents:
67413
diff
changeset
|
296 |
Comment.scan >> (fn (kind, ss) => token (Comment (SOME kind)) ss) || |
27887 | 297 |
Scan.max token_leq scan_lit scan_val || |
67548 | 298 |
scan_string >> token String || |
299 |
scan_str >> token Str || |
|
40525
14a2e686bdac
eliminated slightly odd pervasive Symbol_Pos.symbol;
wenzelm
parents:
40290
diff
changeset
|
300 |
Scan.many1 (Symbol.is_blank o Symbol_Pos.symbol) >> token Space; |
18 | 301 |
in |
27773 | 302 |
(case Scan.error |
30573 | 303 |
(Scan.finite Symbol_Pos.stopper (Scan.repeat scan_token)) syms of |
27887 | 304 |
(toks, []) => toks |
55108
0b7a0c1fdf7e
inner syntax token language allows regular quoted strings;
wenzelm
parents:
55107
diff
changeset
|
305 |
| (_, ss) => |
55624 | 306 |
error ("Inner lexical error" ^ Position.here (#1 (Symbol_Pos.range ss)) ^ |
307 |
Markup.markup Markup.no_report ("\nat " ^ quote (Symbol_Pos.content ss)))) |
|
308 |
||
18 | 309 |
end; |
310 |
||
311 |
||
312 |
||
313 |
(** scan variables **) |
|
314 |
||
15991
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
315 |
(* scan_indexname *) |
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
316 |
|
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
317 |
local |
18 | 318 |
|
27773 | 319 |
val scan_vname = |
18 | 320 |
let |
15991
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
321 |
fun nat n [] = n |
64275
ac2abc987cf9
accomodate Poly/ML repository version, which treats singleton strings as boxed;
wenzelm
parents:
62819
diff
changeset
|
322 |
| nat n (c :: cs) = nat (n * 10 + (ord c - Char.ord #"0")) cs; |
18 | 323 |
|
15991
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
324 |
fun idxname cs ds = (implode (rev cs), nat 0 ds); |
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
325 |
fun chop_idx [] ds = idxname [] ds |
62529
8b7bdfc09f3b
clarified treatment of fragments of Isabelle symbols during bootstrap;
wenzelm
parents:
61476
diff
changeset
|
326 |
| chop_idx (cs as (_ :: "\<^sub>" :: _)) ds = idxname cs ds |
15991
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
327 |
| chop_idx (c :: cs) ds = |
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
328 |
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
|
329 |
else idxname (c :: cs) ds; |
18 | 330 |
|
50239 | 331 |
val scan = |
332 |
(scan_id >> map Symbol_Pos.symbol) -- |
|
62782 | 333 |
Scan.optional ($$ "." |-- Symbol_Pos.scan_nat >> (nat 0 o map Symbol_Pos.symbol)) ~1; |
18 | 334 |
in |
27773 | 335 |
scan >> |
336 |
(fn (cs, ~1) => chop_idx (rev cs) [] |
|
337 |
| (cs, i) => (implode cs, i)) |
|
18 | 338 |
end; |
339 |
||
15991
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
340 |
in |
18 | 341 |
|
55107 | 342 |
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
|
343 |
|
15991
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
344 |
end; |
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
345 |
|
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
346 |
|
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
347 |
(* indexname *) |
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
348 |
|
20313
bf9101cc4385
renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents:
20165
diff
changeset
|
349 |
fun read_indexname s = |
62751 | 350 |
(case Scan.read Symbol_Pos.stopper scan_indexname (Symbol_Pos.explode0 s) of |
15531 | 351 |
SOME xi => xi |
20313
bf9101cc4385
renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents:
20165
diff
changeset
|
352 |
| _ => error ("Lexical error in variable name: " ^ quote s)); |
18 | 353 |
|
354 |
||
4703 | 355 |
(* read_var *) |
18 | 356 |
|
4703 | 357 |
fun read_var str = |
18 | 358 |
let |
359 |
val scan = |
|
55107 | 360 |
$$ "?" |-- scan_indexname --| Scan.ahead (Scan.one Symbol_Pos.is_eof) |
42476 | 361 |
>> Syntax.var || |
58854 | 362 |
Scan.many (Symbol.not_eof o Symbol_Pos.symbol) |
42476 | 363 |
>> (Syntax.free o implode o map Symbol_Pos.symbol); |
62751 | 364 |
in the (Scan.read Symbol_Pos.stopper scan (Symbol_Pos.explode0 str)) end; |
15991
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
365 |
|
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
366 |
|
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
367 |
(* read_variable *) |
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
368 |
|
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
369 |
fun read_variable str = |
55107 | 370 |
let val scan = $$ "?" |-- scan_indexname || scan_indexname |
62751 | 371 |
in Scan.read Symbol_Pos.stopper scan (Symbol_Pos.explode0 str) end; |
4587 | 372 |
|
373 |
||
20313
bf9101cc4385
renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents:
20165
diff
changeset
|
374 |
(* read numbers *) |
bf9101cc4385
renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents:
20165
diff
changeset
|
375 |
|
bf9101cc4385
renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents:
20165
diff
changeset
|
376 |
local |
bf9101cc4385
renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents:
20165
diff
changeset
|
377 |
|
bf9101cc4385
renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents:
20165
diff
changeset
|
378 |
fun nat cs = |
40525
14a2e686bdac
eliminated slightly odd pervasive Symbol_Pos.symbol;
wenzelm
parents:
40290
diff
changeset
|
379 |
Option.map (#1 o Library.read_int o map Symbol_Pos.symbol) |
62782 | 380 |
(Scan.read Symbol_Pos.stopper Symbol_Pos.scan_nat cs); |
5860 | 381 |
|
20313
bf9101cc4385
renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents:
20165
diff
changeset
|
382 |
in |
bf9101cc4385
renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents:
20165
diff
changeset
|
383 |
|
62751 | 384 |
fun read_nat s = nat (Symbol_Pos.explode0 s); |
20313
bf9101cc4385
renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents:
20165
diff
changeset
|
385 |
|
bf9101cc4385
renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents:
20165
diff
changeset
|
386 |
fun read_int s = |
62751 | 387 |
(case Symbol_Pos.explode0 s of |
27773 | 388 |
("-", _) :: cs => Option.map ~ (nat cs) |
20313
bf9101cc4385
renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents:
20165
diff
changeset
|
389 |
| cs => nat cs); |
bf9101cc4385
renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents:
20165
diff
changeset
|
390 |
|
bf9101cc4385
renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents:
20165
diff
changeset
|
391 |
end; |
5860 | 392 |
|
393 |
||
58421 | 394 |
(* read_num: hex/bin/decimal *) |
9326 | 395 |
|
15991
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
396 |
local |
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
397 |
|
64275
ac2abc987cf9
accomodate Poly/ML repository version, which treats singleton strings as boxed;
wenzelm
parents:
62819
diff
changeset
|
398 |
val ten = Char.ord #"0" + 10; |
ac2abc987cf9
accomodate Poly/ML repository version, which treats singleton strings as boxed;
wenzelm
parents:
62819
diff
changeset
|
399 |
val a = Char.ord #"a"; |
ac2abc987cf9
accomodate Poly/ML repository version, which treats singleton strings as boxed;
wenzelm
parents:
62819
diff
changeset
|
400 |
val A = Char.ord #"A"; |
35428 | 401 |
val _ = a > A orelse raise Fail "Bad ASCII"; |
20096 | 402 |
|
403 |
fun remap_hex c = |
|
404 |
let val x = ord c in |
|
405 |
if x >= a then chr (x - a + ten) |
|
406 |
else if x >= A then chr (x - A + ten) |
|
407 |
else c |
|
408 |
end; |
|
15991
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
409 |
|
21781 | 410 |
fun leading_zeros ["0"] = 0 |
411 |
| leading_zeros ("0" :: cs) = 1 + leading_zeros cs |
|
412 |
| leading_zeros _ = 0; |
|
413 |
||
15991
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
414 |
in |
15965
f422f8283491
Use of IntInf.int instead of int in most numeric simprocs; avoids
paulson
parents:
15570
diff
changeset
|
415 |
|
58421 | 416 |
fun read_num str = |
9326 | 417 |
let |
58421 | 418 |
val (radix, digs) = |
419 |
(case Symbol.explode str of |
|
420 |
"0" :: "x" :: cs => (16, map remap_hex cs) |
|
421 |
| "0" :: "b" :: cs => (2, cs) |
|
422 |
| cs => (10, cs)); |
|
29156
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
423 |
in |
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
424 |
{radix = radix, |
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
425 |
leading_zeros = leading_zeros digs, |
58421 | 426 |
value = #1 (Library.read_radix_int radix digs)} |
29156
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
427 |
end; |
9326 | 428 |
|
15991
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
429 |
end; |
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
430 |
|
28904 | 431 |
fun read_float str = |
432 |
let |
|
58421 | 433 |
val cs = Symbol.explode str; |
29156
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
434 |
val (intpart, fracpart) = |
67522 | 435 |
(case chop_prefix Symbol.is_digit cs of |
29156
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
436 |
(intpart, "." :: fracpart) => (intpart, fracpart) |
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
437 |
| _ => raise Fail "read_float"); |
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
438 |
in |
58421 | 439 |
{mant = #1 (Library.read_int (intpart @ fracpart)), |
29156
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
440 |
exp = length fracpart} |
42046
6341c23baf10
added Lexicon.encode_position, Lexicon.decode_position;
wenzelm
parents:
40525
diff
changeset
|
441 |
end; |
6341c23baf10
added Lexicon.encode_position, Lexicon.decode_position;
wenzelm
parents:
40525
diff
changeset
|
442 |
|
42290
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
443 |
|
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
444 |
(* marked logical entities *) |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
445 |
|
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
446 |
fun marker s = (prefix s, unprefix s); |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
447 |
|
62529
8b7bdfc09f3b
clarified treatment of fragments of Isabelle symbols during bootstrap;
wenzelm
parents:
61476
diff
changeset
|
448 |
val (mark_class, unmark_class) = marker "\<^class>"; |
8b7bdfc09f3b
clarified treatment of fragments of Isabelle symbols during bootstrap;
wenzelm
parents:
61476
diff
changeset
|
449 |
val (mark_type, unmark_type) = marker "\<^type>"; |
8b7bdfc09f3b
clarified treatment of fragments of Isabelle symbols during bootstrap;
wenzelm
parents:
61476
diff
changeset
|
450 |
val (mark_const, unmark_const) = marker "\<^const>"; |
8b7bdfc09f3b
clarified treatment of fragments of Isabelle symbols during bootstrap;
wenzelm
parents:
61476
diff
changeset
|
451 |
val (mark_fixed, unmark_fixed) = marker "\<^fixed>"; |
42290
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
452 |
|
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
453 |
fun unmark {case_class, case_type, case_const, case_fixed, case_default} s = |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
454 |
(case try unmark_class s of |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
455 |
SOME c => case_class c |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
456 |
| NONE => |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
457 |
(case try unmark_type s of |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
458 |
SOME c => case_type c |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
459 |
| NONE => |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
460 |
(case try unmark_const s of |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
461 |
SOME c => case_const c |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
462 |
| NONE => |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
463 |
(case try unmark_fixed s of |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
464 |
SOME c => case_fixed c |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
465 |
| NONE => case_default s)))); |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
466 |
|
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
467 |
val is_marked = |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
468 |
unmark {case_class = K true, case_type = K true, case_const = K true, |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
469 |
case_fixed = K true, case_default = K false}; |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
470 |
|
42476 | 471 |
val dummy_type = Syntax.const (mark_type "dummy"); |
472 |
val fun_type = Syntax.const (mark_type "fun"); |
|
42290
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
473 |
|
59196 | 474 |
|
475 |
(* toplevel pretty printing *) |
|
476 |
||
62663 | 477 |
val _ = |
62819
d3ff367a16a0
careful export of type-dependent functions, without losing their special status;
wenzelm
parents:
62782
diff
changeset
|
478 |
ML_system_pp (fn _ => fn _ => |
62663 | 479 |
Pretty.to_polyml o Pretty.str_list "{" "}" o map quote o Scan.dest_lexicon); |
59196 | 480 |
|
0 | 481 |
end; |