src/Pure/Thy/sessions.ML
author wenzelm
Sun, 08 Sep 2019 17:49:35 +0200
changeset 70678 36c8c32346cb
parent 70668 9cac4dec0da9
child 70681 a6c0f2d106c8
permissions -rw-r--r--
clarified syntax: 'directories' and 'theories' belong together;
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
70668
9cac4dec0da9 support for explicit session directories;
wenzelm
parents: 70049
diff changeset
    22
val directory = Parse.position Parse.path -- Parse.opt_keyword "overlapping";
67215
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    23
70668
9cac4dec0da9 support for explicit session directories;
wenzelm
parents: 70049
diff changeset
    24
val theory_entry = Parse.theory_name --| Parse.opt_keyword "global";
67215
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    25
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    26
val theories =
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    27
  Parse.$$$ "theories" |-- Parse.!!! (Scan.optional Parse.options [] -- Scan.repeat1 theory_entry);
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    28
68292
7ca0c23179e6 support 'export_files' in session ROOT;
wenzelm
parents: 67220
diff changeset
    29
val in_path =
7ca0c23179e6 support 'export_files' in session ROOT;
wenzelm
parents: 67220
diff changeset
    30
  Parse.$$$ "(" |-- Parse.!!! (Parse.$$$ "in" |-- Parse.position Parse.path --| Parse.$$$ ")");
7ca0c23179e6 support 'export_files' in session ROOT;
wenzelm
parents: 67220
diff changeset
    31
67215
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    32
val document_files =
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    33
  Parse.$$$ "document_files" |--
68292
7ca0c23179e6 support 'export_files' in session ROOT;
wenzelm
parents: 67220
diff changeset
    34
    Parse.!!! (Scan.optional in_path ("document", Position.none)
67215
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    35
      -- Scan.repeat1 (Parse.position Parse.path));
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" |--
69671
2486792eaf61 support pruning of export names;
wenzelm
parents: 69349
diff changeset
    42
    Parse.!!!
2486792eaf61 support pruning of export names;
wenzelm
parents: 69349
diff changeset
    43
      (Scan.optional in_path ("export", Position.none) -- prune -- Scan.repeat1 Parse.embedded);
68292
7ca0c23179e6 support 'export_files' in session ROOT;
wenzelm
parents: 67220
diff changeset
    44
67215
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    45
in
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    46
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    47
val command_parser =
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    48
  Parse.session_name --
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    49
  Scan.optional (Parse.$$$ "(" |-- Parse.!!! (Scan.repeat1 Parse.name --| Parse.$$$ ")")) [] --
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    50
  Scan.optional (Parse.$$$ "in" |-- Parse.!!! (Parse.position Parse.path)) (".", Position.none) --
