src/Pure/PIDE/resources.scala
author wenzelm
Mon Apr 17 12:20:45 2017 +0200 (2017-04-17)
changeset 65488 331f09d9535e
parent 65487 7847807b07ce
child 65489 f3076367f4a8
permissions -rw-r--r--
tuned signature;
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@65361
    17
  val session_base: Sessions.Base,
wenzelm@65445
    18
  val default_qualifier: String = Sessions.DRAFT,
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@65488
    31
  def append(node_name: Document.Node.Name, source_path: Path): String =
wenzelm@65488
    32
    append(node_name.master_dir, source_path)
wenzelm@65488
    33
wenzelm@46737
    34
wenzelm@64657
    35
  /* source files of Isabelle/ML bootstrap */
wenzelm@64657
    36
wenzelm@64657
    37
  def source_file(raw_name: String): Option[String] =
wenzelm@64657
    38
  {
wenzelm@64657
    39
    if (Path.is_wellformed(raw_name)) {
wenzelm@64657
    40
      if (Path.is_valid(raw_name)) {
wenzelm@64657
    41
        def check(p: Path): Option[Path] = if (p.is_file) Some(p) else None
wenzelm@64657
    42
wenzelm@64657
    43
        val path = Path.explode(raw_name)
wenzelm@64657
    44
        val path1 =
wenzelm@64657
    45
          if (path.is_absolute || path.is_current) check(path)
wenzelm@64657
    46
          else {
wenzelm@64657
    47
            check(Path.explode("~~/src/Pure") + path) orElse
wenzelm@64657
    48
              (if (Isabelle_System.getenv("ML_SOURCES") == "") None
wenzelm@64657
    49
               else check(Path.explode("$ML_SOURCES") + path))
wenzelm@64657
    50
          }
wenzelm@64657
    51
        Some(File.platform_path(path1 getOrElse path))
wenzelm@64657
    52
      }
wenzelm@64657
    53
      else None
wenzelm@64657
    54
    }
wenzelm@64657
    55
    else Some(raw_name)
wenzelm@64657
    56
  }
wenzelm@64657
    57
wenzelm@64657
    58
wenzelm@46737
    59
  /* theory files */
wenzelm@46737
    60
wenzelm@63584
    61
  def loaded_files(syntax: Outer_Syntax, text: String): List[String] =
wenzelm@56393
    62
    if (syntax.load_commands_in(text)) {
wenzelm@57906
    63
      val spans = syntax.parse_spans(text)
wenzelm@59689
    64
      spans.iterator.map(Command.span_files(syntax, _)._1).flatten.toList
wenzelm@56392
    65
    }
wenzelm@56392
    66
    else Nil
wenzelm@48885
    67
wenzelm@65445
    68
  def theory_qualifier(name: Document.Node.Name): String =
wenzelm@65457
    69
    session_base.global_theories.getOrElse(name.theory, Long_Name.qualifier(name.theory))
wenzelm@65445
    70
wenzelm@65476
    71
  def theory_name(qualifier: String, theory0: String): (Boolean, String) =
wenzelm@65471
    72
    session_base.loaded_theories.get(theory0) match {
wenzelm@65471
    73
      case Some(theory) => (true, theory)
wenzelm@65471
    74
      case None =>
wenzelm@65471
    75
        val theory =
wenzelm@65471
    76
          if (Long_Name.is_qualified(theory0) || session_base.global_theories.isDefinedAt(theory0)
wenzelm@65471
    77
              || true /* FIXME */) theory0
wenzelm@65471
    78
          else Long_Name.qualify(qualifier, theory0)
wenzelm@65471
    79
        (false, theory)
wenzelm@65471
    80
    }
wenzelm@65471
    81
wenzelm@65445
    82
  def import_name(qualifier: String, dir: String, s: String): Document.Node.Name =
wenzelm@65476
    83
    theory_name(qualifier, Thy_Header.import_name(s)) match {
wenzelm@65472
    84
      case (true, theory) => Document.Node.Name.loaded_theory(theory)
wenzelm@65472
    85
      case (false, theory) =>
wenzelm@65472
    86
        session_base.known_theories.get(theory) match {
wenzelm@65472
    87
          case Some(node_name) => node_name
wenzelm@65472
    88
          case None =>
wenzelm@65472
    89
            val path = Path.explode(s)
wenzelm@65472
    90
            val node = append(dir, thy_path(path))
wenzelm@65472
    91
            val master_dir = append(dir, path.dir)
wenzelm@65472
    92
            Document.Node.Name(node, master_dir, theory)
wenzelm@65472
    93
        }
wenzelm@65472
    94
    }
wenzelm@51634
    95
wenzelm@64656
    96
  def with_thy_reader[A](name: Document.Node.Name, f: Reader[Char] => A): A =
