src/Pure/Isar/thy_header.ML
author aspinall
Wed, 18 Aug 2004 16:25:27 +0200
changeset 15145 07360eef9f4f
parent 15141 a95c2ff210ba
child 15531 08c8dad8e399
permissions -rw-r--r--
Version for PGIP 2.X, with greatly improved parsing and XML handling.
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
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
     5
Theory headers (old and new-style).
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
     6
*)
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
     7
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
     8
signature THY_HEADER =
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
     9
sig
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    10
  val is_old: (string, 'a) Source.source * Position.T -> bool
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    11
  val args: OuterLex.token list ->
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    12
    (string * string list * (string * bool) list) * OuterLex.token list
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    13
  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
    14
end;
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    15
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    16
structure ThyHeader: THY_HEADER =
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    17
struct
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    18
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    19
structure T = OuterLex;
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    20
structure P = OuterParse;
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    21
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    22
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    23
(* scan header *)
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    24
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    25
fun scan_header get_lex scan (src, pos) =
12840
c7066d8b684f error "Unexpected end of input";
wenzelm
parents: 9139
diff changeset
    26
  let val res =
c7066d8b684f error "Unexpected end of input";
wenzelm
parents: 9139
diff changeset
    27
    src
c7066d8b684f error "Unexpected end of input";
wenzelm
parents: 9139
diff changeset
    28
    |> Symbol.source false
c7066d8b684f error "Unexpected end of input";
wenzelm
parents: 9139
diff changeset
    29
    |> T.source false (fn () => (get_lex (), Scan.empty_lexicon)) pos
c7066d8b684f error "Unexpected end of input";
wenzelm
parents: 9139
diff changeset
    30
    |> T.source_proper
c7066d8b684f error "Unexpected end of input";
wenzelm
parents: 9139
diff changeset
    31
    |> Source.source T.stopper (Scan.single scan) None
c7066d8b684f error "Unexpected end of input";
wenzelm
parents: 9139
diff changeset
    32
    |> Source.get_single;
c7066d8b684f error "Unexpected end of input";
wenzelm
parents: 9139
diff changeset
    33
  in
c7066d8b684f error "Unexpected end of input";
wenzelm
parents: 9139
diff changeset
    34
    (case res of Some (x, _) => x
c7066d8b684f error "Unexpected end of input";
wenzelm
parents: 9139
diff changeset
    35
    | None => error ("Unexpected end of input" ^ Position.str_of pos))
c7066d8b684f error "Unexpected end of input";
wenzelm
parents: 9139
diff changeset
    36
  end;
9139
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    37
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    38
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    39
(* keywords *)
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    40
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    41
val headerN = "header";
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    42
val theoryN = "theory";
15141
a95c2ff210ba import -> imports
nipkow
parents: 15132
diff changeset
    43
val importsN = "imports";
9139
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;
15141
a95c2ff210ba import -> imports
nipkow
parents: 15132
diff changeset
    47
val imports_keyword = P.$$$ importsN;
9139
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    48
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    49
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    50
(* detect new/old headers *)
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    51
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    52
val check_header_lexicon = T.make_lexicon [headerN, theoryN];
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    53
val check_header = Scan.option (header_keyword || theory_keyword);
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    54
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    55
fun is_old src_pos =
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    56
  Library.is_none (scan_header (K check_header_lexicon) check_header src_pos);
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    57
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    58
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    59
(* scan old/new headers *)
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    60
15132
df2b7976d1e7 new theory header syntax.
nipkow
parents: 14981
diff changeset
    61
val header_lexicon = T.make_lexicon
15141
a95c2ff210ba import -> imports
nipkow
parents: 15132
diff changeset
    62
  ["(", ")", "+", ":", ";", "=", "begin", "files", headerN, importsN, theoryN];
9139
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    63
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    64
val file_name =
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    65
  (P.$$$ "(" |-- P.!!! (P.name --| P.$$$ ")")) >> rpair false || P.name >> rpair true;
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 =
15132
df2b7976d1e7 new theory header syntax.
nipkow
parents: 14981
diff changeset
    68
  (P.name -- (P.$$$ "=" |-- P.enum1 "+" P.name ||
15141
a95c2ff210ba import -> imports
nipkow
parents: 15132
diff changeset
    69
              imports_keyword |-- Scan.repeat1 P.name) --
15132
df2b7976d1e7 new theory header syntax.
nipkow
parents: 14981
diff changeset
    70
    Scan.optional (P.$$$ "files" |-- P.!!! (Scan.repeat1 file_name)) [] --|
df2b7976d1e7 new theory header syntax.
nipkow
parents: 14981
diff changeset
    71
  (P.$$$ ":" || P.$$$ "begin"))
9139
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    72
  >> (fn ((A, Bs), files) => (A, Bs, files));
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    73
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    74
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    75
val new_header =
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    76
  header_keyword |-- (P.!!! (P.text -- Scan.repeat P.semicolon -- theory_keyword |-- args)) ||
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    77
  theory_keyword |-- P.!!! args;
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    78
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    79
val old_header =
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    80
  P.!!! (P.group "theory header"
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    81
    (P.name -- (P.$$$ "=" |-- P.name -- Scan.repeat (P.$$$ "+" |-- P.name))))
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    82
  >> (fn (A, (B, Bs)) => (A, B :: Bs, []: (string * bool) list));
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    83
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    84
fun scan src_pos =
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    85
  (*sadly, old-style headers depend on the current (dynamic!) lexicon*)
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    86
  if is_old src_pos then
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    87
    scan_header ThySyn.get_lexicon (Scan.error old_header) src_pos
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    88
  else scan_header (K header_lexicon) (Scan.error new_header) src_pos;
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    89
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    90
bf272b4985ec Theory headers (old and new-style).
wenzelm
parents:
diff changeset
    91
end;