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