src/Pure/Thy/thy_parse.ML
author wenzelm
Mon Oct 06 20:00:31 1997 +0200 (1997-10-06)
changeset 3798 60ae17f6f378
parent 3797 05e47c7cc6fd
child 3813 e6142be74e59
permissions -rw-r--r--
fixed 'begin';
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 =
paulson@1512
    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@710
    61
  val cat: string -> string -> string
wenzelm@656
    62
  val parens: string -> string
wenzelm@656
    63
  val brackets: string -> string
wenzelm@656
    64
  val mk_list: string list -> string
wenzelm@656
    65
  val mk_big_list: string list -> string
wenzelm@656
    66
  val mk_pair: string * string -> string
wenzelm@656
    67
  val mk_triple: string * string * string -> string
wenzelm@656
    68
  val strip_quotes: string -> string
paulson@1512
    69
  end;
wenzelm@389
    70
paulson@1512
    71
paulson@1512
    72
structure ThyParse : THY_PARSE=
wenzelm@389
    73
struct
wenzelm@389
    74
wenzelm@389
    75
open ThyScan;
wenzelm@389
    76
wenzelm@389
    77
wenzelm@389
    78
(** parser toolbox **)
wenzelm@389
    79
wenzelm@389
    80
type token = token_kind * string * int;
wenzelm@389
    81
wenzelm@389
    82
wenzelm@389
    83
(* errors *)
wenzelm@389
    84
wenzelm@389
    85
exception SYNTAX_ERROR of string * string * int;
wenzelm@389
    86
wenzelm@389
    87
fun syn_err s1 s2 n = raise SYNTAX_ERROR (s1, s2, n);
wenzelm@389
    88
wenzelm@389
    89
fun eof_err () = error "Unexpected end-of-file";
wenzelm@389
    90
lcp@570
    91
(*Similar to Prolog's cut: reports any syntax error instead of backtracking
lcp@570
    92
  through a superior || *)
wenzelm@389
    93
fun !! parse toks = parse toks
wenzelm@389
    94
  handle SYNTAX_ERROR (s1, s2, n) => error ("Syntax error on line " ^
wenzelm@389
    95
    string_of_int n ^ ": " ^ s1 ^ " expected and " ^ s2 ^ " was found");
wenzelm@389
    96
wenzelm@389
    97
wenzelm@389
    98
(* parser combinators *)
wenzelm@389
    99
wenzelm@389
   100
fun (parse >> f) toks = apfst f (parse toks);
wenzelm@389
   101
wenzelm@389
   102
fun (parse1 || parse2) toks =
wenzelm@389
   103
  parse1 toks handle SYNTAX_ERROR _ => parse2 toks;
wenzelm@389
   104
wenzelm@389
   105
fun (parse1 -- parse2) toks =
wenzelm@389
   106
  let
