src/Pure/PIDE/resources.scala
author wenzelm
Sat Mar 14 18:18:40 2015 +0100 (2015-03-14)
changeset 59694 d2bb4b5ed862
parent 59691 f6ff19188842
child 59695 a03e0561bdbf
permissions -rw-r--r--
misc tuning -- more uniform ML vs. Scala;
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@59694
    89
  def check_thy_reader(qualifier: String, node_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@59694
    96
        val base_name = Long_Name.base_name(node_name.theory)
wenzelm@59694
    97
        val (name, pos) = header.name
wenzelm@59694
    98
        if (base_name != name)
wenzelm@57615
    99
          error("Bad file name " + Resources.thy_path(Path.basic(base_name)) +
wenzelm@59694
   100
            " for theory " + quote(name) + Position.here(pos))
wenzelm@48885
   101
wenzelm@59694
   102
        val imports =
wenzelm@59694
   103
          header.imports.map({ case (s, _) => import_name(qualifier, node_name, s) })
wenzelm@57615
   104
        Document.Node.Header(imports, header.keywords, Nil)
wenzelm@57615
   105
      }
wenzelm@57615
   106
      catch { case exn: Throwable => Document.Node.bad_header(Exn.message(exn)) }
wenzelm@50414
   107
    }
wenzelm@57615
   108
    else Document.Node.no_header
wenzelm@46737
   109
  }
wenzelm@46748
   110
wenzelm@56801
   111
  def check_thy(qualifier: String, name: Document.Node.Name): Document.Node.Header =
wenzelm@56823
   112
    with_thy_reader(name, check_thy_reader(qualifier, name, _))
wenzelm@50566
   113
wenzelm@59691
   114
  def check_file(file: String): Boolean =
wenzelm@59691
   115
    try {
wenzelm@59691
   116
      if (Url.is_wellformed(file)) Url.is_readable(file)
wenzelm@59691
   117
      else (new JFile(file)).isFile
wenzelm@59691
   118
    }
wenzelm@59691
   119
    catch { case ERROR(_) => false }
wenzelm@59691
   120
wenzelm@50566
   121
wenzelm@56316
   122
  /* document changes */
wenzelm@50566
   123
wenzelm@56316
   124
  def parse_change(
wenzelm@56315
   125
      reparse_limit: Int,
wenzelm@56315
   126
      previous: Document.Version,
wenzelm@56315
   127
      doc_blobs: Document.Blobs,
wenzelm@56315
   128
      edits: List[Document.Edit_Text]): Session.Change =
wenzelm@56316
   129
    Thy_Syntax.parse_change(this, reparse_limit, previous, doc_blobs, edits)
wenzelm@55134
   130
wenzelm@56316
   131
  def commit(change: Session.Change) { }
wenzelm@56387
   132
wenzelm@56387
   133
wenzelm@56387
   134
  /* prover process */
wenzelm@56387
   135
wenzelm@56387
   136
  def start_prover(receiver: Prover.Message => Unit, name: String, args: List[String]): Prover =
wenzelm@57917
   137
    Isabelle_Process(receiver, args)
wenzelm@43651
   138
}
wenzelm@43651
   139