src/Pure/PIDE/resources.scala
author wenzelm
Fri Apr 07 13:19:11 2017 +0200 (2017-04-07)
changeset 65423 4527b33d5b3e
parent 65409 ad9e2c1665b6
child 65429 fcff401fb609
permissions -rw-r--r--
tuned;
     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 import_name(dir: String, s: String): Document.Node.Name =
    71   {
    72     val theory0 = Thy_Header.base_name(s)
    73     val theory =
    74       if (Long_Name.is_qualified(theory0) || session_base.global_theories.contains(theory0)) theory0
    75       else Long_Name.qualify(session_name, theory0)
    76 
    77     session_base.known_theories.get(theory) orElse session_base.known_theories.get(theory0) match
    78     {
    79       case Some(name) =>
    80         if (session_base.loaded_theory(name)) name.loaded_theory else name
    81       case None =>
    82         val path = Path.explode(s)
    83         val node = append(dir, thy_path(path))
    84         val master_dir = append(dir, path.dir)
    85         Document.Node.Name(node, master_dir, theory)
    86     }
    87   }
    88 
    89   def with_thy_reader[A](name: Document.Node.Name, f: Reader[Char] => A): A =
    90   {
    91     val path = Path.explode(name.node)
    92     if (!path.is_file) error("No such file: " + path.toString)
    93 
    94     val reader = Scan.byte_reader(path.file)
    95     try { f(reader) } finally { reader.close }
    96   }
    97 
    98   def check_thy_reader(node_name: Document.Node.Name, reader: Reader[Char],
    99     start: Token.Pos = Token.Pos.command, strict: Boolean = true): Document.Node.Header =
   100   {
   101     if (node_name.is_theory && reader.source.length > 0) {
   102       try {
   103         val header = Thy_Header.read(reader, start, strict).decode_symbols
   104 
   105         val base_name = Long_Name.base_name(node_name.theory)
   106         val (name, pos) = header.name
   107         if (base_name != name)
   108           error("Bad theory name " + quote(name) +
   109             " for file " + thy_path(Path.basic(base_name)) + Position.here(pos) +
   110             Completion.report_names(pos, 1, List((base_name, ("theory", base_name)))))
   111 
   112         val imports =
   113           header.imports.map({ case (s, pos) => (import_name(node_name.master_dir, s), pos) })
   114         Document.Node.Header(imports, header.keywords, header.abbrevs)
   115       }
   116       catch { case exn: Throwable => Document.Node.bad_header(Exn.message(exn)) }
   117     }
   118     else Document.Node.no_header
   119   }
   120 
   121   def check_thy(name: Document.Node.Name, start: Token.Pos = Token.Pos.command,
   122       strict: Boolean = true): Document.Node.Header =
   123     with_thy_reader(name, check_thy_reader(name, _, start, strict))
   124 
   125 
   126   /* special header */
   127 
   128   def special_header(name: Document.Node.Name): Option[Document.Node.Header] =
   129     if (Thy_Header.is_ml_root(name.theory))
   130       Some(Document.Node.Header(
   131         List((import_name(name.master_dir, Thy_Header.ML_BOOTSTRAP), Position.none))))
   132     else if (Thy_Header.is_bootstrap(name.theory))
   133       Some(Document.Node.Header(
   134         List((import_name(name.master_dir, Thy_Header.PURE), Position.none))))
   135     else None
   136 
   137 
   138   /* blobs */
   139 
   140   def undefined_blobs(nodes: Document.Nodes): List[Document.Node.Name] =
   141     (for {
   142       (node_name, node) <- nodes.iterator
   143       if !session_base.loaded_theory(node_name)
   144       cmd <- node.load_commands.iterator
   145       name <- cmd.blobs_undefined.iterator
   146     } yield name).toList
   147 
   148 
   149   /* document changes */
   150 
   151   def parse_change(
   152       reparse_limit: Int,
   153       previous: Document.Version,
   154       doc_blobs: Document.Blobs,
   155       edits: List[Document.Edit_Text]): Session.Change =
   156     Thy_Syntax.parse_change(this, reparse_limit, previous, doc_blobs, edits)
   157 
   158   def commit(change: Session.Change) { }
   159 }