src/Pure/Thy/sessions.ML
author wenzelm
Mon, 06 Dec 2021 15:34:54 +0100
changeset 74887 56247fdb8bbb
parent 72841 fd8d82c4433b
child 75679 aa89255b704c
permissions -rw-r--r--
discontinued old-style {* verbatim *} tokens;
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
72841
fd8d82c4433b more accurate markup (refining 1c59b555ac4a);
wenzelm
parents: 72600
diff changeset
    22
val theory_entry = Parse.input Parse.theory_name --| Parse.opt_keyword "global";
67215
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    23
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    24
val theories =
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    25
  Parse.$$$ "theories" |-- Parse.!!! (Scan.optional Parse.options [] -- Scan.repeat1 theory_entry);
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    26
68292
7ca0c23179e6 support 'export_files' in session ROOT;
wenzelm
parents: 67220
diff changeset
    27
val in_path =
72841
fd8d82c4433b more accurate markup (refining 1c59b555ac4a);
wenzelm
parents: 72600
diff changeset
    28
  Parse.$$$ "(" |-- Parse.!!! (Parse.$$$ "in" |-- Parse.path_input --| Parse.$$$ ")");
68292
7ca0c23179e6 support 'export_files' in session ROOT;
wenzelm
parents: 67220
diff changeset
    29
72600
2fa4f25d9d07 official support for document theories from other sessions;
wenzelm
parents: 70683
diff changeset
    30
val document_theories =
72841
fd8d82c4433b more accurate markup (refining 1c59b555ac4a);
wenzelm
parents: 72600
diff changeset
    31
  Parse.$$$ "document_theories" |-- Scan.repeat1 (Parse.input Parse.theory_name);
72600
2fa4f25d9d07 official support for document theories from other sessions;
wenzelm
parents: 70683
diff changeset
    32
67215
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    33
val document_files =
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    34
  Parse.$$$ "document_files" |--
72841
fd8d82c4433b more accurate markup (refining 1c59b555ac4a);
wenzelm
parents: 72600
diff changeset
    35
    Parse.!!! (Scan.optional in_path (Input.string "document") -- Scan.repeat1 Parse.path_input);
67215
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    36
69671
2486792eaf61 support pruning of export names;
wenzelm
parents: 69349
diff changeset
    37
val prune =
2486792eaf61 support pruning of export names;
wenzelm
parents: 69349
diff changeset
    38
  Scan.optional (Parse.$$$ "[" |-- Parse.!!! (Parse.nat --| Parse.$$$ "]")) 0;
2486792eaf61 support pruning of export names;
wenzelm
parents: 69349
diff changeset
    39
68292
7ca0c23179e6 support 'export_files' in session ROOT;
wenzelm
parents: 67220
diff changeset
    40
val export_files =
7ca0c23179e6 support 'export_files' in session ROOT;
wenzelm
parents: 67220
diff changeset
    41
  Parse.$$$ "export_files" |--
72841
fd8d82c4433b more accurate markup (refining 1c59b555ac4a);
wenzelm
parents: 72600
diff changeset
    42
    Parse.!!! (Scan.optional in_path (Input.string "export") -- prune -- Scan.repeat1 Parse.embedded);
fd8d82c4433b more accurate markup (refining 1c59b555ac4a);
wenzelm
parents: 72600
diff changeset
    43
fd8d82c4433b more accurate markup (refining 1c59b555ac4a);
wenzelm
parents: 72600
diff changeset
    44
fun path_source source path =
fd8d82c4433b more accurate markup (refining 1c59b555ac4a);
wenzelm
parents: 72600
diff changeset
    45
  Input.source (Input.is_delimited source) (Path.implode path) (Input.range_of source);
68292
7ca0c23179e6 support 'export_files' in session ROOT;
wenzelm
parents: 67220
diff changeset
    46
67215
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    47
in
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    48
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    49
val command_parser =
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    50
  Parse.session_name --
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    51
  Scan.optional (Parse.$$$ "(" |-- Parse.!!! (Scan.repeat1 Parse.name --| Parse.$$$ ")")) [] --
72841
fd8d82c4433b more accurate markup (refining 1c59b555ac4a);
wenzelm
parents: 72600
diff changeset
    52
  Scan.optional (Parse.$$$ "in" |-- Parse.!!! Parse.path_input) (Input.string ".") --
