author | wenzelm |
Sun, 27 Dec 2009 21:34:23 +0100 | |
changeset 34188 | fbfc18be1f8c |
parent 34169 | 7501b2910900 |
child 34190 | dfcf667bbfed |
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} |
|
12 |
||
34188
fbfc18be1f8c
scan: operate on file (via Scan.byte_reader), more robust exception handling;
wenzelm
parents:
34169
diff
changeset
|
13 |
import java.io.File |
fbfc18be1f8c
scan: operate on file (via Scan.byte_reader), more robust exception handling;
wenzelm
parents:
34169
diff
changeset
|
14 |
|
34169 | 15 |
|
32466 | 16 |
object Thy_Header |
32450 | 17 |
{ |
28495 | 18 |
val HEADER = "header" |
19 |
val THEORY = "theory" |
|
20 |
val IMPORTS = "imports" |
|
21 |
val USES = "uses" |
|
22 |
val BEGIN = "begin" |
|
23 |
||
34169 | 24 |
val lexicon = Scan.Lexicon("%", "(", ")", ";", BEGIN, HEADER, IMPORTS, THEORY, USES) |
28495 | 25 |
} |
34169 | 26 |
|
27 |
||
28 |
class Thy_Header(symbols: Symbol.Interpretation) extends Outer_Parse.Parser |
|
29 |
{ |
|
30 |
import Thy_Header._ |
|
31 |
||
32 |
||
33 |
/* header */ |
|
34 |
||
35 |
val header: Parser[(String, List[String], List[String])] = |
|
36 |
{ |
|
37 |
val file_name = atom("file name", _.is_name) |
|
38 |
val theory_name = atom("theory name", _.is_name) |
|
39 |
||
40 |
val file = |
|
41 |
keyword("(") ~! (file_name ~ keyword(")")) ^^ { case _ ~ (x ~ _) => x } | file_name |
|
42 |
||
43 |
val uses = opt(keyword(USES) ~! (rep1(file))) ^^ { case None => Nil case Some(_ ~ xs) => xs } |
|
44 |
||
45 |
val args = |
|
46 |
theory_name ~ (keyword(IMPORTS) ~! (rep1(theory_name) ~ uses ~ keyword(BEGIN))) ^^ |
|
47 |
{ case x ~ (_ ~ (ys ~ zs ~ _)) => (x, ys, zs) } |
|
48 |
||
49 |
(keyword(HEADER) ~ tags) ~! |
|
50 |
((doc_source ~ rep(keyword(";")) ~ keyword(THEORY) ~ tags) ~> args) ^^ { case _ ~ x => x } | |
|
51 |
(keyword(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x } |
|
52 |
} |
|
53 |
||
54 |
||
55 |
/* scan -- lazy approximation */ |
|
56 |
||
34188
fbfc18be1f8c
scan: operate on file (via Scan.byte_reader), more robust exception handling;
wenzelm
parents:
34169
diff
changeset
|
57 |
def scan(file: File): List[Outer_Lex.Token] = |
34169 | 58 |
{ |
59 |
val token = lexicon.token(symbols, _ => false) |
|
60 |
val toks = new mutable.ListBuffer[Outer_Lex.Token] |
|
34188
fbfc18be1f8c
scan: operate on file (via Scan.byte_reader), more robust exception handling;
wenzelm
parents:
34169
diff
changeset
|
61 |
|
fbfc18be1f8c
scan: operate on file (via Scan.byte_reader), more robust exception handling;
wenzelm
parents:
34169
diff
changeset
|
62 |
def scan_to_begin(in: Reader[Char]) |
34169 | 63 |
{ |
64 |
token(in) match { |
|
65 |
case lexicon.Success(tok, rest) => |
|
66 |
toks += tok |
|
67 |
if (!(tok.kind == Outer_Lex.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
|
68 |
scan_to_begin(rest) |
34169 | 69 |
case _ => |
70 |
} |
|
71 |
} |
|
34188
fbfc18be1f8c
scan: operate on file (via Scan.byte_reader), more robust exception handling;
wenzelm
parents:
34169
diff
changeset
|
72 |
|
fbfc18be1f8c
scan: operate on file (via Scan.byte_reader), more robust exception handling;
wenzelm
parents:
34169
diff
changeset
|
73 |
val reader = Scan.byte_reader(file) |
fbfc18be1f8c
scan: operate on file (via Scan.byte_reader), more robust exception handling;
wenzelm
parents:
34169
diff
changeset
|
74 |
try { scan_to_begin(reader) } finally { reader.close } |
34169 | 75 |
toks.toList |
76 |
} |
|
77 |
} |