src/Pure/Isar/thy_header.ML
author wenzelm
Wed Dec 05 03:18:03 2001 +0100 (2001-12-05 ago)
changeset 12385 389d11fb62c8
parent 9139 bf272b4985ec
child 12840 c7066d8b684f
permissions -rw-r--r--
removed unused functionality (weight etc.);
wenzelm@9139
     1
(*  Title:      Pure/Isar/thy_header.ML
wenzelm@9139
     2
    ID:         $Id$
wenzelm@9139
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@9139
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
wenzelm@9139
     5
wenzelm@9139
     6
Theory headers (old and new-style).
wenzelm@9139
     7
*)
wenzelm@9139
     8
wenzelm@9139
     9
signature THY_HEADER =
wenzelm@9139
    10
sig
wenzelm@9139
    11
  val is_old: (string, 'a) Source.source * Position.T -> bool
wenzelm@9139
    12
  val args: OuterLex.token list ->
wenzelm@9139
    13
    (string * string list * (string * bool) list) * OuterLex.token list
wenzelm@9139
    14
  val scan: (string, 'a) Source.source * Position.T -> string * string list * (string * bool) list
wenzelm@9139
    15
end;
wenzelm@9139
    16
wenzelm@9139
    17
structure ThyHeader: THY_HEADER =
wenzelm@9139
    18
struct
wenzelm@9139
    19
wenzelm@9139
    20
structure T = OuterLex;
wenzelm@9139
    21
structure P = OuterParse;
wenzelm@9139
    22
wenzelm@9139
    23
wenzelm@9139
    24
(* scan header *)
wenzelm@9139
    25
wenzelm@9139
    26
fun scan_header get_lex scan (src, pos) =
wenzelm@9139
    27
  src
wenzelm@9139
    28
  |> Symbol.source false
wenzelm@9139
    29
  |> T.source false (fn () => (get_lex (), Scan.empty_lexicon)) pos
wenzelm@9139
    30
  |> T.source_proper
wenzelm@9139
    31
  |> Source.source T.stopper (Scan.single scan) None
wenzelm@9139
    32
  |> (fst o the o Source.get_single);
wenzelm@9139
    33
wenzelm@9139
    34
wenzelm@9139
    35
(* keywords *)
wenzelm@9139
    36
wenzelm@9139
    37
val headerN = "header";
wenzelm@9139
    38
val theoryN = "theory";
wenzelm@9139
    39
wenzelm@9139
    40
val theory_keyword = P.$$$ theoryN;
wenzelm@9139
    41
val header_keyword = P.$$$ headerN;
wenzelm@9139
    42
wenzelm@9139
    43
wenzelm@9139
    44
(* detect new/old headers *)
wenzelm@9139
    45
wenzelm@9139
    46
val check_header_lexicon = T.make_lexicon [headerN, theoryN];
wenzelm@9139
    47
val check_header = Scan.option (header_keyword || theory_keyword);
wenzelm@9139
    48
wenzelm@9139
    49
fun is_old src_pos =
wenzelm@9139
    50
  Library.is_none (scan_header (K check_header_lexicon) check_header src_pos);
wenzelm@9139
    51
wenzelm@9139
    52
wenzelm@9139
    53
(* scan old/new headers *)
wenzelm@9139
    54
wenzelm@9139
    55
val header_lexicon =
wenzelm@9139
    56
  T.make_lexicon ["(", ")", "+", ":", ";", "=", "files", headerN, theoryN];
wenzelm@9139
    57
wenzelm@9139
    58
val file_name =
wenzelm@9139
    59
  (P.$$$ "(" |-- P.!!! (P.name --| P.$$$ ")")) >> rpair false || P.name >> rpair true;
wenzelm@9139
    60
wenzelm@9139
    61
wenzelm@9139
    62
val args =
wenzelm@9139
    63
  (P.name -- (P.$$$ "=" |-- P.enum1 "+" P.name) --
wenzelm@9139
    64
    Scan.optional (P.$$$ "files" |-- P.!!! (Scan.repeat1 file_name)) [] --|  P.$$$ ":")
wenzelm@9139
    65
  >> (fn ((A, Bs), files) => (A, Bs, files));
wenzelm@9139
    66
wenzelm@9139
    67
wenzelm@9139
    68
val new_header =
wenzelm@9139
    69
  header_keyword |-- (P.!!! (P.text -- Scan.repeat P.semicolon -- theory_keyword |-- args)) ||
wenzelm@9139
    70
  theory_keyword |-- P.!!! args;
wenzelm@9139
    71
wenzelm@9139
    72
val old_header =
wenzelm@9139
    73
  P.!!! (P.group "theory header"
wenzelm@9139
    74
    (P.name -- (P.$$$ "=" |-- P.name -- Scan.repeat (P.$$$ "+" |-- P.name))))
wenzelm@9139
    75
  >> (fn (A, (B, Bs)) => (A, B :: Bs, []: (string * bool) list));
wenzelm@9139
    76
wenzelm@9139
    77
fun scan src_pos =
wenzelm@9139
    78
  (*sadly, old-style headers depend on the current (dynamic!) lexicon*)
wenzelm@9139
    79
  if is_old src_pos then
wenzelm@9139
    80
    scan_header ThySyn.get_lexicon (Scan.error old_header) src_pos
wenzelm@9139
    81
  else scan_header (K header_lexicon) (Scan.error new_header) src_pos;
wenzelm@9139
    82
wenzelm@9139
    83
wenzelm@9139
    84
end;