src/Pure/PIDE/resources.scala
author wenzelm
Tue Aug 02 18:45:34 2016 +0200 (2016-08-02)
changeset 63584 68751fe1c036
parent 63579 73939a9b70a3
child 64654 31b681e38c70
permissions -rw-r--r--
tuned signature -- prover-independence is presently theoretical;
     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   val empty: Resources = new Resources(Set.empty, Map.empty, Outer_Syntax.empty)
    21 }
    22 
    23 class Resources(
    24   val loaded_theories: Set[String],
    25   val known_theories: Map[String, Document.Node.Name],
    26   val base_syntax: Outer_Syntax)
    27 {
    28   /* document node names */
    29 
    30   def node_name(qualifier: String, raw_path: Path): Document.Node.Name =
    31   {
    32     val no_qualifier = "" // FIXME
    33     val path = raw_path.expand
    34     val node = path.implode
    35     val theory = Long_Name.qualify(no_qualifier, Thy_Header.thy_name(node).getOrElse(""))
    36     val master_dir = if (theory == "") "" else path.dir.implode
    37     Document.Node.Name(node, master_dir, theory)
    38   }
    39 
    40 
    41   /* file-system operations */
    42 
    43   def append(dir: String, source_path: Path): String =
    44     (Path.explode(dir) + source_path).expand.implode
    45 
    46   def with_thy_reader[A](name: Document.Node.Name, f: Reader[Char] => A): A =
    47   {
    48     val path = Path.explode(name.node)
    49     if (!path.is_file) error("No such file: " + path.toString)
    50 
    51     val reader = Scan.byte_reader(path.file)
    52     try { f(reader) } finally { reader.close }
    53   }
    54 
    55 
    56   /* theory files */
    57 
    58   def loaded_files(syntax: Outer_Syntax, text: String): List[String] =
    59     if (syntax.load_commands_in(text)) {
    60       val spans = syntax.parse_spans(text)
    61       spans.iterator.map(Command.span_files(syntax, _)._1).flatten.toList
    62     }
    63     else Nil
    64 
    65   private def dummy_name(theory: String): Document.Node.Name =
    66     Document.Node.Name(theory + ".thy", "", theory)
    67 
    68   def import_name(qualifier: String, master: Document.Node.Name, s: String): Document.Node.Name =
    69   {
    70     val no_qualifier = "" // FIXME
    71     val thy1 = Thy_Header.base_name(s)
    72     val thy2 = if (Long_Name.is_qualified(thy1)) thy1 else Long_Name.qualify(no_qualifier, thy1)
    73     (known_theories.get(thy1) orElse
    74      known_theories.get(thy2) orElse
    75      known_theories.get(Long_Name.base_name(thy1))) match {
    76       case Some(name) if loaded_theories(name.theory) => dummy_name(name.theory)
    77       case Some(name) => name
    78       case None =>
    79         val path = Path.explode(s)
    80         val theory = path.base.implode
    81         if (Long_Name.is_qualified(theory)) dummy_name(theory)
    82         else {
    83           val node = append(master.master_dir, Resources.thy_path(path))
    84           val master_dir = append(master.master_dir, path.dir)
    85           Document.Node.Name(node, master_dir, Long_Name.qualify(no_qualifier, theory))
    86         }
    87     }
    88   }
    89 
    90   def check_thy_reader(qualifier: String, node_name: Document.Node.Name,
    91     reader: Reader[Char], start: Token.Pos): Document.Node.Header =
    92   {
    93     if (reader.source.length > 0) {
    94       try {
    95         val header = Thy_Header.read(reader, start).decode_symbols
    96 
    97         val base_name = Long_Name.base_name(node_name.theory)
    98         val (name, pos) = header.name
    99         if (base_name != name)
   100           error("Bad theory name " + quote(name) +
   101             " for file " + Resources.thy_path(Path.basic(base_name)) + Position.here(pos) +
   102             Completion.report_names(pos, 1, List((base_name, ("theory", base_name)))))
   103 
   104         val imports =
   105           header.imports.map({ case (s, pos) => (import_name(qualifier, node_name, s), pos) })
   106         Document.Node.Header(imports, header.keywords, header.abbrevs)
   107       }
   108       catch { case exn: Throwable => Document.Node.bad_header(Exn.message(exn)) }
   109     }
   110     else Document.Node.no_header
   111   }
   112 
   113   def check_thy(qualifier: String, name: Document.Node.Name, start: Token.Pos)
   114     : Document.Node.Header =
   115     with_thy_reader(name, check_thy_reader(qualifier, name, _, start))
   116 
   117   def check_file(file: String): Boolean =
   118     try {
   119       if (Url.is_wellformed(file)) Url.is_readable(file)
   120       else (new JFile(file)).isFile
   121     }
   122     catch { case ERROR(_) => false }
   123 
   124 
   125   /* document changes */
   126 
   127   def parse_change(
   128       reparse_limit: Int,
   129       previous: Document.Version,
   130       doc_blobs: Document.Blobs,
   131       edits: List[Document.Edit_Text]): Session.Change =
   132     Thy_Syntax.parse_change(this, reparse_limit, previous, doc_blobs, edits)
   133 
   134   def commit(change: Session.Change) { }
   135 }
   136