src/Pure/Thy/thy_parse.ML
author wenzelm
Thu May 19 16:26:19 1994 +0200 (1994-05-19)
changeset 389 85105a7fb668
child 414 9dca566d6d96
permissions -rw-r--r--
(replaces Thy/parse.ML and Thy/syntax.ML)
much simpler because of new theory extension functions;
theory is now built up from an arbitrary list of definable 'sections';
new axclass and instance sections;
wenzelm@389
     1
(*  Title:      Pure/Thy/thy_parse.ML
wenzelm@389
     2
    ID:         $Id$
wenzelm@389
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@389
     4
wenzelm@389
     5
The parser for theory files.
wenzelm@389
     6
wenzelm@389
     7
TODO:
wenzelm@389
     8
  remove quote in syn_err (?)
wenzelm@389
     9
  check: names vs names1
wenzelm@389
    10
*)
wenzelm@389
    11
wenzelm@389
    12
infix 5 -- --$$ $$-- ^^;
wenzelm@389
    13
infix 3 >>;
wenzelm@389
    14
infix 0 ||;
wenzelm@389
    15
wenzelm@389
    16
signature THY_PARSE =
wenzelm@389
    17
sig
wenzelm@389
    18
  type token
wenzelm@389
    19
  val !! : ('a -> 'b * 'c) -> 'a -> 'b * 'c
wenzelm@389
    20
  val >> : ('a -> 'b * 'c) * ('b -> 'd) -> 'a -> 'd * 'c
wenzelm@389
    21
  val || : ('a -> 'b) * ('a -> 'b) -> 'a -> 'b
wenzelm@389
    22
  val -- : ('a -> 'b * 'c) * ('c -> 'd * 'e) -> 'a -> ('b * 'd) * 'e
wenzelm@389
    23
  val ^^ : ('a -> string * 'b) * ('b -> string * 'c) -> 'a -> string * 'c
wenzelm@389
    24
  val $$ : string -> token list -> string * token list
wenzelm@389
    25
  val $$-- : string * (token list -> 'b * 'c) -> token list -> 'b * 'c
wenzelm@389
    26
  val --$$ : ('a -> 'b * token list) * string -> 'a -> 'b * token list
wenzelm@389
    27
  val ident: token list -> string * token list
wenzelm@389
    28
  val long_ident: token list -> string * token list
wenzelm@389
    29
  val long_id: token list -> string * token list
wenzelm@389
    30
  val type_var: token list -> string * token list
wenzelm@389
    31
  val nat: token list -> string * token list
wenzelm@389
    32
  val string: token list -> string * token list
wenzelm@389
    33
  val verbatim: token list -> string * token list
wenzelm@389
    34
  val empty: 'a -> 'b list * 'a
wenzelm@389
    35
  val optional: ('a -> 'b * 'a) -> 'b -> 'a -> 'b * 'a
wenzelm@389
    36
  val repeat: ('a -> 'b * 'a) -> 'a -> 'b list * 'a
wenzelm@389
    37
  val repeat1: ('a -> 'b * 'a) -> 'a -> 'b list * 'a
wenzelm@389
    38
  val enum: string -> (token list -> 'a * token list)
wenzelm@389
    39
    -> token list -> 'a list * token list
wenzelm@389
    40
  val enum1: string -> (token list -> 'a * token list)
wenzelm@389
    41
    -> token list -> 'a list * token list
wenzelm@389
    42
  val list: (token list -> 'a * token list)
wenzelm@389
    43
    -> token list -> 'a list * token list
wenzelm@389
    44
  val list1: (token list -> 'a * token list)
wenzelm@389
    45
    -> token list -> 'a list * token list
wenzelm@389
    46
  val name: token list -> string * token list
wenzelm@389
    47
  val sort: token list -> string * token list
wenzelm@389
    48
  type syntax
wenzelm@389
    49
  val make_syntax: string list ->
wenzelm@389
    50
    (string * (token list -> (string * string) * token list)) list -> syntax
wenzelm@389
    51
  val parse_thy: syntax -> string -> string
wenzelm@389
    52
  val section: string -> string -> (token list -> string * token list)
wenzelm@389
    53
    -> (string * (token list -> (string * string) * token list))
wenzelm@389
    54
  val axm_section: string -> string
wenzelm@389
    55
    -> (token list -> (string * string list) * token list)
wenzelm@389
    56
    -> (string * (token list -> (string * string) * token list))
wenzelm@389
    57
  val pure_keywords: string list
wenzelm@389
    58
  val pure_sections:
wenzelm@389
    59
    (string * (token list -> (string * string) * token list)) list
wenzelm@389
    60
  val pure_syntax: syntax
wenzelm@389
    61
end;
wenzelm@389
    62
wenzelm@389
    63
functor ThyParseFun(structure Symtab: SYMTAB and ThyScan: THY_SCAN)(*: THY_PARSE *) = (* FIXME *)
wenzelm@389
    64
struct
wenzelm@389
    65
wenzelm@389
    66
open ThyScan;
wenzelm@389
    67
wenzelm@389
    68
wenzelm@389
    69
(** parser toolbox **)
wenzelm@389
    70
wenzelm@389
    71
type token = token_kind * string * int;
wenzelm@389
    72
wenzelm@389
    73
wenzelm@389
    74
(* errors *)
wenzelm@389
    75
wenzelm@389
    76
exception SYNTAX_ERROR of string * string * int;
wenzelm@389
    77
wenzelm@389
    78
fun syn_err s1 s2 n = raise SYNTAX_ERROR (s1, s2, n);
wenzelm@389
    79
wenzelm@389
    80
fun eof_err () = error "Unexpected end-of-file";
wenzelm@389
    81
wenzelm@389
    82
fun !! parse toks = parse toks
wenzelm@389
    83
  handle SYNTAX_ERROR (s1, s2, n) => error ("Syntax error on line " ^
wenzelm@389
    84
    string_of_int n ^ ": " ^ s1 ^ " expected and " ^ s2 ^ " was found");
wenzelm@389
    85
wenzelm@389
    86
wenzelm@389
    87
(* parser combinators *)
wenzelm@389
    88
wenzelm@389
    89
fun (parse >> f) toks = apfst f (parse toks);
wenzelm@389
    90
wenzelm@389
    91
fun (parse1 || parse2) toks =
wenzelm@389
    92
  parse1 toks handle SYNTAX_ERROR _ => parse2 toks;
wenzelm@389
    93
wenzelm@389
    94
fun (parse1 -- parse2) toks =
wenzelm@389
    95
  let
wenzelm@389
    96
    val (x, toks') = parse1 toks;
wenzelm@389
    97
    val (y, toks'') = parse2 toks';
wenzelm@389
    98
  in
wenzelm@389
    99
    ((x, y), toks'')
wenzelm@389
   100
  end;
wenzelm@389
   101
wenzelm@389
   102
fun (parse1 ^^ parse2) = parse1 -- parse2 >> op ^;
wenzelm@389
   103
wenzelm@389
   104
wenzelm@389
   105
(* generic parsers *)
wenzelm@389
   106
wenzelm@389
   107
fun $$ a ((k, b, n) :: toks) =
wenzelm@389
   108
      if k = Keyword andalso a = b then (a, toks)
wenzelm@389
   109
      else syn_err (quote a) (quote b) n
wenzelm@389
   110
  | $$ _ [] = eof_err ();
wenzelm@389
   111
wenzelm@389
   112
fun (a $$-- parse) = $$ a -- parse >> #2;
wenzelm@389
   113
wenzelm@389
   114
fun (parse --$$ a) = parse -- $$ a >> #1;
wenzelm@389
   115
wenzelm@389
   116
wenzelm@389
   117
fun kind k1 ((k2, s, n) :: toks) =
wenzelm@389
   118
      if k1 = k2 then (s, toks)
wenzelm@389
   119
      else syn_err (name_of_kind k1) (quote s) n
wenzelm@389
   120
  | kind _ [] = eof_err ();
wenzelm@389
   121
wenzelm@389
   122
val ident = kind Ident;
wenzelm@389
   123
val long_ident = kind LongIdent;
wenzelm@389
   124
val long_id = ident || long_ident;
wenzelm@389
   125
val type_var = kind TypeVar >> quote;
wenzelm@389
   126
val nat = kind Nat;
wenzelm@389
   127
val string = kind String;
wenzelm@389
   128
val verbatim = kind Verbatim;
wenzelm@389
   129
val eof = kind EOF;
wenzelm@389
   130
wenzelm@389
   131
fun empty toks = ([], toks);
wenzelm@389
   132
wenzelm@389
   133
fun optional parse def = parse || empty >> K def;
wenzelm@389
   134
wenzelm@389
   135
fun repeat parse toks = (parse -- repeat parse >> op :: || empty) toks;
wenzelm@389
   136
fun repeat1 parse = parse -- repeat parse >> op ::;
wenzelm@389
   137
wenzelm@389
   138
fun enum1 sep parse = parse -- repeat (sep $$-- parse) >> op ::;
wenzelm@389
   139
fun enum sep parse = enum1 sep parse || empty;
wenzelm@389
   140
wenzelm@389
   141
val list = enum ",";
wenzelm@389
   142
val list1 = enum1 ",";
wenzelm@389
   143
wenzelm@389
   144
wenzelm@389
   145
wenzelm@389
   146
(** theory parsers **)
wenzelm@389
   147
wenzelm@389
   148
(* misc utilities *)
wenzelm@389
   149
wenzelm@389
   150
fun cat s1 s2 = s1 ^ " " ^ s2;
wenzelm@389
   151
wenzelm@389
   152
val pars = parents "(" ")";
wenzelm@389
   153
val brackets = parents "[" "]";
wenzelm@389
   154
wenzelm@389
   155
val mk_list = brackets o commas;
wenzelm@389
   156
val mk_big_list = brackets o space_implode ",\n ";
wenzelm@389
   157
wenzelm@389
   158
fun mk_pair (x, y) = pars (commas [x, y]);
wenzelm@389
   159
fun mk_triple (x, y, z) = pars (commas [x, y, z]);
wenzelm@389
   160
fun mk_triple1 ((x, y), z) = mk_triple (x, y, z);
wenzelm@389
   161
fun mk_triple2 (x, (y, z)) = mk_triple (x, y, z);
wenzelm@389
   162
wenzelm@389
   163
val split_decls = flat o map (fn (xs, y) => map (rpair y) xs);
wenzelm@389
   164
wenzelm@389
   165
fun strip_quotes str =
wenzelm@389
   166
  implode (tl (take (size str - 1, explode str)));
wenzelm@389
   167
wenzelm@389
   168
wenzelm@389
   169
(* names *)
wenzelm@389
   170
wenzelm@389
   171
val name = ident >> quote || string;
wenzelm@389
   172
val names = list name;
wenzelm@389
   173
val names1 = list1 name;
wenzelm@389
   174
val name_list = names >> mk_list;
wenzelm@389
   175
val name_list1 = names1 >> mk_list;
wenzelm@389
   176
wenzelm@389
   177
wenzelm@389
   178
(* classes *)
wenzelm@389
   179
wenzelm@389
   180
fun mk_subclass (c, cs) = mk_triple ("[]", c, cs);
wenzelm@389
   181
wenzelm@389
   182
val subclass = name -- optional ("<" $$-- !! name_list1) "[]";
wenzelm@389
   183
wenzelm@389
   184
val class_decls = repeat1 (subclass >> mk_subclass) >> mk_big_list;
wenzelm@389
   185
wenzelm@389
   186
wenzelm@389
   187
(* arities *)
wenzelm@389
   188
wenzelm@389
   189
val sort =
wenzelm@389
   190
  name >> brackets ||
wenzelm@389
   191
  "{" $$-- name_list --$$ "}";
wenzelm@389
   192
wenzelm@389
   193
val sort_list1 = list1 sort >> mk_list;
wenzelm@389
   194
wenzelm@389
   195
wenzelm@389
   196
val arity = optional ("(" $$-- !! (sort_list1 --$$")")) "[]" -- sort;
wenzelm@389
   197
wenzelm@389
   198
val arity_decls = repeat1 (names1 --$$ "::" -- !! arity)
wenzelm@389
   199
  >> (mk_big_list o map mk_triple2 o split_decls);
wenzelm@389
   200
wenzelm@389
   201
wenzelm@389
   202
(* mixfix annotations *)
wenzelm@389
   203
wenzelm@389
   204
val infxl = "infixl" $$-- !! nat >> cat "Infixl";
wenzelm@389
   205
val infxr = "infixr" $$-- !! nat >> cat "Infixr";
wenzelm@389
   206
wenzelm@389
   207
val binder = "binder" $$-- !! (string -- nat) >> (cat "Binder" o mk_pair);
wenzelm@389
   208
wenzelm@389
   209
val opt_pris = optional ("[" $$-- !! (list nat --$$ "]")) [] >> mk_list;
wenzelm@389
   210
wenzelm@389
   211
val mixfix = string -- !! (opt_pris -- optional nat "max_pri")
wenzelm@389
   212
  >> (cat "Mixfix" o mk_triple2);
wenzelm@389
   213
wenzelm@389
   214
fun opt_syn fx =
wenzelm@389
   215
  "(" $$-- fx --$$ ")" ||
wenzelm@389
   216
  empty >> K "NoSyn";
wenzelm@389
   217
wenzelm@389
   218
val opt_infix = opt_syn (infxl || infxr);
wenzelm@389
   219
val opt_mixfix = opt_syn (mixfix || infxl || infxr || binder);
wenzelm@389
   220
wenzelm@389
   221
wenzelm@389
   222
(* types *)
wenzelm@389
   223
wenzelm@389
   224
fun mk_old_type_decl ((ts, n), syn) =
wenzelm@389
   225
  map (fn t => (mk_triple (t, n, syn), false)) ts;
wenzelm@389
   226
wenzelm@389
   227
fun mk_type_decl (((xs, t), None), syn) =
wenzelm@389
   228
      [(mk_triple (t, string_of_int (length xs), syn), false)]
wenzelm@389
   229
  | mk_type_decl (((xs, t), Some rhs), syn) =
wenzelm@389
   230
      [(pars (commas [t, mk_list xs, rhs, syn]), true)];
wenzelm@389
   231
wenzelm@389
   232
fun mk_type_decls tys =
wenzelm@389
   233
  "also add_types\n" ^ mk_big_list (keyfilter tys false) ^ "\n\n\
wenzelm@389
   234
  \also add_tyabbrs\n" ^ mk_big_list (keyfilter tys true);
wenzelm@389
   235
wenzelm@389
   236
wenzelm@389
   237
val old_type_decl = names -- nat -- opt_infix >> mk_old_type_decl;
wenzelm@389
   238
wenzelm@389
   239
val type_args =
wenzelm@389
   240
  type_var >> (fn x => [x]) ||
wenzelm@389
   241
  "(" $$-- !! (list1 type_var --$$ ")") ||
wenzelm@389
   242
  empty >> K [];
wenzelm@389
   243
wenzelm@389
   244
val type_decl = type_args -- name -- optional ("=" $$-- !! string >> Some) None
wenzelm@389
   245
  -- opt_infix >> mk_type_decl;
wenzelm@389
   246
wenzelm@389
   247
val type_decls = repeat1 (old_type_decl || type_decl)
wenzelm@389
   248
  >> (rpair "" o mk_type_decls o flat);
wenzelm@389
   249
wenzelm@389
   250
wenzelm@389
   251
(* consts *)
wenzelm@389
   252
wenzelm@389
   253
val const_decls = repeat1 (names1 --$$ "::" -- !! (string -- opt_mixfix))
wenzelm@389
   254
  >> (mk_big_list o map mk_triple2 o split_decls);
wenzelm@389
   255
wenzelm@389
   256
wenzelm@389
   257
(* translations *)
wenzelm@389
   258
wenzelm@389
   259
val trans_pat =
wenzelm@389
   260
  optional ("(" $$-- !! (name --$$ ")")) "\"logic\"" -- string >> mk_pair;
wenzelm@389
   261
wenzelm@389
   262
val trans_arrow =
wenzelm@389
   263
  $$ "=>" >> K " |-> " ||
wenzelm@389
   264
  $$ "<=" >> K " <-| " ||
wenzelm@389
   265
  $$ "==" >> K " <-> ";
wenzelm@389
   266
wenzelm@389
   267
val trans_decls = repeat1 (trans_pat ^^ !! (trans_arrow ^^ trans_pat))
wenzelm@389
   268
  >> mk_big_list;
wenzelm@389
   269
wenzelm@389
   270
wenzelm@389
   271
(* ML translations *)
wenzelm@389
   272
wenzelm@389
   273
val trfun_defs =
wenzelm@389
   274
  " val parse_ast_translation = [];\n\
wenzelm@389
   275
  \ val parse_translation = [];\n\
wenzelm@389
   276
  \ val print_translation = [];\n\
wenzelm@389
   277
  \ val print_ast_translation = [];";
wenzelm@389
   278
wenzelm@389
   279
val trfun_args =
wenzelm@389
   280
  "(parse_ast_translation, parse_translation, \
wenzelm@389
   281
  \print_translation, print_ast_translation)";
wenzelm@389
   282
wenzelm@389
   283
fun mk_mltrans txt =
wenzelm@389
   284
  "let\n"
wenzelm@389
   285
  ^ trfun_defs ^ "\n"
wenzelm@389
   286
  ^ txt ^ "\n\
wenzelm@389
   287
  \in\n\
wenzelm@389
   288
  \ " ^ trfun_args ^ "\n\
wenzelm@389
   289
  \end";
wenzelm@389
   290
wenzelm@389
   291
val mltrans = verbatim >> mk_mltrans;
wenzelm@389
   292
wenzelm@389
   293
wenzelm@389
   294
(* axioms *)
wenzelm@389
   295
wenzelm@389
   296
val mk_axms = mk_big_list o map (mk_pair o apfst quote);
wenzelm@389
   297
wenzelm@389
   298
fun mk_axiom_decls axms = (mk_axms axms, map fst axms);
wenzelm@389
   299
wenzelm@389
   300
val axiom_decls = repeat1 (ident -- !! string) >> mk_axiom_decls;
wenzelm@389
   301
wenzelm@389
   302
wenzelm@389
   303
(* axclass *)
wenzelm@389
   304
wenzelm@389
   305
fun mk_axclass_decl ((c, cs), axms) =
wenzelm@389
   306
  (mk_pair (c, cs) ^ "\n" ^ mk_axms axms,
wenzelm@389
   307
    (strip_quotes c ^ "I") :: map fst axms);
wenzelm@389
   308
wenzelm@389
   309
val axclass_decl = subclass -- repeat (ident -- !! string) >> mk_axclass_decl;
wenzelm@389
   310
wenzelm@389
   311
wenzelm@389
   312
(* instance *)
wenzelm@389
   313
wenzelm@389
   314
fun mk_instance_decl ((((t, ss), c), axths), opt_tac) =
wenzelm@389
   315
  mk_triple (t, ss, c) ^ "\n" ^
wenzelm@389
   316
  mk_list (keyfilter axths false) ^ "\n" ^
wenzelm@389
   317
  mk_list (keyfilter axths true) ^ "\n" ^
wenzelm@389
   318
  opt_tac;
wenzelm@389
   319
wenzelm@389
   320
val axm_or_thm =
wenzelm@389
   321
  string >> rpair false ||
wenzelm@389
   322
  long_id >> rpair true;
wenzelm@389
   323
wenzelm@389
   324
val instance_decl =
wenzelm@389
   325
  name --$$ "::" -- optional ("(" $$-- sort_list1 --$$")") "[]" -- name --
wenzelm@389
   326
  optional ("(" $$-- list1 axm_or_thm --$$ ")") [] --
wenzelm@389
   327
  optional (verbatim >> (pars o cat "Some" o pars)) "None"
wenzelm@389
   328
  >> mk_instance_decl;
wenzelm@389
   329
wenzelm@389
   330
wenzelm@389
   331
wenzelm@389
   332
(** theory syntax **)
wenzelm@389
   333
wenzelm@389
   334
type syntax =
wenzelm@389
   335
  lexicon * (token list -> (string * string) * token list) Symtab.table;
wenzelm@389
   336
wenzelm@389
   337
fun make_syntax keywords sects =
wenzelm@389
   338
  (make_lexicon keywords, Symtab.make sects handle Symtab.DUPS names
wenzelm@389
   339
    => error ("Duplicate sections in thy syntax: " ^ commas_quote names));
wenzelm@389
   340
wenzelm@389
   341
wenzelm@389
   342
(* header *)
wenzelm@389
   343
wenzelm@389
   344
fun mk_header (thy_name, bases) =
wenzelm@389
   345
  (thy_name, "(base_on " ^ mk_list bases ^ " " ^ quote thy_name ^ ")");
wenzelm@389
   346
wenzelm@389
   347
val base =
wenzelm@389
   348
  ident >> (cat "Thy" o quote) ||
wenzelm@389
   349
  string >> cat "File";
wenzelm@389
   350
wenzelm@389
   351
val header = ident --$$ "=" -- enum1 "+" base >> mk_header;
wenzelm@389
   352
wenzelm@389
   353
wenzelm@389
   354
(* extension *)
wenzelm@389
   355
wenzelm@389
   356
fun mk_extension (txts, mltxt) =
wenzelm@389
   357
  let
wenzelm@389
   358
    val cat_sects = space_implode "\n\n" o filter_out (equal "");
wenzelm@389
   359
    val (extxts, postxts) = split_list txts;
wenzelm@389
   360
  in
wenzelm@389
   361
    (cat_sects extxts, cat_sects postxts, mltxt)
wenzelm@389
   362
  end;
wenzelm@389
   363
wenzelm@389
   364
fun sect tab ((Keyword, s, n) :: toks) =
wenzelm@389
   365
      (case Symtab.lookup (tab, s) of
wenzelm@389
   366
        Some parse => !! parse toks
wenzelm@389
   367
      | None => syn_err "section" s n)
wenzelm@389
   368
  | sect _ ((_, s, n) :: _) = syn_err "section" s n
wenzelm@389
   369
  | sect _ [] = eof_err ();
wenzelm@389
   370
wenzelm@389
   371
fun extension sectab = "+" $$-- !! (repeat (sect sectab) --$$ "end") --
wenzelm@389
   372
  optional ("ML" $$-- verbatim) "" >> mk_extension;
wenzelm@389
   373
wenzelm@389
   374
wenzelm@389
   375
(* theory definition *)
wenzelm@389
   376
wenzelm@389
   377
fun mk_structure ((thy_name, old_thys), Some (extxt, postxt, mltxt)) =
wenzelm@389
   378
      "structure " ^ thy_name ^ " =\n\
wenzelm@389
   379
      \struct\n\
wenzelm@389
   380
      \\n\
wenzelm@389
   381
      \local\n"              ^ " open Mixfix;\n"  (* FIXME tmp *)
wenzelm@389
   382
      ^ trfun_defs ^ "\n\
wenzelm@389
   383
      \in\n\
wenzelm@389
   384
      \\n"
wenzelm@389
   385
      ^ mltxt ^ "\n\
wenzelm@389
   386
      \\n\
wenzelm@389
   387
      \val thy = " ^ old_thys ^ "\n\n\
wenzelm@389
   388
      \also add_trfuns\n"
wenzelm@389
   389
      ^ trfun_args ^ "\n\
wenzelm@389
   390
      \\n"
wenzelm@389
   391
      ^ extxt ^ "\n\
wenzelm@389
   392
      \\n\
wenzelm@389
   393
      \also add_thyname " ^ quote thy_name ^ ";\n\
wenzelm@389
   394
      \\n\
wenzelm@389
   395
      \\n"
wenzelm@389
   396
      ^ postxt ^ "\n\
wenzelm@389
   397
      \\n\
wenzelm@389
   398
      \end;\n\
wenzelm@389
   399
      \end;\n"
wenzelm@389
   400
  | mk_structure ((thy_name, old_thys), None) =
wenzelm@389
   401
      "structure " ^ thy_name ^ " =\n\
wenzelm@389
   402
      \struct\n\
wenzelm@389
   403
      \\n\
wenzelm@389
   404
      \val thy = " ^ old_thys ^ ";\n\
wenzelm@389
   405
      \\n\
wenzelm@389
   406
      \end;\n";
wenzelm@389
   407
wenzelm@389
   408
fun theory_defn sectab =
wenzelm@389
   409
  header -- optional (extension sectab >> Some) None -- eof
wenzelm@389
   410
  >> (mk_structure o #1);
wenzelm@389
   411
wenzelm@389
   412
fun parse_thy (lex, sectab) str =
wenzelm@389
   413
  #1 (!! (theory_defn sectab) (tokenize lex str));
wenzelm@389
   414
wenzelm@389
   415
wenzelm@389
   416
(* standard sections *)
wenzelm@389
   417
wenzelm@389
   418
fun mk_val ax = "val " ^ ax ^ " = get_axiom thy " ^ quote ax ^ ";";
wenzelm@389
   419
wenzelm@389
   420
fun mk_axm_sect pretxt (txt, axs) =
wenzelm@389
   421
  (pretxt ^ "\n" ^ txt, cat_lines (map mk_val axs));
wenzelm@389
   422
wenzelm@389
   423
fun axm_section name pretxt parse =
wenzelm@389
   424
  (name, parse >> mk_axm_sect pretxt);
wenzelm@389
   425
wenzelm@389
   426
fun section name pretxt parse =
wenzelm@389
   427
  axm_section name pretxt (parse >> rpair []);
wenzelm@389
   428
wenzelm@389
   429
wenzelm@389
   430
val pure_keywords =
wenzelm@389
   431
 ["classes", "default", "types", "arities", "consts", "syntax",
wenzelm@389
   432
  "translations", "MLtrans", "MLtext", "rules", "defns", "axclass",
wenzelm@389
   433
  "instance", "end", "ML", "mixfix", "infixr", "infixl", "binder", "=", "+",
wenzelm@389
   434
  ",", "<", "{", "}", "(", ")", "[", "]", "::", "==", "=>", "<="];
wenzelm@389
   435
wenzelm@389
   436
val pure_sections =
wenzelm@389
   437
 [section "classes" "also add_classes" class_decls,
wenzelm@389
   438
  section "default" "also add_defsort" sort,
wenzelm@389
   439
  ("types", type_decls),
wenzelm@389
   440
  section "arities" "also add_arities" arity_decls,
wenzelm@389
   441
  section "consts" "also add_consts" const_decls,
wenzelm@389
   442
  section "syntax" "also add_syntax" const_decls,
wenzelm@389
   443
  section "translations" "also add_trrules" trans_decls,
wenzelm@389
   444
  section "MLtrans" "also add_trfuns" mltrans,
wenzelm@389
   445
  ("MLtext", verbatim >> rpair ""),
wenzelm@389
   446
  axm_section "rules" "also add_axioms" axiom_decls,
wenzelm@389
   447
  axm_section "defns" "also add_defns" axiom_decls,
wenzelm@389
   448
  axm_section "axclass" "also add_axclass" axclass_decl,
wenzelm@389
   449
  section "instance" "also add_instance" instance_decl];
wenzelm@389
   450
wenzelm@389
   451
wenzelm@389
   452
(* FIXME -> thy_read.ML *)
wenzelm@389
   453
val pure_syntax = make_syntax pure_keywords pure_sections;
wenzelm@389
   454
wenzelm@389
   455
end;
wenzelm@389
   456