src/Pure/Thy/thy_load.scala
author wenzelm
Tue, 24 Jul 2012 20:56:18 +0200
changeset 48484 70898d016538
parent 48422 9613780a805b
child 48707 ba531af91148
permissions -rw-r--r--
more explicit checks during parsing;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43651
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/Thy/thy_load.scala
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
     3
44577
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents: 44574
diff changeset
     4
Primitives for loading theory files.
43651
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
     5
*/
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
     6
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
     7
package isabelle
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
     8
44953
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
     9
48409
0d2114eb412a more explicit java.io.{File => JFile};
wenzelm
parents: 46938
diff changeset
    10
import java.io.{File => JFile}
44953
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    11
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    12
48422
9613780a805b determine source dependencies, relatively to preloaded theories;
wenzelm
parents: 48409
diff changeset
    13
object Thy_Load
9613780a805b determine source dependencies, relatively to preloaded theories;
wenzelm
parents: 48409
diff changeset
    14
{
9613780a805b determine source dependencies, relatively to preloaded theories;
wenzelm
parents: 48409
diff changeset
    15
  def thy_path(path: Path): Path = path.ext("thy")
48484
70898d016538 more explicit checks during parsing;
wenzelm
parents: 48422
diff changeset
    16
70898d016538 more explicit checks during parsing;
wenzelm
parents: 48422
diff changeset
    17
  def is_ok(str: String): Boolean =
70898d016538 more explicit checks during parsing;
wenzelm
parents: 48422
diff changeset
    18
    try { thy_path(Path.explode(str)); true }
70898d016538 more explicit checks during parsing;
wenzelm
parents: 48422
diff changeset
    19
    catch { case ERROR(_) => false }
48422
9613780a805b determine source dependencies, relatively to preloaded theories;
wenzelm
parents: 48409
diff changeset
    20
}
44953
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    21
48422
9613780a805b determine source dependencies, relatively to preloaded theories;
wenzelm
parents: 48409
diff changeset
    22
9613780a805b determine source dependencies, relatively to preloaded theories;
wenzelm
parents: 48409
diff changeset
    23
class Thy_Load(preloaded: Set[String] = Set.empty)
43651
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    24
{
44953
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    25
  /* loaded theories provided by prover */
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    26
48422
9613780a805b determine source dependencies, relatively to preloaded theories;
wenzelm
parents: 48409
diff changeset
    27
  private var loaded_theories: Set[String] = preloaded
44953
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    28
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    29
  def register_thy(thy_name: String): Unit =
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    30
    synchronized { loaded_theories += thy_name }
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    31
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    32
  def is_loaded(thy_name: String): Boolean =
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    33
    synchronized { loaded_theories.contains(thy_name) }
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    34
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    35
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    36
  /* file-system operations */
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    37
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    38
  def append(dir: String, source_path: Path): String =
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    39
    (Path.explode(dir) + source_path).implode
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    40
46737
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    41
  def read_header(name: Document.Node.Name): Thy_Header =
44953
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    42
  {
48409
0d2114eb412a more explicit java.io.{File => JFile};
wenzelm
parents: 46938
diff changeset
    43
    val file = new JFile(name.node)
44953
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    44
    if (!file.exists || !file.isFile) error("No such file: " + quote(file.toString))
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    45
    Thy_Header.read(file)
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    46
  }
46737
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    47
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    48
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    49
  /* theory files */
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    50
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    51
  private def import_name(dir: String, s: String): Document.Node.Name =
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    52
  {
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    53
    val theory = Thy_Header.base_name(s)
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    54
    if (is_loaded(theory)) Document.Node.Name(theory, "", theory)
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    55
    else {
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    56
      val path = Path.explode(s)
48422
9613780a805b determine source dependencies, relatively to preloaded theories;
wenzelm
parents: 48409
diff changeset
    57
      val node = append(dir, Thy_Load.thy_path(path))
46737
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    58
      val dir1 = append(dir, path.dir)
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    59
      Document.Node.Name(node, dir1, theory)
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    60
    }
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    61
  }
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    62
46748
8f3ae4d04a2d refined node_header -- more direct buffer access (again);
wenzelm
parents: 46737
diff changeset
    63
  def check_header(name: Document.Node.Name, header: Thy_Header): Document.Node.Deps =
46737
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    64
  {
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    65
    val name1 = header.name
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    66
    val imports = header.imports.map(import_name(name.dir, _))
46770
44c28a33c461 retain original "uses" (again) -- still required for Thy_Load.use_file etc. in ML (notably for maintaining required/provided);
wenzelm
parents: 46748
diff changeset
    67
    // FIXME val uses = header.uses.map(p => (append(name.dir, Path.explode(p._1)), p._2))
44c28a33c461 retain original "uses" (again) -- still required for Thy_Load.use_file etc. in ML (notably for maintaining required/provided);
wenzelm
parents: 46748
diff changeset
    68
    val uses = header.uses
46737
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    69
    if (name.theory != name1)
48422
9613780a805b determine source dependencies, relatively to preloaded theories;
wenzelm
parents: 48409
diff changeset
    70
      error("Bad file name " + Thy_Load.thy_path(Path.basic(name.theory)) +
9613780a805b determine source dependencies, relatively to preloaded theories;
wenzelm
parents: 48409
diff changeset
    71
        " for theory " + quote(name1))
46938
cda018294515 some support for outer syntax keyword declarations within theory header;
wenzelm
parents: 46770
diff changeset
    72
    Document.Node.Deps(imports, header.keywords, uses)
46737
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    73
  }
46748
8f3ae4d04a2d refined node_header -- more direct buffer access (again);
wenzelm
parents: 46737
diff changeset
    74
8f3ae4d04a2d refined node_header -- more direct buffer access (again);
wenzelm
parents: 46737
diff changeset
    75
  def check_thy(name: Document.Node.Name): Document.Node.Deps =
8f3ae4d04a2d refined node_header -- more direct buffer access (again);
wenzelm
parents: 46737
diff changeset
    76
    check_header(name, read_header(name))
43651
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    77
}
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    78