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