src/Pure/PIDE/resources.ML
author wenzelm
Tue Aug 12 00:08:32 2014 +0200 (2014-08-12 ago)
changeset 57905 c0c5652e796e
parent 56803 d3cc56ca54c9
child 57918 f5d73caba4e5
permissions -rw-r--r--
separate module Command_Span: mostly syntactic representation;
potentially prover-specific Output_Syntax.parse_spans;
wenzelm@56208
     1
(*  Title:      Pure/PIDE/resources.ML
wenzelm@42002
     2
    Author:     Makarius
wenzelm@6168
     3
wenzelm@56208
     4
Resources for theories and auxiliary files.
wenzelm@6168
     5
*)
wenzelm@6168
     6
wenzelm@56208
     7
signature RESOURCES =
wenzelm@6168
     8
sig
wenzelm@37949
     9
  val master_directory: theory -> Path.T
wenzelm@48927
    10
  val imports_of: theory -> (string * Position.T) list
wenzelm@46737
    11
  val thy_path: Path.T -> Path.T
wenzelm@48898
    12
  val check_thy: Path.T -> string ->
wenzelm@48928
    13
   {master: Path.T * SHA1.digest, text: string, theory_pos: Position.T,
wenzelm@51293
    14
    imports: (string * Position.T) list, keywords: Thy_Header.keywords}
wenzelm@54525
    15
  val parse_files: string -> (theory -> Token.file list) parser
wenzelm@48906
    16
  val provide: Path.T * SHA1.digest -> theory -> theory
wenzelm@48906
    17
  val provide_parse_files: string -> (theory -> Token.file list * theory) parser
wenzelm@37949
    18
  val loaded_files: theory -> Path.T list
wenzelm@54446
    19
  val loaded_files_current: theory -> bool
wenzelm@46938
    20
  val begin_theory: Path.T -> Thy_Header.header -> theory list -> theory
wenzelm@54458
    21
  val load_thy: bool -> (Toplevel.transition -> Time.time option) -> int -> Path.T ->
wenzelm@54458
    22
    Thy_Header.header -> Position.T -> string -> theory list -> theory * (unit -> unit) * int
wenzelm@6168
    23
end;
wenzelm@6168
    24
wenzelm@56208
    25
structure Resources: RESOURCES =
wenzelm@6168
    26
struct
wenzelm@6168
    27
wenzelm@37949
    28
(* manage source files *)
wenzelm@37949
    29
wenzelm@37949
    30
type files =
wenzelm@40741
    31
 {master_dir: Path.T,  (*master directory of theory source*)
wenzelm@48927
    32
  imports: (string * Position.T) list,  (*source specification of imports*)
wenzelm@48896
    33
  provided: (Path.T * SHA1.digest) list};  (*source path, digest*)
wenzelm@37949
    34
wenzelm@48886
    35
fun make_files (master_dir, imports, provided): files =
wenzelm@48886
    36
 {master_dir = master_dir, imports = imports, provided = provided};
wenzelm@37949
    37
wenzelm@37949
    38
structure Files = Theory_Data
wenzelm@37949
    39
(
wenzelm@37949
    40
  type T = files;
wenzelm@48886
    41
  val empty = make_files (Path.current, [], []);
wenzelm@37949
    42
  fun extend _ = empty;
wenzelm@37949
    43
  fun merge _ = empty;
wenzelm@37949
    44
);
wenzelm@37949
    45
wenzelm@37949
    46
fun map_files f =
wenzelm@48886
    47
  Files.map (fn {master_dir, imports, provided} =>
wenzelm@48886
    48
    make_files (f (master_dir, imports, provided)));
wenzelm@37949
    49
wenzelm@37949
    50
wenzelm@37949
    51
val master_directory = #master_dir o Files.get;
wenzelm@41548
    52
val imports_of = #imports o Files.get;
wenzelm@37949
    53
wenzelm@48888
    54
fun put_deps master_dir imports = map_files (fn _ => (master_dir, imports, []));
wenzelm@37949
    55
wenzelm@37949
    56
wenzelm@54525
    57