wenzelm@389
   107
    val (x, toks') = parse1 toks;
wenzelm@389
   108
    val (y, toks'') = parse2 toks';
wenzelm@389
   109
  in
wenzelm@389
   110
    ((x, y), toks'')
wenzelm@389
   111
  end;
wenzelm@389
   112
wenzelm@389
   113
fun (parse1 ^^ parse2) = parse1 -- parse2 >> op ^;
wenzelm@389
   114
wenzelm@389
   115
wenzelm@389
   116
(* generic parsers *)
wenzelm@389
   117
wenzelm@389
   118
fun $$ a ((k, b, n) :: toks) =
wenzelm@389
   119
      if k = Keyword andalso a = b then (a, toks)
wenzelm@389
   120
      else syn_err (quote a) (quote b) n
wenzelm@389
   121
  | $$ _ [] = eof_err ();
wenzelm@389
   122
wenzelm@389
   123
fun (a $$-- parse) = $$ a -- parse >> #2;
wenzelm@389
   124
wenzelm@389
   125
fun (parse --$$ a) = parse -- $$ a >> #1;
wenzelm@389
   126
wenzelm@389
   127
wenzelm@389
   128
fun kind k1 ((k2, s, n) :: toks) =
wenzelm@389
   129
      if k1 = k2 then (s, toks)
wenzelm@389
   130
      else syn_err (name_of_kind k1) (quote s) n
wenzelm@389
   131
  | kind _ [] = eof_err ();
wenzelm@389
   132
wenzelm@389
   133
val ident = kind Ident;
wenzelm@389
   134
val long_ident = kind LongIdent;
wenzelm@389
   135
val long_id = ident || long_ident;
wenzelm@389
   136
val type_var = kind TypeVar >> quote;
wenzelm@389
   137
val nat = kind Nat;
wenzelm@389
   138
val string = kind String;
wenzelm@389
   139
val verbatim = kind Verbatim;
wenzelm@389
   140
val eof = kind EOF;
wenzelm@389
   141
wenzelm@389
   142
fun empty toks = ([], toks);
wenzelm@389
   143
wenzelm@389
   144
fun optional parse def = parse || empty >> K def;
wenzelm@389
   145
wenzelm@389
   146
fun repeat parse toks = (parse -- repeat parse >> op :: || empty) toks;
wenzelm@389
   147
fun repeat1 parse = parse -- repeat parse >> op ::;
wenzelm@389
   148
wenzelm@389
   149
fun enum1 sep parse = parse -- repeat (sep $$-- parse) >> op ::;
wenzelm@389
   150
fun enum sep parse = enum1 sep parse || empty;
wenzelm@389
   151
paulson@2231
   152
fun list  parse = enum  "," parse;
paulson@2231
   153
fun list1 parse = enum1 "," parse;
wenzelm@389
   154
wenzelm@389
   155
wenzelm@389
   156
(** theory parsers **)
wenzelm@389
   157
wenzelm@389
   158
(* misc utilities *)
wenzelm@389
   159
wenzelm@389
   160
fun cat s1 s2 = s1 ^ " " ^ s2;
wenzelm@389
   161
wenzelm@558
   162
val parens = enclose "(" ")";
wenzelm@558
   163
val brackets = enclose "[" "]";
wenzelm@389
   164
wenzelm@389
   165
val mk_list = brackets o commas;
wenzelm@389
   166
val mk_big_list = brackets o space_implode ",\n ";
wenzelm@389
   167
wenzelm@558
   168
fun mk_pair (x, y) = parens (commas [x, y]);
wenzelm@558
   169
fun mk_triple (x, y, z) = parens (commas [x, y, z]);
wenzelm@389
   170
fun mk_triple1 ((x, y), z) = mk_triple (x, y, z);
wenzelm@389
   171
fun mk_triple2 (x, (y, z)) = mk_triple (x, y, z);
wenzelm@389
   172
paulson@2231
   173
fun split_decls l = flat (map (fn (xs, y) => map (rpair y) xs) l);
wenzelm@389
   174
wenzelm@389
   175
fun strip_quotes str =
wenzelm@389
   176
  implode (tl (take (size str - 1, explode str)));
wenzelm@389
   177
wenzelm@389
   178
wenzelm@389
   179
(* names *)
wenzelm@389
   180
wenzelm@389
   181
val name = ident >> quote || string;
wenzelm@389
   182
val names = list name;
wenzelm@389
   183
val names1 = list1 name;
wenzelm@389
   184
val name_list = names >> mk_list;
wenzelm@389
   185
val name_list1 = names1 >> mk_list;
wenzelm@389
   186
wenzelm@389
   187
wenzelm@389
   188
(* classes *)
wenzelm@389
   189
wenzelm@389
   190
val subclass = name -- optional ("<" $$-- !! name_list1) "[]";
wenzelm@389
   191
wenzelm@558
   192
val class_decls = repeat1 (subclass >> mk_pair) >> mk_big_list;
wenzelm@389
   193
wenzelm@389
   194
wenzelm@389
   195
(* arities *)
wenzelm@389
   196
wenzelm@389
   197
val sort =
wenzelm@389
   198
  name >> brackets ||
wenzelm@389
   199
  "{" $$-- name_list --$$ "}";
wenzelm@389
   200
wenzelm@389
   201
val sort_list1 = list1 sort >> mk_list;
wenzelm@389
   202
wenzelm@389
   203
wenzelm@389
   204
val arity = optional ("(" $$-- !! (sort_list1 --$$")")) "[]" -- sort;
wenzelm@389
   205
wenzelm@389
   206
val arity_decls = repeat1 (names1 --$$ "::" -- !! arity)
wenzelm@389
   207
  >> (mk_big_list o map mk_triple2 o split_decls);
wenzelm@389
   208
wenzelm@389
   209
wenzelm@389
   210
(* mixfix annotations *)
wenzelm@389
   211
wenzelm@2203
   212
val infxl =
wenzelm@2203
   213
  "infixl" $$-- !! (nat >> cat "Infixl" || string -- nat >> (cat "InfixlName" o mk_pair));
wenzelm@2203
   214
val infxr =
wenzelm@2203
   215
  "infixr" $$-- !! (nat >> cat "Infixr" || string -- nat >> (cat "InfixrName" o mk_pair));
wenzelm@389
   216
wenzelm@889
   217
val binder = "binder" $$--
wenzelm@2203
   218
  !! (string -- (("[" $$-- nat --$$ "]") -- nat || nat >> (fn n => (n, n))))
wenzelm@2203
   219
    >> (cat "Binder" o mk_triple2);
wenzelm@389
   220
wenzelm@389
   221
val opt_pris = optional ("[" $$-- !! (list nat --$$ "]")) [] >> mk_list;
wenzelm@389
   222
wenzelm@389
   223
val mixfix = string -- !! (opt_pris -- optional nat "max_pri")
wenzelm@389
   224
  >> (cat "Mixfix" o mk_triple2);
wenzelm@389
   225
wenzelm@558
   226
fun opt_syn fx = optional ("(" $$-- fx --$$ ")") "NoSyn";
wenzelm@389
   227
wenzelm@389
   228
val opt_infix = opt_syn (infxl || infxr);
wenzelm@389
   229
val opt_mixfix = opt_syn (mixfix || infxl || infxr || binder);
wenzelm@389
   230
wenzelm@389
   231
wenzelm@389
   232
(* types *)
wenzelm@389
   233
clasohm@1705
   234
(*Parse an identifier, but only if it is not followed by "::", "=" or ",";
clasohm@1377
   235
  the exclusion of a postfix comma can be controlled to allow expressions
clasohm@1377
   236
  like "(id, id)" but disallow ones like "'a => id id,id :: ..."*)
clasohm@1377
   237
fun ident_no_colon _ [] = eof_err()
clasohm@1377
   238
  | ident_no_colon allow_comma ((Ident, s, n) :: (rest as (Keyword, s2, n2) ::
clasohm@1377
   239
                                toks)) =
clasohm@1705
   240
      if s2 = "::" orelse s2 = "=" orelse (not allow_comma andalso s2 = ",")
clasohm@1705
   241
      then syn_err (name_of_kind Ident) (quote s2) n2
clasohm@1377
   242
      else (s, rest)
clasohm@1377
   243
  | ident_no_colon _ ((Ident, s, n) :: toks) = (s, toks)
clasohm@1377
   244
  | ident_no_colon _ ((k, s, n) :: _) =
clasohm@1377
   245
      syn_err (name_of_kind Ident) (quote s) n;
clasohm@1321
   246
wenzelm@3110
   247
(*type used in types, consts and syntax sections*)
clasohm@1377
   248
fun const_type allow_comma toks =
clasohm@1377
   249
  let val simple_type =
clasohm@1377
   250
        (ident ||
clasohm@1377
   251
         kind TypeVar -- optional ("::" $$-- ident >> cat "::") "" >>
clasohm@1377
   252
           (fn (tv, cl) => cat tv cl)) --
clasohm@1377
   253
           repeat (ident_no_colon allow_comma) >>
clasohm@1377
   254
           (fn (args, ts) => cat args (space_implode " " ts)) ||
clasohm@1377
   255
         ("(" $$-- (list1 (const_type true)) --$$ ")" >> (parens o commas)) --
clasohm@1377
   256
           repeat1 (ident_no_colon allow_comma) >>
clasohm@1377
   257
           (fn (args, ts) => cat args (space_implode " " ts));
clasohm@1377
   258
clasohm@1377
   259
      val appl_param =
clasohm@1377
   260
        simple_type || "(" $$-- const_type true --$$ ")" >> parens || 
clasohm@1377
   261
        "[" $$-- (list1 (const_type true)) --$$ "]" --$$ "=>" --
clasohm@1377
   262
          const_type allow_comma >>
clasohm@1377
   263
          (fn (src, dest) => mk_list src ^ " => " ^ dest);
clasohm@1377
   264
  in ("[" $$-- (list1 (const_type true)) --$$ "]" --$$ "=>" --
clasohm@1377
   265
        const_type allow_comma >>
clasohm@1377
   266
        (fn (src, dest) => mk_list src ^ " => " ^ dest) ||
clasohm@1377
   267
      repeat1 (appl_param --$$ "=>") -- const_type allow_comma >>
clasohm@1377
   268
        (fn (src, dest) => space_implode " => " (src@[dest])) ||
clasohm@1377
   269
      simple_type ||
clasohm@1377
   270
      "(" $$-- const_type true --$$ ")" >> parens) toks
clasohm@1321
   271
  end;
clasohm@1321
   272
clasohm@1383
   273
fun mk_old_type_decl ((ts, n), syn) =
clasohm@1383
   274
  map (fn t => (mk_triple (t, n, syn), false)) ts;
clasohm@1383
   275
clasohm@1383
   276
fun mk_type_decl (((xs, t), None), syn) =
clasohm@1383
   277
      [(mk_triple (t, string_of_int (length xs), syn), false)]
clasohm@1383
   278
  | mk_type_decl (((xs, t), Some rhs), syn) =
clasohm@1383
   279
      [(parens (commas [t, mk_list xs, rhs, syn]), true)];
clasohm@1383
   280
clasohm@1383
   281
fun mk_type_decls tys =
wenzelm@3764
   282
  "|> Theory.add_types\n" ^ mk_big_list (keyfilter tys false) ^ "\n\n\
wenzelm@3764
   283
  \|> Theory.add_tyabbrs\n" ^ mk_big_list (keyfilter tys true);
clasohm@1383
   284
clasohm@1383
   285
clasohm@1383
   286
val old_type_decl = names1 -- nat -- opt_infix >> mk_old_type_decl;
clasohm@1383
   287
clasohm@1383
   288
val type_args =
clasohm@1383
   289
  type_var >> (fn x => [x]) ||
clasohm@1383
   290
  "(" $$-- !! (list1 type_var --$$ ")") ||
clasohm@1383
   291
  empty >> K [];
clasohm@1383
   292
clasohm@1383
   293
val type_decl = type_args -- name --
clasohm@1383
   294
  optional ("=" $$-- (string || (const_type false >> quote)) >> Some) None
clasohm@1383
   295
  -- opt_infix >> mk_type_decl;
clasohm@1383
   296
wenzelm@2360
   297
val type_decls =
wenzelm@2360
   298
  repeat1 (old_type_decl || type_decl) >> (mk_type_decls o flat);
clasohm@1383
   299
clasohm@1383
   300
clasohm@1383
   301
(* consts *)
clasohm@1383
   302
wenzelm@2360
   303
val const_decls =
wenzelm@2360
   304
  repeat1
wenzelm@2360
   305
    (names1 --$$ "::" -- !! ((string || const_type false >> quote) -- opt_mixfix))
wenzelm@2360
   306
  >> (mk_big_list o map mk_triple2 o split_decls);
wenzelm@389
   307
wenzelm@2360
   308
val opt_mode =
wenzelm@2360
   309
  optional
wenzelm@2360
   310
    ("(" $$-- !! (name -- optional ($$ "output" >> K "false") "true" --$$ ")"))
wenzelm@2360
   311
    ("\"\"", "true")
wenzelm@2360
   312
  >> mk_pair;
wenzelm@2360
   313
wenzelm@2360
   314
val syntax_decls = opt_mode -- const_decls >> (fn (mode, txt) => mode ^ "\n" ^ txt);
wenzelm@2203
   315
wenzelm@389
   316
wenzelm@389
   317
(* translations *)
wenzelm@389
   318
wenzelm@389
   319
val trans_pat =
wenzelm@389
   320
  optional ("(" $$-- !! (name --$$ ")")) "\"logic\"" -- string >> mk_pair;
wenzelm@389
   321
wenzelm@389
   322
val trans_arrow =
wenzelm@3528
   323
  $$ "=>" >> K "Syntax.ParseRule " ||
wenzelm@3528
   324
  $$ "<=" >> K "Syntax.PrintRule " ||
wenzelm@3528
   325
  $$ "==" >> K "Syntax.ParsePrintRule ";
wenzelm@389
   326
paulson@1810
   327
val trans_line =
wenzelm@2203
   328
  trans_pat -- !! (trans_arrow -- trans_pat)
wenzelm@2203
   329
    >> (fn (left, (arr, right)) => arr ^ mk_pair (left, right));
paulson@1810
   330
paulson@1810
   331
val trans_decls = repeat1 trans_line >> mk_big_list;
wenzelm@389
   332
wenzelm@389
   333
wenzelm@389
   334
(* ML translations *)
wenzelm@389
   335
wenzelm@389
   336
val trfun_defs =
wenzelm@389
   337
  " val parse_ast_translation = [];\n\
wenzelm@389
   338
  \ val parse_translation = [];\n\
wenzelm@389
   339
  \ val print_translation = [];\n\
wenzelm@2385
   340
  \ val typed_print_translation = [];\n\
wenzelm@2694
   341
  \ val print_ast_translation = [];\n\
wenzelm@2694
   342
  \ val token_translation = [];";
wenzelm@389
   343
wenzelm@389
   344
val trfun_args =
wenzelm@389
   345
  "(parse_ast_translation, parse_translation, \
wenzelm@389
   346
  \print_translation, print_ast_translation)";
