src/Pure/Thy/thy_load.scala
author wenzelm
Sun Dec 30 18:23:31 2012 +0100 (2012-12-30)
changeset 50641 b908e56e83ca
parent 50566 b43c4f660320
child 51293 05b1bbae748d
permissions -rw-r--r--
tuned;
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@50566
    26
  /* document node names */
wenzelm@50204
    27
wenzelm@50566
    28
  def path_node_name(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@50415
    83
  def body_files_test(syntax: Outer_Syntax, text: String): Boolean =
wenzelm@50415
    84
    syntax.thy_load_commands.exists({ case (cmd, _) => text.containsSlice(cmd) })
wenzelm@50415
    85
wenzelm@50415
    86
  def body_files(syntax: Outer_Syntax, text: String): List[String] =
wenzelm@48885
    87
  {
wenzelm@48885
    88
    val thy_load_commands = syntax.thy_load_commands
wenzelm@50415
    89
    val spans = Thy_Syntax.parse_spans(syntax.scan(text))
wenzelm@50415
    90
    spans.iterator.map({
wenzelm@50415
    91
      case toks @ (tok :: _) if tok.is_command =>
wenzelm@50415
    92
        thy_load_commands.find({ case (cmd, _) => cmd == tok.content }) match {
wenzelm@50415
    93
          case Some((_, exts)) =>
wenzelm@50415
    94
            find_file(toks) match {
wenzelm@50415
    95
              case Some(file) =>
wenzelm@50415
    96
                if (exts.isEmpty) List(file)
wenzelm@50415
    97
                else exts.map(ext => file + "." + ext)
wenzelm@50415
    98
              case None => Nil
wenzelm@50415
    99
            }
wenzelm@50415
   100
          case None => Nil
wenzelm@50415
   101
        }
wenzelm@50415
   102
      case _ => Nil
wenzelm@50415
   103
    }).flatten.toList
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@50414
   108
    try {
wenzelm@50414
   109
      val header = Thy_Header.read(text)
wenzelm@48885
   110
wenzelm@50414
   111
      val name1 = header.name
wenzelm@50414
   112
      if (name.theory != name1)
wenzelm@50414
   113
        error("Bad file name " + Thy_Load.thy_path(Path.basic(name.theory)) +
wenzelm@50414
   114
          " for theory " + quote(name1))
wenzelm@48885
   115
wenzelm@50414
   116
      val imports = header.imports.map(import_name(name.dir, _))
wenzelm@50414
   117
      val uses = header.uses
wenzelm@50414
   118
      Document.Node.Header(imports, header.keywords, uses)
wenzelm@50414
   119
    }
wenzelm@50641
   120
    catch { case exn: Throwable => Document.Node.bad_header(Exn.message(exn)) }
wenzelm@46737
   121
  }
wenzelm@46748
   122
wenzelm@48885
   123
  def check_thy(name: Document.Node.Name): Document.Node.Header =
wenzelm@48885
   124
    with_thy_text(name, check_thy_text(name, _))
wenzelm@50566
   125
wenzelm@50566
   126
wenzelm@50566
   127
  /* theory text edits */
wenzelm@50566
   128
wenzelm@50566
   129
  def text_edits(reparse_limit: Int, previous: Document.Version, edits: List[Document.Edit_Text])
wenzelm@50566
   130
      : (List[Document.Edit_Command], Document.Version) =
wenzelm@50566
   131
    Thy_Syntax.text_edits(base_syntax, reparse_limit, previous, edits)
wenzelm@43651
   132
}
wenzelm@43651
   133