src/Pure/Thy/thy_load.scala
author wenzelm
Fri, 07 Dec 2012 13:38:32 +0100
changeset 50414 e17a1f179bb0
parent 50291 674893679352
child 50415 0d60de55c58a
permissions -rw-r--r--
explore theory_body_files via future, for improved performance; further internalization of header errors;
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
48885
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    10
import scala.annotation.tailrec
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    11
48409
0d2114eb412a more explicit java.io.{File => JFile};
wenzelm
parents: 46938
diff changeset
    12
import java.io.{File => JFile}
44953
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    13
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    14
48422
9613780a805b determine source dependencies, relatively to preloaded theories;
wenzelm
parents: 48409
diff changeset
    15
object Thy_Load
9613780a805b determine source dependencies, relatively to preloaded theories;
wenzelm
parents: 48409
diff changeset
    16
{
50204
daeb1674fb91 tuned signature -- avoid intrusion of module Path in generic PIDE concepts;
wenzelm
parents: 48889
diff changeset
    17
  /* paths */
daeb1674fb91 tuned signature -- avoid intrusion of module Path in generic PIDE concepts;
wenzelm
parents: 48889
diff changeset
    18
48422
9613780a805b determine source dependencies, relatively to preloaded theories;
wenzelm
parents: 48409
diff changeset
    19
  def thy_path(path: Path): Path = path.ext("thy")
48484
70898d016538 more explicit checks during parsing;
wenzelm
parents: 48422
diff changeset
    20
70898d016538 more explicit checks during parsing;
wenzelm
parents: 48422
diff changeset
    21
  def is_ok(str: String): Boolean =
70898d016538 more explicit checks during parsing;
wenzelm
parents: 48422
diff changeset
    22
    try { thy_path(Path.explode(str)); true }
70898d016538 more explicit checks during parsing;
wenzelm
parents: 48422
diff changeset
    23
    catch { case ERROR(_) => false }
50204
daeb1674fb91 tuned signature -- avoid intrusion of module Path in generic PIDE concepts;
wenzelm
parents: 48889
diff changeset
    24
daeb1674fb91 tuned signature -- avoid intrusion of module Path in generic PIDE concepts;
wenzelm
parents: 48889
diff changeset
    25
daeb1674fb91 tuned signature -- avoid intrusion of module Path in generic PIDE concepts;
wenzelm
parents: 48889
diff changeset
    26
  /* node names */
daeb1674fb91 tuned signature -- avoid intrusion of module Path in generic PIDE concepts;
wenzelm
parents: 48889
diff changeset
    27
daeb1674fb91 tuned signature -- avoid intrusion of module Path in generic PIDE concepts;
wenzelm
parents: 48889
diff changeset
    28
  def external_node(raw_path: Path): Document.Node.Name =
daeb1674fb91 tuned signature -- avoid intrusion of module Path in generic PIDE concepts;
wenzelm
parents: 48889
diff changeset
    29
  {
daeb1674fb91 tuned signature -- avoid intrusion of module Path in generic PIDE concepts;
wenzelm
parents: 48889
diff changeset
    30
    val path = raw_path.expand
daeb1674fb91 tuned signature -- avoid intrusion of module Path in generic PIDE concepts;
wenzelm
parents: 48889
diff changeset
    31
    val node = path.implode
daeb1674fb91 tuned signature -- avoid intrusion of module Path in generic PIDE concepts;
wenzelm
parents: 48889
diff changeset
    32
    val dir = path.dir.implode
daeb1674fb91 tuned signature -- avoid intrusion of module Path in generic PIDE concepts;
wenzelm
parents: 48889
diff changeset
    33
    val theory = Thy_Header.thy_name(node) getOrElse error("Bad theory file name: " + path)
daeb1674fb91 tuned signature -- avoid intrusion of module Path in generic PIDE concepts;
wenzelm
parents: 48889
diff changeset
    34
    Document.Node.Name(node, dir, theory)
daeb1674fb91 tuned signature -- avoid intrusion of module Path in generic PIDE concepts;
wenzelm
parents: 48889
diff changeset
    35
  }
48422
9613780a805b determine source dependencies, relatively to preloaded theories;
wenzelm
parents: 48409
diff changeset
    36
}
44953
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    37
48422
9613780a805b determine source dependencies, relatively to preloaded theories;
wenzelm
parents: 48409
diff changeset
    38
48870
4accee106f0f clarified initialization of Thy_Load, Thy_Info, Session;
wenzelm
parents: 48827
diff changeset
    39