wenzelm@389
   347
wenzelm@389
   348
wenzelm@389
   349
(* axioms *)
wenzelm@389
   350
wenzelm@389
   351
val mk_axms = mk_big_list o map (mk_pair o apfst quote);
wenzelm@389
   352
wenzelm@389
   353
fun mk_axiom_decls axms = (mk_axms axms, map fst axms);
wenzelm@389
   354
wenzelm@389
   355
val axiom_decls = repeat1 (ident -- !! string) >> mk_axiom_decls;
wenzelm@389
   356
wenzelm@389
   357
clasohm@1555
   358
(* combined consts and axioms *)
clasohm@1555
   359
clasohm@1555
   360
fun mk_constaxiom_decls x =
clasohm@1555
   361
  let
clasohm@1555
   362
    val (axms_defs, axms_names) =
clasohm@1555
   363
      mk_axiom_decls (map (fn ((id, _), def) => (id ^ "_def", def)) x);
clasohm@1555
   364
  in ((mk_big_list o map mk_triple2 o map (apfst quote o fst)) x ^
wenzelm@3764
   365
       "\n\n|> Theory.add_defs\n" ^ axms_defs, axms_names)
clasohm@1555
   366
  end;
clasohm@1555
   367
clasohm@1555
   368
val constaxiom_decls =
clasohm@1555
   369
  repeat1 (ident --$$ "::" -- !!
clasohm@1555
   370
           ((string || const_type false >> quote) -- opt_mixfix) -- !!
clasohm@1555
   371
           string) >> mk_constaxiom_decls;
