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