src/Pure/PIDE/resources.ML
author wenzelm
Mon Mar 16 16:26:02 2015 +0100 (2015-03-16 ago)
changeset 59718 5d0c539537c9
parent 59689 7968c57ea240
child 61379 c57820ceead3
permissions -rw-r--r--
tuned message -- include completion;
     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 master_directory: theory -> Path.T
    10   val imports_of: theory -> (string * Position.T) list
    11   val thy_path: Path.T -> Path.T
    12   val check_thy: Path.T -> string ->
    13    {master: Path.T * SHA1.digest, text: string, theory_pos: Position.T,
    14     imports: (string * Position.T) list, keywords: Thy_Header.keywords}
    15   val parse_files: string -> (theory -> Token.file list) parser
    16   val provide: Path.T * SHA1.digest -> theory -> theory
    17   val provide_parse_files: string -> (theory -> Token.file list * theory) parser
    18   val loaded_files_current: theory -> bool
    19   val begin_theory: Path.T -> Thy_Header.header -> theory list -> theory
    20   val load_thy: bool -> (Toplevel.transition -> Time.time option) -> int -> Path.T ->
    21     Thy_Header.header -> Position.T -> string -> theory list -> theory * (unit -> unit) * int
    22 end;
    23 
    24 structure Resources: RESOURCES =
    25 struct
    26 
    27 (* manage source files *)
    28 
    29 type files =
    30  {master_dir: Path.T,  (*master directory of theory source*)
    31   imports: (string * Position.T) list,  (*source specification of imports*)
    32   provided: (Path.T * SHA1.digest) list};  (*source path, digest*)
    33 
    34 fun make_files (master_dir, imports, provided): files =
    35  {master_dir = master_dir, imports = imports, provided = provided};
    36 
    37 structure Files = Theory_Data
    38 (
    39   type T = files;
    40   val empty = make_files (Path.current, [], []);
    41   fun extend _ = empty;
    42   fun merge _ = empty;
    43 );
    44 
    45 fun map_files f =
    46   Files.map (fn {master_dir, imports, provided} =>
    47     make_files (f (master_dir, imports, provided)));
    48 
    49 
    50 val master_directory = #master_dir o Files.get;
    51 val imports_of = #imports o Files.get;
    52 
    53 fun put_deps master_dir imports = map_files (fn _ => (master_dir, imports, []));
    54 
    55 
    56 (* theory files *)
    57 
    58 val thy_path = Path.ext "thy";
    59 
    60 fun check_file dir file = File.check_file (File.full_path dir file);
    61 
    62 fun check_thy dir thy_name =
    63   let
    64     val path = thy_path (Path.basic thy_name);
    65     val master_file = check_file dir path;
    66     val text = File.read master_file;
    67 
    68     val {name = (name, pos), imports, keywords} =
    69       Thy_Header.read (Path.position master_file) text;
    70     val _ = thy_name <> name andalso
    71       error ("Bad theory name " ^ quote name ^
    72         " for file " ^ Path.print path ^ Position.here pos);
    73   in
    74    {master = (master_file, SHA1.digest text), text = text, theory_pos = pos,
    75     imports = imports, keywords = keywords}
    76   end;
    77 
    78 
    79 (* load files *)
    80 
    81 fun parse_files cmd =
    82   Scan.ahead Parse.not_eof -- Parse.path >> (fn (tok, name) => fn thy =>
    83     (case Token.get_files tok of
    84       [] =>
    85         let
    86           val keywords = Thy_Header.get_keywords thy;
    87           val master_dir = master_directory thy;
    88           val pos = Token.pos_of tok;
    89           val src_paths = Keyword.command_files keywords cmd (Path.explode name);
    90         in map (Command.read_file master_dir pos) src_paths end
    91     | files => map Exn.release files));
    92 
    93 fun provide (src_path, id) =
    94   map_files (fn (master_dir, imports, provided) =>
    95     if AList.defined (op =) provided src_path then
    96       error ("Duplicate use of source file: " ^ Path.print src_path)
    97     else (master_dir, imports, (src_path, id) :: provided));
    98 
    99 fun provide_parse_files cmd =
   100   parse_files cmd >> (fn files => fn thy =>
   101     let
   102       val fs = files thy;
   103       val thy' = fold (fn {src_path, digest, ...} => provide (src_path, digest)) fs thy;
   104     in (fs, thy') end);
   105 
   106 fun load_file thy src_path =
   107   let
   108     val full_path = check_file (master_directory thy) src_path;
   109     val text = File.read full_path;
   110     val id = SHA1.digest text;
   111   in ((full_path, id), text) end;
   112 
   113 fun loaded_files_current thy =
   114   #provided (Files.get thy) |>
   115     forall (fn (src_path, id) =>
   116       (case try (load_file thy) src_path of
   117         NONE => false
   118       | SOME ((_, id'), _) => id = id'));
   119 
   120 
   121 (* load theory *)
   122 
   123 fun begin_theory master_dir {name, imports, keywords} parents =
   124   Theory.begin_theory name parents
   125   |> put_deps master_dir imports
   126   |> Thy_Header.add_keywords keywords;
   127 
   128 fun excursion keywords master_dir last_timing init elements =
   129   let
   130     fun prepare_span st span =
   131       Command_Span.content span
   132       |> Command.read keywords (Command.read_thy st) master_dir init ([], ~1)
   133       |> (fn tr => Toplevel.put_timing (last_timing tr) tr);
   134 
   135     fun element_result span_elem (st, _) =
   136       let
   137         val elem = Thy_Syntax.map_element (prepare_span st) span_elem;
   138         val (results, st') = Toplevel.element_result keywords elem st;
   139         val pos' = Toplevel.pos_of (Thy_Syntax.last_element elem);
   140       in (results, (st', pos')) end;
   141 
   142     val (results, (end_state, end_pos)) =
   143       fold_map element_result elements (Toplevel.toplevel, Position.none);
   144 
   145     val thy = Toplevel.end_theory end_pos end_state;
   146   in (results, thy) end;
   147 
   148 fun load_thy document last_timing update_time master_dir header text_pos text parents =
   149   let
   150     val (name, _) = #name header;
   151     val keywords =
   152       fold (curry Keyword.merge_keywords o Thy_Header.get_keywords) parents
   153         (Keyword.add_keywords (#keywords header) Keyword.empty_keywords);
   154 
   155     val toks = Token.explode keywords text_pos text;
   156     val spans = Outer_Syntax.parse_spans toks;
   157     val elements = Thy_Syntax.parse_elements keywords spans;
   158 
   159     fun init () =
   160       begin_theory master_dir header parents
   161       |> Present.begin_theory update_time
   162           (fn () => HTML.html_mode (implode o map (Thy_Syntax.present_span keywords)) spans);
   163 
   164     val (results, thy) =
   165       cond_timeit true ("theory " ^ quote name)
   166         (fn () => excursion keywords master_dir last_timing init elements);
   167 
   168     fun present () =
   169       let
   170         val res = filter_out (Toplevel.is_ignored o #1) (maps Toplevel.join_results results);
   171       in
   172         if exists (Toplevel.is_skipped_proof o #2) res then
   173           warning ("Cannot present theory with skipped proofs: " ^ quote name)
   174         else
   175           let val tex_source = Thy_Output.present_thy thy res toks |> Buffer.content;
   176           in if document then Present.theory_output name tex_source else () end
   177       end;
   178 
   179   in (thy, present, size text) end;
   180 
   181 
   182 (* antiquotations *)
   183 
   184 local
   185 
   186 fun check_path strict ctxt dir (name, pos) =
   187   let
   188     val _ = Context_Position.report ctxt pos Markup.language_path;
   189 
   190     val path = Path.append dir (Path.explode name)
   191       handle ERROR msg => error (msg ^ Position.here pos);
   192 
   193     val _ = Context_Position.report ctxt pos (Markup.path (Path.smart_implode path));
   194     val _ =
   195       if can Path.expand path andalso File.exists path then ()
   196       else
   197         let
   198           val path' = perhaps (try Path.expand) path;
   199           val msg = "Bad file: " ^ Path.print path' ^ Position.here pos;
   200         in
   201           if strict then error msg
   202           else if Context_Position.is_visible ctxt then
   203             Output.report
   204               [Markup.markup (Markup.bad |> Markup.properties (Position.properties_of pos)) msg]
   205           else ()
   206         end;
   207   in path end;
   208 
   209 fun file_antiq strict ctxt (name, pos) =
   210   let
   211     val dir = master_directory (Proof_Context.theory_of ctxt);
   212     val _ = check_path strict ctxt dir (name, pos);
   213   in
   214     space_explode "/" name
   215     |> map Latex.output_ascii
   216     |> space_implode (Latex.output_ascii "/" ^ "\\discretionary{}{}{}")
   217     |> enclose "\\isatt{" "}"
   218   end;
   219 
   220 in
   221 
   222 val _ = Theory.setup
   223  (Thy_Output.antiquotation @{binding file} (Scan.lift (Parse.position Parse.path))
   224     (file_antiq true o #context) #>
   225   Thy_Output.antiquotation @{binding file_unchecked} (Scan.lift (Parse.position Parse.path))
   226     (file_antiq false o #context) #>
   227   ML_Antiquotation.value @{binding path}
   228     (Args.context -- Scan.lift (Parse.position Parse.path) >> (fn (ctxt, arg) =>
   229       let val path = check_path true ctxt Path.current arg
   230       in "Path.explode " ^ ML_Syntax.print_string (Path.implode path) end)));
   231 
   232 end;
   233 
   234 end;