src/Pure/PIDE/resources.ML
author wenzelm
Sat Nov 10 19:01:20 2018 +0100 (11 months ago ago)
changeset 69286 599b6d0d199b
parent 69243 d98cfb369cbd
child 69287 94fa3376ba33
permissions -rw-r--r--
tuned signature;
     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     {sessions: (string * Properties.T) list,
    12      docs: string list,
    13      global_theories: (string * string) list,
    14      loaded_theories: string list,
    15      known_theories: (string * string) list} -> unit
    16   val finish_session_base: unit -> unit
    17   val global_theory: string -> string option
    18   val loaded_theory: string -> bool
    19   val known_theory: string -> Path.T option
    20   val check_session: Proof.context -> string * Position.T -> string
    21   val check_doc: Proof.context -> string * Position.T -> string
    22   val master_directory: theory -> Path.T
    23   val imports_of: theory -> (string * Position.T) list
    24   val begin_theory: Path.T -> Thy_Header.header -> theory list -> theory
    25   val thy_path: Path.T -> Path.T
    26   val theory_qualifier: string -> string
    27   val import_name: string -> Path.T -> string ->
    28     {node_name: Path.T, master_dir: Path.T, theory_name: string}
    29   val check_thy: Path.T -> string ->
    30    {master: Path.T * SHA1.digest, text: string, theory_pos: Position.T,
    31     imports: (string * Position.T) list, keywords: Thy_Header.keywords}
    32   val parse_files: string -> (theory -> Token.file list) parser
    33   val provide: Path.T * SHA1.digest -> theory -> theory
    34   val provide_parse_files: string -> (theory -> Token.file list * theory) parser
    35   val loaded_files_current: theory -> bool
    36   val check_path: Proof.context -> Path.T -> string * Position.T -> Path.T
    37   val check_file: Proof.context -> Path.T -> string * Position.T -> Path.T
    38   val check_dir: Proof.context -> Path.T -> string * Position.T -> Path.T
    39 end;
    40 
    41 structure Resources: RESOURCES =
    42 struct
    43 
    44 (* session base *)
    45 
    46 val default_qualifier = "Draft";
    47 
    48 type entry = {pos: Position.T, serial: serial};
    49 
    50 fun make_entry props : entry =
    51   {pos = Position.of_properties props, serial = serial ()};
    52 
    53 val empty_session_base =
    54   {sessions = []: (string * entry) list,
    55    docs = []: (string * entry) list,
    56    global_theories = Symtab.empty: string Symtab.table,
    57    loaded_theories = Symtab.empty: unit Symtab.table,
    58    known_theories = Symtab.empty: Path.T Symtab.table};
    59 
    60 val global_session_base =
    61   Synchronized.var "Sessions.base" empty_session_base;
    62 
    63 fun init_session_base {sessions, docs, global_theories, loaded_theories, known_theories} =
    64   Synchronized.change global_session_base
    65     (fn _ =>
    66       {sessions = sort_by #1 (map (apsnd make_entry) sessions),
    67        docs = sort_by #1 (map (apsnd make_entry o rpair []) docs),
    68        global_theories = Symtab.make global_theories,
    69        loaded_theories = Symtab.make_set loaded_theories,
    70        known_theories = Symtab.make (map (apsnd Path.explode) known_theories)});
    71 
    72 fun finish_session_base () =
    73   Synchronized.change global_session_base
    74     (fn {global_theories, loaded_theories, ...} =>
    75       {sessions = [],
    76        docs = [],
    77        global_theories = global_theories,
    78        loaded_theories = loaded_theories,
    79        known_theories = #known_theories empty_session_base});
    80 
    81 fun get_session_base f = f (Synchronized.value global_session_base);
    82 
    83 fun global_theory a = Symtab.lookup (get_session_base #global_theories) a;
    84 fun loaded_theory a = Symtab.defined (get_session_base #loaded_theories) a;
    85 fun known_theory a = Symtab.lookup (get_session_base #known_theories) a;
    86 
    87 fun check_name which kind markup ctxt (name, pos) =
    88   let val entries = get_session_base which in
    89     (case AList.lookup (op =) entries name of
    90       SOME entry => (Context_Position.report ctxt pos (markup name entry); name)
    91     | NONE =>
    92         let
    93           val completion =
    94             Completion.make (name, pos) (fn completed =>
    95                 entries
    96                 |> map #1
    97                 |> filter completed
    98                 |> sort_strings
    99                 |> map (fn a => (a, (kind, a))));
   100           val report = Markup.markup_report (Completion.reported_text completion);
   101         in error ("Bad " ^ kind ^ " " ^ quote name ^ Position.here pos ^ report) end)
   102   end;
   103 
   104 fun markup_session name {pos, serial} =
   105   Markup.properties
   106     (Position.entity_properties_of false serial pos) (Markup.entity Markup.sessionN name);
   107 
   108 val check_session = check_name #sessions "session" markup_session;
   109 val check_doc = check_name #docs "documentation" (fn name => fn _ => Markup.doc name);
   110 
   111 
   112 
   113 (* manage source files *)
   114 
   115 type files =
   116  {master_dir: Path.T,  (*master directory of theory source*)
   117   imports: (string * Position.T) list,  (*source specification of imports*)
   118   provided: (Path.T * SHA1.digest) list};  (*source path, digest*)
   119 
   120 fun make_files (master_dir, imports, provided): files =
   121  {master_dir = master_dir, imports = imports, provided = provided};
   122 
   123 structure Files = Theory_Data
   124 (
   125   type T = files;
   126   val empty = make_files (Path.current, [], []);
   127   fun extend _ = empty;
   128   fun merge _ = empty;
   129 );
   130 
   131 fun map_files f =
   132   Files.map (fn {master_dir, imports, provided} =>
   133     make_files (f (master_dir, imports, provided)));
   134 
   135 
   136 val master_directory = #master_dir o Files.get;
   137 val imports_of = #imports o Files.get;
   138 
   139 fun begin_theory master_dir {name, imports, keywords} parents =
   140   Theory.begin_theory name parents
   141   |> map_files (fn _ => (Path.explode (Path.smart_implode master_dir), imports, []))
   142   |> Thy_Header.add_keywords keywords;
   143 
   144 
   145 (* theory files *)
   146 
   147 val thy_path = Path.ext "thy";
   148 
   149 fun theory_qualifier theory =
   150   (case global_theory theory of
   151     SOME qualifier => qualifier
   152   | NONE => Long_Name.qualifier theory);
   153 
   154 fun theory_name qualifier theory =
   155   if Long_Name.is_qualified theory orelse is_some (global_theory theory)
   156   then theory
   157   else Long_Name.qualify qualifier theory;
   158 
   159 fun import_name qualifier dir s =
   160   let val theory = theory_name qualifier (Thy_Header.import_name s) in
   161     if loaded_theory theory
   162     then {node_name = Path.basic theory, master_dir = Path.current, theory_name = theory}
   163     else
   164       let
   165         val node_name =
   166           (case known_theory theory of
   167             SOME node_name => node_name
   168           | NONE =>
   169               if Thy_Header.is_base_name s andalso Long_Name.is_qualified s
   170               then Path.explode s
   171               else File.full_path dir (thy_path (Path.expand (Path.explode s))));
   172       in {node_name = node_name, master_dir = Path.dir node_name, theory_name = theory} end
   173   end;
   174 
   175 fun check_file dir file = File.check_file (File.full_path dir file);
   176 
   177 fun check_thy dir thy_name =
   178   let
   179     val thy_base_name = Long_Name.base_name thy_name;
   180     val master_file =
   181       (case known_theory thy_name of
   182         SOME known_path => check_file Path.current known_path
   183       | NONE => check_file dir (thy_path (Path.basic thy_base_name)));
   184     val text = File.read master_file;
   185 
   186     val {name = (name, pos), imports, keywords} =
   187       Thy_Header.read (Path.position master_file) text;
   188     val _ =
   189       thy_base_name <> name andalso
   190         error ("Bad theory name " ^ quote name ^
   191           " for file " ^ Path.print (Path.base master_file) ^ Position.here pos);
   192   in
   193    {master = (master_file, SHA1.digest text), text = text, theory_pos = pos,
   194     imports = imports, keywords = keywords}
   195   end;
   196 
   197 
   198 (* load files *)
   199 
   200 fun parse_files cmd =
   201   Scan.ahead Parse.not_eof -- Parse.path >> (fn (tok, name) => fn thy =>
   202     (case Token.get_files tok of
   203       [] =>
   204         let
   205           val keywords = Thy_Header.get_keywords thy;
   206           val master_dir = master_directory thy;
   207           val pos = Token.pos_of tok;
   208           val src_paths = Keyword.command_files keywords cmd (Path.explode name);
   209         in map (Command.read_file master_dir pos) src_paths end
   210     | files => map Exn.release files));
   211 
   212 fun provide (src_path, id) =
   213   map_files (fn (master_dir, imports, provided) =>
   214     if AList.defined (op =) provided src_path then
   215       error ("Duplicate use of source file: " ^ Path.print src_path)
   216     else (master_dir, imports, (src_path, id) :: provided));
   217 
   218 fun provide_parse_files cmd =
   219   parse_files cmd >> (fn files => fn thy =>
   220     let
   221       val fs = files thy;
   222       val thy' = fold (fn {src_path, digest, ...} => provide (src_path, digest)) fs thy;
   223     in (fs, thy') end);
   224 
   225 fun load_file thy src_path =
   226   let
   227     val full_path = check_file (master_directory thy) src_path;
   228     val text = File.read full_path;
   229     val id = SHA1.digest text;
   230   in ((full_path, id), text) end;
   231 
   232 fun loaded_files_current thy =
   233   #provided (Files.get thy) |>
   234     forall (fn (src_path, id) =>
   235       (case try (load_file thy) src_path of
   236         NONE => false
   237       | SOME ((_, id'), _) => id = id'));
   238 
   239 
   240 (* formal check *)
   241 
   242 fun formal_check check_file ctxt dir (name, pos) =
   243   let
   244     fun err msg = error (msg ^ Position.here pos);
   245 
   246     val _ = Context_Position.report ctxt pos Markup.language_path;
   247     val path = Path.append dir (Path.explode name) handle ERROR msg => err msg;
   248     val _ = Path.expand path handle ERROR msg => err msg;
   249     val _ = Context_Position.report ctxt pos (Markup.path (Path.smart_implode path));
   250     val _ = check_file path handle ERROR msg => err msg;
   251   in path end;
   252 
   253 val check_path = formal_check I;
   254 val check_file = formal_check File.check_file;
   255 val check_dir = formal_check File.check_dir;
   256 
   257 
   258 (* antiquotations *)
   259 
   260 local
   261 
   262 fun document_antiq check =
   263   Args.context -- Scan.lift (Parse.position Parse.path) >> (fn (ctxt, (name, pos)) =>
   264     let
   265       val dir = master_directory (Proof_Context.theory_of ctxt);
   266       val _: Path.T = check ctxt dir (name, pos);
   267       val latex =
   268         space_explode "/" name
   269         |> map Latex.output_ascii
   270         |> space_implode (Latex.output_ascii "/" ^ "\\discretionary{}{}{}");
   271     in Latex.enclose_block "\\isatt{" "}" [Latex.string latex] end);
   272 
   273 fun ML_antiq check =
   274   Args.context -- Scan.lift (Parse.position Parse.path) >> (fn (ctxt, (name, pos)) =>
   275     check ctxt Path.current (name, pos) |> ML_Syntax.print_path);
   276 
   277 in
   278 
   279 val _ = Theory.setup
   280  (Thy_Output.antiquotation_verbatim \<^binding>\<open>session\<close>
   281     (Scan.lift (Parse.position Parse.embedded)) check_session #>
   282   Thy_Output.antiquotation_verbatim \<^binding>\<open>doc\<close>
   283     (Scan.lift (Parse.position Parse.embedded)) check_doc #>
   284   Thy_Output.antiquotation_raw \<^binding>\<open>path\<close> (document_antiq check_path) (K I) #>
   285   Thy_Output.antiquotation_raw \<^binding>\<open>file\<close> (document_antiq check_file) (K I) #>
   286   Thy_Output.antiquotation_raw \<^binding>\<open>dir\<close> (document_antiq check_dir) (K I) #>
   287   ML_Antiquotation.value \<^binding>\<open>path\<close> (ML_antiq check_path) #>
   288   ML_Antiquotation.value \<^binding>\<open>file\<close> (ML_antiq check_file) #>
   289   ML_Antiquotation.value \<^binding>\<open>dir\<close> (ML_antiq check_dir));
   290 
   291 end;
   292 
   293 end;