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