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