author | wenzelm |
Sat, 15 Sep 2007 19:25:19 +0200 | |
changeset 24577 | c6acb6d79757 |
parent 24022 | ab76c73b3b58 |
child 25579 | 22869d9d545b |
permissions | -rw-r--r-- |
5825 | 1 |
(* Title: Pure/Isar/outer_lex.ML |
2 |
ID: $Id$ |
|
3 |
Author: Markus Wenzel, TU Muenchen |
|
4 |
||
5 |
Outer lexical syntax for Isabelle/Isar. |
|
6 |
*) |
|
7 |
||
8 |
signature OUTER_LEX = |
|
9 |
sig |
|
10 |
datatype token_kind = |
|
23729
d1ba656978c5
separated Malformed (symbolic char) from Error (bad input);
wenzelm
parents:
23721
diff
changeset
|
11 |
Command | Keyword | Ident | LongIdent | SymIdent | Var | TypeIdent | TypeVar | Nat | |
23788
54ce229dc858
Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents:
23729
diff
changeset
|
12 |
String | AltString | Verbatim | Space | Comment | Malformed | Error of string | Sync | EOF |
15143
05b5995f214e
Make token an eqtype to assist reconstructing input
aspinall
parents:
14991
diff
changeset
|
13 |
eqtype token |
5825 | 14 |
val str_of_kind: token_kind -> string |
15 |
val stopper: token * (token -> bool) |
|
6859 | 16 |
val not_sync: token -> bool |
5825 | 17 |
val not_eof: token -> bool |
18 |
val position_of: token -> Position.T |
|
19 |
val pos_of: token -> string |
|
23721 | 20 |
val kind_of: token -> token_kind |
5825 | 21 |
val is_kind: token_kind -> token -> bool |
7026 | 22 |
val keyword_with: (string -> bool) -> token -> bool |
16029 | 23 |
val ident_with: (string -> bool) -> token -> bool |
5825 | 24 |
val is_proper: token -> bool |
9130 | 25 |
val is_semicolon: token -> bool |
17069 | 26 |
val is_comment: token -> bool |
8580 | 27 |
val is_begin_ignore: token -> bool |
28 |
val is_end_ignore: token -> bool |
|
17069 | 29 |
val is_blank: token -> bool |
8651 | 30 |
val is_newline: token -> bool |
14991 | 31 |
val unparse: token -> string |
23788
54ce229dc858
Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents:
23729
diff
changeset
|
32 |
val text_of: token -> string * string |
5825 | 33 |
val val_of: token -> string |
5876 | 34 |
val is_sid: string -> bool |
9130 | 35 |
val !!! : string -> (Position.T * 'a -> 'b) -> Position.T * 'a -> 'b |
36 |
val incr_line: ('a -> 'b * 'c) -> Position.T * 'a -> 'b * (Position.T * 'c) |
|
37 |
val keep_line: ('a -> 'b * 'c) -> Position.T * 'a -> 'b * (Position.T * 'c) |
|
38 |
val scan_blank: Position.T * Symbol.symbol list |
|
39 |
-> Symbol.symbol * (Position.T * Symbol.symbol list) |
|
40 |
val scan_string: Position.T * Symbol.symbol list -> string * (Position.T * Symbol.symbol list) |
|
7026 | 41 |
val scan: (Scan.lexicon * Scan.lexicon) -> |
5825 | 42 |
Position.T * Symbol.symbol list -> token * (Position.T * Symbol.symbol list) |
23721 | 43 |
val source: bool option -> (unit -> Scan.lexicon * Scan.lexicon) -> |
7026 | 44 |
Position.T -> (Symbol.symbol, 'a) Source.source -> |
7682
46de8064c93c
added Space, Comment token kinds (keep actual text);
wenzelm
parents:
7477
diff
changeset
|
45 |
(token, Position.T * (Symbol.symbol, 'a) Source.source) Source.source |
17164
a786e1a1ce02
added AltString token (delimited by ASCII back-quotes);
wenzelm
parents:
17069
diff
changeset
|
46 |
val source_proper: (token, 'a) Source.source -> |
a786e1a1ce02
added AltString token (delimited by ASCII back-quotes);
wenzelm
parents:
17069
diff
changeset
|
47 |
(token, (token, 'a) Source.source) Source.source |
5825 | 48 |
end; |
49 |
||
50 |
structure OuterLex: OUTER_LEX = |
|
51 |
struct |
|
52 |
||
53 |
||
54 |
(** tokens **) |
|
55 |
||
56 |
(* datatype token *) |
|
57 |
||
58 |
datatype token_kind = |
|
23729
d1ba656978c5
separated Malformed (symbolic char) from Error (bad input);
wenzelm
parents:
23721
diff
changeset
|
59 |
Command | Keyword | Ident | LongIdent | SymIdent | Var | TypeIdent | TypeVar | Nat | |
23788
54ce229dc858
Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents:
23729
diff
changeset
|
60 |
String | AltString | Verbatim | Space | Comment | Malformed | Error of string | Sync | EOF; |
5825 | 61 |
|
62 |
datatype token = Token of Position.T * (token_kind * string); |
|
63 |
||
64 |
val str_of_kind = |
|
7026 | 65 |
fn Command => "command" |
66 |
| Keyword => "keyword" |
|
5825 | 67 |
| Ident => "identifier" |
68 |
| LongIdent => "long identifier" |
|
69 |
| SymIdent => "symbolic identifier" |
|
70 |
| Var => "schematic variable" |
|
71 |
| TypeIdent => "type variable" |
|
72 |
| TypeVar => "schematic type variable" |
|
73 |
| Nat => "number" |
|
74 |
| String => "string" |
|
17164
a786e1a1ce02
added AltString token (delimited by ASCII back-quotes);
wenzelm
parents:
17069
diff
changeset
|
75 |
| AltString => "back-quoted string" |
5825 | 76 |
| Verbatim => "verbatim text" |
7682
46de8064c93c
added Space, Comment token kinds (keep actual text);
wenzelm
parents:
7477
diff
changeset
|
77 |
| Space => "white space" |
46de8064c93c
added Space, Comment token kinds (keep actual text);
wenzelm
parents:
7477
diff
changeset
|
78 |
| Comment => "comment text" |
23729
d1ba656978c5
separated Malformed (symbolic char) from Error (bad input);
wenzelm
parents:
23721
diff
changeset
|
79 |
| Malformed => "malformed symbolic character" |
d1ba656978c5
separated Malformed (symbolic char) from Error (bad input);
wenzelm
parents:
23721
diff
changeset
|
80 |
| Error _ => "bad input" |
23788
54ce229dc858
Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents:
23729
diff
changeset
|
81 |
| Sync => "sync marker" |
5825 | 82 |
| EOF => "end-of-file"; |
83 |
||
84 |
||
10748 | 85 |
(* control tokens *) |
6859 | 86 |
|
5825 | 87 |
val eof = Token (Position.none, (EOF, "")); |
88 |
||
89 |
fun is_eof (Token (_, (EOF, _))) = true |
|
90 |
| is_eof _ = false; |
|
91 |
||
92 |
val stopper = (eof, is_eof); |
|
93 |
val not_eof = not o is_eof; |
|
94 |
||
95 |
||
23678 | 96 |
fun not_sync (Token (_, (Sync, _))) = false |
97 |
| not_sync _ = true; |
|
98 |
||
99 |
||
5825 | 100 |
(* get position *) |
101 |
||
102 |
fun position_of (Token (pos, _)) = pos; |
|
103 |
val pos_of = Position.str_of o position_of; |
|
104 |
||
105 |
||
106 |
(* kind of token *) |
|
107 |
||
23721 | 108 |
fun kind_of (Token (_, (k, _))) = k; |
109 |
||
5825 | 110 |
fun is_kind k (Token (_, (k', _))) = k = k'; |
111 |
||
7026 | 112 |
fun keyword_with pred (Token (_, (Keyword, x))) = pred x |
113 |
| keyword_with _ _ = false; |
|
5825 | 114 |
|
16029 | 115 |
fun ident_with pred (Token (_, (Ident, x))) = pred x |
116 |
| ident_with _ _ = false; |
|
117 |
||
7682
46de8064c93c
added Space, Comment token kinds (keep actual text);
wenzelm
parents:
7477
diff
changeset
|
118 |
fun is_proper (Token (_, (Space, _))) = false |
46de8064c93c
added Space, Comment token kinds (keep actual text);
wenzelm
parents:
7477
diff
changeset
|
119 |
| is_proper (Token (_, (Comment, _))) = false |
5825 | 120 |
| is_proper _ = true; |
121 |
||
9195 | 122 |
fun is_semicolon (Token (_, (Keyword, ";"))) = true |
9130 | 123 |
| is_semicolon _ = false; |
124 |
||
17069 | 125 |
fun is_comment (Token (_, (Comment, _))) = true |
126 |
| is_comment _ = false; |
|
127 |
||
8580 | 128 |
fun is_begin_ignore (Token (_, (Comment, "<"))) = true |
129 |
| is_begin_ignore _ = false; |
|
130 |
||
131 |
fun is_end_ignore (Token (_, (Comment, ">"))) = true |
|
132 |
| is_end_ignore _ = false; |
|
133 |
||
8651 | 134 |
|
17069 | 135 |
(* blanks and newlines -- space tokens obey lines *) |
8651 | 136 |
|
23678 | 137 |
fun is_blank (Token (_, (Space, x))) = not (String.isSuffix "\n" x) |
17069 | 138 |
| is_blank _ = false; |
139 |
||
23678 | 140 |
fun is_newline (Token (_, (Space, x))) = String.isSuffix "\n" x |
8651 | 141 |
| is_newline _ = false; |
142 |
||
5825 | 143 |
|
14991 | 144 |
(* token content *) |
9155 | 145 |
|
18547 | 146 |
fun escape q = |
147 |
implode o map (fn s => if s = q orelse s = "\\" then "\\" ^ s else s) o Symbol.explode; |
|
148 |
||
14991 | 149 |
fun unparse (Token (_, (kind, x))) = |
150 |
(case kind of |
|
18547 | 151 |
String => x |> quote o escape "\"" |
152 |
| AltString => x |> enclose "`" "`" o escape "`" |
|
14991 | 153 |
| Verbatim => x |> enclose "{*" "*}" |
154 |
| Comment => x |> enclose "(*" "*)" |
|
23788
54ce229dc858
Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents:
23729
diff
changeset
|
155 |
| Malformed => Output.escape_malformed x |
23729
d1ba656978c5
separated Malformed (symbolic char) from Error (bad input);
wenzelm
parents:
23721
diff
changeset
|
156 |
| Sync => "" |
d1ba656978c5
separated Malformed (symbolic char) from Error (bad input);
wenzelm
parents:
23721
diff
changeset
|
157 |
| EOF => "" |
14991 | 158 |
| _ => x); |
159 |
||
23788
54ce229dc858
Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents:
23729
diff
changeset
|
160 |
fun text_of tok = |
54ce229dc858
Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents:
23729
diff
changeset
|
161 |
if is_semicolon tok then ("terminator", "") |
23729
d1ba656978c5
separated Malformed (symbolic char) from Error (bad input);
wenzelm
parents:
23721
diff
changeset
|
162 |
else |
23788
54ce229dc858
Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents:
23729
diff
changeset
|
163 |
let |
54ce229dc858
Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents:
23729
diff
changeset
|
164 |
val k = str_of_kind (kind_of tok); |
54ce229dc858
Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents:
23729
diff
changeset
|
165 |
val s = unparse tok; |
54ce229dc858
Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents:
23729
diff
changeset
|
166 |
in |
54ce229dc858
Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents:
23729
diff
changeset
|
167 |
if s = "" then (k, "") |
54ce229dc858
Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents:
23729
diff
changeset
|
168 |
else if size s < 40 andalso not (exists_string (fn c => c = "\n") s) then (k ^ " " ^ s, "") |
54ce229dc858
Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents:
23729
diff
changeset
|
169 |
else (k, s) |
54ce229dc858
Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents:
23729
diff
changeset
|
170 |
end; |
23729
d1ba656978c5
separated Malformed (symbolic char) from Error (bad input);
wenzelm
parents:
23721
diff
changeset
|
171 |
|
5825 | 172 |
fun val_of (Token (_, (_, x))) = x; |
173 |
||
174 |
fun token_leq (Token (_, (_, x)), Token (_, (_, x'))) = x <= x'; |
|
175 |
||
176 |
||
177 |
||
178 |
(** scanners **) |
|
179 |
||
180 |
fun change_prompt scan = Scan.prompt "# " scan; |
|
181 |
||
182 |
||
183 |
(* diagnostics *) |
|
184 |
||
185 |
fun lex_err msg ((pos, cs), _) = "Outer lexical error" ^ Position.str_of pos ^ ": " ^ msg cs; |
|
9130 | 186 |
fun !!! msg scan = Scan.!! (lex_err (K msg)) scan; |
5825 | 187 |
|
188 |
||
189 |
(* line numbering *) |
|
190 |
||
191 |
fun incr_line scan = Scan.depend (fn pos => scan >> pair (Position.inc pos)); |
|
192 |
val keep_line = Scan.lift; |
|
193 |
||
194 |
val scan_blank = |
|
195 |
incr_line ($$ "\n") || |
|
196 |
keep_line (Scan.one Symbol.is_blank); |
|
197 |
||
198 |
||
199 |
(* scan symbolic idents *) |
|
200 |
||
20664 | 201 |
val is_sym_char = member (op =) (explode "!#$%&*+-/<=>?@^_|~"); |
5825 | 202 |
|
8231 | 203 |
val scan_symid = |
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
20982
diff
changeset
|
204 |
Scan.many1 is_sym_char >> implode || |
8231 | 205 |
Scan.one Symbol.is_symbolic; |
5825 | 206 |
|
8231 | 207 |
fun is_symid str = |
208 |
(case try Symbol.explode str of |
|
15531 | 209 |
SOME [s] => Symbol.is_symbolic s orelse is_sym_char s |
210 |
| SOME ss => forall is_sym_char ss |
|
8231 | 211 |
| _ => false); |
212 |
||
20982 | 213 |
fun is_sid "begin" = false |
214 |
| is_sid ":" = true |
|
22873 | 215 |
| is_sid "::" = true |
20982 | 216 |
| is_sid s = is_symid s orelse Syntax.is_identifier s; |
5825 | 217 |
|
218 |
||
219 |
(* scan strings *) |
|
220 |
||
17164
a786e1a1ce02
added AltString token (delimited by ASCII back-quotes);
wenzelm
parents:
17069
diff
changeset
|
221 |
local |
a786e1a1ce02
added AltString token (delimited by ASCII back-quotes);
wenzelm
parents:
17069
diff
changeset
|
222 |
|
a786e1a1ce02
added AltString token (delimited by ASCII back-quotes);
wenzelm
parents:
17069
diff
changeset
|
223 |
fun scan_str q = |
7682
46de8064c93c
added Space, Comment token kinds (keep actual text);
wenzelm
parents:
7477
diff
changeset
|
224 |
scan_blank || |
9130 | 225 |
keep_line ($$ "\\") |-- !!! "bad escape character in string" |
17164
a786e1a1ce02
added AltString token (delimited by ASCII back-quotes);
wenzelm
parents:
17069
diff
changeset
|
226 |
(scan_blank || keep_line ($$ q || $$ "\\")) || |
23788
54ce229dc858
Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents:
23729
diff
changeset
|
227 |
keep_line (Scan.one (fn s => s <> q andalso s <> "\\" andalso Symbol.is_regular s)); |
5825 | 228 |
|
17164
a786e1a1ce02
added AltString token (delimited by ASCII back-quotes);
wenzelm
parents:
17069
diff
changeset
|
229 |
fun scan_strs q = |
a786e1a1ce02
added AltString token (delimited by ASCII back-quotes);
wenzelm
parents:
17069
diff
changeset
|
230 |
keep_line ($$ q) |-- |
9130 | 231 |
!!! "missing quote at end of string" |
17164
a786e1a1ce02
added AltString token (delimited by ASCII back-quotes);
wenzelm
parents:
17069
diff
changeset
|
232 |
(change_prompt ((Scan.repeat (scan_str q) >> implode) --| keep_line ($$ q))); |
a786e1a1ce02
added AltString token (delimited by ASCII back-quotes);
wenzelm
parents:
17069
diff
changeset
|
233 |
|
a786e1a1ce02
added AltString token (delimited by ASCII back-quotes);
wenzelm
parents:
17069
diff
changeset
|
234 |
in |
a786e1a1ce02
added AltString token (delimited by ASCII back-quotes);
wenzelm
parents:
17069
diff
changeset
|
235 |
|
a786e1a1ce02
added AltString token (delimited by ASCII back-quotes);
wenzelm
parents:
17069
diff
changeset
|
236 |
val scan_string = scan_strs "\""; |
a786e1a1ce02
added AltString token (delimited by ASCII back-quotes);
wenzelm
parents:
17069
diff
changeset
|
237 |
val scan_alt_string = scan_strs "`"; |
a786e1a1ce02
added AltString token (delimited by ASCII back-quotes);
wenzelm
parents:
17069
diff
changeset
|
238 |
|
a786e1a1ce02
added AltString token (delimited by ASCII back-quotes);
wenzelm
parents:
17069
diff
changeset
|
239 |
end; |
5825 | 240 |
|
241 |
||
242 |
(* scan verbatim text *) |
|
243 |
||
244 |
val scan_verb = |
|
245 |
scan_blank || |
|
19305 | 246 |
keep_line ($$ "*" --| Scan.ahead (~$$ "}")) || |
23788
54ce229dc858
Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents:
23729
diff
changeset
|
247 |
keep_line (Scan.one (fn s => s <> "*" andalso Symbol.is_regular s)); |
5825 | 248 |
|
249 |
val scan_verbatim = |
|
6743
5d50225637c8
changed {| |} verbatim syntax to {* *} in order to simplify ProofGeneral setup;
wenzelm
parents:
5876
diff
changeset
|
250 |
keep_line ($$ "{" -- $$ "*") |-- |
9130 | 251 |
!!! "missing end of verbatim text" |
6743
5d50225637c8
changed {| |} verbatim syntax to {* *} in order to simplify ProofGeneral setup;
wenzelm
parents:
5876
diff
changeset
|
252 |
(change_prompt ((Scan.repeat scan_verb >> implode) --| keep_line ($$ "*" -- $$ "}"))); |
5825 | 253 |
|
254 |
||
255 |
(* scan space *) |
|
256 |
||
19305 | 257 |
fun is_space s = Symbol.is_blank s andalso s <> "\n"; |
5825 | 258 |
|
259 |
val scan_space = |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
20982
diff
changeset
|
260 |
(keep_line (Scan.many1 is_space) -- Scan.optional (incr_line ($$ "\n")) "" || |
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
20982
diff
changeset
|
261 |
keep_line (Scan.many is_space) -- incr_line ($$ "\n")) >> (fn (cs, c) => implode cs ^ c); |
5825 | 262 |
|
263 |
||
264 |
(* scan nested comments *) |
|
265 |
||
266 |
val scan_cmt = |
|
267 |
Scan.lift scan_blank || |
|
268 |
Scan.depend (fn d => keep_line ($$ "(" ^^ $$ "*") >> pair (d + 1)) || |
|
269 |
Scan.depend (fn 0 => Scan.fail | d => keep_line ($$ "*" ^^ $$ ")") >> pair (d - 1)) || |
|
19305 | 270 |
Scan.lift (keep_line ($$ "*" --| Scan.ahead (~$$ ")"))) || |
23788
54ce229dc858
Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents:
23729
diff
changeset
|
271 |
Scan.lift (keep_line (Scan.one (fn s => s <> "*" andalso Symbol.is_regular s))); |
5825 | 272 |
|
273 |
val scan_comment = |
|
274 |
keep_line ($$ "(" -- $$ "*") |-- |
|
9130 | 275 |
!!! "missing end of comment" |
5825 | 276 |
(change_prompt |
7682
46de8064c93c
added Space, Comment token kinds (keep actual text);
wenzelm
parents:
7477
diff
changeset
|
277 |
(Scan.pass 0 (Scan.repeat scan_cmt >> implode) --| keep_line ($$ "*" -- $$ ")"))); |
5825 | 278 |
|
279 |
||
23678 | 280 |
(* scan malformed symbols *) |
281 |
||
282 |
local |
|
283 |
||
284 |
val scan_mal = |
|
285 |
scan_blank || |
|
23788
54ce229dc858
Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents:
23729
diff
changeset
|
286 |
keep_line (Scan.one Symbol.is_regular); |
23678 | 287 |
|
288 |
in |
|
289 |
||
290 |
val scan_malformed = |
|
291 |
keep_line ($$ Symbol.malformed) |-- |
|
23729
d1ba656978c5
separated Malformed (symbolic char) from Error (bad input);
wenzelm
parents:
23721
diff
changeset
|
292 |
change_prompt (Scan.repeat scan_mal >> implode) |
23678 | 293 |
--| keep_line (Scan.option ($$ Symbol.end_malformed)); |
294 |
||
295 |
end; |
|
296 |
||
297 |
||
5825 | 298 |
(* scan token *) |
299 |
||
9130 | 300 |
fun scan (lex1, lex2) = |
5825 | 301 |
let |
24022 | 302 |
val scanner = Scan.state :|-- (fn pos => |
9130 | 303 |
let |
304 |
fun token k x = Token (pos, (k, x)); |
|
305 |
fun sync _ = token Sync Symbol.sync; |
|
306 |
in |
|
307 |
scan_string >> token String || |
|
17164
a786e1a1ce02
added AltString token (delimited by ASCII back-quotes);
wenzelm
parents:
17069
diff
changeset
|
308 |
scan_alt_string >> token AltString || |
9130 | 309 |
scan_verbatim >> token Verbatim || |
310 |
scan_space >> token Space || |
|
311 |
scan_comment >> token Comment || |
|
23729
d1ba656978c5
separated Malformed (symbolic char) from Error (bad input);
wenzelm
parents:
23721
diff
changeset
|
312 |
scan_malformed >> token Malformed || |
9130 | 313 |
keep_line (Scan.one Symbol.is_sync >> sync) || |
314 |
keep_line (Scan.max token_leq |
|
315 |
(Scan.max token_leq |
|
316 |
(Scan.literal lex1 >> (token Keyword o implode)) |
|
317 |
(Scan.literal lex2 >> (token Command o implode))) |
|
318 |
(Syntax.scan_longid >> token LongIdent || |
|
319 |
Syntax.scan_id >> token Ident || |
|
320 |
Syntax.scan_var >> token Var || |
|
321 |
Syntax.scan_tid >> token TypeIdent || |
|
322 |
Syntax.scan_tvar >> token TypeVar || |
|
323 |
Syntax.scan_nat >> token Nat || |
|
324 |
scan_symid >> token SymIdent)) |
|
24022 | 325 |
end); |
14729 | 326 |
in !! (lex_err (fn cs => "bad input " ^ quote (Symbol.beginning 10 cs))) scanner end; |
5825 | 327 |
|
328 |
||
9130 | 329 |
(* token sources *) |
5825 | 330 |
|
23678 | 331 |
local |
332 |
||
23788
54ce229dc858
Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents:
23729
diff
changeset
|
333 |
val is_junk = (not o Symbol.is_blank) andf Symbol.is_regular; |
23678 | 334 |
|
24022 | 335 |
fun recover msg = Scan.state :|-- (fn pos => |
336 |
keep_line (Scan.many is_junk) >> (fn xs => [Token (pos, (Error msg, implode xs))])); |
|
23678 | 337 |
|
338 |
in |
|
5825 | 339 |
|
340 |
fun source do_recover get_lex pos src = |
|
341 |
Source.source' pos Symbol.stopper (Scan.bulk (fn xs => scan (get_lex ()) xs)) |
|
23682
cf4773532006
nested source: explicit interactive flag for recover avoids duplicate errors;
wenzelm
parents:
23678
diff
changeset
|
342 |
(Option.map (rpair recover) do_recover) src; |
23678 | 343 |
|
344 |
end; |
|
5825 | 345 |
|
9130 | 346 |
fun source_proper src = src |> Source.filter is_proper; |
347 |
||
348 |
||
5825 | 349 |
end; |