clasohm@1555
   372
clasohm@1555
   373
wenzelm@389
   374
(* axclass *)
wenzelm@389
   375
wenzelm@389
   376
fun mk_axclass_decl ((c, cs), axms) =
wenzelm@389
   377
  (mk_pair (c, cs) ^ "\n" ^ mk_axms axms,
wenzelm@389
   378
    (strip_quotes c ^ "I") :: map fst axms);
wenzelm@389
   379
wenzelm@389
   380
val axclass_decl = subclass -- repeat (ident -- !! string) >> mk_axclass_decl;
wenzelm@389
   381
wenzelm@389
   382
wenzelm@451
   383
(* instance *)
wenzelm@389
   384
wenzelm@425
   385
fun mk_witness (axths, opt_tac) =
wenzelm@389
   386
  mk_list (keyfilter axths false) ^ "\n" ^
wenzelm@389
   387
  mk_list (keyfilter axths true) ^ "\n" ^
wenzelm@389
   388
  opt_tac;
wenzelm@389
   389
wenzelm@389
   390
val axm_or_thm =
wenzelm@389
   391
  string >> rpair false ||
wenzelm@389
   392
  long_id >> rpair true;
wenzelm@389
   393
wenzelm@451
   394
wenzelm@425
   395
val opt_witness =
wenzelm@389
   396
  optional ("(" $$-- list1 axm_or_thm --$$ ")") [] --