67220
0049bed35f5a more markup;
wenzelm
parents: 67215
diff changeset
    53
  (Parse.$$$ "=" |--
69349
7cef9e386ffe more accurate positions for "name" (quoted string) and "embedded" (cartouche): refer to content without delimiters, which is e.g. relevant for systematic selection/renaming of scope groups;
wenzelm
parents: 68808
diff changeset
    54
    Parse.!!! (Scan.option (Parse.session_name --| Parse.!!! (Parse.$$$ "+")) --
74887
56247fdb8bbb discontinued old-style {* verbatim *} tokens;
wenzelm
parents: 72841
diff changeset
    55
      Scan.optional (Parse.$$$ "description" |-- Parse.!!! (Parse.input Parse.embedded)) Input.empty --
67220
0049bed35f5a more markup;
wenzelm
parents: 67215
diff changeset
    56
      Scan.optional (Parse.$$$ "options" |-- Parse.!!! Parse.options) [] --
0049bed35f5a more markup;
wenzelm
parents: 67215
diff changeset
    57
      Scan.optional (Parse.$$$ "sessions" |--
69349
7cef9e386ffe more accurate positions for "name" (quoted string) and "embedded" (cartouche): refer to content without delimiters, which is e.g. relevant for systematic selection/renaming of scope groups;
wenzelm
parents: 68808
diff changeset
    58
        Parse.!!! (Scan.repeat1 Parse.session_name)) [] --
72841
fd8d82c4433b more accurate markup (refining 1c59b555ac4a);
wenzelm
parents: 72600
diff changeset
    59
      Scan.optional (Parse.$$$ "directories" |-- Parse.!!! (Scan.repeat1 Parse.path_input)) [] --
67220
0049bed35f5a more markup;
wenzelm
parents: 67215
diff changeset
    60
      Scan.repeat theories --
72600
2fa4f25d9d07 official support for document theories from other sessions;
wenzelm
parents: 70683
diff changeset
    61
      Scan.optional document_theories [] --
68292
7ca0c23179e6 support 'export_files' in session ROOT;
wenzelm
parents: 67220
diff changeset
    62
      Scan.repeat document_files --
7ca0c23179e6 support 'export_files' in session ROOT;
wenzelm
parents: 67220
diff changeset
    63
      Scan.repeat export_files))
