src/Pure/Thy/sessions.ML
author wenzelm
Sat, 16 Dec 2017 16:46:01 +0100
changeset 67215 03d0c958d65a
child 67220 0049bed35f5a
permissions -rw-r--r--
PIDE markup for session ROOT files;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
67215
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Thy/sessions.ML
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
     3
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
     4
Support for session ROOT syntax.
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
     5
*)
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
     6
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
     7
signature SESSIONS =
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
     8
sig
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
     9
  val root_name: string
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    10
  val theory_name: string
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    11
  val command_parser: (Toplevel.transition -> Toplevel.transition) parser
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    12
end;
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    13
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    14
structure Sessions: SESSIONS =
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    15
struct
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    16
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    17
val root_name = "ROOT";
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    18
val theory_name = "Pure.Sessions";
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    19
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    20
local
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    21
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    22
val global =
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    23
  Parse.$$$ "(" -- Parse.!!! (Parse.$$$ "global" -- Parse.$$$ ")") >> K true || Scan.succeed false;
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    24
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    25
val theory_entry = Parse.position Parse.theory_name --| global;
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    26
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    27
val theories =
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    28
  Parse.$$$ "theories" |-- Parse.!!! (Scan.optional Parse.options [] -- Scan.repeat1 theory_entry);
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    29
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    30
val document_files =
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    31
  Parse.$$$ "document_files" |--
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    32
    Parse.!!!
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    33
      (Scan.optional
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    34
        (Parse.$$$ "(" |--
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    35
            Parse.!!! (Parse.$$$ "in" |-- Parse.position Parse.path --| Parse.$$$ ")"))
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    36
        ("document", Position.none)
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    37
      -- Scan.repeat1 (Parse.position Parse.path));
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    38
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    39
in
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    40
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    41
val command_parser =
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    42
  Parse.session_name --
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    43
  Scan.optional (Parse.$$$ "(" |-- Parse.!!! (Scan.repeat1 Parse.name --| Parse.$$$ ")")) [] --
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    44
  Scan.optional (Parse.$$$ "in" |-- Parse.!!! (Parse.position Parse.path)) (".", Position.none) --
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    45
  (Parse.$$$ "=" |-- Parse.!!! (Scan.option (Parse.session_name --| Parse.!!! (Parse.$$$ "+")) --
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    46
    Scan.optional (Parse.$$$ "description" |-- Parse.!!! (Parse.input Parse.text)) Input.empty --
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    47
    Scan.optional (Parse.$$$ "options" |-- Parse.!!! Parse.options) [] --
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    48
    Scan.optional (Parse.$$$ "sessions" |-- Parse.!!! (Scan.repeat1 Parse.session_name)) [] --
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    49
    Scan.repeat theories --
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    50
    Scan.repeat document_files))
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    51
  >> (fn (((session, _), dir), ((((((_, descr), options), _), theories), document_files))) =>
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    52
    Toplevel.keep (fn state =>
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    53
      let
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    54
        val ctxt = Toplevel.context_of state;
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    55
        val thy = Toplevel.theory_of state;
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    56
        val session_dir = Resources.check_dir ctxt (Resources.master_directory thy) dir;
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    57
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    58
        val _ =
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    59
          Context_Position.report ctxt
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    60
            (Position.range_position (Symbol_Pos.range (Input.source_explode descr)))
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    61
            Markup.comment;
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    62
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    63
        val _ =
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    64
          (options @ maps #1 theories) |> List.app (fn (x, y) =>
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    65
            ignore (Completion.check_option_value ctxt x y (Options.default ())));
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    66
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    67
        val _ =
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    68
          maps #2 theories |> List.app (fn (s, pos) =>
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    69
            let
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    70
              val {node_name, theory_name, ...} =
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    71
                Resources.import_name session session_dir s
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    72
                  handle ERROR msg => error (msg ^ Position.here pos);
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    73
              val theory_path = the_default node_name (Resources.known_theory theory_name);
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    74
              val _ = Resources.check_file ctxt Path.current (Path.implode theory_path, pos);
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    75
            in () end);
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    76
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    77
        val _ =
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    78
          document_files |> List.app (fn (doc_dir, doc_files) =>
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    79
            let
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    80
              val dir = Resources.check_dir ctxt session_dir doc_dir;
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    81
              val _ = List.app (ignore o Resources.check_file ctxt dir) doc_files;
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    82
            in () end);
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    83
      in () end));
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    84
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    85
end;
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    86
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    87
end;