author | aspinall |
Tue, 28 Sep 2004 10:43:34 +0200 | |
changeset 15209 | b62f72ea3bb0 |
parent 15143 | 05b5995f214e |
child 15224 | 1bd35fd87963 |
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, "")); |
86 |
||
6859 | 87 |
|
5825 | 88 |
(* eof token *) |
89 |
||
90 |
val eof = Token (Position.none, (EOF, "")); |
|
91 |
||
92 |
fun is_eof (Token (_, (EOF, _))) = true |
|
93 |
| is_eof _ = false; |
|
94 |
||
95 |
val stopper = (eof, is_eof); |
|
96 |
val not_eof = not o is_eof; |
|
97 |
||
98 |
||
99 |
(* get position *) |
|
100 |
||
101 |
fun position_of (Token (pos, _)) = pos; |
|
102 |
val pos_of = Position.str_of o position_of; |
|
103 |
||
104 |
||
105 |
(* kind of token *) |
|
106 |
||
107 |
fun is_kind k (Token (_, (k', _))) = k = k'; |
|
108 |
||
7026 | 109 |
fun keyword_with pred (Token (_, (Keyword, x))) = pred x |
110 |
| keyword_with _ _ = false; |
|
5825 | 111 |
|
7682
46de8064c93c
added Space, Comment token kinds (keep actual text);
wenzelm
parents:
7477
diff
changeset
|
112 |
fun is_proper (Token (_, (Space, _))) = false |
46de8064c93c
added Space, Comment token kinds (keep actual text);
wenzelm
parents:
7477
diff
changeset
|
113 |
| is_proper (Token (_, (Comment, _))) = false |
5825 | 114 |
| is_proper _ = true; |
115 |
||
9195 | 116 |
fun is_semicolon (Token (_, (Keyword, ";"))) = true |
9130 | 117 |
| is_semicolon _ = false; |
118 |
||
8580 | 119 |
fun is_begin_ignore (Token (_, (Comment, "<"))) = true |
120 |
| is_begin_ignore _ = false; |
|
121 |
||
122 |
fun is_end_ignore (Token (_, (Comment, ">"))) = true |
|
123 |
| is_end_ignore _ = false; |
|
124 |
||
8651 | 125 |
|
126 |
(* newline and indentations (note that space tokens obey lines) *) |
|
127 |
||
128 |
fun is_newline (Token (_, (Space, "\n"))) = true |
|
129 |
| is_newline _ = false; |
|
130 |
||
7902 | 131 |
fun is_indent (Token (_, (Space, s))) = |
132 |
let val n = size s in n > 0 andalso String.substring (s, n - 1, 1) <> "\n" end |
|
133 |
| is_indent _ = false; |
|
134 |
||
5825 | 135 |
|
14991 | 136 |
(* token content *) |
9155 | 137 |
|
138 |
fun name_of (tok as Token (_, (k, x))) = |
|
10748 | 139 |
if is_semicolon tok then "terminator" |
140 |
else if x = "" then str_of_kind k |
|
141 |
else str_of_kind k ^ " " ^ quote x; |
|
5825 | 142 |
|
14991 | 143 |
fun unparse (Token (_, (kind, x))) = |
144 |
(case kind of |
|
15209 | 145 |
String => x |> quote |
14991 | 146 |
| Verbatim => x |> enclose "{*" "*}" |
147 |
| Comment => x |> enclose "(*" "*)" |
|
148 |
| _ => x); |
|
149 |
||
5825 | 150 |
fun val_of (Token (_, (_, x))) = x; |
151 |
||
152 |
fun token_leq (Token (_, (_, x)), Token (_, (_, x'))) = x <= x'; |
|
153 |
||
154 |
||
155 |
||
156 |
(** scanners **) |
|
157 |
||
158 |
fun change_prompt scan = Scan.prompt "# " scan; |
|
159 |
||
160 |
||
161 |
(* diagnostics *) |
|
162 |
||
163 |
fun lex_err msg ((pos, cs), _) = "Outer lexical error" ^ Position.str_of pos ^ ": " ^ msg cs; |
|
9130 | 164 |
fun !!! msg scan = Scan.!! (lex_err (K msg)) scan; |
5825 | 165 |
|
166 |
||
167 |
(* line numbering *) |
|
168 |
||
169 |
fun incr_line scan = Scan.depend (fn pos => scan >> pair (Position.inc pos)); |
|
170 |
val keep_line = Scan.lift; |
|
171 |
||
172 |
val scan_blank = |
|
173 |
incr_line ($$ "\n") || |
|
174 |
keep_line (Scan.one Symbol.is_blank); |
|
175 |
||
176 |
||
177 |
(* scan symbolic idents *) |
|
178 |
||
179 |
val sym_chars = explode "!#$%&*+-/:<=>?@^_`|~"; |
|
180 |
fun is_sym_char s = s mem sym_chars; |
|
181 |
||
8231 | 182 |
val scan_symid = |
183 |
Scan.any1 is_sym_char >> implode || |
|
184 |
Scan.one Symbol.is_symbolic; |
|
5825 | 185 |
|
8231 | 186 |
fun is_symid str = |
187 |
(case try Symbol.explode str of |
|
188 |
Some [s] => Symbol.is_symbolic s orelse is_sym_char s |
|
189 |
| Some ss => forall is_sym_char ss |
|
190 |
| _ => false); |
|
191 |
||
5876 | 192 |
val is_sid = is_symid orf Syntax.is_identifier; |
5825 | 193 |
|
194 |
||
195 |
(* scan strings *) |
|
196 |
||
197 |
val scan_str = |
|
7682
46de8064c93c
added Space, Comment token kinds (keep actual text);
wenzelm
parents:
7477
diff
changeset
|
198 |
scan_blank || |
9130 | 199 |
keep_line ($$ "\\") |-- !!! "bad escape character in string" |
9051 | 200 |
(scan_blank || keep_line ($$ "\"" || $$ "\\")) || |
6859 | 201 |
keep_line (Scan.one (not_equal "\\" andf not_equal "\"" andf |
202 |
Symbol.not_sync andf Symbol.not_eof)); |
|
5825 | 203 |
|
204 |
val scan_string = |
|
205 |
keep_line ($$ "\"") |-- |
|
9130 | 206 |
!!! "missing quote at end of string" |
5825 | 207 |
(change_prompt ((Scan.repeat scan_str >> implode) --| keep_line ($$ "\""))); |
208 |
||
209 |
||
210 |
(* scan verbatim text *) |
|
211 |
||
212 |
val scan_verb = |
|
213 |
scan_blank || |
|
6743
5d50225637c8
changed {| |} verbatim syntax to {* *} in order to simplify ProofGeneral setup;
wenzelm
parents:
5876
diff
changeset
|
214 |
keep_line ($$ "*" --| Scan.ahead (Scan.one (not_equal "}"))) || |
6859 | 215 |
keep_line (Scan.one (not_equal "*" andf Symbol.not_sync andf Symbol.not_eof)); |
5825 | 216 |
|
217 |
val scan_verbatim = |
|
6743
5d50225637c8
changed {| |} verbatim syntax to {* *} in order to simplify ProofGeneral setup;
wenzelm
parents:
5876
diff
changeset
|
218 |
keep_line ($$ "{" -- $$ "*") |-- |
9130 | 219 |
!!! "missing end of verbatim text" |
6743
5d50225637c8
changed {| |} verbatim syntax to {* *} in order to simplify ProofGeneral setup;
wenzelm
parents:
5876
diff
changeset
|
220 |
(change_prompt ((Scan.repeat scan_verb >> implode) --| keep_line ($$ "*" -- $$ "}"))); |
5825 | 221 |
|
222 |
||
223 |
(* scan space *) |
|
224 |
||
225 |
val is_space = Symbol.is_blank andf not_equal "\n"; |
|
226 |
||
227 |
val scan_space = |
|
7682
46de8064c93c
added Space, Comment token kinds (keep actual text);
wenzelm
parents:
7477
diff
changeset
|
228 |
(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
|
229 |
keep_line (Scan.any is_space) -- incr_line ($$ "\n")) >> (fn (cs, c) => implode cs ^ c); |
5825 | 230 |
|
231 |
||
232 |
(* scan nested comments *) |
|
233 |
||
234 |
val scan_cmt = |
|
235 |
Scan.lift scan_blank || |
|
236 |
Scan.depend (fn d => keep_line ($$ "(" ^^ $$ "*") >> pair (d + 1)) || |
|
237 |
Scan.depend (fn 0 => Scan.fail | d => keep_line ($$ "*" ^^ $$ ")") >> pair (d - 1)) || |
|
238 |
Scan.lift (keep_line ($$ "*" --| Scan.ahead (Scan.one (not_equal ")")))) || |
|
6859 | 239 |
Scan.lift (keep_line (Scan.one (not_equal "*" andf Symbol.not_sync andf Symbol.not_eof))); |
5825 | 240 |
|
241 |
val scan_comment = |
|
242 |
keep_line ($$ "(" -- $$ "*") |-- |
|
9130 | 243 |
!!! "missing end of comment" |
5825 | 244 |
(change_prompt |
7682
46de8064c93c
added Space, Comment token kinds (keep actual text);
wenzelm
parents:
7477
diff
changeset
|
245 |
(Scan.pass 0 (Scan.repeat scan_cmt >> implode) --| keep_line ($$ "*" -- $$ ")"))); |
5825 | 246 |
|
247 |
||
248 |
(* scan token *) |
|
249 |
||
9130 | 250 |
fun scan (lex1, lex2) = |
5825 | 251 |
let |
9130 | 252 |
val scanner = Scan.state :-- (fn pos => |
253 |
let |
|
254 |
fun token k x = Token (pos, (k, x)); |
|
255 |
fun sync _ = token Sync Symbol.sync; |
|
256 |
in |
|
257 |
scan_string >> token String || |
|
258 |
scan_verbatim >> token Verbatim || |
|
259 |
scan_space >> token Space || |
|
260 |
scan_comment >> token Comment || |
|
261 |
keep_line (Scan.one Symbol.is_sync >> sync) || |
|
262 |
keep_line (Scan.max token_leq |
|
263 |
(Scan.max token_leq |
|
264 |
(Scan.literal lex1 >> (token Keyword o implode)) |
|
265 |
(Scan.literal lex2 >> (token Command o implode))) |
|
266 |
(Syntax.scan_longid >> token LongIdent || |
|
267 |
Syntax.scan_id >> token Ident || |
|
268 |
Syntax.scan_var >> token Var || |
|
269 |
Syntax.scan_tid >> token TypeIdent || |
|
270 |
Syntax.scan_tvar >> token TypeVar || |
|
271 |
Syntax.scan_nat >> token Nat || |
|
272 |
scan_symid >> token SymIdent)) |
|
273 |
end) >> #2; |
|
14729 | 274 |
in !! (lex_err (fn cs => "bad input " ^ quote (Symbol.beginning 10 cs))) scanner end; |
5825 | 275 |
|
276 |
||
9130 | 277 |
(* token sources *) |
5825 | 278 |
|
6859 | 279 |
val is_junk = (not o Symbol.is_blank) andf Symbol.not_sync andf Symbol.not_eof; |
10748 | 280 |
fun recover xs = (keep_line (Scan.any is_junk) >> K [malformed]) xs; |
5825 | 281 |
|
282 |
fun source do_recover get_lex pos src = |
|
283 |
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
|
284 |
(if do_recover then Some recover else None) src; |
5825 | 285 |
|
9130 | 286 |
fun source_proper src = src |> Source.filter is_proper; |
287 |
||
288 |
||
289 |
(* lexicons *) |
|
290 |
||
291 |
val make_lexicon = Scan.make_lexicon o map Symbol.explode; |
|
5825 | 292 |
|
293 |
end; |