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