72600
2fa4f25d9d07 official support for document theories from other sessions;
wenzelm
parents: 70683
diff changeset
    64
  >> (fn (((((session, _), _), dir),
2fa4f25d9d07 official support for document theories from other sessions;
wenzelm
parents: 70683
diff changeset
    65
          (((((((((parent, descr), options), sessions), directories), theories),
2fa4f25d9d07 official support for document theories from other sessions;
wenzelm
parents: 70683
diff changeset
    66
            document_theories), document_files), export_files)))) =>
67215
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    67
    Toplevel.keep (fn state =>
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    68
      let
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    69
        val ctxt = Toplevel.context_of state;
70049
c1226e4c273e tuned signature;
wenzelm
parents: 69671
diff changeset
    70
        val session_dir = Resources.check_dir ctxt NONE dir;
67215
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    71
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    72
        val _ =
67220
0049bed35f5a more markup;
wenzelm
parents: 67215
diff changeset
    73
          (the_list parent @ sessions) |> List.app (fn arg =>
0049bed35f5a more markup;
wenzelm
parents: 67215
diff changeset
    74
            ignore (Resources.check_session ctxt arg)
0049bed35f5a more markup;
wenzelm
parents: 67215
diff changeset
    75
              handle ERROR msg => Output.error_message msg);
0049bed35f5a more markup;
wenzelm
parents: 67215
diff changeset
    76
0049bed35f5a more markup;
wenzelm
parents: 67215
diff changeset
    77
        val _ =
67215
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    78
          Context_Position.report ctxt
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    79
            (Position.range_position (Symbol_Pos.range (Input.source_explode descr)))
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    80
            Markup.comment;
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    81
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    82
        val _ =
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    83
          (options @ maps #1 theories) |> List.app (fn (x, y) =>
67220
0049bed35f5a more markup;
wenzelm
parents: 67215
diff changeset
    84
            ignore (Completion.check_option_value ctxt x y (Options.default ()))
0049bed35f5a more markup;
wenzelm
parents: 67215
diff changeset
    85
              handle ERROR msg => Output.error_message msg);
67215
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    86
72841
fd8d82c4433b more accurate markup (refining 1c59b555ac4a);
wenzelm
parents: 72600
diff changeset
    87
        fun check_thy source =
fd8d82c4433b more accurate markup (refining 1c59b555ac4a);
wenzelm
parents: 72600
diff changeset
    88
          ignore (Resources.check_file ctxt (SOME Path.current) source)
72600
2fa4f25d9d07 official support for document theories from other sessions;
wenzelm
parents: 70683
diff changeset
    89
            handle ERROR msg => Output.error_message msg;
2fa4f25d9d07 official support for document theories from other sessions;
wenzelm
parents: 70683
diff changeset
    90
67215
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    91
        val _ =
72841
fd8d82c4433b more accurate markup (refining 1c59b555ac4a);
wenzelm
parents: 72600
diff changeset
    92
          maps #2 theories |> List.app (fn source =>
67215
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    93
            let
72841
fd8d82c4433b more accurate markup (refining 1c59b555ac4a);
wenzelm
parents: 72600
diff changeset
    94
              val s = Input.string_of source;
fd8d82c4433b more accurate markup (refining 1c59b555ac4a);
wenzelm
parents: 72600
diff changeset
    95
              val pos = Input.pos_of source;
67215
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    96
              val {node_name, theory_name, ...} =
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    97
                Resources.import_name session session_dir s
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    98
                  handle ERROR msg => error (msg ^ Position.here pos);
72841
fd8d82c4433b more accurate markup (refining 1c59b555ac4a);
wenzelm
parents: 72600
diff changeset
    99
              val thy_path = the_default node_name (Resources.find_theory_file theory_name);
fd8d82c4433b more accurate markup (refining 1c59b555ac4a);
wenzelm
parents: 72600
diff changeset
   100
            in check_thy (path_source source thy_path) end);
67215
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
   101
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
   102
        val _ =
70681
a6c0f2d106c8 disallow overlapping session directories;
wenzelm
parents: 70678
diff changeset
   103
          directories |> List.app (ignore o Resources.check_dir ctxt (SOME session_dir));
70668
9cac4dec0da9 support for explicit session directories;
wenzelm
parents: 70049
diff changeset
   104
9cac4dec0da9 support for explicit session directories;
wenzelm
parents: 70049
diff changeset
   105
        val _ =
72841
fd8d82c4433b more accurate markup (refining 1c59b555ac4a);
wenzelm
parents: 72600
diff changeset
   106
          document_theories |> List.app (fn source =>
fd8d82c4433b more accurate markup (refining 1c59b555ac4a);
wenzelm
parents: 72600
diff changeset
   107
            let
fd8d82c4433b more accurate markup (refining 1c59b555ac4a);
wenzelm
parents: 72600
diff changeset
   108
              val thy = Input.string_of source;
fd8d82c4433b more accurate markup (refining 1c59b555ac4a);
wenzelm
parents: 72600
diff changeset
   109
              val pos = Input.pos_of source;
fd8d82c4433b more accurate markup (refining 1c59b555ac4a);
wenzelm
parents: 72600
diff changeset
   110
            in
fd8d82c4433b more accurate markup (refining 1c59b555ac4a);
wenzelm
parents: 72600
diff changeset
   111
              (case Resources.find_theory_file thy of
fd8d82c4433b more accurate markup (refining 1c59b555ac4a);
wenzelm
parents: 72600
diff changeset
   112
                NONE => Output.error_message ("Unknown theory " ^ quote thy ^ Position.here pos)
fd8d82c4433b more accurate markup (refining 1c59b555ac4a);
wenzelm
parents: 72600
diff changeset
   113
              | SOME path => check_thy (path_source source path))
fd8d82c4433b more accurate markup (refining 1c59b555ac4a);
wenzelm
parents: 72600
diff changeset
   114
            end);
72600
2fa4f25d9d07 official support for document theories from other sessions;
wenzelm
parents: 70683
diff changeset
   115
2fa4f25d9d07 official support for document theories from other sessions;
wenzelm
parents: 70683
diff changeset
   116
        val _ =
67215
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
   117
          document_files |> List.app (fn (doc_dir, doc_files) =>
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
   118
            let
70049
c1226e4c273e tuned signature;
wenzelm
parents: 69671
diff changeset
   119
              val dir = Resources.check_dir ctxt (SOME session_dir) doc_dir;
c1226e4c273e tuned signature;
wenzelm
parents: 69671
diff changeset
   120
              val _ = List.app (ignore o Resources.check_file ctxt (SOME dir)) doc_files;
67215
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
   121
            in () end);
68292
7ca0c23179e6 support 'export_files' in session ROOT;
wenzelm
parents: 67220
diff changeset
   122
7ca0c23179e6 support 'export_files' in session ROOT;
wenzelm
parents: 67220
diff changeset
   123
        val _ =
69671
2486792eaf61 support pruning of export names;
wenzelm
parents: 69349
diff changeset
   124
          export_files |> List.app (fn ((export_dir, _), _) =>
70049
c1226e4c273e tuned signature;
wenzelm
parents: 69671
diff changeset
   125
            ignore (Resources.check_path ctxt (SOME session_dir) export_dir));
67215
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
   126
      in () end));
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
   127
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
   128
end;
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
   129
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
   130
end;