src/Pure/PIDE/resources.scala
author wenzelm
Mon Apr 17 12:11:02 2017 +0200 (2017-04-17)
changeset 65487 7847807b07ce
parent 65476 a72ae9eb4462
child 65488 331f09d9535e
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 class Resources(
    17   val session_base: Sessions.Base,
    18   val default_qualifier: String = Sessions.DRAFT,
    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 
    32   /* source files of Isabelle/ML bootstrap */
    33 
    34   def source_file(raw_name: String): Option[String] =
    35   {
    36     if (Path.is_wellformed(raw_name)) {
    37       if (Path.is_valid(raw_name)) {
    38         def check(p: Path): Option[Path] = if (p.is_file) Some(p) else None
    39 
    40         val path = Path.explode(raw_name)
    41         val path1 =
    42           if (path.is_absolute || path.is_current) check(path)
    43           else {
    44             check(Path.explode("~~/src/Pure") + path) orElse
    45               (if (Isabelle_System.getenv("ML_SOURCES") == "") None
    46                else check(Path.explode("$ML_SOURCES") + path))
    47           }
    48         Some(File.platform_path(path1 getOrElse path))
    49       }
    50       else None
    51     }
    52     else Some(raw_name)
    53   }
    54 
    55 
    56   /* theory files */
    57 
    58   def loaded_files(syntax: Outer_Syntax, text: String): List[String] =
    59     if (syntax.load_commands_in(text)) {
    60       val spans = syntax.parse_spans(text)
    61       spans.iterator.map(Command.span_files(syntax, _)._1).flatten.toList
    62     }
    63     else Nil
    64 
    65   def theory_qualifier(name: Document.Node.Name): String =
    66     session_base.global_theories.getOrElse(name.theory, Long_Name.qualifier(name.theory))
    67 
    68   def theory_name(qualifier: String, theory0: String): (Boolean, String) =
    69     session_base.loaded_theories.get(theory0) match {
    70       case Some(theory) => (true, theory)
    71       case None =>
    72         val theory =
    73           if (Long_Name.is_qualified(theory0) || session_base.global_theories.isDefinedAt(theory0)
    74               || true /* FIXME */) theory0
    75           else Long_Name.qualify(qualifier, theory0)
    76         (false, theory)
    77     }
    78 
    79   def import_name(qualifier: String, dir: String, s: String): Document.Node.Name =
    80     theory_name(qualifier, Thy_Header.import_name(s)) match {
    81       case (true, theory) => Document.Node.Name.loaded_theory(theory)
    82       case (false, theory) =>
    83         session_base.known_theories.get(theory) match {
    84           case Some(node_name) => node_name
    85           case None =>
    86             val path = Path.explode(s)
    87             val node = append(dir, thy_path(path))
    88             val master_dir = append(dir, path.dir)
    89             Document.Node.Name(node, master_dir, theory)
    90         }
    91     }
    92 
    93   def with_thy_reader[A](name: Document.Node.Name, f: Reader[Char] => A): A =
    94   {
    95     val path = Path.explode(name.node)
    96     if (!path.is_file) error("No such file: " + path.toString)
    97 
    98     val reader = Scan.byte_reader(path.file)
    99     try { f(reader) } finally { reader.close }
   100   }
   101 
   102   def check_thy_reader(node_name: Document.Node.Name, reader: Reader[Char],
   103     start: Token.Pos = Token.Pos.command, strict: Boolean = true): Document.Node.Header =
   104   {
   105     if (node_name.is_theory && reader.source.length > 0) {
   106       try {
   107         val header = Thy_Header.read(reader, start, strict).decode_symbols
   108 
   109         val base_name = node_name.theory_base_name
   110         val (name, pos) = header.name
   111         if (base_name != name)
   112           error("Bad theory name " + quote(name) +
   113             " for file " + thy_path(Path.basic(base_name)) + Position.here(pos) +
   114             Completion.report_names(pos, 1, List((base_name, ("theory", base_name)))))
   115 
   116         val imports =
   117           header.imports.map({ case (s, pos) =>
   118             (import_name(theory_qualifier(node_name), node_name.master_dir, s), pos) })
   119         Document.Node.Header(imports, header.keywords, header.abbrevs)
   120       }
   121       catch { case exn: Throwable => Document.Node.bad_header(Exn.message(exn)) }
   122     }
   123     else Document.Node.no_header
   124   }
   125 
   126   def check_thy(name: Document.Node.Name, start: Token.Pos = Token.Pos.command,
   127       strict: Boolean = true): Document.Node.Header =
   128     with_thy_reader(name, check_thy_reader(name, _, start, strict))
   129 
   130 
   131   /* special header */
   132 
   133   def special_header(name: Document.Node.Name): Option[Document.Node.Header] =
   134   {
   135     val qualifier = theory_qualifier(name)
   136     val dir = name.master_dir
   137 
   138     val imports =
   139       if (Thy_Header.is_ml_root(name.theory))
   140         List(import_name(qualifier, dir, Thy_Header.ML_BOOTSTRAP))
   141       else if (Thy_Header.is_bootstrap(name.theory))
   142         List(import_name(qualifier, dir, Thy_Header.PURE))
   143       else Nil
   144 
   145     if (imports.isEmpty) None
   146     else Some(Document.Node.Header(imports.map((_, Position.none))))
   147   }
   148 
   149 
   150   /* blobs */
   151 
   152   def undefined_blobs(nodes: Document.Nodes): List[Document.Node.Name] =
   153     (for {
   154       (node_name, node) <- nodes.iterator
   155       if !session_base.loaded_theory(node_name)
   156       cmd <- node.load_commands.iterator
   157       name <- cmd.blobs_undefined.iterator
   158     } yield name).toList
   159 
   160 
   161   /* document changes */
   162 
   163   def parse_change(
   164       reparse_limit: Int,
   165       previous: Document.Version,
   166       doc_blobs: Document.Blobs,
   167       edits: List[Document.Edit_Text]): Session.Change =
   168     Thy_Syntax.parse_change(this, reparse_limit, previous, doc_blobs, edits)
   169 
   170   def commit(change: Session.Change) { }
   171 }