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