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