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