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