67220
0049bed35f5a more markup;
wenzelm
parents: 67215
diff changeset
    51
  (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
    52
    Parse.!!! (Scan.option (Parse.session_name --| Parse.!!! (Parse.$$$ "+")) --
67220
0049bed35f5a more markup;
wenzelm
parents: 67215
diff changeset
    53
      Scan.optional (Parse.$$$ "description" |-- Parse.!!! (Parse.input Parse.text)) Input.empty --
0049bed35f5a more markup;
wenzelm
parents: 67215
diff changeset
    54
      Scan.optional (Parse.$$$ "options" |-- Parse.!!! Parse.options) [] --
0049bed35f5a more markup;
wenzelm
parents: 67215
diff changeset
    55
      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
    56
        Parse.!!! (Scan.repeat1 Parse.session_name)) [] --
70678
36c8c32346cb clarified syntax: 'directories' and 'theories' belong together;
wenzelm
parents: 70668
diff changeset
    57
      Scan.optional (Parse.$$$ "directories" |-- Parse.!!! (Scan.repeat1 directory)) [] --
67220
0049bed35f5a more markup;
wenzelm
parents: 67215
diff changeset
    58
      Scan.repeat theories --
68292
7ca0c23179e6 support 'export_files' in session ROOT;
wenzelm
parents: 67220
diff changeset
    59
      Scan.repeat document_files --
7ca0c23179e6 support 'export_files' in session ROOT;
wenzelm
parents: 67220
diff changeset
    60
      Scan.repeat export_files))
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
    61
  >> (fn ((((session, _), _), dir),
70678
36c8c32346cb clarified syntax: 'directories' and 'theories' belong together;
wenzelm
parents: 70668
diff changeset
    62
          ((((((((parent, descr), options), sessions), directories), theories),
70668
9cac4dec0da9 support for explicit session directories;
wenzelm
parents: 70049
diff changeset
    63
            document_files), export_files))) =>
67215
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    64
    Toplevel.keep (fn state =>
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    65
      let
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    66
        val ctxt = Toplevel.context_of state;
70049
c1226e4c273e tuned signature;
wenzelm
parents: 69671
diff changeset
    67
        val session_dir = Resources.check_dir ctxt NONE dir;
67215
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    68
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    69
        val _ =
67220
0049bed35f5a more markup;
wenzelm
parents: 67215
diff changeset
    70
          (the_list parent @ sessions) |> List.app (fn arg =>
0049bed35f5a more markup;
wenzelm
parents: 67215
diff changeset
    71
            ignore (Resources.check_session ctxt arg)
0049bed35f5a more markup;
wenzelm
parents: 67215
diff changeset
    72
              handle ERROR msg => Output.error_message msg);
0049bed35f5a more markup;
wenzelm
parents: 67215
diff changeset
    73
0049bed35f5a more markup;
wenzelm
parents: 67215
diff changeset
    74
        val _ =
67215
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    75
          Context_Position.report ctxt
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    76
            (Position.range_position (Symbol_Pos.range (Input.source_explode descr)))
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    77
            Markup.comment;
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    78
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    79
        val _ =
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    80
          (options @ maps #1 theories) |> List.app (fn (x, y) =>
67220
0049bed35f5a more markup;
wenzelm
parents: 67215
diff changeset
    81
            ignore (Completion.check_option_value ctxt x y (Options.default ()))
0049bed35f5a more markup;
wenzelm
parents: 67215
diff changeset
    82
              handle ERROR msg => Output.error_message msg);
67215
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    83
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    84
        val _ =
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    85
          maps #2 theories |> List.app (fn (s, pos) =>
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    86
            let
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    87
              val {node_name, theory_name, ...} =
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    88
                Resources.import_name session session_dir s
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    89
                  handle ERROR msg => error (msg ^ Position.here pos);
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    90
              val theory_path = the_default node_name (Resources.known_theory theory_name);
70049
c1226e4c273e tuned signature;
wenzelm
parents: 69671
diff changeset
    91
              val _ = Resources.check_file ctxt (SOME Path.current) (Path.implode theory_path, pos);
67215
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    92
            in () end);
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    93
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    94
        val _ =
70668
9cac4dec0da9 support for explicit session directories;
wenzelm
parents: 70049
diff changeset
    95
          directories |> List.app (ignore o Resources.check_dir ctxt (SOME session_dir) o #1);
9cac4dec0da9 support for explicit session directories;
wenzelm
parents: 70049
diff changeset
    96
9cac4dec0da9 support for explicit session directories;
wenzelm
parents: 70049
diff changeset
    97
        val _ =
67215
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    98
          document_files |> List.app (fn (doc_dir, doc_files) =>
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
    99
            let
70049
c1226e4c273e tuned signature;
wenzelm
parents: 69671
diff changeset
   100
              val dir = Resources.check_dir ctxt (SOME session_dir) doc_dir;
c1226e4c273e tuned signature;
wenzelm
parents: 69671
diff changeset
   101
              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
   102
            in () end);
68292
7ca0c23179e6 support 'export_files' in session ROOT;
wenzelm
parents: 67220
diff changeset
   103
7ca0c23179e6 support 'export_files' in session ROOT;
wenzelm
parents: 67220
diff changeset
   104
        val _ =
69671
2486792eaf61 support pruning of export names;
wenzelm
parents: 69349
diff changeset
   105
          export_files |> List.app (fn ((export_dir, _), _) =>
70049
c1226e4c273e tuned signature;
wenzelm
parents: 69671
diff changeset
   106
            ignore (Resources.check_path ctxt (SOME session_dir) export_dir));
67215
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
   107
      in () end));
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
   108
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
   109
end;
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
   110
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents:
diff changeset
   111
end;