src/Pure/Isar/thy_header.ML
author skalberg
Sun, 13 Feb 2005 17:15:14 +0100
changeset 15531 08c8dad8e399
parent 15141 a95c2ff210ba
child 16265 ee2497cde564
permissions -rw-r--r--
Deleted Library.option type.

(*  Title:      Pure/Isar/thy_header.ML
    ID:         $Id$
    Author:     Markus Wenzel, TU Muenchen

Theory headers (old and new-style).
*)

signature THY_HEADER =
sig
  val is_old: (string, 'a) Source.source * Position.T -> bool
  val args: OuterLex.token list ->
    (string * string list * (string * bool) list) * OuterLex.token list
  val scan: (string, 'a) Source.source * Position.T -> string * string list * (string * bool) list
end;

structure ThyHeader: THY_HEADER =
struct

structure T = OuterLex;
structure P = OuterParse;


(* scan header *)

fun scan_header get_lex scan (src, pos) =
  let val res =
    src
    |> Symbol.source false
    |> T.source false (fn () => (get_lex (), Scan.empty_lexicon)) pos
    |> T.source_proper
    |> Source.source T.stopper (Scan.single scan) NONE
    |> Source.get_single;
  in
    (case res of SOME (x, _) => x
    | NONE => error ("Unexpected end of input" ^ Position.str_of pos))
  end;


(* keywords *)

val headerN = "header";
val theoryN = "theory";
val importsN = "imports";

val theory_keyword = P.$$$ theoryN;
val header_keyword = P.$$$ headerN;
val imports_keyword = P.$$$ importsN;


(* detect new/old headers *)

val check_header_lexicon = T.make_lexicon [headerN, theoryN];
val check_header = Scan.option (header_keyword || theory_keyword);

fun is_old src_pos =
  Library.is_none (scan_header (K check_header_lexicon) check_header src_pos);


(* scan old/new headers *)

val header_lexicon = T.make_lexicon
  ["(", ")", "+", ":", ";", "=", "begin", "files", headerN, importsN, theoryN];

val file_name =
  (P.$$$ "(" |-- P.!!! (P.name --| P.$$$ ")")) >> rpair false || P.name >> rpair true;

val args =
  (P.name -- (P.$$$ "=" |-- P.enum1 "+" P.name ||
              imports_keyword |-- Scan.repeat1 P.name) --
    Scan.optional (P.$$$ "files" |-- P.!!! (Scan.repeat1 file_name)) [] --|
  (P.$$$ ":" || P.$$$ "begin"))
  >> (fn ((A, Bs), files) => (A, Bs, files));


val new_header =
  header_keyword |-- (P.!!! (P.text -- Scan.repeat P.semicolon -- theory_keyword |-- args)) ||
  theory_keyword |-- P.!!! args;

val old_header =
  P.!!! (P.group "theory header"
    (P.name -- (P.$$$ "=" |-- P.name -- Scan.repeat (P.$$$ "+" |-- P.name))))
  >> (fn (A, (B, Bs)) => (A, B :: Bs, []: (string * bool) list));

fun scan src_pos =
  (*sadly, old-style headers depend on the current (dynamic!) lexicon*)
  if is_old src_pos then
    scan_header ThySyn.get_lexicon (Scan.error old_header) src_pos
  else scan_header (K header_lexicon) (Scan.error new_header) src_pos;


end;