author | wenzelm |
Sun, 30 May 2010 21:34:19 +0200 | |
changeset 37198 | 3af985b10550 |
parent 37195 | e87d305a4490 |
child 37209 | 1c8cf0048934 |
permissions | -rw-r--r-- |
24579 | 1 |
(* Title: Pure/ML/ml_lex.ML |
2 |
Author: Makarius |
|
3 |
||
4 |
Lexical syntax for SML. |
|
5 |
*) |
|
6 |
||
7 |
signature ML_LEX = |
|
8 |
sig |
|
9 |
datatype token_kind = |
|
24596 | 10 |
Keyword | Ident | LongIdent | TypeVar | Word | Int | Real | Char | String | |
24579 | 11 |
Space | Comment | Error of string | EOF |
12 |
eqtype token |
|
27732 | 13 |
val stopper: token Scan.stopper |
24596 | 14 |
val is_regular: token -> bool |
15 |
val is_improper: token -> bool |
|
30683
e8ac1f9d9469
datatype antiquote: maintain original Position.range, which is eventually attached to the resulting ML tokens;
wenzelm
parents:
30645
diff
changeset
|
16 |
val set_range: Position.range -> token -> token |
30636 | 17 |
val pos_of: token -> Position.T |
31474 | 18 |
val end_pos_of: token -> Position.T |
24579 | 19 |
val kind_of: token -> token_kind |
27817 | 20 |
val content_of: token -> string |
31426 | 21 |
val check_content_of: token -> string |
31332 | 22 |
val flatten: token list -> string |
30614 | 23 |
val report_token: token -> unit |
24579 | 24 |
val keywords: string list |
24596 | 25 |
val source: (Symbol.symbol, 'a) Source.source -> |
30573 | 26 |
(token, (Symbol_Pos.T, Position.T * (Symbol.symbol, 'a) Source.source) |
27772 | 27 |
Source.source) Source.source |
30591 | 28 |
val tokenize: string -> token list |
37198
3af985b10550
replaced ML_Lex.read_antiq by more concise ML_Lex.read, which includes full read/report with explicit position information;
wenzelm
parents:
37195
diff
changeset
|
29 |
val read: Position.T -> Symbol_Pos.text -> token Antiquote.antiquote list |
24579 | 30 |
end; |
31 |
||
32 |
structure ML_Lex: ML_LEX = |
|
33 |
struct |
|
34 |
||
35 |
(** tokens **) |
|
36 |
||
37 |
(* datatype token *) |
|
38 |
||
39 |
datatype token_kind = |
|
24596 | 40 |
Keyword | Ident | LongIdent | TypeVar | Word | Int | Real | Char | String | |
24579 | 41 |
Space | Comment | Error of string | EOF; |
42 |
||
27772 | 43 |
datatype token = Token of Position.range * (token_kind * string); |
44 |
||
45 |
||
46 |
(* position *) |
|
47 |
||
30683
e8ac1f9d9469
datatype antiquote: maintain original Position.range, which is eventually attached to the resulting ML tokens;
wenzelm
parents:
30645
diff
changeset
|
48 |
fun set_range range (Token (_, x)) = Token (range, x); |
e8ac1f9d9469
datatype antiquote: maintain original Position.range, which is eventually attached to the resulting ML tokens;
wenzelm
parents:
30645
diff
changeset
|
49 |
|
30636 | 50 |
fun pos_of (Token ((pos, _), _)) = pos; |
51 |
fun end_pos_of (Token ((_, pos), _)) = pos; |
|
24579 | 52 |
|
53 |
||
24596 | 54 |
(* control tokens *) |
24579 | 55 |
|
27772 | 56 |
fun mk_eof pos = Token ((pos, Position.none), (EOF, "")); |
57 |
val eof = mk_eof Position.none; |
|
24579 | 58 |
|
59 |
fun is_eof (Token (_, (EOF, _))) = true |
|
60 |
| is_eof _ = false; |
|
61 |
||
27772 | 62 |
val stopper = |
30636 | 63 |
Scan.stopper (fn [] => eof | toks => mk_eof (end_pos_of (List.last toks))) is_eof; |
27772 | 64 |
|
24579 | 65 |
|
27772 | 66 |
(* token content *) |
67 |
||
31426 | 68 |
fun kind_of (Token (_, (k, _))) = k; |
69 |
||
27817 | 70 |
fun content_of (Token (_, (_, x))) = x; |
71 |
fun token_leq (tok, tok') = content_of tok <= content_of tok'; |
|
27772 | 72 |
|
31426 | 73 |
fun check_content_of tok = |
30636 | 74 |
(case kind_of tok of |
30645 | 75 |
Error msg => error msg |
31426 | 76 |
| _ => content_of tok); |
30636 | 77 |
|
31426 | 78 |
val flatten = implode o map (Symbol.escape o check_content_of); |
31332 | 79 |
|
24596 | 80 |
fun is_regular (Token (_, (Error _, _))) = false |
81 |
| is_regular (Token (_, (EOF, _))) = false |
|
82 |
| is_regular _ = true; |
|
83 |
||
84 |
fun is_improper (Token (_, (Space, _))) = true |
|
85 |
| is_improper (Token (_, (Comment, _))) = true |
|
86 |
| is_improper _ = false; |
|
87 |
||
88 |
||
30614 | 89 |
(* markup *) |
90 |
||
37195 | 91 |
local |
92 |
||
30645 | 93 |
val token_kind_markup = |
94 |
fn Keyword => Markup.ML_keyword |
|
95 |
| Ident => Markup.ML_ident |
|
96 |
| LongIdent => Markup.ML_ident |
|
97 |
| TypeVar => Markup.ML_tvar |
|
98 |
| Word => Markup.ML_numeral |
|
99 |
| Int => Markup.ML_numeral |
|
100 |
| Real => Markup.ML_numeral |
|
101 |
| Char => Markup.ML_char |
|
102 |
| String => Markup.ML_string |
|
103 |
| Space => Markup.none |
|
104 |
| Comment => Markup.ML_comment |
|
105 |
| Error _ => Markup.ML_malformed |
|
106 |
| EOF => Markup.none; |
|
30614 | 107 |
|
37195 | 108 |
fun token_markup kind x = |
109 |
if kind = Keyword andalso exists_string (not o Symbol.is_ascii_letter) x |
|
110 |
then Markup.ML_delimiter |
|
111 |
else token_kind_markup kind; |
|
112 |
||
113 |
in |
|
114 |
||
115 |
fun report_token (Token ((pos, _), (kind, x))) = Position.report (token_markup kind x) pos; |
|
116 |
||
117 |
end; |
|
30614 | 118 |
|
119 |
||
24579 | 120 |
|
121 |
(** scanners **) |
|
122 |
||
30573 | 123 |
open Basic_Symbol_Pos; |
24579 | 124 |
|
30573 | 125 |
fun !!! msg = Symbol_Pos.!!! ("SML lexical error: " ^ msg); |
24579 | 126 |
|
127 |
||
27772 | 128 |
(* blanks *) |
24579 | 129 |
|
27772 | 130 |
val scan_blank = Scan.one (Symbol.is_ascii_blank o symbol); |
131 |
val scan_blanks1 = Scan.repeat1 scan_blank; |
|
24579 | 132 |
|
133 |
||
134 |
(* keywords *) |
|
135 |
||
136 |
val keywords = ["#", "(", ")", ",", "->", "...", ":", ":>", ";", "=", |
|
137 |
"=>", "[", "]", "_", "{", "|", "}", "abstype", "and", "andalso", "as", |
|
138 |
"case", "datatype", "do", "else", "end", "eqtype", "exception", "fn", |
|
139 |
"fun", "functor", "handle", "if", "in", "include", "infix", "infixr", |
|
140 |
"let", "local", "nonfix", "of", "op", "open", "orelse", "raise", "rec", |
|
141 |
"sharing", "sig", "signature", "struct", "structure", "then", "type", |
|
142 |
"val", "where", "while", "with", "withtype"]; |
|
143 |
||
27772 | 144 |
val lex = Scan.make_lexicon (map explode keywords); |
145 |
fun scan_keyword x = Scan.literal lex x; |
|
24579 | 146 |
|
147 |
||
148 |
(* identifiers *) |
|
149 |
||
24596 | 150 |
local |
151 |
||
24579 | 152 |
val scan_letdigs = |
27772 | 153 |
Scan.many ((Symbol.is_ascii_letter orf Symbol.is_ascii_digit orf Symbol.is_ascii_quasi) o symbol); |
24579 | 154 |
|
27772 | 155 |
val scan_alphanumeric = Scan.one (Symbol.is_ascii_letter o symbol) -- scan_letdigs >> op ::; |
24579 | 156 |
|
27772 | 157 |
val scan_symbolic = Scan.many1 (member (op =) (explode "!#$%&*+-/:<=>?@\\^`|~") o symbol); |
24579 | 158 |
|
24596 | 159 |
in |
160 |
||
24579 | 161 |
val scan_ident = scan_alphanumeric || scan_symbolic; |
162 |
||
163 |
val scan_longident = |
|
27772 | 164 |
(Scan.repeat1 (scan_alphanumeric @@@ $$$ ".") >> flat) @@@ (scan_ident || $$$ "="); |
24579 | 165 |
|
27772 | 166 |
val scan_typevar = $$$ "'" @@@ scan_letdigs; |
24579 | 167 |
|
24596 | 168 |
end; |
24579 | 169 |
|
170 |
||
171 |
(* numerals *) |
|
172 |
||
24596 | 173 |
local |
174 |
||
27772 | 175 |
val scan_dec = Scan.many1 (Symbol.is_ascii_digit o symbol); |
176 |
val scan_hex = Scan.many1 (Symbol.is_ascii_hex o symbol); |
|
177 |
val scan_sign = Scan.optional ($$$ "~") []; |
|
178 |
val scan_decint = scan_sign @@@ scan_dec; |
|
24579 | 179 |
|
24596 | 180 |
in |
181 |
||
27772 | 182 |
val scan_word = |
183 |
$$$ "0" @@@ $$$ "w" @@@ $$$ "x" @@@ scan_hex || |
|
184 |
$$$ "0" @@@ $$$ "w" @@@ scan_dec; |
|
24579 | 185 |
|
27772 | 186 |
val scan_int = scan_sign @@@ ($$$ "0" @@@ $$$ "x" @@@ scan_hex || scan_dec); |
24579 | 187 |
|
27772 | 188 |
val scan_exp = ($$$ "E" || $$$ "e") @@@ scan_decint; |
24579 | 189 |
|
190 |
val scan_real = |
|
27772 | 191 |
scan_decint @@@ $$$ "." @@@ scan_dec @@@ Scan.optional scan_exp [] || |
192 |
scan_decint @@@ scan_exp; |
|
24579 | 193 |
|
24596 | 194 |
end; |
195 |
||
24579 | 196 |
|
197 |
(* chars and strings *) |
|
198 |
||
24596 | 199 |
local |
200 |
||
201 |
val scan_escape = |
|
27772 | 202 |
Scan.one (member (op =) (explode "\"\\abtnvfr") o symbol) >> single || |
203 |
$$$ "^" @@@ (Scan.one (fn (s, _) => ord "@" <= ord s andalso ord s <= ord "_") >> single) || |
|
204 |
Scan.one (Symbol.is_ascii_digit o symbol) -- |
|
205 |
Scan.one (Symbol.is_ascii_digit o symbol) -- |
|
206 |
Scan.one (Symbol.is_ascii_digit o symbol) >> (fn ((a, b), c) => [a, b, c]); |
|
24596 | 207 |
|
208 |
val scan_str = |
|
30600
de241396389c
allow non-printable symbols within string tokens;
wenzelm
parents:
30593
diff
changeset
|
209 |
Scan.one (fn (s, _) => Symbol.is_regular s andalso s <> "\"" andalso s <> "\\" andalso |
de241396389c
allow non-printable symbols within string tokens;
wenzelm
parents:
30593
diff
changeset
|
210 |
(not (Symbol.is_char s) orelse Symbol.is_printable s)) >> single || |
27772 | 211 |
$$$ "\\" @@@ !!! "bad escape character in string" scan_escape; |
24596 | 212 |
|
27772 | 213 |
val scan_gap = $$$ "\\" @@@ scan_blanks1 @@@ $$$ "\\"; |
214 |
val scan_gaps = Scan.repeat scan_gap >> flat; |
|
24579 | 215 |
|
24596 | 216 |
in |
24579 | 217 |
|
218 |
val scan_char = |
|
27772 | 219 |
$$$ "#" @@@ $$$ "\"" @@@ scan_gaps @@@ scan_str @@@ scan_gaps @@@ $$$ "\""; |
24579 | 220 |
|
221 |
val scan_string = |
|
27772 | 222 |
$$$ "\"" @@@ !!! "missing quote at end of string" |
223 |
((Scan.repeat (scan_gap || scan_str) >> flat) @@@ $$$ "\""); |
|
24596 | 224 |
|
225 |
end; |
|
24579 | 226 |
|
227 |
||
30645 | 228 |
(* scan tokens *) |
24579 | 229 |
|
230 |
local |
|
231 |
||
30593 | 232 |
fun token k ss = Token (Symbol_Pos.range ss, (k, Symbol_Pos.content ss)); |
24579 | 233 |
|
30593 | 234 |
val scan_ml = |
27772 | 235 |
(scan_char >> token Char || |
236 |
scan_string >> token String || |
|
237 |
scan_blanks1 >> token Space || |
|
30573 | 238 |
Symbol_Pos.scan_comment !!! >> token Comment || |
27772 | 239 |
Scan.max token_leq |
240 |
(scan_keyword >> token Keyword) |
|
241 |
(scan_word >> token Word || |
|
242 |
scan_real >> token Real || |
|
243 |
scan_int >> token Int || |
|
244 |
scan_longident >> token LongIdent || |
|
245 |
scan_ident >> token Ident || |
|
246 |
scan_typevar >> token TypeVar)); |
|
247 |
||
30645 | 248 |
val scan_antiq = Antiquote.scan || scan_ml >> Antiquote.Text; |
249 |
||
27772 | 250 |
fun recover msg = |
251 |
Scan.many (((not o Symbol.is_blank) andf Symbol.is_regular) o symbol) |
|
252 |
>> (fn cs => [token (Error msg) cs]); |
|
24579 | 253 |
|
254 |
in |
|
255 |
||
24596 | 256 |
fun source src = |
30573 | 257 |
Symbol_Pos.source (Position.line 1) src |
30593 | 258 |
|> Source.source Symbol_Pos.stopper (Scan.bulk (!!! "bad input" scan_ml)) (SOME (false, recover)); |
30591 | 259 |
|
31543
5bef6c7cc819
allow Isabelle symbols within low-level ML source;
wenzelm
parents:
31474
diff
changeset
|
260 |
val tokenize = |
5bef6c7cc819
allow Isabelle symbols within low-level ML source;
wenzelm
parents:
31474
diff
changeset
|
261 |
Source.of_string #> |
5bef6c7cc819
allow Isabelle symbols within low-level ML source;
wenzelm
parents:
31474
diff
changeset
|
262 |
Symbol.source {do_recover = true} #> |
5bef6c7cc819
allow Isabelle symbols within low-level ML source;
wenzelm
parents:
31474
diff
changeset
|
263 |
source #> |
5bef6c7cc819
allow Isabelle symbols within low-level ML source;
wenzelm
parents:
31474
diff
changeset
|
264 |
Source.exhaust; |
24579 | 265 |
|
37198
3af985b10550
replaced ML_Lex.read_antiq by more concise ML_Lex.read, which includes full read/report with explicit position information;
wenzelm
parents:
37195
diff
changeset
|
266 |
fun read pos txt = |
3af985b10550
replaced ML_Lex.read_antiq by more concise ML_Lex.read, which includes full read/report with explicit position information;
wenzelm
parents:
37195
diff
changeset
|
267 |
let |
3af985b10550
replaced ML_Lex.read_antiq by more concise ML_Lex.read, which includes full read/report with explicit position information;
wenzelm
parents:
37195
diff
changeset
|
268 |
val _ = Position.report Markup.ML_source pos; |
3af985b10550
replaced ML_Lex.read_antiq by more concise ML_Lex.read, which includes full read/report with explicit position information;
wenzelm
parents:
37195
diff
changeset
|
269 |
val syms = Symbol_Pos.explode (txt, pos); |
3af985b10550
replaced ML_Lex.read_antiq by more concise ML_Lex.read, which includes full read/report with explicit position information;
wenzelm
parents:
37195
diff
changeset
|
270 |
in |
3af985b10550
replaced ML_Lex.read_antiq by more concise ML_Lex.read, which includes full read/report with explicit position information;
wenzelm
parents:
37195
diff
changeset
|
271 |
(Source.of_list syms |
3af985b10550
replaced ML_Lex.read_antiq by more concise ML_Lex.read, which includes full read/report with explicit position information;
wenzelm
parents:
37195
diff
changeset
|
272 |
|> Source.source Symbol_Pos.stopper (Scan.bulk (!!! "bad input" scan_antiq)) |
3af985b10550
replaced ML_Lex.read_antiq by more concise ML_Lex.read, which includes full read/report with explicit position information;
wenzelm
parents:
37195
diff
changeset
|
273 |
(SOME (false, fn msg => recover msg >> map Antiquote.Text)) |
3af985b10550
replaced ML_Lex.read_antiq by more concise ML_Lex.read, which includes full read/report with explicit position information;
wenzelm
parents:
37195
diff
changeset
|
274 |
|> Source.exhaust |
3af985b10550
replaced ML_Lex.read_antiq by more concise ML_Lex.read, which includes full read/report with explicit position information;
wenzelm
parents:
37195
diff
changeset
|
275 |
|> tap (List.app (Antiquote.report report_token)) |
3af985b10550
replaced ML_Lex.read_antiq by more concise ML_Lex.read, which includes full read/report with explicit position information;
wenzelm
parents:
37195
diff
changeset
|
276 |
|> tap Antiquote.check_nesting |
3af985b10550
replaced ML_Lex.read_antiq by more concise ML_Lex.read, which includes full read/report with explicit position information;
wenzelm
parents:
37195
diff
changeset
|
277 |
|> tap (List.app (fn Antiquote.Text tok => ignore (check_content_of tok) | _ => ()))) |
3af985b10550
replaced ML_Lex.read_antiq by more concise ML_Lex.read, which includes full read/report with explicit position information;
wenzelm
parents:
37195
diff
changeset
|
278 |
handle ERROR msg => |
3af985b10550
replaced ML_Lex.read_antiq by more concise ML_Lex.read, which includes full read/report with explicit position information;
wenzelm
parents:
37195
diff
changeset
|
279 |
cat_error msg ("The error(s) above occurred in ML source" ^ Position.str_of pos) |
3af985b10550
replaced ML_Lex.read_antiq by more concise ML_Lex.read, which includes full read/report with explicit position information;
wenzelm
parents:
37195
diff
changeset
|
280 |
end; |
30645 | 281 |
|
24579 | 282 |
end; |
283 |
||
24596 | 284 |
end; |
24579 | 285 |