src/Pure/PIDE/resources.scala
author wenzelm
Mon Apr 03 13:39:13 2017 +0200 (2017-04-03)
changeset 65357 9a2c266f97c8
parent 65355 403eabd73c9a
child 65358 e345e9420109
permissions -rw-r--r--
simplified direct theory name (again, see also 570ba266f5b5, 2a7f9e79cb28);
     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   def import_name(qualifier: String, master: Document.Node.Name, s: String): Document.Node.Name =
    81   {
    82     val no_qualifier = "" // FIXME
    83     val thy1 = Thy_Header.base_name(s)
    84     val thy2 = if (Long_Name.is_qualified(thy1)) thy1 else Long_Name.qualify(no_qualifier, thy1)
    85     (base.known_theories.get(thy1) orElse
    86      base.known_theories.get(thy2) orElse
    87      base.known_theories.get(Long_Name.base_name(thy1))) match {
    88       case Some(name) if base.loaded_theory(name) => Document.Node.Name.theory(name.theory)
    89       case Some(name) => name
    90       case None =>
    91         val path = Path.explode(s)
    92         val theory = path.base.implode
    93         if (Long_Name.is_qualified(theory)) Document.Node.Name.theory(theory)
    94         else {
    95           val node = append(master.master_dir, thy_path(path))
    96           val master_dir = append(master.master_dir, path.dir)
    97           Document.Node.Name(node, master_dir, Long_Name.qualify(no_qualifier, theory))
    98         }
    99     }
   100   }
   101 
   102   def with_thy_reader[A](name: Document.Node.Name, f: Reader[Char] => A): A =
   103   {
   104     val path = Path.explode(name.node)
   105     if (!path.is_file) error("No such file: " + path.toString)
   106 
   107     val reader = Scan.byte_reader(path.file)
   108     try { f(reader) } finally { reader.close }
   109   }
   110 
   111   def check_thy_reader(qualifier: String, node_name: Document.Node.Name,
   112       reader: Reader[Char], start: Token.Pos = Token.Pos.command, strict: Boolean = true)
   113     : Document.Node.Header =
   114   {
   115     if (node_name.is_theory && reader.source.length > 0) {
   116       try {
   117         val header = Thy_Header.read(reader, start, strict).decode_symbols
   118 
   119         val base_name = Long_Name.base_name(node_name.theory)
   120         val (name, pos) = header.name
   121         if (base_name != name)
   122           error("Bad theory name " + quote(name) +
   123             " for file " + thy_path(Path.basic(base_name)) + Position.here(pos) +
   124             Completion.report_names(pos, 1, List((base_name, ("theory", base_name)))))
   125 
   126         val imports =
   127           header.imports.map({ case (s, pos) => (import_name(qualifier, node_name, s), pos) })
   128         Document.Node.Header(imports, header.keywords, header.abbrevs)
   129       }
   130       catch { case exn: Throwable => Document.Node.bad_header(Exn.message(exn)) }
   131     }
   132     else Document.Node.no_header
   133   }
   134 
   135   def check_thy(qualifier: String, name: Document.Node.Name,
   136       start: Token.Pos = Token.Pos.command, strict: Boolean = true): Document.Node.Header =
   137     with_thy_reader(name, check_thy_reader(qualifier, name, _, start, strict))
   138 
   139 
   140   /* special header */
   141 
   142   def special_header(name: Document.Node.Name): Option[Document.Node.Header] =
   143     if (Thy_Header.is_ml_root(name.theory))
   144       Some(Document.Node.Header(List((import_name("", name, Thy_Header.ML_BOOTSTRAP), Position.none))))
   145     else if (Thy_Header.is_bootstrap(name.theory))
   146       Some(Document.Node.Header(List((import_name("", name, Thy_Header.PURE), Position.none))))
   147     else None
   148 
   149 
   150   /* blobs */
   151 
   152   def undefined_blobs(nodes: Document.Nodes): List[Document.Node.Name] =
   153     (for {
   154       (node_name, node) <- nodes.iterator
   155       if !base.loaded_theory(node_name)
   156       cmd <- node.load_commands.iterator
   157       name <- cmd.blobs_undefined.iterator
   158     } yield name).toList
   159 
   160 
   161   /* document changes */
   162 
   163   def parse_change(
   164       reparse_limit: Int,
   165       previous: Document.Version,
   166       doc_blobs: Document.Blobs,
   167       edits: List[Document.Edit_Text]): Session.Change =
   168     Thy_Syntax.parse_change(this, reparse_limit, previous, doc_blobs, edits)
   169 
   170   def commit(change: Session.Change) { }
   171 }