src/Pure/Thy/thy_load.scala
author wenzelm
Wed, 22 Aug 2012 18:04:30 +0200
changeset 48885 d5fdaf7dd1f8
parent 48883 04cd2fddb4d9
child 48889 04deeb14327c
permissions -rw-r--r--
find files via load commands within theory text; clarified Thy_Load.with_thy_text, simplified Thy_Load.check_thy;
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
{
9613780a805b determine source dependencies, relatively to preloaded theories;
wenzelm
parents: 48409
diff changeset
    17
  def thy_path(path: Path): Path = path.ext("thy")
48484
70898d016538 more explicit checks during parsing;
wenzelm
parents: 48422
diff changeset
    18
70898d016538 more explicit checks during parsing;
wenzelm
parents: 48422
diff changeset
    19
  def is_ok(str: String): Boolean =
70898d016538 more explicit checks during parsing;
wenzelm
parents: 48422
diff changeset
    20
    try { thy_path(Path.explode(str)); true }
70898d016538 more explicit checks during parsing;
wenzelm
parents: 48422
diff changeset
    21
    catch { case ERROR(_) => false }
48422
9613780a805b determine source dependencies, relatively to preloaded theories;
wenzelm
parents: 48409
diff changeset
    22
}
44953
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    23
48422
9613780a805b determine source dependencies, relatively to preloaded theories;
wenzelm
parents: 48409
diff changeset
    24
48870
4accee106f0f clarified initialization of Thy_Load, Thy_Info, Session;
wenzelm
parents: 48827
diff changeset
    25
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
    26
{
44953
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    27
  /* file-system operations */
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 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
    30
    (Path.explode(dir) + source_path).expand.implode
44953
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    31
48885
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    32
  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
    33
  {
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    34
    val path = Path.explode(name.node)
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    35
    if (!path.is_file) error("No such file: " + path.toString)
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    36
    f(File.read(path))
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    37
  }
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    38
46737
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    39
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    40
  /* theory files */
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    41
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    42
  private def import_name(dir: String, s: String): Document.Node.Name =
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    43
  {
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    44
    val theory = Thy_Header.base_name(s)
48870
4accee106f0f clarified initialization of Thy_Load, Thy_Info, Session;
wenzelm
parents: 48827
diff changeset
    45
    if (loaded_theories(theory)) Document.Node.Name(theory, "", theory)
46737
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    46
    else {
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    47
      val path = Path.explode(s)
48422
9613780a805b determine source dependencies, relatively to preloaded theories;
wenzelm
parents: 48409
diff changeset
    48
      val node = append(dir, Thy_Load.thy_path(path))
46737
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    49
      val dir1 = append(dir, path.dir)
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    50
      Document.Node.Name(node, dir1, theory)
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    51
    }
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    52
  }
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    53
48885
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    54
  private def find_file(tokens: List[Token]): Option[String] =
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    55
  {
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    56
    def clean(toks: List[Token]): List[Token] =
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    57
      toks match {
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    58
        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
    59
        case t :: ts => t :: clean(ts)
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    60
        case Nil => Nil
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    61
      }
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    62
    val clean_tokens = clean(tokens.filter(_.is_proper))
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    63
    clean_tokens.reverse.find(_.is_name).map(_.content)
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    64
  }
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    65
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    66
  def find_files(syntax: Outer_Syntax, text: String): List[String] =
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    67
  {
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    68
    val thy_load_commands = syntax.thy_load_commands
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    69
    if (thy_load_commands.exists({ case (cmd, _) => text.containsSlice(cmd) })) {
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    70
      val spans = Thy_Syntax.parse_spans(syntax.scan(text))
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    71
      spans.iterator.map({
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    72
        case toks @ (tok :: _) if tok.is_command =>
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    73
          thy_load_commands.find({ case (cmd, _) => cmd == tok.content }) match {
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    74
            case Some((_, exts)) =>
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    75
              find_file(toks) match {
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    76
                case Some(file) =>
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    77
                  if (exts.isEmpty) List(file)
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    78
                  else exts.map(ext => file + "." + ext)
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    79
                case None => Nil
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    80
              }
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    81
            case None => Nil
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    82
          }
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    83
        case _ => Nil
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    84
      }).flatten.toList
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    85
    }
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    86
    else Nil
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    87
  }
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    88
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    89
  def check_thy_text(name: Document.Node.Name, text: CharSequence): Document.Node.Header =
46737
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    90
  {
48882
61dc7d5d150a use Thy_Header.read on explicit text only -- potentially via File.read, not Scan.byte_reader;
wenzelm
parents: 48870
diff changeset
    91
    val header = Thy_Header.read(text)
48885
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    92
46737
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    93
    val name1 = header.name
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    94
    if (name.theory != name1)
48422
9613780a805b determine source dependencies, relatively to preloaded theories;
wenzelm
parents: 48409
diff changeset
    95
      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
    96
        " for theory " + quote(name1))
48885
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
    val imports = header.imports.map(import_name(name.dir, _))
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    99
    val uses = header.uses
48707
ba531af91148 simplified Document.Node.Header -- internalized errors;
wenzelm
parents: 48484
diff changeset
   100
    Document.Node.Header(imports, header.keywords, uses)
46737
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
   101
  }
46748
8f3ae4d04a2d refined node_header -- more direct buffer access (again);
wenzelm
parents: 46737
diff changeset
   102
48885
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
   103
  def check_thy(name: Document.Node.Name): Document.Node.Header =
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
   104
    with_thy_text(name, check_thy_text(name, _))
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_files(syntax: Outer_Syntax, name: Document.Node.Name): Document.Node.Header =
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
   107
    with_thy_text(name, text =>
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
   108
      {
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
   109
        val string = text.toString
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
   110
        val header = check_thy_text(name, string)
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
   111
        val more_uses = find_files(syntax, string)
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
   112
        header.copy(uses = header.uses ::: more_uses.map((_, false)))
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
   113
      })
43651
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
   114
}
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
   115