src/Pure/PIDE/resources.scala
author wenzelm
Fri May 02 13:52:45 2014 +0200 (2014-05-02)
changeset 56823 37be55461dbe
parent 56801 8dd9df88f647
child 56913 df4cd6e1fdfa
permissions -rw-r--r--
more frugal access to theory text via Reader, reduced costs for I/O text decoding;
prefer non-strict Symbol.decode, since Reader[Char] may present symbols in either way;
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@54514
    31
    val path = raw_path.expand
wenzelm@54514
    32
    val node = path.implode
wenzelm@56801
    33
    val theory = Long_Name.qualify(qualifier, Thy_Header.thy_name(node).getOrElse(""))
wenzelm@54515
    34
    val master_dir = if (theory == "") "" else path.dir.implode
wenzelm@54515
    35
    Document.Node.Name(node, master_dir, theory)
wenzelm@54514
    36
  }
wenzelm@54514
    37
wenzelm@54514
    38
wenzelm@44953
    39
  /* file-system operations */
wenzelm@44953
    40
wenzelm@44953
    41
  def append(dir: String, source_path: Path): String =
wenzelm@48711
    42
    (Path.explode(dir) + source_path).expand.implode
wenzelm@44953
    43
wenzelm@56823
    44
  def with_thy_reader[A](name: Document.Node.Name, f: Reader[Char] => A): A =
wenzelm@48885
    45
  {
wenzelm@48885
    46
    val path = Path.explode(name.node)
wenzelm@48885
    47
    if (!path.is_file) error("No such file: " + path.toString)
wenzelm@50291
    48
wenzelm@56823
    49
    val reader = Scan.byte_reader(path.file)
wenzelm@56823
    50
    try { f(reader) } finally { reader.close }
wenzelm@48885
    51
  }
wenzelm@48885
    52
wenzelm@46737
    53
wenzelm@46737
    54
  /* theory files */
wenzelm@46737
    55
wenzelm@56393
    56
  def loaded_files(syntax: Prover.Syntax, text: String): List[String] =
wenzelm@56393
    57
    if (syntax.load_commands_in(text)) {
wenzelm@56392
    58
      val spans = Thy_Syntax.parse_spans(syntax.scan(text))
wenzelm@56392
    59
      spans.iterator.map(Thy_Syntax.span_files(syntax, _)).flatten.toList
wenzelm@56392
    60
    }
wenzelm@56392
    61
    else Nil
wenzelm@48885
    62
wenzelm@56801
    63
  private def dummy_name(theory: String): Document.Node.Name =
wenzelm@56801
    64
    Document.Node.Name(theory + ".thy", "", theory)
wenzelm@56801
    65
wenzelm@56801
    66
  def import_name(qualifier: String, master: Document.Node.Name, s: String): Document.Node.Name =
wenzelm@51634
    67
  {
wenzelm@56801
    68
    val thy1 = Thy_Header.base_name(s)
wenzelm@56801
    69
    val thy2 = if (Long_Name.is_qualified(thy1)) thy1 else Long_Name.qualify(qualifier, thy1)
wenzelm@56801
    70
    (known_theories.get(thy1) orElse
wenzelm@56801
    71
     known_theories.get(thy2) orElse
wenzelm@56801
    72
     known_theories.get(Long_Name.base_name(thy1))) match {
wenzelm@56801
    73
      case Some(name) if loaded_theories(name.theory) => dummy_name(name.theory)
wenzelm@56801
    74
      case Some(name) => name
wenzelm@56801
    75
      case None =>
wenzelm@56801
    76
        val path = Path.explode(s)
wenzelm@56801
    77
        val theory = path.base.implode
wenzelm@56801
    78
        if (Long_Name.is_qualified(theory)) dummy_name(theory)
wenzelm@56801
    79
        else {
wenzelm@56801
    80
          val node = append(master.master_dir, Resources.thy_path(path))
wenzelm@56801
    81
          val master_dir = append(master.master_dir, path.dir)
wenzelm@56801
    82
          Document.Node.Name(node, master_dir, Long_Name.qualify(qualifier, theory))
wenzelm@56801
    83
        }
wenzelm@51634
    84
    }
wenzelm@51634
    85
  }
wenzelm@51634
    86
wenzelm@56823
    87
  def check_thy_reader(qualifier: String, name: Document.Node.Name, reader: Reader[Char])
wenzelm@56801
    88
    : Document.Node.Header =
wenzelm@46737
    89
  {
wenzelm@50414
    90
    try {
wenzelm@56823
    91
      val header = Thy_Header.read(reader).decode_symbols
wenzelm@48885
    92
wenzelm@56801
    93
      val base_name = Long_Name.base_name(name.theory)
wenzelm@50414
    94
      val name1 = header.name
wenzelm@56801
    95
      if (base_name != name1)
wenzelm@56801
    96
        error("Bad file name " + Resources.thy_path(Path.basic(base_name)) +
wenzelm@50414
    97
          " for theory " + quote(name1))
wenzelm@48885
    98
wenzelm@56801
    99
      val imports = header.imports.map(import_name(qualifier, name, _))
wenzelm@51293
   100
      Document.Node.Header(imports, header.keywords, Nil)
wenzelm@50414
   101
    }
wenzelm@50641
   102
    catch { case exn: Throwable => Document.Node.bad_header(Exn.message(exn)) }
wenzelm@46737
   103
  }
wenzelm@46748
   104
wenzelm@56801
   105
  def check_thy(qualifier: String, name: Document.Node.Name): Document.Node.Header =
wenzelm@56823
   106
    with_thy_reader(name, check_thy_reader(qualifier, name, _))
wenzelm@50566
   107
wenzelm@50566
   108
wenzelm@56316
   109
  /* document changes */
wenzelm@50566
   110
wenzelm@56316
   111
  def parse_change(
wenzelm@56315
   112
      reparse_limit: Int,
wenzelm@56315
   113
      previous: Document.Version,
wenzelm@56315
   114
      doc_blobs: Document.Blobs,
wenzelm@56315
   115
      edits: List[Document.Edit_Text]): Session.Change =
wenzelm@56316
   116
    Thy_Syntax.parse_change(this, reparse_limit, previous, doc_blobs, edits)
wenzelm@55134
   117
wenzelm@56316
   118
  def commit(change: Session.Change) { }
wenzelm@56387
   119
wenzelm@56387
   120
wenzelm@56387
   121
  /* prover process */
wenzelm@56387
   122
wenzelm@56387
   123
  def start_prover(receiver: Prover.Message => Unit, name: String, args: List[String]): Prover =
wenzelm@56387
   124
    new Isabelle_Process(receiver, args) with Protocol
wenzelm@43651
   125
}
wenzelm@43651
   126