src/Pure/PIDE/resources.ML
author wenzelm
Wed Dec 03 14:04:38 2014 +0100 (2014-12-03 ago)
changeset 59083 88b0b1f28adc
parent 58934 385a4cc7426f
child 59123 e68e44836d04
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 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 file name " ^ Path.print path ^ " for theory " ^ quote name ^ Position.here pos);
    72   in
    73    {master = (master_file, SHA1.digest text), text = text, theory_pos = pos,
    74     imports = imports, keywords = keywords}
    75   end;
    76 
    77 
    78 (* load files *)
    79 
    80 fun parse_files cmd =
    81   Scan.ahead Parse.not_eof -- Parse.path >> (fn (tok, name) => fn thy =>
    82     (case Token.get_files tok of
    83       [] =>
    84         let
    85           val keywords = Thy_Header.get_keywords thy;
    86           val master_dir = master_directory thy;
    87           val pos = Token.pos_of tok;
    88           val src_paths = Keyword.command_files keywords cmd (Path.explode name);
    89         in map (Command.read_file master_dir pos) src_paths end
    90     | files => map Exn.release files));
    91 
    92 fun provide (src_path, id) =
    93   map_files (fn (master_dir, imports, provided) =>
    94     if AList.defined (op =) provided src_path then
    95       error ("Duplicate use of source file: " ^ Path.print src_path)
    96     else (master_dir, imports, (src_path, id) :: provided));
    97 
    98 fun provide_parse_files cmd =
    99   parse_files cmd >> (fn files => fn thy =>
   100     let
   101       val fs = files thy;
   102       val thy' = fold (fn {src_path, digest, ...} => provide (src_path, digest)) fs thy;
   103     in (fs, thy') end);
   104 
   105 fun load_file thy src_path =
   106   let
   107     val full_path = check_file (master_directory thy) src_path;
   108     val text = File.read full_path;
   109     val id = SHA1.digest text;
   110   in ((full_path, id), text) end;
   111 
   112 fun loaded_files_current thy =
   113   #provided (Files.get thy) |>
   114     forall (fn (src_path, id) =>
   115       (case try (load_file thy) src_path of
   116         NONE => false
   117       | SOME ((_, id'), _) => id = id'));
   118 
   119 
   120 (* load theory *)
   121 
   122 fun begin_theory master_dir {name, imports, keywords} parents =
   123   Theory.begin_theory name parents
   124   |> put_deps master_dir imports
   125   |> Thy_Header.add_keywords keywords;
   126 
   127 fun excursion keywords master_dir last_timing init elements =
   128   let
   129     fun prepare_span st span =
   130       Command_Span.content span
   131       |> Command.read keywords (Command.read_thy st) master_dir init []
   132       |> (fn tr => Toplevel.put_timing (last_timing tr) tr);
   133 
   134     fun element_result span_elem (st, _) =
   135       let
   136         val elem = Thy_Syntax.map_element (prepare_span st) span_elem;
   137         val (results, st') = Toplevel.element_result keywords elem st;
   138         val pos' = Toplevel.pos_of (Thy_Syntax.last_element elem);
   139       in (results, (st', pos')) end;
   140 
   141     val (results, (end_state, end_pos)) =
   142       fold_map element_result elements (Toplevel.toplevel, Position.none);
   143 
   144     val thy = Toplevel.end_theory end_pos end_state;
   145   in (results, thy) end;
   146 
   147 fun load_thy document last_timing update_time master_dir header text_pos text parents =
   148   let
   149     val (name, _) = #name header;
   150     val keywords =
   151       fold (curry Keyword.merge_keywords o Thy_Header.get_keywords) parents
   152         (Keyword.add_keywords (#keywords header) Keyword.empty_keywords);
   153 
   154     val toks = Token.explode keywords text_pos text;
   155     val spans = Outer_Syntax.parse_spans toks;
   156     val elements = Thy_Syntax.parse_elements keywords spans;
   157 
   158     fun init () =
   159       begin_theory master_dir header parents
   160       |> Present.begin_theory update_time
   161           (fn () => HTML.html_mode (implode o map Thy_Syntax.present_span) spans);
   162 
   163     val (results, thy) =
   164       cond_timeit true ("theory " ^ quote name)
   165         (fn () => excursion keywords master_dir last_timing init elements);
   166 
   167     fun present () =
   168       let
   169         val res = filter_out (Toplevel.is_ignored o #1) (maps Toplevel.join_results results);
   170       in
   171         if exists (Toplevel.is_skipped_proof o #2) res then
   172           warning ("Cannot present theory with skipped proofs: " ^ quote name)
   173         else
   174           let val tex_source = Thy_Output.present_thy thy res toks |> Buffer.content;
   175           in if document then Present.theory_output name tex_source else () end
   176       end;
   177 
   178   in (thy, present, size text) end;
   179 
   180 
   181 (* antiquotations *)
   182 
   183 local
   184 
   185 fun check_path strict ctxt dir (name, pos) =
   186   let
   187     val _ = Context_Position.report ctxt pos Markup.language_path;
   188 
   189     val path = Path.append dir (Path.explode name)
   190       handle ERROR msg => error (msg ^ Position.here pos);
   191 
   192     val _ = Context_Position.report ctxt pos (Markup.path (Path.smart_implode path));
   193     val _ =
   194       if can Path.expand path andalso File.exists path then ()
   195       else
   196         let
   197           val path' = perhaps (try Path.expand) path;
   198           val msg = "Bad file: " ^ Path.print path' ^ Position.here pos;
   199         in
   200           if strict then error msg
   201           else if Context_Position.is_visible ctxt then
   202             Output.report
   203               [Markup.markup (Markup.bad |> Markup.properties (Position.properties_of pos)) msg]
   204           else ()
   205         end;
   206   in path end;
   207 
   208 fun file_antiq strict ctxt (name, pos) =
   209   let
   210     val dir = master_directory (Proof_Context.theory_of ctxt);
   211     val _ = check_path strict ctxt dir (name, pos);
   212   in
   213     space_explode "/" name
   214     |> map Latex.output_ascii
   215     |> space_implode (Latex.output_ascii "/" ^ "\\discretionary{}{}{}")
   216     |> enclose "\\isatt{" "}"
   217   end;
   218 
   219 in
   220 
   221 val _ = Theory.setup
   222  (Thy_Output.antiquotation @{binding file} (Scan.lift (Parse.position Parse.path))
   223     (file_antiq true o #context) #>
   224   Thy_Output.antiquotation @{binding file_unchecked} (Scan.lift (Parse.position Parse.path))
   225     (file_antiq false o #context) #>
   226   ML_Antiquotation.value @{binding path}
   227     (Args.context -- Scan.lift (Parse.position Parse.path) >> (fn (ctxt, arg) =>
   228       let val path = check_path true ctxt Path.current arg
   229       in "Path.explode " ^ ML_Syntax.print_string (Path.implode path) end)));
   230 
   231 end;
   232 
   233 end;