src/Pure/Thy/thy_load.scala
author wenzelm
Tue Aug 21 12:15:25 2012 +0200 (2012-08-21)
changeset 48870 4accee106f0f
parent 48827 8791d106e30b
child 48882 61dc7d5d150a
permissions -rw-r--r--
clarified initialization of Thy_Load, Thy_Info, Session;
wenzelm@43651
     1
/*  Title:      Pure/Thy/thy_load.scala
wenzelm@43651
     2
    Author:     Makarius
wenzelm@43651
     3
wenzelm@44577
     4
Primitives for loading theory files.
wenzelm@43651
     5
*/
wenzelm@43651
     6
wenzelm@43651
     7
package isabelle
wenzelm@43651
     8
wenzelm@44953
     9
wenzelm@48409
    10
import java.io.{File => JFile}
wenzelm@44953
    11
wenzelm@44953
    12
wenzelm@48422
    13
object Thy_Load
wenzelm@48422
    14
{
wenzelm@48422
    15
  def thy_path(path: Path): Path = path.ext("thy")
wenzelm@48484
    16
wenzelm@48484
    17
  def is_ok(str: String): Boolean =
wenzelm@48484
    18
    try { thy_path(Path.explode(str)); true }
wenzelm@48484
    19
    catch { case ERROR(_) => false }
wenzelm@48422
    20
}
wenzelm@44953
    21
wenzelm@48422
    22
wenzelm@48870
    23
class Thy_Load(val loaded_theories: Set[String] = Set.empty, val base_syntax: Outer_Syntax)
wenzelm@43651
    24
{
wenzelm@44953
    25
  /* file-system operations */
wenzelm@44953
    26
wenzelm@44953
    27
  def append(dir: String, source_path: Path): String =
wenzelm@48711
    28
    (Path.explode(dir) + source_path).expand.implode
wenzelm@44953
    29
wenzelm@46737
    30
  def read_header(name: Document.Node.Name): Thy_Header =
wenzelm@44953
    31
  {
wenzelm@48827
    32
    val path = Path.explode(name.node)
wenzelm@48827
    33
    if (!path.is_file) error("No such file: " + path.toString)
wenzelm@48827
    34
    Thy_Header.read(path.file)
wenzelm@44953
    35
  }
wenzelm@46737
    36
wenzelm@46737
    37
wenzelm@46737
    38
  /* theory files */
wenzelm@46737
    39
wenzelm@46737
    40
  private def import_name(dir: String, s: String): Document.Node.Name =
wenzelm@46737
    41
  {
wenzelm@46737
    42
    val theory = Thy_Header.base_name(s)
wenzelm@48870
    43
    if (loaded_theories(theory)) Document.Node.Name(theory, "", theory)
wenzelm@46737
    44
    else {
wenzelm@46737
    45
      val path = Path.explode(s)
wenzelm@48422
    46
      val node = append(dir, Thy_Load.thy_path(path))
wenzelm@46737
    47
      val dir1 = append(dir, path.dir)
wenzelm@46737
    48
      Document.Node.Name(node, dir1, theory)
wenzelm@46737
    49
    }
wenzelm@46737
    50
  }
wenzelm@46737
    51
wenzelm@48707
    52
  def check_header(name: Document.Node.Name, header: Thy_Header): Document.Node.Header =
wenzelm@46737
    53
  {
wenzelm@46737
    54
    val name1 = header.name
wenzelm@46737
    55
    val imports = header.imports.map(import_name(name.dir, _))
wenzelm@46770
    56
    // FIXME val uses = header.uses.map(p => (append(name.dir, Path.explode(p._1)), p._2))
wenzelm@46770
    57
    val uses = header.uses
wenzelm@46737
    58
    if (name.theory != name1)
wenzelm@48422
    59
      error("Bad file name " + Thy_Load.thy_path(Path.basic(name.theory)) +
wenzelm@48422
    60
        " for theory " + quote(name1))
wenzelm@48707
    61
    Document.Node.Header(imports, header.keywords, uses)
wenzelm@46737
    62
  }
wenzelm@46748
    63
wenzelm@48707
    64
  def check_thy(name: Document.Node.Name): Document.Node.Header =
wenzelm@46748
    65
    check_header(name, read_header(name))
wenzelm@43651
    66
}
wenzelm@43651
    67