wenzelm@64656
    97
  {
wenzelm@64656
    98
    val path = Path.explode(name.node)
wenzelm@64656
    99
    if (!path.is_file) error("No such file: " + path.toString)
wenzelm@64656
   100
wenzelm@64656
   101
    val reader = Scan.byte_reader(path.file)
wenzelm@64656
   102
    try { f(reader) } finally { reader.close }
wenzelm@64656
   103
  }
wenzelm@64656
   104
wenzelm@65359
   105
  def check_thy_reader(node_name: Document.Node.Name, reader: Reader[Char],
wenzelm@65359
   106
    start: Token.Pos = Token.Pos.command, strict: Boolean = true): Document.Node.Header =
wenzelm@46737
   107
  {
wenzelm@64826
   108
    if (node_name.is_theory && reader.source.length > 0) {
wenzelm@57615
   109
      try {
wenzelm@64825
   110
        val header = Thy_Header.read(reader, start, strict).decode_symbols
wenzelm@48885
   111
wenzelm@65439
   112
        val base_name = node_name.theory_base_name
wenzelm@59694
   113
        val (name, pos) = header.name
wenzelm@59694
   114
        if (base_name != name)
wenzelm@59718
   115
          error("Bad theory name " + quote(name) +
wenzelm@65250
   116
            " for file " + thy_path(Path.basic(base_name)) + Position.here(pos) +
wenzelm@59718
   117
            Completion.report_names(pos, 1, List((base_name, ("theory", base_name)))))
wenzelm@48885
   118
wenzelm@59694
   119
        val imports =
wenzelm@65445
   120
          header.imports.map({ case (s, pos) =>
wenzelm@65445
   121
            (import_name(theory_qualifier(node_name), node_name.master_dir, s), pos) })
wenzelm@63579
   122
        Document.Node.Header(imports, header.keywords, header.abbrevs)
wenzelm@57615
   123
      }
wenzelm@57615
   124
      catch { case exn: Throwable => Document.Node.bad_header(Exn.message(exn)) }
wenzelm@50414
   125
    }
wenzelm@57615
   126
    else Document.Node.no_header
wenzelm@46737
   127
  }
wenzelm@46748
   128
wenzelm@65359
   129
  def check_thy(name: Document.Node.Name, start: Token.Pos = Token.Pos.command,
wenzelm@65359
   130
      strict: Boolean = true): Document.Node.Header =
wenzelm@65359
   131
    with_thy_reader(name, check_thy_reader(name, _, start, strict))
wenzelm@50566
   132
wenzelm@50566
   133
wenzelm@64673
   134
  /* special header */
wenzelm@64673
   135
wenzelm@64673
   136
  def special_header(name: Document.Node.Name): Option[Document.Node.Header] =
wenzelm@65445
   137
  {
wenzelm@65445
   138
    val qualifier = theory_qualifier(name)
wenzelm@65445
   139
    val dir = name.master_dir
wenzelm@65445
   140
wenzelm@65445
   141
    val imports =
wenzelm@65445
   142
      if (Thy_Header.is_ml_root(name.theory))
wenzelm@65445
   143
        List(import_name(qualifier, dir, Thy_Header.ML_BOOTSTRAP))
wenzelm@65445
   144
      else if (Thy_Header.is_bootstrap(name.theory))
wenzelm@65445
   145
        List(import_name(qualifier, dir, Thy_Header.PURE))
wenzelm@65445
   146
      else Nil
wenzelm@65445
   147
wenzelm@65445
   148
    if (imports.isEmpty) None
wenzelm@65445
   149
    else Some(Document.Node.Header(imports.map((_, Position.none))))
wenzelm@65445
   150
  }
wenzelm@64673
   151
wenzelm@64673
   152
wenzelm@64797
   153
  /* blobs */
wenzelm@64797
   154
wenzelm@64797
   155
  def undefined_blobs(nodes: Document.Nodes): List[Document.Node.Name] =
wenzelm@64797
   156
    (for {
wenzelm@64797
   157
      (node_name, node) <- nodes.iterator
wenzelm@65361
   158
      if !session_base.loaded_theory(node_name)
wenzelm@64797
   159
      cmd <- node.load_commands.iterator
wenzelm@64797
   160
      name <- cmd.blobs_undefined.iterator
wenzelm@64797
   161
    } yield name).toList
wenzelm@64797
   162
wenzelm@64797
   163
wenzelm@56316
   164
  /* document changes */
wenzelm@50566
   165
wenzelm@56316
   166
  def parse_change(
wenzelm@56315
   167
      reparse_limit: Int,
wenzelm@56315
   168
      previous: Document.Version,
wenzelm@56315
   169
      doc_blobs: Document.Blobs,
wenzelm@56315
   170
      edits: List[Document.Edit_Text]): Session.Change =
wenzelm@56316
   171
    Thy_Syntax.parse_change(this, reparse_limit, previous, doc_blobs, edits)
wenzelm@55134
   172
wenzelm@56316
   173
  def commit(change: Session.Change) { }
wenzelm@43651
   174
}