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