author | wenzelm |
Wed, 05 Nov 2014 22:17:05 +0100 | |
changeset 58908 | 58bedbc18915 |
parent 58907 | 0ee3563803c9 |
child 58928 | 23d0ffd48006 |
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 |
{ |
28495 | 18 |
val HEADER = "header" |
58868
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58861
diff
changeset
|
19 |
val CHAPTER = "chapter" |
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58861
diff
changeset
|
20 |
val SECTION = "section" |
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58861
diff
changeset
|
21 |
val SUBSECTION = "subsection" |
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58861
diff
changeset
|
22 |
val SUBSUBSECTION = "subsubsection" |
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58861
diff
changeset
|
23 |
|
28495 | 24 |
val THEORY = "theory" |
25 |
val IMPORTS = "imports" |
|
46938
cda018294515
some support for outer syntax keyword declarations within theory header;
wenzelm
parents:
46737
diff
changeset
|
26 |
val KEYWORDS = "keywords" |
cda018294515
some support for outer syntax keyword declarations within theory header;
wenzelm
parents:
46737
diff
changeset
|
27 |
val AND = "and" |
28495 | 28 |
val BEGIN = "begin" |
29 |
||
58903 | 30 |
private val header_keywords = |
58907 | 31 |
Keyword.Keywords.empty + |
32 |
"%" + "(" + ")" + "," + "::" + "==" + AND + BEGIN + IMPORTS + KEYWORDS + |
|
33 |
(HEADER, Keyword.HEADING) + |
|
34 |
(CHAPTER, Keyword.HEADING) + |
|
35 |
(SECTION, Keyword.HEADING) + |
|
36 |
(SUBSECTION, Keyword.HEADING) + |
|
37 |
(SUBSUBSECTION, Keyword.HEADING) + |
|
38 |
(THEORY, Keyword.THY_BEGIN) |
|
34190 | 39 |
|
38149
3c380380beac
somewhat uniform Thy_Header.split_thy_path in ML and Scala;
wenzelm
parents:
36956
diff
changeset
|
40 |
|
44160
8848867501fb
clarified document model header: master_dir (native wrt. editor, potentially URL) and node_name (full canonical path);
wenzelm
parents:
44159
diff
changeset
|
41 |
/* theory file name */ |
8848867501fb
clarified document model header: master_dir (native wrt. editor, potentially URL) and node_name (full canonical path);
wenzelm
parents:
44159
diff
changeset
|
42 |
|
44225
a8f921e6484f
more robust Thy_Header.base_name, with minimal assumptions about path syntax;
wenzelm
parents:
44222
diff
changeset
|
43 |
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
|
44 |
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
|
45 |
|
44225
a8f921e6484f
more robust Thy_Header.base_name, with minimal assumptions about path syntax;
wenzelm
parents:
44222
diff
changeset
|
46 |
def base_name(s: String): String = |
a8f921e6484f
more robust Thy_Header.base_name, with minimal assumptions about path syntax;
wenzelm
parents:
44222
diff
changeset
|
47 |
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
|
48 |
|
44222
9d5ef6cd4ee1
use full .thy file name as node name, which makes MiscUtilities.resolveSymlinks/File.getCanonicalPath more predictable;
wenzelm
parents:
44185
diff
changeset
|
49 |
def thy_name(s: String): Option[String] = |
9d5ef6cd4ee1
use full .thy file name as node name, which makes MiscUtilities.resolveSymlinks/File.getCanonicalPath more predictable;
wenzelm
parents:
44185
diff
changeset
|
50 |
s match { case Thy_Name(name) => Some(name) case _ => None } |
38149
3c380380beac
somewhat uniform Thy_Header.split_thy_path in ML and Scala;
wenzelm
parents:
36956
diff
changeset
|
51 |
|
34169 | 52 |
|
53 |
/* header */ |
|
54 |
||
44159 | 55 |
val header: Parser[Thy_Header] = |
34169 | 56 |
{ |
43611 | 57 |
val file_name = atom("file name", _.is_name) |
34169 | 58 |
|
48864
3ee314ae1e0a
added keyword kind "thy_load" (with optional list of file extensions);
wenzelm
parents:
48706
diff
changeset
|
59 |
val opt_files = |
58908 | 60 |
$$$("(") ~! (rep1sep(name, $$$(",")) <~ $$$(")")) ^^ { case _ ~ x => x } | |
48864
3ee314ae1e0a
added keyword kind "thy_load" (with optional list of file extensions);
wenzelm
parents:
48706
diff
changeset
|
61 |
success(Nil) |
3ee314ae1e0a
added keyword kind "thy_load" (with optional list of file extensions);
wenzelm
parents:
48706
diff
changeset
|
62 |
val keyword_spec = |
3ee314ae1e0a
added keyword kind "thy_load" (with optional list of file extensions);
wenzelm
parents:
48706
diff
changeset
|
63 |
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
|
64 |
{ case x ~ y ~ z => ((x, y), z) } |
3ee314ae1e0a
added keyword kind "thy_load" (with optional list of file extensions);
wenzelm
parents:
48706
diff
changeset
|
65 |
|
46938
cda018294515
some support for outer syntax keyword declarations within theory header;
wenzelm
parents:
46737
diff
changeset
|
66 |
val keyword_decl = |
50128
599c935aac82
alternative completion for outer syntax keywords;
wenzelm
parents:
48882
diff
changeset
|
67 |
rep1(string) ~ |
58908 | 68 |
opt($$$("::") ~! keyword_spec ^^ { case _ ~ x => x }) ~ |
69 |
opt($$$("==") ~! name ^^ { case _ ~ x => x }) ^^ |
|
50128
599c935aac82
alternative completion for outer syntax keywords;
wenzelm
parents:
48882
diff
changeset
|
70 |
{ case xs ~ y ~ z => xs.map((_, y, z)) } |
46939 | 71 |
val keyword_decls = |
58908 | 72 |
keyword_decl ~ rep($$$(AND) ~! keyword_decl ^^ { case _ ~ x => x }) ^^ |
46939 | 73 |
{ case xs ~ yss => (xs :: yss).flatten } |
46938
cda018294515
some support for outer syntax keyword declarations within theory header;
wenzelm
parents:
46737
diff
changeset
|
74 |
|
34169 | 75 |
val file = |
58908 | 76 |
$$$("(") ~! (file_name ~ $$$(")")) ^^ { case _ ~ (x ~ _) => (x, false) } | |
44185 | 77 |
file_name ^^ (x => (x, true)) |
34169 | 78 |
|
79 |
val args = |
|
46938
cda018294515
some support for outer syntax keyword declarations within theory header;
wenzelm
parents:
46737
diff
changeset
|
80 |
theory_name ~ |
58908 | 81 |
(opt($$$(IMPORTS) ~! (rep1(theory_xname))) ^^ |
56823
37be55461dbe
more frugal access to theory text via Reader, reduced costs for I/O text decoding;
wenzelm
parents:
56801
diff
changeset
|
82 |
{ case None => Nil case Some(_ ~ xs) => xs }) ~ |
58908 | 83 |
(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
|
84 |
{ case None => Nil case Some(_ ~ xs) => xs }) ~ |
58908 | 85 |
$$$(BEGIN) ^^ |
51294
0850d43cb355
discontinued obsolete header "files" -- these are loaded explicitly after exploring dependencies;
wenzelm
parents:
51293
diff
changeset
|
86 |
{ case x ~ ys ~ zs ~ _ => Thy_Header(x, ys, zs) } |
34169 | 87 |
|
58868
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58861
diff
changeset
|
88 |
val heading = |
58907 | 89 |
(command(HEADER) | |
90 |
command(CHAPTER) | |
|
91 |
command(SECTION) | |
|
92 |
command(SUBSECTION) | |
|
93 |
command(SUBSUBSECTION)) ~ |
|
58868
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58861
diff
changeset
|
94 |
tags ~! document_source |
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58861
diff
changeset
|
95 |
|
58907 | 96 |
(rep(heading) ~ command(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x } |
34169 | 97 |
} |
98 |
||
99 |
||
34190 | 100 |
/* read -- lazy scanning */ |
34169 | 101 |
|
44159 | 102 |
def read(reader: Reader[Char]): Thy_Header = |
34169 | 103 |
{ |
58903 | 104 |
val token = Token.Parsers.token(header_keywords) |
36956
21be4832c362
renamed class Outer_Lex to Token and Token_Kind to Token.Kind;
wenzelm
parents:
36948
diff
changeset
|
105 |
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
|
106 |
|
43611 | 107 |
@tailrec def scan_to_begin(in: Reader[Char]) |
34169 | 108 |
{ |
109 |
token(in) match { |
|
55494 | 110 |
case Token.Parsers.Success(tok, rest) => |
34169 | 111 |
toks += tok |
43611 | 112 |
if (!tok.is_begin) scan_to_begin(rest) |
34169 | 113 |
case _ => |
114 |
} |
|
115 |
} |
|
43611 | 116 |
scan_to_begin(reader) |
34190 | 117 |
|
36956
21be4832c362
renamed class Outer_Lex to Token and Token_Kind to Token.Kind;
wenzelm
parents:
36948
diff
changeset
|
118 |
parse(commit(header), Token.reader(toks.toList)) match { |
34190 | 119 |
case Success(result, _) => result |
120 |
case bad => error(bad.toString) |
|
121 |
} |
|
34169 | 122 |
} |
43611 | 123 |
|
44159 | 124 |
def read(source: CharSequence): Thy_Header = |
43646 | 125 |
read(new CharSequenceReader(source)) |
126 |
||
48706 | 127 |
|
128 |
/* keywords */ |
|
129 |
||
58900 | 130 |
type Keywords = List[(String, Option[Keyword.Spec], Option[String])] |
34169 | 131 |
} |
44159 | 132 |
|
133 |
||
44185 | 134 |
sealed case class Thy_Header( |
50128
599c935aac82
alternative completion for outer syntax keywords;
wenzelm
parents:
48882
diff
changeset
|
135 |
name: String, |
599c935aac82
alternative completion for outer syntax keywords;
wenzelm
parents:
48882
diff
changeset
|
136 |
imports: List[String], |
51294
0850d43cb355
discontinued obsolete header "files" -- these are loaded explicitly after exploring dependencies;
wenzelm
parents:
51293
diff
changeset
|
137 |
keywords: Thy_Header.Keywords) |
44159 | 138 |
{ |
139 |
def map(f: String => String): Thy_Header = |
|
51294
0850d43cb355
discontinued obsolete header "files" -- these are loaded explicitly after exploring dependencies;
wenzelm
parents:
51293
diff
changeset
|
140 |
Thy_Header(f(name), imports.map(f), keywords) |
56823
37be55461dbe
more frugal access to theory text via Reader, reduced costs for I/O text decoding;
wenzelm
parents:
56801
diff
changeset
|
141 |
|
37be55461dbe
more frugal access to theory text via Reader, reduced costs for I/O text decoding;
wenzelm
parents:
56801
diff
changeset
|
142 |
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
|
143 |
{ |
37be55461dbe
more frugal access to theory text via Reader, reduced costs for I/O text decoding;
wenzelm
parents:
56801
diff
changeset
|
144 |
val f = Symbol.decode _ |
37be55461dbe
more frugal access to theory text via Reader, reduced costs for I/O text decoding;
wenzelm
parents:
56801
diff
changeset
|
145 |
Thy_Header(f(name), imports.map(f), |
37be55461dbe
more frugal access to theory text via Reader, reduced costs for I/O text decoding;
wenzelm
parents:
56801
diff
changeset
|
146 |
keywords.map({ case (a, b, c) => |
58899 | 147 |
(f(a), b.map({ case ((x, y), z) => ((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
|
148 |
} |
44159 | 149 |
} |
150 |