| 
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  | 
  | 
| 
 | 
    30  | 
val document_files =
  | 
| 
 | 
    31  | 
  Parse.$$$ "document_files" |--
  | 
| 
 | 
    32  | 
    Parse.!!!
  | 
| 
 | 
    33  | 
      (Scan.optional
  | 
| 
 | 
    34  | 
        (Parse.$$$ "(" |--
 | 
| 
 | 
    35  | 
            Parse.!!! (Parse.$$$ "in" |-- Parse.position Parse.path --| Parse.$$$ ")"))
  | 
| 
 | 
    36  | 
        ("document", Position.none)
 | 
| 
 | 
    37  | 
      -- Scan.repeat1 (Parse.position Parse.path));
  | 
| 
 | 
    38  | 
  | 
| 
 | 
    39  | 
in
  | 
| 
 | 
    40  | 
  | 
| 
 | 
    41  | 
val command_parser =
  | 
| 
 | 
    42  | 
  Parse.session_name --
  | 
| 
 | 
    43  | 
  Scan.optional (Parse.$$$ "(" |-- Parse.!!! (Scan.repeat1 Parse.name --| Parse.$$$ ")")) [] --
 | 
| 
 | 
    44  | 
  Scan.optional (Parse.$$$ "in" |-- Parse.!!! (Parse.position Parse.path)) (".", Position.none) --
 | 
| 
67220
 | 
    45  | 
  (Parse.$$$ "=" |--
  | 
| 
 | 
    46  | 
    Parse.!!! (Scan.option (Parse.position Parse.session_name --| Parse.!!! (Parse.$$$ "+")) --
  | 
| 
 | 
    47  | 
      Scan.optional (Parse.$$$ "description" |-- Parse.!!! (Parse.input Parse.text)) Input.empty --
  | 
| 
 | 
    48  | 
      Scan.optional (Parse.$$$ "options" |-- Parse.!!! Parse.options) [] --
  | 
| 
 | 
    49  | 
      Scan.optional (Parse.$$$ "sessions" |--
  | 
| 
 | 
    50  | 
        Parse.!!! (Scan.repeat1 (Parse.position Parse.session_name))) [] --
  | 
| 
 | 
    51  | 
      Scan.repeat theories --
  | 
| 
 | 
    52  | 
      Scan.repeat document_files))
  | 
| 
 | 
    53  | 
  >> (fn (((session, _), dir),
  | 
| 
 | 
    54  | 
          ((((((parent, descr), options), sessions), theories), document_files))) =>
  | 
| 
67215
 | 
    55  | 
    Toplevel.keep (fn state =>
  | 
| 
 | 
    56  | 
      let
  | 
| 
 | 
    57  | 
        val ctxt = Toplevel.context_of state;
  | 
| 
 | 
    58  | 
        val thy = Toplevel.theory_of state;
  | 
| 
 | 
    59  | 
        val session_dir = Resources.check_dir ctxt (Resources.master_directory thy) dir;
  | 
| 
 | 
    60  | 
  | 
| 
 | 
    61  | 
        val _ =
  | 
| 
67220
 | 
    62  | 
          (the_list parent @ sessions) |> List.app (fn arg =>
  | 
| 
 | 
    63  | 
            ignore (Resources.check_session ctxt arg)
  | 
| 
 | 
    64  | 
              handle ERROR msg => Output.error_message msg);
  | 
| 
 | 
    65  | 
  | 
| 
 | 
    66  | 
        val _ =
  | 
| 
67215
 | 
    67  | 
          Context_Position.report ctxt
  | 
| 
 | 
    68  | 
            (Position.range_position (Symbol_Pos.range (Input.source_explode descr)))
  | 
| 
 | 
    69  | 
            Markup.comment;
  | 
| 
 | 
    70  | 
  | 
| 
 | 
    71  | 
        val _ =
  | 
| 
 | 
    72  | 
          (options @ maps #1 theories) |> List.app (fn (x, y) =>
  | 
| 
67220
 | 
    73  | 
            ignore (Completion.check_option_value ctxt x y (Options.default ()))
  | 
| 
 | 
    74  | 
              handle ERROR msg => Output.error_message msg);
  | 
| 
67215
 | 
    75  | 
  | 
| 
 | 
    76  | 
        val _ =
  | 
| 
 | 
    77  | 
          maps #2 theories |> List.app (fn (s, pos) =>
  | 
| 
 | 
    78  | 
            let
  | 
| 
 | 
    79  | 
              val {node_name, theory_name, ...} =
 | 
| 
 | 
    80  | 
                Resources.import_name session session_dir s
  | 
| 
 | 
    81  | 
                  handle ERROR msg => error (msg ^ Position.here pos);
  | 
| 
 | 
    82  | 
              val theory_path = the_default node_name (Resources.known_theory theory_name);
  | 
| 
 | 
    83  | 
              val _ = Resources.check_file ctxt Path.current (Path.implode theory_path, pos);
  | 
| 
 | 
    84  | 
            in () end);
  | 
| 
 | 
    85  | 
  | 
| 
 | 
    86  | 
        val _ =
  | 
| 
 | 
    87  | 
          document_files |> List.app (fn (doc_dir, doc_files) =>
  | 
| 
 | 
    88  | 
            let
  | 
| 
 | 
    89  | 
              val dir = Resources.check_dir ctxt session_dir doc_dir;
  | 
| 
 | 
    90  | 
              val _ = List.app (ignore o Resources.check_file ctxt dir) doc_files;
  | 
| 
 | 
    91  | 
            in () end);
  | 
| 
 | 
    92  | 
      in () end));
  | 
| 
 | 
    93  | 
  | 
| 
 | 
    94  | 
end;
  | 
| 
 | 
    95  | 
  | 
| 
 | 
    96  | 
end;
  |