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