src/Pure/PIDE/resources.scala
author wenzelm
Wed, 12 Apr 2017 22:47:21 +0200
changeset 65472 f83081bcdd0e
parent 65471 05e5bffcf1d8
child 65476 a72ae9eb4462
permissions -rw-r--r--
tuned;
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,
65445
e9e7f5f5794c more qualifier treatment, but in the end it is still ignored;
wenzelm
parents: 65441
diff changeset
    18
  val default_qualifier: String = Sessions.DRAFT,
65359
9ca34f0407a9 provide session qualifier via resources;
wenzelm
parents: 65358
diff changeset
    19
  val log: Logger = No_Logger)
43651
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    20
{
64839
842163abfc0d tuned signature;
wenzelm
parents: 64835
diff changeset
    21
  val thy_info = new Thy_Info(this)
842163abfc0d tuned signature;
wenzelm
parents: 64835
diff changeset
    22
65250
13a6c81534a8 tuned signature;
wenzelm
parents: 64856
diff changeset
    23
  def thy_path(path: Path): Path = path.ext("thy")
13a6c81534a8 tuned signature;
wenzelm
parents: 64856
diff changeset
    24
64839
842163abfc0d tuned signature;
wenzelm
parents: 64835
diff changeset
    25
44953
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    26
  /* file-system operations */
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    27
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    28
  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
    29
    (Path.explode(dir) + source_path).expand.implode
44953
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    30
64654
31b681e38c70 clarified signature;
wenzelm
parents: 63584
diff changeset
    31
  def append_file(dir: String, raw_name: String): String =
31b681e38c70 clarified signature;
wenzelm
parents: 63584
diff changeset
    32
    if (Path.is_valid(raw_name)) append(dir, Path.explode(raw_name))
31b681e38c70 clarified signature;
wenzelm
parents: 63584
diff changeset
    33
    else raw_name
31b681e38c70 clarified signature;
wenzelm
parents: 63584
diff changeset
    34
31b681e38c70 clarified signature;
wenzelm
parents: 63584
diff changeset
    35
46737
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    36
64657
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    37
  /* source files of Isabelle/ML bootstrap */
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    38
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    39
  def source_file(raw_name: String): Option[String] =
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    40
  {
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    41
    if (Path.is_wellformed(raw_name)) {
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    42
      if (Path.is_valid(raw_name)) {
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    43
        def check(p: Path): Option[Path] = if (p.is_file) Some(p) else None
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    44
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    45
        val path = Path.explode(raw_name)
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    46
        val path1 =
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    47
          if (path.is_absolute || path.is_current) check(path)
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    48
          else {
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    49
            check(Path.explode("~~/src/Pure") + path) orElse
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    50
              (if (Isabelle_System.getenv("ML_SOURCES") == "") None
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    51
               else check(Path.explode("$ML_SOURCES") + path))
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    52
          }
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    53
        Some(File.platform_path(path1 getOrElse path))
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    54
      }
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    55
      else None
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    56
    }
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    57
    else Some(raw_name)
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    58
  }
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    59
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    60
46737
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    61
  /* theory files */
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    62
63584
68751fe1c036 tuned signature -- prover-independence is presently theoretical;
wenzelm
parents: 63579
diff changeset
    63
  def loaded_files(syntax: Outer_Syntax, text: String): List[String] =
56393
22f533e6a049 more abstract Prover.Syntax, as proposed by Carst Tankink;
wenzelm
parents: 56392
diff changeset
    64
    if (syntax.load_commands_in(text)) {
57906
020df63dd0a9 tuned signature;
wenzelm
parents: 57905
diff changeset
    65
      val spans = syntax.parse_spans(text)
59689
7968c57ea240 simplified Command.resolve_files in ML, using blobs_index from Scala;
wenzelm
parents: 59683
diff changeset
    66
      spans.iterator.map(Command.span_files(syntax, _)._1).flatten.toList
56392
bc118a32a870 tuned signature (see also 0850d43cb355);
wenzelm
parents: 56387
diff changeset
    67
    }
bc118a32a870 tuned signature (see also 0850d43cb355);
wenzelm
parents: 56387
diff changeset
    68
    else Nil
48885
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    69
65445
e9e7f5f5794c more qualifier treatment, but in the end it is still ignored;
wenzelm
parents: 65441
diff changeset
    70
  def theory_qualifier(name: Document.Node.Name): String =
65457
2bf0d2fcd506 proper import qualifier for global theories;
wenzelm
parents: 65452
diff changeset
    71
    session_base.global_theories.getOrElse(name.theory, Long_Name.qualifier(name.theory))
65445
e9e7f5f5794c more qualifier treatment, but in the end it is still ignored;
wenzelm
parents: 65441
diff changeset
    72
65472
wenzelm
parents: 65471
diff changeset
    73
  def loaded_theory_name(qualifier: String, theory0: String): (Boolean, String) =
65471
05e5bffcf1d8 clarified loaded_theories: map to qualified theory name;
wenzelm
parents: 65467
diff changeset
    74
    session_base.loaded_theories.get(theory0) match {
05e5bffcf1d8 clarified loaded_theories: map to qualified theory name;
wenzelm
parents: 65467
diff changeset
    75
      case Some(theory) => (true, theory)
05e5bffcf1d8 clarified loaded_theories: map to qualified theory name;
wenzelm
parents: 65467
diff changeset
    76
      case None =>
05e5bffcf1d8 clarified loaded_theories: map to qualified theory name;
wenzelm
parents: 65467
diff changeset
    77
        val theory =
05e5bffcf1d8 clarified loaded_theories: map to qualified theory name;
wenzelm
parents: 65467
diff changeset
    78
          if (Long_Name.is_qualified(theory0) || session_base.global_theories.isDefinedAt(theory0)
05e5bffcf1d8 clarified loaded_theories: map to qualified theory name;
wenzelm
parents: 65467
diff changeset
    79
              || true /* FIXME */) theory0
05e5bffcf1d8 clarified loaded_theories: map to qualified theory name;
wenzelm
parents: 65467
diff changeset
    80
          else Long_Name.qualify(qualifier, theory0)
05e5bffcf1d8 clarified loaded_theories: map to qualified theory name;
wenzelm
parents: 65467
diff changeset
    81
        (false, theory)
05e5bffcf1d8 clarified loaded_theories: map to qualified theory name;
wenzelm
parents: 65467
diff changeset
    82
    }
05e5bffcf1d8 clarified loaded_theories: map to qualified theory name;
wenzelm
parents: 65467
diff changeset
    83
65445
e9e7f5f5794c more qualifier treatment, but in the end it is still ignored;
wenzelm
parents: 65441
diff changeset
    84
  def import_name(qualifier: String, dir: String, s: String): Document.Node.Name =
65472
wenzelm
parents: 65471
diff changeset
    85
    loaded_theory_name(qualifier, Thy_Header.import_name(s)) match {
wenzelm
parents: 65471
diff changeset
    86
      case (true, theory) => Document.Node.Name.loaded_theory(theory)
wenzelm
parents: 65471
diff changeset
    87
      case (false, theory) =>
wenzelm
parents: 65471
diff changeset
    88
        session_base.known_theories.get(theory) match {
wenzelm
parents: 65471
diff changeset
    89
          case Some(node_name) => node_name
wenzelm
parents: 65471
diff changeset
    90
          case None =>
wenzelm
parents: 65471
diff changeset
    91
            val path = Path.explode(s)
wenzelm
parents: 65471
diff changeset
    92
            val node = append(dir, thy_path(path))
wenzelm
parents: 65471
diff changeset
    93
            val master_dir = append(dir, path.dir)
wenzelm
parents: 65471
diff changeset
    94
            Document.Node.Name(node, master_dir, theory)
wenzelm
parents: 65471
diff changeset
    95
        }
wenzelm
parents: 65471
diff changeset
    96
    }
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
    97
64656
wenzelm
parents: 64654
diff changeset
    98
  def with_thy_reader[A](name: Document.Node.Name, f: Reader[Char] => A): A =
wenzelm
parents: 64654
diff changeset
    99
  {
wenzelm
parents: 64654
diff changeset
   100
    val path = Path.explode(name.node)
wenzelm
parents: 64654
diff changeset
   101
    if (!path.is_file) error("No such file: " + path.toString)
wenzelm
parents: 64654
diff changeset
   102
wenzelm
parents: 64654
diff changeset
   103
    val reader = Scan.byte_reader(path.file)
wenzelm
parents: 64654
diff changeset
   104
    try { f(reader) } finally { reader.close }
wenzelm
parents: 64654
diff changeset
   105
  }
wenzelm
parents: 64654
diff changeset
   106
65359
9ca34f0407a9 provide session qualifier via resources;
wenzelm
parents: 65358
diff changeset
   107
  def check_thy_reader(node_name: Document.Node.Name, reader: Reader[Char],
9ca34f0407a9 provide session qualifier via resources;
wenzelm
parents: 65358
diff changeset
   108
    start: Token.Pos = Token.Pos.command, strict: Boolean = true): Document.Node.Header =
46737
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
   109
  {
64826
c97296294f6d clarified check_thy_reader: check node_name here;
wenzelm
parents: 64825
diff changeset
   110
    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
   111
      try {
64825
e78b62c289bb more uniform node_header (non-strict);
wenzelm
parents: 64823
diff changeset
   112
        val header = Thy_Header.read(reader, start, strict).decode_symbols
48885
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
   113
65439
862bfd2b4fd4 tuned signature;
wenzelm
parents: 65429
diff changeset
   114
        val base_name = node_name.theory_base_name
59694
d2bb4b5ed862 misc tuning -- more uniform ML vs. Scala;
wenzelm
parents: 59691
diff changeset
   115
        val (name, pos) = header.name
d2bb4b5ed862 misc tuning -- more uniform ML vs. Scala;
wenzelm
parents: 59691
diff changeset
   116
        if (base_name != name)
59718
5d0c539537c9 tuned message -- include completion;
wenzelm
parents: 59705
diff changeset
   117
          error("Bad theory name " + quote(name) +
65250
13a6c81534a8 tuned signature;
wenzelm
parents: 64856
diff changeset
   118
            " for file " + thy_path(Path.basic(base_name)) + Position.here(pos) +
59718
5d0c539537c9 tuned message -- include completion;
wenzelm
parents: 59705
diff changeset
   119
            Completion.report_names(pos, 1, List((base_name, ("theory", base_name)))))
48885
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
   120
59694
d2bb4b5ed862 misc tuning -- more uniform ML vs. Scala;
wenzelm
parents: 59691
diff changeset
   121
        val imports =
65445
e9e7f5f5794c more qualifier treatment, but in the end it is still ignored;
wenzelm
parents: 65441
diff changeset
   122
          header.imports.map({ case (s, pos) =>
e9e7f5f5794c more qualifier treatment, but in the end it is still ignored;
wenzelm
parents: 65441
diff changeset
   123
            (import_name(theory_qualifier(node_name), node_name.master_dir, s), pos) })
63579
73939a9b70a3 support 'abbrevs' within theory header;
wenzelm
parents: 63020
diff changeset
   124
        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
   125
      }
df1b3452d71c more explicit discrimination of empty nodes -- suppress from Theories panel;
wenzelm
parents: 56913
diff changeset
   126
      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
   127
    }
57615
df1b3452d71c more explicit discrimination of empty nodes -- suppress from Theories panel;
wenzelm
parents: 56913
diff changeset
   128
    else Document.Node.no_header
46737
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
   129
  }
