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