src/Pure/Thy/thy_parse.ML
author wenzelm
Wed Jul 06 14:39:32 1994 +0200 (1994-07-06)
changeset 451 9ebdead316e0
parent 425 c42f384c89de
child 476 836cad329311
permissions -rw-r--r--
exported opt_infix, opt_mixfix parsers;
removed 'subclass' section (now handled by 'instance');
improved make_syntax: section names now added automatically to keywords;
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@451
     6
wenzelm@451
     7
TODO:
wenzelm@451
     8
  remove sigclass (?)
wenzelm@389
     9
*)
wenzelm@389
    10
wenzelm@389
    11
infix 5 -- --$$ $$-- ^^;
wenzelm@389
    12
infix 3 >>;
wenzelm@389
    13
infix 0 ||;
wenzelm@389
    14
wenzelm@389
    15
signature THY_PARSE =
wenzelm@389
    16
sig
wenzelm@389
    17
  type token
wenzelm@389
    18
  val !! : ('a -> 'b * 'c) -> 'a -> 'b * 'c
wenzelm@389
    19
  val >> : ('a -> 'b * 'c) * ('b -> 'd) -> 'a -> 'd * 'c
wenzelm@389
    20
  val || : ('a -> 'b) * ('a -> 'b) -> 'a -> 'b
wenzelm@389
    21
  val -- : ('a -> 'b * 'c) * ('c -> 'd * 'e) -> 'a -> ('b * 'd) * 'e
wenzelm@389
    22
  val ^^ : ('a -> string * 'b) * ('b -> string * 'c) -> 'a -> string * 'c
wenzelm@389
    23
  val $$ : string -> token list -> string * token list
wenzelm@389
    24
  val $$-- : string * (token list -> 'b * 'c) -> token list -> 'b * 'c
wenzelm@389
    25
  val --$$ : ('a -> 'b * token list) * string -> 'a -> 'b * token list
wenzelm@389
    26
  val ident: token list -> string * token list
wenzelm@389
    27
  val long_ident: token list -> string * token list
wenzelm@389
    28
  val long_id: token list -> string * token list
wenzelm@389
    29
  val type_var: token list -> string * token list
wenzelm@389
    30
  val nat: token list -> string * token list
wenzelm@389
    31
  val string: token list -> string * token list
wenzelm@389
    32
  val verbatim: token list -> string * token list
wenzelm@389
    33
  val empty: 'a -> 'b list * 'a
wenzelm@389
    34
  val optional: ('a -> 'b * 'a) -> 'b -> 'a -> 'b * 'a
wenzelm@389
    35
  val repeat: ('a -> 'b * 'a) -> 'a -> 'b list * 'a
wenzelm@389
    36
  val repeat1: ('a -> 'b * 'a) -> 'a -> 'b list * 'a
wenzelm@389
    37
  val enum: string -> (token list -> 'a * token list)
wenzelm@389
    38
    -> token list -> 'a list * token list
wenzelm@389
    39
  val enum1: string -> (token list -> 'a * token list)
wenzelm@389
    40
    -> token list -> 'a list * token list
wenzelm@389
    41
  val list: (token list -> 'a * token list)
wenzelm@389
    42
    -> token list -> 'a list * token list
wenzelm@389
    43
  val list1: (token list -> 'a * token list)
wenzelm@389
    44
    -> token list -> 'a list * token list
wenzelm@389
    45
  val name: token list -> string * token list
wenzelm@389
    46
  val sort: token list -> string * token list
wenzelm@451
    47
  val opt_infix: token list -> string * token list
wenzelm@451
    48
  val opt_mixfix: token list -> string * token list
wenzelm@389
    49
  type syntax
wenzelm@389
    50
  val make_syntax: string list ->
wenzelm@389
    51
    (string * (token list -> (string * string) * token list)) list -> syntax
wenzelm@389
    52
  val parse_thy: syntax -> string -> string
wenzelm@389
    53
  val section: string -> string -> (token list -> string * token list)
wenzelm@389
    54
    -> (string * (token list -> (string * string) * token list))
wenzelm@389
    55
  val axm_section: string -> string
wenzelm@389
    56
    -> (token list -> (string * string list) * token list)
wenzelm@389
    57
    -> (string * (token list -> (string * string) * token list))
wenzelm@389
    58
  val pure_keywords: string list
wenzelm@389
    59
  val pure_sections:
wenzelm@389
    60
    (string * (token list -> (string * string) * token list)) list
wenzelm@389
    61
end;
wenzelm@389
    62
wenzelm@414
    63
functor ThyParseFun(structure Symtab: SYMTAB and ThyScan: THY_SCAN): THY_PARSE =
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@414
   233
  "|> add_types\n" ^ mk_big_list (keyfilter tys false) ^ "\n\n\
wenzelm@414
   234
  \|> add_tyabbrs\n" ^ mk_big_list (keyfilter tys true);
wenzelm@389
   235
wenzelm@389
   236
wenzelm@425
   237
val old_type_decl = names1 -- 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@414
   312
(* sigclass *)
wenzelm@414
   313
wenzelm@425
   314
fun mk_sigclass_decl ((c, cs), consts) =
wenzelm@414
   315
  (mk_pair (c, cs) ^ "\n" ^ consts, [strip_quotes c ^ "I"]);
wenzelm@414
   316
wenzelm@414
   317
val sigclass_decl = subclass -- optional const_decls "[]" >> mk_sigclass_decl;
wenzelm@414
   318
wenzelm@414
   319
wenzelm@451
   320
(* instance *)
wenzelm@389
   321
wenzelm@425
   322
fun mk_witness (axths, opt_tac) =
wenzelm@389
   323
  mk_list (keyfilter axths false) ^ "\n" ^
wenzelm@389
   324
  mk_list (keyfilter axths true) ^ "\n" ^
wenzelm@389
   325
  opt_tac;
wenzelm@389
   326
wenzelm@389
   327
val axm_or_thm =
wenzelm@389
   328
  string >> rpair false ||
wenzelm@389
   329
  long_id >> rpair true;
wenzelm@389
   330
wenzelm@451
   331
wenzelm@425
   332
val opt_witness =
wenzelm@389
   333
  optional ("(" $$-- list1 axm_or_thm --$$ ")") [] --
wenzelm@389
   334
  optional (verbatim >> (pars o cat "Some" o pars)) "None"
wenzelm@425
   335
  >> mk_witness;
wenzelm@425
   336
wenzelm@425
   337
val instance_decl =
wenzelm@451
   338
  (name --$$ "<" -- name >> (pair "|> add_inst_subclass" o mk_pair) ||
wenzelm@451
   339
    name --$$ "::" -- arity >> (pair "|> add_inst_arity" o mk_triple2))
wenzelm@451
   340
  -- opt_witness
wenzelm@451
   341
  >> (fn ((x, y), z) => (cat_lines [x, y, z], ""));
wenzelm@389
   342
wenzelm@389
   343
wenzelm@389
   344
wenzelm@389
   345
(** theory syntax **)
wenzelm@389
   346
wenzelm@389
   347
type syntax =
wenzelm@389
   348
  lexicon * (token list -> (string * string) * token list) Symtab.table;
wenzelm@389
   349
wenzelm@389
   350
fun make_syntax keywords sects =
wenzelm@451
   351
  (make_lexicon (map fst sects @ keywords),
wenzelm@451
   352
    Symtab.make sects handle Symtab.DUPS dups =>
wenzelm@451
   353
      error ("Duplicate sections in theory file syntax: " ^ commas_quote dups));
wenzelm@389
   354
wenzelm@389
   355
wenzelm@389
   356
(* header *)
wenzelm@389
   357
wenzelm@389
   358
fun mk_header (thy_name, bases) =
wenzelm@425
   359
  (thy_name, "base_on " ^ mk_list bases ^ " " ^ quote thy_name);
wenzelm@389
   360
wenzelm@389
   361
val base =
wenzelm@389
   362
  ident >> (cat "Thy" o quote) ||
wenzelm@389
   363
  string >> cat "File";
wenzelm@389
   364
wenzelm@389
   365
val header = ident --$$ "=" -- enum1 "+" base >> mk_header;
wenzelm@389
   366
wenzelm@389
   367
wenzelm@389
   368
(* extension *)
wenzelm@389
   369
wenzelm@389
   370
fun mk_extension (txts, mltxt) =
wenzelm@389
   371
  let
wenzelm@389
   372
    val cat_sects = space_implode "\n\n" o filter_out (equal "");
wenzelm@389
   373
    val (extxts, postxts) = split_list txts;
wenzelm@389
   374
  in
wenzelm@389
   375
    (cat_sects extxts, cat_sects postxts, mltxt)
wenzelm@389
   376
  end;
wenzelm@389
   377
wenzelm@389
   378
fun sect tab ((Keyword, s, n) :: toks) =
wenzelm@389
   379
      (case Symtab.lookup (tab, s) of
wenzelm@389
   380
        Some parse => !! parse toks
wenzelm@389
   381
      | None => syn_err "section" s n)
wenzelm@389
   382
  | sect _ ((_, s, n) :: _) = syn_err "section" s n
wenzelm@389
   383
  | sect _ [] = eof_err ();
wenzelm@389
   384
wenzelm@389
   385
fun extension sectab = "+" $$-- !! (repeat (sect sectab) --$$ "end") --
wenzelm@389
   386
  optional ("ML" $$-- verbatim) "" >> mk_extension;
wenzelm@389
   387
wenzelm@389
   388
wenzelm@389
   389
(* theory definition *)
wenzelm@389
   390
wenzelm@389
   391
fun mk_structure ((thy_name, old_thys), Some (extxt, postxt, mltxt)) =
wenzelm@389
   392
      "structure " ^ thy_name ^ " =\n\
wenzelm@389
   393
      \struct\n\
wenzelm@389
   394
      \\n\
wenzelm@425
   395
      \local\n"
wenzelm@389
   396
      ^ trfun_defs ^ "\n\
wenzelm@389
   397
      \in\n\
wenzelm@389
   398
      \\n"
wenzelm@389
   399
      ^ mltxt ^ "\n\
wenzelm@389
   400
      \\n\
wenzelm@425
   401
      \val thy = (" ^ old_thys ^ " true)\n\n\
wenzelm@414
   402
      \|> add_trfuns\n"
wenzelm@389
   403
      ^ trfun_args ^ "\n\
wenzelm@389
   404
      \\n"
wenzelm@389
   405
      ^ extxt ^ "\n\
wenzelm@389
   406
      \\n\
wenzelm@414
   407
      \|> add_thyname " ^ quote thy_name ^ ";\n\
wenzelm@389
   408
      \\n\
wenzelm@389
   409
      \\n"
wenzelm@389
   410
      ^ postxt ^ "\n\
wenzelm@389
   411
      \\n\
wenzelm@389
   412
      \end;\n\
wenzelm@389
   413
      \end;\n"
wenzelm@389
   414
  | mk_structure ((thy_name, old_thys), None) =
wenzelm@389
   415
      "structure " ^ thy_name ^ " =\n\
wenzelm@389
   416
      \struct\n\
wenzelm@389
   417
      \\n\
wenzelm@425
   418
      \val thy = (" ^ old_thys ^ " false);\n\
wenzelm@389
   419
      \\n\
wenzelm@389
   420
      \end;\n";
wenzelm@389
   421
wenzelm@389
   422
fun theory_defn sectab =
wenzelm@389
   423
  header -- optional (extension sectab >> Some) None -- eof
wenzelm@389
   424
  >> (mk_structure o #1);
wenzelm@389
   425
wenzelm@389
   426
fun parse_thy (lex, sectab) str =
wenzelm@389
   427
  #1 (!! (theory_defn sectab) (tokenize lex str));
wenzelm@389
   428
wenzelm@389
   429
wenzelm@389
   430
(* standard sections *)
wenzelm@389
   431
wenzelm@389
   432
fun mk_val ax = "val " ^ ax ^ " = get_axiom thy " ^ quote ax ^ ";";
wenzelm@389
   433
wenzelm@389
   434
fun mk_axm_sect pretxt (txt, axs) =
wenzelm@389
   435
  (pretxt ^ "\n" ^ txt, cat_lines (map mk_val axs));
wenzelm@389
   436
wenzelm@389
   437
fun axm_section name pretxt parse =
wenzelm@389
   438
  (name, parse >> mk_axm_sect pretxt);
wenzelm@389
   439
wenzelm@389
   440
fun section name pretxt parse =
wenzelm@389
   441
  axm_section name pretxt (parse >> rpair []);
wenzelm@389
   442
wenzelm@389
   443
wenzelm@389
   444
val pure_keywords =
wenzelm@451
   445
 ["end", "ML", "mixfix", "infixr", "infixl", "binder", "=", "+", ",", "<",
wenzelm@451
   446
   "{", "}", "(", ")", "[", "]", "::", "==", "=>", "<="];
wenzelm@389
   447
wenzelm@389
   448
val pure_sections =
wenzelm@414
   449
 [section "classes" "|> add_classes" class_decls,
wenzelm@414
   450
  section "default" "|> add_defsort" sort,
wenzelm@389
   451
  ("types", type_decls),
wenzelm@414
   452
  section "arities" "|> add_arities" arity_decls,
wenzelm@414
   453
  section "consts" "|> add_consts" const_decls,
wenzelm@414
   454
  section "syntax" "|> add_syntax" const_decls,
wenzelm@414
   455
  section "translations" "|> add_trrules" trans_decls,
wenzelm@414
   456
  section "MLtrans" "|> add_trfuns" mltrans,
wenzelm@389
   457
  ("MLtext", verbatim >> rpair ""),
wenzelm@414
   458
  axm_section "rules" "|> add_axioms" axiom_decls,
wenzelm@414
   459
  axm_section "defns" "|> add_defns" axiom_decls,
wenzelm@414
   460
  axm_section "axclass" "|> add_axclass" axclass_decl,
wenzelm@414
   461
  axm_section "sigclass" "|> add_sigclass" sigclass_decl,
wenzelm@451
   462
  ("instance", instance_decl)];
wenzelm@389
   463
wenzelm@389
   464
wenzelm@389
   465
end;
wenzelm@389
   466