src/Pure/PIDE/resources.scala
author wenzelm
Mon, 06 Nov 2017 17:24:09 +0100
changeset 67015 1a9e2a2bf251
parent 66966 f3f9a492bee6
child 67053 57c37ee49c39
permissions -rw-r--r--
tuned signature;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56208
06cc31dff138 clarifed module name;
wenzelm
parents: 55879
diff changeset
     1
/*  Title:      Pure/PIDE/resources.scala
43651
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
     3
56208
06cc31dff138 clarifed module name;
wenzelm
parents: 55879
diff changeset
     4
Resources for theories and auxiliary files.
43651
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
     5
*/
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
     6
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
     7
package isabelle
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
     8
44953
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
     9
48885
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    10
import scala.annotation.tailrec
56823
37be55461dbe more frugal access to theory text via Reader, reduced costs for I/O text decoding;
wenzelm
parents: 56801
diff changeset
    11
import scala.util.parsing.input.Reader
48885
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    12
48409
0d2114eb412a more explicit java.io.{File => JFile};
wenzelm
parents: 46938
diff changeset
    13
import java.io.{File => JFile}
44953
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    14
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    15
65359
9ca34f0407a9 provide session qualifier via resources;
wenzelm
parents: 65358
diff changeset
    16
class Resources(
65361
ecefb68dc21d tuned signature;
wenzelm
parents: 65359
diff changeset
    17
  val session_base: Sessions.Base,
65359
9ca34f0407a9 provide session qualifier via resources;
wenzelm
parents: 65358
diff changeset
    18
  val log: Logger = No_Logger)
