author | wenzelm |
Sat, 09 Aug 2008 22:43:56 +0200 | |
changeset 27814 | 05a50886dacb |
parent 27799 | 52f07d5292cd |
child 27835 | ff8b8513965a |
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 = |
|
27814 | 11 |
Command | Keyword | Ident | LongIdent | SymIdent | Var | TypeIdent | TypeVar | |
12 |
Nat | String | AltString | Verbatim | Space | Comment | InternalValue | |
|
13 |
Malformed | Error of string | Sync | EOF |
|
14 |
datatype value = |
|
15 |
Text of string | Typ of typ | Term of term | Fact of thm list | |
|
16 |
Attribute of morphism -> attribute |
|
17 |
type token |
|
5825 | 18 |
val str_of_kind: token_kind -> string |
19 |
val position_of: token -> Position.T |
|
27752
ea7d573e565f
removed obsolete range_of (already included in position);
wenzelm
parents:
27747
diff
changeset
|
20 |
val end_position_of: token -> Position.T |
5825 | 21 |
val pos_of: token -> string |
27733
d3d7038fb7b5
abstract type Scan.stopper, position taken from last input token;
wenzelm
parents:
27663
diff
changeset
|
22 |
val eof: token |
d3d7038fb7b5
abstract type Scan.stopper, position taken from last input token;
wenzelm
parents:
27663
diff
changeset
|
23 |
val is_eof: token -> bool |
d3d7038fb7b5
abstract type Scan.stopper, position taken from last input token;
wenzelm
parents:
27663
diff
changeset
|
24 |
val not_eof: token -> bool |
d3d7038fb7b5
abstract type Scan.stopper, position taken from last input token;
wenzelm
parents:
27663
diff
changeset
|
25 |
val not_sync: token -> bool |
d3d7038fb7b5
abstract type Scan.stopper, position taken from last input token;
wenzelm
parents:
27663
diff
changeset
|
26 |
val stopper: token Scan.stopper |
23721 | 27 |
val kind_of: token -> token_kind |
5825 | 28 |
val is_kind: token_kind -> token -> bool |
7026 | 29 |
val keyword_with: (string -> bool) -> token -> bool |
16029 | 30 |
val ident_with: (string -> bool) -> token -> bool |
5825 | 31 |
val is_proper: token -> bool |
9130 | 32 |
val is_semicolon: token -> bool |
17069 | 33 |
val is_comment: token -> bool |
8580 | 34 |
val is_begin_ignore: token -> bool |
35 |
val is_end_ignore: token -> bool |
|
17069 | 36 |
val is_blank: token -> bool |
8651 | 37 |
val is_newline: token -> bool |
27747
d41abb7bc08a
token: maintain of source, which retains original position information;
wenzelm
parents:
27733
diff
changeset
|
38 |
val source_of: token -> string |
27814 | 39 |
val content_of: token -> string |
14991 | 40 |
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
|
41 |
val text_of: token -> string * string |
27814 | 42 |
val get_value: token -> value option |
43 |
val map_value: (value -> value) -> token -> token |
|
44 |
val mk_text: string -> token |
|
45 |
val mk_typ: typ -> token |
|
46 |
val mk_term: term -> token |
|
47 |
val mk_fact: thm list -> token |
|
48 |
val mk_attribute: (morphism -> attribute) -> token |
|
49 |
val assignable: token -> token |
|
50 |
val assign: value option -> token -> unit |
|
51 |
val closure: token -> token |
|
52 |
val ident_or_symbolic: string -> bool |
|
27769 | 53 |
val !!! : string -> (SymbolPos.T list -> 'a) -> SymbolPos.T list -> 'a |
54 |
val scan_quoted: SymbolPos.T list -> SymbolPos.T list * SymbolPos.T list |
|
55 |
val source_proper: (token, 'a) Source.source -> (token, (token, 'a) Source.source) Source.source |
|
27780
7d0910f662f7
more precise positions due to SymbolsPos.implode_delim;
wenzelm
parents:
27769
diff
changeset
|
56 |
val source': bool Option.option -> (unit -> Scan.lexicon * Scan.lexicon) -> |
7d0910f662f7
more precise positions due to SymbolsPos.implode_delim;
wenzelm
parents:
27769
diff
changeset
|
57 |
(SymbolPos.T, 'a) Source.source -> (token, (SymbolPos.T, 'a) Source.source) Source.source |
27769 | 58 |
val source: bool Option.option -> (unit -> Scan.lexicon * Scan.lexicon) -> |
59 |
Position.T -> (Symbol.symbol, 'a) Source.source -> (token, |
|
60 |
(SymbolPos.T, Position.T * (Symbol.symbol, 'a) Source.source) Source.source) Source.source |
|
5825 | 61 |
end; |
62 |
||
63 |
structure OuterLex: OUTER_LEX = |
|
64 |
struct |
|
65 |
||
66 |
(** tokens **) |
|
67 |
||
27814 | 68 |
(* token values *) |
69 |
||
70 |
(*The value slot assigns an (optional) internal value to a token, |
|
71 |
usually as a side-effect of special scanner setup (see also |
|
72 |
args.ML). Note that an assignable ref designates an intermediate |
|
73 |
state of internalization -- it is NOT meant to persist.*) |
|
74 |
||
75 |
datatype value = |
|
76 |
Text of string | |
|
77 |
Typ of typ | |
|
78 |
Term of term | |
|
79 |
Fact of thm list | |
|
80 |
Attribute of morphism -> attribute; |
|
81 |
||
82 |
datatype slot = |
|
83 |
Slot | |
|
84 |
Value of value option | |
|
85 |
Assignable of value option ref; |
|
86 |
||
87 |
||
5825 | 88 |
(* datatype token *) |
89 |
||
90 |
datatype token_kind = |
|
27814 | 91 |
Command | Keyword | Ident | LongIdent | SymIdent | Var | TypeIdent | TypeVar | |
92 |
Nat | String | AltString | Verbatim | Space | Comment | InternalValue | |
|
93 |
Malformed | Error of string | Sync | EOF; |
|
5825 | 94 |
|
27814 | 95 |
datatype token = Token of (SymbolPos.text * Position.range) * (token_kind * string) * slot; |
5825 | 96 |
|
97 |
val str_of_kind = |
|
7026 | 98 |
fn Command => "command" |
99 |
| Keyword => "keyword" |
|
5825 | 100 |
| Ident => "identifier" |
101 |
| LongIdent => "long identifier" |
|
102 |
| SymIdent => "symbolic identifier" |
|
103 |
| Var => "schematic variable" |
|
104 |
| TypeIdent => "type variable" |
|
105 |
| TypeVar => "schematic type variable" |
|
106 |
| Nat => "number" |
|
107 |
| String => "string" |
|
17164
a786e1a1ce02
added AltString token (delimited by ASCII back-quotes);
wenzelm
parents:
17069
diff
changeset
|
108 |
| AltString => "back-quoted string" |
5825 | 109 |
| Verbatim => "verbatim text" |
7682
46de8064c93c
added Space, Comment token kinds (keep actual text);
wenzelm
parents:
7477
diff
changeset
|
110 |
| Space => "white space" |
46de8064c93c
added Space, Comment token kinds (keep actual text);
wenzelm
parents:
7477
diff
changeset
|
111 |
| Comment => "comment text" |
27814 | 112 |
| InternalValue => "internal value" |
23729
d1ba656978c5
separated Malformed (symbolic char) from Error (bad input);
wenzelm
parents:
23721
diff
changeset
|
113 |
| Malformed => "malformed symbolic character" |
d1ba656978c5
separated Malformed (symbolic char) from Error (bad input);
wenzelm
parents:
23721
diff
changeset
|
114 |
| 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
|
115 |
| Sync => "sync marker" |
5825 | 116 |
| EOF => "end-of-file"; |
117 |
||
118 |
||
27733
d3d7038fb7b5
abstract type Scan.stopper, position taken from last input token;
wenzelm
parents:
27663
diff
changeset
|
119 |
(* position *) |
5825 | 120 |
|
27814 | 121 |
fun position_of (Token ((_, (pos, _)), _, _)) = pos; |
122 |
fun end_position_of (Token ((_, (_, pos)), _, _)) = pos; |
|
27663 | 123 |
|
5825 | 124 |
val pos_of = Position.str_of o position_of; |
125 |
||
126 |
||
27733
d3d7038fb7b5
abstract type Scan.stopper, position taken from last input token;
wenzelm
parents:
27663
diff
changeset
|
127 |
(* control tokens *) |
d3d7038fb7b5
abstract type Scan.stopper, position taken from last input token;
wenzelm
parents:
27663
diff
changeset
|
128 |
|
27814 | 129 |
fun mk_eof pos = Token (("", (pos, Position.none)), (EOF, ""), Slot); |
27733
d3d7038fb7b5
abstract type Scan.stopper, position taken from last input token;
wenzelm
parents:
27663
diff
changeset
|
130 |
val eof = mk_eof Position.none; |
d3d7038fb7b5
abstract type Scan.stopper, position taken from last input token;
wenzelm
parents:
27663
diff
changeset
|
131 |
|
27814 | 132 |
fun is_eof (Token (_, (EOF, _), _)) = true |
27733
d3d7038fb7b5
abstract type Scan.stopper, position taken from last input token;
wenzelm
parents:
27663
diff
changeset
|
133 |
| is_eof _ = false; |
d3d7038fb7b5
abstract type Scan.stopper, position taken from last input token;
wenzelm
parents:
27663
diff
changeset
|
134 |
|
d3d7038fb7b5
abstract type Scan.stopper, position taken from last input token;
wenzelm
parents:
27663
diff
changeset
|
135 |
val not_eof = not o is_eof; |
d3d7038fb7b5
abstract type Scan.stopper, position taken from last input token;
wenzelm
parents:
27663
diff
changeset
|
136 |
|
27814 | 137 |
fun not_sync (Token (_, (Sync, _), _)) = false |
27733
d3d7038fb7b5
abstract type Scan.stopper, position taken from last input token;
wenzelm
parents:
27663
diff
changeset
|
138 |
| not_sync _ = true; |
d3d7038fb7b5
abstract type Scan.stopper, position taken from last input token;
wenzelm
parents:
27663
diff
changeset
|
139 |
|
27752
ea7d573e565f
removed obsolete range_of (already included in position);
wenzelm
parents:
27747
diff
changeset
|
140 |
val stopper = |
ea7d573e565f
removed obsolete range_of (already included in position);
wenzelm
parents:
27747
diff
changeset
|
141 |
Scan.stopper (fn [] => eof | toks => mk_eof (end_position_of (List.last toks))) is_eof; |
27733
d3d7038fb7b5
abstract type Scan.stopper, position taken from last input token;
wenzelm
parents:
27663
diff
changeset
|
142 |
|
d3d7038fb7b5
abstract type Scan.stopper, position taken from last input token;
wenzelm
parents:
27663
diff
changeset
|
143 |
|
5825 | 144 |
(* kind of token *) |
145 |
||
27814 | 146 |
fun kind_of (Token (_, (k, _), _)) = k; |
147 |
fun is_kind k (Token (_, (k', _), _)) = k = k'; |
|
5825 | 148 |
|
27814 | 149 |
fun keyword_with pred (Token (_, (Keyword, x), _)) = pred x |
7026 | 150 |
| keyword_with _ _ = false; |
5825 | 151 |
|
27814 | 152 |
fun ident_with pred (Token (_, (Ident, x), _)) = pred x |
16029 | 153 |
| ident_with _ _ = false; |
154 |
||
27814 | 155 |
fun is_proper (Token (_, (Space, _), _)) = false |
156 |
| is_proper (Token (_, (Comment, _), _)) = false |
|
5825 | 157 |
| is_proper _ = true; |
158 |
||
27814 | 159 |
fun is_semicolon (Token (_, (Keyword, ";"), _)) = true |
9130 | 160 |
| is_semicolon _ = false; |
161 |
||
27814 | 162 |
fun is_comment (Token (_, (Comment, _), _)) = true |
17069 | 163 |
| is_comment _ = false; |
164 |
||
27814 | 165 |
fun is_begin_ignore (Token (_, (Comment, "<"), _)) = true |
8580 | 166 |
| is_begin_ignore _ = false; |
167 |
||
27814 | 168 |
fun is_end_ignore (Token (_, (Comment, ">"), _)) = true |
8580 | 169 |
| is_end_ignore _ = false; |
170 |
||
8651 | 171 |
|
17069 | 172 |
(* blanks and newlines -- space tokens obey lines *) |
8651 | 173 |
|
27814 | 174 |
fun is_blank (Token (_, (Space, x), _)) = not (String.isSuffix "\n" x) |
17069 | 175 |
| is_blank _ = false; |
176 |
||
27814 | 177 |
fun is_newline (Token (_, (Space, x), _)) = String.isSuffix "\n" x |
8651 | 178 |
| is_newline _ = false; |
179 |
||
5825 | 180 |
|
14991 | 181 |
(* token content *) |
9155 | 182 |
|
27814 | 183 |
fun source_of (Token ((source, (pos, _)), _, _)) = |
184 |
YXML.string_of (XML.Elem (Markup.tokenN, Position.properties_of pos, [XML.Text source])); |
|
25642
ebdff0dca2a5
text_of: made even more robust against recurrent errors;
wenzelm
parents:
25582
diff
changeset
|
185 |
|
27814 | 186 |
fun content_of (Token (_, (_, x), _)) = x; |
27747
d41abb7bc08a
token: maintain of source, which retains original position information;
wenzelm
parents:
27733
diff
changeset
|
187 |
|
d41abb7bc08a
token: maintain of source, which retains original position information;
wenzelm
parents:
27733
diff
changeset
|
188 |
|
d41abb7bc08a
token: maintain of source, which retains original position information;
wenzelm
parents:
27733
diff
changeset
|
189 |
(* unparse *) |
d41abb7bc08a
token: maintain of source, which retains original position information;
wenzelm
parents:
27733
diff
changeset
|
190 |
|
18547 | 191 |
fun escape q = |
192 |
implode o map (fn s => if s = q orelse s = "\\" then "\\" ^ s else s) o Symbol.explode; |
|
193 |
||
27814 | 194 |
fun unparse (Token (_, (kind, x), _)) = |
14991 | 195 |
(case kind of |
18547 | 196 |
String => x |> quote o escape "\"" |
197 |
| AltString => x |> enclose "`" "`" o escape "`" |
|
14991 | 198 |
| Verbatim => x |> enclose "{*" "*}" |
199 |
| Comment => x |> enclose "(*" "*)" |
|
25642
ebdff0dca2a5
text_of: made even more robust against recurrent errors;
wenzelm
parents:
25582
diff
changeset
|
200 |
| Malformed => Output.escape (translate_string Output.output x) |
23729
d1ba656978c5
separated Malformed (symbolic char) from Error (bad input);
wenzelm
parents:
23721
diff
changeset
|
201 |
| Sync => "" |
d1ba656978c5
separated Malformed (symbolic char) from Error (bad input);
wenzelm
parents:
23721
diff
changeset
|
202 |
| EOF => "" |
14991 | 203 |
| _ => x); |
204 |
||
23788
54ce229dc858
Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents:
23729
diff
changeset
|
205 |
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
|
206 |
if is_semicolon tok then ("terminator", "") |
23729
d1ba656978c5
separated Malformed (symbolic char) from Error (bad input);
wenzelm
parents:
23721
diff
changeset
|
207 |
else |
23788
54ce229dc858
Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents:
23729
diff
changeset
|
208 |
let |
54ce229dc858
Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents:
23729
diff
changeset
|
209 |
val k = str_of_kind (kind_of tok); |
25642
ebdff0dca2a5
text_of: made even more robust against recurrent errors;
wenzelm
parents:
25582
diff
changeset
|
210 |
val s = unparse tok |
27814 | 211 |
handle ERROR _ => Symbol.separate_chars (content_of tok); |
23788
54ce229dc858
Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents:
23729
diff
changeset
|
212 |
in |
54ce229dc858
Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents:
23729
diff
changeset
|
213 |
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
|
214 |
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
|
215 |
else (k, s) |
54ce229dc858
Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents:
23729
diff
changeset
|
216 |
end; |
23729
d1ba656978c5
separated Malformed (symbolic char) from Error (bad input);
wenzelm
parents:
23721
diff
changeset
|
217 |
|
5825 | 218 |
|
219 |
||
27814 | 220 |
(** associated values **) |
221 |
||
222 |
(* access values *) |
|
223 |
||
224 |
fun get_value (Token (_, _, Value v)) = v |
|
225 |
| get_value _ = NONE; |
|
226 |
||
227 |
fun map_value f (Token (x, y, Value (SOME v))) = Token (x, y, Value (SOME (f v))) |
|
228 |
| map_value _ tok = tok; |
|
229 |
||
230 |
||
231 |
(* make values *) |
|
232 |
||
233 |
fun mk_value k v = Token ((k, Position.no_range), (InternalValue, k), Value (SOME v)); |
|
234 |
||
235 |
val mk_text = mk_value "<text>" o Text; |
|
236 |
val mk_typ = mk_value "<typ>" o Typ; |
|
237 |
val mk_term = mk_value "<term>" o Term; |
|
238 |
val mk_fact = mk_value "<fact>" o Fact; |
|
239 |
val mk_attribute = mk_value "<attribute>" o Attribute; |
|
240 |
||
241 |
||
242 |
(* static binding *) |
|
243 |
||
244 |
(*1st stage: make empty slots assignable*) |
|
245 |
fun assignable (Token (x, y, Slot)) = Token (x, y, Assignable (ref NONE)) |
|
246 |
| assignable tok = tok; |
|
247 |
||
248 |
(*2nd stage: assign values as side-effect of scanning*) |
|
249 |
fun assign v (Token (_, _, Assignable r)) = r := v |
|
250 |
| assign _ _ = (); |
|
251 |
||
252 |
(*3rd stage: static closure of final values*) |
|
253 |
fun closure (Token (x, y, Assignable (ref v))) = Token (x, y, Value v) |
|
254 |
| closure tok = tok; |
|
255 |
||
256 |
||
257 |
||
5825 | 258 |
(** scanners **) |
259 |
||
27769 | 260 |
open BasicSymbolPos; |
5825 | 261 |
|
27769 | 262 |
fun !!! msg = SymbolPos.!!! ("Outer lexical error: " ^ msg); |
5825 | 263 |
|
27769 | 264 |
fun change_prompt scan = Scan.prompt "# " scan; |
5825 | 265 |
|
266 |
||
267 |
(* scan symbolic idents *) |
|
268 |
||
20664 | 269 |
val is_sym_char = member (op =) (explode "!#$%&*+-/<=>?@^_|~"); |
5825 | 270 |
|
8231 | 271 |
val scan_symid = |
27769 | 272 |
Scan.many1 (is_sym_char o symbol) || |
273 |
Scan.one (Symbol.is_symbolic o symbol) >> single; |
|
5825 | 274 |
|
8231 | 275 |
fun is_symid str = |
276 |
(case try Symbol.explode str of |
|
15531 | 277 |
SOME [s] => Symbol.is_symbolic s orelse is_sym_char s |
278 |
| SOME ss => forall is_sym_char ss |
|
8231 | 279 |
| _ => false); |
280 |
||
27814 | 281 |
fun ident_or_symbolic "begin" = false |
282 |
| ident_or_symbolic ":" = true |
|
283 |
| ident_or_symbolic "::" = true |
|
284 |
| ident_or_symbolic s = Syntax.is_identifier s orelse is_symid s; |
|
5825 | 285 |
|
286 |
||
287 |
(* scan strings *) |
|
288 |
||
17164
a786e1a1ce02
added AltString token (delimited by ASCII back-quotes);
wenzelm
parents:
17069
diff
changeset
|
289 |
local |
a786e1a1ce02
added AltString token (delimited by ASCII back-quotes);
wenzelm
parents:
17069
diff
changeset
|
290 |
|
25579
22869d9d545b
(alt)string: allow explicit character codes (as in ML);
wenzelm
parents:
24577
diff
changeset
|
291 |
val char_code = |
27769 | 292 |
Scan.one (Symbol.is_ascii_digit o symbol) -- |
293 |
Scan.one (Symbol.is_ascii_digit o symbol) -- |
|
294 |
Scan.one (Symbol.is_ascii_digit o symbol) :|-- |
|
295 |
(fn (((a, pos), (b, _)), (c, _)) => |
|
25579
22869d9d545b
(alt)string: allow explicit character codes (as in ML);
wenzelm
parents:
24577
diff
changeset
|
296 |
let val (n, _) = Library.read_int [a, b, c] |
27769 | 297 |
in if n <= 255 then Scan.succeed [(chr n, pos)] else Scan.fail end); |
25579
22869d9d545b
(alt)string: allow explicit character codes (as in ML);
wenzelm
parents:
24577
diff
changeset
|
298 |
|
17164
a786e1a1ce02
added AltString token (delimited by ASCII back-quotes);
wenzelm
parents:
17069
diff
changeset
|
299 |
fun scan_str q = |
27769 | 300 |
$$$ "\\" |-- !!! "bad escape character in string" ($$$ q || $$$ "\\" || char_code) || |
301 |
Scan.one (fn (s, _) => s <> q andalso s <> "\\" andalso Symbol.is_regular s) >> single; |
|
5825 | 302 |
|
17164
a786e1a1ce02
added AltString token (delimited by ASCII back-quotes);
wenzelm
parents:
17069
diff
changeset
|
303 |
fun scan_strs q = |
27780
7d0910f662f7
more precise positions due to SymbolsPos.implode_delim;
wenzelm
parents:
27769
diff
changeset
|
304 |
(SymbolPos.scan_pos --| $$$ q) -- !!! "missing quote at end of string" |
7d0910f662f7
more precise positions due to SymbolsPos.implode_delim;
wenzelm
parents:
27769
diff
changeset
|
305 |
(change_prompt ((Scan.repeat (scan_str q) >> flat) -- ($$$ q |-- SymbolPos.scan_pos))); |
17164
a786e1a1ce02
added AltString token (delimited by ASCII back-quotes);
wenzelm
parents:
17069
diff
changeset
|
306 |
|
a786e1a1ce02
added AltString token (delimited by ASCII back-quotes);
wenzelm
parents:
17069
diff
changeset
|
307 |
in |
a786e1a1ce02
added AltString token (delimited by ASCII back-quotes);
wenzelm
parents:
17069
diff
changeset
|
308 |
|
a786e1a1ce02
added AltString token (delimited by ASCII back-quotes);
wenzelm
parents:
17069
diff
changeset
|
309 |
val scan_string = scan_strs "\""; |
a786e1a1ce02
added AltString token (delimited by ASCII back-quotes);
wenzelm
parents:
17069
diff
changeset
|
310 |
val scan_alt_string = scan_strs "`"; |
a786e1a1ce02
added AltString token (delimited by ASCII back-quotes);
wenzelm
parents:
17069
diff
changeset
|
311 |
|
27769 | 312 |
val scan_quoted = Scan.trace (scan_string || scan_alt_string) >> #2; |
27752
ea7d573e565f
removed obsolete range_of (already included in position);
wenzelm
parents:
27747
diff
changeset
|
313 |
|
17164
a786e1a1ce02
added AltString token (delimited by ASCII back-quotes);
wenzelm
parents:
17069
diff
changeset
|
314 |
end; |
5825 | 315 |
|
316 |
||
317 |
(* scan verbatim text *) |
|
318 |
||
319 |
val scan_verb = |
|
27769 | 320 |
$$$ "*" --| Scan.ahead (~$$$ "}") || |
321 |
Scan.one (fn (s, _) => s <> "*" andalso Symbol.is_regular s) >> single; |
|
5825 | 322 |
|
323 |
val scan_verbatim = |
|
27780
7d0910f662f7
more precise positions due to SymbolsPos.implode_delim;
wenzelm
parents:
27769
diff
changeset
|
324 |
(SymbolPos.scan_pos --| $$$ "{" --| $$$ "*") -- !!! "missing end of verbatim text" |
7d0910f662f7
more precise positions due to SymbolsPos.implode_delim;
wenzelm
parents:
27769
diff
changeset
|
325 |
(change_prompt ((Scan.repeat scan_verb >> flat) -- ($$$ "*" |-- $$$ "}" |-- SymbolPos.scan_pos))); |
5825 | 326 |
|
327 |
||
328 |
(* scan space *) |
|
329 |
||
19305 | 330 |
fun is_space s = Symbol.is_blank s andalso s <> "\n"; |
5825 | 331 |
|
332 |
val scan_space = |
|
27769 | 333 |
Scan.many1 (is_space o symbol) @@@ Scan.optional ($$$ "\n") [] || |
334 |
Scan.many (is_space o symbol) @@@ $$$ "\n"; |
|
5825 | 335 |
|
336 |
||
27780
7d0910f662f7
more precise positions due to SymbolsPos.implode_delim;
wenzelm
parents:
27769
diff
changeset
|
337 |
(* scan comment *) |
5825 | 338 |
|
339 |
val scan_comment = |
|
27780
7d0910f662f7
more precise positions due to SymbolsPos.implode_delim;
wenzelm
parents:
27769
diff
changeset
|
340 |
SymbolPos.scan_pos -- (SymbolPos.scan_comment_body !!! -- SymbolPos.scan_pos); |
5825 | 341 |
|
342 |
||
23678 | 343 |
(* scan malformed symbols *) |
344 |
||
345 |
val scan_malformed = |
|
27769 | 346 |
$$$ Symbol.malformed |-- |
347 |
change_prompt (Scan.many (Symbol.is_regular o symbol)) |
|
348 |
--| Scan.option ($$$ Symbol.end_malformed); |
|
27752
ea7d573e565f
removed obsolete range_of (already included in position);
wenzelm
parents:
27747
diff
changeset
|
349 |
|
ea7d573e565f
removed obsolete range_of (already included in position);
wenzelm
parents:
27747
diff
changeset
|
350 |
|
27663 | 351 |
|
27769 | 352 |
(** token sources **) |
5825 | 353 |
|
27769 | 354 |
fun source_proper src = src |> Source.filter is_proper; |
5825 | 355 |
|
23678 | 356 |
local |
357 |
||
27769 | 358 |
fun token_leq ((_, syms1), (_, syms2)) = length syms1 <= length syms2; |
27780
7d0910f662f7
more precise positions due to SymbolsPos.implode_delim;
wenzelm
parents:
27769
diff
changeset
|
359 |
|
27799 | 360 |
fun token k ss = |
27814 | 361 |
Token ((SymbolPos.implode ss, SymbolPos.range ss), (k, SymbolPos.content ss), Slot); |
27799 | 362 |
|
363 |
fun token_range k (pos1, (ss, pos2)) = |
|
27814 | 364 |
Token (SymbolPos.implode_range pos1 pos2 ss, (k, SymbolPos.content ss), Slot); |
23678 | 365 |
|
27769 | 366 |
fun scan (lex1, lex2) = !!! "bad input" |
27799 | 367 |
(scan_string >> token_range String || |
368 |
scan_alt_string >> token_range AltString || |
|
369 |
scan_verbatim >> token_range Verbatim || |
|
370 |
scan_comment >> token_range Comment || |
|
27780
7d0910f662f7
more precise positions due to SymbolsPos.implode_delim;
wenzelm
parents:
27769
diff
changeset
|
371 |
scan_space >> token Space || |
7d0910f662f7
more precise positions due to SymbolsPos.implode_delim;
wenzelm
parents:
27769
diff
changeset
|
372 |
scan_malformed >> token Malformed || |
7d0910f662f7
more precise positions due to SymbolsPos.implode_delim;
wenzelm
parents:
27769
diff
changeset
|
373 |
Scan.one (Symbol.is_sync o symbol) >> (token Sync o single) || |
7d0910f662f7
more precise positions due to SymbolsPos.implode_delim;
wenzelm
parents:
27769
diff
changeset
|
374 |
(Scan.max token_leq |
27769 | 375 |
(Scan.max token_leq |
376 |
(Scan.literal lex2 >> pair Command) |
|
377 |
(Scan.literal lex1 >> pair Keyword)) |
|
378 |
(Syntax.scan_longid >> pair LongIdent || |
|
379 |
Syntax.scan_id >> pair Ident || |
|
380 |
Syntax.scan_var >> pair Var || |
|
381 |
Syntax.scan_tid >> pair TypeIdent || |
|
382 |
Syntax.scan_tvar >> pair TypeVar || |
|
383 |
Syntax.scan_nat >> pair Nat || |
|
27780
7d0910f662f7
more precise positions due to SymbolsPos.implode_delim;
wenzelm
parents:
27769
diff
changeset
|
384 |
scan_symid >> pair SymIdent) >> uncurry token)); |
27769 | 385 |
|
386 |
fun recover msg = |
|
387 |
Scan.many ((Symbol.is_regular andf (not o Symbol.is_blank)) o symbol) |
|
27780
7d0910f662f7
more precise positions due to SymbolsPos.implode_delim;
wenzelm
parents:
27769
diff
changeset
|
388 |
>> (single o token (Error msg)); |
23678 | 389 |
|
390 |
in |
|
5825 | 391 |
|
27780
7d0910f662f7
more precise positions due to SymbolsPos.implode_delim;
wenzelm
parents:
27769
diff
changeset
|
392 |
fun source' do_recover get_lex = |
7d0910f662f7
more precise positions due to SymbolsPos.implode_delim;
wenzelm
parents:
27769
diff
changeset
|
393 |
Source.source SymbolPos.stopper (Scan.bulk (fn xs => scan (get_lex ()) xs)) |
7d0910f662f7
more precise positions due to SymbolsPos.implode_delim;
wenzelm
parents:
27769
diff
changeset
|
394 |
(Option.map (rpair recover) do_recover); |
7d0910f662f7
more precise positions due to SymbolsPos.implode_delim;
wenzelm
parents:
27769
diff
changeset
|
395 |
|
5825 | 396 |
fun source do_recover get_lex pos src = |
27769 | 397 |
SymbolPos.source pos src |
27780
7d0910f662f7
more precise positions due to SymbolsPos.implode_delim;
wenzelm
parents:
27769
diff
changeset
|
398 |
|> source' do_recover get_lex; |
23678 | 399 |
|
400 |
end; |
|
5825 | 401 |
|
402 |
end; |