author | wenzelm |
Wed, 01 Nov 2017 21:02:16 +0100 | |
changeset 66984 | a1d3e5df0c95 |
parent 66918 | ec2b50aeb0dd |
child 67004 | af72fa58f71b |
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 |
||
43611 | 10 |
import scala.annotation.tailrec |
34169 | 11 |
import scala.collection.mutable |
64824 | 12 |
import scala.util.parsing.input.Reader |
38149
3c380380beac
somewhat uniform Thy_Header.split_thy_path in ML and Scala;
wenzelm
parents:
36956
diff
changeset
|
13 |
import scala.util.matching.Regex |
34169 | 14 |
|
15 |
||
43661
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43652
diff
changeset
|
16 |
object Thy_Header extends Parse.Parser |
32450 | 17 |
{ |
58928
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58908
diff
changeset
|
18 |
/* bootstrap keywords */ |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58908
diff
changeset
|
19 |
|
63579 | 20 |
type Keywords = List[(String, Keyword.Spec)] |
21 |
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
|
22 |
|
58868
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58861
diff
changeset
|
23 |
val CHAPTER = "chapter" |
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58861
diff
changeset
|
24 |
val SECTION = "section" |
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58861
diff
changeset
|
25 |
val SUBSECTION = "subsection" |
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58861
diff
changeset
|
26 |
val SUBSUBSECTION = "subsubsection" |
61463 | 27 |
val PARAGRAPH = "paragraph" |
28 |
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
|
29 |
val TEXT = "text" |
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58928
diff
changeset
|
30 |
val TXT = "txt" |
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58928
diff
changeset
|
31 |
val TEXT_RAW = "text_raw" |
58868
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58861
diff
changeset
|
32 |
|
28495 | 33 |
val THEORY = "theory" |
34 |
val IMPORTS = "imports" |
|
46938
cda018294515
some support for outer syntax keyword declarations within theory header;
wenzelm
parents:
46737
diff
changeset
|
35 |
val KEYWORDS = "keywords" |
63579 | 36 |
val ABBREVS = "abbrevs" |
46938
cda018294515
some support for outer syntax keyword declarations within theory header;
wenzelm
parents:
46737
diff
changeset
|
37 |
val AND = "and" |
28495 | 38 |
val BEGIN = "begin" |
39 |
||
64854 | 40 |
val bootstrap_header: Keywords = |
58928
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58908
diff
changeset
|
41 |
List( |
65384 | 42 |
("%", Keyword.Spec.none), |
43 |
("(", Keyword.Spec.none), |
|
44 |
(")", Keyword.Spec.none), |
|
45 |
(",", Keyword.Spec.none), |
|
46 |
("::", Keyword.Spec.none), |
|
47 |
("=", Keyword.Spec.none), |
|
48 |
(AND, Keyword.Spec.none), |
|
49 |
(BEGIN, Keyword.Spec(Keyword.QUASI_COMMAND)), |
|
50 |
(IMPORTS, Keyword.Spec(Keyword.QUASI_COMMAND)), |
|
51 |
(KEYWORDS, Keyword.Spec(Keyword.QUASI_COMMAND)), |
|
52 |
(ABBREVS, Keyword.Spec(Keyword.QUASI_COMMAND)), |
|
53 |
(CHAPTER, Keyword.Spec(Keyword.DOCUMENT_HEADING)), |
|
54 |
(SECTION, Keyword.Spec(Keyword.DOCUMENT_HEADING)), |
|
55 |
(SUBSECTION, Keyword.Spec(Keyword.DOCUMENT_HEADING)), |
|
56 |
(SUBSUBSECTION, Keyword.Spec(Keyword.DOCUMENT_HEADING)), |
|
57 |
(PARAGRAPH, Keyword.Spec(Keyword.DOCUMENT_HEADING)), |
|
58 |
(SUBPARAGRAPH, Keyword.Spec(Keyword.DOCUMENT_HEADING)), |
|
59 |
(TEXT, Keyword.Spec(Keyword.DOCUMENT_BODY)), |
|
60 |
(TXT, Keyword.Spec(Keyword.DOCUMENT_BODY)), |
|
61 |
(TEXT_RAW, Keyword.Spec(Keyword.DOCUMENT_RAW)), |
|
62 |
(THEORY, Keyword.Spec(Keyword.THY_BEGIN, tags = List("theory"))), |
|
63 |
("ML", Keyword.Spec(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
|
64 |
|
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58908
diff
changeset
|
65 |
private val bootstrap_keywords = |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58908
diff
changeset
|
66 |
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
|
67 |
|
59736
5c1a0069b9d3
tight span for theory header, which is relevant for error positions (including semantic completion);
wenzelm
parents:
59705
diff
changeset
|
68 |
lazy val bootstrap_syntax: Outer_Syntax = |
66984 | 69 |
Outer_Syntax.init.add_keywords(bootstrap_header) |
34190 | 70 |
|
38149
3c380380beac
somewhat uniform Thy_Header.split_thy_path in ML and Scala;
wenzelm
parents:
36956
diff
changeset
|
71 |
|
64673
b5965890e54d
more uniform treatment of file name vs. theory name and special header;
wenzelm
parents:
64671
diff
changeset
|
72 |
/* 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
|
73 |
|
63022 | 74 |
val PURE = "Pure" |
62895
54c2abe7e9a4
treat ROOT.ML as theory with header "theory ML_Root imports ML_Bootstrap begin";
wenzelm
parents:
62849
diff
changeset
|
75 |
val ML_BOOTSTRAP = "ML_Bootstrap" |
65490 | 76 |
val ml_roots = List("ROOT0.ML" -> "ML_Root0", "ROOT.ML" -> "ML_Root") |
63022 | 77 |
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
|
78 |
|
65490 | 79 |
val bootstrap_global_theories = (ml_roots ::: bootstrap_thys).map(p => (p._2 -> PURE)) |
80 |
||
65452 | 81 |
private val Thy_File_Name = new Regex(""".*?([^/\\:]+)\.thy""") |
82 |
private val Import_Name = new Regex(""".*?([^/\\:]+)""") |
|
44160
8848867501fb
clarified document model header: master_dir (native wrt. editor, potentially URL) and node_name (full canonical path);
wenzelm
parents:
44159
diff
changeset
|
83 |
|
65526 | 84 |
def is_base_name(s: String): Boolean = |
85 |
s != "" && !s.exists("/\\:".contains(_)) |
|
86 |
||
65452 | 87 |
def import_name(s: String): String = |
88 |
s match { case Import_Name(name) => name case _ => error("Malformed import: " + quote(s)) } |
|
44225
a8f921e6484f
more robust Thy_Header.base_name, with minimal assumptions about path syntax;
wenzelm
parents:
44222
diff
changeset
|
89 |
|
65452 | 90 |
def theory_name(s: String): String = |
62895
54c2abe7e9a4
treat ROOT.ML as theory with header "theory ML_Root imports ML_Bootstrap begin";
wenzelm
parents:
62849
diff
changeset
|
91 |
s match { |
66195 | 92 |
case Thy_File_Name(name) => bootstrap_name(name) |
65452 | 93 |
case Import_Name(name) => |
94 |
ml_roots.collectFirst({ case (a, b) if a == name => b }).getOrElse("") |
|
95 |
case _ => "" |
|
62895
54c2abe7e9a4
treat ROOT.ML as theory with header "theory ML_Root imports ML_Bootstrap begin";
wenzelm
parents:
62849
diff
changeset
|
96 |
} |
38149
3c380380beac
somewhat uniform Thy_Header.split_thy_path in ML and Scala;
wenzelm
parents:
36956
diff
changeset
|
97 |
|
64673
b5965890e54d
more uniform treatment of file name vs. theory name and special header;
wenzelm
parents:
64671
diff
changeset
|
98 |
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
|
99 |
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
|
100 |
|
b5965890e54d
more uniform treatment of file name vs. theory name and special header;
wenzelm
parents:
64671
diff
changeset
|
101 |
def is_bootstrap(theory: String): Boolean = |
b5965890e54d
more uniform treatment of file name vs. theory name and special header;
wenzelm
parents:
64671
diff
changeset
|
102 |
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
|
103 |
|
66195 | 104 |
def bootstrap_name(theory: String): String = |
105 |
bootstrap_thys.collectFirst({ case (a, b) if a == theory => b }).getOrElse(theory) |
|
106 |
||
34169 | 107 |
|
108 |
/* header */ |
|
109 |
||
44159 | 110 |
val header: Parser[Thy_Header] = |
34169 | 111 |
{ |
48864
3ee314ae1e0a
added keyword kind "thy_load" (with optional list of file extensions);
wenzelm
parents:
48706
diff
changeset
|
112 |
val opt_files = |
58908 | 113 |
$$$("(") ~! (rep1sep(name, $$$(",")) <~ $$$(")")) ^^ { case _ ~ x => x } | |
48864
3ee314ae1e0a
added keyword kind "thy_load" (with optional list of file extensions);
wenzelm
parents:
48706
diff
changeset
|
114 |
success(Nil) |
59694 | 115 |
|
48864
3ee314ae1e0a
added keyword kind "thy_load" (with optional list of file extensions);
wenzelm
parents:
48706
diff
changeset
|
116 |
val keyword_spec = |
3ee314ae1e0a
added keyword kind "thy_load" (with optional list of file extensions);
wenzelm
parents:
48706
diff
changeset
|
117 |
atom("outer syntax keyword specification", _.is_name) ~ opt_files ~ tags ^^ |
65384 | 118 |
{ case x ~ y ~ z => Keyword.Spec(x, y, z) } |
48864
3ee314ae1e0a
added keyword kind "thy_load" (with optional list of file extensions);
wenzelm
parents:
48706
diff
changeset
|
119 |
|
46938
cda018294515
some support for outer syntax keyword declarations within theory header;
wenzelm
parents:
46737
diff
changeset
|
120 |
val keyword_decl = |
50128
599c935aac82
alternative completion for outer syntax keywords;
wenzelm
parents:
48882
diff
changeset
|
121 |
rep1(string) ~ |
63579 | 122 |
opt($$$("::") ~! keyword_spec ^^ { case _ ~ x => x }) ^^ |
65384 | 123 |
{ case xs ~ y => xs.map((_, y.getOrElse(Keyword.Spec.none))) } |
59694 | 124 |
|
46939 | 125 |
val keyword_decls = |
58908 | 126 |
keyword_decl ~ rep($$$(AND) ~! keyword_decl ^^ { case _ ~ x => x }) ^^ |
46939 | 127 |
{ case xs ~ yss => (xs :: yss).flatten } |
46938
cda018294515
some support for outer syntax keyword declarations within theory header;
wenzelm
parents:
46737
diff
changeset
|
128 |
|
63579 | 129 |
val abbrevs = |
130 |
rep1(text ~ ($$$("=") ~! text) ^^ { case a ~ (_ ~ b) => (a, b) }) |
|
131 |
||
34169 | 132 |
val args = |
59694 | 133 |
position(theory_name) ~ |
62969 | 134 |
(opt($$$(IMPORTS) ~! rep1(position(theory_name))) ^^ |
56823
37be55461dbe
more frugal access to theory text via Reader, reduced costs for I/O text decoding;
wenzelm
parents:
56801
diff
changeset
|
135 |
{ case None => Nil case Some(_ ~ xs) => xs }) ~ |
58908 | 136 |
(opt($$$(KEYWORDS) ~! keyword_decls) ^^ |
56823
37be55461dbe
more frugal access to theory text via Reader, reduced costs for I/O text decoding;
wenzelm
parents:
56801
diff
changeset
|
137 |
{ case None => Nil case Some(_ ~ xs) => xs }) ~ |
63579 | 138 |
(opt($$$(ABBREVS) ~! abbrevs) ^^ |
139 |
{ case None => Nil case Some(_ ~ xs) => xs }) ~ |
|
58908 | 140 |
$$$(BEGIN) ^^ |
66713 | 141 |
{ case (name, pos) ~ imports ~ keywords ~ abbrevs ~ _ => |
142 |
val f = Symbol.decode _ |
|
143 |
Thy_Header((f(name), pos), |
|
144 |
imports.map({ case (a, b) => (f(a), b) }), |
|
145 |
keywords.map({ case (a, Keyword.Spec(b, c, d)) => |
|
146 |
(f(a), Keyword.Spec(f(b), c.map(f), d.map(f))) }), |
|
147 |
abbrevs.map({ case (a, b) => (f(a), f(b)) })) |
|
148 |
} |
|
34169 | 149 |
|
58868
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58861
diff
changeset
|
150 |
val heading = |
62453 | 151 |
(command(CHAPTER) | |
58907 | 152 |
command(SECTION) | |
153 |
command(SUBSECTION) | |
|
58999
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58928
diff
changeset
|
154 |
command(SUBSUBSECTION) | |
61463 | 155 |
command(PARAGRAPH) | |
156 |
command(SUBPARAGRAPH) | |
|
58999
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58928
diff
changeset
|
157 |
command(TEXT) | |
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58928
diff
changeset
|
158 |
command(TXT) | |
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58928
diff
changeset
|
159 |
command(TEXT_RAW)) ~ |
58868
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58861
diff
changeset
|
160 |
tags ~! document_source |
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58861
diff
changeset
|
161 |
|
58907 | 162 |
(rep(heading) ~ command(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x } |
34169 | 163 |
} |
164 |
||
165 |
||
34190 | 166 |
/* read -- lazy scanning */ |
34169 | 167 |
|
65539 | 168 |
private def read_tokens(reader: Reader[Char], strict: Boolean): (List[Token], List[Token]) = |
34169 | 169 |
{ |
58928
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58908
diff
changeset
|
170 |
val token = Token.Parsers.token(bootstrap_keywords) |
64825 | 171 |
def make_tokens(in: Reader[Char]): Stream[Token] = |
34169 | 172 |
token(in) match { |
64825 | 173 |
case Token.Parsers.Success(tok, rest) => tok #:: make_tokens(rest) |
174 |
case _ => Stream.empty |
|
34169 | 175 |
} |
64825 | 176 |
|
65539 | 177 |
val all_tokens = make_tokens(reader) |
178 |
val drop_tokens = |
|
179 |
if (strict) Nil |
|
180 |
else all_tokens.takeWhile(tok => !tok.is_command(Thy_Header.THEORY)).toList |
|
34190 | 181 |
|
65539 | 182 |
val tokens = all_tokens.drop(drop_tokens.length) |
64825 | 183 |
val tokens1 = tokens.takeWhile(tok => !tok.is_begin).toList |
184 |
val tokens2 = tokens.dropWhile(tok => !tok.is_begin).headOption.toList |
|
185 |
||
65539 | 186 |
(drop_tokens, tokens1 ::: tokens2) |
187 |
} |
|
188 |
||
189 |
def read(reader: Reader[Char], start: Token.Pos, strict: Boolean = true): Thy_Header = |
|
190 |
{ |
|
191 |
val (_, tokens0) = read_tokens(reader, true) |
|
66918
ec2b50aeb0dd
more robust treatment of UTF8 in raw byte sources;
wenzelm
parents:
66713
diff
changeset
|
192 |
val text = Scan.reader_decode_utf8(reader, Token.implode(tokens0)) |
65539 | 193 |
|
194 |
val (drop_tokens, tokens) = read_tokens(Scan.char_reader(text), strict) |
|
195 |
val pos = (start /: drop_tokens)(_.advance(_)) |
|
196 |
||
197 |
parse(commit(header), Token.reader(tokens, pos)) match { |
|
34190 | 198 |
case Success(result, _) => result |
199 |
case bad => error(bad.toString) |
|
200 |
} |
|
34169 | 201 |
} |
202 |
} |
|
44159 | 203 |
|
44185 | 204 |
sealed case class Thy_Header( |
59694 | 205 |
name: (String, Position.T), |
206 |
imports: List[(String, Position.T)], |
|
63579 | 207 |
keywords: Thy_Header.Keywords, |
208 |
abbrevs: Thy_Header.Abbrevs) |