src/Pure/PIDE/resources.scala
author wenzelm
Wed Apr 05 22:00:44 2017 +0200 (2017-04-05)
changeset 65392 f365f61f2081
parent 65373 905ed0102c69
child 65408 c728f922f657
permissions -rw-r--r--
uniform import_name, with treatment of global and qualified theories;
wenzelm@56208
     1
/*  Title:      Pure/PIDE/resources.scala
wenzelm@43651
     2
    Author:     Makarius
wenzelm@43651
     3
wenzelm@56208
     4
Resources for theories and auxiliary 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@56823
    11
import scala.util.parsing.input.Reader
wenzelm@48885
    12
wenzelm@48409
    13
import java.io.{File => JFile}
wenzelm@44953
    14
wenzelm@44953
    15
wenzelm@65359
    16
class Resources(
wenzelm@65359
    17
  val session_name: String,
wenzelm@65361
    18
  val session_base: Sessions.Base,
wenzelm@65359
    19
  val log: Logger = No_Logger)
wenzelm@43651
    20
{
wenzelm@64839
    21
  val thy_info = new Thy_Info(this)
wenzelm@64839
    22
wenzelm@65250
    23
  def thy_path(path: Path): Path = path.ext("thy")
wenzelm@65250
    24
wenzelm@64839
    25
wenzelm@44953
    26
  /* file-system operations */
wenzelm@44953
    27
wenzelm@44953
    28
  def append(dir: String, source_path: Path): String =
wenzelm@48711
    29
    (Path.explode(dir) + source_path).expand.implode
wenzelm@44953
    30
wenzelm@64654
    31
  def append_file(dir: String, raw_name: String): String =
wenzelm@64654
    32
    if (Path.is_valid(raw_name)) append(dir, Path.explode(raw_name))
wenzelm@64654
    33
    else raw_name
wenzelm@64654
    34
wenzelm@64654
    35
wenzelm@46737
    36
wenzelm@64657
    37
  /* source files of Isabelle/ML bootstrap */
wenzelm@64657
    38
wenzelm@64657
    39
  def source_file(raw_name: String): Option[String] =
wenzelm@64657
    40
  {
wenzelm@64657
    41
    if (Path.is_wellformed(raw_name)) {
wenzelm@64657
    42
      if (Path.is_valid(raw_name)) {
wenzelm@64657
    43
        def check(p: Path): Option[Path] = if (p.is_file) Some(p) else None
wenzelm@64657
    44
wenzelm@64657
    45
        val path = Path.explode(raw_name)
wenzelm@64657
    46
        val path1 =
wenzelm@64657
    47
          if (path.is_absolute || path.is_current) check(path)
wenzelm@64657
    48
          else {
wenzelm@64657
    49
            check(Path.explode("~~/src/Pure") + path) orElse
wenzelm@64657
    50
              (if (Isabelle_System.getenv("ML_SOURCES") == "") None
wenzelm@64657
    51
               else check(Path.explode("$ML_SOURCES") + path))
wenzelm@64657
    52
          }
wenzelm@64657
    53
        Some(File.platform_path(path1 getOrElse path))
wenzelm@64657
    54
      }
wenzelm@64657
    55
      else None
wenzelm@64657
    56
    }
wenzelm@64657
    57
    else Some(raw_name)
wenzelm@64657
    58
  }
wenzelm@64657
    59
wenzelm@64657
    60
wenzelm@46737
    61
  /* theory files */
wenzelm@46737
    62
wenzelm@63584
    63
  def loaded_files(syntax: Outer_Syntax, text: String): List[String] =
wenzelm@56393
    64
    if (syntax.load_commands_in(text)) {
wenzelm@57906
    65
      val spans = syntax.parse_spans(text)
wenzelm@59689
    66
      spans.iterator.map(Command.span_files(syntax, _)._1).flatten.toList
wenzelm@56392
    67
    }
wenzelm@56392
    68
    else Nil
wenzelm@48885
    69
wenzelm@65392
    70
  def import_name(dir: String, s: String): Document.Node.Name =
wenzelm@65359
    71
  {
wenzelm@65392
    72
    val thy = Thy_Header.base_name(s)
wenzelm@65392
    73
    val is_global = session_base.global_theories.contains(thy)
wenzelm@65392
    74
    val is_qualified = Long_Name.is_qualified(thy)
wenzelm@65362
    75
wenzelm@65362
    76
    val known_theory =
wenzelm@65392
    77
      session_base.known_theories.get(thy) orElse
wenzelm@65392
    78
      (if (is_global) None
wenzelm@65392
    79
       else if (is_qualified) session_base.known_theories.get(Long_Name.base_name(thy))
wenzelm@65392
    80
       else session_base.known_theories.get(Long_Name.qualify(session_name, thy)))
wenzelm@65362
    81
wenzelm@65362
    82
    known_theory match {
wenzelm@65361
    83
      case Some(name) if session_base.loaded_theory(name) => Document.Node.Name.theory(name.theory)
wenzelm@56801
    84
      case Some(name) => name
wenzelm@56801
    85
      case None =>
wenzelm@56801
    86
        val path = Path.explode(s)
wenzelm@65392
    87
        val node = append(dir, thy_path(path))
wenzelm@65392
    88
        val master_dir = append(dir, path.dir)
wenzelm@65392
    89
        val theory = if (is_global || is_qualified) thy else Long_Name.qualify(session_name, thy)
wenzelm@65392
    90
        Document.Node.Name(node, master_dir, theory)
wenzelm@51634
    91
    }
wenzelm@51634
    92
  }
