author | wenzelm |
Mon, 03 Apr 2017 21:17:47 +0200 | |
changeset 65362 | 908a27a4b9c9 |
parent 64854 | f5aa712e6250 |
child 65384 | 36255c43c64c |
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( |
63579 | 42 |
("%", Keyword.no_spec), |
43 |
("(", Keyword.no_spec), |
|
44 |
(")", Keyword.no_spec), |
|
45 |
(",", Keyword.no_spec), |
|
46 |
("::", Keyword.no_spec), |
|
47 |
("=", Keyword.no_spec), |
|
48 |
(AND, Keyword.no_spec), |
|
49 |
(BEGIN, Keyword.quasi_command_spec), |
|
50 |
(IMPORTS, Keyword.quasi_command_spec), |
|
51 |
(KEYWORDS, Keyword.quasi_command_spec), |
|
52 |
(ABBREVS, Keyword.quasi_command_spec), |
|
53 |
(CHAPTER, (((Keyword.DOCUMENT_HEADING, Nil), Nil))), |
|
54 |
(SECTION, (((Keyword.DOCUMENT_HEADING, Nil), Nil))), |
|
55 |
(SUBSECTION, (((Keyword.DOCUMENT_HEADING, Nil), Nil))), |
|
56 |
(SUBSUBSECTION, (((Keyword.DOCUMENT_HEADING, Nil), Nil))), |
|
57 |
(PARAGRAPH, (((Keyword.DOCUMENT_HEADING, Nil), Nil))), |
|
58 |
(SUBPARAGRAPH, (((Keyword.DOCUMENT_HEADING, Nil), Nil))), |
|
59 |
(TEXT, (((Keyword.DOCUMENT_BODY, Nil), Nil))), |
|
60 |
(TXT, (((Keyword.DOCUMENT_BODY, Nil), Nil))), |
|
61 |
(TEXT_RAW, (((Keyword.DOCUMENT_RAW, Nil), Nil))), |
|
62 |
(THEORY, ((Keyword.THY_BEGIN, Nil), List("theory"))), |
|
63 |
("ML", ((Keyword.THY_DECL, Nil), 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 = |
58928
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58908
diff
changeset
|
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" |
63022 | 76 |
val ML_ROOT = "ML_Root" |
77 |
val ml_roots = List("ROOT0.ML" -> "ML_Root0", "ROOT.ML" -> ML_ROOT) |
|
78 |
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
|
79 |
|
44225
a8f921e6484f
more robust Thy_Header.base_name, with minimal assumptions about path syntax;
wenzelm
parents:
44222
diff
changeset
|
80 |
private val Base_Name = new Regex(""".*?([^/\\:]+)""") |
44222
9d5ef6cd4ee1
use full .thy file name as node name, which makes MiscUtilities.resolveSymlinks/File.getCanonicalPath more predictable;
wenzelm
parents:
44185
diff
changeset
|
81 |
private val Thy_Name = new Regex(""".*?([^/\\:]+)\.thy""") |
44160
8848867501fb
clarified document model header: master_dir (native wrt. editor, potentially URL) and node_name (full canonical path);
wenzelm
parents:
44159
diff
changeset
|
82 |
|
65362 | 83 |
def is_base_name(s: String): Boolean = |
84 |
s != "" && !s.exists("/\\:".contains(_)) |
|
85 |
||
44225
a8f921e6484f
more robust Thy_Header.base_name, with minimal assumptions about path syntax;
wenzelm
parents:
44222
diff
changeset
|
86 |
def base_name(s: String): String = |
a8f921e6484f
more robust Thy_Header.base_name, with minimal assumptions about path syntax;
wenzelm
parents:
44222
diff
changeset
|
87 |
s match { case Base_Name(name) => name case _ => error("Malformed import: " + quote(s)) } |
a8f921e6484f
more robust Thy_Header.base_name, with minimal assumptions about path syntax;
wenzelm
parents:
44222
diff
changeset
|
88 |
|
44222
9d5ef6cd4ee1
use full .thy file name as node name, which makes MiscUtilities.resolveSymlinks/File.getCanonicalPath more predictable;
wenzelm
parents:
44185
diff
changeset
|
89 |
def thy_name(s: String): Option[String] = |
63022 | 90 |
s match { case Thy_Name(name) => Some(name) case _ => None } |
91 |
||
92 |
def thy_name_bootstrap(s: String): Option[String] = |
|
62895
54c2abe7e9a4
treat ROOT.ML as theory with header "theory ML_Root imports ML_Bootstrap begin";
wenzelm
parents:
62849
diff
changeset
|
93 |
s match { |
63022 | 94 |
case Thy_Name(name) => |
95 |
Some(bootstrap_thys.collectFirst({ case (a, b) if a == name => b }).getOrElse(name)) |
|
62946 | 96 |
case Base_Name(name) => ml_roots.collectFirst({ case (a, b) if a == name => b }) |
62895
54c2abe7e9a4
treat ROOT.ML as theory with header "theory ML_Root imports ML_Bootstrap begin";
wenzelm
parents:
62849
diff
changeset
|
97 |
case _ => None |
54c2abe7e9a4
treat ROOT.ML as theory with header "theory ML_Root imports ML_Bootstrap begin";
wenzelm
parents:
62849
diff
changeset
|
98 |
} |
38149
3c380380beac
somewhat uniform Thy_Header.split_thy_path in ML and Scala;
wenzelm
parents:
36956
diff
changeset
|
99 |
|
64673
b5965890e54d
more uniform treatment of file name vs. theory name and special header;
wenzelm
parents:
64671
diff
changeset
|
100 |
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
|
101 |
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
|
102 |
|
b5965890e54d
more uniform treatment of file name vs. theory name and special header;
wenzelm
parents:
64671
diff
changeset
|
103 |
def is_bootstrap(theory: String): Boolean = |
b5965890e54d
more uniform treatment of file name vs. theory name and special header;
wenzelm
parents:
64671
diff
changeset
|
104 |
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
|
105 |
|
34169 | 106 |
|
107 |
/* header */ |
|
108 |
||
44159 | 109 |
val header: Parser[Thy_Header] = |
34169 | 110 |
{ |
48864
3ee314ae1e0a
added keyword kind "thy_load" (with optional list of file extensions);
wenzelm
parents:
48706
diff
changeset
|
111 |
val opt_files = |
58908 | 112 |
$$$("(") ~! (rep1sep(name, $$$(",")) <~ $$$(")")) ^^ { case _ ~ x => x } | |
48864
3ee314ae1e0a
added keyword kind "thy_load" (with optional list of file extensions);
wenzelm
parents:
48706
diff
changeset
|
113 |
success(Nil) |
59694 | 114 |
|
48864
3ee314ae1e0a
added keyword kind "thy_load" (with optional list of file extensions);
wenzelm
parents:
48706
diff
changeset
|
115 |
val keyword_spec = |
3ee314ae1e0a
added keyword kind "thy_load" (with optional list of file extensions);
wenzelm
parents:
48706
diff
changeset
|
116 |
atom("outer syntax keyword specification", _.is_name) ~ opt_files ~ tags ^^ |
3ee314ae1e0a
added keyword kind "thy_load" (with optional list of file extensions);
wenzelm
parents:
48706
diff
changeset
|
117 |
{ case x ~ y ~ z => ((x, y), z) } |
3ee314ae1e0a
added keyword kind "thy_load" (with optional list of file extensions);
wenzelm
parents:
48706
diff
changeset
|
118 |
|
46938
cda018294515
some support for outer syntax keyword declarations within theory header;
wenzelm
parents:
46737
diff
changeset
|
119 |
val keyword_decl = |
50128
599c935aac82
alternative completion for outer syntax keywords;
wenzelm
parents:
48882
diff
changeset
|
120 |
rep1(string) ~ |
63579 | 121 |
opt($$$("::") ~! keyword_spec ^^ { case _ ~ x => x }) ^^ |
122 |
{ case xs ~ y => xs.map((_, y.getOrElse(Keyword.no_spec))) } |
|
59694 | 123 |
|
46939 | 124 |
val keyword_decls = |
58908 | 125 |
keyword_decl ~ rep($$$(AND) ~! keyword_decl ^^ { case _ ~ x => x }) ^^ |
46939 | 126 |
{ case xs ~ yss => (xs :: yss).flatten } |
46938
cda018294515
some support for outer syntax keyword declarations within theory header;
wenzelm
parents:
46737
diff
changeset
|
127 |
|
63579 | 128 |
val abbrevs = |
129 |
rep1(text ~ ($$$("=") ~! text) ^^ { case a ~ (_ ~ b) => (a, b) }) |
|
130 |
||
34169 | 131 |
val args = |
59694 | 132 |
position(theory_name) ~ |
62969 | 133 |
(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
|
134 |
{ case None => Nil case Some(_ ~ xs) => xs }) ~ |
58908 | 135 |
(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
|
136 |
{ case None => Nil case Some(_ ~ xs) => xs }) ~ |
63579 | 137 |
(opt($$$(ABBREVS) ~! abbrevs) ^^ |
138 |
{ case None => Nil case Some(_ ~ xs) => xs }) ~ |
|
58908 | 139 |
$$$(BEGIN) ^^ |
63579 | 140 |
{ case x ~ ys ~ zs ~ ws ~ _ => Thy_Header(x, ys, zs, ws) } |
34169 | 141 |
|
58868
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58861
diff
changeset
|
142 |
val heading = |
62453 | 143 |
(command(CHAPTER) | |
58907 | 144 |
command(SECTION) | |
145 |
command(SUBSECTION) | |
|
58999
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58928
diff
changeset
|
146 |
command(SUBSUBSECTION) | |
61463 | 147 |
command(PARAGRAPH) | |
148 |
command(SUBPARAGRAPH) | |
|
58999
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58928
diff
changeset
|
149 |
command(TEXT) | |
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58928
diff
changeset
|
150 |
command(TXT) | |
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58928
diff
changeset
|
151 |
command(TEXT_RAW)) ~ |
58868
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58861
diff
changeset
|
152 |
tags ~! document_source |
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58861
diff
changeset
|
153 |
|
58907 | 154 |
(rep(heading) ~ command(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x } |
34169 | 155 |
} |
156 |
||
157 |
||
34190 | 158 |
/* read -- lazy scanning */ |
34169 | 159 |
|
64825 | 160 |
def read(reader: Reader[Char], start: Token.Pos, strict: Boolean = true): Thy_Header = |
34169 | 161 |
{ |
58928
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58908
diff
changeset
|
162 |
val token = Token.Parsers.token(bootstrap_keywords) |
64825 | 163 |
def make_tokens(in: Reader[Char]): Stream[Token] = |
34169 | 164 |
token(in) match { |
64825 | 165 |
case Token.Parsers.Success(tok, rest) => tok #:: make_tokens(rest) |
166 |
case _ => Stream.empty |
|
34169 | 167 |
} |
64825 | 168 |
|
169 |
val tokens = |
|
170 |
if (strict) make_tokens(reader) |
|
171 |
else make_tokens(reader).dropWhile(tok => !tok.is_command(Thy_Header.THEORY)) |
|
34190 | 172 |
|
64825 | 173 |
val tokens1 = tokens.takeWhile(tok => !tok.is_begin).toList |
174 |
val tokens2 = tokens.dropWhile(tok => !tok.is_begin).headOption.toList |
|
175 |
||
176 |
parse(commit(header), Token.reader(tokens1 ::: tokens2, start)) match { |
|
34190 | 177 |
case Success(result, _) => result |
178 |
case bad => error(bad.toString) |
|
179 |
} |
|
34169 | 180 |
} |
181 |
} |
|
44159 | 182 |
|
183 |
||
44185 | 184 |
sealed case class Thy_Header( |
59694 | 185 |
name: (String, Position.T), |
186 |
imports: List[(String, Position.T)], |
|
63579 | 187 |
keywords: Thy_Header.Keywords, |
188 |
abbrevs: Thy_Header.Abbrevs) |
|
44159 | 189 |
{ |
56823
37be55461dbe
more frugal access to theory text via Reader, reduced costs for I/O text decoding;
wenzelm
parents:
56801
diff
changeset
|
190 |
def decode_symbols: Thy_Header = |
37be55461dbe
more frugal access to theory text via Reader, reduced costs for I/O text decoding;
wenzelm
parents:
56801
diff
changeset
|
191 |
{ |
37be55461dbe
more frugal access to theory text via Reader, reduced costs for I/O text decoding;
wenzelm
parents:
56801
diff
changeset
|
192 |
val f = Symbol.decode _ |
63579 | 193 |
Thy_Header((f(name._1), name._2), |
194 |
imports.map({ case (a, b) => (f(a), b) }), |
|
195 |
keywords.map({ case (a, ((b, c), d)) => (f(a), ((f(b), c.map(f)), d.map(f))) }), |
|
196 |
abbrevs.map({ case (a, b) => (f(a), f(b)) })) |
|
56823
37be55461dbe
more frugal access to theory text via Reader, reduced costs for I/O text decoding;
wenzelm
parents:
56801
diff
changeset
|
197 |
} |
44159 | 198 |
} |