src/Pure/Thy/thy_load.scala
author wenzelm
Wed Aug 22 18:04:30 2012 +0200 (2012-08-22)
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;
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@48885
    10
import scala.annotation.tailrec
wenzelm@48885
    11
wenzelm@48409
    12
import java.io.{File => JFile}
wenzelm@44953
    13
wenzelm@44953
    14
wenzelm@48422
    15
object Thy_Load
wenzelm@48422
    16
{
wenzelm@48422
    17
  def thy_path(path: Path): Path = path.ext("thy")
wenzelm@48484
    18
wenzelm@48484
    19
  def is_ok(str: String): Boolean =
wenzelm@48484
    20
    try { thy_path(Path.explode(str)); true }
wenzelm@48484
    21
    catch { case ERROR(_) => false }
wenzelm@48422
    22
}
wenzelm@44953
    23
wenzelm@48422
    24
wenzelm@48870
    25
class Thy_Load(val loaded_theories: Set[String] = Set.empty, val base_syntax: Outer_Syntax)
wenzelm@43651
    26
{
wenzelm@44953
    27
  /* file-system operations */
wenzelm@44953
    28
wenzelm@44953
    29
  def append(dir: String, source_path: Path): String =
wenzelm@48711
    30
    (Path.explode(dir) + source_path).expand.implode
wenzelm@44953
    31
wenzelm@48885
    32
  def with_thy_text[A](name: Document.Node.Name, f: CharSequence => A): A =
wenzelm@48885
    33
  {
wenzelm@48885
    34
    val path = Path.explode(name.node)
wenzelm@48885
    35
    if (!path.is_file) error("No such file: " + path.toString)
wenzelm@48885
    36
    f(File.read(path))
wenzelm@48885
    37
  }
wenzelm@48885
    38
wenzelm@46737
    39
wenzelm@46737
    40
  /* theory files */
wenzelm@46737
    41
wenzelm@46737
    42
  private def import_name(dir: String, s: String): Document.Node.Name =
wenzelm@46737
    43
  {
wenzelm@46737
    44
    val theory = Thy_Header.base_name(s)
wenzelm@48870
    45
    if (loaded_theories(theory)) Document.Node.Name(theory, "", theory)
wenzelm@46737
    46
    else {
wenzelm@46737
    47
      val path = Path.explode(s)
wenzelm@48422
    48
      val node = append(dir, Thy_Load.thy_path(path))
wenzelm@46737
    49
      val dir1 = append(dir, path.dir)
wenzelm@46737
    50
      Document.Node.Name(node, dir1, theory)
wenzelm@46737
    51
    }
wenzelm@46737
    52
  }
wenzelm@46737
    53
wenzelm@48885
    54
  private def find_file(tokens: List[Token]): Option[String] =
wenzelm@48885
    55
  {
wenzelm@48885
    56
    def clean(toks: List[Token]): List[Token] =
wenzelm@48885
    57
      toks match {
wenzelm@48885
    58
        case t :: _ :: ts if t.is_keyword && (t.source == "%" || t.source == "--") => clean(ts)
wenzelm@48885
    59
        case t :: ts => t :: clean(ts)
wenzelm@48885
    60
        case Nil => Nil
wenzelm@48885
    61
      }
wenzelm@48885
    62
    val clean_tokens = clean(tokens.filter(_.is_proper))
wenzelm@48885
    63
    clean_tokens.reverse.find(_.is_name).map(_.content)
wenzelm@48885
    64
  }
wenzelm@48885
    65
wenzelm@48885
    66
  def find_files(syntax: Outer_Syntax, text: String): List[String] =
wenzelm@48885
    67
  {
wenzelm@48885
    68
    val thy_load_commands = syntax.thy_load_commands
wenzelm@48885
    69
    if (thy_load_commands.exists({ case (cmd, _) => text.containsSlice(cmd) })) {
wenzelm@48885
    70
      val spans = Thy_Syntax.parse_spans(syntax.scan(text))
wenzelm@48885
    71
      spans.iterator.map({
wenzelm@48885
    72
        case toks @ (tok :: _) if tok.is_command =>
wenzelm@48885
    73
          thy_load_commands.find({ case (cmd, _) => cmd == tok.content }) match {
wenzelm@48885
    74
            case Some((_, exts)) =>
wenzelm@48885
    75
              find_file(toks) match {
wenzelm@48885
    76
                case Some(file) =>
wenzelm@48885
    77
                  if (exts.isEmpty) List(file)
wenzelm@48885
    78
                  else exts.map(ext => file + "." + ext)
wenzelm@48885
    79
                case None => Nil
wenzelm@48885
    80
              }
wenzelm@48885
    81
            case None => Nil
wenzelm@48885
    82
          }
wenzelm@48885
    83
        case _ => Nil
wenzelm@48885
    84
      }).flatten.toList
wenzelm@48885
    85
    }
wenzelm@48885
    86
    else Nil
wenzelm@48885
    87
  }
wenzelm@48885
    88
wenzelm@48885
    89
  def check_thy_text(name: Document.Node.Name, text: CharSequence): Document.Node.Header =
wenzelm@46737
    90
  {
wenzelm@48882
    91
    val header = Thy_Header.read(text)
wenzelm@48885
    92
wenzelm@46737
    93
    val name1 = header.name
wenzelm@46737
    94
    if (name.theory != name1)
wenzelm@48422
    95
      error("Bad file name " + Thy_Load.thy_path(Path.basic(name.theory)) +
wenzelm@48422
    96
        " for theory " + quote(name1))
wenzelm@48885
    97
wenzelm@48885
    98
    val imports = header.imports.map(import_name(name.dir, _))
wenzelm@48885
    99
    val uses = header.uses
wenzelm@48707
   100
    Document.Node.Header(imports, header.keywords, uses)
wenzelm@46737
   101
  }
wenzelm@46748
   102
wenzelm@48885
   103
  def check_thy(name: Document.Node.Name): Document.Node.Header =
wenzelm@48885
   104
    with_thy_text(name, check_thy_text(name, _))
wenzelm@48885
   105
wenzelm@48885
   106
  def check_thy_files(syntax: Outer_Syntax, name: Document.Node.Name): Document.Node.Header =
wenzelm@48885
   107
    with_thy_text(name, text =>
wenzelm@48885
   108
      {
wenzelm@48885
   109
        val string = text.toString
wenzelm@48885
   110
        val header = check_thy_text(name, string)
wenzelm@48885
   111
        val more_uses = find_files(syntax, string)
wenzelm@48885
   112
        header.copy(uses = header.uses ::: more_uses.map((_, false)))
wenzelm@48885
   113
      })
wenzelm@43651
   114
}
wenzelm@43651
   115