author | wenzelm |
Fri, 12 Aug 2011 22:10:49 +0200 | |
changeset 44163 | 32e0c150c010 |
parent 44160 | 8848867501fb |
child 44185 | 05641edb5d30 |
permissions | -rw-r--r-- |
28495 | 1 |
/* Title: Pure/Thy/thy_header.scala |
2 |
Author: Makarius |
|
3 |
||
34169 | 4 |
Theory headers -- independent of outer syntax. |
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 |
|
34188
fbfc18be1f8c
scan: operate on file (via Scan.byte_reader), more robust exception handling;
wenzelm
parents:
34169
diff
changeset
|
15 |
import java.io.File |
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" |
|
23 |
val USES = "uses" |
|
24 |
val BEGIN = "begin" |
|
25 |
||
34169 | 26 |
val lexicon = Scan.Lexicon("%", "(", ")", ";", BEGIN, HEADER, IMPORTS, THEORY, USES) |
34190 | 27 |
|
38149
3c380380beac
somewhat uniform Thy_Header.split_thy_path in ML and Scala;
wenzelm
parents:
36956
diff
changeset
|
28 |
|
44160
8848867501fb
clarified document model header: master_dir (native wrt. editor, potentially URL) and node_name (full canonical path);
wenzelm
parents:
44159
diff
changeset
|
29 |
/* 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
|
30 |
|
8848867501fb
clarified document model header: master_dir (native wrt. editor, potentially URL) and node_name (full canonical path);
wenzelm
parents:
44159
diff
changeset
|
31 |
private val Thy_Name = new Regex(""".*?([^/\\:]+)\.thy""") |
8848867501fb
clarified document model header: master_dir (native wrt. editor, potentially URL) and node_name (full canonical path);
wenzelm
parents:
44159
diff
changeset
|
32 |
|
8848867501fb
clarified document model header: master_dir (native wrt. editor, potentially URL) and node_name (full canonical path);
wenzelm
parents:
44159
diff
changeset
|
33 |
def thy_name(s: String): Option[String] = |
8848867501fb
clarified document model header: master_dir (native wrt. editor, potentially URL) and node_name (full canonical path);
wenzelm
parents:
44159
diff
changeset
|
34 |
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
|
35 |
|
43648 | 36 |
def thy_path(name: String): Path = Path.basic(name).ext("thy") |
37 |
||
34169 | 38 |
|
39 |
/* header */ |
|
40 |
||
44159 | 41 |
val header: Parser[Thy_Header] = |
34169 | 42 |
{ |
43611 | 43 |
val file_name = atom("file name", _.is_name) |
44 |
val theory_name = atom("theory name", _.is_name) |
|
34169 | 45 |
|
46 |
val file = |
|
47 |
keyword("(") ~! (file_name ~ keyword(")")) ^^ { case _ ~ (x ~ _) => x } | file_name |
|
48 |
||
49 |
val uses = opt(keyword(USES) ~! (rep1(file))) ^^ { case None => Nil case Some(_ ~ xs) => xs } |
|
50 |
||
51 |
val args = |
|
52 |
theory_name ~ (keyword(IMPORTS) ~! (rep1(theory_name) ~ uses ~ keyword(BEGIN))) ^^ |
|
44159 | 53 |
{ case x ~ (_ ~ (ys ~ zs ~ _)) => Thy_Header(x, ys, zs) } |
34169 | 54 |
|
55 |
(keyword(HEADER) ~ tags) ~! |
|
56 |
((doc_source ~ rep(keyword(";")) ~ keyword(THEORY) ~ tags) ~> args) ^^ { case _ ~ x => x } | |
|
57 |
(keyword(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x } |
|
58 |
} |
|
59 |
||
60 |
||
34190 | 61 |
/* read -- lazy scanning */ |
34169 | 62 |
|
44159 | 63 |
def read(reader: Reader[Char]): Thy_Header = |
34169 | 64 |
{ |
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43672
diff
changeset
|
65 |
val token = lexicon.token(_ => false) |
36956
21be4832c362
renamed class Outer_Lex to Token and Token_Kind to Token.Kind;
wenzelm
parents:
36948
diff
changeset
|
66 |
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
|
67 |
|
43611 | 68 |
@tailrec def scan_to_begin(in: Reader[Char]) |
34169 | 69 |
{ |
70 |
token(in) match { |
|
71 |
case lexicon.Success(tok, rest) => |
|
72 |
toks += tok |
|
43611 | 73 |
if (!tok.is_begin) scan_to_begin(rest) |
34169 | 74 |
case _ => |
75 |
} |
|
76 |
} |
|
43611 | 77 |
scan_to_begin(reader) |
34190 | 78 |
|
36956
21be4832c362
renamed class Outer_Lex to Token and Token_Kind to Token.Kind;
wenzelm
parents:
36948
diff
changeset
|
79 |
parse(commit(header), Token.reader(toks.toList)) match { |
34190 | 80 |
case Success(result, _) => result |
81 |
case bad => error(bad.toString) |
|
82 |
} |
|
34169 | 83 |
} |
43611 | 84 |
|
44159 | 85 |
def read(source: CharSequence): Thy_Header = |
43646 | 86 |
read(new CharSequenceReader(source)) |
87 |
||
44159 | 88 |
def read(file: File): Thy_Header = |
43611 | 89 |
{ |
90 |
val reader = Scan.byte_reader(file) |
|
43699 | 91 |
try { read(reader).map(Standard_System.decode_permissive_utf8) } |
43611 | 92 |
finally { reader.close } |
93 |
} |
|
43648 | 94 |
|
95 |
||
96 |
/* check */ |
|
97 |
||
44159 | 98 |
def check(name: String, source: CharSequence): Thy_Header = |
43648 | 99 |
{ |
100 |
val header = read(source) |
|
101 |
val name1 = header.name |
|
43672 | 102 |
if (name != name1) error("Bad file name " + thy_path(name) + " for theory " + quote(name1)) |
103 |
Path.explode(name) |
|
104 |
header |
|
43648 | 105 |
} |
34169 | 106 |
} |
44159 | 107 |
|
108 |
||
109 |
sealed case class Thy_Header(val name: String, val imports: List[String], val uses: List[String]) |
|
110 |
{ |
|
111 |
def map(f: String => String): Thy_Header = |
|
112 |
Thy_Header(f(name), imports.map(f), uses.map(f)) |
|
44163 | 113 |
|
114 |
def norm_deps(f: String => String): Thy_Header = |
|
115 |
copy(imports = imports.map(name => f(name) + ".thy"), uses = uses.map(f)) |
|
44159 | 116 |
} |
117 |