author | wenzelm |
Mon, 03 Nov 1997 14:09:16 +0100 | |
changeset 4099 | 0ec0d2dbe3f4 |
parent 4056 | abb0f4742ed7 |
child 4101 | e8ad51c88be9 |
permissions | -rw-r--r-- |
389 | 1 |
(* Title: Pure/Thy/thy_parse.ML |
2 |
ID: $Id$ |
|
3 |
Author: Markus Wenzel, TU Muenchen |
|
4 |
||
5 |
The parser for theory files. |
|
6 |
*) |
|
7 |
||
3900 | 8 |
(* FIXME tmp *) |
4011 | 9 |
val global_names = ref false; |
3900 | 10 |
|
11 |
||
389 | 12 |
infix 5 -- --$$ $$-- ^^; |
13 |
infix 3 >>; |
|
14 |
infix 0 ||; |
|
15 |
||
16 |
signature THY_PARSE = |
|
3977 | 17 |
sig |
389 | 18 |
type token |
19 |
val !! : ('a -> 'b * 'c) -> 'a -> 'b * 'c |
|
20 |
val >> : ('a -> 'b * 'c) * ('b -> 'd) -> 'a -> 'd * 'c |
|
21 |
val || : ('a -> 'b) * ('a -> 'b) -> 'a -> 'b |
|
22 |
val -- : ('a -> 'b * 'c) * ('c -> 'd * 'e) -> 'a -> ('b * 'd) * 'e |
|
23 |
val ^^ : ('a -> string * 'b) * ('b -> string * 'c) -> 'a -> string * 'c |
|
24 |
val $$ : string -> token list -> string * token list |
|
25 |
val $$-- : string * (token list -> 'b * 'c) -> token list -> 'b * 'c |
|
26 |
val --$$ : ('a -> 'b * token list) * string -> 'a -> 'b * token list |
|
27 |
val ident: token list -> string * token list |
|
28 |
val long_ident: token list -> string * token list |
|
29 |
val long_id: token list -> string * token list |
|
30 |
val type_var: token list -> string * token list |
|
636 | 31 |
val type_args: token list -> string list * token list |
389 | 32 |
val nat: token list -> string * token list |
33 |
val string: token list -> string * token list |
|
34 |
val verbatim: token list -> string * token list |
|
35 |
val empty: 'a -> 'b list * 'a |
|
36 |
val optional: ('a -> 'b * 'a) -> 'b -> 'a -> 'b * 'a |
|
37 |
val repeat: ('a -> 'b * 'a) -> 'a -> 'b list * 'a |
|
38 |
val repeat1: ('a -> 'b * 'a) -> 'a -> 'b list * 'a |
|
39 |
val enum: string -> (token list -> 'a * token list) |
|
40 |
-> token list -> 'a list * token list |
|
41 |
val enum1: string -> (token list -> 'a * token list) |
|
42 |
-> token list -> 'a list * token list |
|
43 |
val list: (token list -> 'a * token list) |
|
44 |
-> token list -> 'a list * token list |
|
45 |
val list1: (token list -> 'a * token list) |
|
46 |
-> token list -> 'a list * token list |
|
47 |
val name: token list -> string * token list |
|
48 |
val sort: token list -> string * token list |
|
3977 | 49 |
val typ: token list -> string * token list |
451 | 50 |
val opt_infix: token list -> string * token list |
51 |
val opt_mixfix: token list -> string * token list |
|
636 | 52 |
val opt_witness: token list -> string * token list |
4099 | 53 |
val const_decls: token list -> string * token list |
389 | 54 |
type syntax |
55 |
val make_syntax: string list -> |
|
56 |
(string * (token list -> (string * string) * token list)) list -> syntax |
|
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset
|
57 |
val parse_thy: syntax -> string -> string -> string |
389 | 58 |
val section: string -> string -> (token list -> string * token list) |
59 |
-> (string * (token list -> (string * string) * token list)) |
|
60 |
val axm_section: string -> string |
|
61 |
-> (token list -> (string * string list) * token list) |
|
62 |
-> (string * (token list -> (string * string) * token list)) |
|
63 |
val pure_keywords: string list |
|
64 |
val pure_sections: |
|
65 |
(string * (token list -> (string * string) * token list)) list |
|
570
6333c181a3f7
Pure/Thy/thy_parse/THY_PARSE: deleted duplicate specifications of parens,
lcp
parents:
558
diff
changeset
|
66 |
(*items for building strings*) |
710 | 67 |
val cat: string -> string -> string |
656 | 68 |
val parens: string -> string |
69 |
val brackets: string -> string |
|
70 |
val mk_list: string list -> string |
|
71 |
val mk_big_list: string list -> string |
|
72 |
val mk_pair: string * string -> string |
|
73 |
val mk_triple: string * string * string -> string |
|
74 |
val strip_quotes: string -> string |
|
3977 | 75 |
end; |
389 | 76 |
|
1512 | 77 |
|
78 |
structure ThyParse : THY_PARSE= |
|
389 | 79 |
struct |
80 |
||
81 |
open ThyScan; |
|
82 |
||
83 |
||
84 |
(** parser toolbox **) |
|
85 |
||
86 |
type token = token_kind * string * int; |
|
87 |
||
88 |
||
89 |
(* errors *) |
|
90 |
||
91 |
exception SYNTAX_ERROR of string * string * int; |
|
92 |
||
93 |
fun syn_err s1 s2 n = raise SYNTAX_ERROR (s1, s2, n); |
|
94 |
||
95 |
fun eof_err () = error "Unexpected end-of-file"; |
|
96 |
||
570
6333c181a3f7
Pure/Thy/thy_parse/THY_PARSE: deleted duplicate specifications of parens,
lcp
parents:
558
diff
changeset
|
97 |
(*Similar to Prolog's cut: reports any syntax error instead of backtracking |
6333c181a3f7
Pure/Thy/thy_parse/THY_PARSE: deleted duplicate specifications of parens,
lcp
parents:
558
diff
changeset
|
98 |
through a superior || *) |
389 | 99 |
fun !! parse toks = parse toks |
100 |
handle SYNTAX_ERROR (s1, s2, n) => error ("Syntax error on line " ^ |
|
101 |
string_of_int n ^ ": " ^ s1 ^ " expected and " ^ s2 ^ " was found"); |
|
102 |
||
103 |
||
104 |
(* parser combinators *) |
|
105 |
||
106 |
fun (parse >> f) toks = apfst f (parse toks); |
|
107 |
||
108 |
fun (parse1 || parse2) toks = |
|
109 |
parse1 toks handle SYNTAX_ERROR _ => parse2 toks; |
|
110 |
||
111 |
fun (parse1 -- parse2) toks = |
|
112 |
let |
|
113 |
val (x, toks') = parse1 toks; |
|
114 |
val (y, toks'') = parse2 toks'; |
|
115 |
in |
|
116 |
((x, y), toks'') |
|
117 |
end; |
|
118 |
||
119 |
fun (parse1 ^^ parse2) = parse1 -- parse2 >> op ^; |
|
120 |
||
121 |
||
122 |
(* generic parsers *) |
|
123 |
||
124 |
fun $$ a ((k, b, n) :: toks) = |
|
125 |
if k = Keyword andalso a = b then (a, toks) |
|
126 |
else syn_err (quote a) (quote b) n |
|
127 |
| $$ _ [] = eof_err (); |
|
128 |
||
129 |
fun (a $$-- parse) = $$ a -- parse >> #2; |
|
130 |
||
131 |
fun (parse --$$ a) = parse -- $$ a >> #1; |
|
132 |
||
133 |
||
134 |
fun kind k1 ((k2, s, n) :: toks) = |
|
135 |
if k1 = k2 then (s, toks) |
|
136 |
else syn_err (name_of_kind k1) (quote s) n |
|
137 |
| kind _ [] = eof_err (); |
|
138 |
||
139 |
val ident = kind Ident; |
|
140 |
val long_ident = kind LongIdent; |
|
141 |
val long_id = ident || long_ident; |
|
142 |
val type_var = kind TypeVar >> quote; |
|
143 |
val nat = kind Nat; |
|
144 |
val string = kind String; |
|
145 |
val verbatim = kind Verbatim; |
|
146 |
val eof = kind EOF; |
|
147 |
||
148 |
fun empty toks = ([], toks); |
|
149 |
||
150 |
fun optional parse def = parse || empty >> K def; |
|
151 |
||
152 |
fun repeat parse toks = (parse -- repeat parse >> op :: || empty) toks; |
|
153 |
fun repeat1 parse = parse -- repeat parse >> op ::; |
|
154 |
||
155 |
fun enum1 sep parse = parse -- repeat (sep $$-- parse) >> op ::; |
|
156 |
fun enum sep parse = enum1 sep parse || empty; |
|
157 |
||
2231
71385461570a
Eta-expansion of a function definition, for value polymorphism
paulson
parents:
2203
diff
changeset
|
158 |
fun list1 parse = enum1 "," parse; |
3977 | 159 |
fun list parse = enum "," parse; |
160 |
||
389 | 161 |
|
162 |
||
163 |
(** theory parsers **) |
|
164 |
||
165 |
(* misc utilities *) |
|
166 |
||
167 |
fun cat s1 s2 = s1 ^ " " ^ s2; |
|
168 |
||
558 | 169 |
val parens = enclose "(" ")"; |
170 |
val brackets = enclose "[" "]"; |
|
389 | 171 |
|
172 |
val mk_list = brackets o commas; |
|
173 |
val mk_big_list = brackets o space_implode ",\n "; |
|
174 |
||
558 | 175 |
fun mk_pair (x, y) = parens (commas [x, y]); |
176 |
fun mk_triple (x, y, z) = parens (commas [x, y, z]); |
|
389 | 177 |
fun mk_triple1 ((x, y), z) = mk_triple (x, y, z); |
178 |
fun mk_triple2 (x, (y, z)) = mk_triple (x, y, z); |
|
179 |
||
2231
71385461570a
Eta-expansion of a function definition, for value polymorphism
paulson
parents:
2203
diff
changeset
|
180 |
fun split_decls l = flat (map (fn (xs, y) => map (rpair y) xs) l); |
389 | 181 |
|
182 |
fun strip_quotes str = |
|
183 |
implode (tl (take (size str - 1, explode str))); |
|
184 |
||
185 |
||
186 |
(* names *) |
|
187 |
||
188 |
val name = ident >> quote || string; |
|
189 |
val names = list name; |
|
190 |
val names1 = list1 name; |
|
191 |
val name_list = names >> mk_list; |
|
192 |
val name_list1 = names1 >> mk_list; |
|
193 |
||
194 |
||
195 |
(* classes *) |
|
196 |
||
197 |
val subclass = name -- optional ("<" $$-- !! name_list1) "[]"; |
|
198 |
||
558 | 199 |
val class_decls = repeat1 (subclass >> mk_pair) >> mk_big_list; |
389 | 200 |
|
201 |
||
202 |
(* arities *) |
|
203 |
||
204 |
val sort = |
|
205 |
name >> brackets || |
|
206 |
"{" $$-- name_list --$$ "}"; |
|
207 |
||
208 |
val sort_list1 = list1 sort >> mk_list; |
|
209 |
||
210 |
||
211 |
val arity = optional ("(" $$-- !! (sort_list1 --$$")")) "[]" -- sort; |
|
212 |
||
213 |
val arity_decls = repeat1 (names1 --$$ "::" -- !! arity) |
|
214 |
>> (mk_big_list o map mk_triple2 o split_decls); |
|
215 |
||
216 |
||
217 |
(* mixfix annotations *) |
|
218 |
||
2203 | 219 |
val infxl = |
220 |
"infixl" $$-- !! (nat >> cat "Infixl" || string -- nat >> (cat "InfixlName" o mk_pair)); |
|
221 |
val infxr = |
|
222 |
"infixr" $$-- !! (nat >> cat "Infixr" || string -- nat >> (cat "InfixrName" o mk_pair)); |
|
389 | 223 |
|
889 | 224 |
val binder = "binder" $$-- |
2203 | 225 |
!! (string -- (("[" $$-- nat --$$ "]") -- nat || nat >> (fn n => (n, n)))) |
226 |
>> (cat "Binder" o mk_triple2); |
|
389 | 227 |
|
228 |
val opt_pris = optional ("[" $$-- !! (list nat --$$ "]")) [] >> mk_list; |
|
229 |
||
230 |
val mixfix = string -- !! (opt_pris -- optional nat "max_pri") |
|
231 |
>> (cat "Mixfix" o mk_triple2); |
|
232 |
||
558 | 233 |
fun opt_syn fx = optional ("(" $$-- fx --$$ ")") "NoSyn"; |
389 | 234 |
|
235 |
val opt_infix = opt_syn (infxl || infxr); |
|
236 |
val opt_mixfix = opt_syn (mixfix || infxl || infxr || binder); |
|
237 |
||
238 |
||
239 |
(* types *) |
|
240 |
||
3977 | 241 |
(* FIXME clean!! *) |
242 |
||
1705
19fe0ab646b4
changed ident_no_colon so that it forbids postfix "=", too
clasohm
parents:
1555
diff
changeset
|
243 |
(*Parse an identifier, but only if it is not followed by "::", "=" or ","; |
1377 | 244 |
the exclusion of a postfix comma can be controlled to allow expressions |
245 |
like "(id, id)" but disallow ones like "'a => id id,id :: ..."*) |
|
246 |
fun ident_no_colon _ [] = eof_err() |
|
247 |
| ident_no_colon allow_comma ((Ident, s, n) :: (rest as (Keyword, s2, n2) :: |
|
248 |
toks)) = |
|
1705
19fe0ab646b4
changed ident_no_colon so that it forbids postfix "=", too
clasohm
parents:
1555
diff
changeset
|
249 |
if s2 = "::" orelse s2 = "=" orelse (not allow_comma andalso s2 = ",") |
19fe0ab646b4
changed ident_no_colon so that it forbids postfix "=", too
clasohm
parents:
1555
diff
changeset
|
250 |
then syn_err (name_of_kind Ident) (quote s2) n2 |
1377 | 251 |
else (s, rest) |
252 |
| ident_no_colon _ ((Ident, s, n) :: toks) = (s, toks) |
|
253 |
| ident_no_colon _ ((k, s, n) :: _) = |
|
254 |
syn_err (name_of_kind Ident) (quote s) n; |
|
1321
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
255 |
|
3110 | 256 |
(*type used in types, consts and syntax sections*) |
1377 | 257 |
fun const_type allow_comma toks = |
3977 | 258 |
let |
259 |
val simple_type = |
|
260 |
(ident || kind TypeVar ^^ optional ($$ "::" ^^ ident) "") -- |
|
261 |
repeat (ident_no_colon allow_comma) |
|
262 |
>> (fn (args, ts) => cat args (space_implode " " ts)) || |
|
263 |
("(" $$-- (list1 (const_type true)) --$$ ")" >> (parens o commas)) -- |
|
264 |
repeat1 (ident_no_colon allow_comma) |
|
265 |
>> (fn (args, ts) => cat args (space_implode " " ts)); |
|
1377 | 266 |
|
267 |
val appl_param = |
|
268 |
simple_type || "(" $$-- const_type true --$$ ")" >> parens || |
|
269 |
"[" $$-- (list1 (const_type true)) --$$ "]" --$$ "=>" -- |
|
270 |
const_type allow_comma >> |
|
271 |
(fn (src, dest) => mk_list src ^ " => " ^ dest); |
|
272 |
in ("[" $$-- (list1 (const_type true)) --$$ "]" --$$ "=>" -- |
|
273 |
const_type allow_comma >> |
|
274 |
(fn (src, dest) => mk_list src ^ " => " ^ dest) || |
|
275 |
repeat1 (appl_param --$$ "=>") -- const_type allow_comma >> |
|
276 |
(fn (src, dest) => space_implode " => " (src@[dest])) || |
|
277 |
simple_type || |
|
278 |
"(" $$-- const_type true --$$ ")" >> parens) toks |
|
1321
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
279 |
end; |
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
280 |
|
3977 | 281 |
val typ = string || (const_type false >> quote); |
282 |
||
283 |
||
1383 | 284 |
fun mk_old_type_decl ((ts, n), syn) = |
285 |
map (fn t => (mk_triple (t, n, syn), false)) ts; |
|
286 |
||
287 |
fun mk_type_decl (((xs, t), None), syn) = |
|
288 |
[(mk_triple (t, string_of_int (length xs), syn), false)] |
|
289 |
| mk_type_decl (((xs, t), Some rhs), syn) = |
|
290 |
[(parens (commas [t, mk_list xs, rhs, syn]), true)]; |
|
291 |
||
292 |
fun mk_type_decls tys = |
|
3764 | 293 |
"|> Theory.add_types\n" ^ mk_big_list (keyfilter tys false) ^ "\n\n\ |
294 |
\|> Theory.add_tyabbrs\n" ^ mk_big_list (keyfilter tys true); |
|
1383 | 295 |
|
296 |
||
297 |
val old_type_decl = names1 -- nat -- opt_infix >> mk_old_type_decl; |
|
298 |
||
299 |
val type_args = |
|
300 |
type_var >> (fn x => [x]) || |
|
301 |
"(" $$-- !! (list1 type_var --$$ ")") || |
|
302 |
empty >> K []; |
|
303 |
||
304 |
val type_decl = type_args -- name -- |
|
3977 | 305 |
optional ("=" $$-- typ >> Some) None -- opt_infix >> mk_type_decl; |
1383 | 306 |
|
2360 | 307 |
val type_decls = |
308 |
repeat1 (old_type_decl || type_decl) >> (mk_type_decls o flat); |
|
1383 | 309 |
|
310 |
||
311 |
(* consts *) |
|
312 |
||
2360 | 313 |
val const_decls = |
3977 | 314 |
repeat1 (names1 --$$ "::" -- !! (typ -- opt_mixfix)) |
2360 | 315 |
>> (mk_big_list o map mk_triple2 o split_decls); |
389 | 316 |
|
2360 | 317 |
val opt_mode = |
318 |
optional |
|
319 |
("(" $$-- !! (name -- optional ($$ "output" >> K "false") "true" --$$ ")")) |
|
320 |
("\"\"", "true") |
|
321 |
>> mk_pair; |
|
322 |
||
323 |
val syntax_decls = opt_mode -- const_decls >> (fn (mode, txt) => mode ^ "\n" ^ txt); |
|
2203 | 324 |
|
389 | 325 |
|
326 |
(* translations *) |
|
327 |
||
328 |
val trans_pat = |
|
329 |
optional ("(" $$-- !! (name --$$ ")")) "\"logic\"" -- string >> mk_pair; |
|
330 |
||
331 |
val trans_arrow = |
|
3528 | 332 |
$$ "=>" >> K "Syntax.ParseRule " || |
333 |
$$ "<=" >> K "Syntax.PrintRule " || |
|
334 |
$$ "==" >> K "Syntax.ParsePrintRule "; |
|
389 | 335 |
|
1810
0eef167ebe1b
Translation infixes <->, etc., no longer available at top-level
paulson
parents:
1705
diff
changeset
|
336 |
val trans_line = |
2203 | 337 |
trans_pat -- !! (trans_arrow -- trans_pat) |
338 |
>> (fn (left, (arr, right)) => arr ^ mk_pair (left, right)); |
|
1810
0eef167ebe1b
Translation infixes <->, etc., no longer available at top-level
paulson
parents:
1705
diff
changeset
|
339 |
|
0eef167ebe1b
Translation infixes <->, etc., no longer available at top-level
paulson
parents:
1705
diff
changeset
|
340 |
val trans_decls = repeat1 trans_line >> mk_big_list; |
389 | 341 |
|
342 |
||
343 |
(* ML translations *) |
|
344 |
||
4047 | 345 |
val local_defs = |
389 | 346 |
" val parse_ast_translation = [];\n\ |
347 |
\ val parse_translation = [];\n\ |
|
348 |
\ val print_translation = [];\n\ |
|
2385 | 349 |
\ val typed_print_translation = [];\n\ |
2694 | 350 |
\ val print_ast_translation = [];\n\ |
4047 | 351 |
\ val token_translation = [];\n\ |
352 |
\ val thy_data = []"; |
|
389 | 353 |
|
354 |
val trfun_args = |
|
355 |
"(parse_ast_translation, parse_translation, \ |
|
356 |
\print_translation, print_ast_translation)"; |
|
357 |
||
358 |
||
359 |
(* axioms *) |
|
360 |
||
361 |
val mk_axms = mk_big_list o map (mk_pair o apfst quote); |
|
362 |
||
363 |
fun mk_axiom_decls axms = (mk_axms axms, map fst axms); |
|
364 |
||
365 |
val axiom_decls = repeat1 (ident -- !! string) >> mk_axiom_decls; |
|
366 |
||
367 |
||
3813 | 368 |
(* oracle *) |
369 |
||
370 |
val oracle_decl = (name --$$ "=" -- long_id) >> mk_pair; |
|
371 |
||
372 |
||
1555 | 373 |
(* combined consts and axioms *) |
374 |
||
375 |
fun mk_constaxiom_decls x = |
|
376 |
let |
|
377 |
val (axms_defs, axms_names) = |
|
378 |
mk_axiom_decls (map (fn ((id, _), def) => (id ^ "_def", def)) x); |
|
379 |
in ((mk_big_list o map mk_triple2 o map (apfst quote o fst)) x ^ |
|
4020 | 380 |
"\n\n|> PureThy.add_store_defs\n" ^ axms_defs, axms_names) |
1555 | 381 |
end; |
382 |
||
383 |
val constaxiom_decls = |
|
3977 | 384 |
repeat1 (ident --$$ "::" -- !! (typ -- opt_mixfix) -- !! string) |
385 |
>> mk_constaxiom_decls; |
|
1555 | 386 |
|
387 |
||
389 | 388 |
(* axclass *) |
389 |
||
390 |
fun mk_axclass_decl ((c, cs), axms) = |
|
391 |
(mk_pair (c, cs) ^ "\n" ^ mk_axms axms, |
|
392 |
(strip_quotes c ^ "I") :: map fst axms); |
|
393 |
||
394 |
val axclass_decl = subclass -- repeat (ident -- !! string) >> mk_axclass_decl; |
|
395 |
||
396 |
||
451 | 397 |
(* instance *) |
389 | 398 |
|
425 | 399 |
fun mk_witness (axths, opt_tac) = |
389 | 400 |
mk_list (keyfilter axths false) ^ "\n" ^ |
401 |
mk_list (keyfilter axths true) ^ "\n" ^ |
|
402 |
opt_tac; |
|
403 |
||
404 |
val axm_or_thm = |
|
405 |
string >> rpair false || |
|
406 |
long_id >> rpair true; |
|
407 |
||
451 | 408 |
|
425 | 409 |
val opt_witness = |
389 | 410 |
optional ("(" $$-- list1 axm_or_thm --$$ ")") [] -- |
558 | 411 |
optional (verbatim >> (parens o cat "Some" o parens)) "None" |
425 | 412 |
>> mk_witness; |
413 |
||
414 |
val instance_decl = |
|
636 | 415 |
(name --$$ "<" -- name >> (pair "|> AxClass.add_inst_subclass" o mk_pair) || |
416 |
name --$$ "::" -- arity >> (pair "|> AxClass.add_inst_arity" o mk_triple2)) |
|
451 | 417 |
-- opt_witness |
777 | 418 |
>> (fn ((x, y), z) => (cat_lines [x, y, z])); |
389 | 419 |
|
420 |
||
3931 | 421 |
(* local, global path *) |
422 |
||
3958 | 423 |
fun empty_decl toks = (empty >> K "") toks; |
3900 | 424 |
|
3931 | 425 |
val global_path = |
426 |
"|> (fn thy => if ! global_names then thy else Theory.add_path \"/\" thy)"; |
|
427 |
||
428 |
val local_path = |
|
429 |
global_path ^ "\n\ |
|
430 |
\|> (fn thy => if ! global_names then thy\ |
|
431 |
\ else Theory.add_path thy_name thy)"; |
|
3900 | 432 |
|
433 |
||
389 | 434 |
|
435 |
(** theory syntax **) |
|
436 |
||
437 |
type syntax = |
|
438 |
lexicon * (token list -> (string * string) * token list) Symtab.table; |
|
439 |
||
440 |
fun make_syntax keywords sects = |
|
4056 | 441 |
let |
442 |
val dups = duplicates (map fst sects); |
|
443 |
val sects' = gen_distinct eq_fst sects; |
|
444 |
in |
|
445 |
if null dups then () |
|
446 |
else warning ("Duplicate declaration of theory file sections:\n" ^ commas_quote dups); |
|
447 |
(make_lexicon (map fst sects' @ keywords), Symtab.make sects') |
|
448 |
end; |
|
389 | 449 |
|
450 |
||
451 |
(* header *) |
|
452 |
||
453 |
fun mk_header (thy_name, bases) = |
|
586
201e115d8031
renamed base_on into mk_base and moved it to the beginning of the generated
clasohm
parents:
570
diff
changeset
|
454 |
(thy_name, "mk_base " ^ mk_list bases ^ " " ^ quote thy_name); |
389 | 455 |
|
456 |
val base = |
|
457 |
ident >> (cat "Thy" o quote) || |
|
458 |
string >> cat "File"; |
|
459 |
||
460 |
val header = ident --$$ "=" -- enum1 "+" base >> mk_header; |
|
461 |
||
462 |
||
463 |
(* extension *) |
|
464 |
||
3900 | 465 |
fun mk_extension (txts, mltxt) = |
389 | 466 |
let |
467 |
val cat_sects = space_implode "\n\n" o filter_out (equal ""); |
|
468 |
val (extxts, postxts) = split_list txts; |
|
469 |
in |
|
3900 | 470 |
(cat_sects extxts, cat_sects postxts, mltxt) |
389 | 471 |
end; |
472 |
||
473 |
fun sect tab ((Keyword, s, n) :: toks) = |
|
474 |
(case Symtab.lookup (tab, s) of |
|
475 |
Some parse => !! parse toks |
|
476 |
| None => syn_err "section" s n) |
|
477 |
| sect _ ((_, s, n) :: _) = syn_err "section" s n |
|
478 |
| sect _ [] = eof_err (); |
|
479 |
||
3900 | 480 |
fun extension sectab = "+" $$-- !! |
481 |
(repeat (sect sectab) --$$ "end" -- optional ("ML" $$-- verbatim) "") |
|
482 |
>> mk_extension; |
|
3813 | 483 |
|
3900 | 484 |
fun opt_extension sectab = optional (extension sectab) ("", "", ""); |
3875 | 485 |
|
389 | 486 |
|
487 |
(* theory definition *) |
|
488 |
||
3900 | 489 |
fun mk_structure tname ((thy_name, old_thys), (extxt, postxt, mltxt)) = |
558 | 490 |
if thy_name <> tname then |
491 |
error ("Filename \"" ^ tname ^ ".thy\" and theory name " |
|
492 |
^ quote thy_name ^ " are different") |
|
493 |
else |
|
3875 | 494 |
"val thy = " ^ old_thys ^ ";\n\n\ |
495 |
\structure " ^ thy_name ^ " =\n\ |
|
496 |
\struct\n\ |
|
497 |
\\n\ |
|
3931 | 498 |
\local\n\ |
499 |
\ val thy_name = \"" ^ tname ^ "\";\n" |
|
4047 | 500 |
^ local_defs ^ "\n\ |
3875 | 501 |
\in\n\ |
502 |
\\n" |
|
503 |
^ mltxt ^ "\n\ |
|
504 |
\\n\ |
|
505 |
\val thy = thy\n\ |
|
3931 | 506 |
\\n" |
507 |
^ local_path ^ |
|
3875 | 508 |
"\n\ |
509 |
\|> Theory.add_trfuns\n" |
|
510 |
^ trfun_args ^ "\n\ |
|
4047 | 511 |
\|> Theory.add_trfunsT typed_print_translation\n\ |
512 |
\|> Theory.add_tokentrfuns token_translation\n\ |
|
513 |
\|> Theory.init_data thy_data\n\ |
|
3875 | 514 |
\\n" |
515 |
^ extxt ^ "\n\ |
|
516 |
\\n\ |
|
517 |
\|> Theory.add_name " ^ quote thy_name ^ ";\n\ |
|
518 |
\\n\ |
|
519 |
\val _ = store_theory (thy, " ^ quote thy_name ^ ");\n\ |
|
4056 | 520 |
\val _ = context thy;\n\ |
3875 | 521 |
\\n\ |
522 |
\\n" |
|
523 |
^ postxt ^ "\n\ |
|
524 |
\\n\ |
|
525 |
\end;\n\ |
|
526 |
\end;\n\ |
|
527 |
\\n\ |
|
528 |
\open " ^ thy_name ^ ";\n\ |
|
529 |
\\n"; |
|
389 | 530 |
|
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset
|
531 |
fun theory_defn sectab tname = |
3875 | 532 |
header -- opt_extension sectab -- eof >> (mk_structure tname o #1); |
389 | 533 |
|
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset
|
534 |
fun parse_thy (lex, sectab) tname str = |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset
|
535 |
#1 (!! (theory_defn sectab tname) (tokenize lex str)); |
389 | 536 |
|
537 |
||
538 |
(* standard sections *) |
|
539 |
||
540 |
fun mk_val ax = "val " ^ ax ^ " = get_axiom thy " ^ quote ax ^ ";"; |
|
777 | 541 |
val mk_vals = cat_lines o map mk_val; |
389 | 542 |
|
777 | 543 |
fun mk_axm_sect "" (txt, axs) = (txt, mk_vals axs) |
544 |
| mk_axm_sect pretxt (txt, axs) = (pretxt ^ "\n" ^ txt, mk_vals axs); |
|
389 | 545 |
|
546 |
fun axm_section name pretxt parse = |
|
547 |
(name, parse >> mk_axm_sect pretxt); |
|
548 |
||
549 |
fun section name pretxt parse = |
|
550 |
axm_section name pretxt (parse >> rpair []); |
|
551 |
||
552 |
||
553 |
val pure_keywords = |
|
3900 | 554 |
["end", "ML", "mixfix", "infixr", "infixl", "binder", "global", |
3931 | 555 |
"local", "output", "=", "+", ",", "<", "{", "}", "(", ")", "[", "]", |
556 |
"::", "==", "=>", "<="]; |
|
389 | 557 |
|
558 |
val pure_sections = |
|
3813 | 559 |
[section "classes" "|> Theory.add_classes" class_decls, |
3764 | 560 |
section "default" "|> Theory.add_defsort" sort, |
777 | 561 |
section "types" "" type_decls, |
3764 | 562 |
section "arities" "|> Theory.add_arities" arity_decls, |
563 |
section "consts" "|> Theory.add_consts" const_decls, |
|
564 |
section "syntax" "|> Theory.add_modesyntax" syntax_decls, |
|
565 |
section "translations" "|> Theory.add_trrules" trans_decls, |
|
4020 | 566 |
axm_section "rules" "|> PureThy.add_store_axioms" axiom_decls, |
567 |
axm_section "defs" "|> PureThy.add_store_defs" axiom_decls, |
|
3813 | 568 |
section "oracle" "|> Theory.add_oracle" oracle_decl, |
3764 | 569 |
axm_section "constdefs" "|> Theory.add_consts" constaxiom_decls, |
636 | 570 |
axm_section "axclass" "|> AxClass.add_axclass" axclass_decl, |
3780 | 571 |
section "instance" "" instance_decl, |
3900 | 572 |
section "path" "|> Theory.add_path" name, |
3931 | 573 |
section "global" global_path empty_decl, |
574 |
section "local" local_path empty_decl]; |
|
389 | 575 |
|
3813 | 576 |
|
389 | 577 |
end; |