src/Pure/PIDE/resources.scala
author wenzelm
Thu Oct 05 16:33:36 2017 +0200 (19 months ago)
changeset 66767 294c2e9a689e
parent 66766 19f8385ddfd3
child 66771 925d10a7a610
permissions -rw-r--r--
clarified modules;
     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)).expand).toList
    70       }
    71       else Nil
    72     }
    73   }
    74 
    75   def pure_files(syntax: Outer_Syntax, dir: Path): List[Path] =
    76   {
    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 
    88   def theory_qualifier(name: Document.Node.Name): String =
    89     session_base.global_theories.getOrElse(name.theory, Long_Name.qualifier(name.theory))
    90 
    91   def theory_name(qualifier: String, theory: String): (Boolean, String) =
    92     if (session_base.loaded_theory(theory)) (true, theory)
    93     else {
    94       val theory1 =
    95         if (Long_Name.is_qualified(theory) || session_base.global_theories.isDefinedAt(theory))
    96           theory
    97         else Long_Name.qualify(qualifier, theory)
    98       (false, theory1)
    99     }
   100 
   101   def import_name(qualifier: String, dir: String, s: String): Document.Node.Name =
   102     theory_name(qualifier, Thy_Header.import_name(s)) match {
   103       case (true, theory) => Document.Node.Name.loaded_theory(theory)
   104       case (false, theory) =>
   105         session_base.known_theory(theory) match {
   106           case Some(node_name) => node_name
   107           case None =>
   108             val path = Path.explode(s)
   109             val node = append(dir, thy_path(path))
   110             val master_dir = append(dir, path.dir)
   111             Document.Node.Name(node, master_dir, theory)
   112         }
   113     }
   114 
   115   def import_name(name: Document.Node.Name, s: String): Document.Node.Name =
   116     import_name(theory_qualifier(name), name.master_dir, s)
   117 
   118   def standard_import(session_resources: Resources,
   119     qualifier: String, dir: String, s: String): String =
   120   {
   121     val name = import_name(qualifier, dir, s)
   122     val s1 =
   123       if (session_base.loaded_theory(name)) name.theory
   124       else {
   125         session_base.known.get_file(name.path.file) match {
   126           case Some(name1) if session_resources.theory_qualifier(name1) != qualifier =>
   127             name1.theory
   128           case Some(name1) if Thy_Header.is_base_name(s) =>
   129             name1.theory_base_name
   130           case _ => s
   131         }
   132       }
   133     val name2 = import_name(qualifier, dir, s1)
   134     if (name.node == name2.node) s1 else s
   135   }
   136 
   137   def with_thy_reader[A](name: Document.Node.Name, f: Reader[Char] => A): A =
   138   {
   139     val path = File.check_file(name.path)
   140     val reader = Scan.byte_reader(path.file)
   141     try { f(reader) } finally { reader.close }
   142   }
   143 
   144   def check_thy_reader(node_name: Document.Node.Name, reader: Reader[Char],
   145     start: Token.Pos = Token.Pos.command, strict: Boolean = true): Document.Node.Header =
   146   {
   147     if (node_name.is_theory && reader.source.length > 0) {
   148       try {
   149         val header = Thy_Header.read(reader, start, strict)
   150 
   151         val base_name = node_name.theory_base_name
   152         val (name, pos) = header.name
   153         if (base_name != name)
   154           error("Bad theory name " + quote(name) +
   155             " for file " + thy_path(Path.basic(base_name)) + Position.here(pos) +
   156             Completion.report_theories(pos, List(base_name)))
   157 
   158         val imports = header.imports.map({ case (s, pos) => (import_name(node_name, s), pos) })
   159         Document.Node.Header(imports, header.keywords, header.abbrevs)
   160       }
   161       catch { case exn: Throwable => Document.Node.bad_header(Exn.message(exn)) }
   162     }
   163     else Document.Node.no_header
   164   }
   165 
   166   def check_thy(name: Document.Node.Name, start: Token.Pos = Token.Pos.command,
   167       strict: Boolean = true): Document.Node.Header =
   168     with_thy_reader(name, check_thy_reader(name, _, start, strict))
   169 
   170 
   171   /* special header */
   172 
   173   def special_header(name: Document.Node.Name): Option[Document.Node.Header] =
   174   {
   175     val imports =
   176       if (Thy_Header.is_ml_root(name.theory)) List(import_name(name, Thy_Header.ML_BOOTSTRAP))
   177       else if (Thy_Header.is_bootstrap(name.theory)) List(import_name(name, Thy_Header.PURE))
   178       else Nil
   179     if (imports.isEmpty) None
   180     else Some(Document.Node.Header(imports.map((_, Position.none))))
   181   }
   182 
   183 
   184   /* blobs */
   185 
   186   def undefined_blobs(nodes: Document.Nodes): List[Document.Node.Name] =
   187     (for {
   188       (node_name, node) <- nodes.iterator
   189       if !session_base.loaded_theory(node_name)
   190       cmd <- node.load_commands.iterator
   191       name <- cmd.blobs_undefined.iterator
   192     } yield name).toList
   193 
   194 
   195   /* document changes */
   196 
   197   def parse_change(
   198       reparse_limit: Int,
   199       previous: Document.Version,
   200       doc_blobs: Document.Blobs,
   201       edits: List[Document.Edit_Text]): Session.Change =
   202     Thy_Syntax.parse_change(this, reparse_limit, previous, doc_blobs, edits)
   203 
   204   def commit(change: Session.Change) { }
   205 }