src/Pure/PIDE/resources.scala
author wenzelm
Wed Mar 15 11:07:07 2017 +0100 (2017-03-15)
changeset 65255 d388e63a46fc
parent 65250 13a6c81534a8
child 65355 403eabd73c9a
permissions -rw-r--r--
unused;
     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 class Resources(val base: Sessions.Base, val log: Logger = No_Logger)
    17 {
    18   val thy_info = new Thy_Info(this)
    19 
    20   def thy_path(path: Path): Path = path.ext("thy")
    21 
    22 
    23   /* document node names */
    24 
    25   def node_name(qualifier: String, raw_path: Path): Document.Node.Name =
    26   {
    27     val no_qualifier = "" // FIXME
    28     val path = raw_path.expand
    29     val node = path.implode
    30     val theory = Long_Name.qualify(no_qualifier, Thy_Header.thy_name(node).getOrElse(""))
    31     val master_dir = if (theory == "") "" else path.dir.implode
    32     Document.Node.Name(node, master_dir, theory)
    33   }
    34 
    35 
    36   /* file-system operations */
    37 
    38   def append(dir: String, source_path: Path): String =
    39     (Path.explode(dir) + source_path).expand.implode
    40 
    41   def append_file(dir: String, raw_name: String): String =
    42     if (Path.is_valid(raw_name)) append(dir, Path.explode(raw_name))
    43     else raw_name
    44 
    45 
    46 
    47   /* source files of Isabelle/ML bootstrap */
    48 
    49   def source_file(raw_name: String): Option[String] =
    50   {
    51     if (Path.is_wellformed(raw_name)) {
    52       if (Path.is_valid(raw_name)) {
    53         def check(p: Path): Option[Path] = if (p.is_file) Some(p) else None
    54 
    55         val path = Path.explode(raw_name)
    56         val path1 =
    57           if (path.is_absolute || path.is_current) check(path)
    58           else {
    59             check(Path.explode("~~/src/Pure") + path) orElse
    60               (if (Isabelle_System.getenv("ML_SOURCES") == "") None
    61                else check(Path.explode("$ML_SOURCES") + path))
    62           }
    63         Some(File.platform_path(path1 getOrElse path))
    64       }
    65       else None
    66     }
    67     else Some(raw_name)
    68   }
    69 
    70 
    71   /* theory files */
    72 
    73   def loaded_files(syntax: Outer_Syntax, text: String): List[String] =
    74     if (syntax.load_commands_in(text)) {
    75       val spans = syntax.parse_spans(text)
    76       spans.iterator.map(Command.span_files(syntax, _)._1).flatten.toList
    77     }
    78     else Nil
    79 
    80   private def dummy_name(theory: String): Document.Node.Name =
    81     Document.Node.Name(theory + ".thy", "", theory)
    82 
    83   def import_name(qualifier: String, master: Document.Node.Name, s: String): Document.Node.Name =
    84   {
    85     val no_qualifier = "" // FIXME
    86     val thy1 = Thy_Header.base_name(s)
    87     val thy2 = if (Long_Name.is_qualified(thy1)) thy1 else Long_Name.qualify(no_qualifier, thy1)
    88     (base.known_theories.get(thy1) orElse
    89      base.known_theories.get(thy2) orElse
    90      base.known_theories.get(Long_Name.base_name(thy1))) match {
    91       case Some(name) if base.loaded_theories(name.theory) => dummy_name(name.theory)
    92       case Some(name) => name
    93       case None =>
    94         val path = Path.explode(s)
    95         val theory = path.base.implode
    96         if (Long_Name.is_qualified(theory)) dummy_name(theory)
    97         else {
    98           val node = append(master.master_dir, thy_path(path))
    99           val master_dir = append(master.master_dir, path.dir)
   100           Document.Node.Name(node, master_dir, Long_Name.qualify(no_qualifier, theory))
   101         }
   102     }
   103   }
   104 
   105   def with_thy_reader[A](name: Document.Node.Name, f: Reader[Char] => A): A =
   106   {
   107     val path = Path.explode(name.node)
   108     if (!path.is_file) error("No such file: " + path.toString)
   109 
   110     val reader = Scan.byte_reader(path.file)
   111     try { f(reader) } finally { reader.close }
   112   }
   113 
   114   def check_thy_reader(qualifier: String, node_name: Document.Node.Name,
   115       reader: Reader[Char], start: Token.Pos = Token.Pos.command, strict: Boolean = true)
   116     : Document.Node.Header =
   117   {
   118     if (node_name.is_theory && reader.source.length > 0) {
   119       try {
   120         val header = Thy_Header.read(reader, start, strict).decode_symbols
   121 
   122         val base_name = Long_Name.base_name(node_name.theory)
   123         val (name, pos) = header.name
   124         if (base_name != name)
   125           error("Bad theory name " + quote(name) +
   126             " for file " + thy_path(Path.basic(base_name)) + Position.here(pos) +
   127             Completion.report_names(pos, 1, List((base_name, ("theory", base_name)))))
   128 
   129         val imports =
   130           header.imports.map({ case (s, pos) => (import_name(qualifier, node_name, s), pos) })
   131         Document.Node.Header(imports, header.keywords, header.abbrevs)
   132       }
   133       catch { case exn: Throwable => Document.Node.bad_header(Exn.message(exn)) }
   134     }
   135     else Document.Node.no_header
   136   }
   137 
   138   def check_thy(qualifier: String, name: Document.Node.Name,
   139       start: Token.Pos = Token.Pos.command, strict: Boolean = true): Document.Node.Header =
   140     with_thy_reader(name, check_thy_reader(qualifier, name, _, start, strict))
   141 
   142 
   143   /* special header */
   144 
   145   def special_header(name: Document.Node.Name): Option[Document.Node.Header] =
   146     if (Thy_Header.is_ml_root(name.theory))
   147       Some(Document.Node.Header(List((import_name("", name, Thy_Header.ML_BOOTSTRAP), Position.none))))
   148     else if (Thy_Header.is_bootstrap(name.theory))
   149       Some(Document.Node.Header(List((import_name("", name, Thy_Header.PURE), Position.none))))
   150     else None
   151 
   152 
   153   /* blobs */
   154 
   155   def undefined_blobs(nodes: Document.Nodes): List[Document.Node.Name] =
   156     (for {
   157       (node_name, node) <- nodes.iterator
   158       if !base.loaded_theories(node_name.theory)
   159       cmd <- node.load_commands.iterator
   160       name <- cmd.blobs_undefined.iterator
   161     } yield name).toList
   162 
   163 
   164   /* document changes */
   165 
   166   def parse_change(
   167       reparse_limit: Int,
   168       previous: Document.Version,
   169       doc_blobs: Document.Blobs,
   170       edits: List[Document.Edit_Text]): Session.Change =
   171     Thy_Syntax.parse_change(this, reparse_limit, previous, doc_blobs, edits)
   172 
   173   def commit(change: Session.Change) { }
   174 }