src/Pure/Isar/thy_header.ML
author ballarin
Fri, 02 Apr 2004 14:08:30 +0200
changeset 14508 859b11514537
parent 12840 c7066d8b684f
child 14981 e73f8140af78
permissions -rw-r--r--
Experimental command for instantiation of locales in proof contexts: instantiate <label>: <loc>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9139
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Isar/thy_header.ML
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
     3
    Author:     Markus Wenzel, TU Muenchen
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
     5
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
     6
Theory headers (old and new-style).
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
     7
*)
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
     8
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
     9
signature THY_HEADER =
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    10
sig
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    11
  val is_old: (string, 'a) Source.source * Position.T -> bool
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    12
  val args: OuterLex.token list ->
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    13
    (string * string list * (string * bool) list) * OuterLex.token list
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    14
  val scan: (string, 'a) Source.source * Position.T -> string * string list * (string * bool) list
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    15
end;
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    16
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    17
structure ThyHeader: THY_HEADER =
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    18
struct
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    19
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    20
structure T = OuterLex;
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    21
structure P = OuterParse;
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    22
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    23
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    24
(* scan header *)
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    25
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    26
fun scan_header get_lex scan (src, pos) =
12840
c7066d8b684f error "Unexpected end of input";
wenzelm
parents: 9139
diff changeset
    27
  let val res =
c7066d8b684f error "Unexpected end of input";
wenzelm
parents: 9139
diff changeset
    28
    src
c7066d8b684f error "Unexpected end of input";
wenzelm
parents: 9139
diff changeset
    29
    |> Symbol.source false
c7066d8b684f error "Unexpected end of input";
wenzelm
parents: 9139
diff changeset
    30
    |> T.source false (fn () => (get_lex (), Scan.empty_lexicon)) pos
c7066d8b684f error "Unexpected end of input";
wenzelm
parents: 9139
diff changeset
    31
    |> T.source_proper
c7066d8b684f error "Unexpected end of input";
wenzelm
parents: 9139
diff changeset
    32
    |> Source.source T.stopper (Scan.single scan) None
c7066d8b684f error "Unexpected end of input";
wenzelm
parents: 9139
diff changeset
    33
    |> Source.get_single;
c7066d8b684f error "Unexpected end of input";
wenzelm
parents: 9139
diff changeset
    34
  in
c7066d8b684f error "Unexpected end of input";
wenzelm
parents: 9139
diff changeset
    35
    (case res of Some (x, _) => x
c7066d8b684f error "Unexpected end of input";
wenzelm
parents: 9139
diff changeset
    36
    | None => error ("Unexpected end of input" ^ Position.str_of pos))
c7066d8b684f error "Unexpected end of input";
wenzelm
parents: 9139
diff changeset
    37
  end;
9139
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    38
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    39
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    40
(* keywords *)
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    41
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    42
val headerN = "header";
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    43
val theoryN = "theory";
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    44
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    45
val theory_keyword = P.$$$ theoryN;
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    46
val header_keyword = P.$$$ headerN;
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    47
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    48
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    49
(* detect new/old headers *)
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    50
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    51
val check_header_lexicon = T.make_lexicon [headerN, theoryN];
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    52
val check_header = Scan.option (header_keyword || theory_keyword);
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    53
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    54
fun is_old src_pos =
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    55
  Library.is_none (scan_header (K check_header_lexicon) check_header src_pos);
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    56
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    57
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    58
(* scan old/new headers *)
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    59
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    60
val header_lexicon =
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    61
  T.make_lexicon ["(", ")", "+", ":", ";", "=", "files", headerN, theoryN];
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    62
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    63
val file_name =
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    64
  (P.$$$ "(" |-- P.!!! (P.name --| P.$$$ ")")) >> rpair false || P.name >> rpair true;
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    65
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    66
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    67
val args =
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    68
  (P.name -- (P.$$$ "=" |-- P.enum1 "+" P.name) --
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    69
    Scan.optional (P.$$$ "files" |-- P.!!! (Scan.repeat1 file_name)) [] --|  P.$$$ ":")
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    70
  >> (fn ((A, Bs), files) => (A, Bs, files));
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    71
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    72
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    73
val new_header =
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    74
  header_keyword |-- (P.!!! (P.text -- Scan.repeat P.semicolon -- theory_keyword |-- args)) ||
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    75
  theory_keyword |-- P.!!! args;
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    76
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    77
val old_header =
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    78
  P.!!! (P.group "theory header"
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    79
    (P.name -- (P.$$$ "=" |-- P.name -- Scan.repeat (P.$$$ "+" |-- P.name))))
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    80
  >> (fn (A, (B, Bs)) => (A, B :: Bs, []: (string * bool) list));
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    81
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    82
fun scan src_pos =
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    83
  (*sadly, old-style headers depend on the current (dynamic!) lexicon*)
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    84
  if is_old src_pos then
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    85
    scan_header ThySyn.get_lexicon (Scan.error old_header) src_pos
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    86
  else scan_header (K header_lexicon) (Scan.error new_header) src_pos;
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    87
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    88
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    89
end;