author | wenzelm |
Mon, 11 Jul 2016 18:39:30 +0200 | |
changeset 63448 | 998acd66fbd7 |
parent 63429 | baedd4724f08 |
child 63449 | b3f6e81cd13b |
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 |
43646 | 12 |
import scala.util.parsing.input.{Reader, CharSequenceReader} |
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 |
|
63429 | 20 |
type Keywords = List[(String, Keyword.Spec, Option[String])] |
58928
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58908
diff
changeset
|
21 |
|
58868
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58861
diff
changeset
|
22 |
val CHAPTER = "chapter" |
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58861
diff
changeset
|
23 |
val SECTION = "section" |
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58861
diff
changeset
|
24 |
val SUBSECTION = "subsection" |
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58861
diff
changeset
|
25 |
val SUBSUBSECTION = "subsubsection" |
61463 | 26 |
val PARAGRAPH = "paragraph" |
27 |
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
|
28 |
val TEXT = "text" |
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58928
diff
changeset
|
29 |
val TXT = "txt" |
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58928
diff
changeset
|
30 |
val TEXT_RAW = "text_raw" |
58868
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58861
diff
changeset
|
31 |
|
28495 | 32 |
val THEORY = "theory" |
33 |
val IMPORTS = "imports" |
|
46938
cda018294515
some support for outer syntax keyword declarations within theory header;
wenzelm
parents:
46737
diff
changeset
|
34 |
val KEYWORDS = "keywords" |
cda018294515
some support for outer syntax keyword declarations within theory header;
wenzelm
parents:
46737
diff
changeset
|
35 |
val AND = "and" |
28495 | 36 |
val BEGIN = "begin" |
37 |
||
58928
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58908
diff
changeset
|
38 |
private val bootstrap_header: Keywords = |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58908
diff
changeset
|
39 |
List( |
63429 | 40 |
("%", Keyword.no_spec, None), |
41 |
("(", Keyword.no_spec, None), |
|
42 |
(")", Keyword.no_spec, None), |
|
43 |
(",", Keyword.no_spec, None), |
|
44 |
("::", Keyword.no_spec, None), |
|
45 |
("==", Keyword.no_spec, None), |
|
46 |
(AND, Keyword.no_spec, None), |
|
47 |
(BEGIN, Keyword.no_spec, None), |
|
63448 | 48 |
(IMPORTS, Keyword.quasi_command_spec, None), |
49 |
(KEYWORDS, Keyword.quasi_command_spec, None), |
|
63429 | 50 |
(CHAPTER, (((Keyword.DOCUMENT_HEADING, Nil), Nil)), None), |
51 |
(SECTION, (((Keyword.DOCUMENT_HEADING, Nil), Nil)), None), |
|
52 |
(SUBSECTION, (((Keyword.DOCUMENT_HEADING, Nil), Nil)), None), |
|
53 |
(SUBSUBSECTION, (((Keyword.DOCUMENT_HEADING, Nil), Nil)), None), |
|
54 |
(PARAGRAPH, (((Keyword.DOCUMENT_HEADING, Nil), Nil)), None), |
|
55 |
(SUBPARAGRAPH, (((Keyword.DOCUMENT_HEADING, Nil), Nil)), None), |
|
56 |
(TEXT, (((Keyword.DOCUMENT_BODY, Nil), Nil)), None), |
|
57 |
(TXT, (((Keyword.DOCUMENT_BODY, Nil), Nil)), None), |
|
58 |
(TEXT_RAW, (((Keyword.DOCUMENT_RAW, Nil), Nil)), None), |
|
59 |
(THEORY, ((Keyword.THY_BEGIN, Nil), List("theory")), None), |
|
60 |
("ML", ((Keyword.THY_DECL, Nil), List("ML")), None)) |
|
58928
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58908
diff
changeset
|
61 |
|
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58908
diff
changeset
|
62 |
private val bootstrap_keywords = |
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 |
|
59736
5c1a0069b9d3
tight span for theory header, which is relevant for error positions (including semantic completion);
wenzelm
parents:
59705
diff
changeset
|
65 |
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
|
66 |
Outer_Syntax.init().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 |
|
62895
54c2abe7e9a4
treat ROOT.ML as theory with header "theory ML_Root imports ML_Bootstrap begin";
wenzelm
parents:
62849
diff
changeset
|
69 |
/* file name */ |
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" |
63022 | 73 |
val ML_ROOT = "ML_Root" |
74 |
val ml_roots = List("ROOT0.ML" -> "ML_Root0", "ROOT.ML" -> ML_ROOT) |
|
75 |
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
|
76 |
|
44225
a8f921e6484f
more robust Thy_Header.base_name, with minimal assumptions about path syntax;
wenzelm
parents:
44222
diff
changeset
|
77 |
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
|
78 |
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
|
79 |
|
44225
a8f921e6484f
more robust Thy_Header.base_name, with minimal assumptions about path syntax;
wenzelm
parents:
44222
diff
changeset
|
80 |
def base_name(s: String): String = |
a8f921e6484f
more robust Thy_Header.base_name, with minimal assumptions about path syntax;
wenzelm
parents:
44222
diff
changeset
|
81 |
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
|
82 |
|
44222
9d5ef6cd4ee1
use full .thy file name as node name, which makes MiscUtilities.resolveSymlinks/File.getCanonicalPath more predictable;
wenzelm
parents:
44185
diff
changeset
|
83 |
def thy_name(s: String): Option[String] = |
63022 | 84 |
s match { case Thy_Name(name) => Some(name) case _ => None } |
85 |
||
86 |
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
|
87 |
s match { |
63022 | 88 |
case Thy_Name(name) => |
89 |
Some(bootstrap_thys.collectFirst({ case (a, b) if a == name => b }).getOrElse(name)) |
|
62946 | 90 |
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
|
91 |
case _ => None |
54c2abe7e9a4
treat ROOT.ML as theory with header "theory ML_Root imports ML_Bootstrap begin";
wenzelm
parents:
62849
diff
changeset
|
92 |
} |
38149
3c380380beac
somewhat uniform Thy_Header.split_thy_path in ML and Scala;
wenzelm
parents:
36956
diff
changeset
|
93 |
|
34169 | 94 |
|
95 |
/* header */ |
|
96 |
||
44159 | 97 |
val header: Parser[Thy_Header] = |
34169 | 98 |
{ |
48864
3ee314ae1e0a
added keyword kind "thy_load" (with optional list of file extensions);
wenzelm
parents:
48706
diff
changeset
|
99 |
val opt_files = |
58908 | 100 |
$$$("(") ~! (rep1sep(name, $$$(",")) <~ $$$(")")) ^^ { case _ ~ x => x } | |
48864
3ee314ae1e0a
added keyword kind "thy_load" (with optional list of file extensions);
wenzelm
parents:
48706
diff
changeset
|
101 |
success(Nil) |
59694 | 102 |
|
48864
3ee314ae1e0a
added keyword kind "thy_load" (with optional list of file extensions);
wenzelm
parents:
48706
diff
changeset
|
103 |
val keyword_spec = |
3ee314ae1e0a
added keyword kind "thy_load" (with optional list of file extensions);
wenzelm
parents:
48706
diff
changeset
|
104 |
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
|
105 |
{ case x ~ y ~ z => ((x, y), z) } |
3ee314ae1e0a
added keyword kind "thy_load" (with optional list of file extensions);
wenzelm
parents:
48706
diff
changeset
|
106 |
|
46938
cda018294515
some support for outer syntax keyword declarations within theory header;
wenzelm
parents:
46737
diff
changeset
|
107 |
val keyword_decl = |
50128
599c935aac82
alternative completion for outer syntax keywords;
wenzelm
parents:
48882
diff
changeset
|
108 |
rep1(string) ~ |
58908 | 109 |
opt($$$("::") ~! keyword_spec ^^ { case _ ~ x => x }) ~ |
110 |
opt($$$("==") ~! name ^^ { case _ ~ x => x }) ^^ |
|
63429 | 111 |
{ case xs ~ y ~ z => xs.map((_, y.getOrElse(Keyword.no_spec), z)) } |
59694 | 112 |
|
46939 | 113 |
val keyword_decls = |
58908 | 114 |
keyword_decl ~ rep($$$(AND) ~! keyword_decl ^^ { case _ ~ x => x }) ^^ |
46939 | 115 |
{ case xs ~ yss => (xs :: yss).flatten } |
46938
cda018294515
some support for outer syntax keyword declarations within theory header;
wenzelm
parents:
46737
diff
changeset
|
116 |
|
34169 | 117 |
val args = |
59694 | 118 |
position(theory_name) ~ |
62969 | 119 |
(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
|
120 |
{ case None => Nil case Some(_ ~ xs) => xs }) ~ |
58908 | 121 |
(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
|
122 |
{ case None => Nil case Some(_ ~ xs) => xs }) ~ |
58908 | 123 |
$$$(BEGIN) ^^ |
51294
0850d43cb355
discontinued obsolete header "files" -- these are loaded explicitly after exploring dependencies;
wenzelm
parents:
51293
diff
changeset
|
124 |
{ case x ~ ys ~ zs ~ _ => Thy_Header(x, ys, zs) } |
34169 | 125 |
|
58868
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58861
diff
changeset
|
126 |
val heading = |
62453 | 127 |
(command(CHAPTER) | |
58907 | 128 |
command(SECTION) | |
129 |
command(SUBSECTION) | |
|
58999
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58928
diff
changeset
|
130 |
command(SUBSUBSECTION) | |
61463 | 131 |
command(PARAGRAPH) | |
132 |
command(SUBPARAGRAPH) | |
|
58999
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58928
diff
changeset
|
133 |
command(TEXT) | |
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58928
diff
changeset
|
134 |
command(TXT) | |
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58928
diff
changeset
|
135 |
command(TEXT_RAW)) ~ |
58868
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58861
diff
changeset
|
136 |
tags ~! document_source |
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58861
diff
changeset
|
137 |
|
58907 | 138 |
(rep(heading) ~ command(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x } |
34169 | 139 |
} |
140 |
||
141 |
||
34190 | 142 |
/* read -- lazy scanning */ |
34169 | 143 |
|
59705 | 144 |
def read(reader: Reader[Char], start: Token.Pos): Thy_Header = |
34169 | 145 |
{ |
58928
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58908
diff
changeset
|
146 |
val token = Token.Parsers.token(bootstrap_keywords) |
36956
21be4832c362
renamed class Outer_Lex to Token and Token_Kind to Token.Kind;
wenzelm
parents:
36948
diff
changeset
|
147 |
val toks = new mutable.ListBuffer[Token] |
34188
fbfc18be1f8c
scan: operate on file (via Scan.byte_reader), more robust exception handling;
wenzelm
parents:
34169
diff
changeset
|
148 |
|
43611 | 149 |
@tailrec def scan_to_begin(in: Reader[Char]) |
34169 | 150 |
{ |
151 |
token(in) match { |
|
55494 | 152 |
case Token.Parsers.Success(tok, rest) => |
34169 | 153 |
toks += tok |
43611 | 154 |
if (!tok.is_begin) scan_to_begin(rest) |
34169 | 155 |
case _ => |
156 |
} |
|
157 |
} |
|
43611 | 158 |
scan_to_begin(reader) |
34190 | 159 |
|
59705 | 160 |
parse(commit(header), Token.reader(toks.toList, start)) match { |
34190 | 161 |
case Success(result, _) => result |
162 |
case bad => error(bad.toString) |
|
163 |
} |
|
34169 | 164 |
} |
43611 | 165 |
|
59705 | 166 |
def read(source: CharSequence, start: Token.Pos): Thy_Header = |
167 |
read(new CharSequenceReader(source), start) |
|
34169 | 168 |
} |
44159 | 169 |
|
170 |
||
44185 | 171 |
sealed case class Thy_Header( |
59694 | 172 |
name: (String, Position.T), |
173 |
imports: List[(String, Position.T)], |
|
51294
0850d43cb355
discontinued obsolete header "files" -- these are loaded explicitly after exploring dependencies;
wenzelm
parents:
51293
diff
changeset
|
174 |
keywords: Thy_Header.Keywords) |
44159 | 175 |
{ |
56823
37be55461dbe
more frugal access to theory text via Reader, reduced costs for I/O text decoding;
wenzelm
parents:
56801
diff
changeset
|
176 |
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
|
177 |
{ |
37be55461dbe
more frugal access to theory text via Reader, reduced costs for I/O text decoding;
wenzelm
parents:
56801
diff
changeset
|
178 |
val f = Symbol.decode _ |
59694 | 179 |
Thy_Header((f(name._1), name._2), imports.map({ case (a, b) => (f(a), b) }), |
63429 | 180 |
keywords.map({ case (a, ((x, y), z), c) => |
181 |
(f(a), ((f(x), y.map(f)), z.map(f)), c.map(f)) })) |
|
56823
37be55461dbe
more frugal access to theory text via Reader, reduced costs for I/O text decoding;
wenzelm
parents:
56801
diff
changeset
|
182 |
} |
44159 | 183 |
} |