author | wenzelm |
Sat, 15 Feb 2014 18:28:18 +0100 | |
changeset 55505 | 2a1ca7f6607b |
parent 55496 | 5d2835453ad3 |
child 55550 | bcc643ac071a |
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 |
|
55505 | 9 |
val keywords: string list |
24579 | 10 |
datatype token_kind = |
24596 | 11 |
Keyword | Ident | LongIdent | TypeVar | Word | Int | Real | Char | String | |
24579 | 12 |
Space | Comment | Error of string | EOF |
13 |
eqtype token |
|
27732 | 14 |
val stopper: token Scan.stopper |
24596 | 15 |
val is_regular: token -> bool |
16 |
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
|
17 |
val set_range: Position.range -> token -> token |
30636 | 18 |
val pos_of: token -> Position.T |
31474 | 19 |
val end_pos_of: token -> Position.T |
24579 | 20 |
val kind_of: token -> token_kind |
27817 | 21 |
val content_of: token -> string |
31426 | 22 |
val check_content_of: token -> string |
31332 | 23 |
val flatten: token list -> string |
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 |
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
|
28 |
val read: Position.T -> Symbol_Pos.text -> token Antiquote.antiquote list |
24579 | 29 |
end; |
30 |
||
31 |
structure ML_Lex: ML_LEX = |
|
32 |
struct |
|
33 |
||
55505 | 34 |
(** keywords **) |
35 |
||
36 |
val keywords = |
|
37 |
["#", "(", ")", ",", "->", "...", ":", ":>", ";", "=", "=>", "[", |
|
38 |
"]", "_", "{", "|", "}", "abstype", "and", "andalso", "as", "case", |
|
39 |
"datatype", "do", "else", "end", "eqtype", "exception", "fn", "fun", |
|
40 |
"functor", "handle", "if", "in", "include", "infix", "infixr", |
|
41 |
"let", "local", "nonfix", "of", "op", "open", "orelse", "raise", |
|
42 |
"rec", "sharing", "sig", "signature", "struct", "structure", "then", |
|
43 |
"type", "val", "where", "while", "with", "withtype"]; |
|
44 |
||
45 |
val keywords2 = |
|
46 |
["case", "do", "else", "end", "if", "in", "let", "local", "of", |
|
47 |
"sig", "struct", "then", "while", "with"]; |
|
48 |
||
49 |
val keywords3 = |
|
50 |
["handle", "open", "raise"]; |
|
51 |
||
52 |
val lexicon = Scan.make_lexicon (map raw_explode keywords); |
|
53 |
||
54 |
||
55 |
||
24579 | 56 |
(** tokens **) |
57 |
||
58 |
(* datatype token *) |
|
59 |
||
60 |
datatype token_kind = |
|
24596 | 61 |
Keyword | Ident | LongIdent | TypeVar | Word | Int | Real | Char | String | |
24579 | 62 |
Space | Comment | Error of string | EOF; |
63 |
||
27772 | 64 |
datatype token = Token of Position.range * (token_kind * string); |
65 |
||
66 |
||
67 |
(* position *) |
|
68 |
||
30683
e8ac1f9d9469
datatype antiquote: maintain original Position.range, which is eventually attached to the resulting ML tokens;
wenzelm
parents:
30645
diff
changeset
|
69 |
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
|
70 |
|
30636 | 71 |
fun pos_of (Token ((pos, _), _)) = pos; |
72 |
fun end_pos_of (Token ((_, pos), _)) = pos; |
|
24579 | 73 |
|
74 |
||
24596 | 75 |
(* control tokens *) |
24579 | 76 |
|
27772 | 77 |
fun mk_eof pos = Token ((pos, Position.none), (EOF, "")); |
78 |
val eof = mk_eof Position.none; |
|
24579 | 79 |
|
80 |
fun is_eof (Token (_, (EOF, _))) = true |
|
81 |
| is_eof _ = false; |
|
82 |
||
27772 | 83 |
val stopper = |
30636 | 84 |
Scan.stopper (fn [] => eof | toks => mk_eof (end_pos_of (List.last toks))) is_eof; |
27772 | 85 |
|
24579 | 86 |
|
27772 | 87 |
(* token content *) |
88 |
||
31426 | 89 |
fun kind_of (Token (_, (k, _))) = k; |
90 |
||
27817 | 91 |
fun content_of (Token (_, (_, x))) = x; |
92 |
fun token_leq (tok, tok') = content_of tok <= content_of tok'; |
|
27772 | 93 |
|
55505 | 94 |
fun is_keyword (Token (_, (Keyword, _))) = true |
95 |
| is_keyword _ = false; |
|
96 |
||
97 |
fun is_delimiter (Token (_, (Keyword, x))) = not (Symbol.is_ascii_identifier x) |
|
98 |
| is_delimiter _ = false; |
|
99 |
||
41502
967cbbc77abd
refined ML_Lex.flatten: ensure proper separation of tokens;
wenzelm
parents:
40627
diff
changeset
|
100 |
fun is_regular (Token (_, (Error _, _))) = false |
967cbbc77abd
refined ML_Lex.flatten: ensure proper separation of tokens;
wenzelm
parents:
40627
diff
changeset
|
101 |
| is_regular (Token (_, (EOF, _))) = false |
967cbbc77abd
refined ML_Lex.flatten: ensure proper separation of tokens;
wenzelm
parents:
40627
diff
changeset
|
102 |
| is_regular _ = true; |
967cbbc77abd
refined ML_Lex.flatten: ensure proper separation of tokens;
wenzelm
parents:
40627
diff
changeset
|
103 |
|
967cbbc77abd
refined ML_Lex.flatten: ensure proper separation of tokens;
wenzelm
parents:
40627
diff
changeset
|
104 |
fun is_improper (Token (_, (Space, _))) = true |
967cbbc77abd
refined ML_Lex.flatten: ensure proper separation of tokens;
wenzelm
parents:
40627
diff
changeset
|
105 |
| is_improper (Token (_, (Comment, _))) = true |
967cbbc77abd
refined ML_Lex.flatten: ensure proper separation of tokens;
wenzelm
parents:
40627
diff
changeset
|
106 |
| is_improper _ = false; |
967cbbc77abd
refined ML_Lex.flatten: ensure proper separation of tokens;
wenzelm
parents:
40627
diff
changeset
|
107 |
|
40337
d25bbb5f1c9e
warn in correlation with report -- avoid spurious message duplicates;
wenzelm
parents:
40319
diff
changeset
|
108 |
fun warn tok = |
d25bbb5f1c9e
warn in correlation with report -- avoid spurious message duplicates;
wenzelm
parents:
40319
diff
changeset
|
109 |
(case tok of |
d25bbb5f1c9e
warn in correlation with report -- avoid spurious message duplicates;
wenzelm
parents:
40319
diff
changeset
|
110 |
Token (_, (Keyword, ":>")) => |
d25bbb5f1c9e
warn in correlation with report -- avoid spurious message duplicates;
wenzelm
parents:
40319
diff
changeset
|
111 |
warning ("Opaque signature matching (:>) fails to work with ML pretty printing --\n\ |
d25bbb5f1c9e
warn in correlation with report -- avoid spurious message duplicates;
wenzelm
parents:
40319
diff
changeset
|
112 |
\prefer non-opaque matching (:) possibly with abstype" ^ |
48992 | 113 |
Position.here (pos_of tok)) |
40337
d25bbb5f1c9e
warn in correlation with report -- avoid spurious message duplicates;
wenzelm
parents:
40319
diff
changeset
|
114 |
| _ => ()); |
d25bbb5f1c9e
warn in correlation with report -- avoid spurious message duplicates;
wenzelm
parents:
40319
diff
changeset
|
115 |
|
31426 | 116 |
fun check_content_of tok = |
30636 | 117 |
(case kind_of tok of |
30645 | 118 |
Error msg => error msg |
40337
d25bbb5f1c9e
warn in correlation with report -- avoid spurious message duplicates;
wenzelm
parents:
40319
diff
changeset
|
119 |
| _ => content_of tok); |
30636 | 120 |
|
41502
967cbbc77abd
refined ML_Lex.flatten: ensure proper separation of tokens;
wenzelm
parents:
40627
diff
changeset
|
121 |
fun flatten_content (tok :: (toks as tok' :: _)) = |
967cbbc77abd
refined ML_Lex.flatten: ensure proper separation of tokens;
wenzelm
parents:
40627
diff
changeset
|
122 |
Symbol.escape (check_content_of tok) :: |
967cbbc77abd
refined ML_Lex.flatten: ensure proper separation of tokens;
wenzelm
parents:
40627
diff
changeset
|
123 |
(if is_improper tok orelse is_improper tok' then flatten_content toks |
967cbbc77abd
refined ML_Lex.flatten: ensure proper separation of tokens;
wenzelm
parents:
40627
diff
changeset
|
124 |
else Symbol.space :: flatten_content toks) |
967cbbc77abd
refined ML_Lex.flatten: ensure proper separation of tokens;
wenzelm
parents:
40627
diff
changeset
|
125 |
| flatten_content toks = map (Symbol.escape o check_content_of) toks; |
31332 | 126 |
|
41502
967cbbc77abd
refined ML_Lex.flatten: ensure proper separation of tokens;
wenzelm
parents:
40627
diff
changeset
|
127 |
val flatten = implode o flatten_content; |
24596 | 128 |
|
129 |
||
30614 | 130 |
(* markup *) |
131 |
||
37195 | 132 |
local |
133 |
||
30645 | 134 |
val token_kind_markup = |
55505 | 135 |
fn Keyword => (Markup.empty, "") |
48768
abc45de5bb22
clarified "bad" markup: proper body text, invent missing serial on Scala side (counting backwards);
wenzelm
parents:
48756
diff
changeset
|
136 |
| Ident => (Markup.empty, "") |
abc45de5bb22
clarified "bad" markup: proper body text, invent missing serial on Scala side (counting backwards);
wenzelm
parents:
48756
diff
changeset
|
137 |
| LongIdent => (Markup.empty, "") |
50201
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents:
48992
diff
changeset
|
138 |
| TypeVar => (Markup.ML_tvar, "") |
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents:
48992
diff
changeset
|
139 |
| Word => (Markup.ML_numeral, "") |
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents:
48992
diff
changeset
|
140 |
| Int => (Markup.ML_numeral, "") |
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents:
48992
diff
changeset
|
141 |
| Real => (Markup.ML_numeral, "") |
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents:
48992
diff
changeset
|
142 |
| Char => (Markup.ML_char, "") |
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents:
48992
diff
changeset
|
143 |
| String => (Markup.ML_string, "") |
48768
abc45de5bb22
clarified "bad" markup: proper body text, invent missing serial on Scala side (counting backwards);
wenzelm
parents:
48756
diff
changeset
|
144 |
| Space => (Markup.empty, "") |
50201
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents:
48992
diff
changeset
|
145 |
| Comment => (Markup.ML_comment, "") |
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents:
48992
diff
changeset
|
146 |
| Error msg => (Markup.bad, msg) |
48768
abc45de5bb22
clarified "bad" markup: proper body text, invent missing serial on Scala side (counting backwards);
wenzelm
parents:
48756
diff
changeset
|
147 |
| EOF => (Markup.empty, ""); |
30614 | 148 |
|
37195 | 149 |
in |
150 |
||
55505 | 151 |
fun report_of_token (tok as Token ((pos, _), (kind, x))) = |
152 |
let |
|
153 |
val (markup, txt) = |
|
154 |
if not (is_keyword tok) then token_kind_markup kind |
|
155 |
else if is_delimiter tok then (Markup.ML_delimiter, "") |
|
156 |
else if member (op =) keywords2 x then (Markup.ML_keyword2, "") |
|
157 |
else if member (op =) keywords3 x then (Markup.ML_keyword3, "") |
|
158 |
else (Markup.ML_keyword1, ""); |
|
48768
abc45de5bb22
clarified "bad" markup: proper body text, invent missing serial on Scala side (counting backwards);
wenzelm
parents:
48756
diff
changeset
|
159 |
in ((pos, markup), txt) end; |
37195 | 160 |
|
161 |
end; |
|
30614 | 162 |
|
163 |
||
24579 | 164 |
|
165 |
(** scanners **) |
|
166 |
||
30573 | 167 |
open Basic_Symbol_Pos; |
24579 | 168 |
|
55105 | 169 |
val err_prefix = "SML lexical error: "; |
170 |
||
171 |
fun !!! msg = Symbol_Pos.!!! (fn () => err_prefix ^ msg); |
|
24579 | 172 |
|
173 |
||
174 |
(* identifiers *) |
|
175 |
||
24596 | 176 |
local |
177 |
||
24579 | 178 |
val scan_letdigs = |
55496 | 179 |
Scan.many (Symbol.is_ascii_letdig o Symbol_Pos.symbol); |
24579 | 180 |
|
40525
14a2e686bdac
eliminated slightly odd pervasive Symbol_Pos.symbol;
wenzelm
parents:
40523
diff
changeset
|
181 |
val scan_alphanumeric = |
14a2e686bdac
eliminated slightly odd pervasive Symbol_Pos.symbol;
wenzelm
parents:
40523
diff
changeset
|
182 |
Scan.one (Symbol.is_ascii_letter o Symbol_Pos.symbol) -- scan_letdigs >> op ::; |
24579 | 183 |
|
40525
14a2e686bdac
eliminated slightly odd pervasive Symbol_Pos.symbol;
wenzelm
parents:
40523
diff
changeset
|
184 |
val scan_symbolic = |
40627
becf5d5187cc
renamed raw "explode" function to "raw_explode" to emphasize its meaning;
wenzelm
parents:
40525
diff
changeset
|
185 |
Scan.many1 (member (op =) (raw_explode "!#$%&*+-/:<=>?@\\^`|~") o Symbol_Pos.symbol); |
24579 | 186 |
|
24596 | 187 |
in |
188 |
||
24579 | 189 |
val scan_ident = scan_alphanumeric || scan_symbolic; |
190 |
||
55496 | 191 |
val scan_long_ident = |
27772 | 192 |
(Scan.repeat1 (scan_alphanumeric @@@ $$$ ".") >> flat) @@@ (scan_ident || $$$ "="); |
24579 | 193 |
|
55496 | 194 |
val scan_type_var = $$$ "'" @@@ scan_letdigs; |
24579 | 195 |
|
24596 | 196 |
end; |
24579 | 197 |
|
198 |
||
199 |
(* numerals *) |
|
200 |
||
24596 | 201 |
local |
202 |
||
40525
14a2e686bdac
eliminated slightly odd pervasive Symbol_Pos.symbol;
wenzelm
parents:
40523
diff
changeset
|
203 |
val scan_dec = Scan.many1 (Symbol.is_ascii_digit o Symbol_Pos.symbol); |
14a2e686bdac
eliminated slightly odd pervasive Symbol_Pos.symbol;
wenzelm
parents:
40523
diff
changeset
|
204 |
val scan_hex = Scan.many1 (Symbol.is_ascii_hex o Symbol_Pos.symbol); |
27772 | 205 |
val scan_sign = Scan.optional ($$$ "~") []; |
206 |
val scan_decint = scan_sign @@@ scan_dec; |
|
55496 | 207 |
val scan_exp = ($$$ "E" || $$$ "e") @@@ scan_decint; |
24579 | 208 |
|
24596 | 209 |
in |
210 |
||
27772 | 211 |
val scan_word = |
212 |
$$$ "0" @@@ $$$ "w" @@@ $$$ "x" @@@ scan_hex || |
|
213 |
$$$ "0" @@@ $$$ "w" @@@ scan_dec; |
|
24579 | 214 |
|
27772 | 215 |
val scan_int = scan_sign @@@ ($$$ "0" @@@ $$$ "x" @@@ scan_hex || scan_dec); |
24579 | 216 |
|
217 |
val scan_real = |
|
27772 | 218 |
scan_decint @@@ $$$ "." @@@ scan_dec @@@ Scan.optional scan_exp [] || |
219 |
scan_decint @@@ scan_exp; |
|
24579 | 220 |
|
24596 | 221 |
end; |
222 |
||
24579 | 223 |
|
224 |
(* chars and strings *) |
|
225 |
||
55496 | 226 |
val scan_blanks1 = Scan.many1 (Symbol.is_ascii_blank o Symbol_Pos.symbol); |
227 |
||
24596 | 228 |
local |
229 |
||
230 |
val scan_escape = |
|
40627
becf5d5187cc
renamed raw "explode" function to "raw_explode" to emphasize its meaning;
wenzelm
parents:
40525
diff
changeset
|
231 |
Scan.one (member (op =) (raw_explode "\"\\abtnvfr") o Symbol_Pos.symbol) >> single || |
27772 | 232 |
$$$ "^" @@@ (Scan.one (fn (s, _) => ord "@" <= ord s andalso ord s <= ord "_") >> single) || |
40525
14a2e686bdac
eliminated slightly odd pervasive Symbol_Pos.symbol;
wenzelm
parents:
40523
diff
changeset
|
233 |
Scan.one (Symbol.is_ascii_digit o Symbol_Pos.symbol) -- |
14a2e686bdac
eliminated slightly odd pervasive Symbol_Pos.symbol;
wenzelm
parents:
40523
diff
changeset
|
234 |
Scan.one (Symbol.is_ascii_digit o Symbol_Pos.symbol) -- |
14a2e686bdac
eliminated slightly odd pervasive Symbol_Pos.symbol;
wenzelm
parents:
40523
diff
changeset
|
235 |
Scan.one (Symbol.is_ascii_digit o Symbol_Pos.symbol) >> (fn ((a, b), c) => [a, b, c]); |
24596 | 236 |
|
237 |
val scan_str = |
|
30600
de241396389c
allow non-printable symbols within string tokens;
wenzelm
parents:
30593
diff
changeset
|
238 |
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
|
239 |
(not (Symbol.is_char s) orelse Symbol.is_printable s)) >> single || |
27772 | 240 |
$$$ "\\" @@@ !!! "bad escape character in string" scan_escape; |
24596 | 241 |
|
27772 | 242 |
val scan_gap = $$$ "\\" @@@ scan_blanks1 @@@ $$$ "\\"; |
243 |
val scan_gaps = Scan.repeat scan_gap >> flat; |
|
24579 | 244 |
|
24596 | 245 |
in |
24579 | 246 |
|
247 |
val scan_char = |
|
27772 | 248 |
$$$ "#" @@@ $$$ "\"" @@@ scan_gaps @@@ scan_str @@@ scan_gaps @@@ $$$ "\""; |
24579 | 249 |
|
48743
a72f8ffecf31
refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents:
45666
diff
changeset
|
250 |
val recover_char = |
a72f8ffecf31
refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents:
45666
diff
changeset
|
251 |
$$$ "#" @@@ $$$ "\"" @@@ scan_gaps @@@ Scan.optional (Scan.permissive scan_str @@@ scan_gaps) []; |
a72f8ffecf31
refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents:
45666
diff
changeset
|
252 |
|
24579 | 253 |
val scan_string = |
55103 | 254 |
Scan.ahead ($$ "\"") |-- |
255 |
!!! "unclosed string literal" |
|
256 |
($$$ "\"" @@@ (Scan.repeat (scan_gap || scan_str) >> flat) @@@ $$$ "\""); |
|
24596 | 257 |
|
48743
a72f8ffecf31
refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents:
45666
diff
changeset
|
258 |
val recover_string = |
a72f8ffecf31
refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents:
45666
diff
changeset
|
259 |
$$$ "\"" @@@ (Scan.repeat (scan_gap || Scan.permissive scan_str) >> flat); |
a72f8ffecf31
refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents:
45666
diff
changeset
|
260 |
|
24596 | 261 |
end; |
24579 | 262 |
|
263 |
||
30645 | 264 |
(* scan tokens *) |
24579 | 265 |
|
266 |
local |
|
267 |
||
30593 | 268 |
fun token k ss = Token (Symbol_Pos.range ss, (k, Symbol_Pos.content ss)); |
24579 | 269 |
|
30593 | 270 |
val scan_ml = |
27772 | 271 |
(scan_char >> token Char || |
272 |
scan_string >> token String || |
|
273 |
scan_blanks1 >> token Space || |
|
55105 | 274 |
Symbol_Pos.scan_comment err_prefix >> token Comment || |
27772 | 275 |
Scan.max token_leq |
55505 | 276 |
(Scan.literal lexicon >> token Keyword) |
27772 | 277 |
(scan_word >> token Word || |
278 |
scan_real >> token Real || |
|
279 |
scan_int >> token Int || |
|
55496 | 280 |
scan_long_ident >> token LongIdent || |
27772 | 281 |
scan_ident >> token Ident || |
55496 | 282 |
scan_type_var >> token TypeVar)); |
27772 | 283 |
|
53167 | 284 |
val scan_antiq = Antiquote.scan_antiq >> Antiquote.Antiq || scan_ml >> Antiquote.Text; |
30645 | 285 |
|
27772 | 286 |
fun recover msg = |
48743
a72f8ffecf31
refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents:
45666
diff
changeset
|
287 |
(recover_char || |
a72f8ffecf31
refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents:
45666
diff
changeset
|
288 |
recover_string || |
a72f8ffecf31
refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents:
45666
diff
changeset
|
289 |
Symbol_Pos.recover_comment || |
a72f8ffecf31
refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents:
45666
diff
changeset
|
290 |
Scan.one (Symbol.not_eof o Symbol_Pos.symbol) >> single) |
a72f8ffecf31
refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents:
45666
diff
changeset
|
291 |
>> (single o token (Error msg)); |
24579 | 292 |
|
293 |
in |
|
294 |
||
24596 | 295 |
fun source src = |
30573 | 296 |
Symbol_Pos.source (Position.line 1) src |
30593 | 297 |
|> Source.source Symbol_Pos.stopper (Scan.bulk (!!! "bad input" scan_ml)) (SOME (false, recover)); |
30591 | 298 |
|
40523
1050315f6ee2
simplified/robustified treatment of malformed symbols, which are now fully internalized (total Symbol.explode etc.);
wenzelm
parents:
40337
diff
changeset
|
299 |
val tokenize = Source.of_string #> Symbol.source #> source #> Source.exhaust; |
24579 | 300 |
|
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
|
301 |
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
|
302 |
let |
50201
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents:
48992
diff
changeset
|
303 |
val _ = Position.report pos Markup.ML_source; |
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
|
304 |
val syms = Symbol_Pos.explode (txt, pos); |
37209
1c8cf0048934
terminate ML compiler input produced by ML_Lex.read (cf. 85e864045497);
wenzelm
parents:
37198
diff
changeset
|
305 |
val termination = |
1c8cf0048934
terminate ML compiler input produced by ML_Lex.read (cf. 85e864045497);
wenzelm
parents:
37198
diff
changeset
|
306 |
if null syms then [] |
1c8cf0048934
terminate ML compiler input produced by ML_Lex.read (cf. 85e864045497);
wenzelm
parents:
37198
diff
changeset
|
307 |
else |
1c8cf0048934
terminate ML compiler input produced by ML_Lex.read (cf. 85e864045497);
wenzelm
parents:
37198
diff
changeset
|
308 |
let |
1c8cf0048934
terminate ML compiler input produced by ML_Lex.read (cf. 85e864045497);
wenzelm
parents:
37198
diff
changeset
|
309 |
val pos1 = List.last syms |-> Position.advance; |
1c8cf0048934
terminate ML compiler input produced by ML_Lex.read (cf. 85e864045497);
wenzelm
parents:
37198
diff
changeset
|
310 |
val pos2 = Position.advance Symbol.space pos1; |
1c8cf0048934
terminate ML compiler input produced by ML_Lex.read (cf. 85e864045497);
wenzelm
parents:
37198
diff
changeset
|
311 |
in [Antiquote.Text (Token (Position.range pos1 pos2, (Space, Symbol.space)))] end; |
1c8cf0048934
terminate ML compiler input produced by ML_Lex.read (cf. 85e864045497);
wenzelm
parents:
37198
diff
changeset
|
312 |
val input = |
1c8cf0048934
terminate ML compiler input produced by ML_Lex.read (cf. 85e864045497);
wenzelm
parents:
37198
diff
changeset
|
313 |
(Source.of_list syms |
1c8cf0048934
terminate ML compiler input produced by ML_Lex.read (cf. 85e864045497);
wenzelm
parents:
37198
diff
changeset
|
314 |
|> Source.source Symbol_Pos.stopper (Scan.bulk (!!! "bad input" scan_antiq)) |
1c8cf0048934
terminate ML compiler input produced by ML_Lex.read (cf. 85e864045497);
wenzelm
parents:
37198
diff
changeset
|
315 |
(SOME (false, fn msg => recover msg >> map Antiquote.Text)) |
1c8cf0048934
terminate ML compiler input produced by ML_Lex.read (cf. 85e864045497);
wenzelm
parents:
37198
diff
changeset
|
316 |
|> Source.exhaust |
48768
abc45de5bb22
clarified "bad" markup: proper body text, invent missing serial on Scala side (counting backwards);
wenzelm
parents:
48756
diff
changeset
|
317 |
|> tap (Position.reports_text o Antiquote.reports_of (single o report_of_token)) |
55103 | 318 |
|> tap (List.app (fn Antiquote.Text tok => (check_content_of tok; warn tok) | _ => ()))); |
37209
1c8cf0048934
terminate ML compiler input produced by ML_Lex.read (cf. 85e864045497);
wenzelm
parents:
37198
diff
changeset
|
319 |
in input @ termination end; |
30645 | 320 |
|
24579 | 321 |
end; |
322 |
||
24596 | 323 |
end; |
24579 | 324 |