43651
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    19
{
66959
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
    20
  resources =>
64839
842163abfc0d tuned signature;
wenzelm
parents: 64835
diff changeset
    21
67015
1a9e2a2bf251 tuned signature;
wenzelm
parents: 66966
diff changeset
    22
1a9e2a2bf251 tuned signature;
wenzelm
parents: 66966
diff changeset
    23
  /* theory files */
1a9e2a2bf251 tuned signature;
wenzelm
parents: 66966
diff changeset
    24
65250
13a6c81534a8 tuned signature;
wenzelm
parents: 64856
diff changeset
    25
  def thy_path(path: Path): Path = path.ext("thy")
13a6c81534a8 tuned signature;
wenzelm
parents: 64856
diff changeset
    26
67015
1a9e2a2bf251 tuned signature;
wenzelm
parents: 66966
diff changeset
    27
  def thy_node_name(qualifier: String, file: JFile, bootstrap: Boolean = false)
1a9e2a2bf251 tuned signature;
wenzelm
parents: 66966
diff changeset
    28
    : Document.Node.Name =
1a9e2a2bf251 tuned signature;
wenzelm
parents: 66966
diff changeset
    29
  {
1a9e2a2bf251 tuned signature;
wenzelm
parents: 66966
diff changeset
    30
    session_base.known.get_file(file, bootstrap) getOrElse {
1a9e2a2bf251 tuned signature;
wenzelm
parents: 66966
diff changeset
    31
      val node = file.getPath
1a9e2a2bf251 tuned signature;
wenzelm
parents: 66966
diff changeset
    32
      theory_name(qualifier, Thy_Header.theory_name(node)) match {
1a9e2a2bf251 tuned signature;
wenzelm
parents: 66966
diff changeset
    33
        case (true, theory) => Document.Node.Name.loaded_theory(theory)
1a9e2a2bf251 tuned signature;
wenzelm
parents: 66966
diff changeset
    34
        case (false, theory) =>
1a9e2a2bf251 tuned signature;
wenzelm
parents: 66966
diff changeset
    35
          val master_dir = if (theory == "") "" else file.getParent
1a9e2a2bf251 tuned signature;
wenzelm
parents: 66966
diff changeset
    36
          Document.Node.Name(node, master_dir, theory)
1a9e2a2bf251 tuned signature;
wenzelm
parents: 66966
diff changeset
    37
      }
1a9e2a2bf251 tuned signature;
wenzelm
parents: 66966
diff changeset
    38
    }
1a9e2a2bf251 tuned signature;
wenzelm
parents: 66966
diff changeset
    39
  }
1a9e2a2bf251 tuned signature;
wenzelm
parents: 66966
diff changeset
    40
64839
842163abfc0d tuned signature;
wenzelm
parents: 64835
diff changeset
    41
44953
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    42
  /* file-system operations */
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    43
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    44
  def append(dir: String, source_path: Path): String =
48711
8d381fdef898 need to expand path in order to resolve imports like "~~/src/Tools/Code_Generator";
wenzelm
parents: 48710
diff changeset
    45
    (Path.explode(dir) + source_path).expand.implode
44953
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    46
65488
331f09d9535e tuned signature;
wenzelm
parents: 65487
diff changeset
    47
  def append(node_name: Document.Node.Name, source_path: Path): String =
331f09d9535e tuned signature;
wenzelm
parents: 65487
diff changeset
    48
    append(node_name.master_dir, source_path)
331f09d9535e tuned signature;
wenzelm
parents: 65487
diff changeset
    49
46737
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    50
64657
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    51
  /* source files of Isabelle/ML bootstrap */
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    52
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    53
  def source_file(raw_name: String): Option[String] =
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    54
  {
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    55
    if (Path.is_wellformed(raw_name)) {
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    56
      if (Path.is_valid(raw_name)) {
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    57
        def check(p: Path): Option[Path] = if (p.is_file) Some(p) else None
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    58
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    59
        val path = Path.explode(raw_name)
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    60
        val path1 =
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    61
          if (path.is_absolute || path.is_current) check(path)
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    62
          else {
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    63
            check(Path.explode("~~/src/Pure") + path) orElse
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    64
              (if (Isabelle_System.getenv("ML_SOURCES") == "") None
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    65
               else check(Path.explode("$ML_SOURCES") + path))
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    66
          }
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    67
        Some(File.platform_path(path1 getOrElse path))
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    68
      }
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    69
      else None
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    70
    }
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    71
    else Some(raw_name)
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    72
  }
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    73
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    74
46737
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    75
  /* theory files */
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    76
66698
5b9dc3f7bcde prefer sequential file-system access, but parallel parse;
wenzelm
parents: 66697
diff changeset
    77
  def loaded_files(syntax: Outer_Syntax, name: Document.Node.Name): () => List[Path] =
66695
wenzelm
parents: 65532
diff changeset
    78
  {
66918
ec2b50aeb0dd more robust treatment of UTF8 in raw byte sources;
wenzelm
parents: 66917
diff changeset
    79
    val (is_utf8, raw_text) =
ec2b50aeb0dd more robust treatment of UTF8 in raw byte sources;
wenzelm
parents: 66917
diff changeset
    80
      with_thy_reader(name, reader => (Scan.reader_is_utf8(reader), reader.source.toString))
66698
5b9dc3f7bcde prefer sequential file-system access, but parallel parse;
wenzelm
parents: 66697
diff changeset
    81
    () => {
66917
fcf84cd6c94f clarified check, assuming that load commands are plain ASCII;
wenzelm
parents: 66850
diff changeset
    82
      if (syntax.load_commands_in(raw_text)) {
66918
ec2b50aeb0dd more robust treatment of UTF8 in raw byte sources;
wenzelm
parents: 66917
diff changeset
    83
        val text = Symbol.decode(Scan.reader_decode_utf8(is_utf8, raw_text))
66698
5b9dc3f7bcde prefer sequential file-system access, but parallel parse;
wenzelm
parents: 66697
diff changeset
    84
        val spans = syntax.parse_spans(text)
5b9dc3f7bcde prefer sequential file-system access, but parallel parse;
wenzelm
parents: 66697
diff changeset
    85
        val dir = Path.explode(name.master_dir)
5b9dc3f7bcde prefer sequential file-system access, but parallel parse;
wenzelm
parents: 66697
diff changeset
    86
        spans.iterator.map(Command.span_files(syntax, _)._1).flatten.
66700
5174ce7c84f0 clarified: more uniform results;
wenzelm
parents: 66699
diff changeset
    87
          map(a => (dir + Path.explode(a)).expand).toList
66698
5b9dc3f7bcde prefer sequential file-system access, but parallel parse;
wenzelm
parents: 66697
diff changeset
    88
      }
5b9dc3f7bcde prefer sequential file-system access, but parallel parse;
wenzelm
parents: 66697
diff changeset
    89
      else Nil
66696
8f863dae78a0 clarified pure_files, based on uniform loaded_files;
wenzelm
parents: 66695
diff changeset
    90
    }
66695
wenzelm
parents: 65532
diff changeset
    91
  }
wenzelm
parents: 65532
diff changeset
    92
66701
d181f8a0e857 maintain loaded_files for each theory;
wenzelm
parents: 66700
diff changeset
    93
  def pure_files(syntax: Outer_Syntax, dir: Path): List[Path] =
d181f8a0e857 maintain loaded_files for each theory;
wenzelm
parents: 66700
diff changeset
    94
  {
d181f8a0e857 maintain loaded_files for each theory;
wenzelm
parents: 66700
diff changeset
    95
    val roots =
d181f8a0e857 maintain loaded_files for each theory;
wenzelm
parents: 66700
diff changeset
    96
      for { (name, _) <- Thy_Header.ml_roots }
d181f8a0e857 maintain loaded_files for each theory;
wenzelm
parents: 66700
diff changeset
    97
      yield (dir + Path.explode(name)).expand
d181f8a0e857 maintain loaded_files for each theory;
wenzelm
parents: 66700
diff changeset
    98
    val files =
d181f8a0e857 maintain loaded_files for each theory;
wenzelm
parents: 66700
diff changeset
    99
      for {
d181f8a0e857 maintain loaded_files for each theory;
wenzelm
parents: 66700
diff changeset
   100
        (path, (_, theory)) <- roots zip Thy_Header.ml_roots
d181f8a0e857 maintain loaded_files for each theory;
wenzelm
parents: 66700
diff changeset
   101
        file <- loaded_files(syntax, Document.Node.Name(path.implode, path.dir.implode, theory))()
d181f8a0e857 maintain loaded_files for each theory;
wenzelm
parents: 66700
diff changeset
   102
      } yield file
d181f8a0e857 maintain loaded_files for each theory;
wenzelm
parents: 66700
diff changeset
   103
    roots ::: files
d181f8a0e857 maintain loaded_files for each theory;
wenzelm
parents: 66700
diff changeset
   104
  }
66696
8f863dae78a0 clarified pure_files, based on uniform loaded_files;
wenzelm
parents: 66695
diff changeset
   105
66712
4c98c929a12a session-qualified theory names are mandatory;
wenzelm
parents: 66701
diff changeset
   106
  def theory_name(qualifier: String, theory: String): (Boolean, String) =
4c98c929a12a session-qualified theory names are mandatory;
wenzelm
parents: 66701
diff changeset
   107
    if (session_base.loaded_theory(theory)) (true, theory)
4c98c929a12a session-qualified theory names are mandatory;
wenzelm
parents: 66701
diff changeset
   108
    else {
4c98c929a12a session-qualified theory names are mandatory;
wenzelm
parents: 66701
diff changeset
   109
      val theory1 =
4c98c929a12a session-qualified theory names are mandatory;
wenzelm
parents: 66701
diff changeset
   110
        if (Long_Name.is_qualified(theory) || session_base.global_theories.isDefinedAt(theory))
4c98c929a12a session-qualified theory names are mandatory;
wenzelm
parents: 66701
diff changeset
   111
          theory
4c98c929a12a session-qualified theory names are mandatory;
wenzelm
parents: 66701
diff changeset
   112
        else Long_Name.qualify(qualifier, theory)
4c98c929a12a session-qualified theory names are mandatory;
wenzelm
parents: 66701
diff changeset
   113
      (false, theory1)
65471
05e5bffcf1d8 clarified loaded_theories: map to qualified theory name;
wenzelm
parents: 65467
diff changeset
   114
    }
05e5bffcf1d8 clarified loaded_theories: map to qualified theory name;
wenzelm
parents: 65467
diff changeset
   115
65445
e9e7f5f5794c more qualifier treatment, but in the end it is still ignored;
wenzelm
parents: 65441
diff changeset
   116
  def import_name(qualifier: String, dir: String, s: String): Document.Node.Name =
65476
a72ae9eb4462 tuned signature (again);
wenzelm
parents: 65472
diff changeset
   117
    theory_name(qualifier, Thy_Header.import_name(s)) match {
65472
wenzelm
parents: 65471
diff changeset
   118
      case (true, theory) => Document.Node.Name.loaded_theory(theory)
wenzelm
parents: 65471
diff changeset
   119
      case (false, theory) =>
65498
2af863e28204 tuned signature;
wenzelm
parents: 65494
diff changeset
   120
        session_base.known_theory(theory) match {
65472
wenzelm
parents: 65471
diff changeset
   121
          case Some(node_name) => node_name
wenzelm
parents: 65471
diff changeset
   122
          case None =>
66771
925d10a7a610 clarified error for bad session-qualified imports;
wenzelm
parents: 66767
diff changeset
   123
            if (Thy_Header.is_base_name(s) && Long_Name.is_qualified(s))
925d10a7a610 clarified error for bad session-qualified imports;
wenzelm
parents: 66767
diff changeset
   124
              Document.Node.Name.loaded_theory(theory)
925d10a7a610 clarified error for bad session-qualified imports;
wenzelm
parents: 66767
diff changeset
   125
            else {
925d10a7a610 clarified error for bad session-qualified imports;
wenzelm
parents: 66767
diff changeset
   126
              val path = Path.explode(s)
925d10a7a610 clarified error for bad session-qualified imports;
wenzelm
parents: 66767
diff changeset
   127
              val node = append(dir, thy_path(path))
925d10a7a610 clarified error for bad session-qualified imports;
wenzelm
parents: 66767
diff changeset
   128
              val master_dir = append(dir, path.dir)
925d10a7a610 clarified error for bad session-qualified imports;
wenzelm
parents: 66767
diff changeset
   129
              Document.Node.Name(node, master_dir, theory)
925d10a7a610 clarified error for bad session-qualified imports;
wenzelm
parents: 66767
diff changeset
   130
            }
65472
wenzelm
parents: 65471
diff changeset
   131
        }
wenzelm
parents: 65471
diff changeset
   132
    }
51634
553953ad8165 more general Thy_Load.import_name, e.g. relevant for Isabelle/eclipse -- NB: Thy_Load serves as main hub for funny overriding to adapt to provers and editors;
wenzelm
parents: 51293
diff changeset
   133
65529
wenzelm
parents: 65503
diff changeset
   134
  def import_name(name: Document.Node.Name, s: String): Document.Node.Name =
66966
f3f9a492bee6 clarified signature;
wenzelm
parents: 66959
diff changeset
   135
    import_name(session_base.theory_qualifier(name), name.master_dir, s)
65489
f3076367f4a8 tuned signature;
wenzelm
parents: 65488
diff changeset
   136
66966
f3f9a492bee6 clarified signature;
wenzelm
parents: 66959
diff changeset
   137
  def standard_import(base: Sessions.Base, qualifier: String, dir: String, s: String): String =
66767
294c2e9a689e clarified modules;
wenzelm
parents: 66766
diff changeset
   138
  {
294c2e9a689e clarified modules;
wenzelm
parents: 66766
diff changeset
   139
    val name = import_name(qualifier, dir, s)
294c2e9a689e clarified modules;
wenzelm
parents: 66766
diff changeset
   140
    val s1 =
294c2e9a689e clarified modules;
wenzelm
parents: 66766
diff changeset
   141
      if (session_base.loaded_theory(name)) name.theory
294c2e9a689e clarified modules;
wenzelm
parents: 66766
diff changeset
   142
      else {
66850
a91b6d80c911 more robust: allow URLs;
wenzelm
parents: 66771
diff changeset
   143
        (try { Some(name.path) } catch { case ERROR(_) => None }) match {
a91b6d80c911 more robust: allow URLs;
wenzelm
parents: 66771
diff changeset
   144
          case None => s
a91b6d80c911 more robust: allow URLs;
wenzelm
parents: 66771
diff changeset
   145
          case Some(path) =>
a91b6d80c911 more robust: allow URLs;
wenzelm
parents: 66771
diff changeset
   146
            session_base.known.get_file(path.file) match {
66966
f3f9a492bee6 clarified signature;
wenzelm
parents: 66959
diff changeset
   147
              case Some(name1) if base.theory_qualifier(name1) != qualifier =>
66850
a91b6d80c911 more robust: allow URLs;
wenzelm
parents: 66771
diff changeset
   148
                name1.theory
a91b6d80c911 more robust: allow URLs;
wenzelm
parents: 66771
diff changeset
   149
              case Some(name1) if Thy_Header.is_base_name(s) =>
a91b6d80c911 more robust: allow URLs;
wenzelm
parents: 66771
diff changeset
   150
                name1.theory_base_name
a91b6d80c911 more robust: allow URLs;
wenzelm
parents: 66771
diff changeset
   151
              case _ => s
a91b6d80c911 more robust: allow URLs;
wenzelm
parents: 66771
diff changeset
   152
            }
66767
294c2e9a689e clarified modules;
wenzelm
parents: 66766
diff changeset
   153
        }
294c2e9a689e clarified modules;
wenzelm
parents: 66766
diff changeset
   154
      }
294c2e9a689e clarified modules;
wenzelm
parents: 66766
diff changeset
   155
    val name2 = import_name(qualifier, dir, s1)
294c2e9a689e clarified modules;
wenzelm
parents: 66766
diff changeset
   156
    if (name.node == name2.node) s1 else s
294c2e9a689e clarified modules;
wenzelm
parents: 66766
diff changeset
   157
  }
294c2e9a689e clarified modules;
wenzelm
parents: 66766
diff changeset
   158
64656
wenzelm
parents: 64654
diff changeset
   159
  def with_thy_reader[A](name: Document.Node.Name, f: Reader[Char] => A): A =
wenzelm
parents: 64654
diff changeset
   160
  {
66771
925d10a7a610 clarified error for bad session-qualified imports;
wenzelm
parents: 66767
diff changeset
   161
    val path = name.path
925d10a7a610 clarified error for bad session-qualified imports;
wenzelm
parents: 66767
diff changeset
   162
    if (path.is_file) {
925d10a7a610 clarified error for bad session-qualified imports;
wenzelm
parents: 66767
diff changeset
   163
      val reader = Scan.byte_reader(path.file)
925d10a7a610 clarified error for bad session-qualified imports;
wenzelm
parents: 66767
diff changeset
   164
      try { f(reader) } finally { reader.close }
925d10a7a610 clarified error for bad session-qualified imports;
wenzelm
parents: 66767
diff changeset
   165
    }
925d10a7a610 clarified error for bad session-qualified imports;
wenzelm
parents: 66767
diff changeset
   166
    else if (name.node == name.theory)
925d10a7a610 clarified error for bad session-qualified imports;
wenzelm
parents: 66767
diff changeset
   167
      error("Cannot load theory " + quote(name.theory))
925d10a7a610 clarified error for bad session-qualified imports;
wenzelm
parents: 66767
diff changeset
   168
    else error ("Cannot load theory file " + path)
64656
wenzelm
parents: 64654
diff changeset
   169
  }
wenzelm
parents: 64654
diff changeset
   170
65359
9ca34f0407a9 provide session qualifier via resources;
wenzelm
parents: 65358
diff changeset
   171
  def check_thy_reader(node_name: Document.Node.Name, reader: Reader[Char],
9ca34f0407a9 provide session qualifier via resources;
wenzelm
parents: 65358
diff changeset
   172
    start: Token.Pos = Token.Pos.command, strict: Boolean = true): Document.Node.Header =
46737
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
   173
  {
64826
c97296294f6d clarified check_thy_reader: check node_name here;
wenzelm
parents: 64825
diff changeset
   174
    if (node_name.is_theory && reader.source.length > 0) {
57615
df1b3452d71c more explicit discrimination of empty nodes -- suppress from Theories panel;
wenzelm
parents: 56913
diff changeset
   175
      try {
66713
wenzelm
parents: 66712
diff changeset
   176
        val header = Thy_Header.read(reader, start, strict)
48885
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
   177
65439
862bfd2b4fd4 tuned signature;
wenzelm
parents: 65429
diff changeset
   178
        val base_name = node_name.theory_base_name
59694
d2bb4b5ed862 misc tuning -- more uniform ML vs. Scala;
wenzelm
parents: 59691
diff changeset
   179
        val (name, pos) = header.name
d2bb4b5ed862 misc tuning -- more uniform ML vs. Scala;
wenzelm
parents: 59691
diff changeset
   180
        if (base_name != name)
59718
5d0c539537c9 tuned message -- include completion;
wenzelm
parents: 59705
diff changeset
   181
          error("Bad theory name " + quote(name) +
65250
13a6c81534a8 tuned signature;
wenzelm
parents: 64856
diff changeset
   182
            " for file " + thy_path(Path.basic(base_name)) + Position.here(pos) +
66766
19f8385ddfd3 tuned signature;
wenzelm
parents: 66716
diff changeset
   183
            Completion.report_theories(pos, List(base_name)))
48885
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
   184
65489
f3076367f4a8 tuned signature;
wenzelm
parents: 65488
diff changeset
   185
        val imports = header.imports.map({ case (s, pos) => (import_name(node_name, s), pos) })
63579
73939a9b70a3 support 'abbrevs' within theory header;
wenzelm
parents: 63020
diff changeset
   186
        Document.Node.Header(imports, header.keywords, header.abbrevs)
57615
df1b3452d71c more explicit discrimination of empty nodes -- suppress from Theories panel;
wenzelm
parents: 56913
diff changeset
   187
      }
df1b3452d71c more explicit discrimination of empty nodes -- suppress from Theories panel;
wenzelm
parents: 56913
diff changeset
   188
      catch { case exn: Throwable => Document.Node.bad_header(Exn.message(exn)) }
50414
e17a1f179bb0 explore theory_body_files via future, for improved performance;
wenzelm
parents: 50291
diff changeset
   189
    }
57615
df1b3452d71c more explicit discrimination of empty nodes -- suppress from Theories panel;
wenzelm
parents: 56913
diff changeset
   190
    else Document.Node.no_header
46737
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
   191
  }
46748
8f3ae4d04a2d refined node_header -- more direct buffer access (again);
wenzelm
parents: 46737
diff changeset
   192
65359
9ca34f0407a9 provide session qualifier via resources;
wenzelm
parents: 65358
diff changeset
   193
  def check_thy(name: Document.Node.Name, start: Token.Pos = Token.Pos.command,
9ca34f0407a9 provide session qualifier via resources;
wenzelm
parents: 65358
diff changeset
   194
      strict: Boolean = true): Document.Node.Header =
9ca34f0407a9 provide session qualifier via resources;
wenzelm
parents: 65358
diff changeset
   195
    with_thy_reader(name, check_thy_reader(name, _, start, strict))
50566
b43c4f660320 tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents: 50415
diff changeset
   196
b43c4f660320 tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents: 50415
diff changeset
   197
64673
b5965890e54d more uniform treatment of file name vs. theory name and special header;
wenzelm
parents: 64657
diff changeset
   198
  /* special header */
b5965890e54d more uniform treatment of file name vs. theory name and special header;
wenzelm
parents: 64657
diff changeset
   199
b5965890e54d more uniform treatment of file name vs. theory name and special header;
wenzelm
parents: 64657
diff changeset
   200
  def special_header(name: Document.Node.Name): Option[Document.Node.Header] =
65445
e9e7f5f5794c more qualifier treatment, but in the end it is still ignored;
wenzelm
parents: 65441
diff changeset
   201
  {
e9e7f5f5794c more qualifier treatment, but in the end it is still ignored;
wenzelm
parents: 65441
diff changeset
   202
    val imports =
65489
f3076367f4a8 tuned signature;
wenzelm
parents: 65488
diff changeset
   203
      if (Thy_Header.is_ml_root(name.theory)) List(import_name(name, Thy_Header.ML_BOOTSTRAP))
f3076367f4a8 tuned signature;
wenzelm
parents: 65488
diff changeset
   204
      else if (Thy_Header.is_bootstrap(name.theory)) List(import_name(name, Thy_Header.PURE))
65445
e9e7f5f5794c more qualifier treatment, but in the end it is still ignored;
wenzelm
parents: 65441
diff changeset
   205
      else Nil
e9e7f5f5794c more qualifier treatment, but in the end it is still ignored;
wenzelm
parents: 65441
diff changeset
   206
    if (imports.isEmpty) None
e9e7f5f5794c more qualifier treatment, but in the end it is still ignored;
wenzelm
parents: 65441
diff changeset
   207
    else Some(Document.Node.Header(imports.map((_, Position.none))))
e9e7f5f5794c more qualifier treatment, but in the end it is still ignored;
wenzelm
parents: 65441
diff changeset
   208
  }
64673
b5965890e54d more uniform treatment of file name vs. theory name and special header;
wenzelm
parents: 64657
diff changeset
   209
b5965890e54d more uniform treatment of file name vs. theory name and special header;
wenzelm
parents: 64657
diff changeset
   210
64797
wenzelm
parents: 64759
diff changeset
   211
  /* blobs */
wenzelm
parents: 64759
diff changeset
   212
wenzelm
parents: 64759
diff changeset
   213
  def undefined_blobs(nodes: Document.Nodes): List[Document.Node.Name] =
wenzelm
parents: 64759
diff changeset
   214
    (for {
wenzelm
parents: 64759
diff changeset
   215
      (node_name, node) <- nodes.iterator
65361
ecefb68dc21d tuned signature;
wenzelm
parents: 65359
diff changeset
   216
      if !session_base.loaded_theory(node_name)
64797
wenzelm
parents: 64759
diff changeset
   217
      cmd <- node.load_commands.iterator
wenzelm
parents: 64759
diff changeset
   218
      name <- cmd.blobs_undefined.iterator
wenzelm
parents: 64759
diff changeset
   219
    } yield name).toList
wenzelm
parents: 64759
diff changeset
   220
wenzelm
parents: 64759
diff changeset
   221
56316
b1cf8ddc2e04 propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents: 56315
diff changeset
   222
  /* document changes */
50566
b43c4f660320 tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents: 50415
diff changeset
   223
56316
b1cf8ddc2e04 propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents: 56315
diff changeset
   224
  def parse_change(
56315
c20053f67093 tuned signature;
wenzelm
parents: 56314
diff changeset
   225
      reparse_limit: Int,
c20053f67093 tuned signature;
wenzelm
parents: 56314
diff changeset
   226
      previous: Document.Version,
c20053f67093 tuned signature;
wenzelm
parents: 56314
diff changeset
   227
      doc_blobs: Document.Blobs,
c20053f67093 tuned signature;
wenzelm
parents: 56314
diff changeset
   228
      edits: List[Document.Edit_Text]): Session.Change =
66959
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   229
    Thy_Syntax.parse_change(resources, reparse_limit, previous, doc_blobs, edits)
55134
1b67b17cdad5 propagate update of outer syntax keywords: global propertiesChanged, buffer TokenMarker.markTokens, text area repainting;
wenzelm
parents: 54521
diff changeset
   230
56316
b1cf8ddc2e04 propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents: 56315
diff changeset
   231
  def commit(change: Session.Change) { }
66959
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   232
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   233
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   234
  /* theory and file dependencies */
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   235
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   236
  object Dependencies
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   237
  {
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   238
    val empty = new Dependencies(Nil, Set.empty)
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   239
  }
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   240
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   241
  final class Dependencies private(
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   242
    rev_entries: List[Document.Node.Entry],
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   243
    val seen: Set[Document.Node.Name])
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   244
  {
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   245
    def :: (entry: Document.Node.Entry): Dependencies =
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   246
      new Dependencies(entry :: rev_entries, seen)
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   247
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   248
    def + (name: Document.Node.Name): Dependencies =
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   249
      new Dependencies(rev_entries, seen + name)
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   250
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   251
    def entries: List[Document.Node.Entry] = rev_entries.reverse
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   252
    def names: List[Document.Node.Name] = entries.map(_.name)
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   253
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   254
    def errors: List[String] = entries.flatMap(_.header.errors)
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   255
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   256
    lazy val loaded_theories: Graph[String, Outer_Syntax] =
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   257
      (session_base.loaded_theories /: entries)({ case (graph, entry) =>
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   258
        val name = entry.name.theory
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   259
        val imports = entry.header.imports.map(p => p._1.theory)
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   260
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   261
        val graph1 = (graph /: (name :: imports))(_.default_node(_, Outer_Syntax.empty))
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   262
        val graph2 = (graph1 /: imports)(_.add_edge(_, name))
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   263
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   264
        val syntax0 = if (name == Thy_Header.PURE) List(Thy_Header.bootstrap_syntax) else Nil
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   265
        val syntax1 = (name :: graph2.imm_preds(name).toList).map(graph2.get_node(_))
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   266
        val syntax = Outer_Syntax.merge(syntax0 ::: syntax1) + entry.header
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   267
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   268
        graph2.map_node(name, _ => syntax)
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   269
      })
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   270
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   271
    def loaded_files: List[(String, List[Path])] =
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   272
    {
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   273
      names.map(_.theory) zip
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   274
        Par_List.map((e: () => List[Path]) => e(),
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   275
          names.map(name => resources.loaded_files(loaded_theories.get_node(name.theory), name)))
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   276
    }
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   277
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   278
    def imported_files: List[Path] =
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   279
    {
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   280
      val base_theories =
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   281
        loaded_theories.all_preds(names.map(_.theory)).
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   282
          filter(session_base.loaded_theories.defined(_))
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   283
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   284
      base_theories.map(theory => session_base.known.theories(theory).path) :::
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   285
      base_theories.flatMap(session_base.known.loaded_files(_))
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   286
    }
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   287
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   288
    lazy val overall_syntax: Outer_Syntax =
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   289
      Outer_Syntax.merge(loaded_theories.maximals.map(loaded_theories.get_node(_)))
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   290
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   291
    override def toString: String = entries.toString
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   292
  }
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   293
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   294
  private def show_path(names: List[Document.Node.Name]): String =
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   295
    names.map(name => quote(name.theory)).mkString(" via ")
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   296
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   297
  private def cycle_msg(names: List[Document.Node.Name]): String =
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   298
    "Cyclic dependency of " + show_path(names)
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   299
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   300
  private def required_by(initiators: List[Document.Node.Name]): String =
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   301
    if (initiators.isEmpty) ""
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   302
    else "\n(required by " + show_path(initiators.reverse) + ")"
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   303
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   304
  private def require_thy(initiators: List[Document.Node.Name], required: Dependencies,
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   305
    thy: (Document.Node.Name, Position.T)): Dependencies =
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   306
  {
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   307
    val (name, pos) = thy
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   308
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   309
    def message: String =
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   310
      "The error(s) above occurred for theory " + quote(name.theory) +
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   311
        required_by(initiators) + Position.here(pos)
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   312
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   313
    val required1 = required + name
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   314
    if (required.seen(name)) required
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   315
    else if (session_base.loaded_theory(name)) required1
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   316
    else {
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   317
      try {
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   318
        if (initiators.contains(name)) error(cycle_msg(initiators))
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   319
        val header =
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   320
          try { check_thy(name, Token.Pos.file(name.node)).cat_errors(message) }
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   321
          catch { case ERROR(msg) => cat_error(msg, message) }
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   322
        Document.Node.Entry(name, header) ::
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   323
          require_thys(name :: initiators, required1, header.imports)
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   324
      }
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   325
      catch {
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   326
        case e: Throwable =>
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   327
          Document.Node.Entry(name, Document.Node.bad_header(Exn.message(e))) :: required1
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   328
      }
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   329
    }
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   330
  }
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   331
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   332
  private def require_thys(initiators: List[Document.Node.Name], required: Dependencies,
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   333
      thys: List[(Document.Node.Name, Position.T)]): Dependencies =
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   334
    (required /: thys)(require_thy(initiators, _, _))
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   335
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   336
  def dependencies(thys: List[(Document.Node.Name, Position.T)]): Dependencies =
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   337
    require_thys(Nil, Dependencies.empty, thys)
43651
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
   338
}