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