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