(* theory files *)
wenzelm@54525
    58
wenzelm@54525
    59
val thy_path = Path.ext "thy";
wenzelm@48869
    60
wenzelm@48878
    61
fun check_file dir file = File.check_file (File.full_path dir file);
wenzelm@48878
    62
wenzelm@54525
    63
fun check_thy dir thy_name =
wenzelm@54525
    64
  let
wenzelm@54525
    65
    val path = thy_path (Path.basic thy_name);
wenzelm@54525
    66
    val master_file = check_file dir path;
wenzelm@54525
    67
    val text = File.read master_file;
wenzelm@54525
    68
wenzelm@54525
    69
    val {name = (name, pos), imports, keywords} =
wenzelm@54525
    70
      Thy_Header.read (Path.position master_file) text;
wenzelm@54525
    71
    val _ = thy_name <> name andalso
wenzelm@54525
    72
      error ("Bad file name " ^ Path.print path ^ " for theory " ^ quote name ^ Position.here pos);
wenzelm@54525
    73
  in
wenzelm@54525
    74
   {master = (master_file, SHA1.digest text), text = text, theory_pos = pos,
wenzelm@54525
    75
    imports = imports, keywords = keywords}
wenzelm@54525
    76
  end;
wenzelm@54525
    77
wenzelm@54525
    78
wenzelm@54526
    79
(* load files *)
wenzelm@48898
    80
wenzelm@48898
    81
fun parse_files cmd =
wenzelm@48898
    82
  Scan.ahead Parse.not_eof -- Parse.path >> (fn (tok, name) => fn thy =>
wenzelm@48898
    83
    (case Token.get_files tok of
wenzelm@54526
    84
      [] =>
wenzelm@54526
    85
        let
wenzelm@54526
    86
          val master_dir = master_directory thy;
wenzelm@55708
    87
          val pos = Token.pos_of tok;
wenzelm@54526
    88
          val src_paths = Keyword.command_files cmd (Path.explode name);
wenzelm@54526
    89
        in map (Command.read_file master_dir pos) src_paths end
wenzelm@54520
    90
    | files => map Exn.release files));
wenzelm@48869
    91
wenzelm@48906
    92
fun provide (src_path, id) =
wenzelm@48906
    93
  map_files (fn (master_dir, imports, provided) =>
wenzelm@48906
    94
    if AList.defined (op =) provided src_path then
wenzelm@48906
    95
      error ("Duplicate use of source file: " ^ Path.print src_path)
wenzelm@48906
    96
    else (master_dir, imports, (src_path, id) :: provided));
wenzelm@48906
    97
wenzelm@48906
    98
