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