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