src/Pure/PIDE/resources.scala
changeset 56208 06cc31dff138
parent 55879 ac979f750c1a
child 56209 3c89e21d9be2
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/Pure/PIDE/resources.scala	Tue Mar 18 17:39:03 2014 +0100
     1.3 @@ -0,0 +1,111 @@
     1.4 +/*  Title:      Pure/PIDE/resources.scala
     1.5 +    Author:     Makarius
     1.6 +
     1.7 +Resources for theories and auxiliary files.
     1.8 +*/
     1.9 +
    1.10 +package isabelle
    1.11 +
    1.12 +
    1.13 +import scala.annotation.tailrec
    1.14 +
    1.15 +import java.io.{File => JFile}
    1.16 +
    1.17 +
    1.18 +object Resources
    1.19 +{
    1.20 +  /* paths */
    1.21 +
    1.22 +  def thy_path(path: Path): Path = path.ext("thy")
    1.23 +
    1.24 +  def is_wellformed_thy_path(str: String): Boolean =
    1.25 +    try { thy_path(Path.explode(str)); true }
    1.26 +    catch { case ERROR(_) => false }
    1.27 +}
    1.28 +
    1.29 +
    1.30 +class Resources(val loaded_theories: Set[String] = Set.empty, val base_syntax: Outer_Syntax)
    1.31 +{
    1.32 +  /* document node names */
    1.33 +
    1.34 +  def node_name(raw_path: Path): Document.Node.Name =
    1.35 +  {
    1.36 +    val path = raw_path.expand
    1.37 +    val node = path.implode
    1.38 +    val theory = Thy_Header.thy_name(node).getOrElse("")
    1.39 +    val master_dir = if (theory == "") "" else path.dir.implode
    1.40 +    Document.Node.Name(node, master_dir, theory)
    1.41 +  }
    1.42 +
    1.43 +
    1.44 +  /* file-system operations */
    1.45 +
    1.46 +  def append(dir: String, source_path: Path): String =
    1.47 +    (Path.explode(dir) + source_path).expand.implode
    1.48 +
    1.49 +  def with_thy_text[A](name: Document.Node.Name, f: CharSequence => A): A =
    1.50 +  {
    1.51 +    val path = Path.explode(name.node)
    1.52 +    if (!path.is_file) error("No such file: " + path.toString)
    1.53 +
    1.54 +    val text = File.read(path)
    1.55 +    Symbol.decode_strict(text)
    1.56 +    f(text)
    1.57 +  }
    1.58 +
    1.59 +
    1.60 +  /* theory files */
    1.61 +
    1.62 +  def body_files_test(syntax: Outer_Syntax, text: String): Boolean =
    1.63 +    syntax.thy_load_commands.exists({ case (cmd, _) => text.containsSlice(cmd) })
    1.64 +
    1.65 +  def body_files(syntax: Outer_Syntax, text: String): List[String] =
    1.66 +  {
    1.67 +    val spans = Thy_Syntax.parse_spans(syntax.scan(text))
    1.68 +    spans.iterator.map(Thy_Syntax.span_files(syntax, _)).flatten.toList
    1.69 +  }
    1.70 +
    1.71 +  def import_name(master: Document.Node.Name, s: String): Document.Node.Name =
    1.72 +  {
    1.73 +    val theory = Thy_Header.base_name(s)
    1.74 +    if (loaded_theories(theory)) Document.Node.Name(theory + ".thy", "", theory)
    1.75 +    else {
    1.76 +      val path = Path.explode(s)
    1.77 +      val node = append(master.master_dir, Resources.thy_path(path))
    1.78 +      val master_dir = append(master.master_dir, path.dir)
    1.79 +      Document.Node.Name(node, master_dir, theory)
    1.80 +    }
    1.81 +  }
    1.82 +
    1.83 +  def check_thy_text(name: Document.Node.Name, text: CharSequence): Document.Node.Header =
    1.84 +  {
    1.85 +    try {
    1.86 +      val header = Thy_Header.read(text)
    1.87 +
    1.88 +      val name1 = header.name
    1.89 +      if (name.theory != name1)
    1.90 +        error("Bad file name " + Resources.thy_path(Path.basic(name.theory)) +
    1.91 +          " for theory " + quote(name1))
    1.92 +
    1.93 +      val imports = header.imports.map(import_name(name, _))
    1.94 +      Document.Node.Header(imports, header.keywords, Nil)
    1.95 +    }
    1.96 +    catch { case exn: Throwable => Document.Node.bad_header(Exn.message(exn)) }
    1.97 +  }
    1.98 +
    1.99 +  def check_thy(name: Document.Node.Name): Document.Node.Header =
   1.100 +    with_thy_text(name, check_thy_text(name, _))
   1.101 +
   1.102 +
   1.103 +  /* theory text edits */
   1.104 +
   1.105 +  def text_edits(
   1.106 +    reparse_limit: Int,
   1.107 +    previous: Document.Version,
   1.108 +    doc_blobs: Document.Blobs,
   1.109 +    edits: List[Document.Edit_Text]): (Boolean, List[Document.Edit_Command], Document.Version) =
   1.110 +    Thy_Syntax.text_edits(this, reparse_limit, previous, doc_blobs, edits)
   1.111 +
   1.112 +  def syntax_changed() { }
   1.113 +}
   1.114 +