src/Pure/Build/file_format.scala
author wenzelm
Sat, 17 Feb 2024 16:56:55 +0100
changeset 79647 b7187d4cdf68
parent 79502 c7a98469c0e7
permissions -rw-r--r--
clarified default "isabelle build -j0 -H";
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
79502
c7a98469c0e7 clarified directories;
wenzelm
parents: 76860
diff changeset
     1
/*  Title:      Pure/Build/file_format.scala
69255
800b1ce96fce more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
800b1ce96fce more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
diff changeset
     3
73130
5f8f7746b4aa tuned comments;
wenzelm
parents: 72957
diff changeset
     4
Support for user-defined file formats, associated with active session.
69255
800b1ce96fce more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
diff changeset
     5
*/
800b1ce96fce more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
diff changeset
     6
800b1ce96fce more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
diff changeset
     7
package isabelle
800b1ce96fce more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
diff changeset
     8
800b1ce96fce more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
diff changeset
     9
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73367
diff changeset
    10
object File_Format {
76793
fa70b536ec50 tuned output;
wenzelm
parents: 76792
diff changeset
    11
  object No_Data extends AnyRef {
fa70b536ec50 tuned output;
wenzelm
parents: 76792
diff changeset
    12
    override def toString: String = "File_Format.No_Data"
fa70b536ec50 tuned output;
wenzelm
parents: 76792
diff changeset
    13
  }
76792
23f433294173 support for generic File_Format.parse_data, with persistent result in document model;
wenzelm
parents: 75941
diff changeset
    14
69488
b05c0bb47f6d support for File_Format.Session, e.g. server process accessible via prover options;
wenzelm
parents: 69277
diff changeset
    15
  sealed case class Theory_Context(name: Document.Node.Name, content: String)
b05c0bb47f6d support for File_Format.Session, e.g. server process accessible via prover options;
wenzelm
parents: 69277
diff changeset
    16
b05c0bb47f6d support for File_Format.Session, e.g. server process accessible via prover options;
wenzelm
parents: 69277
diff changeset
    17
71733
6c470c918aad more general support for isabelle_scala_service;
wenzelm
parents: 71601
diff changeset
    18
  /* registry */
69488
b05c0bb47f6d support for File_Format.Session, e.g. server process accessible via prover options;
wenzelm
parents: 69277
diff changeset
    19
71733
6c470c918aad more general support for isabelle_scala_service;
wenzelm
parents: 71601
diff changeset
    20
  lazy val registry: Registry =
72159
40b5ee5889d2 clarified management of services: static declarations vs. dynamic instances (e.g. relevant for stateful Session.Protocol_Handler, notably Scala.Handler and session "System");
wenzelm
parents: 71738
diff changeset
    21
    new Registry(Isabelle_System.make_services(classOf[File_Format]))
69255
800b1ce96fce more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
diff changeset
    22
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73367
diff changeset
    23
  final class Registry private [File_Format](file_formats: List[File_Format]) {
69255
800b1ce96fce more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
diff changeset
    24
    override def toString: String = file_formats.mkString("File_Format.Environment(", ",", ")")
800b1ce96fce more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
diff changeset
    25
800b1ce96fce more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
diff changeset
    26
    def get(name: String): Option[File_Format] = file_formats.find(_.detect(name))
800b1ce96fce more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
diff changeset
    27
    def get(name: Document.Node.Name): Option[File_Format] = get(name.node)
800b1ce96fce more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
diff changeset
    28
    def get_theory(name: Document.Node.Name): Option[File_Format] = get(name.theory)
800b1ce96fce more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
diff changeset
    29
    def is_theory(name: Document.Node.Name): Boolean = get_theory(name).isDefined
69488
b05c0bb47f6d support for File_Format.Session, e.g. server process accessible via prover options;
wenzelm
parents: 69277
diff changeset
    30
76849
d431a9340163 more systematic Sessions.illegal_theory, based on File_Format.theory_excluded;
wenzelm
parents: 76846
diff changeset
    31
    def theory_excluded(name: String): Boolean = file_formats.exists(_.theory_excluded(name))
d431a9340163 more systematic Sessions.illegal_theory, based on File_Format.theory_excluded;
wenzelm
parents: 76846
diff changeset
    32
76792
23f433294173 support for generic File_Format.parse_data, with persistent result in document model;
wenzelm
parents: 75941
diff changeset
    33
    def parse_data(name: String, text: String): AnyRef =
23f433294173 support for generic File_Format.parse_data, with persistent result in document model;
wenzelm
parents: 75941
diff changeset
    34
      get(name) match {
23f433294173 support for generic File_Format.parse_data, with persistent result in document model;
wenzelm
parents: 75941
diff changeset
    35
        case Some(file_format) => file_format.parse_data(name, text)
23f433294173 support for generic File_Format.parse_data, with persistent result in document model;
wenzelm
parents: 75941
diff changeset
    36
        case None => No_Data
23f433294173 support for generic File_Format.parse_data, with persistent result in document model;
wenzelm
parents: 75941
diff changeset
    37
      }
23f433294173 support for generic File_Format.parse_data, with persistent result in document model;
wenzelm
parents: 75941
diff changeset
    38
    def parse_data(name: Document.Node.Name, text: String): AnyRef = parse_data(name.node, text)
23f433294173 support for generic File_Format.parse_data, with persistent result in document model;
wenzelm
parents: 75941
diff changeset
    39
69488
b05c0bb47f6d support for File_Format.Session, e.g. server process accessible via prover options;
wenzelm
parents: 69277
diff changeset
    40
    def start_session(session: isabelle.Session): Session =
69493
6fa742b03107 clarified;
wenzelm
parents: 69489
diff changeset
    41
      new Session(file_formats.map(_.start(session)).filterNot(_ == No_Agent))
69255
800b1ce96fce more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
diff changeset
    42
  }
800b1ce96fce more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
diff changeset
    43
69488
b05c0bb47f6d support for File_Format.Session, e.g. server process accessible via prover options;
wenzelm
parents: 69277
diff changeset
    44
b05c0bb47f6d support for File_Format.Session, e.g. server process accessible via prover options;
wenzelm
parents: 69277
diff changeset
    45
  /* session */
b05c0bb47f6d support for File_Format.Session, e.g. server process accessible via prover options;
wenzelm
parents: 69277
diff changeset
    46
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73367
diff changeset
    47
  final class Session private[File_Format](agents: List[Agent]) {
69488
b05c0bb47f6d support for File_Format.Session, e.g. server process accessible via prover options;
wenzelm
parents: 69277
diff changeset
    48
    override def toString: String =
b05c0bb47f6d support for File_Format.Session, e.g. server process accessible via prover options;
wenzelm
parents: 69277
diff changeset
    49
      agents.mkString("File_Format.Session(", ", ", ")")
b05c0bb47f6d support for File_Format.Session, e.g. server process accessible via prover options;
wenzelm
parents: 69277
diff changeset
    50
b05c0bb47f6d support for File_Format.Session, e.g. server process accessible via prover options;
wenzelm
parents: 69277
diff changeset
    51
    def prover_options(options: Options): Options =
73359
d8a0e996614b tuned --- fewer warnings;
wenzelm
parents: 73340
diff changeset
    52
      agents.foldLeft(options) { case (opts, agent) => agent.prover_options(opts) }
69488
b05c0bb47f6d support for File_Format.Session, e.g. server process accessible via prover options;
wenzelm
parents: 69277
diff changeset
    53
76842
18465808e61f tuned signature;
wenzelm
parents: 76828
diff changeset
    54
    def stop_session(): Unit = agents.foreach(_.stop())
69488
b05c0bb47f6d support for File_Format.Session, e.g. server process accessible via prover options;
wenzelm
parents: 69277
diff changeset
    55
  }
b05c0bb47f6d support for File_Format.Session, e.g. server process accessible via prover options;
wenzelm
parents: 69277
diff changeset
    56
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73367
diff changeset
    57
  trait Agent {
69488
b05c0bb47f6d support for File_Format.Session, e.g. server process accessible via prover options;
wenzelm
parents: 69277
diff changeset
    58
    def prover_options(options: Options): Options = options
73367
77ef8bef0593 clarified signature --- fewer warnings;
wenzelm
parents: 73359
diff changeset
    59
    def stop(): Unit = {}
69488
b05c0bb47f6d support for File_Format.Session, e.g. server process accessible via prover options;
wenzelm
parents: 69277
diff changeset
    60
  }
b05c0bb47f6d support for File_Format.Session, e.g. server process accessible via prover options;
wenzelm
parents: 69277
diff changeset
    61
69493
6fa742b03107 clarified;
wenzelm
parents: 69489
diff changeset
    62
  object No_Agent extends Agent
69255
800b1ce96fce more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
diff changeset
    63
}
800b1ce96fce more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
diff changeset
    64
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73367
diff changeset
    65
