src/Pure/Thy/thy_header.ML
author wenzelm
Sun, 20 Mar 2011 17:40:45 +0100
changeset 42003 6e45dc518ebb
parent 41944 b97091ae583a
child 44160 8848867501fb
permissions -rw-r--r--
replaced File.check by specific File.check_file, File.check_dir; clarified File.full_path -- include parts of former Thy_Load.get_file; simplified Thy_Load.check_file -- do not read/digest yet; merged Thy_Load.check_thy/deps_thy -- always read text and parse header; clarified Thy_Header.read -- NB: partial Path.explode outside of args parser combinator; Thy_Info.check_deps etc.: File.read exactly once;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
22106
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Thy/thy_header.ML
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
     2
    Author:     Markus Wenzel, TU Muenchen
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
     3
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
     4
Theory headers -- independent of outer syntax.
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
     5
*)
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
     6
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
     7
signature THY_HEADER =
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
     8
sig
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36950
diff changeset
     9
  val args: Token.T list -> (string * string list * (string * bool) list) * Token.T list
42003
6e45dc518ebb replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents: 41944
diff changeset
    10
  val read: Position.T -> string -> string * string list * (Path.T * bool) list
37950
bc285d91041e moved basic thy file name operations from Thy_Load to Thy_Header;
wenzelm
parents: 36959
diff changeset
    11
  val thy_path: string -> Path.T
bc285d91041e moved basic thy file name operations from Thy_Load to Thy_Header;
wenzelm
parents: 36959
diff changeset
    12
  val split_thy_path: Path.T -> Path.T * string
bc285d91041e moved basic thy file name operations from Thy_Load to Thy_Header;
wenzelm
parents: 36959
diff changeset
    13
  val consistent_name: string -> string -> unit
22106
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
    14
end;
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
    15
32466
a393b7e2a2f8 modernized Thy_Header;
wenzelm
parents: 29606
diff changeset
    16
structure Thy_Header: THY_HEADER =
22106
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
    17
struct
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
    18
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
    19
(* keywords *)
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
    20
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
    21
val headerN = "header";
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
    22
val theoryN = "theory";
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
    23
val importsN = "imports";
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
    24
val usesN = "uses";
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
    25
val beginN = "begin";
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
    26
24577
c6acb6d79757 removed redundant OuterLex.make_lexicon;
wenzelm
parents: 23863
diff changeset
    27
val header_lexicon = Scan.make_lexicon
c6acb6d79757 removed redundant OuterLex.make_lexicon;
wenzelm
parents: 23863
diff changeset
    28
  (map Symbol.explode ["%", "(", ")", ";", beginN, headerN, importsN, theoryN, usesN]);
22106
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
    29
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
    30
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
    31
(* header args *)
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
    32
36950
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 34167
diff changeset
    33
val file_name = Parse.group "file name" Parse.name;
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 34167
diff changeset
    34
val theory_name = Parse.group "theory name" Parse.name;
27890
62ac62e30ab1 args: explicit groups for file_name, theory_name;
wenzelm
parents: 27872
diff changeset
    35
36950
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 34167
diff changeset
    36
val file =
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 34167
diff changeset
    37
  Parse.$$$ "(" |-- Parse.!!! (file_name --| Parse.$$$ ")") >> rpair false ||
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 34167
diff changeset
    38
  file_name >> rpair true;
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 34167
diff changeset
    39
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 34167
diff changeset
    40
val uses = Scan.optional (Parse.$$$ usesN |-- Parse.!!! (Scan.repeat1 file)) [];
22106
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
    41
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
    42
val args =
36950
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 34167
diff changeset
    43
  theory_name -- (Parse.$$$ importsN |--
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 34167
diff changeset
    44
    Parse.!!! (Scan.repeat1 theory_name -- uses --| Parse.$$$ beginN))
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 34167
diff changeset
    45
  >> Parse.triple2;
22106
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
    46
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
    47
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
    48
(* read header *)
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
    49
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
    50
val header =
36950
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 34167
diff changeset
    51
  (Parse.$$$ headerN -- Parse.tags) |--
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 34167
diff changeset
    52
    (Parse.!!! (Parse.doc_source -- Scan.repeat Parse.semicolon --
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 34167
diff changeset
    53
      (Parse.$$$ theoryN -- Parse.tags) |-- args)) ||
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 34167
diff changeset
    54
  (Parse.$$$ theoryN -- Parse.tags) |-- Parse.!!! args;