wenzelm@558
   397
  optional (verbatim >> (parens o cat "Some" o parens)) "None"
wenzelm@425
   398
  >> mk_witness;
wenzelm@425
   399
wenzelm@425
   400
val instance_decl =
wenzelm@636
   401
  (name --$$ "<" -- name >> (pair "|> AxClass.add_inst_subclass" o mk_pair) ||
wenzelm@636
   402
    name --$$ "::" -- arity >> (pair "|> AxClass.add_inst_arity" o mk_triple2))
wenzelm@451
   403
  -- opt_witness
wenzelm@777
   404
  >> (fn ((x, y), z) => (cat_lines [x, y, z]));
wenzelm@389
   405
wenzelm@389
   406
wenzelm@389
   407
wenzelm@389
   408
(** theory syntax **)
wenzelm@389
   409
wenzelm@389
   410
type syntax =
wenzelm@389
   411
  lexicon * (token list -> (string * string) * token list) Symtab.table;
wenzelm@389
   412
wenzelm@389
   413
fun make_syntax keywords sects =
wenzelm@451
   414
  (make_lexicon (map fst sects @ keywords),
wenzelm@451
   415
    Symtab.make sects handle Symtab.DUPS dups =>
wenzelm@451
   416
      error ("Duplicate sections in theory file syntax: " ^ commas_quote dups));
