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