46748
8f3ae4d04a2d refined node_header -- more direct buffer access (again);
wenzelm
parents: 46737
diff changeset
   130
65359
9ca34f0407a9 provide session qualifier via resources;
wenzelm
parents: 65358
diff changeset
   131
  def check_thy(name: Document.Node.Name, start: Token.Pos = Token.Pos.command,
9ca34f0407a9 provide session qualifier via resources;
wenzelm
parents: 65358
diff changeset
   132
      strict: Boolean = true): Document.Node.Header =
9ca34f0407a9 provide session qualifier via resources;
wenzelm
parents: 65358
diff changeset
   133
    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
   134
b43c4f660320 tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents: 50415
diff changeset
   135
64673
b5965890e54d more uniform treatment of file name vs. theory name and special header;
wenzelm
parents: 64657
diff changeset
   136
  /* special header */
b5965890e54d more uniform treatment of file name vs. theory name and special header;
wenzelm
parents: 64657
diff changeset
   137
b5965890e54d more uniform treatment of file name vs. theory name and special header;
wenzelm
parents: 64657
diff changeset
   138
  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
   139
  {
e9e7f5f5794c more qualifier treatment, but in the end it is still ignored;
wenzelm
parents: 65441
diff changeset
   140
    val qualifier = theory_qualifier(name)
e9e7f5f5794c more qualifier treatment, but in the end it is still ignored;
wenzelm
parents: 65441
diff changeset
   141
    val dir = name.master_dir
e9e7f5f5794c more qualifier treatment, but in the end it is still ignored;
wenzelm
parents: 65441
diff changeset
   142
e9e7f5f5794c more qualifier treatment, but in the end it is still ignored;
wenzelm
parents: 65441
diff changeset
   143
    val imports =
e9e7f5f5794c more qualifier treatment, but in the end it is still ignored;
wenzelm
parents: 65441
diff changeset
   144
      if (Thy_Header.is_ml_root(name.theory))
e9e7f5f5794c more qualifier treatment, but in the end it is still ignored;
wenzelm
parents: 65441
diff changeset
   145
        List(import_name(qualifier, dir, Thy_Header.ML_BOOTSTRAP))
e9e7f5f5794c more qualifier treatment, but in the end it is still ignored;
wenzelm
parents: 65441
diff changeset
   146
      else if (Thy_Header.is_bootstrap(name.theory))
e9e7f5f5794c more qualifier treatment, but in the end it is still ignored;
wenzelm
parents: 65441
diff changeset
   147
        List(import_name(qualifier, dir, Thy_Header.PURE))
e9e7f5f5794c more qualifier treatment, but in the end it is still ignored;
wenzelm
parents: 65441
diff changeset
   148
      else Nil
e9e7f5f5794c more qualifier treatment, but in the end it is still ignored;
wenzelm
parents: 65441
diff changeset
   149
e9e7f5f5794c more qualifier treatment, but in the end it is still ignored;
wenzelm
parents: 65441
diff changeset
   150
    if (imports.isEmpty) None
e9e7f5f5794c more qualifier treatment, but in the end it is still ignored;
wenzelm
parents: 65441
diff changeset
   151
    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
   152
  }
