author | wenzelm |
Wed, 01 Aug 2012 19:53:20 +0200 | |
changeset 48638 | 22d65e375c01 |
parent 48484 | 70898d016538 |
child 48706 | e2b512024eab |
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 |
|
48409 | 15 |
import java.io.{File => JFile} |
34188
fbfc18be1f8c
scan: operate on file (via Scan.byte_reader), more robust exception handling;
wenzelm
parents:
34169
diff
changeset
|
16 |
|
34169 | 17 |
|
43661
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43652
diff
changeset
|
18 |
object Thy_Header extends Parse.Parser |
32450 | 19 |
{ |
28495 | 20 |
val HEADER = "header" |
21 |
val THEORY = "theory" |
|
22 |
val IMPORTS = "imports" |
|
46938
cda018294515
some support for outer syntax keyword declarations within theory header;
wenzelm
parents:
46737
diff
changeset
|
23 |
val KEYWORDS = "keywords" |
cda018294515
some support for outer syntax keyword declarations within theory header;
wenzelm
parents:
46737
diff
changeset
|
24 |
val AND = "and" |
28495 | 25 |
val USES = "uses" |
26 |
val BEGIN = "begin" |
|
27 |
||
46938
cda018294515
some support for outer syntax keyword declarations within theory header;
wenzelm
parents:
46737
diff
changeset
|
28 |
private val lexicon = |
cda018294515
some support for outer syntax keyword declarations within theory header;
wenzelm
parents:
46737
diff
changeset
|
29 |
Scan.Lexicon("%", "(", ")", "::", ";", AND, BEGIN, HEADER, IMPORTS, KEYWORDS, THEORY, USES) |
34190 | 30 |
|
38149
3c380380beac
somewhat uniform Thy_Header.split_thy_path in ML and Scala;
wenzelm
parents:
36956
diff
changeset
|
31 |
|
44160
8848867501fb
clarified document model header: master_dir (native wrt. editor, potentially URL) and node_name (full canonical path);
wenzelm
parents:
44159
diff
changeset
|
32 |
/* 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
|
33 |
|
44225
a8f921e6484f
more robust Thy_Header.base_name, with minimal assumptions about path syntax;
wenzelm
parents:
44222
diff
changeset
|
34 |
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
|
35 |
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
|
36 |
|
44225
a8f921e6484f
more robust Thy_Header.base_name, with minimal assumptions about path syntax;
wenzelm
parents:
44222
diff
changeset
|
37 |
def base_name(s: String): String = |
a8f921e6484f
more robust Thy_Header.base_name, with minimal assumptions about path syntax;
wenzelm
parents:
44222
diff
changeset
|
38 |
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
|
39 |
|
44222
9d5ef6cd4ee1
use full .thy file name as node name, which makes MiscUtilities.resolveSymlinks/File.getCanonicalPath more predictable;
wenzelm
parents:
44185
diff
changeset
|
40 |
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
|
41 |
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
|
42 |
|
34169 | 43 |
|
44 |
/* header */ |
|
45 |
||
44159 | 46 |
val header: Parser[Thy_Header] = |
34169 | 47 |
{ |
43611 | 48 |
val file_name = atom("file name", _.is_name) |
34169 | 49 |
|
46938
cda018294515
some support for outer syntax keyword declarations within theory header;
wenzelm
parents:
46737
diff
changeset
|
50 |
val keyword_kind = |
cda018294515
some support for outer syntax keyword declarations within theory header;
wenzelm
parents:
46737
diff
changeset
|
51 |
atom("outer syntax keyword kind", _.is_name) ~ tags ^^ { case x ~ y => (x, y) } |
cda018294515
some support for outer syntax keyword declarations within theory header;
wenzelm
parents:
46737
diff
changeset
|
52 |
val keyword_decl = |
46943 | 53 |
rep1(string) ~ opt(keyword("::") ~! keyword_kind ^^ { case _ ~ x => x }) ^^ |
46939 | 54 |
{ case xs ~ y => xs.map((_, y)) } |
55 |
val keyword_decls = |
|
46938
cda018294515
some support for outer syntax keyword declarations within theory header;
wenzelm
parents:
46737
diff
changeset
|
56 |
keyword_decl ~ rep(keyword(AND) ~! keyword_decl ^^ { case _ ~ x => x }) ^^ |
46939 | 57 |
{ case xs ~ yss => (xs :: yss).flatten } |
46938
cda018294515
some support for outer syntax keyword declarations within theory header;
wenzelm
parents:
46737
diff
changeset
|
58 |
|
34169 | 59 |
val file = |
44185 | 60 |
keyword("(") ~! (file_name ~ keyword(")")) ^^ { case _ ~ (x ~ _) => (x, false) } | |
61 |
file_name ^^ (x => (x, true)) |
|
34169 | 62 |
|
63 |
val args = |
|
46938
cda018294515
some support for outer syntax keyword declarations within theory header;
wenzelm
parents:
46737
diff
changeset
|
64 |
theory_name ~ |
48638 | 65 |
(opt(keyword(IMPORTS) ~! (rep1(theory_name))) ^^ { case None => Nil case Some(_ ~ xs) => xs }) ~ |
46939 | 66 |
(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
|
67 |
(opt(keyword(USES) ~! (rep1(file))) ^^ { case None => Nil case Some(_ ~ xs) => xs }) ~ |
cda018294515
some support for outer syntax keyword declarations within theory header;
wenzelm
parents:
46737
diff
changeset
|
68 |
keyword(BEGIN) ^^ |
cda018294515
some support for outer syntax keyword declarations within theory header;
wenzelm
parents:
46737
diff
changeset
|
69 |
{ case x ~ ys ~ zs ~ ws ~ _ => Thy_Header(x, ys, zs, ws) } |
34169 | 70 |
|
71 |
(keyword(HEADER) ~ tags) ~! |
|
72 |
((doc_source ~ rep(keyword(";")) ~ keyword(THEORY) ~ tags) ~> args) ^^ { case _ ~ x => x } | |
|
73 |
(keyword(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x } |
|
74 |
} |
|
75 |
||
76 |
||
34190 | 77 |
/* read -- lazy scanning */ |
34169 | 78 |
|
44159 | 79 |
def read(reader: Reader[Char]): Thy_Header = |
34169 | 80 |
{ |
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43672
diff
changeset
|
81 |
val token = lexicon.token(_ => false) |
36956
21be4832c362
renamed class Outer_Lex to Token and Token_Kind to Token.Kind;
wenzelm
parents:
36948
diff
changeset
|
82 |
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
|
83 |
|
43611 | 84 |
@tailrec def scan_to_begin(in: Reader[Char]) |
34169 | 85 |
{ |
86 |
token(in) match { |
|
87 |
case lexicon.Success(tok, rest) => |
|
88 |
toks += tok |
|
43611 | 89 |
if (!tok.is_begin) scan_to_begin(rest) |
34169 | 90 |
case _ => |
91 |
} |
|
92 |
} |
|
43611 | 93 |
scan_to_begin(reader) |
34190 | 94 |
|
36956
21be4832c362
renamed class Outer_Lex to Token and Token_Kind to Token.Kind;
wenzelm
parents:
36948
diff
changeset
|
95 |
parse(commit(header), Token.reader(toks.toList)) match { |
34190 | 96 |
case Success(result, _) => result |
97 |
case bad => error(bad.toString) |
|
98 |
} |
|
34169 | 99 |
} |
43611 | 100 |
|
44159 | 101 |
def read(source: CharSequence): Thy_Header = |
43646 | 102 |
read(new CharSequenceReader(source)) |
103 |
||
48409 | 104 |
def read(file: JFile): Thy_Header = |
43611 | 105 |
{ |
106 |
val reader = Scan.byte_reader(file) |
|
43699 | 107 |
try { read(reader).map(Standard_System.decode_permissive_utf8) } |
43611 | 108 |
finally { reader.close } |
109 |
} |
|
34169 | 110 |
} |
44159 | 111 |
|
112 |
||
44185 | 113 |
sealed case class Thy_Header( |
46938
cda018294515
some support for outer syntax keyword declarations within theory header;
wenzelm
parents:
46737
diff
changeset
|
114 |
name: String, imports: List[String], |
46940 | 115 |
keywords: List[Outer_Syntax.Decl], |
46938
cda018294515
some support for outer syntax keyword declarations within theory header;
wenzelm
parents:
46737
diff
changeset
|
116 |
uses: List[(String, Boolean)]) |
44159 | 117 |
{ |
118 |
def map(f: String => String): Thy_Header = |
|
46938
cda018294515
some support for outer syntax keyword declarations within theory header;
wenzelm
parents:
46737
diff
changeset
|
119 |
Thy_Header(f(name), imports.map(f), keywords, uses.map(p => (f(p._1), p._2))) |
44159 | 120 |
} |
121 |