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