22106
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
    55
37978
548f3f165d05 simplified Thy_Header.read -- include Source.of_string_limited here;
wenzelm
parents: 37950
diff changeset
    56
fun read pos str =
22106
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
    57
  let val res =
37978
548f3f165d05 simplified Thy_Header.read -- include Source.of_string_limited here;
wenzelm
parents: 37950
diff changeset
    58
    str
548f3f165d05 simplified Thy_Header.read -- include Source.of_string_limited here;
wenzelm
parents: 37950
diff changeset
    59
    |> Source.of_string_limited 8000
40523
1050315f6ee2 simplified/robustified treatment of malformed symbols, which are now fully internalized (total Symbol.explode etc.);
wenzelm
parents: 38149
diff changeset
    60
    |> Symbol.source
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36950
diff changeset
    61
    |> Token.source {do_recover = NONE} (fn () => (header_lexicon, Scan.empty_lexicon)) pos
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36950
diff changeset
    62
    |> Token.source_proper
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36950
diff changeset
    63
    |> Source.source Token.stopper (Scan.single (Scan.error (Parse.!!! header))) NONE
22106
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
    64
    |> Source.get_single;
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
    65
  in
42003
6e45dc518ebb replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents: 41944
diff changeset
    66
    (case res of
6e45dc518ebb replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents: 41944
diff changeset
    67
      SOME ((name, imports, uses), _) => (name, imports, map (apfst Path.explode) uses)
22106
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
    68
    | NONE => error ("Unexpected end of input" ^ Position.str_of pos))
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
    69
  end;
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
    70
37950
bc285d91041e moved basic thy file name operations from Thy_Load to Thy_Header;
wenzelm
parents: 36959
diff changeset
    71
38149
3c380380beac somewhat uniform Thy_Header.split_thy_path in ML and Scala;
wenzelm
parents: 37978
diff changeset
    72
(* file name *)
37950
bc285d91041e moved basic thy file name operations from Thy_Load to Thy_Header;
wenzelm
parents: 36959
diff changeset
    73
bc285d91041e moved basic thy file name operations from Thy_Load to Thy_Header;
wenzelm
parents: 36959
diff changeset
    74
val thy_path = Path.ext "thy" o Path.basic;
bc285d91041e moved basic thy file name operations from Thy_Load to Thy_Header;
wenzelm
parents: 36959
diff changeset
    75
bc285d91041e moved basic thy file name operations from Thy_Load to Thy_Header;
wenzelm
parents: 36959
diff changeset
    76
fun split_thy_path path =
bc285d91041e moved basic thy file name operations from Thy_Load to Thy_Header;
wenzelm
parents: 36959
diff changeset
    77
  (case Path.split_ext path of
bc285d91041e moved basic thy file name operations from Thy_Load to Thy_Header;
wenzelm
parents: 36959
diff changeset
    78
    (path', "thy") => (Path.dir path', Path.implode (Path.base path'))
41944
b97091ae583a Path.print is the official way to show file-system paths to users -- note that Path.implode often indicates violation of the abstract datatype;
wenzelm
parents: 40530
diff changeset
    79
  | _ => error ("Bad theory file specification: " ^ Path.print path));
37950
bc285d91041e moved basic thy file name operations from Thy_Load to Thy_Header;
wenzelm
parents: 36959
diff changeset
    80
bc285d91041e moved basic thy file name operations from Thy_Load to Thy_Header;
wenzelm
parents: 36959
diff changeset
    81
fun consistent_name name name' =
bc285d91041e moved basic thy file name operations from Thy_Load to Thy_Header;
wenzelm
parents: 36959
diff changeset
    82
  if name = name' then ()
41944
b97091ae583a Path.print is the official way to show file-system paths to users -- note that Path.implode often indicates violation of the abstract datatype;
wenzelm
parents: 40530
diff changeset
    83
  else error ("Bad file name " ^ Path.print (thy_path name) ^ " for theory " ^ quote name');
37950
bc285d91041e moved basic thy file name operations from Thy_Load to Thy_Header;
wenzelm
parents: 36959
diff changeset
    84
22106
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
    85
end;