64673
b5965890e54d more uniform treatment of file name vs. theory name and special header;
wenzelm
parents: 64657
diff changeset
   153
b5965890e54d more uniform treatment of file name vs. theory name and special header;
wenzelm
parents: 64657
diff changeset
   154
64797
wenzelm
parents: 64759
diff changeset
   155
  /* blobs */
wenzelm
parents: 64759
diff changeset
   156
wenzelm
parents: 64759
diff changeset
   157
  def undefined_blobs(nodes: Document.Nodes): List[Document.Node.Name] =
wenzelm
parents: 64759
diff changeset
   158
    (for {
wenzelm
parents: 64759
diff changeset
   159
      (node_name, node) <- nodes.iterator
65361
ecefb68dc21d tuned signature;
wenzelm
parents: 65359
diff changeset
   160
      if !session_base.loaded_theory(node_name)
64797
wenzelm
parents: 64759
diff changeset
   161
      cmd <- node.load_commands.iterator
wenzelm
parents: 64759
diff changeset
   162
      name <- cmd.blobs_undefined.iterator
wenzelm
parents: 64759
diff changeset
   163
    } yield name).toList
wenzelm
parents: 64759
diff changeset
   164
wenzelm
parents: 64759
diff changeset
   165
56316
b1cf8ddc2e04 propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents: 56315
diff changeset
   166
  /* document changes */
50566
b43c4f660320 tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents: 50415
diff changeset
   167
56316
b1cf8ddc2e04 propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents: 56315
diff changeset
   168
  def parse_change(
56315
c20053f67093 tuned signature;
wenzelm
parents: 56314
diff changeset
   169
      reparse_limit: Int,
c20053f67093 tuned signature;
wenzelm
parents: 56314
diff changeset
   170
      previous: Document.Version,
c20053f67093 tuned signature;
wenzelm
parents: 56314
diff changeset
   171
      doc_blobs: Document.Blobs,
c20053f67093 tuned signature;
wenzelm
parents: 56314
diff changeset
   172
      edits: List[Document.Edit_Text]): Session.Change =
56316
b1cf8ddc2e04 propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents: 56315
diff changeset
   173
    Thy_Syntax.parse_change(this, 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
   174
56316
b1cf8ddc2e04 propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents: 56315
diff changeset
   175
  def commit(change: Session.Change) { }
43651
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
   176
}