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