author | wenzelm |
Sat, 06 Jun 2009 19:58:11 +0200 | |
changeset 31474 | 0ae32184bde0 |
parent 31426 | 5c9dbd511510 |
child 31543 | 5bef6c7cc819 |
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 |
30645 | 29 |
val read_antiq: Symbol_Pos.T list * Position.T -> 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 |
||
30645 | 91 |
val token_kind_markup = |
92 |
fn Keyword => Markup.ML_keyword |
|
93 |
| Ident => Markup.ML_ident |
|
94 |
| LongIdent => Markup.ML_ident |
|
95 |
| TypeVar => Markup.ML_tvar |
|
96 |
| Word => Markup.ML_numeral |
|
97 |
| Int => Markup.ML_numeral |
|
98 |
| Real => Markup.ML_numeral |
|
99 |
| Char => Markup.ML_char |
|
100 |
| String => Markup.ML_string |
|
101 |
| Space => Markup.none |
|
102 |
| Comment => Markup.ML_comment |
|
103 |
| Error _ => Markup.ML_malformed |
|
104 |
| EOF => Markup.none; |
|
30614 | 105 |
|
30645 | 106 |
fun report_token (Token ((pos, _), (kind, _))) = |
107 |
Position.report (token_kind_markup kind) pos; |
|
30614 | 108 |
|
109 |
||
24579 | 110 |
|
111 |
(** scanners **) |
|
112 |
||
30573 | 113 |
open Basic_Symbol_Pos; |
24579 | 114 |
|
30573 | 115 |
fun !!! msg = Symbol_Pos.!!! ("SML lexical error: " ^ msg); |
24579 | 116 |
|
117 |
||
27772 | 118 |
(* blanks *) |
24579 | 119 |
|
27772 | 120 |
val scan_blank = Scan.one (Symbol.is_ascii_blank o symbol); |
121 |
val scan_blanks1 = Scan.repeat1 scan_blank; |
|
24579 | 122 |
|
123 |
||
124 |
(* keywords *) |
|
125 |
||
126 |
val keywords = ["#", "(", ")", ",", "->", "...", ":", ":>", ";", "=", |
|
127 |
"=>", "[", "]", "_", "{", "|", "}", "abstype", "and", "andalso", "as", |
|
128 |
"case", "datatype", "do", "else", "end", "eqtype", "exception", "fn", |
|
129 |
"fun", "functor", "handle", "if", "in", "include", "infix", "infixr", |
|
130 |
"let", "local", "nonfix", "of", "op", "open", "orelse", "raise", "rec", |
|
131 |
"sharing", "sig", "signature", "struct", "structure", "then", "type", |
|
132 |
"val", "where", "while", "with", "withtype"]; |
|
133 |
||
27772 | 134 |
val lex = Scan.make_lexicon (map explode keywords); |
135 |
fun scan_keyword x = Scan.literal lex x; |
|
24579 | 136 |
|
137 |
||
138 |
(* identifiers *) |
|
139 |
||
24596 | 140 |
local |
141 |
||
24579 | 142 |
val scan_letdigs = |
27772 | 143 |
Scan.many ((Symbol.is_ascii_letter orf Symbol.is_ascii_digit orf Symbol.is_ascii_quasi) o symbol); |
24579 | 144 |
|
27772 | 145 |
val scan_alphanumeric = Scan.one (Symbol.is_ascii_letter o symbol) -- scan_letdigs >> op ::; |
24579 | 146 |
|
27772 | 147 |
val scan_symbolic = Scan.many1 (member (op =) (explode "!#$%&*+-/:<=>?@\\^`|~") o symbol); |
24579 | 148 |
|
24596 | 149 |
in |
150 |
||
24579 | 151 |
val scan_ident = scan_alphanumeric || scan_symbolic; |
152 |
||
153 |
val scan_longident = |
|
27772 | 154 |
(Scan.repeat1 (scan_alphanumeric @@@ $$$ ".") >> flat) @@@ (scan_ident || $$$ "="); |
24579 | 155 |
|
27772 | 156 |
val scan_typevar = $$$ "'" @@@ scan_letdigs; |
24579 | 157 |
|
24596 | 158 |
end; |
24579 | 159 |
|
160 |
||
161 |
(* numerals *) |
|
162 |
||
24596 | 163 |
local |
164 |
||
27772 | 165 |
val scan_dec = Scan.many1 (Symbol.is_ascii_digit o symbol); |
166 |
val scan_hex = Scan.many1 (Symbol.is_ascii_hex o symbol); |
|
167 |
val scan_sign = Scan.optional ($$$ "~") []; |
|
168 |
val scan_decint = scan_sign @@@ scan_dec; |
|
24579 | 169 |
|
24596 | 170 |
in |
171 |
||
27772 | 172 |
val scan_word = |
173 |
$$$ "0" @@@ $$$ "w" @@@ $$$ "x" @@@ scan_hex || |
|
174 |
$$$ "0" @@@ $$$ "w" @@@ scan_dec; |
|
24579 | 175 |
|
27772 | 176 |
val scan_int = scan_sign @@@ ($$$ "0" @@@ $$$ "x" @@@ scan_hex || scan_dec); |
24579 | 177 |
|
27772 | 178 |
val scan_exp = ($$$ "E" || $$$ "e") @@@ scan_decint; |
24579 | 179 |
|
180 |
val scan_real = |
|
27772 | 181 |
scan_decint @@@ $$$ "." @@@ scan_dec @@@ Scan.optional scan_exp [] || |
182 |
scan_decint @@@ scan_exp; |
|
24579 | 183 |
|
24596 | 184 |
end; |
185 |
||
24579 | 186 |
|
187 |
(* chars and strings *) |
|
188 |
||
24596 | 189 |
local |
190 |
||
191 |
val scan_escape = |
|
27772 | 192 |
Scan.one (member (op =) (explode "\"\\abtnvfr") o symbol) >> single || |
193 |
$$$ "^" @@@ (Scan.one (fn (s, _) => ord "@" <= ord s andalso ord s <= ord "_") >> single) || |
|
194 |
Scan.one (Symbol.is_ascii_digit o symbol) -- |
|
195 |
Scan.one (Symbol.is_ascii_digit o symbol) -- |
|
196 |
Scan.one (Symbol.is_ascii_digit o symbol) >> (fn ((a, b), c) => [a, b, c]); |
|
24596 | 197 |
|
198 |
val scan_str = |
|
30600
de241396389c
allow non-printable symbols within string tokens;
wenzelm
parents:
30593
diff
changeset
|
199 |
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
|
200 |
(not (Symbol.is_char s) orelse Symbol.is_printable s)) >> single || |
27772 | 201 |
$$$ "\\" @@@ !!! "bad escape character in string" scan_escape; |
24596 | 202 |
|
27772 | 203 |
val scan_gap = $$$ "\\" @@@ scan_blanks1 @@@ $$$ "\\"; |
204 |
val scan_gaps = Scan.repeat scan_gap >> flat; |
|
24579 | 205 |
|
24596 | 206 |
in |
24579 | 207 |
|
208 |
val scan_char = |
|
27772 | 209 |
$$$ "#" @@@ $$$ "\"" @@@ scan_gaps @@@ scan_str @@@ scan_gaps @@@ $$$ "\""; |
24579 | 210 |
|
211 |
val scan_string = |
|
27772 | 212 |
$$$ "\"" @@@ !!! "missing quote at end of string" |
213 |
((Scan.repeat (scan_gap || scan_str) >> flat) @@@ $$$ "\""); |
|
24596 | 214 |
|
215 |
end; |
|
24579 | 216 |
|
217 |
||
30645 | 218 |
(* scan tokens *) |
24579 | 219 |
|
220 |
local |
|
221 |
||
30593 | 222 |
fun token k ss = Token (Symbol_Pos.range ss, (k, Symbol_Pos.content ss)); |
24579 | 223 |
|
30593 | 224 |
val scan_ml = |
27772 | 225 |
(scan_char >> token Char || |
226 |
scan_string >> token String || |
|
227 |
scan_blanks1 >> token Space || |
|
30573 | 228 |
Symbol_Pos.scan_comment !!! >> token Comment || |
27772 | 229 |
Scan.max token_leq |
230 |
(scan_keyword >> token Keyword) |
|
231 |
(scan_word >> token Word || |
|
232 |
scan_real >> token Real || |
|
233 |
scan_int >> token Int || |
|
234 |
scan_longident >> token LongIdent || |
|
235 |
scan_ident >> token Ident || |
|
236 |
scan_typevar >> token TypeVar)); |
|
237 |
||
30645 | 238 |
val scan_antiq = Antiquote.scan || scan_ml >> Antiquote.Text; |
239 |
||
27772 | 240 |
fun recover msg = |
241 |
Scan.many (((not o Symbol.is_blank) andf Symbol.is_regular) o symbol) |
|
242 |
>> (fn cs => [token (Error msg) cs]); |
|
24579 | 243 |
|
244 |
in |
|
245 |
||
24596 | 246 |
fun source src = |
30573 | 247 |
Symbol_Pos.source (Position.line 1) src |
30593 | 248 |
|> Source.source Symbol_Pos.stopper (Scan.bulk (!!! "bad input" scan_ml)) (SOME (false, recover)); |
30591 | 249 |
|
250 |
val tokenize = Source.of_string #> source #> Source.exhaust; |
|
24579 | 251 |
|
30645 | 252 |
fun read_antiq (syms, pos) = |
253 |
(Source.of_list syms |
|
254 |
|> Source.source Symbol_Pos.stopper (Scan.bulk (!!! "bad input" scan_antiq)) |
|
255 |
(SOME (false, fn msg => recover msg >> map Antiquote.Text)) |
|
256 |
|> Source.exhaust |
|
257 |
|> tap (List.app (Antiquote.report report_token)) |
|
258 |
|> tap Antiquote.check_nesting |
|
31426 | 259 |
|> tap (List.app (fn Antiquote.Text tok => ignore (check_content_of tok) | _ => ()))) |
30645 | 260 |
handle ERROR msg => |
261 |
cat_error msg ("The error(s) above occurred in ML source" ^ Position.str_of pos); |
|
262 |
||
24579 | 263 |
end; |
264 |
||
24596 | 265 |
end; |
24579 | 266 |