wenzelm@389
   417
wenzelm@389
   418
wenzelm@389
   419
(* header *)
wenzelm@389
   420
wenzelm@389
   421
fun mk_header (thy_name, bases) =
clasohm@586
   422
  (thy_name, "mk_base " ^ mk_list bases ^ " " ^ quote thy_name);
wenzelm@389
   423
wenzelm@389
   424
val base =
wenzelm@389
   425
  ident >> (cat "Thy" o quote) ||
wenzelm@389
   426
  string >> cat "File";
wenzelm@389
   427
wenzelm@389
   428
val header = ident --$$ "=" -- enum1 "+" base >> mk_header;
wenzelm@389
   429
wenzelm@389
   430
wenzelm@389
   431
(* extension *)
wenzelm@389
   432
wenzelm@3798
   433
fun mk_extension ((has_begin, txts), mltxt) =
wenzelm@389
   434
  let
wenzelm@389
   435
    val cat_sects = space_implode "\n\n" o filter_out (equal "");
wenzelm@389
   436
    val (extxts, postxts) = split_list txts;
wenzelm@389
   437
  in
wenzelm@3798
   438
    (has_begin, cat_sects extxts, cat_sects postxts, mltxt)
wenzelm@389
   439
  end;
wenzelm@389
   440
wenzelm@389
   441
