author | wenzelm |
Sat, 17 Feb 2024 16:56:55 +0100 | |
changeset 79647 | b7187d4cdf68 |
parent 79502 | c7a98469c0e7 |
permissions | -rw-r--r-- |
79502 | 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 | 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 | 10 |
object File_Format { |
76793 | 11 |
object No_Data extends AnyRef { |
12 |
override def toString: String = "File_Format.No_Data" |
|
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 | 18 |
/* registry */ |
69488
b05c0bb47f6d
support for File_Format.Session, e.g. server process accessible via prover options;
wenzelm
parents:
69277
diff
changeset
|
19 |
|
71733 | 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 | 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 | 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 | 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 | 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 | 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 | 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 | 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 | 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 | 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 | 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 | 69 |
def file_ext: String |
70 |
def detect(name: String): Boolean = name.endsWith("." + file_ext) |
|
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 | 74 |
|
75 |
/* implicit theory context: name and content */ |
|
76 |
||
77 |
def theory_suffix: String = "" |
|
69259 | 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 | 81 |
def make_theory_name( |
82 |
resources: Resources, |
|
83 |
name: Document.Node.Name |
|
84 |
): Option[Document.Node.Name] = { |
|
69257 | 85 |
for { |
76846 | 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 | 88 |
} |
89 |
yield { |
|
76858 | 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 | 92 |
} |
93 |
} |
|
69255
800b1ce96fce
more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
diff
changeset
|
94 |
|
75393 | 95 |
def make_theory_content(resources: Resources, thy_name: Document.Node.Name): Option[String] = { |
69257 | 96 |
for { |
76828 | 97 |
prefix <- Url.strip_base_name(thy_name.node) |
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 | 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 | 102 |
} yield s |
103 |
} |
|
69255
800b1ce96fce
more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
diff
changeset
|
104 |
|
75941 | 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 | 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 | 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 |
} |