wenzelm@51634
    93
wenzelm@64656
    94
  def with_thy_reader[A](name: Document.Node.Name, f: Reader[Char] => A): A =
wenzelm@64656
    95
  {
wenzelm@64656
    96
    val path = Path.explode(name.node)
wenzelm@64656
    97
    if (!path.is_file) error("No such file: " + path.toString)
wenzelm@64656
    98
wenzelm@64656
    99
    val reader = Scan.byte_reader(path.file)
wenzelm@64656
   100
    try { f(reader) } finally { reader.close }
wenzelm@64656
   101
  }
wenzelm@64656
   102
wenzelm@65359
   103
  def check_thy_reader(node_name: Document.Node.Name, reader: Reader[Char],
wenzelm@65359
   104
    start: Token.Pos = Token.Pos.command, strict: Boolean = true): Document.Node.Header =
wenzelm@46737
   105
  {
wenzelm@64826
   106
    if (node_name.is_theory && reader.source.length > 0) {
wenzelm@57615
   107
      try {
wenzelm@64825
   108
        val header = Thy_Header.read(reader, start, strict).decode_symbols
wenzelm@48885
   109
wenzelm@59694
   110
        val base_name = Long_Name.base_name(node_name.theory)
wenzelm@59694
   111
        val (name, pos) = header.name
wenzelm@59694
   112
        if (base_name != name)
wenzelm@59718
   113
          error("Bad theory name " + quote(name) +
wenzelm@65250
   114
            " for file " + thy_path(Path.basic(base_name)) + Position.here(pos) +
wenzelm@59718
   115
            Completion.report_names(pos, 1, List((base_name, ("theory", base_name)))))
wenzelm@48885
   116
wenzelm@59694
   117
        val imports =
wenzelm@65392
   118
          header.imports.map({ case (s, pos) => (import_name(node_name.master_dir, s), pos) })
wenzelm@63579
   119
        Document.Node.Header(imports, header.keywords, header.abbrevs)
wenzelm@57615
   120
      }
wenzelm@57615
   121
      catch { case exn: Throwable => Document.Node.bad_header(Exn.message(exn)) }
wenzelm@50414
   122
    }
wenzelm@57615
   123
    else Document.Node.no_header
wenzelm@46737
   124
  }
wenzelm@46748
   125
wenzelm@65359
   126
  def check_thy(name: Document.Node.Name, start: Token.Pos = Token.Pos.command,
wenzelm@65359
   127
      strict: Boolean = true): Document.Node.Header =
wenzelm@65359
   128
    with_thy_reader(name, check_thy_reader(name, _, start, strict))
wenzelm@50566
   129
wenzelm@50566
   130
wenzelm@64673
   131
  /* special header */
wenzelm@64673
   132
wenzelm@64673
   133
  def special_header(name: Document.Node.Name): Option[Document.Node.Header] =
wenzelm@64673
   134
    if (Thy_Header.is_ml_root(name.theory))
wenzelm@65392
   135
      Some(Document.Node.Header(
wenzelm@65392
   136
        List((import_name(name.master_dir, Thy_Header.ML_BOOTSTRAP), Position.none))))
wenzelm@64673
   137
    else if (Thy_Header.is_bootstrap(name.theory))
wenzelm@65392
   138
      Some(Document.Node.Header(
wenzelm@65392
   139
        List((import_name(name.master_dir, Thy_Header.PURE), Position.none))))
wenzelm@64673
   140
    else None
wenzelm@64673
   141
wenzelm@64673
   142
wenzelm@64797
   143
  /* blobs */
wenzelm@64797
   144
wenzelm@64797
   145
  def undefined_blobs(nodes: Document.Nodes): List[Document.Node.Name] =
wenzelm@64797
   146
    (for {
wenzelm@64797
   147
      (node_name, node) <- nodes.iterator
wenzelm@65361
   148
      if !session_base.loaded_theory(node_name)
wenzelm@64797
   149
      cmd <- node.load_commands.iterator
wenzelm@64797
   150
      name <- cmd.blobs_undefined.iterator
wenzelm@64797
   151
    } yield name).toList
wenzelm@64797
   152
wenzelm@64797
   153
wenzelm@56316
   154
  /* document changes */
wenzelm@50566
   155
wenzelm@56316
   156
  def parse_change(
wenzelm@56315
   157
      reparse_limit: Int,
wenzelm@56315
   158
      previous: Document.Version,
wenzelm@56315
   159
      doc_blobs: Document.Blobs,
wenzelm@56315
   160
      edits: List[Document.Edit_Text]): Session.Change =
wenzelm@56316
   161
    Thy_Syntax.parse_change(this, reparse_limit, previous, doc_blobs, edits)
wenzelm@55134
   162
wenzelm@56316
   163
  def commit(change: Session.Change) { }
wenzelm@43651
   164
}