src/Pure/Thy/sessions.scala
author wenzelm
Thu, 16 Mar 2017 21:22:01 +0100
changeset 65278 b553d0edc440
parent 65269 2947837b9f04
child 65281 c70e7d24a16d
permissions -rw-r--r--
tuned signature;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/Thy/sessions.scala
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
     3
62973
744266e32612 clarified modules;
wenzelm
parents: 62969
diff changeset
     4
Isabelle session information.
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
     5
*/
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
     6
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
     7
package isabelle
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
     8
62704
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
     9
import java.nio.ByteBuffer
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
    10
import java.nio.channels.FileChannel
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
    11
import java.nio.file.StandardOpenOption
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    12
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    13
import scala.collection.SortedSet
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    14
import scala.collection.mutable
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    15
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    16
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    17
object Sessions
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    18
{
62883
b04e9fe29223 clarified ML bootstrap;
wenzelm
parents: 62864
diff changeset
    19
  /* Pure */
b04e9fe29223 clarified ML bootstrap;
wenzelm
parents: 62864
diff changeset
    20
63022
785a59235a15 more IDE support for Isabelle/Pure bootstrap;
wenzelm
parents: 62973
diff changeset
    21
  def pure_name(name: String): Boolean = name == Thy_Header.PURE
62883
b04e9fe29223 clarified ML bootstrap;
wenzelm
parents: 62864
diff changeset
    22
62902
3c0f53eae166 more conventional theory syntax for ML bootstrap, with 'ML_file' instead of 'use';
wenzelm
parents: 62883
diff changeset
    23
  def pure_files(resources: Resources, syntax: Outer_Syntax, dir: Path): List[Path] =
3c0f53eae166 more conventional theory syntax for ML bootstrap, with 'ML_file' instead of 'use';
wenzelm
parents: 62883
diff changeset
    24
  {
62946
wenzelm
parents: 62944
diff changeset
    25
    val roots = Thy_Header.ml_roots.map(_._1)
62902
3c0f53eae166 more conventional theory syntax for ML bootstrap, with 'ML_file' instead of 'use';
wenzelm
parents: 62883
diff changeset
    26
    val loaded_files =
62946
wenzelm
parents: 62944
diff changeset
    27
      roots.flatMap(root => resources.loaded_files(syntax, File.read(dir + Path.explode(root))))
wenzelm
parents: 62944
diff changeset
    28
    (roots ::: loaded_files).map(file => dir + Path.explode(file))
62902
3c0f53eae166 more conventional theory syntax for ML bootstrap, with 'ML_file' instead of 'use';
wenzelm
parents: 62883
diff changeset
    29
  }
62883
b04e9fe29223 clarified ML bootstrap;
wenzelm
parents: 62864
diff changeset
    30
65254
3075aa3b40bf clarified fall-back base, e.g. relevant for "isabelle jedit -l BAD";
wenzelm
parents: 65251
diff changeset
    31
  def pure_base(options: Options): Base = session_base(options, Thy_Header.PURE)
3075aa3b40bf clarified fall-back base, e.g. relevant for "isabelle jedit -l BAD";
wenzelm
parents: 65251
diff changeset
    32
62883
b04e9fe29223 clarified ML bootstrap;
wenzelm
parents: 62864
diff changeset
    33
65251
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    34
  /* base info and source dependencies */
64856
5e9bf964510a clarified modules;
wenzelm
parents: 63996
diff changeset
    35
5e9bf964510a clarified modules;
wenzelm
parents: 63996
diff changeset
    36
  object Base
5e9bf964510a clarified modules;
wenzelm
parents: 63996
diff changeset
    37
  {
5e9bf964510a clarified modules;
wenzelm
parents: 63996
diff changeset
    38
    lazy val bootstrap: Base =
5e9bf964510a clarified modules;
wenzelm
parents: 63996
diff changeset
    39
      Base(keywords = Thy_Header.bootstrap_header, syntax = Thy_Header.bootstrap_syntax)
5e9bf964510a clarified modules;
wenzelm
parents: 63996
diff changeset
    40
  }
5e9bf964510a clarified modules;
wenzelm
parents: 63996
diff changeset
    41
5e9bf964510a clarified modules;
wenzelm
parents: 63996
diff changeset
    42
  sealed case class Base(
5e9bf964510a clarified modules;
wenzelm
parents: 63996
diff changeset
    43
    loaded_theories: Set[String] = Set.empty,
5e9bf964510a clarified modules;
wenzelm
parents: 63996
diff changeset
    44
    known_theories: Map[String, Document.Node.Name] = Map.empty,
5e9bf964510a clarified modules;
wenzelm
parents: 63996
diff changeset
    45
    keywords: Thy_Header.Keywords = Nil,
5e9bf964510a clarified modules;
wenzelm
parents: 63996
diff changeset
    46
    syntax: Outer_Syntax = Outer_Syntax.empty,
5e9bf964510a clarified modules;
wenzelm
parents: 63996
diff changeset
    47
    sources: List[(Path, SHA1.Digest)] = Nil,
5e9bf964510a clarified modules;
wenzelm
parents: 63996
diff changeset
    48
    session_graph: Graph_Display.Graph = Graph_Display.empty_graph)
5e9bf964510a clarified modules;
wenzelm
parents: 63996
diff changeset
    49
65251
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    50
  sealed case class Deps(deps: Map[String, Base])
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    51
  {
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    52
    def is_empty: Boolean = deps.isEmpty
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    53
    def apply(name: String): Base = deps(name)
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    54
    def sources(name: String): List[SHA1.Digest] = deps(name).sources.map(_._2)
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    55
  }
64856
5e9bf964510a clarified modules;
wenzelm
parents: 63996
diff changeset
    56
65251
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    57
  def dependencies(
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    58
      progress: Progress = No_Progress,
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    59
      inlined_files: Boolean = false,
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    60
      verbose: Boolean = false,
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    61
      list_files: Boolean = false,
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    62
      check_keywords: Set[String] = Set.empty,
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    63
      tree: Tree): Deps =
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    64
    Deps((Map.empty[String, Base] /: tree.topological_order)(
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    65
      { case (deps, (name, info)) =>
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    66
          if (progress.stopped) throw Exn.Interrupt()
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    67
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    68
          try {
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    69
            val resources =
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    70
              new Resources(
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    71
                info.parent match {
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    72
                  case None => Base.bootstrap
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    73
                  case Some(parent) => deps(parent)
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    74
                })
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    75
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    76
            if (verbose || list_files) {
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    77
              val groups =
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    78
                if (info.groups.isEmpty) ""
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    79
                else info.groups.mkString(" (", " ", ")")
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    80
              progress.echo("Session " + info.chapter + "/" + name + groups)
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    81
            }
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    82
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    83
            val thy_deps =
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    84
            {
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    85
              val root_theories =
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    86
                info.theories.flatMap({
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    87
                  case (global, _, thys) =>
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    88
                    thys.map(thy =>
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    89
                      (resources.node_name(
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    90
                        if (global) "" else name, info.dir + resources.thy_path(thy)), info.pos))
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    91
                })
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    92
              val thy_deps = resources.thy_info.dependencies(name, root_theories)
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    93
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    94
              thy_deps.errors match {
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    95
                case Nil => thy_deps
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    96
                case errs => error(cat_lines(errs))
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    97
              }
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    98
            }
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
    99
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   100
            val known_theories =
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   101
              (resources.base.known_theories /: thy_deps.deps)({ case (known, dep) =>
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   102
                val name = dep.name
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   103
                known.get(name.theory) match {
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   104
                  case Some(name1) if name != name1 =>
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   105
                    error("Duplicate theory " + quote(name.node) + " vs. " + quote(name1.node))
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   106
                  case _ =>
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   107
                    known + (name.theory -> name) + (Long_Name.base_name(name.theory) -> name)
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   108
                }
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   109
              })
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   110
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   111
            val loaded_theories = thy_deps.loaded_theories
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   112
            val keywords = thy_deps.keywords
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   113
            val syntax = thy_deps.syntax
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   114
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   115
            val theory_files = thy_deps.deps.map(dep => Path.explode(dep.name.node))
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   116
            val loaded_files =
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   117
              if (inlined_files) {
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   118
                val pure_files =
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   119
                  if (pure_name(name)) Sessions.pure_files(resources, syntax, info.dir)
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   120
                  else Nil
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   121
                pure_files ::: thy_deps.loaded_files
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   122
              }
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   123
              else Nil
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   124
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   125
            val all_files =
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   126
              (theory_files ::: loaded_files :::
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   127
                info.files.map(file => info.dir + file) :::
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   128
                info.document_files.map(file => info.dir + file._1 + file._2)).map(_.expand)
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   129
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   130
            if (list_files)
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   131
              progress.echo(cat_lines(all_files.map(_.implode).sorted.map("  " + _)))
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   132
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   133
            if (check_keywords.nonEmpty)
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   134
              Check_Keywords.check_keywords(progress, syntax.keywords, check_keywords, theory_files)
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   135
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   136
            val sources = all_files.map(p => (p, SHA1.digest(p.file)))
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   137
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   138
            val session_graph =
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   139
              Present.session_graph(info.parent getOrElse "",
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   140
                resources.base.loaded_theories, thy_deps.deps)
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   141
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   142
            val base =
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   143
              Base(loaded_theories, known_theories, keywords, syntax, sources, session_graph)
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   144
            deps + (name -> base)
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   145
          }
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   146
          catch {
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   147
            case ERROR(msg) =>
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   148
              cat_error(msg, "The error(s) above occurred in session " +
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   149
                quote(name) + Position.here(info.pos))
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   150
          }
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   151
      }))
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   152
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   153
  def session_base(options: Options, session: String, dirs: List[Path] = Nil): Base =
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   154
  {
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   155
    val (_, tree) = load(options, dirs = dirs).selection(sessions = List(session))
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   156
    dependencies(tree = tree)(session)
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   157
  }
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   158
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   159
4b0a43afc3fb clarified modules;
wenzelm
parents: 64856
diff changeset
   160
  /* session tree */
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   161
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   162
  sealed case class Info(
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   163
    chapter: String,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   164
    select: Boolean,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   165
    pos: Position.T,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   166
    groups: List[String],
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   167
    dir: Path,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   168
    parent: Option[String],
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   169
    description: String,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   170
    options: Options,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   171
    theories: List[(Boolean, Options, List[Path])],
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   172
    files: List[Path],
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   173
    document_files: List[(Path, Path)],
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   174
    meta_digest: SHA1.Digest)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   175
  {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   176
    def timeout: Time = Time.seconds(options.real("timeout") * options.real("timeout_scale"))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   177
  }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   178
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   179
  object Tree
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   180
  {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   181
    def apply(infos: Seq[(String, Info)]): Tree =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   182
    {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   183
      val graph1 =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   184
        (Graph.string[Info] /: infos) {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   185
          case (graph, (name, info)) =>
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   186
            if (graph.defined(name))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   187
              error("Duplicate session " + quote(name) + Position.here(info.pos) +
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   188
                Position.here(graph.get_node(name).pos))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   189
            else graph.new_node(name, info)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   190
        }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   191
      val graph2 =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   192
        (graph1 /: graph1.iterator) {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   193
          case (graph, (name, (info, _))) =>
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   194
            info.parent match {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   195
              case None => graph
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   196
              case Some(parent) =>
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   197
                if (!graph.defined(parent))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   198
                  error("Bad parent session " + quote(parent) + " for " +
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   199
                    quote(name) + Position.here(info.pos))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   200
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   201
                try { graph.add_edge_acyclic(parent, name) }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   202
                catch {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   203
                  case exn: Graph.Cycles[_] =>
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   204
                    error(cat_lines(exn.cycles.map(cycle =>
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   205
                      "Cyclic session dependency of " +
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   206
                        cycle.map(c => quote(c.toString)).mkString(" via "))) +
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   207
                          Position.here(info.pos))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   208
                }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   209
            }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   210
        }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   211
      new Tree(graph2)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   212
    }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   213
  }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   214
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   215
  final class Tree private(val graph: Graph[String, Info])
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   216
    extends PartialFunction[String, Info]
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   217
  {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   218
    def apply(name: String): Info = graph.get_node(name)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   219
    def isDefinedAt(name: String): Boolean = graph.defined(name)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   220
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   221
    def selection(
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   222
      requirements: Boolean = false,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   223
      all_sessions: Boolean = false,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   224
      exclude_session_groups: List[String] = Nil,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   225
      exclude_sessions: List[String] = Nil,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   226
      session_groups: List[String] = Nil,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   227
      sessions: List[String] = Nil): (List[String], Tree) =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   228
    {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   229
      val bad_sessions =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   230
        SortedSet((exclude_sessions ::: sessions).filterNot(isDefinedAt(_)): _*).toList
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   231
      if (bad_sessions.nonEmpty) error("Undefined session(s): " + commas_quote(bad_sessions))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   232
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   233
      val excluded =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   234
      {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   235
        val exclude_group = exclude_session_groups.toSet
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   236
        val exclude_group_sessions =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   237
          (for {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   238
            (name, (info, _)) <- graph.iterator
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   239
            if apply(name).groups.exists(exclude_group)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   240
          } yield name).toList
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   241
        graph.all_succs(exclude_group_sessions ::: exclude_sessions).toSet
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   242
      }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   243
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   244
      val pre_selected =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   245
      {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   246
        if (all_sessions) graph.keys
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   247
        else {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   248
          val select_group = session_groups.toSet
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   249
          val select = sessions.toSet
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   250
          (for {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   251
            (name, (info, _)) <- graph.iterator
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   252
            if info.select || select(name) || apply(name).groups.exists(select_group)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   253
          } yield name).toList
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   254
        }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   255
      }.filterNot(excluded)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   256
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   257
      val selected =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   258
        if (requirements) (graph.all_preds(pre_selected).toSet -- pre_selected).toList
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   259
        else pre_selected
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   260
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   261
      val graph1 = graph.restrict(graph.all_preds(selected).toSet)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   262
      (selected, new Tree(graph1))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   263
    }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   264
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   265
    def ancestors(name: String): List[String] =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   266
      graph.all_preds(List(name)).tail.reverse
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   267
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   268
    def topological_order: List[(String, Info)] =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   269
      graph.topological_order.map(name => (name, apply(name)))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   270
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   271
    override def toString: String = graph.keys_iterator.mkString("Sessions.Tree(", ", ", ")")
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   272
  }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   273
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   274
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   275
  /* parser */
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   276
62864
wenzelm
parents: 62769
diff changeset
   277
  val ROOT = Path.explode("ROOT")
wenzelm
parents: 62769
diff changeset
   278
  val ROOTS = Path.explode("ROOTS")
wenzelm
parents: 62769
diff changeset
   279
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   280
  private val CHAPTER = "chapter"
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   281
  private val SESSION = "session"
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   282
  private val IN = "in"
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   283
  private val DESCRIPTION = "description"
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   284
  private val OPTIONS = "options"
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   285
  private val GLOBAL_THEORIES = "global_theories"
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   286
  private val THEORIES = "theories"
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   287
  private val FILES = "files"
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   288
  private val DOCUMENT_FILES = "document_files"
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   289
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   290
  lazy val root_syntax =
63443
c037248d54e8 clarified keywords;
wenzelm
parents: 63022
diff changeset
   291
    Outer_Syntax.init() + "(" + ")" + "+" + "," + "=" + "[" + "]" + IN +
c037248d54e8 clarified keywords;
wenzelm
parents: 63022
diff changeset
   292
      (CHAPTER, Keyword.THY_DECL) +
c037248d54e8 clarified keywords;
wenzelm
parents: 63022
diff changeset
   293
      (SESSION, Keyword.THY_DECL) +
c037248d54e8 clarified keywords;
wenzelm
parents: 63022
diff changeset
   294
      (DESCRIPTION, Keyword.QUASI_COMMAND) +
c037248d54e8 clarified keywords;
wenzelm
parents: 63022
diff changeset
   295
      (OPTIONS, Keyword.QUASI_COMMAND) +
c037248d54e8 clarified keywords;
wenzelm
parents: 63022
diff changeset
   296
      (GLOBAL_THEORIES, Keyword.QUASI_COMMAND) +
c037248d54e8 clarified keywords;
wenzelm
parents: 63022
diff changeset
   297
      (THEORIES, Keyword.QUASI_COMMAND) +
c037248d54e8 clarified keywords;
wenzelm
parents: 63022
diff changeset
   298
      (FILES, Keyword.QUASI_COMMAND) +
c037248d54e8 clarified keywords;
wenzelm
parents: 63022
diff changeset
   299
      (DOCUMENT_FILES, Keyword.QUASI_COMMAND)
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   300
62968
4e4738698db4 clarified syntax;
wenzelm
parents: 62946
diff changeset
   301
  private object Parser extends Parse.Parser with Options.Parser
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   302
  {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   303
    private abstract class Entry
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   304
    private sealed case class Chapter(name: String) extends Entry
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   305
    private sealed case class Session_Entry(
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   306
      pos: Position.T,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   307
      name: String,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   308
      groups: List[String],
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   309
      path: String,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   310
      parent: Option[String],
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   311
      description: String,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   312
      options: List[Options.Spec],
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   313
      theories: List[(Boolean, List[Options.Spec], List[String])],
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   314
      files: List[String],
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   315
      document_files: List[(String, String)]) extends Entry
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   316
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   317
    private val chapter: Parser[Chapter] =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   318
    {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   319
      val chapter_name = atom("chapter name", _.is_name)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   320
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   321
      command(CHAPTER) ~! chapter_name ^^ { case _ ~ a => Chapter(a) }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   322
    }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   323
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   324
    private val session_entry: Parser[Session_Entry] =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   325
    {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   326
      val session_name = atom("session name", _.is_name)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   327
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   328
      val option =
62968
4e4738698db4 clarified syntax;
wenzelm
parents: 62946
diff changeset
   329
        option_name ~ opt($$$("=") ~! option_value ^^
4e4738698db4 clarified syntax;
wenzelm
parents: 62946
diff changeset
   330
          { case _ ~ x => x }) ^^ { case x ~ y => (x, y) }
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   331
      val options = $$$("[") ~> rep1sep(option, $$$(",")) <~ $$$("]")
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   332
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   333
      val theories =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   334
        ($$$(GLOBAL_THEORIES) | $$$(THEORIES)) ~!
62969
9f394a16c557 eliminated "xname" and variants;
wenzelm
parents: 62968
diff changeset
   335
          ((options | success(Nil)) ~ rep(theory_name)) ^^
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   336
          { case x ~ (y ~ z) => (x == GLOBAL_THEORIES, y, z) }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   337
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   338
      val document_files =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   339
        $$$(DOCUMENT_FILES) ~!
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   340
          (($$$("(") ~! ($$$(IN) ~! (path ~ $$$(")"))) ^^
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   341
              { case _ ~ (_ ~ (x ~ _)) => x } | success("document")) ~
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   342
            rep1(path)) ^^ { case _ ~ (x ~ y) => y.map((x, _)) }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   343
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   344
      command(SESSION) ~!
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   345
        (position(session_name) ~
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   346
          (($$$("(") ~! (rep1(name) <~ $$$(")")) ^^ { case _ ~ x => x }) | success(Nil)) ~
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   347
          (($$$(IN) ~! path ^^ { case _ ~ x => x }) | success(".")) ~
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   348
          ($$$("=") ~!
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   349
            (opt(session_name ~! $$$("+") ^^ { case x ~ _ => x }) ~
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   350
              (($$$(DESCRIPTION) ~! text ^^ { case _ ~ x => x }) | success("")) ~
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   351
              (($$$(OPTIONS) ~! options ^^ { case _ ~ x => x }) | success(Nil)) ~
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   352
              rep1(theories) ~
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   353
              (($$$(FILES) ~! rep1(path) ^^ { case _ ~ x => x }) | success(Nil)) ~
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   354
              (rep(document_files) ^^ (x => x.flatten))))) ^^
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   355
        { case _ ~ ((a, pos) ~ b ~ c ~ (_ ~ (d ~ e ~ f ~ g ~ h ~ i))) =>
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   356
            Session_Entry(pos, a, b, c, d, e, f, g, h, i) }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   357
    }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   358
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   359
    def parse(options: Options, select: Boolean, dir: Path): List[(String, Info)] =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   360
    {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   361
      def make_info(entry_chapter: String, entry: Session_Entry): (String, Info) =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   362
      {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   363
        try {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   364
          val name = entry.name
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   365
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   366
          if (name == "") error("Bad session name")
62883
b04e9fe29223 clarified ML bootstrap;
wenzelm
parents: 62864
diff changeset
   367
          if (pure_name(name) && entry.parent.isDefined) error("Illegal parent session")
b04e9fe29223 clarified ML bootstrap;
wenzelm
parents: 62864
diff changeset
   368
          if (!pure_name(name) && !entry.parent.isDefined) error("Missing parent session")
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   369
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   370
          val session_options = options ++ entry.options
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   371
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   372
          val theories =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   373
            entry.theories.map({ case (global, opts, thys) =>
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   374
              (global, session_options ++ opts, thys.map(Path.explode(_))) })
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   375
          val files = entry.files.map(Path.explode(_))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   376
          val document_files =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   377
            entry.document_files.map({ case (s1, s2) => (Path.explode(s1), Path.explode(s2)) })
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   378
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   379
          val meta_digest =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   380
            SHA1.digest((entry_chapter, name, entry.parent, entry.options,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   381
              entry.theories, entry.files, entry.document_files).toString)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   382
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   383
          val info =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   384
            Info(entry_chapter, select, entry.pos, entry.groups, dir + Path.explode(entry.path),
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   385
              entry.parent, entry.description, session_options, theories, files,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   386
              document_files, meta_digest)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   387
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   388
          (name, info)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   389
        }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   390
        catch {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   391
          case ERROR(msg) =>
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   392
            error(msg + "\nThe error(s) above occurred in session entry " +
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   393
              quote(entry.name) + Position.here(entry.pos))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   394
        }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   395
      }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   396
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   397
      val root = dir + ROOT
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   398
      if (root.is_file) {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   399
        val toks = Token.explode(root_syntax.keywords, File.read(root))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   400
        val start = Token.Pos.file(root.implode)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   401
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   402
        parse_all(rep(chapter | session_entry), Token.reader(toks, start)) match {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   403
          case Success(result, _) =>
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   404
            var entry_chapter = "Unsorted"
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   405
            val infos = new mutable.ListBuffer[(String, Info)]
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   406
            result.foreach {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   407
              case Chapter(name) => entry_chapter = name
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   408
              case entry: Session_Entry => infos += make_info(entry_chapter, entry)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   409
            }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   410
            infos.toList
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   411
          case bad => error(bad.toString)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   412
        }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   413
      }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   414
      else Nil
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   415
    }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   416
  }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   417
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   418
62635
4854a38061de tuned signature;
wenzelm
parents: 62633
diff changeset
   419
  /* load sessions from certain directories */
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   420
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   421
  private def is_session_dir(dir: Path): Boolean =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   422
    (dir + ROOT).is_file || (dir + ROOTS).is_file
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   423
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   424
  private def check_session_dir(dir: Path): Path =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   425
    if (is_session_dir(dir)) dir
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   426
    else error("Bad session root directory: " + dir.toString)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   427
62635
4854a38061de tuned signature;
wenzelm
parents: 62633
diff changeset
   428
  def load(options: Options, dirs: List[Path] = Nil, select_dirs: List[Path] = Nil): Tree =
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   429
  {
62635
4854a38061de tuned signature;
wenzelm
parents: 62633
diff changeset
   430
    def load_dir(select: Boolean, dir: Path): List[(String, Info)] =
4854a38061de tuned signature;
wenzelm
parents: 62633
diff changeset
   431
      load_root(select, dir) ::: load_roots(select, dir)
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   432
62635
4854a38061de tuned signature;
wenzelm
parents: 62633
diff changeset
   433
    def load_root(select: Boolean, dir: Path): List[(String, Info)] =
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   434
      Parser.parse(options, select, dir)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   435
62635
4854a38061de tuned signature;
wenzelm
parents: 62633
diff changeset
   436
    def load_roots(select: Boolean, dir: Path): List[(String, Info)] =
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   437
    {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   438
      val roots = dir + ROOTS
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   439
      if (roots.is_file) {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   440
        for {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   441
          line <- split_lines(File.read(roots))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   442
          if !(line == "" || line.startsWith("#"))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   443
          dir1 =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   444
            try { check_session_dir(dir + Path.explode(line)) }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   445
            catch {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   446
              case ERROR(msg) =>
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   447
                error(msg + "\nThe error(s) above occurred in session catalog " + roots.toString)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   448
            }
62635
4854a38061de tuned signature;
wenzelm
parents: 62633
diff changeset
   449
          info <- load_dir(select, dir1)
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   450
        } yield info
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   451
      }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   452
      else Nil
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   453
    }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   454
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   455
    val default_dirs = Isabelle_System.components().filter(is_session_dir(_))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   456
    dirs.foreach(check_session_dir(_))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   457
    select_dirs.foreach(check_session_dir(_))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   458
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   459
    Tree(
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   460
      for {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   461
        (select, dir) <- (default_dirs ::: dirs).map((false, _)) ::: select_dirs.map((true, _))
62635
4854a38061de tuned signature;
wenzelm
parents: 62633
diff changeset
   462
        info <- load_dir(select, dir)
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   463
      } yield info)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   464
  }
62632
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   465
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   466
62637
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   467
62704
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   468
  /** heap file with SHA1 digest **/
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   469
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   470
  private val sha1_prefix = "SHA1:"
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   471
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   472
  def read_heap_digest(heap: Path): Option[String] =
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   473
  {
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   474
    if (heap.is_file) {
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   475
      val file = FileChannel.open(heap.file.toPath, StandardOpenOption.READ)
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   476
      try {
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   477
        val len = file.size
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   478
        val n = sha1_prefix.length + SHA1.digest_length
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   479
        if (len >= n) {
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   480
          file.position(len - n)
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   481
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   482
          val buf = ByteBuffer.allocate(n)
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   483
          var i = 0
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   484
          var m = 0
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   485
          do {
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   486
            m = file.read(buf)
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   487
            if (m != -1) i += m
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   488
          }
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   489
          while (m != -1 && n > i)
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   490
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   491
          if (i == n) {
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   492
            val prefix = new String(buf.array(), 0, sha1_prefix.length, UTF8.charset)
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   493
            val s = new String(buf.array(), sha1_prefix.length, SHA1.digest_length, UTF8.charset)
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   494
            if (prefix == sha1_prefix) Some(s) else None
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   495
          }
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   496
          else None
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   497
        }
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   498
        else None
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   499
      }
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   500
      finally { file.close }
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   501
    }
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   502
    else None
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   503
  }
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   504
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   505
  def write_heap_digest(heap: Path): String =
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   506
    read_heap_digest(heap) match {
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   507
      case None =>
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   508
        val s = SHA1.digest(heap).rep
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   509
        File.append(heap, sha1_prefix + s)
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   510
        s
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   511
      case Some(s) => s
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   512
    }
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   513
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   514
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   515
62637
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   516
  /** persistent store **/
62632
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   517
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   518
  def store(system_mode: Boolean = false): Store = new Store(system_mode)
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   519
63996
3f47fec9edfc tuned whitespace;
wenzelm
parents: 63443
diff changeset
   520
  class Store private[Sessions](system_mode: Boolean)
62632
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   521
  {
65278
b553d0edc440 tuned signature;
wenzelm
parents: 65269
diff changeset
   522
    /* file names */
b553d0edc440 tuned signature;
wenzelm
parents: 65269
diff changeset
   523
b553d0edc440 tuned signature;
wenzelm
parents: 65269
diff changeset
   524
    def log(name: String): Path = Path.basic("log") + Path.basic(name)
b553d0edc440 tuned signature;
wenzelm
parents: 65269
diff changeset
   525
    def log_gz(name: String): Path = log(name).ext("gz")
b553d0edc440 tuned signature;
wenzelm
parents: 65269
diff changeset
   526
b553d0edc440 tuned signature;
wenzelm
parents: 65269
diff changeset
   527
62637
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   528
    /* output */
62632
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   529
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   530
    val browser_info: Path =
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   531
      if (system_mode) Path.explode("~~/browser_info")
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   532
      else Path.explode("$ISABELLE_BROWSER_INFO")
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   533
62637
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   534
    val output_dir: Path =
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   535
      if (system_mode) Path.explode("~~/heaps/$ML_IDENTIFIER")
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   536
      else Path.explode("$ISABELLE_OUTPUT")
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   537
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   538
    def prepare_output() { Isabelle_System.mkdirs(output_dir + Path.basic("log")) }
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   539
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   540
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   541
    /* input */
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   542
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   543
    private val input_dirs =
62632
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   544
      if (system_mode) List(output_dir)
62633
e57416b649d5 find heaps uniformly via Sessions.Store;
wenzelm
parents: 62632
diff changeset
   545
      else {
e57416b649d5 find heaps uniformly via Sessions.Store;
wenzelm
parents: 62632
diff changeset
   546
        val ml_ident = Path.explode("$ML_IDENTIFIER").expand
e57416b649d5 find heaps uniformly via Sessions.Store;
wenzelm
parents: 62632
diff changeset
   547
        output_dir :: Path.split(Isabelle_System.getenv_strict("ISABELLE_PATH")).map(_ + ml_ident)
e57416b649d5 find heaps uniformly via Sessions.Store;
wenzelm
parents: 62632
diff changeset
   548
      }
62632
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   549
62636
e676ae9f1bf6 clarified signature;
wenzelm
parents: 62635
diff changeset
   550
    def find(name: String): Option[(Path, Option[String])] =
62632
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   551
      input_dirs.find(dir => (dir + log_gz(name)).is_file).map(dir =>
62704
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   552
        (dir + log_gz(name), read_heap_digest(dir + Path.basic(name))))
62632
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   553
62633
e57416b649d5 find heaps uniformly via Sessions.Store;
wenzelm
parents: 62632
diff changeset
   554
    def find_log(name: String): Option[Path] =
e57416b649d5 find heaps uniformly via Sessions.Store;
wenzelm
parents: 62632
diff changeset
   555
      input_dirs.map(_ + log(name)).find(_.is_file)
e57416b649d5 find heaps uniformly via Sessions.Store;
wenzelm
parents: 62632
diff changeset
   556
e57416b649d5 find heaps uniformly via Sessions.Store;
wenzelm
parents: 62632
diff changeset
   557
    def find_log_gz(name: String): Option[Path] =
e57416b649d5 find heaps uniformly via Sessions.Store;
wenzelm
parents: 62632
diff changeset
   558
      input_dirs.map(_ + log_gz(name)).find(_.is_file)
62632
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   559
62637
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   560
    def find_heap(name: String): Option[Path] =
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   561
      input_dirs.map(_ + Path.basic(name)).find(_.is_file)
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   562
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   563
    def heap(name: String): Path =
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   564
      find_heap(name) getOrElse
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   565
        error("Unknown logic " + quote(name) + " -- no heap file found in:\n" +
62769
146945b9e83c tuned message;
wenzelm
parents: 62704
diff changeset
   566
          cat_lines(input_dirs.map(dir => "  " + dir.expand.implode)))
62632
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   567
  }
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   568
}