src/Pure/Thy/thy_load.scala
author wenzelm
Fri Nov 30 10:42:54 2012 +0100 (2012-11-30)
changeset 50291 674893679352
parent 50204 daeb1674fb91
child 50414 e17a1f179bb0
permissions -rw-r--r--
prefer Symbol.decode_strict in batch mode, to avoid files with spurious Unicode symbols that clash with Isabelle symbol interpretation;
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@50291
    50
wenzelm@50291
    51
    val text = File.read(path)
wenzelm@50291
    52
    Symbol.decode_strict(text)
wenzelm@50291
    53
    f(text)
wenzelm@48885
    54
  }
wenzelm@48885
    55
wenzelm@46737
    56
wenzelm@46737
    57
  /* theory files */
wenzelm@46737
    58
wenzelm@46737
    59
  private def import_name(dir: String, s: String): Document.Node.Name =
wenzelm@46737
    60
  {
wenzelm@46737
    61
    val theory = Thy_Header.base_name(s)
wenzelm@48870
    62
    if (loaded_theories(theory)) Document.Node.Name(theory, "", theory)
wenzelm@46737
    63
    else {
wenzelm@46737
    64
      val path = Path.explode(s)
wenzelm@48422
    65
      val node = append(dir, Thy_Load.thy_path(path))
wenzelm@46737
    66
      val dir1 = append(dir, path.dir)
wenzelm@46737
    67
      Document.Node.Name(node, dir1, theory)
wenzelm@46737
    68
    }
wenzelm@46737
    69
  }
wenzelm@46737
    70
wenzelm@48885
    71
  private def find_file(tokens: List[Token]): Option[String] =
wenzelm@48885
    72
  {
wenzelm@48885
    73
    def clean(toks: List[Token]): List[Token] =
wenzelm@48885
    74
      toks match {
wenzelm@48885
    75
        case t :: _ :: ts if t.is_keyword && (t.source == "%" || t.source == "--") => clean(ts)
wenzelm@48885
    76
        case t :: ts => t :: clean(ts)
wenzelm@48885
    77
        case Nil => Nil
wenzelm@48885
    78
      }
wenzelm@48885
    79
    val clean_tokens = clean(tokens.filter(_.is_proper))
wenzelm@48885
    80
    clean_tokens.reverse.find(_.is_name).map(_.content)
wenzelm@48885
    81
  }
wenzelm@48885
    82
wenzelm@48885
    83
  def find_files(syntax: Outer_Syntax, text: String): List[String] =
wenzelm@48885
    84
  {
wenzelm@48885
    85
    val thy_load_commands = syntax.thy_load_commands
wenzelm@48885
    86
    if (thy_load_commands.exists({ case (cmd, _) => text.containsSlice(cmd) })) {
wenzelm@48885
    87
      val spans = Thy_Syntax.parse_spans(syntax.scan(text))
wenzelm@48885
    88
      spans.iterator.map({
wenzelm@48885
    89
        case toks @ (tok :: _) if tok.is_command =>
wenzelm@48885
    90
          thy_load_commands.find({ case (cmd, _) => cmd == tok.content }) match {
wenzelm@48885
    91
            case Some((_, exts)) =>
wenzelm@48885
    92
              find_file(toks) match {
wenzelm@48885
    93
                case Some(file) =>
wenzelm@48885
    94
                  if (exts.isEmpty) List(file)
wenzelm@48885
    95
                  else exts.map(ext => file + "." + ext)
wenzelm@48885
    96
                case None => Nil
wenzelm@48885
    97
              }
wenzelm@48885
    98
            case None => Nil
wenzelm@48885
    99
          }
wenzelm@48885
   100
        case _ => Nil
wenzelm@48885
   101
      }).flatten.toList
wenzelm@48885
   102
    }
wenzelm@48885
   103
    else Nil
wenzelm@48885
   104
  }
wenzelm@48885
   105
wenzelm@48885
   106
  def check_thy_text(name: Document.Node.Name, text: CharSequence): Document.Node.Header =
wenzelm@46737
   107
  {
wenzelm@48882
   108
    val header = Thy_Header.read(text)
wenzelm@48885
   109
wenzelm@46737
   110
    val name1 = header.name
wenzelm@46737
   111
    if (name.theory != name1)
wenzelm@48422
   112
      error("Bad file name " + Thy_Load.thy_path(Path.basic(name.theory)) +
wenzelm@48422
   113
        " for theory " + quote(name1))
wenzelm@48885
   114
wenzelm@48885
   115
    val imports = header.imports.map(import_name(name.dir, _))
wenzelm@48885
   116
    val uses = header.uses
wenzelm@48707
   117
    Document.Node.Header(imports, header.keywords, uses)
wenzelm@46737
   118
  }
wenzelm@46748
   119
wenzelm@48885
   120
  def check_thy(name: Document.Node.Name): Document.Node.Header =
wenzelm@48885
   121
    with_thy_text(name, check_thy_text(name, _))
wenzelm@48885
   122
wenzelm@48885
   123
  def check_thy_files(syntax: Outer_Syntax, name: Document.Node.Name): Document.Node.Header =
wenzelm@48885
   124
    with_thy_text(name, text =>
wenzelm@48885
   125
      {
wenzelm@48885
   126
        val string = text.toString
wenzelm@48885
   127
        val header = check_thy_text(name, string)
wenzelm@48889
   128
        val more_uses = find_files(syntax.add_keywords(header.keywords), string)
wenzelm@48885
   129
        header.copy(uses = header.uses ::: more_uses.map((_, false)))
wenzelm@48885
   130
      })
wenzelm@43651
   131
}
wenzelm@43651
   132