author | wenzelm |
Sun, 10 Jul 2011 13:51:21 +0200 | |
changeset 43724 | 4e58485fa320 |
parent 43723 | 8a63c95b1d5b |
child 43725 | bebcfcad12d4 |
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 |
|
43699 | 28 |
sealed case class Header(val name: String, val imports: List[String], val uses: List[String]) |
43611 | 29 |
{ |
43699 | 30 |
def map(f: String => String): Header = |
31 |
Header(f(name), imports.map(f), uses.map(f)) |
|
43611 | 32 |
} |
38149
3c380380beac
somewhat uniform Thy_Header.split_thy_path in ML and Scala;
wenzelm
parents:
36956
diff
changeset
|
33 |
|
43722 | 34 |
def make_xml_data(header: Header): XML.Body = |
43724 | 35 |
{ import XML_Data.Make._ |
36 |
triple(string, list(string), list(string))(header.name, header.imports, header.uses) } |
|
43722 | 37 |
|
38149
3c380380beac
somewhat uniform Thy_Header.split_thy_path in ML and Scala;
wenzelm
parents:
36956
diff
changeset
|
38 |
|
3c380380beac
somewhat uniform Thy_Header.split_thy_path in ML and Scala;
wenzelm
parents:
36956
diff
changeset
|
39 |
/* file name */ |
3c380380beac
somewhat uniform Thy_Header.split_thy_path in ML and Scala;
wenzelm
parents:
36956
diff
changeset
|
40 |
|
43648 | 41 |
def thy_path(name: String): Path = Path.basic(name).ext("thy") |
42 |
||
43697
77ce24aa1770
explicit Document.Node.Header, with master_dir and thy_name;
wenzelm
parents:
43695
diff
changeset
|
43 |
def split_thy_path(path: Path): (Path, String) = |
77ce24aa1770
explicit Document.Node.Header, with master_dir and thy_name;
wenzelm
parents:
43695
diff
changeset
|
44 |
path.split_ext match { |
77ce24aa1770
explicit Document.Node.Header, with master_dir and thy_name;
wenzelm
parents:
43695
diff
changeset
|
45 |
case (path1, "thy") => (path1.dir, path1.base.implode) |
77ce24aa1770
explicit Document.Node.Header, with master_dir and thy_name;
wenzelm
parents:
43695
diff
changeset
|
46 |
case _ => error("Bad theory file specification: " + path) |
38149
3c380380beac
somewhat uniform Thy_Header.split_thy_path in ML and Scala;
wenzelm
parents:
36956
diff
changeset
|
47 |
} |
34169 | 48 |
|
49 |
||
50 |
/* header */ |
|
51 |
||
34190 | 52 |
val header: Parser[Header] = |
34169 | 53 |
{ |
43611 | 54 |
val file_name = atom("file name", _.is_name) |
55 |
val theory_name = atom("theory name", _.is_name) |
|
34169 | 56 |
|
57 |
val file = |
|
58 |
keyword("(") ~! (file_name ~ keyword(")")) ^^ { case _ ~ (x ~ _) => x } | file_name |
|
59 |
||
60 |
val uses = opt(keyword(USES) ~! (rep1(file))) ^^ { case None => Nil case Some(_ ~ xs) => xs } |
|
61 |
||
62 |
val args = |
|
63 |
theory_name ~ (keyword(IMPORTS) ~! (rep1(theory_name) ~ uses ~ keyword(BEGIN))) ^^ |
|
34190 | 64 |
{ case x ~ (_ ~ (ys ~ zs ~ _)) => Header(x, ys, zs) } |
34169 | 65 |
|
66 |
(keyword(HEADER) ~ tags) ~! |
|
67 |
((doc_source ~ rep(keyword(";")) ~ keyword(THEORY) ~ tags) ~> args) ^^ { case _ ~ x => x } | |
|
68 |
(keyword(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x } |
|
69 |
} |
|
70 |
||
71 |
||
34190 | 72 |
/* read -- lazy scanning */ |
34169 | 73 |
|
43611 | 74 |
def read(reader: Reader[Char]): Header = |
34169 | 75 |
{ |
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43672
diff
changeset
|
76 |
val token = lexicon.token(_ => false) |
36956
21be4832c362
renamed class Outer_Lex to Token and Token_Kind to Token.Kind;
wenzelm
parents:
36948
diff
changeset
|
77 |
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
|
78 |
|
43611 | 79 |
@tailrec def scan_to_begin(in: Reader[Char]) |
34169 | 80 |
{ |
81 |
token(in) match { |
|
82 |
case lexicon.Success(tok, rest) => |
|
83 |
toks += tok |
|
43611 | 84 |
if (!tok.is_begin) scan_to_begin(rest) |
34169 | 85 |
case _ => |
86 |
} |
|
87 |
} |
|
43611 | 88 |
scan_to_begin(reader) |
34190 | 89 |
|
36956
21be4832c362
renamed class Outer_Lex to Token and Token_Kind to Token.Kind;
wenzelm
parents:
36948
diff
changeset
|
90 |
parse(commit(header), Token.reader(toks.toList)) match { |
34190 | 91 |
case Success(result, _) => result |
92 |
case bad => error(bad.toString) |
|
93 |
} |
|
34169 | 94 |
} |
43611 | 95 |
|
43646 | 96 |
def read(source: CharSequence): Header = |
97 |
read(new CharSequenceReader(source)) |
|
98 |
||
43611 | 99 |
def read(file: File): Header = |
100 |
{ |
|
101 |
val reader = Scan.byte_reader(file) |
|
43699 | 102 |
try { read(reader).map(Standard_System.decode_permissive_utf8) } |
43611 | 103 |
finally { reader.close } |
104 |
} |
|
43648 | 105 |
|
106 |
||
107 |
/* check */ |
|
108 |
||
109 |
def check(name: String, source: CharSequence): Header = |
|
110 |
{ |
|
111 |
val header = read(source) |
|
112 |
val name1 = header.name |
|
43672 | 113 |
if (name != name1) error("Bad file name " + thy_path(name) + " for theory " + quote(name1)) |
114 |
Path.explode(name) |
|
115 |
header |
|
43648 | 116 |
} |
34169 | 117 |
} |