author | wenzelm |
Sun, 25 Jun 2000 23:52:15 +0200 | |
changeset 9130 | ff8789b49d2e |
parent 9051 | 887a15590f0e |
child 9155 | adfa40218e06 |
permissions | -rw-r--r-- |
5825 | 1 |
(* Title: Pure/Isar/outer_lex.ML |
2 |
ID: $Id$ |
|
3 |
Author: Markus Wenzel, TU Muenchen |
|
8807 | 4 |
License: GPL (GNU GENERAL PUBLIC LICENSE) |
5825 | 5 |
|
6 |
Outer lexical syntax for Isabelle/Isar. |
|
7 |
*) |
|
8 |
||
9 |
signature OUTER_LEX = |
|
10 |
sig |
|
11 |
datatype token_kind = |
|
7477 | 12 |
Command | Keyword | Ident | LongIdent | SymIdent | Var | TypeIdent | TypeVar | |
7682
46de8064c93c
added Space, Comment token kinds (keep actual text);
wenzelm
parents:
7477
diff
changeset
|
13 |
Nat | String | Verbatim | Space | Comment | Sync | EOF |
5825 | 14 |
type token |
15 |
val str_of_kind: token_kind -> string |
|
16 |
val stopper: token * (token -> bool) |
|
6859 | 17 |
val not_sync: token -> bool |
5825 | 18 |
val not_eof: token -> bool |
19 |
val position_of: token -> Position.T |
|
20 |
val pos_of: token -> string |
|
21 |
val is_kind: token_kind -> token -> bool |
|
7026 | 22 |
val keyword_with: (string -> bool) -> token -> bool |
5825 | 23 |
val name_of: token -> string |
24 |
val is_proper: token -> bool |
|
9130 | 25 |
val is_semicolon: token -> bool |
8580 | 26 |
val is_begin_ignore: token -> bool |
27 |
val is_end_ignore: token -> bool |
|
8651 | 28 |
val is_newline: token -> bool |
7902 | 29 |
val is_indent: token -> bool |
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 | |
7682
46de8064c93c
added Space, Comment token kinds (keep actual text);
wenzelm
parents:
7477
diff
changeset
|
57 |
Nat | String | Verbatim | Space | Comment | Sync | 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" |
5825 | 76 |
| EOF => "end-of-file"; |
77 |
||
78 |
||
6859 | 79 |
(* sync token *) |
80 |
||
81 |
fun not_sync (Token (_, (Sync, _))) = false |
|
82 |
| not_sync _ = true; |
|
83 |
||
84 |
||
5825 | 85 |
(* eof token *) |
86 |
||
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 |
||
96 |
(* get position *) |
|
97 |
||
98 |
fun position_of (Token (pos, _)) = pos; |
|
99 |
val pos_of = Position.str_of o position_of; |
|
100 |
||
101 |
||
102 |
(* kind of token *) |
|
103 |
||
104 |
fun is_kind k (Token (_, (k', _))) = k = k'; |
|
105 |
||
7026 | 106 |
fun keyword_with pred (Token (_, (Keyword, x))) = pred x |
107 |
| keyword_with _ _ = false; |
|
5825 | 108 |
|
109 |
fun name_of (Token (_, (k, _))) = str_of_kind k; |
|
110 |
||
7682
46de8064c93c
added Space, Comment token kinds (keep actual text);
wenzelm
parents:
7477
diff
changeset
|
111 |
fun is_proper (Token (_, (Space, _))) = false |
46de8064c93c
added Space, Comment token kinds (keep actual text);
wenzelm
parents:
7477
diff
changeset
|
112 |
| is_proper (Token (_, (Comment, _))) = false |
5825 | 113 |
| is_proper _ = true; |
114 |
||
9130 | 115 |
fun is_semicolon (Token (_, (Command, ";"))) = true |
116 |
| is_semicolon _ = false; |
|
117 |
||
8580 | 118 |
fun is_begin_ignore (Token (_, (Comment, "<"))) = true |
119 |
| is_begin_ignore _ = false; |
|
120 |
||
121 |
fun is_end_ignore (Token (_, (Comment, ">"))) = true |
|
122 |
| is_end_ignore _ = false; |
|
123 |
||
8651 | 124 |
|
125 |
(* newline and indentations (note that space tokens obey lines) *) |
|
126 |
||
127 |
fun is_newline (Token (_, (Space, "\n"))) = true |
|
128 |
| is_newline _ = false; |
|
129 |
||
7902 | 130 |
fun is_indent (Token (_, (Space, s))) = |
131 |
let val n = size s in n > 0 andalso String.substring (s, n - 1, 1) <> "\n" end |
|
132 |
| is_indent _ = false; |
|
133 |
||
5825 | 134 |
|
135 |
(* value of token *) |
|
136 |
||
137 |
fun val_of (Token (_, (_, x))) = x; |
|
138 |
||
139 |
fun token_leq (Token (_, (_, x)), Token (_, (_, x'))) = x <= x'; |
|
140 |
||
141 |
||
142 |
||
143 |
(** scanners **) |
|
144 |
||
145 |
fun change_prompt scan = Scan.prompt "# " scan; |
|
146 |
||
147 |
||
148 |
(* diagnostics *) |
|
149 |
||
150 |
fun lex_err msg ((pos, cs), _) = "Outer lexical error" ^ Position.str_of pos ^ ": " ^ msg cs; |
|
9130 | 151 |
fun !!! msg scan = Scan.!! (lex_err (K msg)) scan; |
5825 | 152 |
|
153 |
||
154 |
(* line numbering *) |
|
155 |
||
156 |
fun incr_line scan = Scan.depend (fn pos => scan >> pair (Position.inc pos)); |
|
157 |
val keep_line = Scan.lift; |
|
158 |
||
159 |
val scan_blank = |
|
160 |
incr_line ($$ "\n") || |
|
161 |
keep_line (Scan.one Symbol.is_blank); |
|
162 |
||
163 |
||
164 |
(* scan symbolic idents *) |
|
165 |
||
166 |
val sym_chars = explode "!#$%&*+-/:<=>?@^_`|~"; |
|
167 |
fun is_sym_char s = s mem sym_chars; |
|
168 |
||
8231 | 169 |
val scan_symid = |
170 |
Scan.any1 is_sym_char >> implode || |
|
171 |
Scan.one Symbol.is_symbolic; |
|
5825 | 172 |
|
8231 | 173 |
fun is_symid str = |
174 |
(case try Symbol.explode str of |
|
175 |
Some [s] => Symbol.is_symbolic s orelse is_sym_char s |
|
176 |
| Some ss => forall is_sym_char ss |
|
177 |
| _ => false); |
|
178 |
||
5876 | 179 |
val is_sid = is_symid orf Syntax.is_identifier; |
5825 | 180 |
|
181 |
||
182 |
(* scan strings *) |
|
183 |
||
184 |
val scan_str = |
|
7682
46de8064c93c
added Space, Comment token kinds (keep actual text);
wenzelm
parents:
7477
diff
changeset
|
185 |
scan_blank || |
9130 | 186 |
keep_line ($$ "\\") |-- !!! "bad escape character in string" |
9051 | 187 |
(scan_blank || keep_line ($$ "\"" || $$ "\\")) || |
6859 | 188 |
keep_line (Scan.one (not_equal "\\" andf not_equal "\"" andf |
189 |
Symbol.not_sync andf Symbol.not_eof)); |
|
5825 | 190 |
|
191 |
val scan_string = |
|
192 |
keep_line ($$ "\"") |-- |
|
9130 | 193 |
!!! "missing quote at end of string" |
5825 | 194 |
(change_prompt ((Scan.repeat scan_str >> implode) --| keep_line ($$ "\""))); |
195 |
||
196 |
||
197 |
(* scan verbatim text *) |
|
198 |
||
199 |
val scan_verb = |
|
200 |
scan_blank || |
|
6743
5d50225637c8
changed {| |} verbatim syntax to {* *} in order to simplify ProofGeneral setup;
wenzelm
parents:
5876
diff
changeset
|
201 |
keep_line ($$ "*" --| Scan.ahead (Scan.one (not_equal "}"))) || |
6859 | 202 |
keep_line (Scan.one (not_equal "*" andf Symbol.not_sync andf Symbol.not_eof)); |
5825 | 203 |
|
204 |
val scan_verbatim = |
|
6743
5d50225637c8
changed {| |} verbatim syntax to {* *} in order to simplify ProofGeneral setup;
wenzelm
parents:
5876
diff
changeset
|
205 |
keep_line ($$ "{" -- $$ "*") |-- |
9130 | 206 |
!!! "missing end of verbatim text" |
6743
5d50225637c8
changed {| |} verbatim syntax to {* *} in order to simplify ProofGeneral setup;
wenzelm
parents:
5876
diff
changeset
|
207 |
(change_prompt ((Scan.repeat scan_verb >> implode) --| keep_line ($$ "*" -- $$ "}"))); |
5825 | 208 |
|
209 |
||
210 |
(* scan space *) |
|
211 |
||
212 |
val is_space = Symbol.is_blank andf not_equal "\n"; |
|
213 |
||
214 |
val scan_space = |
|
7682
46de8064c93c
added Space, Comment token kinds (keep actual text);
wenzelm
parents:
7477
diff
changeset
|
215 |
(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
|
216 |
keep_line (Scan.any is_space) -- incr_line ($$ "\n")) >> (fn (cs, c) => implode cs ^ c); |
5825 | 217 |
|
218 |
||
219 |
(* scan nested comments *) |
|
220 |
||
221 |
val scan_cmt = |
|
222 |
Scan.lift scan_blank || |
|
223 |
Scan.depend (fn d => keep_line ($$ "(" ^^ $$ "*") >> pair (d + 1)) || |
|
224 |
Scan.depend (fn 0 => Scan.fail | d => keep_line ($$ "*" ^^ $$ ")") >> pair (d - 1)) || |
|
225 |
Scan.lift (keep_line ($$ "*" --| Scan.ahead (Scan.one (not_equal ")")))) || |
|
6859 | 226 |
Scan.lift (keep_line (Scan.one (not_equal "*" andf Symbol.not_sync andf Symbol.not_eof))); |
5825 | 227 |
|
228 |
val scan_comment = |
|
229 |
keep_line ($$ "(" -- $$ "*") |-- |
|
9130 | 230 |
!!! "missing end of comment" |
5825 | 231 |
(change_prompt |
7682
46de8064c93c
added Space, Comment token kinds (keep actual text);
wenzelm
parents:
7477
diff
changeset
|
232 |
(Scan.pass 0 (Scan.repeat scan_cmt >> implode) --| keep_line ($$ "*" -- $$ ")"))); |
5825 | 233 |
|
234 |
||
235 |
(* scan token *) |
|
236 |
||
9130 | 237 |
fun scan (lex1, lex2) = |
5825 | 238 |
let |
9130 | 239 |
val scanner = Scan.state :-- (fn pos => |
240 |
let |
|
241 |
fun token k x = Token (pos, (k, x)); |
|
242 |
fun sync _ = token Sync Symbol.sync; |
|
243 |
in |
|
244 |
scan_string >> token String || |
|
245 |
scan_verbatim >> token Verbatim || |
|
246 |
scan_space >> token Space || |
|
247 |
scan_comment >> token Comment || |
|
248 |
keep_line (Scan.one Symbol.is_sync >> sync) || |
|
249 |
keep_line (Scan.max token_leq |
|
250 |
(Scan.max token_leq |
|
251 |
(Scan.literal lex1 >> (token Keyword o implode)) |
|
252 |
(Scan.literal lex2 >> (token Command o implode))) |
|
253 |
(Syntax.scan_longid >> token LongIdent || |
|
254 |
Syntax.scan_id >> token Ident || |
|
255 |
Syntax.scan_var >> token Var || |
|
256 |
Syntax.scan_tid >> token TypeIdent || |
|
257 |
Syntax.scan_tvar >> token TypeVar || |
|
258 |
Syntax.scan_nat >> token Nat || |
|
259 |
scan_symid >> token SymIdent)) |
|
260 |
end) >> #2; |
|
261 |
in !! (lex_err (fn cs => "bad input " ^ quote (Symbol.beginning cs))) scanner end; |
|
5825 | 262 |
|
263 |
||
9130 | 264 |
(* token sources *) |
5825 | 265 |
|
6859 | 266 |
val is_junk = (not o Symbol.is_blank) andf Symbol.not_sync andf Symbol.not_eof; |
8663 | 267 |
fun recover xs = keep_line (Scan.any is_junk) xs; |
5825 | 268 |
|
269 |
fun source do_recover get_lex pos src = |
|
270 |
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
|
271 |
(if do_recover then Some recover else None) src; |
5825 | 272 |
|
9130 | 273 |
fun source_proper src = src |> Source.filter is_proper; |
274 |
||
275 |
||
276 |
(* lexicons *) |
|
277 |
||
278 |
val make_lexicon = Scan.make_lexicon o map Symbol.explode; |
|
5825 | 279 |
|
280 |
end; |