src/Pure/PIDE/resources.scala
author wenzelm
Thu Apr 03 20:17:12 2014 +0200 (2014-04-03)
changeset 56392 bc118a32a870
parent 56387 d92eb5c3960d
child 56393 22f533e6a049
permissions -rw-r--r--
tuned signature (see also 0850d43cb355);
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@48885
    11
wenzelm@48409
    12
import java.io.{File => JFile}
wenzelm@44953
    13
wenzelm@44953
    14
wenzelm@56208
    15
object Resources
wenzelm@48422
    16
{
wenzelm@48422
    17
  def thy_path(path: Path): Path = path.ext("thy")
wenzelm@48422
    18
}
wenzelm@44953
    19
wenzelm@48422
    20
wenzelm@56208
    21
class Resources(val loaded_theories: Set[String] = Set.empty, val base_syntax: Outer_Syntax)
wenzelm@43651
    22
{
wenzelm@54514
    23
  /* document node names */
wenzelm@54514
    24
wenzelm@54514
    25
  def node_name(raw_path: Path): Document.Node.Name =
wenzelm@54514
    26
  {
wenzelm@54514
    27
    val path = raw_path.expand
wenzelm@54514
    28
    val node = path.implode
wenzelm@54514
    29
    val theory = Thy_Header.thy_name(node).getOrElse("")
wenzelm@54515
    30
    val master_dir = if (theory == "") "" else path.dir.implode
wenzelm@54515
    31
    Document.Node.Name(node, master_dir, theory)
wenzelm@54514
    32
  }
wenzelm@54514
    33
wenzelm@54514
    34
wenzelm@44953
    35
  /* file-system operations */
wenzelm@44953
    36
wenzelm@44953
    37
  def append(dir: String, source_path: Path): String =
wenzelm@48711
    38
    (Path.explode(dir) + source_path).expand.implode
wenzelm@44953
    39
wenzelm@48885
    40
  def with_thy_text[A](name: Document.Node.Name, f: CharSequence => A): A =
wenzelm@48885
    41
  {
wenzelm@48885
    42
    val path = Path.explode(name.node)
wenzelm@48885
    43
    if (!path.is_file) error("No such file: " + path.toString)
wenzelm@50291
    44
wenzelm@50291
    45
    val text = File.read(path)
wenzelm@50291
    46
    Symbol.decode_strict(text)
wenzelm@50291
    47
    f(text)
wenzelm@48885
    48
  }
wenzelm@48885
    49
wenzelm@46737
    50
wenzelm@46737
    51
  /* theory files */
wenzelm@46737
    52
wenzelm@56392
    53
  def loaded_files(syntax: Outer_Syntax, text: String): List[String] =
wenzelm@48885
    54
  {
wenzelm@56392
    55
    if (syntax.load_commands.exists({ case (cmd, _) => text.containsSlice(cmd) })) {
wenzelm@56392
    56
      val spans = Thy_Syntax.parse_spans(syntax.scan(text))
wenzelm@56392
    57
      spans.iterator.map(Thy_Syntax.span_files(syntax, _)).flatten.toList
wenzelm@56392
    58
    }
wenzelm@56392
    59
    else Nil
wenzelm@48885
    60
  }
wenzelm@48885
    61
wenzelm@51634
    62
  def import_name(master: Document.Node.Name, s: String): Document.Node.Name =
wenzelm@51634
    63
  {
wenzelm@51634
    64
    val theory = Thy_Header.base_name(s)
wenzelm@54515
    65
    if (loaded_theories(theory)) Document.Node.Name(theory + ".thy", "", theory)
wenzelm@51634
    66
    else {
wenzelm@51634
    67
      val path = Path.explode(s)
wenzelm@56208
    68
      val node = append(master.master_dir, Resources.thy_path(path))
wenzelm@54515
    69
      val master_dir = append(master.master_dir, path.dir)
wenzelm@54515
    70
      Document.Node.Name(node, master_dir, theory)
wenzelm@51634
    71
    }
wenzelm@51634
    72
  }
wenzelm@51634
    73
wenzelm@48885
    74
  def check_thy_text(name: Document.Node.Name, text: CharSequence): Document.Node.Header =
wenzelm@46737
    75
  {
wenzelm@50414
    76
    try {
wenzelm@50414
    77
      val header = Thy_Header.read(text)
wenzelm@48885
    78
wenzelm@50414
    79
      val name1 = header.name
wenzelm@50414
    80
      if (name.theory != name1)
wenzelm@56208
    81
        error("Bad file name " + Resources.thy_path(Path.basic(name.theory)) +
wenzelm@50414
    82
          " for theory " + quote(name1))
wenzelm@48885
    83
wenzelm@51634
    84
      val imports = header.imports.map(import_name(name, _))
wenzelm@51293
    85
      Document.Node.Header(imports, header.keywords, Nil)
wenzelm@50414
    86
    }
wenzelm@50641
    87
    catch { case exn: Throwable => Document.Node.bad_header(Exn.message(exn)) }
wenzelm@46737
    88
  }
wenzelm@46748
    89
wenzelm@48885
    90
  def check_thy(name: Document.Node.Name): Document.Node.Header =
wenzelm@48885
    91
    with_thy_text(name, check_thy_text(name, _))
wenzelm@50566
    92
wenzelm@50566
    93
wenzelm@56316
    94
  /* document changes */
wenzelm@50566
    95
wenzelm@56316
    96
  def parse_change(
wenzelm@56315
    97
      reparse_limit: Int,
wenzelm@56315
    98
      previous: Document.Version,
wenzelm@56315
    99
      doc_blobs: Document.Blobs,
wenzelm@56315
   100
      edits: List[Document.Edit_Text]): Session.Change =
wenzelm@56316
   101
    Thy_Syntax.parse_change(this, reparse_limit, previous, doc_blobs, edits)
wenzelm@55134
   102
wenzelm@56316
   103
  def commit(change: Session.Change) { }
wenzelm@56387
   104
wenzelm@56387
   105
wenzelm@56387
   106
  /* prover process */
wenzelm@56387
   107
wenzelm@56387
   108
  def start_prover(receiver: Prover.Message => Unit, name: String, args: List[String]): Prover =
wenzelm@56387
   109
    new Isabelle_Process(receiver, args) with Protocol
wenzelm@43651
   110
}
wenzelm@43651
   111