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