author | wenzelm |
Thu, 13 Jan 2011 17:30:52 +0100 | |
changeset 41535 | 0112f14d75ec |
parent 39630 | 44181423183a |
child 43611 | 21a57a0c5f25 |
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 |
||
34169 | 10 |
import scala.collection.mutable |
11 |
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
|
12 |
import scala.util.matching.Regex |
34169 | 13 |
|
34188
fbfc18be1f8c
scan: operate on file (via Scan.byte_reader), more robust exception handling;
wenzelm
parents:
34169
diff
changeset
|
14 |
import java.io.File |
fbfc18be1f8c
scan: operate on file (via Scan.byte_reader), more robust exception handling;
wenzelm
parents:
34169
diff
changeset
|
15 |
|
34169 | 16 |
|
32466 | 17 |
object Thy_Header |
32450 | 18 |
{ |
28495 | 19 |
val HEADER = "header" |
20 |
val THEORY = "theory" |
|
21 |
val IMPORTS = "imports" |
|
22 |
val USES = "uses" |
|
23 |
val BEGIN = "begin" |
|
24 |
||
34169 | 25 |
val lexicon = Scan.Lexicon("%", "(", ")", ";", BEGIN, HEADER, IMPORTS, THEORY, USES) |
34190 | 26 |
|
34300 | 27 |
final case class Header(val name: String, val imports: List[String], val uses: List[String]) |
38149
3c380380beac
somewhat uniform Thy_Header.split_thy_path in ML and Scala;
wenzelm
parents:
36956
diff
changeset
|
28 |
|
3c380380beac
somewhat uniform Thy_Header.split_thy_path in ML and Scala;
wenzelm
parents:
36956
diff
changeset
|
29 |
|
3c380380beac
somewhat uniform Thy_Header.split_thy_path in ML and Scala;
wenzelm
parents:
36956
diff
changeset
|
30 |
/* file name */ |
3c380380beac
somewhat uniform Thy_Header.split_thy_path in ML and Scala;
wenzelm
parents:
36956
diff
changeset
|
31 |
|
3c380380beac
somewhat uniform Thy_Header.split_thy_path in ML and Scala;
wenzelm
parents:
36956
diff
changeset
|
32 |
val Thy_Path1 = new Regex("([^/]*)\\.thy") |
41535 | 33 |
val Thy_Path2 = new Regex("(.*)/([^/]*)\\.thy") |
38149
3c380380beac
somewhat uniform Thy_Header.split_thy_path in ML and Scala;
wenzelm
parents:
36956
diff
changeset
|
34 |
|
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
38149
diff
changeset
|
35 |
def split_thy_path(path: String): Option[(String, String)] = |
38149
3c380380beac
somewhat uniform Thy_Header.split_thy_path in ML and Scala;
wenzelm
parents:
36956
diff
changeset
|
36 |
path match { |
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
38149
diff
changeset
|
37 |
case Thy_Path1(name) => Some(("", name)) |
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
38149
diff
changeset
|
38 |
case Thy_Path2(dir, name) => Some((dir, name)) |
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
38149
diff
changeset
|
39 |
case _ => None |
38149
3c380380beac
somewhat uniform Thy_Header.split_thy_path in ML and Scala;
wenzelm
parents:
36956
diff
changeset
|
40 |
} |
28495 | 41 |
} |
34169 | 42 |
|
43 |
||
36948 | 44 |
class Thy_Header(symbols: Symbol.Interpretation) extends Parse.Parser |
34169 | 45 |
{ |
46 |
import Thy_Header._ |
|
47 |
||
48 |
||
49 |
/* header */ |
|
50 |
||
34190 | 51 |
val header: Parser[Header] = |
34169 | 52 |
{ |
34201
c95dcd12f48a
separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
34198
diff
changeset
|
53 |
val file_name = atom("file name", _.is_name) ^^ Standard_System.decode_permissive_utf8 |
c95dcd12f48a
separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
34198
diff
changeset
|
54 |
val theory_name = atom("theory name", _.is_name) ^^ Standard_System.decode_permissive_utf8 |
34169 | 55 |
|
56 |
val file = |
|
57 |
keyword("(") ~! (file_name ~ keyword(")")) ^^ { case _ ~ (x ~ _) => x } | file_name |
|
58 |
||
59 |
val uses = opt(keyword(USES) ~! (rep1(file))) ^^ { case None => Nil case Some(_ ~ xs) => xs } |
|
60 |
||
61 |
val args = |
|
62 |
theory_name ~ (keyword(IMPORTS) ~! (rep1(theory_name) ~ uses ~ keyword(BEGIN))) ^^ |
|
34190 | 63 |
{ case x ~ (_ ~ (ys ~ zs ~ _)) => Header(x, ys, zs) } |
34169 | 64 |
|
65 |
(keyword(HEADER) ~ tags) ~! |
|
66 |
((doc_source ~ rep(keyword(";")) ~ keyword(THEORY) ~ tags) ~> args) ^^ { case _ ~ x => x } | |
|
67 |
(keyword(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x } |
|
68 |
} |
|
69 |
||
70 |
||
34190 | 71 |
/* read -- lazy scanning */ |
34169 | 72 |
|
34190 | 73 |
def read(file: File): Header = |
34169 | 74 |
{ |
75 |
val token = lexicon.token(symbols, _ => false) |
|
36956
21be4832c362
renamed class Outer_Lex to Token and Token_Kind to Token.Kind;
wenzelm
parents:
36948
diff
changeset
|
76 |
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
|
77 |
|
fbfc18be1f8c
scan: operate on file (via Scan.byte_reader), more robust exception handling;
wenzelm
parents:
34169
diff
changeset
|
78 |
def scan_to_begin(in: Reader[Char]) |
34169 | 79 |
{ |
80 |
token(in) match { |
|
81 |
case lexicon.Success(tok, rest) => |
|
82 |
toks += tok |
|
36956
21be4832c362
renamed class Outer_Lex to Token and Token_Kind to Token.Kind;
wenzelm
parents:
36948
diff
changeset
|
83 |
if (!(tok.kind == Token.Kind.KEYWORD && tok.content == BEGIN)) |
34188
fbfc18be1f8c
scan: operate on file (via Scan.byte_reader), more robust exception handling;
wenzelm
parents:
34169
diff
changeset
|
84 |
scan_to_begin(rest) |
34169 | 85 |
case _ => |
86 |
} |
|
87 |
} |
|
34188
fbfc18be1f8c
scan: operate on file (via Scan.byte_reader), more robust exception handling;
wenzelm
parents:
34169
diff
changeset
|
88 |
val reader = Scan.byte_reader(file) |
fbfc18be1f8c
scan: operate on file (via Scan.byte_reader), more robust exception handling;
wenzelm
parents:
34169
diff
changeset
|
89 |
try { scan_to_begin(reader) } finally { reader.close } |
34190 | 90 |
|
36956
21be4832c362
renamed class Outer_Lex to Token and Token_Kind to Token.Kind;
wenzelm
parents:
36948
diff
changeset
|
91 |
parse(commit(header), Token.reader(toks.toList)) match { |
34190 | 92 |
case Success(result, _) => result |
93 |
case bad => error(bad.toString) |
|
94 |
} |
|
34169 | 95 |
} |
96 |
} |