abstract class File_Format extends Isabelle_System.Service {
69255
800b1ce96fce more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
diff changeset
    66
  def format_name: String
71738
3d514ab74161 tuned message;
wenzelm
parents: 71733
diff changeset
    67
  override def toString: String = "File_Format(" + format_name + ")"
69255
800b1ce96fce more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
diff changeset
    68
69257
039edba27102 clarified signature;
wenzelm
parents: 69255
diff changeset
    69
  def file_ext: String
039edba27102 clarified signature;
wenzelm
parents: 69255
diff changeset
    70
  def detect(name: String): Boolean = name.endsWith("." + file_ext)
039edba27102 clarified signature;
wenzelm
parents: 69255
diff changeset
    71
76792
23f433294173 support for generic File_Format.parse_data, with persistent result in document model;
wenzelm
parents: 75941
diff changeset
    72
  def parse_data(name: String, text: String): AnyRef = File_Format.No_Data
23f433294173 support for generic File_Format.parse_data, with persistent result in document model;
wenzelm
parents: 75941
diff changeset
    73
69257
039edba27102 clarified signature;
wenzelm
parents: 69255
diff changeset
    74
039edba27102 clarified signature;
wenzelm
parents: 69255
diff changeset
    75
  /* implicit theory context: name and content */
039edba27102 clarified signature;
wenzelm
parents: 69255
diff changeset
    76
039edba27102 clarified signature;
wenzelm
parents: 69255
diff changeset
    77
  def theory_suffix: String = ""
69259
wenzelm
parents: 69257
diff changeset
    78
  def theory_content(name: String): String = ""
76849
d431a9340163 more systematic Sessions.illegal_theory, based on File_Format.theory_excluded;
wenzelm
parents: 76846
diff changeset
    79
  def theory_excluded(name: String): Boolean = false
69255
800b1ce96fce more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
diff changeset
    80
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73367
diff changeset
    81
  def make_theory_name(
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73367
diff changeset
    82
    resources: Resources,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73367
diff changeset
    83
    name: Document.Node.Name
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73367
diff changeset
    84
  ): Option[Document.Node.Name] = {
69257
039edba27102 clarified signature;
wenzelm
parents: 69255
diff changeset
    85
    for {
76846
wenzelm
parents: 76843
diff changeset
    86
      theory <- Url.get_base_name(name.node)
72839
a597300290de clarified File_Format.detect: needs to operate on full node name;
wenzelm
parents: 72652
diff changeset
    87
      if detect(name.node) && theory_suffix.nonEmpty
69257
039edba27102 clarified signature;
wenzelm
parents: 69255
diff changeset
    88
    }
039edba27102 clarified signature;
wenzelm
parents: 69255
diff changeset
    89
    yield {
76858
39db5e268aaf tuned signature, following Url.append_path;
wenzelm
parents: 76849
diff changeset
    90
      val node = resources.append_path(name.node, Path.explode(theory_suffix))
76860
f95ed5a0600c clarified signature: uniform master_dir instead of separate field;
wenzelm
parents: 76859
diff changeset
    91
      Document.Node.Name(node, theory = theory)
69257
039edba27102 clarified signature;
wenzelm
parents: 69255
diff changeset
    92
    }
039edba27102 clarified signature;
wenzelm
parents: 69255
diff changeset
    93
  }
69255
800b1ce96fce more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
diff changeset
    94
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73367
diff changeset
    95
  def make_theory_content(resources: Resources, thy_name: Document.Node.Name): Option[String] = {
69257
039edba27102 clarified signature;
wenzelm
parents: 69255
diff changeset
    96
    for {
76828
a5ff9cf61551 clarified generic path operations;
wenzelm
parents: 76793
diff changeset
    97
      prefix <- Url.strip_base_name(thy_name.node)
a5ff9cf61551 clarified generic path operations;
wenzelm
parents: 76793
diff changeset
    98
      suffix <- Url.get_base_name(thy_name.node)
72839
a597300290de clarified File_Format.detect: needs to operate on full node name;
wenzelm
parents: 72652
diff changeset
    99
      if detect(prefix) && suffix == theory_suffix
76828
a5ff9cf61551 clarified generic path operations;
wenzelm
parents: 76793
diff changeset
   100
      thy <- Url.get_base_name(prefix)
72839
a597300290de clarified File_Format.detect: needs to operate on full node name;
wenzelm
parents: 72652
diff changeset
   101
      s <- proper_string(theory_content(thy))
69257
039edba27102 clarified signature;
wenzelm
parents: 69255
diff changeset
   102
    } yield s
039edba27102 clarified signature;
wenzelm
parents: 69255
diff changeset
   103
  }
69255
800b1ce96fce more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
diff changeset
   104
75941
4bbbbaa656f1 clarified modules;
wenzelm
parents: 75393
diff changeset
   105
  def html_document(snapshot: Document.Snapshot): Option[Browser_Info.HTML_Document] = None
69488
b05c0bb47f6d support for File_Format.Session, e.g. server process accessible via prover options;
wenzelm
parents: 69277
diff changeset
   106
b05c0bb47f6d support for File_Format.Session, e.g. server process accessible via prover options;
wenzelm
parents: 69277
diff changeset
   107
69489
18c58b0da0a9 tuned comments;
wenzelm
parents: 69488
diff changeset
   108
  /* PIDE session agent */
69488
b05c0bb47f6d support for File_Format.Session, e.g. server process accessible via prover options;
wenzelm
parents: 69277
diff changeset
   109
69493
6fa742b03107 clarified;
wenzelm
parents: 69489
diff changeset
   110
  def start(session: isabelle.Session): File_Format.Agent = File_Format.No_Agent
69255
800b1ce96fce more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
diff changeset
   111
}