src/Pure/PIDE/resources.ML
author wenzelm
Sat Dec 16 16:46:01 2017 +0100 (21 months ago ago)
changeset 67215 03d0c958d65a
parent 67214 87038a574d09
child 67217 53867014e299
permissions -rw-r--r--
PIDE markup for session ROOT files;
     1 (*  Title:      Pure/PIDE/resources.ML
     2     Author:     Makarius
     3 
     4 Resources for theories and auxiliary files.
     5 *)
     6 
     7 signature RESOURCES =
     8 sig
     9   val default_qualifier: string
    10   val init_session_base:
    11     {global_theories: (string * string) list,
    12      loaded_theories: string list,
    13      known_theories: (string * string) list} -> unit
    14   val finish_session_base: unit -> unit
    15   val global_theory: string -> string option
    16   val loaded_theory: string -> bool
    17   val known_theory: string -> Path.T option
    18   val master_directory: theory -> Path.T
    19   val imports_of: theory -> (string * Position.T) list
    20   val begin_theory: Path.T -> Thy_Header.header -> theory list -> theory
    21   val thy_path: Path.T -> Path.T
    22   val theory_qualifier: string -> string
    23   val import_name: string -> Path.T -> string ->
    24     {node_name: Path.T, master_dir: Path.T, theory_name: string}
    25   val check_thy: Path.T -> string ->
    26    {master: Path.T * SHA1.digest, text: string, theory_pos: Position.T,
    27     imports: (string * Position.T) list, keywords: Thy_Header.keywords}
    28   val parse_files: string -> (theory -> Token.file list) parser
    29   val provide: Path.T * SHA1.digest -> theory -> theory
    30   val provide_parse_files: string -> (theory -> Token.file list * theory) parser
    31   val loaded_files_current: theory -> bool
    32   val check_path: Proof.context -> Path.T -> string * Position.T -> Path.T
    33   val check_file: Proof.context -> Path.T -> string * Position.T -> Path.T
    34   val check_dir: Proof.context -> Path.T -> string * Position.T -> Path.T
    35 end;
    36 
    37 structure Resources: RESOURCES =
    38 struct
    39 
    40 (* session base *)
    41 
    42 val default_qualifier = "Draft";
    43 
    44 val empty_session_base =
    45   {global_theories = Symtab.empty: string Symtab.table,
    46    loaded_theories = Symtab.empty: unit Symtab.table,
    47    known_theories = Symtab.empty: Path.T Symtab.table};
    48 
    49 val global_session_base =
    50   Synchronized.var "Sessions.base" empty_session_base;
    51 
    52 fun init_session_base {global_theories, loaded_theories, known_theories} =
    53   Synchronized.change global_session_base
    54     (fn _ =>
    55       {global_theories = Symtab.make global_theories,
    56        loaded_theories = Symtab.make_set loaded_theories,
    57        known_theories = Symtab.make (map (apsnd Path.explode) known_theories)});
    58 
    59 fun finish_session_base () =
    60   Synchronized.change global_session_base
    61     (fn {global_theories, loaded_theories, ...} =>
    62       {global_theories = global_theories,
    63        loaded_theories = loaded_theories,
    64        known_theories = #known_theories empty_session_base});
    65 
    66 fun get_session_base f = f (Synchronized.value global_session_base);
    67 
    68 fun global_theory a = Symtab.lookup (get_session_base #global_theories) a;
    69 fun loaded_theory a = Symtab.defined (get_session_base #loaded_theories) a;
    70 fun known_theory a = Symtab.lookup (get_session_base #known_theories) a;
    71 
    72 
    73 (* manage source files *)
    74 
    75 type files =
    76  {master_dir: Path.T,  (*master directory of theory source*)
    77   imports: (string * Position.T) list,  (*source specification of imports*)
    78   provided: (Path.T * SHA1.digest) list};  (*source path, digest*)
    79 
    80 fun make_files (master_dir, imports, provided): files =
    81  {master_dir = master_dir, imports = imports, provided = provided};
    82 
    83 structure Files = Theory_Data
    84 (
    85   type T = files;
    86   val empty = make_files (Path.current, [], []);
    87   fun extend _ = empty;
    88   fun merge _ = empty;
    89 );
    90 
    91 fun map_files f =
    92   Files.map (fn {master_dir, imports, provided} =>
    93     make_files (f (master_dir, imports, provided)));
    94 
    95 
    96 val master_directory = #master_dir o Files.get;
    97 val imports_of = #imports o Files.get;
    98 
    99 fun begin_theory master_dir {name, imports, keywords} parents =
   100   Theory.begin_theory name parents
   101   |> map_files (fn _ => (master_dir, imports, []))
   102   |> Thy_Header.add_keywords keywords;
   103 
   104 
   105 (* theory files *)
   106 
   107 val thy_path = Path.ext "thy";
   108 
   109 fun theory_qualifier theory =
   110   (case global_theory theory of
   111     SOME qualifier => qualifier
   112   | NONE => Long_Name.qualifier theory);
   113 
   114 fun theory_name qualifier theory =
   115   if Long_Name.is_qualified theory orelse is_some (global_theory theory)
   116   then theory
   117   else Long_Name.qualify qualifier theory;
   118 
   119 fun import_name qualifier dir s =
   120   let val theory = theory_name qualifier (Thy_Header.import_name s) in
   121     if loaded_theory theory
   122     then {node_name = Path.basic theory, master_dir = Path.current, theory_name = theory}
   123     else
   124       let
   125         val node_name =
   126           (case known_theory theory of
   127             SOME node_name => node_name
   128           | NONE =>
   129               if Thy_Header.is_base_name s andalso Long_Name.is_qualified s
   130               then Path.explode s
   131               else File.full_path dir (thy_path (Path.expand (Path.explode s))));
   132       in {node_name = node_name, master_dir = Path.dir node_name, theory_name = theory} end
   133   end;
   134 
   135 fun check_file dir file = File.check_file (File.full_path dir file);
   136 
   137 fun check_thy dir thy_name =
   138   let
   139     val thy_base_name = Long_Name.base_name thy_name;
   140     val master_file =
   141       (case known_theory thy_name of
   142         SOME known_path => check_file Path.current known_path
   143       | NONE => check_file dir (thy_path (Path.basic thy_base_name)));
   144     val text = File.read master_file;
   145 
   146     val {name = (name, pos), imports, keywords} =
   147       Thy_Header.read (Path.position master_file) text;
   148     val _ =
   149       thy_base_name <> name andalso
   150         error ("Bad theory name " ^ quote name ^
   151           " for file " ^ Path.print (Path.base master_file) ^ Position.here pos);
   152   in
   153    {master = (master_file, SHA1.digest text), text = text, theory_pos = pos,
   154     imports = imports, keywords = keywords}
   155   end;
   156 
   157 
   158 (* load files *)
   159 
   160 fun parse_files cmd =
   161   Scan.ahead Parse.not_eof -- Parse.path >> (fn (tok, name) => fn thy =>
   162     (case Token.get_files tok of
   163       [] =>
   164         let
   165           val keywords = Thy_Header.get_keywords thy;
   166           val master_dir = master_directory thy;
   167           val pos = Token.pos_of tok;
   168           val src_paths = Keyword.command_files keywords cmd (Path.explode name);
   169         in map (Command.read_file master_dir pos) src_paths end
   170     | files => map Exn.release files));
   171 
   172 fun provide (src_path, id) =
   173   map_files (fn (master_dir, imports, provided) =>
   174     if AList.defined (op =) provided src_path then
   175       error ("Duplicate use of source file: " ^ Path.print src_path)
   176     else (master_dir, imports, (src_path, id) :: provided));
   177 
   178 fun provide_parse_files cmd =
   179   parse_files cmd >> (fn files => fn thy =>
   180     let
   181       val fs = files thy;
   182       val thy' = fold (fn {src_path, digest, ...} => provide (src_path, digest)) fs thy;
   183     in (fs, thy') end);
   184 
   185 fun load_file thy src_path =
   186   let
   187     val full_path = check_file (master_directory thy) src_path;
   188     val text = File.read full_path;
   189     val id = SHA1.digest text;
   190   in ((full_path, id), text) end;
   191 
   192 fun loaded_files_current thy =
   193   #provided (Files.get thy) |>
   194     forall (fn (src_path, id) =>
   195       (case try (load_file thy) src_path of
   196         NONE => false
   197       | SOME ((_, id'), _) => id = id'));
   198 
   199 
   200 (* antiquotations *)
   201 
   202 local
   203 
   204 fun gen_check check_file ctxt dir (name, pos) =
   205   let
   206     fun err msg = error (msg ^ Position.here pos);
   207 
   208     val _ = Context_Position.report ctxt pos Markup.language_path;
   209     val path = Path.append dir (Path.explode name) handle ERROR msg => err msg;
   210     val _ = Path.expand path handle ERROR msg => err msg;
   211     val _ = Context_Position.report ctxt pos (Markup.path (Path.smart_implode path));
   212     val _ = check_file path handle ERROR msg => err msg;
   213   in path end;
   214 
   215 fun document_antiq check ctxt (name, pos) =
   216   let
   217     val dir = master_directory (Proof_Context.theory_of ctxt);
   218     val _ = check ctxt dir (name, pos);
   219   in
   220     space_explode "/" name
   221     |> map Latex.output_ascii
   222     |> space_implode (Latex.output_ascii "/" ^ "\\discretionary{}{}{}")
   223     |> enclose "\\isatt{" "}"
   224   end;
   225 
   226 fun ML_antiq check ctxt (name, pos) =
   227   let val path = check ctxt Path.current (name, pos);
   228   in "Path.explode " ^ ML_Syntax.print_string (Path.implode path) end;
   229 
   230 in
   231 
   232 val check_path = gen_check I;
   233 val check_file = gen_check File.check_file;
   234 val check_dir = gen_check File.check_dir;
   235 
   236 val _ = Theory.setup
   237  (Thy_Output.antiquotation \<^binding>\<open>path\<close> (Scan.lift (Parse.position Parse.path))
   238     (document_antiq check_path o #context) #>
   239   Thy_Output.antiquotation \<^binding>\<open>file\<close> (Scan.lift (Parse.position Parse.path))
   240     (document_antiq check_file o #context) #>
   241   Thy_Output.antiquotation \<^binding>\<open>dir\<close> (Scan.lift (Parse.position Parse.path))
   242     (document_antiq check_dir o #context) #>
   243   ML_Antiquotation.value \<^binding>\<open>path\<close>
   244     (Args.context -- Scan.lift (Parse.position Parse.path) >> uncurry (ML_antiq check_path)) #>
   245   ML_Antiquotation.value \<^binding>\<open>file\<close>
   246     (Args.context -- Scan.lift (Parse.position Parse.path) >> uncurry (ML_antiq check_file)) #>
   247   ML_Antiquotation.value \<^binding>\<open>dir\<close>
   248     (Args.context -- Scan.lift (Parse.position Parse.path) >> uncurry (ML_antiq check_dir)));
   249 
   250 end;
   251 
   252 end;