author | aspinall |
Fri, 01 Oct 2004 11:55:43 +0200 | |
changeset 15224 | 1bd35fd87963 |
parent 15209 | b62f72ea3bb0 |
child 15531 | 08c8dad8e399 |
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 = |
|
7477 | 11 |
Command | Keyword | Ident | LongIdent | SymIdent | Var | TypeIdent | TypeVar | |
10748 | 12 |
Nat | String | Verbatim | Space | Comment | Sync | Malformed | 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 |
|
20 |
val is_kind: token_kind -> token -> bool |
|
7026 | 21 |
val keyword_with: (string -> bool) -> token -> bool |
5825 | 22 |
val name_of: token -> string |
23 |
val is_proper: token -> bool |
|
9130 | 24 |
val is_semicolon: token -> bool |
8580 | 25 |
val is_begin_ignore: token -> bool |
26 |
val is_end_ignore: token -> bool |
|
8651 | 27 |
val is_newline: token -> bool |
7902 | 28 |
val is_indent: token -> bool |
14991 | 29 |
val unparse: token -> string |
5825 | 30 |
val val_of: token -> string |
5876 | 31 |
val is_sid: string -> bool |
9130 | 32 |
val !!! : string -> (Position.T * 'a -> 'b) -> Position.T * 'a -> 'b |
33 |
val incr_line: ('a -> 'b * 'c) -> Position.T * 'a -> 'b * (Position.T * 'c) |
|
34 |
val keep_line: ('a -> 'b * 'c) -> Position.T * 'a -> 'b * (Position.T * 'c) |
|
35 |
val scan_blank: Position.T * Symbol.symbol list |
|
36 |
-> Symbol.symbol * (Position.T * Symbol.symbol list) |
|
37 |
val scan_string: Position.T * Symbol.symbol list -> string * (Position.T * Symbol.symbol list) |
|
7026 | 38 |
val scan: (Scan.lexicon * Scan.lexicon) -> |
5825 | 39 |
Position.T * Symbol.symbol list -> token * (Position.T * Symbol.symbol list) |
7026 | 40 |
val source: bool -> (unit -> (Scan.lexicon * Scan.lexicon)) -> |
41 |
Position.T -> (Symbol.symbol, 'a) Source.source -> |
|
7682
46de8064c93c
added Space, Comment token kinds (keep actual text);
wenzelm
parents:
7477
diff
changeset
|
42 |
(token, Position.T * (Symbol.symbol, 'a) Source.source) Source.source |
9130 | 43 |
val source_proper: (token, 'a) Source.source -> (token, (token, 'a) Source.source) Source.source |
44 |
val make_lexicon: string list -> Scan.lexicon |
|
5825 | 45 |
end; |
46 |
||
47 |
structure OuterLex: OUTER_LEX = |
|
48 |
struct |
|
49 |
||
50 |
||
51 |
(** tokens **) |
|
52 |
||
53 |
(* datatype token *) |
|
54 |
||
55 |
datatype token_kind = |
|
7477 | 56 |
Command | Keyword | Ident | LongIdent | SymIdent | Var | TypeIdent | TypeVar | |
10748 | 57 |
Nat | String | Verbatim | Space | Comment | Sync | Malformed | EOF; |
5825 | 58 |
|
59 |
datatype token = Token of Position.T * (token_kind * string); |
|
60 |
||
61 |
val str_of_kind = |
|
7026 | 62 |
fn Command => "command" |
63 |
| Keyword => "keyword" |
|
5825 | 64 |
| Ident => "identifier" |
65 |
| LongIdent => "long identifier" |
|
66 |
| SymIdent => "symbolic identifier" |
|
67 |
| Var => "schematic variable" |
|
68 |
| TypeIdent => "type variable" |
|
69 |
| TypeVar => "schematic type variable" |
|
70 |
| Nat => "number" |
|
71 |
| String => "string" |
|
72 |
| Verbatim => "verbatim text" |
|
7682
46de8064c93c
added Space, Comment token kinds (keep actual text);
wenzelm
parents:
7477
diff
changeset
|
73 |
| Space => "white space" |
46de8064c93c
added Space, Comment token kinds (keep actual text);
wenzelm
parents:
7477
diff
changeset
|
74 |
| Comment => "comment text" |
6859 | 75 |
| Sync => "sync marker" |
10748 | 76 |
| Malformed => "bad input" |
5825 | 77 |
| EOF => "end-of-file"; |
78 |
||
79 |
||
10748 | 80 |
(* control tokens *) |
6859 | 81 |
|
82 |
fun not_sync (Token (_, (Sync, _))) = false |
|
83 |
| not_sync _ = true; |
|
84 |
||
10748 | 85 |
val malformed = Token (Position.none, (Malformed, "")); |
15224
1bd35fd87963
Allow scanning to recover and reconstruct bad input
aspinall
parents:
15209
diff
changeset
|
86 |
fun malformed_of xs = Token (Position.none, (Malformed, implode xs)); |
10748 | 87 |
|
6859 | 88 |
|
5825 | 89 |
(* eof token *) |
90 |
||
91 |
val eof = Token (Position.none, (EOF, "")); |
|
92 |
||
93 |
fun is_eof (Token (_, (EOF, _))) = true |
|
94 |
| is_eof _ = false; |
|
95 |
||
96 |
val stopper = (eof, is_eof); |
|
97 |
val not_eof = not o is_eof; |
|
98 |
||
99 |
||
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 |
||
108 |
fun is_kind k (Token (_, (k', _))) = k = k'; |
|
109 |
||
7026 | 110 |
fun keyword_with pred (Token (_, (Keyword, x))) = pred x |
111 |
| keyword_with _ _ = false; |
|
5825 | 112 |
|
7682
46de8064c93c
added Space, Comment token kinds (keep actual text);
wenzelm
parents:
7477
diff
changeset
|
113 |
fun is_proper (Token (_, (Space, _))) = false |
46de8064c93c
added Space, Comment token kinds (keep actual text);
wenzelm
parents:
7477
diff
changeset
|
114 |
| is_proper (Token (_, (Comment, _))) = false |
5825 | 115 |
| is_proper _ = true; |
116 |
||
9195 | 117 |
fun is_semicolon (Token (_, (Keyword, ";"))) = true |
9130 | 118 |
| is_semicolon _ = false; |
119 |
||
8580 | 120 |
fun is_begin_ignore (Token (_, (Comment, "<"))) = true |
121 |
| is_begin_ignore _ = false; |
|
122 |
||
123 |
fun is_end_ignore (Token (_, (Comment, ">"))) = true |
|
124 |
| is_end_ignore _ = false; |
|
125 |
||
8651 | 126 |
|
127 |
(* newline and indentations (note that space tokens obey lines) *) |
|
128 |
||
129 |
fun is_newline (Token (_, (Space, "\n"))) = true |
|
130 |
| is_newline _ = false; |
|
131 |
||
7902 | 132 |
fun is_indent (Token (_, (Space, s))) = |
133 |
let val n = size s in n > 0 andalso String.substring (s, n - 1, 1) <> "\n" end |
|
134 |
| is_indent _ = false; |
|
135 |
||
5825 | 136 |
|
14991 | 137 |
(* token content *) |
9155 | 138 |
|
139 |
fun name_of (tok as Token (_, (k, x))) = |
|
10748 | 140 |
if is_semicolon tok then "terminator" |
141 |
else if x = "" then str_of_kind k |
|
142 |
else str_of_kind k ^ " " ^ quote x; |
|
5825 | 143 |
|
14991 | 144 |
fun unparse (Token (_, (kind, x))) = |
145 |
(case kind of |
|
15209 | 146 |
String => x |> quote |
14991 | 147 |
| Verbatim => x |> enclose "{*" "*}" |
148 |
| Comment => x |> enclose "(*" "*)" |
|
149 |
| _ => x); |
|
150 |
||
5825 | 151 |
fun val_of (Token (_, (_, x))) = x; |
152 |
||
153 |
fun token_leq (Token (_, (_, x)), Token (_, (_, x'))) = x <= x'; |
|
154 |
||
155 |
||
156 |
||
157 |
(** scanners **) |
|
158 |
||
159 |
fun change_prompt scan = Scan.prompt "# " scan; |
|
160 |
||
161 |
||
162 |
(* diagnostics *) |
|
163 |
||
164 |
fun lex_err msg ((pos, cs), _) = "Outer lexical error" ^ Position.str_of pos ^ ": " ^ msg cs; |
|
9130 | 165 |
fun !!! msg scan = Scan.!! (lex_err (K msg)) scan; |
5825 | 166 |
|
167 |
||
168 |
(* line numbering *) |
|
169 |
||
170 |
fun incr_line scan = Scan.depend (fn pos => scan >> pair (Position.inc pos)); |
|
171 |
val keep_line = Scan.lift; |
|
172 |
||
173 |
val scan_blank = |
|
174 |
incr_line ($$ "\n") || |
|
175 |
keep_line (Scan.one Symbol.is_blank); |
|
176 |
||
177 |
||
178 |
(* scan symbolic idents *) |
|
179 |
||
180 |
val sym_chars = explode "!#$%&*+-/:<=>?@^_`|~"; |
|
181 |
fun is_sym_char s = s mem sym_chars; |
|
182 |
||
8231 | 183 |
val scan_symid = |
184 |
Scan.any1 is_sym_char >> implode || |
|
185 |
Scan.one Symbol.is_symbolic; |
|
5825 | 186 |
|
8231 | 187 |
fun is_symid str = |
188 |
(case try Symbol.explode str of |
|
189 |
Some [s] => Symbol.is_symbolic s orelse is_sym_char s |
|
190 |
| Some ss => forall is_sym_char ss |
|
191 |
| _ => false); |
|
192 |
||
5876 | 193 |
val is_sid = is_symid orf Syntax.is_identifier; |
5825 | 194 |
|
195 |
||
196 |
(* scan strings *) |
|
197 |
||
198 |
val scan_str = |
|
7682
46de8064c93c
added Space, Comment token kinds (keep actual text);
wenzelm
parents:
7477
diff
changeset
|
199 |
scan_blank || |
9130 | 200 |
keep_line ($$ "\\") |-- !!! "bad escape character in string" |
9051 | 201 |
(scan_blank || keep_line ($$ "\"" || $$ "\\")) || |
6859 | 202 |
keep_line (Scan.one (not_equal "\\" andf not_equal "\"" andf |
203 |
Symbol.not_sync andf Symbol.not_eof)); |
|
5825 | 204 |
|
205 |
val scan_string = |
|
206 |
keep_line ($$ "\"") |-- |
|
9130 | 207 |
!!! "missing quote at end of string" |
5825 | 208 |
(change_prompt ((Scan.repeat scan_str >> implode) --| keep_line ($$ "\""))); |
209 |
||
210 |
||
211 |
(* scan verbatim text *) |
|
212 |
||
213 |
val scan_verb = |
|
214 |
scan_blank || |
|
6743
5d50225637c8
changed {| |} verbatim syntax to {* *} in order to simplify ProofGeneral setup;
wenzelm
parents:
5876
diff
changeset
|
215 |
keep_line ($$ "*" --| Scan.ahead (Scan.one (not_equal "}"))) || |
6859 | 216 |
keep_line (Scan.one (not_equal "*" andf Symbol.not_sync andf Symbol.not_eof)); |
5825 | 217 |
|
218 |
val scan_verbatim = |
|
6743
5d50225637c8
changed {| |} verbatim syntax to {* *} in order to simplify ProofGeneral setup;
wenzelm
parents:
5876
diff
changeset
|
219 |
keep_line ($$ "{" -- $$ "*") |-- |
9130 | 220 |
!!! "missing end of verbatim text" |
6743
5d50225637c8
changed {| |} verbatim syntax to {* *} in order to simplify ProofGeneral setup;
wenzelm
parents:
5876
diff
changeset
|
221 |
(change_prompt ((Scan.repeat scan_verb >> implode) --| keep_line ($$ "*" -- $$ "}"))); |
5825 | 222 |
|
223 |
||
224 |
(* scan space *) |
|
225 |
||
226 |
val is_space = Symbol.is_blank andf not_equal "\n"; |
|
227 |
||
228 |
val scan_space = |
|
7682
46de8064c93c
added Space, Comment token kinds (keep actual text);
wenzelm
parents:
7477
diff
changeset
|
229 |
(keep_line (Scan.any1 is_space) -- Scan.optional (incr_line ($$ "\n")) "" || |
46de8064c93c
added Space, Comment token kinds (keep actual text);
wenzelm
parents:
7477
diff
changeset
|
230 |
keep_line (Scan.any is_space) -- incr_line ($$ "\n")) >> (fn (cs, c) => implode cs ^ c); |
5825 | 231 |
|
232 |
||
233 |
(* scan nested comments *) |
|
234 |
||
235 |
val scan_cmt = |
|
236 |
Scan.lift scan_blank || |
|
237 |
Scan.depend (fn d => keep_line ($$ "(" ^^ $$ "*") >> pair (d + 1)) || |
|
238 |
Scan.depend (fn 0 => Scan.fail | d => keep_line ($$ "*" ^^ $$ ")") >> pair (d - 1)) || |
|
239 |
Scan.lift (keep_line ($$ "*" --| Scan.ahead (Scan.one (not_equal ")")))) || |
|
6859 | 240 |
Scan.lift (keep_line (Scan.one (not_equal "*" andf Symbol.not_sync andf Symbol.not_eof))); |
5825 | 241 |
|
242 |
val scan_comment = |
|
243 |
keep_line ($$ "(" -- $$ "*") |-- |
|
9130 | 244 |
!!! "missing end of comment" |
5825 | 245 |
(change_prompt |
7682
46de8064c93c
added Space, Comment token kinds (keep actual text);
wenzelm
parents:
7477
diff
changeset
|
246 |
(Scan.pass 0 (Scan.repeat scan_cmt >> implode) --| keep_line ($$ "*" -- $$ ")"))); |
5825 | 247 |
|
248 |
||
249 |
(* scan token *) |
|
250 |
||
9130 | 251 |
fun scan (lex1, lex2) = |
5825 | 252 |
let |
9130 | 253 |
val scanner = Scan.state :-- (fn pos => |
254 |
let |
|
255 |
fun token k x = Token (pos, (k, x)); |
|
256 |
fun sync _ = token Sync Symbol.sync; |
|
257 |
in |
|
258 |
scan_string >> token String || |
|
259 |
scan_verbatim >> token Verbatim || |
|
260 |
scan_space >> token Space || |
|
261 |
scan_comment >> token Comment || |
|
262 |
keep_line (Scan.one Symbol.is_sync >> sync) || |
|
263 |
keep_line (Scan.max token_leq |
|
264 |
(Scan.max token_leq |
|
265 |
(Scan.literal lex1 >> (token Keyword o implode)) |
|
266 |
(Scan.literal lex2 >> (token Command o implode))) |
|
267 |
(Syntax.scan_longid >> token LongIdent || |
|
268 |
Syntax.scan_id >> token Ident || |
|
269 |
Syntax.scan_var >> token Var || |
|
270 |
Syntax.scan_tid >> token TypeIdent || |
|
271 |
Syntax.scan_tvar >> token TypeVar || |
|
272 |
Syntax.scan_nat >> token Nat || |
|
273 |
scan_symid >> token SymIdent)) |
|
274 |
end) >> #2; |
|
14729 | 275 |
in !! (lex_err (fn cs => "bad input " ^ quote (Symbol.beginning 10 cs))) scanner end; |
5825 | 276 |
|
277 |
||
9130 | 278 |
(* token sources *) |
5825 | 279 |
|
6859 | 280 |
val is_junk = (not o Symbol.is_blank) andf Symbol.not_sync andf Symbol.not_eof; |
15224
1bd35fd87963
Allow scanning to recover and reconstruct bad input
aspinall
parents:
15209
diff
changeset
|
281 |
fun recover xs = (keep_line (Scan.any is_junk) >> (fn ts => [malformed_of ts])) xs; |
5825 | 282 |
|
283 |
fun source do_recover get_lex pos src = |
|
284 |
Source.source' pos Symbol.stopper (Scan.bulk (fn xs => scan (get_lex ()) xs)) |
|
7682
46de8064c93c
added Space, Comment token kinds (keep actual text);
wenzelm
parents:
7477
diff
changeset
|
285 |
(if do_recover then Some recover else None) src; |
5825 | 286 |
|
9130 | 287 |
fun source_proper src = src |> Source.filter is_proper; |
288 |
||
289 |
||
290 |
(* lexicons *) |
|
291 |
||
292 |
val make_lexicon = Scan.make_lexicon o map Symbol.explode; |
|
5825 | 293 |
|
294 |
end; |