src/Pure/PIDE/resources.scala
author wenzelm
Thu Oct 05 16:33:36 2017 +0200 (19 months ago)
changeset 66767 294c2e9a689e
parent 66766 19f8385ddfd3
child 66771 925d10a7a610
permissions -rw-r--r--
clarified modules;
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@65359
    18
  val log: Logger = No_Logger)
wenzelm@43651
    19
{
wenzelm@64839
    20
  val thy_info = new Thy_Info(this)
wenzelm@64839
    21
wenzelm@65250
    22
  def thy_path(path: Path): Path = path.ext("thy")
wenzelm@65250
    23
wenzelm@64839
    24
wenzelm@44953
    25
  /* file-system operations */
wenzelm@44953
    26
wenzelm@44953
    27
  def append(dir: String, source_path: Path): String =
wenzelm@48711
    28
    (Path.explode(dir) + source_path).expand.implode
wenzelm@44953
    29
wenzelm@65488
    30
  def append(node_name: Document.Node.Name, source_path: Path): String =
wenzelm@65488
    31
    append(node_name.master_dir, source_path)
wenzelm@65488
    32
wenzelm@46737
    33
wenzelm@64657
    34
  /* source files of Isabelle/ML bootstrap */
wenzelm@64657
    35
wenzelm@64657
    36
  def source_file(raw_name: String): Option[String] =
wenzelm@64657
    37
  {
wenzelm@64657
    38
    if (Path.is_wellformed(raw_name)) {
wenzelm@64657
    39
      if (Path.is_valid(raw_name)) {
wenzelm@64657
    40
        def check(p: Path): Option[Path] = if (p.is_file) Some(p) else None
wenzelm@64657
    41
wenzelm@64657
    42
        val path = Path.explode(raw_name)
wenzelm@64657
    43
        val path1 =
wenzelm@64657
    44
          if (path.is_absolute || path.is_current) check(path)
wenzelm@64657
    45
          else {
wenzelm@64657
    46
            check(Path.explode("~~/src/Pure") + path) orElse
wenzelm@64657
    47
              (if (Isabelle_System.getenv("ML_SOURCES") == "") None
wenzelm@64657
    48
               else check(Path.explode("$ML_SOURCES") + path))
wenzelm@64657
    49
          }
wenzelm@64657
    50
        Some(File.platform_path(path1 getOrElse path))
wenzelm@64657
    51
      }
wenzelm@64657
    52
      else None
wenzelm@64657
    53
    }
wenzelm@64657
    54
    else Some(raw_name)
wenzelm@64657
    55
  }
wenzelm@64657
    56
wenzelm@64657
    57
wenzelm@46737
    58
  /* theory files */
wenzelm@46737
    59
wenzelm@66698
    60
  def loaded_files(syntax: Outer_Syntax, name: Document.Node.Name): () => List[Path] =
wenzelm@66695
    61
  {
wenzelm@66699
    62
    val raw_text = with_thy_reader(name, reader => reader.source.toString)
wenzelm@66698
    63
    () => {
wenzelm@66699
    64
      val text = Symbol.decode(raw_text)
wenzelm@66698
    65
      if (syntax.load_commands_in(text)) {
wenzelm@66698
    66
        val spans = syntax.parse_spans(text)
wenzelm@66698
    67
        val dir = Path.explode(name.master_dir)
wenzelm@66698
    68
        spans.iterator.map(Command.span_files(syntax, _)._1).flatten.
wenzelm@66700
    69
          map(a => (dir + Path.explode(a)).expand).toList
wenzelm@66698
    70
      }
wenzelm@66698
    71
      else Nil
wenzelm@66696
    72
    }
wenzelm@66695
    73
  }
wenzelm@66695
    74
wenzelm@66701
    75
  def pure_files(syntax: Outer_Syntax, dir: Path): List[Path] =
wenzelm@66701
    76
  {
wenzelm@66701
    77
    val roots =
wenzelm@66701
    78
      for { (name, _) <- Thy_Header.ml_roots }
wenzelm@66701
    79
      yield (dir + Path.explode(name)).expand
wenzelm@66701
    80
    val files =
wenzelm@66701
    81
      for {
wenzelm@66701
    82
        (path, (_, theory)) <- roots zip Thy_Header.ml_roots
wenzelm@66701
    83
        file <- loaded_files(syntax, Document.Node.Name(path.implode, path.dir.implode, theory))()
wenzelm@66701
    84
      } yield file
wenzelm@66701
    85
    roots ::: files
wenzelm@66701
    86
  }
wenzelm@66696
    87
wenzelm@65445
    88
  def theory_qualifier(name: Document.Node.Name): String =
wenzelm@65457
    89
    session_base.global_theories.getOrElse(name.theory, Long_Name.qualifier(name.theory))
wenzelm@65445
    90
wenzelm@66712
    91
  def theory_name(qualifier: String, theory: String): (Boolean, String) =
wenzelm@66712
    92
    if (session_base.loaded_theory(theory)) (true, theory)
wenzelm@66712
    93
    else {
wenzelm@66712
    94
      val theory1 =
wenzelm@66712
    95
        if (Long_Name.is_qualified(theory) || session_base.global_theories.isDefinedAt(theory))
wenzelm@66712
    96
          theory
wenzelm@66712
    97
        else Long_Name.qualify(qualifier, theory)
wenzelm@66712
    98
      (false, theory1)
wenzelm@65471
    99
    }
wenzelm@65471
   100
wenzelm@65445
   101
  def import_name(qualifier: String, dir: String, s: String): Document.Node.Name =
wenzelm@65476
   102
    theory_name(qualifier, Thy_Header.import_name(s)) match {
wenzelm@65472
   103
      case (true, theory) => Document.Node.Name.loaded_theory(theory)
wenzelm@65472
   104
      case (false, theory) =>
wenzelm@65498
   105
        session_base.known_theory(theory) match {
wenzelm@65472
   106
          case Some(node_name) => node_name
wenzelm@65472
   107
          case None =>
wenzelm@65472
   108
            val path = Path.explode(s)
wenzelm@65472
   109
            val node = append(dir, thy_path(path))
wenzelm@65472
   110
            val master_dir = append(dir, path.dir)
wenzelm@65472
   111
            Document.Node.Name(node, master_dir, theory)
wenzelm@65472
   112
        }
wenzelm@65472
   113
    }
wenzelm@51634
   114
wenzelm@65529
   115
  def import_name(name: Document.Node.Name, s: String): Document.Node.Name =
wenzelm@65529
   116
    import_name(theory_qualifier(name), name.master_dir, s)
wenzelm@65489
   117
wenzelm@66767
   118
  def standard_import(session_resources: Resources,
wenzelm@66767
   119
    qualifier: String, dir: String, s: String): String =
wenzelm@66767
   120
  {
wenzelm@66767
   121
    val name = import_name(qualifier, dir, s)
wenzelm@66767
   122
    val s1 =
wenzelm@66767
   123
      if (session_base.loaded_theory(name)) name.theory
wenzelm@66767
   124
      else {
wenzelm@66767
   125
        session_base.known.get_file(name.path.file) match {
wenzelm@66767
   126
          case Some(name1) if session_resources.theory_qualifier(name1) != qualifier =>
wenzelm@66767
   127
            name1.theory
wenzelm@66767
   128
          case Some(name1) if Thy_Header.is_base_name(s) =>
wenzelm@66767
   129
            name1.theory_base_name
wenzelm@66767
   130
          case _ => s
wenzelm@66767
   131
        }
wenzelm@66767
   132
      }
wenzelm@66767
   133
    val name2 = import_name(qualifier, dir, s1)
wenzelm@66767
   134
    if (name.node == name2.node) s1 else s
wenzelm@66767
   135
  }
wenzelm@66767
   136
wenzelm@64656
   137
  def with_thy_reader[A](name: Document.Node.Name, f: Reader[Char] => A): A =
wenzelm@64656
   138
  {
wenzelm@66716
   139
    val path = File.check_file(name.path)
wenzelm@64656
   140
    val reader = Scan.byte_reader(path.file)
wenzelm@64656
   141
    try { f(reader) } finally { reader.close }
wenzelm@64656
   142
  }
wenzelm@64656
   143
wenzelm@65359
   144
  def check_thy_reader(node_name: Document.Node.Name, reader: Reader[Char],
wenzelm@65359
   145
    start: Token.Pos = Token.Pos.command, strict: Boolean = true): Document.Node.Header =
wenzelm@46737
   146
  {
wenzelm@64826
   147
    if (node_name.is_theory && reader.source.length > 0) {
wenzelm@57615
   148
      try {
wenzelm@66713
   149
        val header = Thy_Header.read(reader, start, strict)
wenzelm@48885
   150
wenzelm@65439
   151
        val base_name = node_name.theory_base_name
wenzelm@59694
   152
        val (name, pos) = header.name
wenzelm@59694
   153
        if (base_name != name)
wenzelm@59718
   154
          error("Bad theory name " + quote(name) +
wenzelm@65250
   155
            " for file " + thy_path(Path.basic(base_name)) + Position.here(pos) +
wenzelm@66766
   156
            Completion.report_theories(pos, List(base_name)))
wenzelm@48885
   157
wenzelm@65489
   158
        val imports = header.imports.map({ case (s, pos) => (import_name(node_name, s), pos) })
wenzelm@63579
   159
        Document.Node.Header(imports, header.keywords, header.abbrevs)
wenzelm@57615
   160
      }
wenzelm@57615
   161
      catch { case exn: Throwable => Document.Node.bad_header(Exn.message(exn)) }
wenzelm@50414
   162
    }
wenzelm@57615
   163
    else Document.Node.no_header
wenzelm@46737
   164
  }
wenzelm@46748
   165
wenzelm@65359
   166
  def check_thy(name: Document.Node.Name, start: Token.Pos = Token.Pos.command,
wenzelm@65359
   167
      strict: Boolean = true): Document.Node.Header =
wenzelm@65359
   168
    with_thy_reader(name, check_thy_reader(name, _, start, strict))
wenzelm@50566
   169
wenzelm@50566
   170
wenzelm@64673
   171
  /* special header */
wenzelm@64673
   172
wenzelm@64673
   173
  def special_header(name: Document.Node.Name): Option[Document.Node.Header] =
wenzelm@65445
   174
  {
wenzelm@65445
   175
    val imports =
wenzelm@65489
   176
      if (Thy_Header.is_ml_root(name.theory)) List(import_name(name, Thy_Header.ML_BOOTSTRAP))
wenzelm@65489
   177
      else if (Thy_Header.is_bootstrap(name.theory)) List(import_name(name, Thy_Header.PURE))
wenzelm@65445
   178
      else Nil
wenzelm@65445
   179
    if (imports.isEmpty) None
wenzelm@65445
   180
    else Some(Document.Node.Header(imports.map((_, Position.none))))
wenzelm@65445
   181
  }
wenzelm@64673
   182
wenzelm@64673
   183
wenzelm@64797
   184
  /* blobs */
wenzelm@64797
   185
wenzelm@64797
   186
  def undefined_blobs(nodes: Document.Nodes): List[Document.Node.Name] =
wenzelm@64797
   187
    (for {
wenzelm@64797
   188
      (node_name, node) <- nodes.iterator
wenzelm@65361
   189
      if !session_base.loaded_theory(node_name)
wenzelm@64797
   190
      cmd <- node.load_commands.iterator
wenzelm@64797
   191
      name <- cmd.blobs_undefined.iterator
wenzelm@64797
   192
    } yield name).toList
wenzelm@64797
   193
wenzelm@64797
   194
wenzelm@56316
   195
  /* document changes */
wenzelm@50566
   196
wenzelm@56316
   197
  def parse_change(
wenzelm@56315
   198
      reparse_limit: Int,
wenzelm@56315
   199
      previous: Document.Version,
wenzelm@56315
   200
      doc_blobs: Document.Blobs,
wenzelm@56315
   201
      edits: List[Document.Edit_Text]): Session.Change =
wenzelm@56316
   202
    Thy_Syntax.parse_change(this, reparse_limit, previous, doc_blobs, edits)
wenzelm@55134
   203
wenzelm@56316
   204
  def commit(change: Session.Change) { }
wenzelm@43651
   205
}