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