src/Pure/PIDE/resources.scala
author wenzelm
Wed Mar 15 11:07:07 2017 +0100 (2017-03-15)
changeset 65255 d388e63a46fc
parent 65250 13a6c81534a8
child 65355 403eabd73c9a
permissions -rw-r--r--
unused;
wenzelm@56208
     1
/*  Title:      Pure/PIDE/resources.scala
wenzelm@43651
     2
    Author:     Makarius
wenzelm@43651
     3
wenzelm@56208
     4
Resources for theories and auxiliary files.
wenzelm@43651
     5
*/
wenzelm@43651
     6
wenzelm@43651
     7
package isabelle
wenzelm@43651
     8
wenzelm@44953
     9
wenzelm@48885
    10
import scala.annotation.tailrec
wenzelm@56823
    11
import scala.util.parsing.input.Reader
wenzelm@48885
    12
wenzelm@48409
    13
import java.io.{File => JFile}
wenzelm@44953
    14
wenzelm@44953
    15
wenzelm@64856
    16
class Resources(val base: Sessions.Base, val log: Logger = No_Logger)
wenzelm@43651
    17
{
wenzelm@64839
    18
  val thy_info = new Thy_Info(this)
wenzelm@64839
    19
wenzelm@65250
    20
  def thy_path(path: Path): Path = path.ext("thy")
wenzelm@65250
    21
wenzelm@64839
    22
wenzelm@54514
    23
  /* document node names */
wenzelm@54514
    24
wenzelm@56801
    25
  def node_name(qualifier: String, raw_path: Path): Document.Node.Name =
wenzelm@54514
    26
  {
wenzelm@56913
    27
    val no_qualifier = "" // FIXME
wenzelm@54514
    28
    val path = raw_path.expand
wenzelm@54514
    29
    val node = path.implode
wenzelm@56913
    30
    val theory = Long_Name.qualify(no_qualifier, Thy_Header.thy_name(node).getOrElse(""))
wenzelm@54515
    31
    val master_dir = if (theory == "") "" else path.dir.implode
wenzelm@54515
    32
    Document.Node.Name(node, master_dir, theory)
wenzelm@54514
    33
  }
wenzelm@54514
    34
wenzelm@54514
    35
wenzelm@44953
    36
  /* file-system operations */
wenzelm@44953
    37
wenzelm@44953
    38
  def append(dir: String, source_path: Path): String =
wenzelm@48711
    39
    (Path.explode(dir) + source_path).expand.implode
wenzelm@44953
    40
wenzelm@64654
    41
  def append_file(dir: String, raw_name: String): String =
wenzelm@64654
    42
    if (Path.is_valid(raw_name)) append(dir, Path.explode(raw_name))
wenzelm@64654
    43
    else raw_name
wenzelm@64654
    44
wenzelm@64654
    45
wenzelm@46737
    46
wenzelm@64657
    47
  /* source files of Isabelle/ML bootstrap */
wenzelm@64657
    48
wenzelm@64657
    49
  def source_file(raw_name: String): Option[String] =
wenzelm@64657
    50
  {
wenzelm@64657
    51
    if (Path.is_wellformed(raw_name)) {
wenzelm@64657
    52
      if (Path.is_valid(raw_name)) {
wenzelm@64657
    53
        def check(p: Path): Option[Path] = if (p.is_file) Some(p) else None
wenzelm@64657
    54
wenzelm@64657
    55
        val path = Path.explode(raw_name)
wenzelm@64657
    56
        val path1 =
wenzelm@64657
    57
          if (path.is_absolute || path.is_current) check(path)
wenzelm@64657
    58
          else {
wenzelm@64657
    59
            check(Path.explode("~~/src/Pure") + path) orElse
wenzelm@64657
    60
              (if (Isabelle_System.getenv("ML_SOURCES") == "") None
wenzelm@64657
    61
               else check(Path.explode("$ML_SOURCES") + path))
wenzelm@64657
    62
          }
wenzelm@64657
    63
        Some(File.platform_path(path1 getOrElse path))
wenzelm@64657
    64
      }
wenzelm@64657
    65
      else None
wenzelm@64657
    66
    }
wenzelm@64657
    67
    else Some(raw_name)
wenzelm@64657
    68
  }
wenzelm@64657
    69
wenzelm@64657
    70
wenzelm@46737
    71
  /* theory files */
wenzelm@46737
    72
wenzelm@63584
    73
  def loaded_files(syntax: Outer_Syntax, text: String): List[String] =
wenzelm@56393
    74
    if (syntax.load_commands_in(text)) {
wenzelm@57906
    75
      val spans = syntax.parse_spans(text)
wenzelm@59689
    76
      spans.iterator.map(Command.span_files(syntax, _)._1).flatten.toList
wenzelm@56392
    77
    }
wenzelm@56392
    78
    else Nil
wenzelm@48885
    79
wenzelm@56801
    80
  private def dummy_name(theory: String): Document.Node.Name =
wenzelm@56801
    81
    Document.Node.Name(theory + ".thy", "", theory)
wenzelm@56801
    82
wenzelm@56801
    83
  def import_name(qualifier: String, master: Document.Node.Name, s: String): Document.Node.Name =
wenzelm@51634
    84
  {
wenzelm@56913
    85
    val no_qualifier = "" // FIXME
wenzelm@56801
    86
    val thy1 = Thy_Header.base_name(s)
wenzelm@56913
    87
    val thy2 = if (Long_Name.is_qualified(thy1)) thy1 else Long_Name.qualify(no_qualifier, thy1)
wenzelm@64854
    88
    (base.known_theories.get(thy1) orElse
wenzelm@64854
    89
     base.known_theories.get(thy2) orElse
wenzelm@64854
    90
     base.known_theories.get(Long_Name.base_name(thy1))) match {
wenzelm@64854
    91
      case Some(name) if base.loaded_theories(name.theory) => dummy_name(name.theory)
wenzelm@56801
    92
      case Some(name) => name
wenzelm@56801
    93
      case None =>
wenzelm@56801
    94
        val path = Path.explode(s)
wenzelm@56801
    95
        val theory = path.base.implode
wenzelm@56801
    96
        if (Long_Name.is_qualified(theory)) dummy_name(theory)
wenzelm@56801
    97
        else {
wenzelm@65250
    98
          val node = append(master.master_dir, thy_path(path))
wenzelm@56801
    99
          val master_dir = append(master.master_dir, path.dir)
wenzelm@56913
   100
          Document.Node.Name(node, master_dir, Long_Name.qualify(no_qualifier, theory))
wenzelm@56801
   101
        }
wenzelm@51634
   102
    }
wenzelm@51634
   103
  }
wenzelm@51634
   104
wenzelm@64656
   105
  def with_thy_reader[A](name: Document.Node.Name, f: Reader[Char] => A): A =
wenzelm@64656
   106
  {
wenzelm@64656
   107
    val path = Path.explode(name.node)
wenzelm@64656
   108
    if (!path.is_file) error("No such file: " + path.toString)
wenzelm@64656
   109
wenzelm@64656
   110
    val reader = Scan.byte_reader(path.file)
wenzelm@64656
   111
    try { f(reader) } finally { reader.close }
wenzelm@64656
   112
  }
wenzelm@64656
   113
wenzelm@59705
   114
  def check_thy_reader(qualifier: String, node_name: Document.Node.Name,
wenzelm@64825
   115
      reader: Reader[Char], start: Token.Pos = Token.Pos.command, strict: Boolean = true)
wenzelm@64825
   116
    : Document.Node.Header =
wenzelm@46737
   117
  {
wenzelm@64826
   118
    if (node_name.is_theory && reader.source.length > 0) {
wenzelm@57615
   119
      try {
wenzelm@64825
   120
        val header = Thy_Header.read(reader, start, strict).decode_symbols
wenzelm@48885
   121
wenzelm@59694
   122
        val base_name = Long_Name.base_name(node_name.theory)
wenzelm@59694
   123
        val (name, pos) = header.name
wenzelm@59694
   124
        if (base_name != name)
wenzelm@59718
   125
          error("Bad theory name " + quote(name) +
wenzelm@65250
   126
            " for file " + thy_path(Path.basic(base_name)) + Position.here(pos) +
wenzelm@59718
   127
            Completion.report_names(pos, 1, List((base_name, ("theory", base_name)))))
wenzelm@48885
   128
wenzelm@59694
   129
        val imports =
wenzelm@59695
   130
          header.imports.map({ case (s, pos) => (import_name(qualifier, node_name, s), pos) })
wenzelm@63579
   131
        Document.Node.Header(imports, header.keywords, header.abbrevs)
wenzelm@57615
   132
      }
wenzelm@57615
   133
      catch { case exn: Throwable => Document.Node.bad_header(Exn.message(exn)) }
wenzelm@50414
   134
    }
wenzelm@57615
   135
    else Document.Node.no_header
wenzelm@46737
   136
  }
wenzelm@46748
   137
wenzelm@64825
   138
  def check_thy(qualifier: String, name: Document.Node.Name,
wenzelm@64825
   139
      start: Token.Pos = Token.Pos.command, strict: Boolean = true): Document.Node.Header =
wenzelm@64825
   140
    with_thy_reader(name, check_thy_reader(qualifier, name, _, start, strict))
wenzelm@50566
   141
wenzelm@50566
   142
wenzelm@64673
   143
  /* special header */
wenzelm@64673
   144
wenzelm@64673
   145
  def special_header(name: Document.Node.Name): Option[Document.Node.Header] =
wenzelm@64673
   146
    if (Thy_Header.is_ml_root(name.theory))
wenzelm@64673
   147
      Some(Document.Node.Header(List((import_name("", name, Thy_Header.ML_BOOTSTRAP), Position.none))))
wenzelm@64673
   148
    else if (Thy_Header.is_bootstrap(name.theory))
wenzelm@64673
   149
      Some(Document.Node.Header(List((import_name("", name, Thy_Header.PURE), Position.none))))
wenzelm@64673
   150
    else None
wenzelm@64673
   151
wenzelm@64673
   152
wenzelm@64797
   153
  /* blobs */
wenzelm@64797
   154
wenzelm@64797
   155
  def undefined_blobs(nodes: Document.Nodes): List[Document.Node.Name] =
wenzelm@64797
   156
    (for {
wenzelm@64797
   157
      (node_name, node) <- nodes.iterator
wenzelm@64854
   158
      if !base.loaded_theories(node_name.theory)
wenzelm@64797
   159
      cmd <- node.load_commands.iterator
wenzelm@64797
   160
      name <- cmd.blobs_undefined.iterator
wenzelm@64797
   161
    } yield name).toList
wenzelm@64797
   162
wenzelm@64797
   163
wenzelm@56316
   164
  /* document changes */
wenzelm@50566
   165
wenzelm@56316
   166
  def parse_change(
wenzelm@56315
   167
      reparse_limit: Int,
wenzelm@56315
   168
      previous: Document.Version,
wenzelm@56315
   169
      doc_blobs: Document.Blobs,
wenzelm@56315
   170
      edits: List[Document.Edit_Text]): Session.Change =
wenzelm@56316
   171
    Thy_Syntax.parse_change(this, reparse_limit, previous, doc_blobs, edits)
wenzelm@55134
   172
wenzelm@56316
   173
  def commit(change: Session.Change) { }
wenzelm@43651
   174
}