fun sect tab ((Keyword, s, n) :: toks) =
wenzelm@389
   442
      (case Symtab.lookup (tab, s) of
wenzelm@389
   443
        Some parse => !! parse toks
wenzelm@389
   444
      | None => syn_err "section" s n)
wenzelm@389
   445
  | sect _ ((_, s, n) :: _) = syn_err "section" s n
wenzelm@389
   446
  | sect _ [] = eof_err ();
wenzelm@389
   447
wenzelm@3798
   448
fun extension sectab = "+" $$-- !!
wenzelm@3798
   449
  (optional ($$ "begin" >> K true) false -- (repeat (sect sectab) --$$ "end") --
wenzelm@3798
   450
    optional ("ML" $$-- verbatim) "") >> mk_extension;
wenzelm@389
   451
wenzelm@389
   452
wenzelm@389
   453
(* theory definition *)
wenzelm@389
   454
wenzelm@3798
   455
fun mk_structure tname ((thy_name, old_thys), opt_ext) =
wenzelm@558
   456
  if thy_name <> tname then
wenzelm@558
   457
    error ("Filename \"" ^ tname ^ ".thy\" and theory name "
wenzelm@558
   458
      ^ quote thy_name ^ " are different")
wenzelm@558
   459
  else
wenzelm@3798
   460
    (case opt_ext of
wenzelm@3798
   461
      Some (has_begin, extxt, postxt, mltxt) =>
clasohm@587
   462
        "val thy = " ^ old_thys ^ " true;\n\n\
clasohm@586
   463
        \structure " ^ thy_name ^ " =\n\
clasohm@476
   464
        \struct\n\
clasohm@476
   465
        \\n\
clasohm@476
   466
        \local\n"
clasohm@476
   467
        ^ trfun_defs ^ "\n\
clasohm@476
   468
        \in\n\
clasohm@476
   469
        \\n"
clasohm@476
   470
        ^ mltxt ^ "\n\
clasohm@476
   471
        \\n\
wenzelm@777
   472
        \val thy = thy\n\
wenzelm@3797
   473
        \\n" ^
wenzelm@3798
   474
        (if has_begin then "|> Theory.add_path " ^ quote tname ^ "\n" else "") ^
wenzelm@3797
   475
        "\n\
wenzelm@3764
   476
        \|> Theory.add_trfuns\n"
clasohm@476
   477
        ^ trfun_args ^ "\n\
wenzelm@3764
   478
        \|> Theory.add_trfunsT typed_print_translation \n\
wenzelm@3764
   479
        \|> Theory.add_tokentrfuns token_translation \n\
clasohm@476
   480
        \\n"
clasohm@476
   481
        ^ extxt ^ "\n\
clasohm@476
   482
        \\n\
wenzelm@3780
   483
        \|> Theory.add_path \"/\"\n\
wenzelm@3764
   484
        \|> Theory.add_name " ^ quote thy_name ^ ";\n\
clasohm@476
   485
        \\n\
wenzelm@1235
   486
        \val _ = store_theory (thy, " ^ quote thy_name ^ ");\n\
wenzelm@777
   487
        \\n\
clasohm@476
   488
        \\n"
clasohm@476
   489
        ^ postxt ^ "\n\
clasohm@476
   490
        \\n\
clasohm@476
   491
        \end;\n\
wenzelm@2253
   492
        \end;\n\
wenzelm@2253
   493
        \\n\
wenzelm@2253
   494
        \open " ^ thy_name ^ ";\n\
wenzelm@2253
   495
        \\n"
wenzelm@558
   496
    | None =>
wenzelm@777
   497
        "val thy = " ^ old_thys ^ " false;\n\
wenzelm@777
   498
        \\n\
clasohm@586
   499
        \structure " ^ thy_name ^ " =\n\
clasohm@476
   500
        \struct\n\
clasohm@476
   501
        \\n\
clasohm@587
   502
        \val thy = thy\n\
clasohm@476
   503
        \\n\
wenzelm@1235
   504
        \val _ = store_theory (thy, " ^ quote thy_name ^ ");\n\
wenzelm@777
   505
        \\n\
wenzelm@2253
   506
        \end;\n\
wenzelm@2253
   507
        \\n\
wenzelm@2253
   508
        \open " ^ thy_name ^ ";\n\
wenzelm@2253
   509
        \\n");
wenzelm@2253
   510
wenzelm@389
   511
clasohm@476
   512
fun theory_defn sectab tname =
wenzelm@3798
   513
  header -- optional (extension sectab >> Some) None -- eof
clasohm@476
   514
  >> (mk_structure tname o #1);
wenzelm@389
   515
clasohm@476
   516
fun parse_thy (lex, sectab) tname str =
clasohm@476
   517
  #1 (!! (theory_defn sectab tname) (tokenize lex str));
wenzelm@389
   518
wenzelm@389
   519
wenzelm@389
   520
(* standard sections *)
wenzelm@389
   521
wenzelm@389
   522
fun mk_val ax = "val " ^ ax ^ " = get_axiom thy " ^ quote ax ^ ";";
wenzelm@777
   523
val mk_vals = cat_lines o map mk_val;
wenzelm@389
   524
wenzelm@777
   525
fun mk_axm_sect "" (txt, axs) = (txt, mk_vals axs)
wenzelm@777
   526
  | mk_axm_sect pretxt (txt, axs) = (pretxt ^ "\n" ^ txt, mk_vals axs);
wenzelm@389
   527
wenzelm@389
   528
fun axm_section name pretxt parse =
wenzelm@389
   529
  (name, parse >> mk_axm_sect pretxt);
wenzelm@389
   530
wenzelm@389
   531
fun section name pretxt parse =
wenzelm@389
   532
  axm_section name pretxt (parse >> rpair []);
wenzelm@389
   533
wenzelm@389
   534
wenzelm@389
   535
val pure_keywords =
wenzelm@3797
   536
 ["end", "ML", "mixfix", "infixr", "infixl", "begin", "binder", "output", "=",
wenzelm@2360
   537
  "+", ",", "<", "{", "}", "(", ")", "[", "]", "::", "==", "=>", "<="];
wenzelm@389
   538
wenzelm@389
   539
val pure_sections =
wenzelm@3770
   540
 [section "oracle" "|> Theory.set_oracle" (name >> strip_quotes),
wenzelm@3764
   541
  section "classes" "|> Theory.add_classes" class_decls,
wenzelm@3764
   542
  section "default" "|> Theory.add_defsort" sort,
wenzelm@777
   543
  section "types" "" type_decls,
wenzelm@3764
   544
  section "arities" "|> Theory.add_arities" arity_decls,
wenzelm@3764
   545
  section "consts" "|> Theory.add_consts" const_decls,
wenzelm@3764
   546
  section "syntax" "|> Theory.add_modesyntax" syntax_decls,
wenzelm@3764
   547
  section "translations" "|> Theory.add_trrules" trans_decls,
wenzelm@3764
   548
  axm_section "rules" "|> Theory.add_axioms" axiom_decls,
wenzelm@3764
   549
  axm_section "defs" "|> Theory.add_defs" axiom_decls,
wenzelm@3764
   550
  axm_section "constdefs" "|> Theory.add_consts" constaxiom_decls,
wenzelm@636
   551
  axm_section "axclass" "|> AxClass.add_axclass" axclass_decl,
wenzelm@3780
   552
  section "instance" "" instance_decl,
wenzelm@3780
   553
  section "path" "|> Theory.add_path" name];
wenzelm@389
   554
wenzelm@389
   555
end;