src/Pure/PIDE/resources.scala
author wenzelm
Thu May 08 16:19:16 2014 +0200 (2014-05-08)
changeset 56913 df4cd6e1fdfa
parent 56823 37be55461dbe
child 57615 df1b3452d71c
permissions -rw-r--r--
no qualifier for now, to avoid confusion concerning loaded_theories in PIDE interaction;
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@56392
    59
      val spans = Thy_Syntax.parse_spans(syntax.scan(text))
wenzelm@56392
    60
      spans.iterator.map(Thy_Syntax.span_files(syntax, _)).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@50414
    92
    try {
wenzelm@56823
    93
      val header = Thy_Header.read(reader).decode_symbols
wenzelm@48885
    94
wenzelm@56801
    95
      val base_name = Long_Name.base_name(name.theory)
wenzelm@50414
    96
      val name1 = header.name
wenzelm@56801
    97
      if (base_name != name1)
wenzelm@56801
    98
        error("Bad file name " + Resources.thy_path(Path.basic(base_name)) +
wenzelm@50414
    99
          " for theory " + quote(name1))
wenzelm@48885
   100
wenzelm@56801
   101
      val imports = header.imports.map(import_name(qualifier, name, _))
wenzelm@51293
   102
      Document.Node.Header(imports, header.keywords, Nil)
wenzelm@50414
   103
    }
wenzelm@50641
   104
    catch { case exn: Throwable => Document.Node.bad_header(Exn.message(exn)) }
wenzelm@46737
   105
  }
wenzelm@46748
   106
wenzelm@56801
   107
  def check_thy(qualifier: String, name: Document.Node.Name): Document.Node.Header =
wenzelm@56823
   108
    with_thy_reader(name, check_thy_reader(qualifier, name, _))
wenzelm@50566
   109
wenzelm@50566
   110
wenzelm@56316
   111
  /* document changes */
wenzelm@50566
   112
wenzelm@56316
   113
  def parse_change(
wenzelm@56315
   114
      reparse_limit: Int,
wenzelm@56315
   115
      previous: Document.Version,
wenzelm@56315
   116
      doc_blobs: Document.Blobs,
wenzelm@56315
   117
      edits: List[Document.Edit_Text]): Session.Change =
wenzelm@56316
   118
    Thy_Syntax.parse_change(this, reparse_limit, previous, doc_blobs, edits)
wenzelm@55134
   119
wenzelm@56316
   120
  def commit(change: Session.Change) { }
wenzelm@56387
   121
wenzelm@56387
   122
wenzelm@56387
   123
  /* prover process */
wenzelm@56387
   124
wenzelm@56387
   125
  def start_prover(receiver: Prover.Message => Unit, name: String, args: List[String]): Prover =
wenzelm@56387
   126
    new Isabelle_Process(receiver, args) with Protocol
wenzelm@43651
   127
}
wenzelm@43651
   128