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