author | paulson <lp15@cam.ac.uk> |
Thu, 22 Aug 2024 22:26:28 +0100 | |
changeset 80736 | c8bcb14fcfa8 |
parent 77011 | 3e48f8c6afc9 |
child 82142 | 508a673c87ac |
permissions | -rw-r--r-- |
28495 | 1 |
/* Title: Pure/Thy/thy_header.scala |
2 |
Author: Makarius |
|
3 |
||
46939 | 4 |
Static theory header information. |
28495 | 5 |
*/ |
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
||
64824 | 10 |
import scala.util.parsing.input.Reader |
38149
3c380380beac
somewhat uniform Thy_Header.split_thy_path in ML and Scala;
wenzelm
parents:
36956
diff
changeset
|
11 |
import scala.util.matching.Regex |
34169 | 12 |
|
13 |
||
75393 | 14 |
object Thy_Header { |
58928
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58908
diff
changeset
|
15 |
/* bootstrap keywords */ |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58908
diff
changeset
|
16 |
|
63579 | 17 |
type Keywords = List[(String, Keyword.Spec)] |
18 |
type Abbrevs = List[(String, String)] |
|
58928
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58908
diff
changeset
|
19 |
|
58868
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58861
diff
changeset
|
20 |
val CHAPTER = "chapter" |
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58861
diff
changeset
|
21 |
val SECTION = "section" |
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58861
diff
changeset
|
22 |
val SUBSECTION = "subsection" |
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58861
diff
changeset
|
23 |
val SUBSUBSECTION = "subsubsection" |
61463 | 24 |
val PARAGRAPH = "paragraph" |
25 |
val SUBPARAGRAPH = "subparagraph" |
|
58999
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58928
diff
changeset
|
26 |
val TEXT = "text" |
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58928
diff
changeset
|
27 |
val TXT = "txt" |
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58928
diff
changeset
|
28 |
val TEXT_RAW = "text_raw" |
58868
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58861
diff
changeset
|
29 |
|
28495 | 30 |
val THEORY = "theory" |
31 |
val IMPORTS = "imports" |
|
46938
cda018294515
some support for outer syntax keyword declarations within theory header;
wenzelm
parents:
46737
diff
changeset
|
32 |
val KEYWORDS = "keywords" |
63579 | 33 |
val ABBREVS = "abbrevs" |
46938
cda018294515
some support for outer syntax keyword declarations within theory header;
wenzelm
parents:
46737
diff
changeset
|
34 |
val AND = "and" |
28495 | 35 |
val BEGIN = "begin" |
36 |
||
64854 | 37 |
val bootstrap_header: Keywords = |
58928
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58908
diff
changeset
|
38 |
List( |
72764 | 39 |
("%", Keyword.Spec()), |
40 |
("(", Keyword.Spec()), |
|
41 |
(")", Keyword.Spec()), |
|
42 |
(",", Keyword.Spec()), |
|
43 |
("::", Keyword.Spec()), |
|
44 |
("=", Keyword.Spec()), |
|
45 |
(AND, Keyword.Spec()), |
|
46 |
(BEGIN, Keyword.Spec(kind = Keyword.QUASI_COMMAND)), |
|
47 |
(IMPORTS, Keyword.Spec(kind = Keyword.QUASI_COMMAND)), |
|
48 |
(KEYWORDS, Keyword.Spec(kind = Keyword.QUASI_COMMAND)), |
|
49 |
(ABBREVS, Keyword.Spec(kind = Keyword.QUASI_COMMAND)), |
|
50 |
(CHAPTER, Keyword.Spec(kind = Keyword.DOCUMENT_HEADING)), |
|
51 |
(SECTION, Keyword.Spec(kind = Keyword.DOCUMENT_HEADING)), |
|
52 |
(SUBSECTION, Keyword.Spec(kind = Keyword.DOCUMENT_HEADING)), |
|
53 |
(SUBSUBSECTION, Keyword.Spec(kind = Keyword.DOCUMENT_HEADING)), |
|
54 |
(PARAGRAPH, Keyword.Spec(kind = Keyword.DOCUMENT_HEADING)), |
|
55 |
(SUBPARAGRAPH, Keyword.Spec(kind = Keyword.DOCUMENT_HEADING)), |
|
56 |
(TEXT, Keyword.Spec(kind = Keyword.DOCUMENT_BODY)), |
|
57 |
(TXT, Keyword.Spec(kind = Keyword.DOCUMENT_BODY)), |
|
58 |
(TEXT_RAW, Keyword.Spec(kind = Keyword.DOCUMENT_RAW)), |
|
59 |
(THEORY, Keyword.Spec(kind = Keyword.THY_BEGIN, tags = List("theory"))), |
|
60 |
("ML", Keyword.Spec(kind = Keyword.THY_DECL, tags = List("ML")))) |
|
58928
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58908
diff
changeset
|
61 |
|
77011
3e48f8c6afc9
parse citations from raw source, without formal context;
wenzelm
parents:
76890
diff
changeset
|
62 |
val bootstrap_keywords: Keyword.Keywords = |
58928
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58908
diff
changeset
|
63 |
Keyword.Keywords.empty.add_keywords(bootstrap_header) |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58908
diff
changeset
|
64 |
|
67004
af72fa58f71b
clarified lazy Completion within Outer_Syntax: measurable speedup of Sessions.deps;
wenzelm
parents:
66984
diff
changeset
|
65 |
val bootstrap_syntax: Outer_Syntax = |
af72fa58f71b
clarified lazy Completion within Outer_Syntax: measurable speedup of Sessions.deps;
wenzelm
parents:
66984
diff
changeset
|
66 |
Outer_Syntax.empty.add_keywords(bootstrap_header) |
34190 | 67 |
|
38149
3c380380beac
somewhat uniform Thy_Header.split_thy_path in ML and Scala;
wenzelm
parents:
36956
diff
changeset
|
68 |
|
64673
b5965890e54d
more uniform treatment of file name vs. theory name and special header;
wenzelm
parents:
64671
diff
changeset
|
69 |
/* file name vs. theory name */ |
62895
54c2abe7e9a4
treat ROOT.ML as theory with header "theory ML_Root imports ML_Bootstrap begin";
wenzelm
parents:
62849
diff
changeset
|
70 |
|
63022 | 71 |
val PURE = "Pure" |
62895
54c2abe7e9a4
treat ROOT.ML as theory with header "theory ML_Root imports ML_Bootstrap begin";
wenzelm
parents:
62849
diff
changeset
|
72 |
val ML_BOOTSTRAP = "ML_Bootstrap" |
65490 | 73 |
val ml_roots = List("ROOT0.ML" -> "ML_Root0", "ROOT.ML" -> "ML_Root") |
63022 | 74 |
val bootstrap_thys = List(PURE, ML_BOOTSTRAP).map(a => a -> ("Bootstrap_" + a)) |
44160
8848867501fb
clarified document model header: master_dir (native wrt. editor, potentially URL) and node_name (full canonical path);
wenzelm
parents:
44159
diff
changeset
|
75 |
|
67215 | 76 |
val bootstrap_global_theories = |
77 |
(Sessions.root_name :: (ml_roots ::: bootstrap_thys).map(_._2)).map(_ -> PURE) |
|
65490 | 78 |
|
65452 | 79 |
def import_name(s: String): String = |
76828 | 80 |
Url.get_base_name(s) match { |
81 |
case Some(name) if !File.is_thy(name) => name |
|
67164 | 82 |
case _ => error("Malformed theory import: " + quote(s)) |
83 |
} |
|
44225
a8f921e6484f
more robust Thy_Header.base_name, with minimal assumptions about path syntax;
wenzelm
parents:
44222
diff
changeset
|
84 |
|
76828 | 85 |
def get_thy_name(s: String): Option[String] = Url.get_base_name(s, suffix = ".thy") |
86 |
||
76890
d924a69e7d2b
more robust operations: avoid somewhat fragile Document.Node.Name.master_dir_path;
wenzelm
parents:
76839
diff
changeset
|
87 |
def list_thys(dir: Path): List[String] = { |
d924a69e7d2b
more robust operations: avoid somewhat fragile Document.Node.Name.master_dir_path;
wenzelm
parents:
76839
diff
changeset
|
88 |
val entries = try { File.read_dir(dir) } catch { case ERROR(_) => Nil } |
d924a69e7d2b
more robust operations: avoid somewhat fragile Document.Node.Name.master_dir_path;
wenzelm
parents:
76839
diff
changeset
|
89 |
entries.flatMap(get_thy_name) |
76828 | 90 |
} |
91 |
||
65452 | 92 |
def theory_name(s: String): String = |
76828 | 93 |
get_thy_name(s) match { |
76839 | 94 |
case Some(name) => |
95 |
bootstrap_thys.collectFirst({ case (a, b) if a == name => b }).getOrElse(name) |
|
76828 | 96 |
case None => |
97 |
Url.get_base_name(s) match { |
|
98 |
case Some(name) => |
|
99 |
if (name == Sessions.root_name) name |
|
100 |
else ml_roots.collectFirst({ case (a, b) if a == name => b }).getOrElse("") |
|
101 |
case None => "" |
|
102 |
} |
|
62895
54c2abe7e9a4
treat ROOT.ML as theory with header "theory ML_Root imports ML_Bootstrap begin";
wenzelm
parents:
62849
diff
changeset
|
103 |
} |
38149
3c380380beac
somewhat uniform Thy_Header.split_thy_path in ML and Scala;
wenzelm
parents:
36956
diff
changeset
|
104 |
|
64673
b5965890e54d
more uniform treatment of file name vs. theory name and special header;
wenzelm
parents:
64671
diff
changeset
|
105 |
def is_ml_root(theory: String): Boolean = |
b5965890e54d
more uniform treatment of file name vs. theory name and special header;
wenzelm
parents:
64671
diff
changeset
|
106 |
ml_roots.exists({ case (_, b) => b == theory }) |
b5965890e54d
more uniform treatment of file name vs. theory name and special header;
wenzelm
parents:
64671
diff
changeset
|
107 |
|
b5965890e54d
more uniform treatment of file name vs. theory name and special header;
wenzelm
parents:
64671
diff
changeset
|
108 |
def is_bootstrap(theory: String): Boolean = |
b5965890e54d
more uniform treatment of file name vs. theory name and special header;
wenzelm
parents:
64671
diff
changeset
|
109 |
bootstrap_thys.exists({ case (_, b) => b == theory }) |
b5965890e54d
more uniform treatment of file name vs. theory name and special header;
wenzelm
parents:
64671
diff
changeset
|
110 |
|
34169 | 111 |
|
68841 | 112 |
/* parser */ |
34169 | 113 |
|
75405 | 114 |
trait Parsers extends Parse.Parsers { |
75393 | 115 |
val header: Parser[Thy_Header] = { |
72765 | 116 |
val load_command = |
117 |
($$$("(") ~! (position(name) <~ $$$(")")) ^^ { case _ ~ x => x }) | |
|
118 |
success(("", Position.none)) |
|
72748
04d5f6d769a7
more flexible syntax for theory load commands via Isabelle/Scala;
wenzelm
parents:
72747
diff
changeset
|
119 |
|
72765 | 120 |
val keyword_kind = atom("outer syntax keyword specification", _.is_name) |
68841 | 121 |
val keyword_spec = |
72765 | 122 |
position(keyword_kind) ~ load_command ~ tags ^^ |
123 |
{ case (a, b) ~ c ~ d => |
|
124 |
Keyword.Spec(kind = a, kind_pos = b, |
|
125 |
load_command = c._1, load_command_pos = c._2, tags = d) |
|
126 |
} |
|
59694 | 127 |
|
68841 | 128 |
val keyword_decl = |
129 |
rep1(string) ~ |
|
130 |
opt($$$("::") ~! keyword_spec ^^ { case _ ~ x => x }) ^^ |
|
72764 | 131 |
{ case xs ~ y => xs.map((_, y.getOrElse(Keyword.Spec()))) } |
46938
cda018294515
some support for outer syntax keyword declarations within theory header;
wenzelm
parents:
46737
diff
changeset
|
132 |
|
68841 | 133 |
val keyword_decls = |
134 |
keyword_decl ~ rep($$$(AND) ~! keyword_decl ^^ { case _ ~ x => x }) ^^ |
|
135 |
{ case xs ~ yss => (xs :: yss).flatten } |
|
136 |
||
137 |
val abbrevs = |
|
138 |
rep1sep(rep1(text) ~ ($$$("=") ~! rep1(text)), $$$("and")) ^^ |
|
139 |
{ case res => for ((as ~ (_ ~ bs)) <- res; a <- as; b <- bs) yield (a, b) } |
|
63579 | 140 |
|
68841 | 141 |
val args = |
75407 | 142 |
position(this.theory_name) ~ |
143 |
(opt($$$(IMPORTS) ~! rep1(position(this.theory_name))) ^^ |
|
68841 | 144 |
{ case None => Nil case Some(_ ~ xs) => xs }) ~ |
145 |
(opt($$$(KEYWORDS) ~! keyword_decls) ^^ |
|
146 |
{ case None => Nil case Some(_ ~ xs) => xs }) ~ |
|
147 |
(opt($$$(ABBREVS) ~! abbrevs) ^^ |
|
148 |
{ case None => Nil case Some(_ ~ xs) => xs }) ~ |
|
72779 | 149 |
$$$(BEGIN) ^^ { case a ~ b ~ c ~ d ~ _ => Thy_Header(a._1, a._2, b, c, d) } |
34169 | 150 |
|
68841 | 151 |
val heading = |
152 |
(command(CHAPTER) | |
|
153 |
command(SECTION) | |
|
154 |
command(SUBSECTION) | |
|
155 |
command(SUBSUBSECTION) | |
|
156 |
command(PARAGRAPH) | |
|
157 |
command(SUBPARAGRAPH) | |
|
158 |
command(TEXT) | |
|
159 |
command(TXT) | |
|
160 |
command(TEXT_RAW)) ~ |
|
69887 | 161 |
annotation ~! document_source |
58868
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58861
diff
changeset
|
162 |
|
69887 | 163 |
(rep(heading) ~ command(THEORY) ~ annotation) ~! args ^^ { case _ ~ x => x } |
68841 | 164 |
} |
34169 | 165 |
} |
166 |
||
167 |
||
34190 | 168 |
/* read -- lazy scanning */ |
34169 | 169 |
|
75393 | 170 |
private def read_tokens(reader: Reader[Char], strict: Boolean): (List[Token], List[Token]) = { |
58928
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58908
diff
changeset
|
171 |
val token = Token.Parsers.token(bootstrap_keywords) |
73368 | 172 |
def make_tokens(in: Reader[Char]): LazyList[Token] = |
34169 | 173 |
token(in) match { |
64825 | 174 |
case Token.Parsers.Success(tok, rest) => tok #:: make_tokens(rest) |
73368 | 175 |
case _ => LazyList.empty |
34169 | 176 |
} |
64825 | 177 |
|
65539 | 178 |
val all_tokens = make_tokens(reader) |
179 |
val drop_tokens = |
|
180 |
if (strict) Nil |
|
181 |
else all_tokens.takeWhile(tok => !tok.is_command(Thy_Header.THEORY)).toList |
|
34190 | 182 |
|
65539 | 183 |
val tokens = all_tokens.drop(drop_tokens.length) |
64825 | 184 |
val tokens1 = tokens.takeWhile(tok => !tok.is_begin).toList |
185 |
val tokens2 = tokens.dropWhile(tok => !tok.is_begin).headOption.toList |
|
186 |
||
65539 | 187 |
(drop_tokens, tokens1 ::: tokens2) |
188 |
} |
|
189 |
||
75405 | 190 |
private object Parsers extends Parsers { |
68841 | 191 |
def parse_header(tokens: List[Token], pos: Token.Pos): Thy_Header = |
192 |
parse(commit(header), Token.reader(tokens, pos)) match { |
|
193 |
case Success(result, _) => result |
|
194 |
case bad => error(bad.toString) |
|
195 |
} |
|
196 |
} |
|
197 |
||
72775 | 198 |
def read(node_name: Document.Node.Name, reader: Reader[Char], |
72777
164cb0806d0a
proper positions for inlined command messages, e.g. for completion within theory header;
wenzelm
parents:
72776
diff
changeset
|
199 |
command: Boolean = true, |
75393 | 200 |
strict: Boolean = true |
201 |
): Thy_Header = { |
|
65539 | 202 |
val (_, tokens0) = read_tokens(reader, true) |
66918
ec2b50aeb0dd
more robust treatment of UTF8 in raw byte sources;
wenzelm
parents:
66713
diff
changeset
|
203 |
val text = Scan.reader_decode_utf8(reader, Token.implode(tokens0)) |
65539 | 204 |
|
72777
164cb0806d0a
proper positions for inlined command messages, e.g. for completion within theory header;
wenzelm
parents:
72776
diff
changeset
|
205 |
val (skip_tokens, tokens) = read_tokens(Scan.char_reader(text), strict) |
164cb0806d0a
proper positions for inlined command messages, e.g. for completion within theory header;
wenzelm
parents:
72776
diff
changeset
|
206 |
val pos = |
164cb0806d0a
proper positions for inlined command messages, e.g. for completion within theory header;
wenzelm
parents:
72776
diff
changeset
|
207 |
if (command) Token.Pos.command |
73359 | 208 |
else skip_tokens.foldLeft(Token.Pos.file(node_name.node))(_ advance _) |
65539 | 209 |
|
75405 | 210 |
Parsers.parse_header(tokens, pos).map(Symbol.decode).check(node_name) |
34169 | 211 |
} |
212 |
} |
|
44159 | 213 |
|
44185 | 214 |
sealed case class Thy_Header( |
72778 | 215 |
name: String, |
216 |
pos: Position.T, |
|
217 |
imports: List[(String, Position.T)], |
|
63579 | 218 |
keywords: Thy_Header.Keywords, |
75393 | 219 |
abbrevs: Thy_Header.Abbrevs |
220 |
) { |
|
72764 | 221 |
def map(f: String => String): Thy_Header = |
72778 | 222 |
Thy_Header(f(name), pos, |
223 |
imports.map({ case (a, b) => (f(a), b) }), |
|
72764 | 224 |
keywords.map({ case (a, spec) => (f(a), spec.map(f)) }), |
225 |
abbrevs.map({ case (a, b) => (f(a), f(b)) })) |
|
72765 | 226 |
|
75393 | 227 |
def check(node_name: Document.Node.Name): Thy_Header = { |
72775 | 228 |
val base_name = node_name.theory_base_name |
229 |
if (Long_Name.is_qualified(name)) { |
|
230 |
error("Bad theory name " + quote(name) + " with qualification" + Position.here(pos)) |
|
231 |
} |
|
232 |
if (base_name != name) { |
|
72776 | 233 |
error("Bad theory name " + quote(name) + " for file " + Path.basic(base_name).thy + |
234 |
Position.here(pos) + Completion.report_theories(pos, List(base_name))) |
|
72775 | 235 |
} |
236 |
||
72765 | 237 |
for ((_, spec) <- keywords) { |
238 |
if (spec.kind != Keyword.THY_LOAD && spec.load_command.nonEmpty) { |
|
239 |
error("Illegal load command specification for kind: " + quote(spec.kind) + |
|
240 |
Position.here(spec.kind_pos)) |
|
241 |
} |
|
242 |
if (!Command_Span.load_commands.exists(_.name == spec.load_command)) { |
|
72782 | 243 |
val completion = |
244 |
for { |
|
245 |
load_command <- Command_Span.load_commands |
|
246 |
name = load_command.name |
|
247 |
if name.startsWith(spec.load_command) |
|
248 |
} yield (name, (Markup.LOAD_COMMAND, name)) |
|
72765 | 249 |
error("Unknown load command specification: " + quote(spec.load_command) + |
72782 | 250 |
Position.here(spec.load_command_pos) + |
251 |
Completion.report_names(spec.load_command_pos, completion)) |
|
72765 | 252 |
} |
253 |
} |
|
254 |
this |
|
255 |
} |
|
70638
f164cec7ac22
clarified signature: prefer operations without position;
wenzelm
parents:
69887
diff
changeset
|
256 |
} |