fun provide_parse_files cmd =
wenzelm@48906
    99
  parse_files cmd >> (fn files => fn thy =>
wenzelm@48906
   100
    let
wenzelm@48906
   101
      val fs = files thy;
wenzelm@55788
   102
      val thy' = fold (fn {src_path, digest, ...} => provide (src_path, digest)) fs thy;
wenzelm@48906
   103
    in (fs, thy') end);
wenzelm@48906
   104
wenzelm@43702
   105
fun load_file thy src_path =
wenzelm@43702
   106
  let
wenzelm@43702
   107
    val full_path = check_file (master_directory thy) src_path;
wenzelm@43702
   108
    val text = File.read full_path;
wenzelm@43702
   109
    val id = SHA1.digest text;
wenzelm@43702
   110
  in ((full_path, id), text) end;
wenzelm@43702
   111
wenzelm@54446
   112
fun loaded_files_current thy =
wenzelm@48886
   113
  #provided (Files.get thy) |>
wenzelm@48896
   114
    forall (fn (src_path, id) =>
wenzelm@43702
   115
      (case try (load_file thy) src_path of
wenzelm@37949
   116
        NONE => false
wenzelm@48886
   117
      | SOME ((_, id'), _) => id = id'));
wenzelm@37952
   118
wenzelm@54454
   119
(*Proof General legacy*)
wenzelm@54454
   120
fun loaded_files thy =
wenzelm@54454
   121
  let val {master_dir, provided, ...} = Files.get thy
wenzelm@54454
   122
  in map (File.full_path master_dir o #1) provided end;
wenzelm@54454
   123
wenzelm@37949
   124
wenzelm@54525
   125
(* load theory *)
wenzelm@37977
   126
wenzelm@51294
   127
fun begin_theory master_dir {name, imports, keywords} parents =
wenzelm@37977
   128
  Theory.begin_theory name parents
wenzelm@48888
   129
  |> put_deps master_dir imports
wenzelm@52788
   130
  |> fold Thy_Header.declare_keyword keywords;
wenzelm@37977
   131
wenzelm@54526
   132
fun excursion master_dir last_timing init elements =
wenzelm@46959
   133
  let
wenzelm@51268
   134
    fun prepare_span span =
wenzelm@57905
   135
      Command_Span.content span
wenzelm@54526
   136
      |> Command.read init master_dir []
wenzelm@51268
   137
      |> (fn tr => Toplevel.put_timing (last_timing tr) tr);
wenzelm@51217
   138
wenzelm@51268
   139
    fun element_result span_elem (st, _) =
wenzelm@46959
   140
      let
wenzelm@51273
   141
        val elem = Thy_Syntax.map_element prepare_span span_elem;
wenzelm@51273
   142
        val (results, st') = Toplevel.element_result elem st;
wenzelm@51273
   143
        val pos' = Toplevel.pos_of (Thy_Syntax.last_element elem);
wenzelm@51268
   144
      in (results, (st', pos')) end;
wenzelm@46959
   145
wenzelm@46959
   146
    val (results, (end_state, end_pos)) =
wenzelm@46959
   147
      fold_map element_result elements (Toplevel.toplevel, Position.none);
wenzelm@46959
   148
wenzelm@46959
   149
    val thy = Toplevel.end_theory end_pos end_state;
wenzelm@51273
   150
  in (results, thy) end;
wenzelm@46959
   151
wenzelm@54458
   152
fun load_thy document last_timing update_time master_dir header text_pos text parents =
wenzelm@43712
   153
  let
wenzelm@43712
   154
    val time = ! Toplevel.timing;
wenzelm@43712
   155
wenzelm@51294
   156
    val {name = (name, _), ...} = header;
wenzelm@46958
   157
    val _ = Thy_Header.define_keywords header;
wenzelm@43712
   158
wenzelm@46959
   159
    val lexs = Keyword.get_lexicons ();
wenzelm@57905
   160
    val toks = Outer_Syntax.parse_tokens lexs text_pos text;
wenzelm@57905
   161
    val spans = Outer_Syntax.parse_spans toks;
wenzelm@46959
   162
    val elements = Thy_Syntax.parse_elements spans;
wenzelm@43712
   163
wenzelm@54456
   164
    fun init () =
wenzelm@54456
   165
      begin_theory master_dir header parents
wenzelm@54456
   166
      |> Present.begin_theory update_time
wenzelm@54456
   167
          (fn () => HTML.html_mode (implode o map Thy_Syntax.present_span) spans);
wenzelm@43712
   168
wenzelm@43712
   169
    val _ = if time then writeln ("\n**** Starting theory " ^ quote name ^ " ****") else ();
wenzelm@54526
   170
    val (results, thy) =
wenzelm@54526
   171
      cond_timeit time "" (fn () => excursion master_dir last_timing init elements);
wenzelm@43712
   172
    val _ = if time then writeln ("**** Finished theory " ^ quote name ^ " ****\n") else ();
wenzelm@43712
   173
wenzelm@51331
   174
    fun present () =
wenzelm@51331
   175
      let
wenzelm@51332
   176
        val res = filter_out (Toplevel.is_ignored o #1) (maps Toplevel.join_results results);
wenzelm@51331
   177
        val ((minor, _), outer_syntax) = Outer_Syntax.get_syntax ();
wenzelm@51331
   178
      in
wenzelm@51556
   179
        if exists (Toplevel.is_skipped_proof o #2) res then
wenzelm@51556
   180
          warning ("Cannot present theory with skipped proofs: " ^ quote name)
wenzelm@51556
   181
        else
wenzelm@54458
   182
          let val tex_source =
wenzelm@54458
   183
            Thy_Output.present_thy minor Keyword.command_tags
wenzelm@54458
   184
              (Outer_Syntax.is_markup outer_syntax) res toks
wenzelm@54458
   185
            |> Buffer.content;
wenzelm@54458
   186
          in if document then Present.theory_output name tex_source else () end
wenzelm@51331
   187
      end;
wenzelm@43712
   188
wenzelm@51331
   189
  in (thy, present, size text) end;
wenzelm@43712
   190
wenzelm@42002
   191
wenzelm@56135
   192
(* antiquotations *)
wenzelm@49244
   193
wenzelm@56135
   194
local
wenzelm@56135
   195
wenzelm@56135
   196
fun check_path strict ctxt dir (name, pos) =
wenzelm@54705
   197
  let
wenzelm@56034
   198
    val _ = Context_Position.report ctxt pos Markup.language_path;
wenzelm@56034
   199
wenzelm@55882
   200
    val path = Path.append dir (Path.explode name)
wenzelm@55882
   201
      handle ERROR msg => error (msg ^ Position.here pos);
wenzelm@55882
   202
wenzelm@56134
   203
    val _ = Context_Position.report ctxt pos (Markup.path (Path.smart_implode path));
wenzelm@54705
   204
    val _ =
wenzelm@55882
   205
      if can Path.expand path andalso File.exists path then ()
wenzelm@55882
   206
      else
wenzelm@55882
   207
        let
wenzelm@55882
   208
          val path' = perhaps (try Path.expand) path;
wenzelm@55882
   209
          val msg = "Bad file: " ^ Path.print path' ^ Position.here pos;
wenzelm@55882
   210
        in
wenzelm@55882
   211
          if strict then error msg
wenzelm@56294
   212
          else if Context_Position.is_visible ctxt then
wenzelm@56294
   213
            Output.report
wenzelm@56333
   214
              [Markup.markup (Markup.bad |> Markup.properties (Position.properties_of pos)) msg]
wenzelm@56294
   215
          else ()
wenzelm@55882
   216
        end;
wenzelm@56135
   217
  in path end;
wenzelm@56135
   218
wenzelm@56135
   219
fun file_antiq strict ctxt (name, pos) =
wenzelm@56135
   220
  let
wenzelm@56135
   221
    val dir = master_directory (Proof_Context.theory_of ctxt);
wenzelm@56135
   222
    val _ = check_path strict ctxt dir (name, pos);
wenzelm@54705
   223
  in
wenzelm@54705
   224
    space_explode "/" name
wenzelm@54705
   225
    |> map Thy_Output.verb_text
wenzelm@54705
   226
    |> space_implode (Thy_Output.verb_text "/" ^ "\\discretionary{}{}{}")
wenzelm@54705
   227
  end;
wenzelm@54705
   228
wenzelm@56135
   229
in
wenzelm@56135
   230
wenzelm@53171
   231
val _ = Theory.setup
wenzelm@56204
   232
 (Thy_Output.antiquotation @{binding file} (Scan.lift (Parse.position Parse.path))
wenzelm@54705
   233
    (file_antiq true o #context) #>
wenzelm@56204
   234
  Thy_Output.antiquotation @{binding file_unchecked} (Scan.lift (Parse.position Parse.path))
wenzelm@56135
   235
    (file_antiq false o #context) #>
wenzelm@56204
   236
  ML_Antiquotation.value @{binding path}
wenzelm@56135
   237
    (Args.context -- Scan.lift (Parse.position Parse.path) >> (fn (ctxt, arg) =>
wenzelm@56135
   238
      let val path = check_path true ctxt Path.current arg
wenzelm@56135
   239
      in "Path.explode " ^ ML_Syntax.print_string (Path.implode path) end)));
wenzelm@49244
   240
wenzelm@6168
   241
end;
wenzelm@56135
   242
wenzelm@56135
   243
end;