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;
     1 /*  Title:      Pure/PIDE/resources.scala
     2     Author:     Makarius
     3 
     4 Resources for theories and auxiliary files.
     5 */
     6 
     7 package isabelle
     8 
     9 
    10 import scala.annotation.tailrec
    11 import scala.util.parsing.input.Reader
    12 
    13 import java.io.{File => JFile}
    14 
    15 
    16 object Resources
    17 {
    18   def thy_path(path: Path): Path = path.ext("thy")
    19 }
    20 
    21 
    22 class Resources(
    23   val loaded_theories: Set[String],
    24   val known_theories: Map[String, Document.Node.Name],
    25   val base_syntax: Prover.Syntax)
    26 {
    27   /* document node names */
    28 
    29   def node_name(qualifier: String, raw_path: Path): Document.Node.Name =
    30   {
    31     val path = raw_path.expand
    32     val node = path.implode
    33     val theory = Long_Name.qualify(qualifier, Thy_Header.thy_name(node).getOrElse(""))
    34     val master_dir = if (theory == "") "" else path.dir.implode
    35     Document.Node.Name(node, master_dir, theory)
    36   }
    37 
    38 
    39   /* file-system operations */
    40 
    41   def append(dir: String, source_path: Path): String =
    42     (Path.explode(dir) + source_path).expand.implode
    43 
    44   def with_thy_reader[A](name: Document.Node.Name, f: Reader[Char] => A): A =
    45   {
    46     val path = Path.explode(name.node)
    47     if (!path.is_file) error("No such file: " + path.toString)
    48 
    49     val reader = Scan.byte_reader(path.file)
    50     try { f(reader) } finally { reader.close }
    51   }
    52 
    53 
    54   /* theory files */
    55 
    56   def loaded_files(syntax: Prover.Syntax, text: String): List[String] =
    57     if (syntax.load_commands_in(text)) {
    58       val spans = Thy_Syntax.parse_spans(syntax.scan(text))
    59       spans.iterator.map(Thy_Syntax.span_files(syntax, _)).flatten.toList
    60     }
    61     else Nil
    62 
    63   private def dummy_name(theory: String): Document.Node.Name =
    64     Document.Node.Name(theory + ".thy", "", theory)
    65 
    66   def import_name(qualifier: String, master: Document.Node.Name, s: String): Document.Node.Name =
    67   {
    68     val thy1 = Thy_Header.base_name(s)
    69     val thy2 = if (Long_Name.is_qualified(thy1)) thy1 else Long_Name.qualify(qualifier, thy1)
    70     (known_theories.get(thy1) orElse
    71      known_theories.get(thy2) orElse
    72      known_theories.get(Long_Name.base_name(thy1))) match {
    73       case Some(name) if loaded_theories(name.theory) => dummy_name(name.theory)
    74       case Some(name) => name
    75       case None =>
    76         val path = Path.explode(s)
    77         val theory = path.base.implode
    78         if (Long_Name.is_qualified(theory)) dummy_name(theory)
    79         else {
    80           val node = append(master.master_dir, Resources.thy_path(path))
    81           val master_dir = append(master.master_dir, path.dir)
    82           Document.Node.Name(node, master_dir, Long_Name.qualify(qualifier, theory))
    83         }
    84     }
    85   }
    86 
    87   def check_thy_reader(qualifier: String, name: Document.Node.Name, reader: Reader[Char])
    88     : Document.Node.Header =
    89   {
    90     try {
    91       val header = Thy_Header.read(reader).decode_symbols
    92 
    93       val base_name = Long_Name.base_name(name.theory)
    94       val name1 = header.name
    95       if (base_name != name1)
    96         error("Bad file name " + Resources.thy_path(Path.basic(base_name)) +
    97           " for theory " + quote(name1))
    98 
    99       val imports = header.imports.map(import_name(qualifier, name, _))
   100       Document.Node.Header(imports, header.keywords, Nil)
   101     }
   102     catch { case exn: Throwable => Document.Node.bad_header(Exn.message(exn)) }
   103   }
   104 
   105   def check_thy(qualifier: String, name: Document.Node.Name): Document.Node.Header =
   106     with_thy_reader(name, check_thy_reader(qualifier, name, _))
   107 
   108 
   109   /* document changes */
   110 
   111   def parse_change(
   112       reparse_limit: Int,
   113       previous: Document.Version,
   114       doc_blobs: Document.Blobs,
   115       edits: List[Document.Edit_Text]): Session.Change =
   116     Thy_Syntax.parse_change(this, reparse_limit, previous, doc_blobs, edits)
   117 
   118   def commit(change: Session.Change) { }
   119 
   120 
   121   /* prover process */
   122 
   123   def start_prover(receiver: Prover.Message => Unit, name: String, args: List[String]): Prover =
   124     new Isabelle_Process(receiver, args) with Protocol
   125 }
   126