src/Pure/PIDE/resources.ML
author wenzelm
Mon Mar 31 10:28:08 2014 +0200 (2014-03-31 ago)
changeset 56333 38f1422ef473
parent 56294 85911b8a6868
child 56803 d3cc56ca54c9
permissions -rw-r--r--
support bulk messages consisting of small string segments, which are more healthy to the Poly/ML RTS and might prevent spurious GC crashes such as MTGCProcessMarkPointers::ScanAddressesInObject;
     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 load_file: theory -> Path.T -> (Path.T * SHA1.digest) * string
    19   val loaded_files: theory -> Path.T list
    20   val loaded_files_current: theory -> bool
    21   val begin_theory: Path.T -> Thy_Header.header -> theory list -> theory
    22   val load_thy: bool -> (Toplevel.transition -> Time.time option) -> int -> Path.T ->
    23     Thy_Header.header -> Position.T -> string -> theory list -> theory * (unit -> unit) * int
    24 end;
    25 
    26 structure Resources: RESOURCES =
    27 struct
    28 
    29 (* manage source files *)
    30 
    31 type files =
    32  {master_dir: Path.T,  (*master directory of theory source*)
    33   imports: (string * Position.T) list,  (*source specification of imports*)
    34   provided: (Path.T * SHA1.digest) list};  (*source path, digest*)
    35 
    36 fun make_files (master_dir, imports, provided): files =
    37  {master_dir = master_dir, imports = imports, provided = provided};
    38 
    39 structure Files = Theory_Data
    40 (
    41   type T = files;
    42   val empty = make_files (Path.current, [], []);
    43   fun extend _ = empty;
    44   fun merge _ = empty;
    45 );
    46 
    47 fun map_files f =
    48   Files.map (fn {master_dir, imports, provided} =>
    49     make_files (f (master_dir, imports, provided)));
    50 
    51 
    52 val master_directory = #master_dir o Files.get;
    53 val imports_of = #imports o Files.get;
    54 
    55 fun put_deps master_dir imports = map_files (fn _ => (master_dir, imports, []));
    56 
    57 
    58 (* theory files *)
    59 
    60 val thy_path = Path.ext "thy";
    61 
    62 fun check_file dir file = File.check_file (File.full_path dir file);
    63 
    64 fun check_thy dir thy_name =
    65   let
    66     val path = thy_path (Path.basic thy_name);
    67     val master_file = check_file dir path;
    68     val text = File.read master_file;
    69 
    70     val {name = (name, pos), imports, keywords} =
    71       Thy_Header.read (Path.position master_file) text;
    72     val _ = thy_name <> name andalso
    73       error ("Bad file name " ^ Path.print path ^ " for theory " ^ quote name ^ Position.here pos);
    74   in
    75    {master = (master_file, SHA1.digest text), text = text, theory_pos = pos,
    76     imports = imports, keywords = keywords}
    77   end;
    78 
    79 
    80 (* load files *)
    81 
    82 fun parse_files cmd =
    83   Scan.ahead Parse.not_eof -- Parse.path >> (fn (tok, name) => fn thy =>
    84     (case Token.get_files tok of
    85       [] =>
    86         let
    87           val master_dir = master_directory thy;
    88           val pos = Token.pos_of tok;
    89           val src_paths = Keyword.command_files 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     val _ = legacy_feature ("Raw file-system access to load file " ^ Path.print full_path);
   112   in ((full_path, id), text) end;
   113 
   114 fun loaded_files_current thy =
   115   #provided (Files.get thy) |>
   116     forall (fn (src_path, id) =>
   117       (case try (load_file thy) src_path of
   118         NONE => false
   119       | SOME ((_, id'), _) => id = id'));
   120 
   121 (*Proof General legacy*)
   122 fun loaded_files thy =
   123   let val {master_dir, provided, ...} = Files.get thy
   124   in map (File.full_path master_dir o #1) provided end;
   125 
   126 
   127 (* load theory *)
   128 
   129 fun begin_theory master_dir {name, imports, keywords} parents =
   130   Theory.begin_theory name parents
   131   |> put_deps master_dir imports
   132   |> fold Thy_Header.declare_keyword keywords;
   133 
   134 fun excursion master_dir last_timing init elements =
   135   let
   136     fun prepare_span span =
   137       Thy_Syntax.span_content span
   138       |> Command.read init master_dir []
   139       |> (fn tr => Toplevel.put_timing (last_timing tr) tr);
   140 
   141     fun element_result span_elem (st, _) =
   142       let
   143         val elem = Thy_Syntax.map_element prepare_span span_elem;
   144         val (results, st') = Toplevel.element_result elem st;
   145         val pos' = Toplevel.pos_of (Thy_Syntax.last_element elem);
   146       in (results, (st', pos')) end;
   147 
   148     val (results, (end_state, end_pos)) =
   149       fold_map element_result elements (Toplevel.toplevel, Position.none);
   150 
   151     val thy = Toplevel.end_theory end_pos end_state;
   152   in (results, thy) end;
   153 
   154 fun load_thy document last_timing update_time master_dir header text_pos text parents =
   155   let
   156     val time = ! Toplevel.timing;
   157 
   158     val {name = (name, _), ...} = header;
   159     val _ = Thy_Header.define_keywords header;
   160 
   161     val lexs = Keyword.get_lexicons ();
   162     val toks = Thy_Syntax.parse_tokens lexs text_pos text;
   163     val spans = Thy_Syntax.parse_spans toks;
   164     val elements = Thy_Syntax.parse_elements spans;
   165 
   166     fun init () =
   167       begin_theory master_dir header parents
   168       |> Present.begin_theory update_time
   169           (fn () => HTML.html_mode (implode o map Thy_Syntax.present_span) spans);
   170 
   171     val _ = if time then writeln ("\n**** Starting theory " ^ quote name ^ " ****") else ();
   172     val (results, thy) =
   173       cond_timeit time "" (fn () => excursion master_dir last_timing init elements);
   174     val _ = if time then writeln ("**** Finished theory " ^ quote name ^ " ****\n") else ();
   175 
   176     fun present () =
   177       let
   178         val res = filter_out (Toplevel.is_ignored o #1) (maps Toplevel.join_results results);
   179         val ((minor, _), outer_syntax) = Outer_Syntax.get_syntax ();
   180       in
   181         if exists (Toplevel.is_skipped_proof o #2) res then
   182           warning ("Cannot present theory with skipped proofs: " ^ quote name)
   183         else
   184           let val tex_source =
   185             Thy_Output.present_thy minor Keyword.command_tags
   186               (Outer_Syntax.is_markup outer_syntax) res toks
   187             |> Buffer.content;
   188           in if document then Present.theory_output name tex_source else () end
   189       end;
   190 
   191   in (thy, present, size text) end;
   192 
   193 
   194 (* antiquotations *)
   195 
   196 local
   197 
   198 fun check_path strict ctxt dir (name, pos) =
   199   let
   200     val _ = Context_Position.report ctxt pos Markup.language_path;
   201 
   202     val path = Path.append dir (Path.explode name)
   203       handle ERROR msg => error (msg ^ Position.here pos);
   204 
   205     val _ = Context_Position.report ctxt pos (Markup.path (Path.smart_implode path));
   206     val _ =
   207       if can Path.expand path andalso File.exists path then ()
   208       else
   209         let
   210           val path' = perhaps (try Path.expand) path;
   211           val msg = "Bad file: " ^ Path.print path' ^ Position.here pos;
   212         in
   213           if strict then error msg
   214           else if Context_Position.is_visible ctxt then
   215             Output.report
   216               [Markup.markup (Markup.bad |> Markup.properties (Position.properties_of pos)) msg]
   217           else ()
   218         end;
   219   in path end;
   220 
   221 fun file_antiq strict ctxt (name, pos) =
   222   let
   223     val dir = master_directory (Proof_Context.theory_of ctxt);
   224     val _ = check_path strict ctxt dir (name, pos);
   225   in
   226     space_explode "/" name
   227     |> map Thy_Output.verb_text
   228     |> space_implode (Thy_Output.verb_text "/" ^ "\\discretionary{}{}{}")
   229   end;
   230 
   231 in
   232 
   233 val _ = Theory.setup
   234  (Thy_Output.antiquotation @{binding file} (Scan.lift (Parse.position Parse.path))
   235     (file_antiq true o #context) #>
   236   Thy_Output.antiquotation @{binding file_unchecked} (Scan.lift (Parse.position Parse.path))
   237     (file_antiq false o #context) #>
   238   ML_Antiquotation.value @{binding path}
   239     (Args.context -- Scan.lift (Parse.position Parse.path) >> (fn (ctxt, arg) =>
   240       let val path = check_path true ctxt Path.current arg
   241       in "Path.explode " ^ ML_Syntax.print_string (Path.implode path) end)));
   242 
   243 end;
   244 
   245 end;