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