class Thy_Load(val loaded_theories: Set[String] = Set.empty, val base_syntax: Outer_Syntax)
43651
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    40
{
44953
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    41
  /* file-system operations */
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    42
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    43
  def append(dir: String, source_path: Path): String =
48711
8d381fdef898 need to expand path in order to resolve imports like "~~/src/Tools/Code_Generator";
wenzelm
parents: 48710
diff changeset
    44
    (Path.explode(dir) + source_path).expand.implode
44953
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    45
48885
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    46
  def with_thy_text[A](name: Document.Node.Name, f: CharSequence => A): A =
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    47
  {
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    48
    val path = Path.explode(name.node)
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    49
    if (!path.is_file) error("No such file: " + path.toString)
50291
674893679352 prefer Symbol.decode_strict in batch mode, to avoid files with spurious Unicode symbols that clash with Isabelle symbol interpretation;
wenzelm
parents: 50204
diff changeset
    50
674893679352 prefer Symbol.decode_strict in batch mode, to avoid files with spurious Unicode symbols that clash with Isabelle symbol interpretation;
wenzelm
parents: 50204
diff changeset
    51
    val text = File.read(path)
674893679352 prefer Symbol.decode_strict in batch mode, to avoid files with spurious Unicode symbols that clash with Isabelle symbol interpretation;
wenzelm
parents: 50204
diff changeset
    52
    Symbol.decode_strict(text)
674893679352 prefer Symbol.decode_strict in batch mode, to avoid files with spurious Unicode symbols that clash with Isabelle symbol interpretation;
wenzelm
parents: 50204
diff changeset
    53
    f(text)
48885
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    54
  }
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    55
46737
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    56
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    57
  /* theory files */
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    58
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    59
  private def import_name(dir: String, s: String): Document.Node.Name =
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    60
  {
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    61
    val theory = Thy_Header.base_name(s)
48870
4accee106f0f clarified initialization of Thy_Load, Thy_Info, Session;
wenzelm
parents: 48827
diff changeset
    62
    if (loaded_theories(theory)) Document.Node.Name(theory, "", theory)
46737
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    63
    else {
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    64
      val path = Path.explode(s)
48422
9613780a805b determine source dependencies, relatively to preloaded theories;
wenzelm
parents: 48409
diff changeset
    65
      val node = append(dir, Thy_Load.thy_path(path))
46737
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    66
      val dir1 = append(dir, path.dir)
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    67
      Document.Node.Name(node, dir1, theory)
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    68
    }
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    69
  }
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    70
48885
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    71
  private def find_file(tokens: List[Token]): Option[String] =
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    72
  {
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    73
    def clean(toks: List[Token]): List[Token] =
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    74
      toks match {
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    75
        case t :: _ :: ts if t.is_keyword && (t.source == "%" || t.source == "--") => clean(ts)
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    76
        case t :: ts => t :: clean(ts)
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    77
        case Nil => Nil
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    78
      }
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    79
    val clean_tokens = clean(tokens.filter(_.is_proper))
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    80
    clean_tokens.reverse.find(_.is_name).map(_.content)
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    81
  }
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    82
50414
e17a1f179bb0 explore theory_body_files via future, for improved performance;
wenzelm
parents: 50291
diff changeset
    83
  def theory_body_files(syntax: Outer_Syntax, text: String): List[String] =
48885
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    84
  {
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    85
    val thy_load_commands = syntax.thy_load_commands
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    86
    if (thy_load_commands.exists({ case (cmd, _) => text.containsSlice(cmd) })) {
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    87
      val spans = Thy_Syntax.parse_spans(syntax.scan(text))
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    88
      spans.iterator.map({
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    89
        case toks @ (tok :: _) if tok.is_command =>
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    90
          thy_load_commands.find({ case (cmd, _) => cmd == tok.content }) match {
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    91
            case Some((_, exts)) =>
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    92
              find_file(toks) match {
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    93
                case Some(file) =>
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    94
                  if (exts.isEmpty) List(file)
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    95
                  else exts.map(ext => file + "." + ext)
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    96
                case None => Nil
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    97
              }
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    98
            case None => Nil
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    99
          }
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
   100
        case _ => Nil
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
   101
      }).flatten.toList
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
   102
    }
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
   103
    else Nil
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
   104
  }
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
   105
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
   106
  def check_thy_text(name: Document.Node.Name, text: CharSequence): Document.Node.Header =
46737
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
   107
  {
50414
e17a1f179bb0 explore theory_body_files via future, for improved performance;
wenzelm
parents: 50291
diff changeset
   108
    try {
e17a1f179bb0 explore theory_body_files via future, for improved performance;
wenzelm
parents: 50291
diff changeset
   109
      val header = Thy_Header.read(text)
48885
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
   110
50414
e17a1f179bb0 explore theory_body_files via future, for improved performance;
wenzelm
parents: 50291
diff changeset
   111
      val name1 = header.name
e17a1f179bb0 explore theory_body_files via future, for improved performance;
wenzelm
parents: 50291
diff changeset
   112
      if (name.theory != name1)
e17a1f179bb0 explore theory_body_files via future, for improved performance;
wenzelm
parents: 50291
diff changeset
   113
        error("Bad file name " + Thy_Load.thy_path(Path.basic(name.theory)) +
e17a1f179bb0 explore theory_body_files via future, for improved performance;
wenzelm
parents: 50291
diff changeset
   114
          " for theory " + quote(name1))
48885
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
   115
50414
e17a1f179bb0 explore theory_body_files via future, for improved performance;
wenzelm
parents: 50291
diff changeset
   116
      val imports = header.imports.map(import_name(name.dir, _))
e17a1f179bb0 explore theory_body_files via future, for improved performance;
wenzelm
parents: 50291
diff changeset
   117
      val uses = header.uses
e17a1f179bb0 explore theory_body_files via future, for improved performance;
wenzelm
parents: 50291
diff changeset
   118
      Document.Node.Header(imports, header.keywords, uses)
e17a1f179bb0 explore theory_body_files via future, for improved performance;
wenzelm
parents: 50291
diff changeset
   119
    }
e17a1f179bb0 explore theory_body_files via future, for improved performance;
wenzelm
parents: 50291
diff changeset
   120
    catch { case e: Throwable => Document.Node.bad_header(Exn.message(e)) }
46737
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
   121
  }
46748
8f3ae4d04a2d refined node_header -- more direct buffer access (again);
wenzelm
parents: 46737
diff changeset
   122
48885
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
   123
  def check_thy(name: Document.Node.Name): Document.Node.Header =
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
   124
    with_thy_text(name, check_thy_text(name, _))
43651
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
   125
}
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
   126