src/Pure/Thy/sessions.ML
author wenzelm
Sat, 10 Dec 2022 20:31:47 +0100
changeset 76614 ac08b6e3b9e3
parent 76005 a9bbf075f431
permissions -rw-r--r--
clarified signature;
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
75986
27d98da31985 support 'chapter_definition' with description for presentation purposes;
wenzelm
parents: 75679
diff changeset
    11
  val chapter_definition_parser: (Toplevel.transition -> Toplevel.transition) parser
27d98da31985 support 'chapter_definition' with description for presentation purposes;
wenzelm
parents: 75679
diff changeset
    12
  val session_parser: (Toplevel.transition -> Toplevel.transition) parser
67215
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    13
end;
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    14
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    15
structure Sessions: SESSIONS =
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    16
struct
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    17
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    18
val root_name = "ROOT";
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    19
val theory_name = "Pure.Sessions";
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    20
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    21
local
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    22
76005
a9bbf075f431 include groups from 'chapter_definition' in session info, based on the state of chapter_defs after processing all ROOT files (thus the declaration order does not matter);
wenzelm
parents: 75998
diff changeset
    23
val groups =
a9bbf075f431 include groups from 'chapter_definition' in session info, based on the state of chapter_defs after processing all ROOT files (thus the declaration order does not matter);
wenzelm
parents: 75998
diff changeset
    24
  Scan.optional (Parse.$$$ "(" |-- Parse.!!! (Scan.repeat1 Parse.name --| Parse.$$$ ")")) [];
a9bbf075f431 include groups from 'chapter_definition' in session info, based on the state of chapter_defs after processing all ROOT files (thus the declaration order does not matter);
wenzelm
parents: 75998
diff changeset
    25
75998
c36e5c6f3069 clarified syntax: more uniform;
wenzelm
parents: 75986
diff changeset
    26
val description =
c36e5c6f3069 clarified syntax: more uniform;
wenzelm
parents: 75986
diff changeset
    27
  Scan.optional (Parse.$$$ "description" |-- Parse.!!! (Parse.input Parse.embedded)) Input.empty;
c36e5c6f3069 clarified syntax: more uniform;
wenzelm
parents: 75986
diff changeset
    28
72841
fd8d82c4433b more accurate markup (refining 1c59b555ac4a);
wenzelm
parents: 72600
diff changeset
    29
val theory_entry = Parse.input Parse.theory_name --| Parse.opt_keyword "global";
67215
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    30
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    31
val theories =
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    32
  Parse.$$$ "theories" |-- Parse.!!! (Scan.optional Parse.options [] -- Scan.repeat1 theory_entry);
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    33
72600
2fa4f25d9d07 official support for document theories from other sessions;
wenzelm
parents: 70683
diff changeset
    34
val document_theories =
72841
fd8d82c4433b more accurate markup (refining 1c59b555ac4a);
wenzelm
parents: 72600
diff changeset
    35
  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
    36
67215
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    37
val document_files =
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    38
  Parse.$$$ "document_files" |--
76614
ac08b6e3b9e3 clarified signature;
wenzelm
parents: 76005
diff changeset
    39
    Parse.!!! (Parse.in_path_parens "document" -- Scan.repeat1 Parse.path_input);
67215
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    40
69671
2486792eaf61 support pruning of export names;
wenzelm
parents: 69349
diff changeset
    41
val prune =
2486792eaf61 support pruning of export names;
wenzelm
parents: 69349
diff changeset
    42
  Scan.optional (Parse.$$$ "[" |-- Parse.!!! (Parse.nat --| Parse.$$$ "]")) 0;
2486792eaf61 support pruning of export names;
wenzelm
parents: 69349
diff changeset
    43
68292
7ca0c23179e6 support 'export_files' in session ROOT;
wenzelm
parents: 67220
diff changeset
    44
val export_files =
7ca0c23179e6 support 'export_files' in session ROOT;
wenzelm
parents: 67220
diff changeset
    45
  Parse.$$$ "export_files" |--
