src/Pure/PIDE/resources.scala
author wenzelm
Wed Sep 27 13:29:52 2017 +0200 (20 months ago)
changeset 66699 16fd7655d39d
parent 66698 5b9dc3f7bcde
child 66700 5174ce7c84f0
permissions -rw-r--r--
slightly more parallelism;
     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_base: Sessions.Base,
    18   val log: Logger = No_Logger)
    19 {
    20   val thy_info = new Thy_Info(this)
    21 
    22   def thy_path(path: Path): Path = path.ext("thy")
    23 
    24 
    25   /* file-system operations */
    26 
    27   def append(dir: String, source_path: Path): String =
    28     (Path.explode(dir) + source_path).expand.implode
    29 
    30   def append(node_name: Document.Node.Name, source_path: Path): String =
    31     append(node_name.master_dir, source_path)
    32 
    33 
    34   /* source files of Isabelle/ML bootstrap */
    35 
    36   def source_file(raw_name: String): Option[String] =
    37   {
    38     if (Path.is_wellformed(raw_name)) {
    39       if (Path.is_valid(raw_name)) {
    40         def check(p: Path): Option[Path] = if (p.is_file) Some(p) else None
    41 
    42         val path = Path.explode(raw_name)
    43         val path1 =
    44           if (path.is_absolute || path.is_current) check(path)
    45           else {
    46             check(Path.explode("~~/src/Pure") + path) orElse
    47               (if (Isabelle_System.getenv("ML_SOURCES") == "") None
    48                else check(Path.explode("$ML_SOURCES") + path))
    49           }
    50         Some(File.platform_path(path1 getOrElse path))
    51       }
    52       else None
    53     }
    54     else Some(raw_name)
    55   }
    56 
    57 
    58   /* theory files */
    59 
    60   def loaded_files(syntax: Outer_Syntax, name: Document.Node.Name): () => List[Path] =
    61   {
    62     val raw_text = with_thy_reader(name, reader => reader.source.toString)
    63     () => {
    64       val text = Symbol.decode(raw_text)
    65       if (syntax.load_commands_in(text)) {
    66         val spans = syntax.parse_spans(text)
    67         val dir = Path.explode(name.master_dir)
    68         spans.iterator.map(Command.span_files(syntax, _)._1).flatten.
    69           map(a => dir + Path.explode(a)).toList
    70       }
    71       else Nil
    72     }
    73   }
    74 
    75   def pure_files(syntax: Outer_Syntax, session: String, dir: Path): List[Path] =
    76     if (Sessions.is_pure(session)) {
    77       val roots =
    78         for { (name, _) <- Thy_Header.ml_roots }
    79         yield (dir + Path.explode(name)).expand
    80       val files =
    81         for {
    82           (path, (_, theory)) <- roots zip Thy_Header.ml_roots
    83           file <- loaded_files(syntax, Document.Node.Name(path.implode, path.dir.implode, theory))()
    84         } yield file
    85       roots ::: files
    86     }
    87     else Nil
    88 
    89   def theory_qualifier(name: Document.Node.Name): String =
    90     session_base.global_theories.getOrElse(name.theory, Long_Name.qualifier(name.theory))
    91 
    92   def theory_name(qualifier: String, theory0: String): (Boolean, String) =
    93     session_base.loaded_theories.get(theory0) match {
    94       case Some(theory) => (true, theory)
    95       case None =>
    96         val theory =
    97           if (Long_Name.is_qualified(theory0) || session_base.global_theories.isDefinedAt(theory0))
    98             theory0
    99           else Long_Name.qualify(qualifier, theory0)
   100         (false, theory)
   101     }
   102 
   103   def import_name(qualifier: String, dir: String, s: String): Document.Node.Name =
   104     theory_name(qualifier, Thy_Header.import_name(s)) match {
   105       case (true, theory) => Document.Node.Name.loaded_theory(theory)
   106       case (false, theory) =>
   107         session_base.known_theory(theory) match {
   108           case Some(node_name) => node_name
   109           case None =>
   110             val path = Path.explode(s)
   111             val node = append(dir, thy_path(path))
   112             val master_dir = append(dir, path.dir)
   113             Document.Node.Name(node, master_dir, theory)
   114         }
   115     }
   116 
   117   def import_name(name: Document.Node.Name, s: String): Document.Node.Name =
   118     import_name(theory_qualifier(name), name.master_dir, s)
   119 
   120   def with_thy_reader[A](name: Document.Node.Name, f: Reader[Char] => A): A =
   121   {
   122     val path = File.check_file(Path.explode(name.node))
   123     val reader = Scan.byte_reader(path.file)
   124     try { f(reader) } finally { reader.close }
   125   }
   126 
   127   def check_thy_reader(node_name: Document.Node.Name, reader: Reader[Char],
   128     start: Token.Pos = Token.Pos.command, strict: Boolean = true): Document.Node.Header =
   129   {
   130     if (node_name.is_theory && reader.source.length > 0) {
   131       try {
   132         val header = Thy_Header.read(reader, start, strict).decode_symbols
   133 
   134         val base_name = node_name.theory_base_name
   135         val (name, pos) = header.name
   136         if (base_name != name)
   137           error("Bad theory name " + quote(name) +
   138             " for file " + thy_path(Path.basic(base_name)) + Position.here(pos) +
   139             Completion.report_names(pos, 1, List((base_name, ("theory", base_name)))))
   140 
   141         val imports = header.imports.map({ case (s, pos) => (import_name(node_name, s), pos) })
   142         Document.Node.Header(imports, header.keywords, header.abbrevs)
   143       }
   144       catch { case exn: Throwable => Document.Node.bad_header(Exn.message(exn)) }
   145     }
   146     else Document.Node.no_header
   147   }
   148 
   149   def check_thy(name: Document.Node.Name, start: Token.Pos = Token.Pos.command,
   150       strict: Boolean = true): Document.Node.Header =
   151     with_thy_reader(name, check_thy_reader(name, _, start, strict))
   152 
   153 
   154   /* special header */
   155 
   156   def special_header(name: Document.Node.Name): Option[Document.Node.Header] =
   157   {
   158     val imports =
   159       if (Thy_Header.is_ml_root(name.theory)) List(import_name(name, Thy_Header.ML_BOOTSTRAP))
   160       else if (Thy_Header.is_bootstrap(name.theory)) List(import_name(name, Thy_Header.PURE))
   161       else Nil
   162     if (imports.isEmpty) None
   163     else Some(Document.Node.Header(imports.map((_, Position.none))))
   164   }
   165 
   166 
   167   /* blobs */
   168 
   169   def undefined_blobs(nodes: Document.Nodes): List[Document.Node.Name] =
   170     (for {
   171       (node_name, node) <- nodes.iterator
   172       if !session_base.loaded_theory(node_name)
   173       cmd <- node.load_commands.iterator
   174       name <- cmd.blobs_undefined.iterator
   175     } yield name).toList
   176 
   177 
   178   /* document changes */
   179 
   180   def parse_change(
   181       reparse_limit: Int,
   182       previous: Document.Version,
   183       doc_blobs: Document.Blobs,
   184       edits: List[Document.Edit_Text]): Session.Change =
   185     Thy_Syntax.parse_change(this, reparse_limit, previous, doc_blobs, edits)
   186 
   187   def commit(change: Session.Change) { }
   188 }