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