76614
ac08b6e3b9e3 clarified signature;
wenzelm
parents: 76005
diff changeset
    46
    Parse.!!! (Parse.in_path_parens "export" -- prune -- Scan.repeat1 Parse.embedded);
72841
fd8d82c4433b more accurate markup (refining 1c59b555ac4a);
wenzelm
parents: 72600
diff changeset
    47
75679
aa89255b704c support for classpath artifacts within session structure:
wenzelm
parents: 74887
diff changeset
    48
val export_classpath =
aa89255b704c support for classpath artifacts within session structure:
wenzelm
parents: 74887
diff changeset
    49
  Parse.$$$ "export_classpath" |-- Scan.repeat Parse.embedded;
aa89255b704c support for classpath artifacts within session structure:
wenzelm
parents: 74887
diff changeset
    50
72841
fd8d82c4433b more accurate markup (refining 1c59b555ac4a);
wenzelm
parents: 72600
diff changeset
    51
fun path_source source path =
fd8d82c4433b more accurate markup (refining 1c59b555ac4a);
wenzelm
parents: 72600
diff changeset
    52
  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
    53
67215
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    54
in
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    55
75986
27d98da31985 support 'chapter_definition' with description for presentation purposes;
wenzelm
parents: 75679
diff changeset
    56
val chapter_definition_parser =
76005
a9bbf075f431 include groups from 'chapter_definition' in session info, based on the state of chapter_defs after processing all ROOT files (thus the declaration order does not matter);
wenzelm
parents: 75998
diff changeset
    57
  Parse.chapter_name -- groups -- description >> (fn (_, descr) =>
75998
c36e5c6f3069 clarified syntax: more uniform;
wenzelm
parents: 75986
diff changeset
    58
    Toplevel.keep (fn state =>
c36e5c6f3069 clarified syntax: more uniform;
wenzelm
parents: 75986
diff changeset
    59
      let
c36e5c6f3069 clarified syntax: more uniform;
wenzelm
parents: 75986
diff changeset
    60
        val ctxt = Toplevel.context_of state;
c36e5c6f3069 clarified syntax: more uniform;
wenzelm
parents: 75986
diff changeset
    61
        val _ =
c36e5c6f3069 clarified syntax: more uniform;
wenzelm
parents: 75986
diff changeset
    62
          Context_Position.report ctxt
c36e5c6f3069 clarified syntax: more uniform;
wenzelm
parents: 75986
diff changeset
    63
            (Position.range_position (Symbol_Pos.range (Input.source_explode descr)))
c36e5c6f3069 clarified syntax: more uniform;
wenzelm
parents: 75986
diff changeset
    64
            Markup.comment;
c36e5c6f3069 clarified syntax: more uniform;
wenzelm
parents: 75986
diff changeset
    65
      in () end));
75986
27d98da31985 support 'chapter_definition' with description for presentation purposes;
wenzelm
parents: 75679
diff changeset
    66
27d98da31985 support 'chapter_definition' with description for presentation purposes;
wenzelm
parents: 75679
diff changeset
    67
val session_parser =
76614
ac08b6e3b9e3 clarified signature;
wenzelm
parents: 76005
diff changeset
    68
  Parse.session_name -- groups -- Parse.in_path "." --
67220
0049bed35f5a more markup;
wenzelm
parents: 67215
diff changeset
    69
  (Parse.$$$ "=" |--
75998
c36e5c6f3069 clarified syntax: more uniform;
wenzelm
parents: 75986
diff changeset
    70
    Parse.!!! (Scan.option (Parse.session_name --| Parse.!!! (Parse.$$$ "+")) -- description --
67220
0049bed35f5a more markup;
wenzelm
parents: 67215
diff changeset
    71
      Scan.optional (Parse.$$$ "options" |-- Parse.!!! Parse.options) [] --
0049bed35f5a more markup;
wenzelm
parents: 67215
diff changeset
    72
      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
    73
        Parse.!!! (Scan.repeat1 Parse.session_name)) [] --
72841
fd8d82c4433b more accurate markup (refining 1c59b555ac4a);
wenzelm
parents: 72600
diff changeset
    74
      Scan.optional (Parse.$$$ "directories" |-- Parse.!!! (Scan.repeat1 Parse.path_input)) [] --
67220
0049bed35f5a more markup;
wenzelm
parents: 67215
diff changeset
    75
      Scan.repeat theories --
72600
2fa4f25d9d07 official support for document theories from other sessions;
wenzelm
parents: 70683
diff changeset
    76
      Scan.optional document_theories [] --
68292
7ca0c23179e6 support 'export_files' in session ROOT;
wenzelm
parents: 67220
diff changeset
    77
      Scan.repeat document_files --
75679
aa89255b704c support for classpath artifacts within session structure:
wenzelm
parents: 74887
diff changeset
    78
      Scan.repeat export_files --
aa89255b704c support for classpath artifacts within session structure:
wenzelm
parents: 74887
diff changeset
    79
      Scan.optional export_classpath []))
