src/Pure/PIDE/resources.scala
author wenzelm
Fri Mar 13 12:58:49 2015 +0100 (2015-03-13)
changeset 59689 7968c57ea240
parent 59683 d6824d8490be
child 59691 f6ff19188842
permissions -rw-r--r--
simplified Command.resolve_files in ML, using blobs_index from Scala;
clarified modules;
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@56208
    16
object Resources
wenzelm@48422
    17
{
wenzelm@48422
    18
  def thy_path(path: Path): Path = path.ext("thy")
wenzelm@48422
    19
}
wenzelm@44953
    20
wenzelm@48422
    21
wenzelm@56801
    22
class Resources(
wenzelm@56801
    23
  val loaded_theories: Set[String],
wenzelm@56801
    24
  val known_theories: Map[String, Document.Node.Name],
wenzelm@56801
    25
  val base_syntax: Prover.Syntax)
wenzelm@43651
    26
{
wenzelm@54514
    27
  /* document node names */
wenzelm@54514
    28
wenzelm@56801
    29
  def node_name(qualifier: String, raw_path: Path): Document.Node.Name =
wenzelm@54514
    30
  {
wenzelm@56913
    31
    val no_qualifier = "" // FIXME
wenzelm@54514
    32
    val path = raw_path.expand
wenzelm@54514
    33
    val node = path.implode
wenzelm@56913
    34
    val theory = Long_Name.qualify(no_qualifier, Thy_Header.thy_name(node).getOrElse(""))
wenzelm@54515
    35
    val master_dir = if (theory == "") "" else path.dir.implode
wenzelm@54515
    36
    Document.Node.Name(node, master_dir, theory)
wenzelm@54514
    37
  }
wenzelm@54514
    38
wenzelm@54514
    39
wenzelm@44953
    40
  /* file-system operations */
wenzelm@44953
    41
wenzelm@44953
    42
  def append(dir: String, source_path: Path): String =
wenzelm@48711
    43
    (Path.explode(dir) + source_path).expand.implode
wenzelm@44953
    44
wenzelm@56823
    45
  def with_thy_reader[A](name: Document.Node.Name, f: Reader[Char] => A): A =
wenzelm@48885
    46
  {
wenzelm@48885
    47
    val path = Path.explode(name.node)
wenzelm@48885
    48
    if (!path.is_file) error("No such file: " + path.toString)
wenzelm@50291
    49
wenzelm@56823
    50
    val reader = Scan.byte_reader(path.file)
wenzelm@56823
    51
    try { f(reader) } finally { reader.close }
wenzelm@48885
    52
  }
wenzelm@48885
    53
wenzelm@46737
    54
wenzelm@46737
    55
  /* theory files */
wenzelm@46737
    56
wenzelm@56393
    57
  def loaded_files(syntax: Prover.Syntax, text: String): List[String] =
wenzelm@56393
    58
    if (syntax.load_commands_in(text)) {
wenzelm@57906
    59
      val spans = syntax.parse_spans(text)
wenzelm@59689
    60
      spans.iterator.map(Command.span_files(syntax, _)._1).flatten.toList
wenzelm@56392
    61
    }
wenzelm@56392
    62
    else Nil
wenzelm@48885
    63
wenzelm@56801
    64
  private def dummy_name(theory: String): Document.Node.Name =
wenzelm@56801
    65
    Document.Node.Name(theory + ".thy", "", theory)
wenzelm@56801
    66
wenzelm@56801
    67
  def import_name(qualifier: String, master: Document.Node.Name, s: String): Document.Node.Name =
wenzelm@51634
    68
  {
wenzelm@56913
    69
    val no_qualifier = "" // FIXME
wenzelm@56801
    70
    val thy1 = Thy_Header.base_name(s)
wenzelm@56913
    71
    val thy2 = if (Long_Name.is_qualified(thy1)) thy1 else Long_Name.qualify(no_qualifier, thy1)
wenzelm@56801
    72
    (known_theories.get(thy1) orElse
wenzelm@56801
    73
     known_theories.get(thy2) orElse
wenzelm@56801
    74
     known_theories.get(Long_Name.base_name(thy1))) match {
wenzelm@56801
    75
      case Some(name) if loaded_theories(name.theory) => dummy_name(name.theory)
wenzelm@56801
    76
      case Some(name) => name
wenzelm@56801
    77
      case None =>
wenzelm@56801
    78
        val path = Path.explode(s)
wenzelm@56801
    79
        val theory = path.base.implode
wenzelm@56801
    80
        if (Long_Name.is_qualified(theory)) dummy_name(theory)
wenzelm@56801
    81
        else {
wenzelm@56801
    82
          val node = append(master.master_dir, Resources.thy_path(path))
wenzelm@56801
    83
          val master_dir = append(master.master_dir, path.dir)
wenzelm@56913
    84
          Document.Node.Name(node, master_dir, Long_Name.qualify(no_qualifier, theory))
wenzelm@56801
    85
        }
wenzelm@51634
    86
    }
wenzelm@51634
    87
  }
wenzelm@51634
    88
wenzelm@56823
    89
  def check_thy_reader(qualifier: String, name: Document.Node.Name, reader: Reader[Char])
wenzelm@56801
    90
    : Document.Node.Header =
wenzelm@46737
    91
  {
wenzelm@57615
    92
    if (reader.source.length > 0) {
wenzelm@57615
    93
      try {
wenzelm@57615
    94
        val header = Thy_Header.read(reader).decode_symbols
wenzelm@48885
    95
wenzelm@57615
    96
        val base_name = Long_Name.base_name(name.theory)
wenzelm@57615
    97
        val name1 = header.name
wenzelm@57615
    98
        if (base_name != name1)
wenzelm@57615
    99
          error("Bad file name " + Resources.thy_path(Path.basic(base_name)) +
wenzelm@57615
   100
            " for theory " + quote(name1))
wenzelm@48885
   101
wenzelm@57615
   102
        val imports = header.imports.map(import_name(qualifier, name, _))
wenzelm@57615
   103
        Document.Node.Header(imports, header.keywords, Nil)
wenzelm@57615
   104
      }
wenzelm@57615
   105
      catch { case exn: Throwable => Document.Node.bad_header(Exn.message(exn)) }
wenzelm@50414
   106
    }
wenzelm@57615
   107
    else Document.Node.no_header
wenzelm@46737
   108
  }
wenzelm@46748
   109
wenzelm@56801
   110
  def check_thy(qualifier: String, name: Document.Node.Name): Document.Node.Header =
wenzelm@56823
   111
    with_thy_reader(name, check_thy_reader(qualifier, name, _))
wenzelm@50566
   112
wenzelm@50566
   113
wenzelm@56316
   114
  /* document changes */
wenzelm@50566
   115
wenzelm@56316
   116
  def parse_change(
wenzelm@56315
   117
      reparse_limit: Int,
wenzelm@56315
   118
      previous: Document.Version,
wenzelm@56315
   119
      doc_blobs: Document.Blobs,
wenzelm@56315
   120
      edits: List[Document.Edit_Text]): Session.Change =
wenzelm@56316
   121
    Thy_Syntax.parse_change(this, reparse_limit, previous, doc_blobs, edits)
wenzelm@55134
   122
wenzelm@56316
   123
  def commit(change: Session.Change) { }
wenzelm@56387
   124
wenzelm@56387
   125
wenzelm@56387
   126
  /* prover process */
wenzelm@56387
   127
wenzelm@56387
   128
  def start_prover(receiver: Prover.Message => Unit, name: String, args: List[String]): Prover =
wenzelm@57917
   129
    Isabelle_Process(receiver, args)
wenzelm@43651
   130
}
wenzelm@43651
   131