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