author | wenzelm |
Mon, 20 Jan 2014 20:04:52 +0100 | |
changeset 55105 | 75815b3b38a1 |
parent 55035 | 68afbb5ce4ff |
child 55106 | 080c0006e917 |
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 |
|
30573 | 15 |
val scan_id: Symbol_Pos.T list -> Symbol_Pos.T list * Symbol_Pos.T list |
16 |
val scan_longid: Symbol_Pos.T list -> Symbol_Pos.T list * Symbol_Pos.T list |
|
17 |
val scan_tid: Symbol_Pos.T list -> Symbol_Pos.T list * Symbol_Pos.T list |
|
18 |
val scan_nat: Symbol_Pos.T list -> Symbol_Pos.T list * Symbol_Pos.T list |
|
19 |
val scan_int: Symbol_Pos.T list -> Symbol_Pos.T list * Symbol_Pos.T list |
|
40290
47f572aff50a
support for floating-point tokens in outer syntax (coinciding with inner syntax version);
wenzelm
parents:
39510
diff
changeset
|
20 |
val scan_float: Symbol_Pos.T list -> Symbol_Pos.T list * Symbol_Pos.T list |
30573 | 21 |
val scan_hex: Symbol_Pos.T list -> Symbol_Pos.T list * Symbol_Pos.T list |
22 |
val scan_bin: Symbol_Pos.T list -> Symbol_Pos.T list * Symbol_Pos.T list |
|
23 |
val scan_var: Symbol_Pos.T list -> Symbol_Pos.T list * Symbol_Pos.T list |
|
24 |
val scan_tvar: Symbol_Pos.T list -> Symbol_Pos.T list * Symbol_Pos.T list |
|
50239 | 25 |
val is_tid: string -> bool |
27800
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
26 |
datatype token_kind = |
27887 | 27 |
Literal | IdentSy | LongIdentSy | VarSy | TFreeSy | TVarSy | |
55033 | 28 |
NumSy | FloatSy | XNumSy | StrSy | Cartouche | Space | Comment | EOF |
27800
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
29 |
datatype token = Token of token_kind * string * Position.range |
27806 | 30 |
val str_of_token: token -> string |
31 |
val pos_of_token: token -> Position.T |
|
27887 | 32 |
val is_proper: token -> bool |
27800
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
33 |
val mk_eof: Position.T -> token |
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
34 |
val eof: token |
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
35 |
val is_eof: token -> bool |
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
36 |
val stopper: token Scan.stopper |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
37 |
val terminals: string list |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
38 |
val is_terminal: string -> bool |
49821
d15fe10593ff
clarified output token markup (see also bc22daeed49e);
wenzelm
parents:
48992
diff
changeset
|
39 |
val literal_markup: string -> Markup.T |
44736 | 40 |
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
|
41 |
val reported_token_range: Proof.context -> token -> string |
18 | 42 |
val matching_tokens: token * token -> bool |
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 |
46483 | 45 |
val implode_str: string list -> string |
46 |
val explode_str: string -> string list |
|
30573 | 47 |
val tokenize: Scan.lexicon -> bool -> Symbol_Pos.T list -> token list |
42290
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
48 |
val read_indexname: string -> indexname |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
49 |
val read_var: string -> term |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
50 |
val read_variable: string -> indexname option |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
51 |
val read_nat: string -> int option |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
52 |
val read_int: string -> int option |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
53 |
val read_xnum: string -> {radix: int, leading_zeros: int, value: int} |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
54 |
val read_float: string -> {mant: int, exp: int} |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
55 |
val mark_class: string -> string val unmark_class: string -> string |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
56 |
val mark_type: string -> string val unmark_type: string -> string |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
57 |
val mark_const: string -> string val unmark_const: string -> string |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
58 |
val mark_fixed: string -> string val unmark_fixed: string -> string |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
59 |
val unmark: |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
60 |
{case_class: string -> 'a, |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
61 |
case_type: string -> 'a, |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
62 |
case_const: string -> 'a, |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
63 |
case_fixed: string -> 'a, |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
64 |
case_default: string -> 'a} -> string -> 'a |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
65 |
val is_marked: string -> bool |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
66 |
val dummy_type: term |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
67 |
val fun_type: term |
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 |
||
42476 | 73 |
(** syntaxtic terms **) |
74 |
||
75 |
structure Syntax = |
|
76 |
struct |
|
77 |
||
78 |
fun const c = Const (c, dummyT); |
|
79 |
fun free x = Free (x, dummyT); |
|
80 |
fun var xi = Var (xi, dummyT); |
|
81 |
||
82 |
end; |
|
83 |
||
84 |
||
85 |
||
4703 | 86 |
(** basic scanners **) |
87 |
||
30573 | 88 |
open Basic_Symbol_Pos; |
27773 | 89 |
|
55105 | 90 |
val err_prefix = "Inner lexical error: "; |
91 |
||
92 |
fun !!! msg = Symbol_Pos.!!! (fn () => err_prefix ^ msg); |
|
27773 | 93 |
|
50239 | 94 |
val scan_id = Symbol_Pos.scan_ident; |
27773 | 95 |
val scan_longid = scan_id @@@ (Scan.repeat1 ($$$ "." @@@ scan_id) >> flat); |
96 |
val scan_tid = $$$ "'" @@@ scan_id; |
|
4703 | 97 |
|
40525
14a2e686bdac
eliminated slightly odd pervasive Symbol_Pos.symbol;
wenzelm
parents:
40290
diff
changeset
|
98 |
val scan_nat = Scan.many1 (Symbol.is_digit o Symbol_Pos.symbol); |
27773 | 99 |
val scan_int = $$$ "-" @@@ scan_nat || scan_nat; |
28904 | 100 |
val scan_natdot = scan_nat @@@ $$$ "." @@@ scan_nat; |
101 |
val scan_float = $$$ "-" @@@ scan_natdot || scan_natdot; |
|
40525
14a2e686bdac
eliminated slightly odd pervasive Symbol_Pos.symbol;
wenzelm
parents:
40290
diff
changeset
|
102 |
val scan_hex = $$$ "0" @@@ $$$ "x" @@@ Scan.many1 (Symbol.is_ascii_hex o Symbol_Pos.symbol); |
27773 | 103 |
val scan_bin = $$$ "0" @@@ $$$ "b" @@@ Scan.many1 (fn (s, _) => s = "0" orelse s = "1"); |
4703 | 104 |
|
27773 | 105 |
val scan_id_nat = scan_id @@@ Scan.optional ($$$ "." @@@ scan_nat) []; |
106 |
val scan_var = $$$ "?" @@@ scan_id_nat; |
|
107 |
val scan_tvar = $$$ "?" @@@ $$$ "'" @@@ scan_id_nat; |
|
4703 | 108 |
|
50239 | 109 |
fun is_tid s = |
110 |
(case try (unprefix "'") s of |
|
111 |
SOME s' => Symbol_Pos.is_identifier s' |
|
112 |
| NONE => false); |
|
113 |
||
4703 | 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 | |
55033 | 120 |
NumSy | FloatSy | XNumSy | StrSy | Cartouche | 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 |
|
29156
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
145 |
val terminal_kinds = |
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
146 |
[("id", IdentSy), |
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
147 |
("longid", LongIdentSy), |
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
148 |
("var", VarSy), |
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
149 |
("tid", TFreeSy), |
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
150 |
("tvar", TVarSy), |
45703
c7a13ce60161
renamed inner syntax categories "num" to "num_token" and "xnum" to "xnum_token";
wenzelm
parents:
45666
diff
changeset
|
151 |
("num_token", NumSy), |
29156
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
152 |
("float_token", FloatSy), |
45703
c7a13ce60161
renamed inner syntax categories "num" to "num_token" and "xnum" to "xnum_token";
wenzelm
parents:
45666
diff
changeset
|
153 |
("xnum_token", XNumSy), |
55033 | 154 |
("str_token", StrSy), |
155 |
("cartouche", Cartouche)]; |
|
29156
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
156 |
|
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
157 |
val terminals = map #1 terminal_kinds; |
20664 | 158 |
val is_terminal = member (op =) terminals; |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
164
diff
changeset
|
159 |
|
0 | 160 |
|
27887 | 161 |
(* markup *) |
162 |
||
49821
d15fe10593ff
clarified output token markup (see also bc22daeed49e);
wenzelm
parents:
48992
diff
changeset
|
163 |
fun literal_markup s = |
52189 | 164 |
if Symbol.is_ascii_identifier s orelse exists Symbol.is_letter (Symbol.explode s) |
165 |
then Markup.literal |
|
166 |
else Markup.delimiter; |
|
49821
d15fe10593ff
clarified output token markup (see also bc22daeed49e);
wenzelm
parents:
48992
diff
changeset
|
167 |
|
27887 | 168 |
val token_kind_markup = |
51612
6a1e40f9dd55
added var_position in analogy to longid_position, for typing reports on input;
wenzelm
parents:
50242
diff
changeset
|
169 |
fn TFreeSy => Markup.tfree |
55033 | 170 |
| TVarSy => Markup.tvar |
171 |
| NumSy => Markup.numeral |
|
50201
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents:
49821
diff
changeset
|
172 |
| FloatSy => Markup.numeral |
55033 | 173 |
| XNumSy => Markup.numeral |
174 |
| StrSy => Markup.inner_string |
|
175 |
| Cartouche => Markup.inner_cartouche |
|
50201
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents:
49821
diff
changeset
|
176 |
| Comment => Markup.inner_comment |
55033 | 177 |
| _ => Markup.empty; |
27887 | 178 |
|
44736 | 179 |
fun report_of_token (Token (kind, s, (pos, _))) = |
49821
d15fe10593ff
clarified output token markup (see also bc22daeed49e);
wenzelm
parents:
48992
diff
changeset
|
180 |
let val markup = if kind = Literal then literal_markup s else token_kind_markup kind |
44736 | 181 |
in (pos, markup) end; |
27887 | 182 |
|
39510
d9f5f01faa1b
Syntax.read_asts error: report token ranges within message -- no side-effect here;
wenzelm
parents:
39507
diff
changeset
|
183 |
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
|
184 |
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
|
185 |
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
|
186 |
else ""; |
39168
e3ac771235f7
report token range after inner parse error -- often provides important clues about misunderstanding concerning lexical phase;
wenzelm
parents:
38474
diff
changeset
|
187 |
|
27887 | 188 |
|
18 | 189 |
(* matching_tokens *) |
0 | 190 |
|
27800
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
191 |
fun matching_tokens (Token (Literal, x, _), Token (Literal, y, _)) = x = y |
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
192 |
| matching_tokens (Token (k, _, _), Token (k', _, _)) = k = k'; |
0 | 193 |
|
194 |
||
18 | 195 |
(* valued_token *) |
0 | 196 |
|
27800
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
197 |
fun valued_token (Token (Literal, _, _)) = false |
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
198 |
| valued_token (Token (EOF, _, _)) = false |
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
199 |
| valued_token _ = true; |
0 | 200 |
|
201 |
||
18 | 202 |
(* predef_term *) |
0 | 203 |
|
29156
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
204 |
fun predef_term s = |
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
205 |
(case AList.lookup (op =) terminal_kinds s of |
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
206 |
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
|
207 |
| NONE => NONE); |
0 | 208 |
|
209 |
||
46483 | 210 |
(* str tokens *) |
18 | 211 |
|
14730 | 212 |
val scan_chr = |
27773 | 213 |
$$$ "\\" |-- $$$ "'" || |
40525
14a2e686bdac
eliminated slightly odd pervasive Symbol_Pos.symbol;
wenzelm
parents:
40290
diff
changeset
|
214 |
Scan.one |
14a2e686bdac
eliminated slightly odd pervasive Symbol_Pos.symbol;
wenzelm
parents:
40290
diff
changeset
|
215 |
((fn s => s <> "\\" andalso s <> "'" andalso Symbol.is_regular s) o |
14a2e686bdac
eliminated slightly odd pervasive Symbol_Pos.symbol;
wenzelm
parents:
40290
diff
changeset
|
216 |
Symbol_Pos.symbol) >> single || |
27773 | 217 |
$$$ "'" --| Scan.ahead (~$$$ "'"); |
14730 | 218 |
|
219 |
val scan_str = |
|
27800
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
220 |
$$$ "'" @@@ $$$ "'" @@@ !!! "missing end of string" |
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
221 |
((Scan.repeat scan_chr >> flat) @@@ $$$ "'" @@@ $$$ "'"); |
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
222 |
|
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
223 |
val scan_str_body = |
27773 | 224 |
$$$ "'" |-- $$$ "'" |-- !!! "missing end of string" |
225 |
((Scan.repeat scan_chr >> flat) --| $$$ "'" --| $$$ "'"); |
|
14730 | 226 |
|
0 | 227 |
|
46483 | 228 |
fun implode_str cs = enclose "''" "''" (implode (map (fn "'" => "\\'" | c => c) cs)); |
18 | 229 |
|
46483 | 230 |
fun explode_str str = |
30573 | 231 |
(case Scan.read Symbol_Pos.stopper scan_str_body (Symbol_Pos.explode (str, Position.none)) of |
40525
14a2e686bdac
eliminated slightly odd pervasive Symbol_Pos.symbol;
wenzelm
parents:
40290
diff
changeset
|
232 |
SOME cs => map Symbol_Pos.symbol cs |
5868 | 233 |
| _ => error ("Inner lexical error: literal string expected at " ^ quote str)); |
18 | 234 |
|
235 |
||
27806 | 236 |
|
18 | 237 |
(** tokenize **) |
238 |
||
27800
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
239 |
fun token_leq (Token (_, s1, _), Token (_, s2, _)) = s1 <= s2; |
30573 | 240 |
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
|
241 |
|
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
242 |
fun tokenize lex xids syms = |
18 | 243 |
let |
244 |
val scan_xid = |
|
27773 | 245 |
if xids then $$$ "_" @@@ scan_id || scan_id |
18 | 246 |
else scan_id; |
247 |
||
20096 | 248 |
val scan_num = scan_hex || scan_bin || scan_int; |
249 |
||
550
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents:
376
diff
changeset
|
250 |
val scan_val = |
26007
3760d3ff4cce
basic scanners: produce symbol list instead of imploded string;
wenzelm
parents:
24630
diff
changeset
|
251 |
scan_tvar >> token TVarSy || |
3760d3ff4cce
basic scanners: produce symbol list instead of imploded string;
wenzelm
parents:
24630
diff
changeset
|
252 |
scan_var >> token VarSy || |
3760d3ff4cce
basic scanners: produce symbol list instead of imploded string;
wenzelm
parents:
24630
diff
changeset
|
253 |
scan_tid >> token TFreeSy || |
28904 | 254 |
scan_float >> token FloatSy || |
26007
3760d3ff4cce
basic scanners: produce symbol list instead of imploded string;
wenzelm
parents:
24630
diff
changeset
|
255 |
scan_num >> token NumSy || |
27773 | 256 |
$$$ "#" @@@ scan_num >> token XNumSy || |
26007
3760d3ff4cce
basic scanners: produce symbol list instead of imploded string;
wenzelm
parents:
24630
diff
changeset
|
257 |
scan_longid >> token LongIdentSy || |
3760d3ff4cce
basic scanners: produce symbol list instead of imploded string;
wenzelm
parents:
24630
diff
changeset
|
258 |
scan_xid >> token IdentSy; |
18 | 259 |
|
27800
df444ddeff56
datatype token: maintain range, tuned representation;
wenzelm
parents:
27773
diff
changeset
|
260 |
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
|
261 |
|
4703 | 262 |
val scan_token = |
55105 | 263 |
Symbol_Pos.scan_cartouche err_prefix >> token Cartouche || |
264 |
Symbol_Pos.scan_comment err_prefix >> token Comment || |
|
27887 | 265 |
Scan.max token_leq scan_lit scan_val || |
266 |
scan_str >> token StrSy || |
|
40525
14a2e686bdac
eliminated slightly odd pervasive Symbol_Pos.symbol;
wenzelm
parents:
40290
diff
changeset
|
267 |
Scan.many1 (Symbol.is_blank o Symbol_Pos.symbol) >> token Space; |
18 | 268 |
in |
27773 | 269 |
(case Scan.error |
30573 | 270 |
(Scan.finite Symbol_Pos.stopper (Scan.repeat scan_token)) syms of |
27887 | 271 |
(toks, []) => toks |
30573 | 272 |
| (_, ss) => error ("Inner lexical error at: " ^ Symbol_Pos.content ss ^ |
48992 | 273 |
Position.here (#1 (Symbol_Pos.range ss)))) |
18 | 274 |
end; |
275 |
||
276 |
||
277 |
||
278 |
(** scan variables **) |
|
279 |
||
15991
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
280 |
(* scan_indexname *) |
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
281 |
|
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
282 |
local |
18 | 283 |
|
27773 | 284 |
val scan_vname = |
18 | 285 |
let |
15991
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
286 |
fun nat n [] = n |
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
287 |
| nat n (c :: cs) = nat (n * 10 + (ord c - ord "0")) cs; |
18 | 288 |
|
15991
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
289 |
fun idxname cs ds = (implode (rev cs), nat 0 ds); |
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
290 |
fun chop_idx [] ds = idxname [] ds |
50242
56b9c792a98b
support for sub-structured identifier syntax (inactive);
wenzelm
parents:
50239
diff
changeset
|
291 |
| chop_idx (cs as (_ :: "\\<^sub>" :: _)) ds = idxname cs ds |
15991
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
292 |
| chop_idx (c :: cs) ds = |
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
293 |
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
|
294 |
else idxname (c :: cs) ds; |
18 | 295 |
|
50239 | 296 |
val scan = |
297 |
(scan_id >> map Symbol_Pos.symbol) -- |
|
40525
14a2e686bdac
eliminated slightly odd pervasive Symbol_Pos.symbol;
wenzelm
parents:
40290
diff
changeset
|
298 |
Scan.optional ($$$ "." |-- scan_nat >> (nat 0 o map Symbol_Pos.symbol)) ~1; |
18 | 299 |
in |
27773 | 300 |
scan >> |
301 |
(fn (cs, ~1) => chop_idx (rev cs) [] |
|
302 |
| (cs, i) => (implode cs, i)) |
|
18 | 303 |
end; |
304 |
||
15991
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
305 |
in |
18 | 306 |
|
27773 | 307 |
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
|
308 |
|
15991
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
309 |
end; |
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
310 |
|
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
311 |
|
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
312 |
(* indexname *) |
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
313 |
|
20313
bf9101cc4385
renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents:
20165
diff
changeset
|
314 |
fun read_indexname s = |
30573 | 315 |
(case Scan.read Symbol_Pos.stopper scan_indexname (Symbol_Pos.explode (s, Position.none)) of |
15531 | 316 |
SOME xi => xi |
20313
bf9101cc4385
renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents:
20165
diff
changeset
|
317 |
| _ => error ("Lexical error in variable name: " ^ quote s)); |
18 | 318 |
|
319 |
||
4703 | 320 |
(* read_var *) |
18 | 321 |
|
4703 | 322 |
fun read_var str = |
18 | 323 |
let |
324 |
val scan = |
|
42476 | 325 |
$$$ "?" |-- scan_indexname --| Scan.ahead (Scan.one Symbol_Pos.is_eof) |
326 |
>> Syntax.var || |
|
327 |
Scan.many (Symbol.is_regular o Symbol_Pos.symbol) |
|
328 |
>> (Syntax.free o implode o map Symbol_Pos.symbol); |
|
30573 | 329 |
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
|
330 |
|
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
331 |
|
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
332 |
(* read_variable *) |
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
333 |
|
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
334 |
fun read_variable str = |
27773 | 335 |
let val scan = $$$ "?" |-- scan_indexname || scan_indexname |
30573 | 336 |
in Scan.read Symbol_Pos.stopper scan (Symbol_Pos.explode (str, Position.none)) end; |
4587 | 337 |
|
338 |
||
20313
bf9101cc4385
renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents:
20165
diff
changeset
|
339 |
(* read numbers *) |
bf9101cc4385
renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents:
20165
diff
changeset
|
340 |
|
bf9101cc4385
renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents:
20165
diff
changeset
|
341 |
local |
bf9101cc4385
renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents:
20165
diff
changeset
|
342 |
|
bf9101cc4385
renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents:
20165
diff
changeset
|
343 |
fun nat cs = |
40525
14a2e686bdac
eliminated slightly odd pervasive Symbol_Pos.symbol;
wenzelm
parents:
40290
diff
changeset
|
344 |
Option.map (#1 o Library.read_int o map Symbol_Pos.symbol) |
30573 | 345 |
(Scan.read Symbol_Pos.stopper scan_nat cs); |
5860 | 346 |
|
20313
bf9101cc4385
renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents:
20165
diff
changeset
|
347 |
in |
bf9101cc4385
renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents:
20165
diff
changeset
|
348 |
|
30573 | 349 |
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
|
350 |
|
bf9101cc4385
renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents:
20165
diff
changeset
|
351 |
fun read_int s = |
30573 | 352 |
(case Symbol_Pos.explode (s, Position.none) of |
27773 | 353 |
("-", _) :: cs => Option.map ~ (nat cs) |
20313
bf9101cc4385
renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents:
20165
diff
changeset
|
354 |
| cs => nat cs); |
bf9101cc4385
renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents:
20165
diff
changeset
|
355 |
|
bf9101cc4385
renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents:
20165
diff
changeset
|
356 |
end; |
5860 | 357 |
|
358 |
||
20096 | 359 |
(* read_xnum: hex/bin/decimal *) |
9326 | 360 |
|
15991
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
361 |
local |
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
362 |
|
20096 | 363 |
val ten = ord "0" + 10; |
364 |
val a = ord "a"; |
|
365 |
val A = ord "A"; |
|
35428 | 366 |
val _ = a > A orelse raise Fail "Bad ASCII"; |
20096 | 367 |
|
368 |
fun remap_hex c = |
|
369 |
let val x = ord c in |
|
370 |
if x >= a then chr (x - a + ten) |
|
371 |
else if x >= A then chr (x - A + ten) |
|
372 |
else c |
|
373 |
end; |
|
15991
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
374 |
|
21781 | 375 |
fun leading_zeros ["0"] = 0 |
376 |
| leading_zeros ("0" :: cs) = 1 + leading_zeros cs |
|
377 |
| leading_zeros _ = 0; |
|
378 |
||
15991
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
379 |
in |
15965
f422f8283491
Use of IntInf.int instead of int in most numeric simprocs; avoids
paulson
parents:
15570
diff
changeset
|
380 |
|
9326 | 381 |
fun read_xnum str = |
382 |
let |
|
20096 | 383 |
val (sign, radix, digs) = |
384 |
(case Symbol.explode (perhaps (try (unprefix "#")) str) of |
|
385 |
"0" :: "x" :: cs => (1, 16, map remap_hex cs) |
|
386 |
| "0" :: "b" :: cs => (1, 2, cs) |
|
387 |
| "-" :: cs => (~1, 10, cs) |
|
388 |
| cs => (1, 10, cs)); |
|
29156
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
389 |
in |
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
390 |
{radix = radix, |
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
391 |
leading_zeros = leading_zeros digs, |
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
392 |
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
|
393 |
end; |
9326 | 394 |
|
15991
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
395 |
end; |
670f8e4b5a98
added read_variable: optional question mark on input;
wenzelm
parents:
15965
diff
changeset
|
396 |
|
28904 | 397 |
fun read_float str = |
398 |
let |
|
399 |
val (sign, cs) = |
|
29156
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
400 |
(case Symbol.explode str of |
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
401 |
"-" :: cs => (~1, cs) |
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
402 |
| cs => (1, cs)); |
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
403 |
val (intpart, fracpart) = |
28904 | 404 |
(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
|
405 |
(intpart, "." :: fracpart) => (intpart, fracpart) |
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
406 |
| _ => raise Fail "read_float"); |
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
407 |
in |
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
408 |
{mant = sign * #1 (Library.read_int (intpart @ fracpart)), |
89f76a58a378
renamed terminal category "float" to "float_token", to avoid name
wenzelm
parents:
28904
diff
changeset
|
409 |
exp = length fracpart} |
42046
6341c23baf10
added Lexicon.encode_position, Lexicon.decode_position;
wenzelm
parents:
40525
diff
changeset
|
410 |
end; |
6341c23baf10
added Lexicon.encode_position, Lexicon.decode_position;
wenzelm
parents:
40525
diff
changeset
|
411 |
|
42290
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
412 |
|
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
413 |
(* marked logical entities *) |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
414 |
|
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
415 |
fun marker s = (prefix s, unprefix s); |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
416 |
|
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
417 |
val (mark_class, unmark_class) = marker "\\<^class>"; |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
418 |
val (mark_type, unmark_type) = marker "\\<^type>"; |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
419 |
val (mark_const, unmark_const) = marker "\\<^const>"; |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
420 |
val (mark_fixed, unmark_fixed) = marker "\\<^fixed>"; |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
421 |
|
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
422 |
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
|
423 |
(case try unmark_class s of |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
424 |
SOME c => case_class c |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
425 |
| NONE => |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
426 |
(case try unmark_type s of |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
427 |
SOME c => case_type c |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
428 |
| NONE => |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
429 |
(case try unmark_const s of |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
430 |
SOME c => case_const c |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
431 |
| NONE => |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
432 |
(case try unmark_fixed s of |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
433 |
SOME c => case_fixed c |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
434 |
| NONE => case_default s)))); |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
435 |
|
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
436 |
val is_marked = |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
437 |
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
|
438 |
case_fixed = K true, case_default = K false}; |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
439 |
|
42476 | 440 |
val dummy_type = Syntax.const (mark_type "dummy"); |
441 |
val fun_type = Syntax.const (mark_type "fun"); |
|
42290
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42264
diff
changeset
|
442 |
|
0 | 443 |
end; |