src/Pure/Thy/thy_load.scala
author wenzelm
Sat Jan 25 15:29:40 2014 +0100 (2014-01-25)
changeset 55134 1b67b17cdad5
parent 54521 744ea0025e11
child 55879 ac979f750c1a
permissions -rw-r--r--
propagate update of outer syntax keywords: global propertiesChanged, buffer TokenMarker.markTokens, text area repainting;
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@48422
    24
}
wenzelm@44953
    25
wenzelm@48422
    26
wenzelm@48870
    27
class Thy_Load(val loaded_theories: Set[String] = Set.empty, val base_syntax: Outer_Syntax)
wenzelm@43651
    28
{
wenzelm@54514
    29
  /* document node names */
wenzelm@54514
    30
wenzelm@54514
    31
  def node_name(raw_path: Path): Document.Node.Name =
wenzelm@54514
    32
  {
wenzelm@54514
    33
    val path = raw_path.expand
wenzelm@54514
    34
    val node = path.implode
wenzelm@54514
    35
    val theory = Thy_Header.thy_name(node).getOrElse("")
wenzelm@54515
    36
    val master_dir = if (theory == "") "" else path.dir.implode
wenzelm@54515
    37
    Document.Node.Name(node, master_dir, theory)
wenzelm@54514
    38
  }
wenzelm@54514
    39
wenzelm@54514
    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@50415
    59
  def body_files_test(syntax: Outer_Syntax, text: String): Boolean =
wenzelm@50415
    60
    syntax.thy_load_commands.exists({ case (cmd, _) => text.containsSlice(cmd) })
wenzelm@50415
    61
wenzelm@50415
    62
  def body_files(syntax: Outer_Syntax, text: String): List[String] =
wenzelm@48885
    63
  {
wenzelm@50415
    64
    val spans = Thy_Syntax.parse_spans(syntax.scan(text))
wenzelm@54513
    65
    spans.iterator.map(Thy_Syntax.span_files(syntax, _)).flatten.toList
wenzelm@48885
    66
  }
wenzelm@48885
    67
wenzelm@51634
    68
  def import_name(master: Document.Node.Name, s: String): Document.Node.Name =
wenzelm@51634
    69
  {
wenzelm@51634
    70
    val theory = Thy_Header.base_name(s)
wenzelm@54515
    71
    if (loaded_theories(theory)) Document.Node.Name(theory + ".thy", "", theory)
wenzelm@51634
    72
    else {
wenzelm@51634
    73
      val path = Path.explode(s)
wenzelm@54515
    74
      val node = append(master.master_dir, Thy_Load.thy_path(path))
wenzelm@54515
    75
      val master_dir = append(master.master_dir, path.dir)
wenzelm@54515
    76
      Document.Node.Name(node, master_dir, theory)
wenzelm@51634
    77
    }
wenzelm@51634
    78
  }
wenzelm@51634
    79
wenzelm@48885
    80
  def check_thy_text(name: Document.Node.Name, text: CharSequence): Document.Node.Header =
wenzelm@46737
    81
  {
wenzelm@50414
    82
    try {
wenzelm@50414
    83
      val header = Thy_Header.read(text)
wenzelm@48885
    84
wenzelm@50414
    85
      val name1 = header.name
wenzelm@50414
    86
      if (name.theory != name1)
wenzelm@50414
    87
        error("Bad file name " + Thy_Load.thy_path(Path.basic(name.theory)) +
wenzelm@50414
    88
          " for theory " + quote(name1))
wenzelm@48885
    89
wenzelm@51634
    90
      val imports = header.imports.map(import_name(name, _))
wenzelm@51293
    91
      Document.Node.Header(imports, header.keywords, Nil)
wenzelm@50414
    92
    }
wenzelm@50641
    93
    catch { case exn: Throwable => Document.Node.bad_header(Exn.message(exn)) }
wenzelm@46737
    94
  }
wenzelm@46748
    95
wenzelm@48885
    96
  def check_thy(name: Document.Node.Name): Document.Node.Header =
wenzelm@48885
    97
    with_thy_text(name, check_thy_text(name, _))
wenzelm@50566
    98
wenzelm@50566
    99
wenzelm@50566
   100
  /* theory text edits */
wenzelm@50566
   101
wenzelm@54521
   102
  def text_edits(
wenzelm@54521
   103
    reparse_limit: Int,
wenzelm@54521
   104
    previous: Document.Version,
wenzelm@54521
   105
    doc_blobs: Document.Blobs,
wenzelm@55134
   106
    edits: List[Document.Edit_Text]): (Boolean, List[Document.Edit_Command], Document.Version) =
wenzelm@54521
   107
    Thy_Syntax.text_edits(this, reparse_limit, previous, doc_blobs, edits)
wenzelm@55134
   108
wenzelm@55134
   109
  def syntax_changed() { }
wenzelm@43651
   110
}
wenzelm@43651
   111