72600
2fa4f25d9d07 official support for document theories from other sessions;
wenzelm
parents: 70683
diff changeset
    80
  >> (fn (((((session, _), _), dir),
75679
aa89255b704c support for classpath artifacts within session structure:
wenzelm
parents: 74887
diff changeset
    81
          ((((((((((parent, descr), options), sessions), directories), theories),
aa89255b704c support for classpath artifacts within session structure:
wenzelm
parents: 74887
diff changeset
    82
            document_theories), document_files), export_files), _)))) =>
67215
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    83
    Toplevel.keep (fn state =>
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    84
      let
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    85
        val ctxt = Toplevel.context_of state;
70049
c1226e4c273e tuned signature;
wenzelm
parents: 69671
diff changeset
    86
        val session_dir = Resources.check_dir ctxt NONE dir;
67215
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    87
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    88
        val _ =
67220
0049bed35f5a more markup;
wenzelm
parents: 67215
diff changeset
    89
          (the_list parent @ sessions) |> List.app (fn arg =>
0049bed35f5a more markup;
wenzelm
parents: 67215
diff changeset
    90
            ignore (Resources.check_session ctxt arg)
0049bed35f5a more markup;
wenzelm
parents: 67215
diff changeset
    91
              handle ERROR msg => Output.error_message msg);
0049bed35f5a more markup;
wenzelm
parents: 67215
diff changeset
    92
0049bed35f5a more markup;
wenzelm
parents: 67215
diff changeset
    93
        val _ =
67215
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    94
          Context_Position.report ctxt
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    95
            (Position.range_position (Symbol_Pos.range (Input.source_explode descr)))
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    96
            Markup.comment;
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    97
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    98
        val _ =
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    99
          (options @ maps #1 theories) |> List.app (fn (x, y) =>
67220
0049bed35f5a more markup;
wenzelm
parents: 67215
diff changeset
   100
            ignore (Completion.check_option_value ctxt x y (Options.default ()))
0049bed35f5a more markup;
wenzelm
parents: 67215
diff changeset
   101
              handle ERROR msg => Output.error_message msg);
67215
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
   102
72841
fd8d82c4433b more accurate markup (refining 1c59b555ac4a);
wenzelm
parents: 72600
diff changeset
   103
        fun check_thy source =
fd8d82c4433b more accurate markup (refining 1c59b555ac4a);
wenzelm
parents: 72600
diff changeset
   104
          ignore (Resources.check_file ctxt (SOME Path.current) source)
72600
2fa4f25d9d07 official support for document theories from other sessions;
wenzelm
parents: 70683
diff changeset
   105
            handle ERROR msg => Output.error_message msg;
2fa4f25d9d07 official support for document theories from other sessions;
wenzelm
parents: 70683
diff changeset
   106
67215
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
   107
        val _ =
72841
fd8d82c4433b more accurate markup (refining 1c59b555ac4a);
wenzelm
parents: 72600
diff changeset
   108
          maps #2 theories |> List.app (fn source =>
67215
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
   109
            let
72841
fd8d82c4433b more accurate markup (refining 1c59b555ac4a);
wenzelm
parents: 72600
diff changeset
   110
              val s = Input.string_of source;
fd8d82c4433b more accurate markup (refining 1c59b555ac4a);
wenzelm
parents: 72600
diff changeset
   111
              val pos = Input.pos_of source;
67215
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
   112
              val {node_name, theory_name, ...} =
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
   113
                Resources.import_name session session_dir s
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
   114
                  handle ERROR msg => error (msg ^ Position.here pos);
72841
fd8d82c4433b more accurate markup (refining 1c59b555ac4a);
wenzelm
parents: 72600
diff changeset
   115
              val thy_path = the_default node_name (Resources.find_theory_file theory_name);
fd8d82c4433b more accurate markup (refining 1c59b555ac4a);
wenzelm
parents: 72600
diff changeset
   116
            in check_thy (path_source source thy_path) end);
67215
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
   117
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
   118
        val _ =
70681
a6c0f2d106c8 disallow overlapping session directories;
wenzelm
parents: 70678
diff changeset
   119
          directories |> List.app (ignore o Resources.check_dir ctxt (SOME session_dir));
70668
9cac4dec0da9 support for explicit session directories;
wenzelm
parents: 70049
diff changeset
   120
9cac4dec0da9 support for explicit session directories;
wenzelm
parents: 70049
diff changeset
   121
        val _ =
72841
fd8d82c4433b more accurate markup (refining 1c59b555ac4a);
wenzelm
parents: 72600
diff changeset
   122
          document_theories |> List.app (fn source =>
fd8d82c4433b more accurate markup (refining 1c59b555ac4a);
wenzelm
parents: 72600
diff changeset
   123
            let
fd8d82c4433b more accurate markup (refining 1c59b555ac4a);
wenzelm
parents: 72600
diff changeset
   124
              val thy = Input.string_of source;
fd8d82c4433b more accurate markup (refining 1c59b555ac4a);
wenzelm
parents: 72600
diff changeset
   125
              val pos = Input.pos_of source;
fd8d82c4433b more accurate markup (refining 1c59b555ac4a);
wenzelm
parents: 72600
diff changeset
   126
            in
fd8d82c4433b more accurate markup (refining 1c59b555ac4a);
wenzelm
parents: 72600
diff changeset
   127
              (case Resources.find_theory_file thy of
fd8d82c4433b more accurate markup (refining 1c59b555ac4a);
wenzelm
parents: 72600
diff changeset
   128
                NONE => Output.error_message ("Unknown theory " ^ quote thy ^ Position.here pos)
fd8d82c4433b more accurate markup (refining 1c59b555ac4a);
wenzelm
parents: 72600
diff changeset
   129
              | SOME path => check_thy (path_source source path))
fd8d82c4433b more accurate markup (refining 1c59b555ac4a);
wenzelm
parents: 72600
diff changeset
   130
            end);
72600
2fa4f25d9d07 official support for document theories from other sessions;
wenzelm
parents: 70683
diff changeset
   131
2fa4f25d9d07 official support for document theories from other sessions;
wenzelm
parents: 70683
diff changeset
   132
        val _ =
67215
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
   133
          document_files |> List.app (fn (doc_dir, doc_files) =>
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
   134
            let
70049
c1226e4c273e tuned signature;
wenzelm
parents: 69671
diff changeset
   135
              val dir = Resources.check_dir ctxt (SOME session_dir) doc_dir;
c1226e4c273e tuned signature;
wenzelm
parents: 69671
diff changeset
   136
              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
   137
            in () end);
68292
7ca0c23179e6 support 'export_files' in session ROOT;
wenzelm
parents: 67220
diff changeset
   138
7ca0c23179e6 support 'export_files' in session ROOT;
wenzelm
parents: 67220
diff changeset
   139
        val _ =
69671
2486792eaf61 support pruning of export names;
wenzelm
parents: 69349
diff changeset
   140
          export_files |> List.app (fn ((export_dir, _), _) =>
70049
c1226e4c273e tuned signature;
wenzelm
parents: 69671
diff changeset
   141
            ignore (Resources.check_path ctxt (SOME session_dir) export_dir));
67215
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
   142
      in () end));
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
   143
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
   144
end;
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
   145
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
   146
end;