| 67215 |      1 | (*  Title:      Pure/Thy/sessions.ML
 | 
|  |      2 |     Author:     Makarius
 | 
|  |      3 | 
 | 
|  |      4 | Support for session ROOT syntax.
 | 
|  |      5 | *)
 | 
|  |      6 | 
 | 
|  |      7 | signature SESSIONS =
 | 
|  |      8 | sig
 | 
|  |      9 |   val root_name: string
 | 
|  |     10 |   val theory_name: string
 | 
|  |     11 |   val command_parser: (Toplevel.transition -> Toplevel.transition) parser
 | 
|  |     12 | end;
 | 
|  |     13 | 
 | 
|  |     14 | structure Sessions: SESSIONS =
 | 
|  |     15 | struct
 | 
|  |     16 | 
 | 
|  |     17 | val root_name = "ROOT";
 | 
|  |     18 | val theory_name = "Pure.Sessions";
 | 
|  |     19 | 
 | 
|  |     20 | local
 | 
|  |     21 | 
 | 
|  |     22 | val global =
 | 
|  |     23 |   Parse.$$$ "(" -- Parse.!!! (Parse.$$$ "global" -- Parse.$$$ ")") >> K true || Scan.succeed false;
 | 
|  |     24 | 
 | 
|  |     25 | val theory_entry = Parse.position Parse.theory_name --| global;
 | 
|  |     26 | 
 | 
|  |     27 | val theories =
 | 
|  |     28 |   Parse.$$$ "theories" |-- Parse.!!! (Scan.optional Parse.options [] -- Scan.repeat1 theory_entry);
 | 
|  |     29 | 
 | 
| 68292 |     30 | val in_path =
 | 
|  |     31 |   Parse.$$$ "(" |-- Parse.!!! (Parse.$$$ "in" |-- Parse.position Parse.path --| Parse.$$$ ")");
 | 
|  |     32 | 
 | 
| 67215 |     33 | val document_files =
 | 
|  |     34 |   Parse.$$$ "document_files" |--
 | 
| 68292 |     35 |     Parse.!!! (Scan.optional in_path ("document", Position.none)
 | 
| 67215 |     36 |       -- Scan.repeat1 (Parse.position Parse.path));
 | 
|  |     37 | 
 | 
| 68292 |     38 | val export_files =
 | 
|  |     39 |   Parse.$$$ "export_files" |--
 | 
| 68808 |     40 |     Parse.!!! (Scan.optional in_path ("export", Position.none) -- Scan.repeat1 Parse.embedded);
 | 
| 68292 |     41 | 
 | 
| 67215 |     42 | in
 | 
|  |     43 | 
 | 
|  |     44 | val command_parser =
 | 
|  |     45 |   Parse.session_name --
 | 
|  |     46 |   Scan.optional (Parse.$$$ "(" |-- Parse.!!! (Scan.repeat1 Parse.name --| Parse.$$$ ")")) [] --
 | 
|  |     47 |   Scan.optional (Parse.$$$ "in" |-- Parse.!!! (Parse.position Parse.path)) (".", Position.none) --
 | 
| 67220 |     48 |   (Parse.$$$ "=" |--
 | 
|  |     49 |     Parse.!!! (Scan.option (Parse.position Parse.session_name --| Parse.!!! (Parse.$$$ "+")) --
 | 
|  |     50 |       Scan.optional (Parse.$$$ "description" |-- Parse.!!! (Parse.input Parse.text)) Input.empty --
 | 
|  |     51 |       Scan.optional (Parse.$$$ "options" |-- Parse.!!! Parse.options) [] --
 | 
|  |     52 |       Scan.optional (Parse.$$$ "sessions" |--
 | 
|  |     53 |         Parse.!!! (Scan.repeat1 (Parse.position Parse.session_name))) [] --
 | 
|  |     54 |       Scan.repeat theories --
 | 
| 68292 |     55 |       Scan.repeat document_files --
 | 
|  |     56 |       Scan.repeat export_files))
 | 
| 67220 |     57 |   >> (fn (((session, _), dir),
 | 
| 68292 |     58 |           (((((((parent, descr), options), sessions), theories), document_files), export_files))) =>
 | 
| 67215 |     59 |     Toplevel.keep (fn state =>
 | 
|  |     60 |       let
 | 
|  |     61 |         val ctxt = Toplevel.context_of state;
 | 
|  |     62 |         val thy = Toplevel.theory_of state;
 | 
|  |     63 |         val session_dir = Resources.check_dir ctxt (Resources.master_directory thy) dir;
 | 
|  |     64 | 
 | 
|  |     65 |         val _ =
 | 
| 67220 |     66 |           (the_list parent @ sessions) |> List.app (fn arg =>
 | 
|  |     67 |             ignore (Resources.check_session ctxt arg)
 | 
|  |     68 |               handle ERROR msg => Output.error_message msg);
 | 
|  |     69 | 
 | 
|  |     70 |         val _ =
 | 
| 67215 |     71 |           Context_Position.report ctxt
 | 
|  |     72 |             (Position.range_position (Symbol_Pos.range (Input.source_explode descr)))
 | 
|  |     73 |             Markup.comment;
 | 
|  |     74 | 
 | 
|  |     75 |         val _ =
 | 
|  |     76 |           (options @ maps #1 theories) |> List.app (fn (x, y) =>
 | 
| 67220 |     77 |             ignore (Completion.check_option_value ctxt x y (Options.default ()))
 | 
|  |     78 |               handle ERROR msg => Output.error_message msg);
 | 
| 67215 |     79 | 
 | 
|  |     80 |         val _ =
 | 
|  |     81 |           maps #2 theories |> List.app (fn (s, pos) =>
 | 
|  |     82 |             let
 | 
|  |     83 |               val {node_name, theory_name, ...} =
 | 
|  |     84 |                 Resources.import_name session session_dir s
 | 
|  |     85 |                   handle ERROR msg => error (msg ^ Position.here pos);
 | 
|  |     86 |               val theory_path = the_default node_name (Resources.known_theory theory_name);
 | 
|  |     87 |               val _ = Resources.check_file ctxt Path.current (Path.implode theory_path, pos);
 | 
|  |     88 |             in () end);
 | 
|  |     89 | 
 | 
|  |     90 |         val _ =
 | 
|  |     91 |           document_files |> List.app (fn (doc_dir, doc_files) =>
 | 
|  |     92 |             let
 | 
|  |     93 |               val dir = Resources.check_dir ctxt session_dir doc_dir;
 | 
|  |     94 |               val _ = List.app (ignore o Resources.check_file ctxt dir) doc_files;
 | 
|  |     95 |             in () end);
 | 
| 68292 |     96 | 
 | 
|  |     97 |         val _ =
 | 
|  |     98 |           export_files |> List.app (fn (export_dir, _) =>
 | 
|  |     99 |             ignore (Resources.check_path ctxt session_dir export_dir));
 | 
| 67215 |    100 |       in () end));
 | 
|  |    101 | 
 | 
|  |    102 | end;
 | 
|  |    103 | 
 | 
|  |    104 | end;
 |