author | wenzelm |
Sat, 13 Nov 2010 19:21:53 +0100 | |
changeset 40523 | 1050315f6ee2 |
parent 38149 | 3c380380beac |
child 40530 | f814863f3918 |
permissions | -rw-r--r-- |
22106 | 1 |
(* Title: Pure/Thy/thy_header.ML |
2 |
Author: Markus Wenzel, TU Muenchen |
|
3 |
||
4 |
Theory headers -- independent of outer syntax. |
|
5 |
*) |
|
6 |
||
7 |
signature THY_HEADER = |
|
8 |
sig |
|
36959
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
36950
diff
changeset
|
9 |
val args: Token.T list -> (string * string list * (string * bool) list) * Token.T list |
37978
548f3f165d05
simplified Thy_Header.read -- include Source.of_string_limited here;
wenzelm
parents:
37950
diff
changeset
|
10 |
val read: Position.T -> string -> string * string list * (string * bool) list |
37950
bc285d91041e
moved basic thy file name operations from Thy_Load to Thy_Header;
wenzelm
parents:
36959
diff
changeset
|
11 |
val thy_path: string -> Path.T |
bc285d91041e
moved basic thy file name operations from Thy_Load to Thy_Header;
wenzelm
parents:
36959
diff
changeset
|
12 |
val split_thy_path: Path.T -> Path.T * string |
bc285d91041e
moved basic thy file name operations from Thy_Load to Thy_Header;
wenzelm
parents:
36959
diff
changeset
|
13 |
val consistent_name: string -> string -> unit |
22106 | 14 |
end; |
15 |
||
32466 | 16 |
structure Thy_Header: THY_HEADER = |
22106 | 17 |
struct |
18 |
||
19 |
(* keywords *) |
|
20 |
||
21 |
val headerN = "header"; |
|
22 |
val theoryN = "theory"; |
|
23 |
val importsN = "imports"; |
|
24 |
val usesN = "uses"; |
|
25 |
val beginN = "begin"; |
|
26 |
||
24577 | 27 |
val header_lexicon = Scan.make_lexicon |
28 |
(map Symbol.explode ["%", "(", ")", ";", beginN, headerN, importsN, theoryN, usesN]); |
|
22106 | 29 |
|
30 |
||
31 |
(* header args *) |
|
32 |
||
36950 | 33 |
val file_name = Parse.group "file name" Parse.name; |
34 |
val theory_name = Parse.group "theory name" Parse.name; |
|
27890
62ac62e30ab1
args: explicit groups for file_name, theory_name;
wenzelm
parents:
27872
diff
changeset
|
35 |
|
36950 | 36 |
val file = |
37 |
Parse.$$$ "(" |-- Parse.!!! (file_name --| Parse.$$$ ")") >> rpair false || |
|
38 |
file_name >> rpair true; |
|
39 |
||
40 |
val uses = Scan.optional (Parse.$$$ usesN |-- Parse.!!! (Scan.repeat1 file)) []; |
|
22106 | 41 |
|
42 |
val args = |
|
36950 | 43 |
theory_name -- (Parse.$$$ importsN |-- |
44 |
Parse.!!! (Scan.repeat1 theory_name -- uses --| Parse.$$$ beginN)) |
|
45 |
>> Parse.triple2; |
|
22106 | 46 |
|
47 |
||
48 |
(* read header *) |
|
49 |
||
50 |
val header = |
|
36950 | 51 |
(Parse.$$$ headerN -- Parse.tags) |-- |
52 |
(Parse.!!! (Parse.doc_source -- Scan.repeat Parse.semicolon -- |
|
53 |
(Parse.$$$ theoryN -- Parse.tags) |-- args)) || |
|
54 |
(Parse.$$$ theoryN -- Parse.tags) |-- Parse.!!! args; |
|
22106 | 55 |
|
37978
548f3f165d05
simplified Thy_Header.read -- include Source.of_string_limited here;
wenzelm
parents:
37950
diff
changeset
|
56 |
fun read pos str = |
22106 | 57 |
let val res = |
37978
548f3f165d05
simplified Thy_Header.read -- include Source.of_string_limited here;
wenzelm
parents:
37950
diff
changeset
|
58 |
str |
548f3f165d05
simplified Thy_Header.read -- include Source.of_string_limited here;
wenzelm
parents:
37950
diff
changeset
|
59 |
|> Source.of_string_limited 8000 |
40523
1050315f6ee2
simplified/robustified treatment of malformed symbols, which are now fully internalized (total Symbol.explode etc.);
wenzelm
parents:
38149
diff
changeset
|
60 |
|> Symbol.source |
36959
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
36950
diff
changeset
|
61 |
|> Token.source {do_recover = NONE} (fn () => (header_lexicon, Scan.empty_lexicon)) pos |
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
36950
diff
changeset
|
62 |
|> Token.source_proper |
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
36950
diff
changeset
|
63 |
|> Source.source Token.stopper (Scan.single (Scan.error (Parse.!!! header))) NONE |
22106 | 64 |
|> Source.get_single; |
65 |
in |
|
66 |
(case res of SOME (x, _) => x |
|
67 |
| NONE => error ("Unexpected end of input" ^ Position.str_of pos)) |
|
68 |
end; |
|
69 |
||
37950
bc285d91041e
moved basic thy file name operations from Thy_Load to Thy_Header;
wenzelm
parents:
36959
diff
changeset
|
70 |
|
38149
3c380380beac
somewhat uniform Thy_Header.split_thy_path in ML and Scala;
wenzelm
parents:
37978
diff
changeset
|
71 |
(* file name *) |
37950
bc285d91041e
moved basic thy file name operations from Thy_Load to Thy_Header;
wenzelm
parents:
36959
diff
changeset
|
72 |
|
bc285d91041e
moved basic thy file name operations from Thy_Load to Thy_Header;
wenzelm
parents:
36959
diff
changeset
|
73 |
val thy_path = Path.ext "thy" o Path.basic; |
bc285d91041e
moved basic thy file name operations from Thy_Load to Thy_Header;
wenzelm
parents:
36959
diff
changeset
|
74 |
|
bc285d91041e
moved basic thy file name operations from Thy_Load to Thy_Header;
wenzelm
parents:
36959
diff
changeset
|
75 |
fun split_thy_path path = |
bc285d91041e
moved basic thy file name operations from Thy_Load to Thy_Header;
wenzelm
parents:
36959
diff
changeset
|
76 |
(case Path.split_ext path of |
bc285d91041e
moved basic thy file name operations from Thy_Load to Thy_Header;
wenzelm
parents:
36959
diff
changeset
|
77 |
(path', "thy") => (Path.dir path', Path.implode (Path.base path')) |
38149
3c380380beac
somewhat uniform Thy_Header.split_thy_path in ML and Scala;
wenzelm
parents:
37978
diff
changeset
|
78 |
| _ => error ("Bad theory file specification: " ^ Path.implode path)); |
37950
bc285d91041e
moved basic thy file name operations from Thy_Load to Thy_Header;
wenzelm
parents:
36959
diff
changeset
|
79 |
|
bc285d91041e
moved basic thy file name operations from Thy_Load to Thy_Header;
wenzelm
parents:
36959
diff
changeset
|
80 |
fun consistent_name name name' = |
bc285d91041e
moved basic thy file name operations from Thy_Load to Thy_Header;
wenzelm
parents:
36959
diff
changeset
|
81 |
if name = name' then () |
bc285d91041e
moved basic thy file name operations from Thy_Load to Thy_Header;
wenzelm
parents:
36959
diff
changeset
|
82 |
else error ("Filename " ^ quote (Path.implode (thy_path name)) ^ |
bc285d91041e
moved basic thy file name operations from Thy_Load to Thy_Header;
wenzelm
parents:
36959
diff
changeset
|
83 |
" does not agree with theory name " ^ quote name'); |
bc285d91041e
moved basic thy file name operations from Thy_Load to Thy_Header;
wenzelm
parents:
36959
diff
changeset
|
84 |
|
22106 | 85 |
end; |