src/Pure/PIDE/resources.ML
author wenzelm
Wed Dec 06 18:59:33 2017 +0100 (22 months ago ago)
changeset 67147 dea94b1aabc3
parent 67104 a2fa0c6a7aff
child 67209 fca5f2988091
permissions -rw-r--r--
prefer control symbol antiquotations;
     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 default_qualifier: string
    10   val init_session_base:
    11     {global_theories: (string * string) list,
    12      loaded_theories: string list,
    13      known_theories: (string * string) list} -> unit
    14   val finish_session_base: unit -> unit
    15   val global_theory: string -> string option
    16   val loaded_theory: string -> bool
    17   val known_theory: string -> Path.T option
    18   val master_directory: theory -> Path.T
    19   val imports_of: theory -> (string * Position.T) list
    20   val begin_theory: Path.T -> Thy_Header.header -> theory list -> theory
    21   val thy_path: Path.T -> Path.T
    22   val theory_qualifier: string -> string
    23   val import_name: string -> Path.T -> string -> {master_dir: Path.T, theory_name: string}
    24   val check_thy: Path.T -> string ->
    25    {master: Path.T * SHA1.digest, text: string, theory_pos: Position.T,
    26     imports: (string * Position.T) list, keywords: Thy_Header.keywords}
    27   val parse_files: string -> (theory -> Token.file list) parser
    28   val provide: Path.T * SHA1.digest -> theory -> theory
    29   val provide_parse_files: string -> (theory -> Token.file list * theory) parser
    30   val loaded_files_current: theory -> bool
    31 end;
    32 
    33 structure Resources: RESOURCES =
    34 struct
    35 
    36 (* session base *)
    37 
    38 val default_qualifier = "Draft";
    39 
    40 val empty_session_base =
    41   {global_theories = Symtab.empty: string Symtab.table,
    42    loaded_theories = Symtab.empty: unit Symtab.table,
    43    known_theories = Symtab.empty: Path.T Symtab.table};
    44 
    45 val global_session_base =
    46   Synchronized.var "Sessions.base" empty_session_base;
    47 
    48 fun init_session_base {global_theories, loaded_theories, known_theories} =
    49   Synchronized.change global_session_base
    50     (fn _ =>
    51       {global_theories = Symtab.make global_theories,
    52        loaded_theories = Symtab.make_set loaded_theories,
    53        known_theories = Symtab.make (map (apsnd Path.explode) known_theories)});
    54 
    55 fun finish_session_base () =
    56   Synchronized.change global_session_base
    57     (fn {global_theories, loaded_theories, ...} =>
    58       {global_theories = global_theories,
    59        loaded_theories = loaded_theories,
    60        known_theories = #known_theories empty_session_base});
    61 
    62 fun get_session_base f = f (Synchronized.value global_session_base);
    63 
    64 fun global_theory a = Symtab.lookup (get_session_base #global_theories) a;
    65 fun loaded_theory a = Symtab.defined (get_session_base #loaded_theories) a;
    66 fun known_theory a = Symtab.lookup (get_session_base #known_theories) a;
    67 
    68 
    69 (* manage source files *)
    70 
    71 type files =
    72  {master_dir: Path.T,  (*master directory of theory source*)
    73   imports: (string * Position.T) list,  (*source specification of imports*)
    74   provided: (Path.T * SHA1.digest) list};  (*source path, digest*)
    75 
    76 fun make_files (master_dir, imports, provided): files =
    77  {master_dir = master_dir, imports = imports, provided = provided};
    78 
    79 structure Files = Theory_Data
    80 (
    81   type T = files;
    82   val empty = make_files (Path.current, [], []);
    83   fun extend _ = empty;
    84   fun merge _ = empty;
    85 );
    86 
    87 fun map_files f =
    88   Files.map (fn {master_dir, imports, provided} =>
    89     make_files (f (master_dir, imports, provided)));
    90 
    91 
    92 val master_directory = #master_dir o Files.get;
    93 val imports_of = #imports o Files.get;
    94 
    95 fun begin_theory master_dir {name, imports, keywords} parents =
    96   Theory.begin_theory name parents
    97   |> map_files (fn _ => (master_dir, imports, []))
    98   |> Thy_Header.add_keywords keywords;
    99 
   100 
   101 (* theory files *)
   102 
   103 val thy_path = Path.ext "thy";
   104 
   105 fun theory_qualifier theory =
   106   (case global_theory theory of
   107     SOME qualifier => qualifier
   108   | NONE => Long_Name.qualifier theory);
   109 
   110 fun theory_name qualifier theory =
   111   if Long_Name.is_qualified theory orelse is_some (global_theory theory)
   112   then theory
   113   else Long_Name.qualify qualifier theory;
   114 
   115 fun import_name qualifier dir s =
   116   let val theory = theory_name qualifier (Thy_Header.import_name s) in
   117     if loaded_theory theory then {master_dir = Path.current, theory_name = theory}
   118     else
   119       let
   120         val node_name =
   121           (case known_theory theory of
   122             SOME node_name => node_name
   123           | NONE =>
   124               if Thy_Header.is_base_name s andalso Long_Name.is_qualified s
   125               then Path.explode s
   126               else File.full_path dir (thy_path (Path.expand (Path.explode s))));
   127       in {master_dir = Path.dir node_name, theory_name = theory} end
   128   end;
   129 
   130 fun check_file dir file = File.check_file (File.full_path dir file);
   131 
   132 fun check_thy dir thy_name =
   133   let
   134     val thy_base_name = Long_Name.base_name thy_name;
   135     val master_file =
   136       (case known_theory thy_name of
   137         SOME known_path => check_file Path.current known_path
   138       | NONE => check_file dir (thy_path (Path.basic thy_base_name)));
   139     val text = File.read master_file;
   140 
   141     val {name = (name, pos), imports, keywords} =
   142       Thy_Header.read (Path.position master_file) text;
   143     val _ =
   144       thy_base_name <> name andalso
   145         error ("Bad theory name " ^ quote name ^
   146           " for file " ^ Path.print (Path.base master_file) ^ Position.here pos);
   147   in
   148    {master = (master_file, SHA1.digest text), text = text, theory_pos = pos,
   149     imports = imports, keywords = keywords}
   150   end;
   151 
   152 
   153 (* load files *)
   154 
   155 fun parse_files cmd =
   156   Scan.ahead Parse.not_eof -- Parse.path >> (fn (tok, name) => fn thy =>
   157     (case Token.get_files tok of
   158       [] =>
   159         let
   160           val keywords = Thy_Header.get_keywords thy;
   161           val master_dir = master_directory thy;
   162           val pos = Token.pos_of tok;
   163           val src_paths = Keyword.command_files keywords cmd (Path.explode name);
   164         in map (Command.read_file master_dir pos) src_paths end
   165     | files => map Exn.release files));
   166 
   167 fun provide (src_path, id) =
   168   map_files (fn (master_dir, imports, provided) =>
   169     if AList.defined (op =) provided src_path then
   170       error ("Duplicate use of source file: " ^ Path.print src_path)
   171     else (master_dir, imports, (src_path, id) :: provided));
   172 
   173 fun provide_parse_files cmd =
   174   parse_files cmd >> (fn files => fn thy =>
   175     let
   176       val fs = files thy;
   177       val thy' = fold (fn {src_path, digest, ...} => provide (src_path, digest)) fs thy;
   178     in (fs, thy') end);
   179 
   180 fun load_file thy src_path =
   181   let
   182     val full_path = check_file (master_directory thy) src_path;
   183     val text = File.read full_path;
   184     val id = SHA1.digest text;
   185   in ((full_path, id), text) end;
   186 
   187 fun loaded_files_current thy =
   188   #provided (Files.get thy) |>
   189     forall (fn (src_path, id) =>
   190       (case try (load_file thy) src_path of
   191         NONE => false
   192       | SOME ((_, id'), _) => id = id'));
   193 
   194 
   195 (* antiquotations *)
   196 
   197 local
   198 
   199 fun err msg pos = error (msg ^ Position.here pos);
   200 
   201 fun check_path check_file ctxt dir (name, pos) =
   202   let
   203     val _ = Context_Position.report ctxt pos Markup.language_path;
   204 
   205     val path = Path.append dir (Path.explode name) handle ERROR msg => err msg pos;
   206     val _ = Path.expand path handle ERROR msg => err msg pos;
   207     val _ = Context_Position.report ctxt pos (Markup.path (Path.smart_implode path));
   208     val _ =
   209       (case check_file of
   210         NONE => path
   211       | SOME check => (check path handle ERROR msg => err msg pos));
   212   in path end;
   213 
   214 fun document_antiq check_file ctxt (name, pos) =
   215   let
   216     val dir = master_directory (Proof_Context.theory_of ctxt);
   217     val _ = check_path check_file ctxt dir (name, pos);
   218   in
   219     space_explode "/" name
   220     |> map Latex.output_ascii
   221     |> space_implode (Latex.output_ascii "/" ^ "\\discretionary{}{}{}")
   222     |> enclose "\\isatt{" "}"
   223   end;
   224 
   225 fun ML_antiq check_file ctxt (name, pos) =
   226   let val path = check_path check_file ctxt Path.current (name, pos);
   227   in "Path.explode " ^ ML_Syntax.print_string (Path.implode path) end;
   228 
   229 in
   230 
   231 val _ = Theory.setup
   232  (Thy_Output.antiquotation \<^binding>\<open>path\<close> (Scan.lift (Parse.position Parse.path))
   233     (document_antiq NONE o #context) #>
   234   Thy_Output.antiquotation \<^binding>\<open>file\<close> (Scan.lift (Parse.position Parse.path))
   235     (document_antiq (SOME File.check_file) o #context) #>
   236   Thy_Output.antiquotation \<^binding>\<open>dir\<close> (Scan.lift (Parse.position Parse.path))
   237     (document_antiq (SOME File.check_dir) o #context) #>
   238   ML_Antiquotation.value \<^binding>\<open>path\<close>
   239     (Args.context -- Scan.lift (Parse.position Parse.path)
   240       >> uncurry (ML_antiq NONE)) #>
   241   ML_Antiquotation.value \<^binding>\<open>file\<close>
   242     (Args.context -- Scan.lift (Parse.position Parse.path)
   243       >> uncurry (ML_antiq (SOME File.check_file))) #>
   244   ML_Antiquotation.value \<^binding>\<open>dir\<close>
   245     (Args.context -- Scan.lift (Parse.position Parse.path)
   246       >> uncurry (ML_antiq (SOME File.check_dir))));
   247 
   248 end;
   249 
   250 end;