src/Pure/PIDE/resources.scala
author wenzelm
Tue Jan 03 14:17:03 2017 +0100 (2017-01-03)
changeset 64759 100941134718
parent 64718 3197b68f4314
child 64797 891a25a87ea1
permissions -rw-r--r--
clarified master_dir: file-URL;
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@56208
    16
object Resources
wenzelm@48422
    17
{
wenzelm@48422
    18
  def thy_path(path: Path): Path = path.ext("thy")
wenzelm@60835
    19
wenzelm@60835
    20
  val empty: Resources = new Resources(Set.empty, Map.empty, Outer_Syntax.empty)
wenzelm@48422
    21
}
wenzelm@44953
    22
wenzelm@56801
    23
class Resources(
wenzelm@56801
    24
  val loaded_theories: Set[String],
wenzelm@56801
    25
  val known_theories: Map[String, Document.Node.Name],
wenzelm@64718
    26
  val base_syntax: Outer_Syntax,
wenzelm@64718
    27
  val log: Logger = No_Logger)
wenzelm@43651
    28
{
wenzelm@54514
    29
  /* document node names */
wenzelm@54514
    30
wenzelm@56801
    31
  def node_name(qualifier: String, raw_path: Path): Document.Node.Name =
wenzelm@54514
    32
  {
wenzelm@56913
    33
    val no_qualifier = "" // FIXME
wenzelm@54514
    34
    val path = raw_path.expand
wenzelm@54514
    35
    val node = path.implode
wenzelm@56913
    36
    val theory = Long_Name.qualify(no_qualifier, Thy_Header.thy_name(node).getOrElse(""))
wenzelm@54515
    37
    val master_dir = if (theory == "") "" else path.dir.implode
wenzelm@54515
    38
    Document.Node.Name(node, master_dir, theory)
wenzelm@54514
    39
  }
wenzelm@54514
    40
wenzelm@54514
    41
wenzelm@44953
    42
  /* file-system operations */
wenzelm@44953
    43
wenzelm@44953
    44
  def append(dir: String, source_path: Path): String =
wenzelm@48711
    45
    (Path.explode(dir) + source_path).expand.implode
wenzelm@44953
    46
wenzelm@64654
    47
  def append_file(dir: String, raw_name: String): String =
wenzelm@64654
    48
    if (Path.is_valid(raw_name)) append(dir, Path.explode(raw_name))
wenzelm@64654
    49
    else raw_name
wenzelm@64654
    50
wenzelm@64654
    51
wenzelm@46737
    52
wenzelm@64657
    53
  /* source files of Isabelle/ML bootstrap */
wenzelm@64657
    54
wenzelm@64657
    55
  def source_file(raw_name: String): Option[String] =
wenzelm@64657
    56
  {
wenzelm@64657
    57
    if (Path.is_wellformed(raw_name)) {
wenzelm@64657
    58
      if (Path.is_valid(raw_name)) {
wenzelm@64657
    59
        def check(p: Path): Option[Path] = if (p.is_file) Some(p) else None
wenzelm@64657
    60
wenzelm@64657
    61
        val path = Path.explode(raw_name)
wenzelm@64657
    62
        val path1 =
wenzelm@64657
    63
          if (path.is_absolute || path.is_current) check(path)
wenzelm@64657
    64
          else {
wenzelm@64657
    65
            check(Path.explode("~~/src/Pure") + path) orElse
wenzelm@64657
    66
              (if (Isabelle_System.getenv("ML_SOURCES") == "") None
wenzelm@64657
    67
               else check(Path.explode("$ML_SOURCES") + path))
wenzelm@64657
    68
          }
wenzelm@64657
    69
        Some(File.platform_path(path1 getOrElse path))
wenzelm@64657
    70
      }
wenzelm@64657
    71
      else None
wenzelm@64657
    72
    }
wenzelm@64657
    73
    else Some(raw_name)
wenzelm@64657
    74
  }
wenzelm@64657
    75
wenzelm@64657
    76
wenzelm@46737
    77
  /* theory files */
wenzelm@46737
    78
wenzelm@63584
    79
  def loaded_files(syntax: Outer_Syntax, text: String): List[String] =
wenzelm@56393
    80
    if (syntax.load_commands_in(text)) {
wenzelm@57906
    81
      val spans = syntax.parse_spans(text)
wenzelm@59689
    82
      spans.iterator.map(Command.span_files(syntax, _)._1).flatten.toList
wenzelm@56392
    83
    }
wenzelm@56392
    84
    else Nil
wenzelm@48885
    85
wenzelm@56801
    86
  private def dummy_name(theory: String): Document.Node.Name =
wenzelm@56801
    87
    Document.Node.Name(theory + ".thy", "", theory)
wenzelm@56801
    88
wenzelm@56801
    89
  def import_name(qualifier: String, master: Document.Node.Name, s: String): Document.Node.Name =
wenzelm@51634
    90
  {
wenzelm@56913
    91
    val no_qualifier = "" // FIXME
wenzelm@56801
    92
    val thy1 = Thy_Header.base_name(s)
wenzelm@56913
    93
    val thy2 = if (Long_Name.is_qualified(thy1)) thy1 else Long_Name.qualify(no_qualifier, thy1)
wenzelm@56801
    94
    (known_theories.get(thy1) orElse
wenzelm@56801
    95
     known_theories.get(thy2) orElse
wenzelm@56801
    96
     known_theories.get(Long_Name.base_name(thy1))) match {
wenzelm@56801
    97
      case Some(name) if loaded_theories(name.theory) => dummy_name(name.theory)
wenzelm@56801
    98
      case Some(name) => name
wenzelm@56801
    99
      case None =>
wenzelm@56801
   100
        val path = Path.explode(s)
wenzelm@56801
   101
        val theory = path.base.implode
wenzelm@56801
   102
        if (Long_Name.is_qualified(theory)) dummy_name(theory)
wenzelm@56801
   103
        else {
wenzelm@56801
   104
          val node = append(master.master_dir, Resources.thy_path(path))
wenzelm@56801
   105
          val master_dir = append(master.master_dir, path.dir)
wenzelm@56913
   106
          Document.Node.Name(node, master_dir, Long_Name.qualify(no_qualifier, theory))
wenzelm@56801
   107
        }
wenzelm@51634
   108
    }
wenzelm@51634
   109
  }
wenzelm@51634
   110
wenzelm@64656
   111
  def with_thy_reader[A](name: Document.Node.Name, f: Reader[Char] => A): A =
wenzelm@64656
   112
  {
wenzelm@64656
   113
    val path = Path.explode(name.node)
wenzelm@64656
   114
    if (!path.is_file) error("No such file: " + path.toString)
wenzelm@64656
   115
wenzelm@64656
   116
    val reader = Scan.byte_reader(path.file)
wenzelm@64656
   117
    try { f(reader) } finally { reader.close }
wenzelm@64656
   118
  }
wenzelm@64656
   119
wenzelm@59705
   120
  def check_thy_reader(qualifier: String, node_name: Document.Node.Name,
wenzelm@59705
   121
    reader: Reader[Char], start: Token.Pos): Document.Node.Header =
wenzelm@46737
   122
  {
wenzelm@57615
   123
    if (reader.source.length > 0) {
wenzelm@57615
   124
      try {
wenzelm@59705
   125
        val header = Thy_Header.read(reader, start).decode_symbols
wenzelm@48885
   126
wenzelm@59694
   127
        val base_name = Long_Name.base_name(node_name.theory)
wenzelm@59694
   128
        val (name, pos) = header.name
wenzelm@59694
   129
        if (base_name != name)
wenzelm@59718
   130
          error("Bad theory name " + quote(name) +
wenzelm@59718
   131
            " for file " + Resources.thy_path(Path.basic(base_name)) + Position.here(pos) +
wenzelm@59718
   132
            Completion.report_names(pos, 1, List((base_name, ("theory", base_name)))))
wenzelm@48885
   133
wenzelm@59694
   134
        val imports =
wenzelm@59695
   135
          header.imports.map({ case (s, pos) => (import_name(qualifier, node_name, s), pos) })
wenzelm@63579
   136
        Document.Node.Header(imports, header.keywords, header.abbrevs)
wenzelm@57615
   137
      }
wenzelm@57615
   138
      catch { case exn: Throwable => Document.Node.bad_header(Exn.message(exn)) }
wenzelm@50414
   139
    }
wenzelm@57615
   140
    else Document.Node.no_header
wenzelm@46737
   141
  }
wenzelm@46748
   142
wenzelm@59705
   143
  def check_thy(qualifier: String, name: Document.Node.Name, start: Token.Pos)
wenzelm@59705
   144
    : Document.Node.Header =
wenzelm@59705
   145
    with_thy_reader(name, check_thy_reader(qualifier, name, _, start))
wenzelm@50566
   146
wenzelm@59691
   147
  def check_file(file: String): Boolean =
wenzelm@59691
   148
    try {
wenzelm@59691
   149
      if (Url.is_wellformed(file)) Url.is_readable(file)
wenzelm@59691
   150
      else (new JFile(file)).isFile
wenzelm@59691
   151
    }
wenzelm@59691
   152
    catch { case ERROR(_) => false }
wenzelm@59691
   153
wenzelm@50566
   154
wenzelm@64673
   155
  /* special header */
wenzelm@64673
   156
wenzelm@64673
   157
  def special_header(name: Document.Node.Name): Option[Document.Node.Header] =
wenzelm@64673
   158
    if (Thy_Header.is_ml_root(name.theory))
wenzelm@64673
   159
      Some(Document.Node.Header(List((import_name("", name, Thy_Header.ML_BOOTSTRAP), Position.none))))
wenzelm@64673
   160
    else if (Thy_Header.is_bootstrap(name.theory))
wenzelm@64673
   161
      Some(Document.Node.Header(List((import_name("", name, Thy_Header.PURE), Position.none))))
wenzelm@64673
   162
    else None
wenzelm@64673
   163
wenzelm@64673
   164
wenzelm@56316
   165
  /* document changes */
wenzelm@50566
   166
wenzelm@56316
   167
  def parse_change(
wenzelm@56315
   168
      reparse_limit: Int,
wenzelm@56315
   169
      previous: Document.Version,
wenzelm@56315
   170
      doc_blobs: Document.Blobs,
wenzelm@56315
   171
      edits: List[Document.Edit_Text]): Session.Change =
wenzelm@56316
   172
    Thy_Syntax.parse_change(this, reparse_limit, previous, doc_blobs, edits)
wenzelm@55134
   173
wenzelm@56316
   174
  def commit(change: Session.Change) { }
wenzelm@43651
   175
}