| author | wenzelm |
| Thu, 28 Mar 2013 14:01:56 +0100 | |
| changeset 51567 | a86c5e02ba58 |
| parent 51294 | 0850d43cb355 |
| child 51627 | 589daaf48dba |
| 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" |
19 |
val THEORY = "theory" |
|
20 |
val IMPORTS = "imports" |
|
|
46938
cda018294515
some support for outer syntax keyword declarations within theory header;
wenzelm
parents:
46737
diff
changeset
|
21 |
val KEYWORDS = "keywords" |
|
cda018294515
some support for outer syntax keyword declarations within theory header;
wenzelm
parents:
46737
diff
changeset
|
22 |
val AND = "and" |
| 28495 | 23 |
val BEGIN = "begin" |
24 |
||
|
46938
cda018294515
some support for outer syntax keyword declarations within theory header;
wenzelm
parents:
46737
diff
changeset
|
25 |
private val lexicon = |
|
50128
599c935aac82
alternative completion for outer syntax keywords;
wenzelm
parents:
48882
diff
changeset
|
26 |
Scan.Lexicon("%", "(", ")", ",", "::", ";", "==",
|
|
51293
05b1bbae748d
discontinued obsolete 'uses' within theory header;
wenzelm
parents:
50128
diff
changeset
|
27 |
AND, BEGIN, HEADER, IMPORTS, KEYWORDS, THEORY) |
| 34190 | 28 |
|
|
38149
3c380380beac
somewhat uniform Thy_Header.split_thy_path in ML and Scala;
wenzelm
parents:
36956
diff
changeset
|
29 |
|
|
44160
8848867501fb
clarified document model header: master_dir (native wrt. editor, potentially URL) and node_name (full canonical path);
wenzelm
parents:
44159
diff
changeset
|
30 |
/* 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
|
31 |
|
|
44225
a8f921e6484f
more robust Thy_Header.base_name, with minimal assumptions about path syntax;
wenzelm
parents:
44222
diff
changeset
|
32 |
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
|
33 |
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
|
34 |
|
|
44225
a8f921e6484f
more robust Thy_Header.base_name, with minimal assumptions about path syntax;
wenzelm
parents:
44222
diff
changeset
|
35 |
def base_name(s: String): String = |
|
a8f921e6484f
more robust Thy_Header.base_name, with minimal assumptions about path syntax;
wenzelm
parents:
44222
diff
changeset
|
36 |
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
|
37 |
|
|
44222
9d5ef6cd4ee1
use full .thy file name as node name, which makes MiscUtilities.resolveSymlinks/File.getCanonicalPath more predictable;
wenzelm
parents:
44185
diff
changeset
|
38 |
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
|
39 |
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
|
40 |
|
| 34169 | 41 |
|
42 |
/* header */ |
|
43 |
||
| 44159 | 44 |
val header: Parser[Thy_Header] = |
| 34169 | 45 |
{
|
| 43611 | 46 |
val file_name = atom("file name", _.is_name)
|
| 34169 | 47 |
|
|
48864
3ee314ae1e0a
added keyword kind "thy_load" (with optional list of file extensions);
wenzelm
parents:
48706
diff
changeset
|
48 |
val opt_files = |
|
3ee314ae1e0a
added keyword kind "thy_load" (with optional list of file extensions);
wenzelm
parents:
48706
diff
changeset
|
49 |
keyword("(") ~! (rep1sep(name, keyword(",")) <~ keyword(")")) ^^ { case _ ~ x => x } |
|
|
3ee314ae1e0a
added keyword kind "thy_load" (with optional list of file extensions);
wenzelm
parents:
48706
diff
changeset
|
50 |
success(Nil) |
|
3ee314ae1e0a
added keyword kind "thy_load" (with optional list of file extensions);
wenzelm
parents:
48706
diff
changeset
|
51 |
val keyword_spec = |
|
3ee314ae1e0a
added keyword kind "thy_load" (with optional list of file extensions);
wenzelm
parents:
48706
diff
changeset
|
52 |
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
|
53 |
{ case x ~ y ~ z => ((x, y), z) }
|
|
3ee314ae1e0a
added keyword kind "thy_load" (with optional list of file extensions);
wenzelm
parents:
48706
diff
changeset
|
54 |
|
|
46938
cda018294515
some support for outer syntax keyword declarations within theory header;
wenzelm
parents:
46737
diff
changeset
|
55 |
val keyword_decl = |
|
50128
599c935aac82
alternative completion for outer syntax keywords;
wenzelm
parents:
48882
diff
changeset
|
56 |
rep1(string) ~ |
|
599c935aac82
alternative completion for outer syntax keywords;
wenzelm
parents:
48882
diff
changeset
|
57 |
opt(keyword("::") ~! keyword_spec ^^ { case _ ~ x => x }) ~
|
|
599c935aac82
alternative completion for outer syntax keywords;
wenzelm
parents:
48882
diff
changeset
|
58 |
opt(keyword("==") ~! name ^^ { case _ ~ x => x }) ^^
|
|
599c935aac82
alternative completion for outer syntax keywords;
wenzelm
parents:
48882
diff
changeset
|
59 |
{ case xs ~ y ~ z => xs.map((_, y, z)) }
|
| 46939 | 60 |
val keyword_decls = |
|
46938
cda018294515
some support for outer syntax keyword declarations within theory header;
wenzelm
parents:
46737
diff
changeset
|
61 |
keyword_decl ~ rep(keyword(AND) ~! keyword_decl ^^ { case _ ~ x => x }) ^^
|
| 46939 | 62 |
{ case xs ~ yss => (xs :: yss).flatten }
|
|
46938
cda018294515
some support for outer syntax keyword declarations within theory header;
wenzelm
parents:
46737
diff
changeset
|
63 |
|
| 34169 | 64 |
val file = |
| 44185 | 65 |
keyword("(") ~! (file_name ~ keyword(")")) ^^ { case _ ~ (x ~ _) => (x, false) } |
|
66 |
file_name ^^ (x => (x, true)) |
|
| 34169 | 67 |
|
68 |
val args = |
|
|
46938
cda018294515
some support for outer syntax keyword declarations within theory header;
wenzelm
parents:
46737
diff
changeset
|
69 |
theory_name ~ |
| 48638 | 70 |
(opt(keyword(IMPORTS) ~! (rep1(theory_name))) ^^ { case None => Nil case Some(_ ~ xs) => xs }) ~
|
| 46939 | 71 |
(opt(keyword(KEYWORDS) ~! keyword_decls) ^^ { case None => Nil case Some(_ ~ xs) => xs }) ~
|
|
46938
cda018294515
some support for outer syntax keyword declarations within theory header;
wenzelm
parents:
46737
diff
changeset
|
72 |
keyword(BEGIN) ^^ |
|
51294
0850d43cb355
discontinued obsolete header "files" -- these are loaded explicitly after exploring dependencies;
wenzelm
parents:
51293
diff
changeset
|
73 |
{ case x ~ ys ~ zs ~ _ => Thy_Header(x, ys, zs) }
|
| 34169 | 74 |
|
75 |
(keyword(HEADER) ~ tags) ~! |
|
76 |
((doc_source ~ rep(keyword(";")) ~ keyword(THEORY) ~ tags) ~> args) ^^ { case _ ~ x => x } |
|
|
77 |
(keyword(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x }
|
|
78 |
} |
|
79 |
||
80 |
||
| 34190 | 81 |
/* read -- lazy scanning */ |
| 34169 | 82 |
|
| 44159 | 83 |
def read(reader: Reader[Char]): Thy_Header = |
| 34169 | 84 |
{
|
|
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43672
diff
changeset
|
85 |
val token = lexicon.token(_ => false) |
|
36956
21be4832c362
renamed class Outer_Lex to Token and Token_Kind to Token.Kind;
wenzelm
parents:
36948
diff
changeset
|
86 |
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
|
87 |
|
| 43611 | 88 |
@tailrec def scan_to_begin(in: Reader[Char]) |
| 34169 | 89 |
{
|
90 |
token(in) match {
|
|
91 |
case lexicon.Success(tok, rest) => |
|
92 |
toks += tok |
|
| 43611 | 93 |
if (!tok.is_begin) scan_to_begin(rest) |
| 34169 | 94 |
case _ => |
95 |
} |
|
96 |
} |
|
| 43611 | 97 |
scan_to_begin(reader) |
| 34190 | 98 |
|
|
36956
21be4832c362
renamed class Outer_Lex to Token and Token_Kind to Token.Kind;
wenzelm
parents:
36948
diff
changeset
|
99 |
parse(commit(header), Token.reader(toks.toList)) match {
|
| 34190 | 100 |
case Success(result, _) => result |
101 |
case bad => error(bad.toString) |
|
102 |
} |
|
| 34169 | 103 |
} |
| 43611 | 104 |
|
| 44159 | 105 |
def read(source: CharSequence): Thy_Header = |
| 43646 | 106 |
read(new CharSequenceReader(source)) |
107 |
||
| 48706 | 108 |
|
109 |
/* keywords */ |
|
110 |
||
|
50128
599c935aac82
alternative completion for outer syntax keywords;
wenzelm
parents:
48882
diff
changeset
|
111 |
type Keywords = List[(String, Option[((String, List[String]), List[String])], Option[String])] |
| 34169 | 112 |
} |
| 44159 | 113 |
|
114 |
||
| 44185 | 115 |
sealed case class Thy_Header( |
|
50128
599c935aac82
alternative completion for outer syntax keywords;
wenzelm
parents:
48882
diff
changeset
|
116 |
name: String, |
|
599c935aac82
alternative completion for outer syntax keywords;
wenzelm
parents:
48882
diff
changeset
|
117 |
imports: List[String], |
|
51294
0850d43cb355
discontinued obsolete header "files" -- these are loaded explicitly after exploring dependencies;
wenzelm
parents:
51293
diff
changeset
|
118 |
keywords: Thy_Header.Keywords) |
| 44159 | 119 |
{
|
120 |
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
|
121 |
Thy_Header(f(name), imports.map(f), keywords) |
| 44159 | 122 |
} |
123 |