| author | kuncar | 
| Tue, 13 Aug 2013 15:59:22 +0200 | |
| changeset 53010 | ec5e6f69bd65 | 
| parent 51627 | 589daaf48dba | 
| child 53962 | 65bca53daf70 | 
| permissions | -rw-r--r-- | 
| 22106 | 1  | 
(* Title: Pure/Thy/thy_header.ML  | 
| 46939 | 2  | 
Author: Makarius  | 
| 22106 | 3  | 
|
| 46939 | 4  | 
Static theory header information.  | 
| 22106 | 5  | 
*)  | 
6  | 
||
7  | 
signature THY_HEADER =  | 
|
8  | 
sig  | 
|
| 
48874
 
ff9cd47be39b
refined Thy_Load.check_thy: find more uses in body text, based on keywords;
 
wenzelm 
parents: 
48864 
diff
changeset
 | 
9  | 
type keywords = (string * Keyword.spec option) list  | 
| 
46938
 
cda018294515
some support for outer syntax keyword declarations within theory header;
 
wenzelm 
parents: 
46737 
diff
changeset
 | 
10  | 
type header =  | 
| 
48927
 
ef462b5558eb
theory def/ref position reports, which enable hyperlinks etc.;
 
wenzelm 
parents: 
48881 
diff
changeset
 | 
11  | 
   {name: string * Position.T,
 | 
| 
 
ef462b5558eb
theory def/ref position reports, which enable hyperlinks etc.;
 
wenzelm 
parents: 
48881 
diff
changeset
 | 
12  | 
imports: (string * Position.T) list,  | 
| 
51294
 
0850d43cb355
discontinued obsolete header "files" -- these are loaded explicitly after exploring dependencies;
 
wenzelm 
parents: 
51293 
diff
changeset
 | 
13  | 
keywords: keywords}  | 
| 
 
0850d43cb355
discontinued obsolete header "files" -- these are loaded explicitly after exploring dependencies;
 
wenzelm 
parents: 
51293 
diff
changeset
 | 
14  | 
val make: string * Position.T -> (string * Position.T) list -> keywords -> header  | 
| 
46958
 
0ec8f04e753a
define keywords early when processing the theory header, before running the body commands;
 
wenzelm 
parents: 
46950 
diff
changeset
 | 
15  | 
val define_keywords: header -> unit  | 
| 
46961
 
5c6955f487e5
outer syntax command definitions based on formal command_spec derived from theory header declarations;
 
wenzelm 
parents: 
46958 
diff
changeset
 | 
16  | 
val declare_keyword: string * Keyword.spec option -> theory -> theory  | 
| 
 
5c6955f487e5
outer syntax command definitions based on formal command_spec derived from theory header declarations;
 
wenzelm 
parents: 
46958 
diff
changeset
 | 
17  | 
val the_keyword: theory -> string -> Keyword.spec option  | 
| 
46938
 
cda018294515
some support for outer syntax keyword declarations within theory header;
 
wenzelm 
parents: 
46737 
diff
changeset
 | 
18  | 
val args: header parser  | 
| 
 
cda018294515
some support for outer syntax keyword declarations within theory header;
 
wenzelm 
parents: 
46737 
diff
changeset
 | 
19  | 
val read: Position.T -> string -> header  | 
| 
48927
 
ef462b5558eb
theory def/ref position reports, which enable hyperlinks etc.;
 
wenzelm 
parents: 
48881 
diff
changeset
 | 
20  | 
val read_tokens: Token.T list -> header  | 
| 22106 | 21  | 
end;  | 
22  | 
||
| 32466 | 23  | 
structure Thy_Header: THY_HEADER =  | 
| 22106 | 24  | 
struct  | 
25  | 
||
| 
48874
 
ff9cd47be39b
refined Thy_Load.check_thy: find more uses in body text, based on keywords;
 
wenzelm 
parents: 
48864 
diff
changeset
 | 
26  | 
type keywords = (string * Keyword.spec option) list;  | 
| 
 
ff9cd47be39b
refined Thy_Load.check_thy: find more uses in body text, based on keywords;
 
wenzelm 
parents: 
48864 
diff
changeset
 | 
27  | 
|
| 
46938
 
cda018294515
some support for outer syntax keyword declarations within theory header;
 
wenzelm 
parents: 
46737 
diff
changeset
 | 
28  | 
type header =  | 
| 
48927
 
ef462b5558eb
theory def/ref position reports, which enable hyperlinks etc.;
 
wenzelm 
parents: 
48881 
diff
changeset
 | 
29  | 
 {name: string * Position.T,
 | 
| 
 
ef462b5558eb
theory def/ref position reports, which enable hyperlinks etc.;
 
wenzelm 
parents: 
48881 
diff
changeset
 | 
30  | 
imports: (string * Position.T) list,  | 
| 
51294
 
0850d43cb355
discontinued obsolete header "files" -- these are loaded explicitly after exploring dependencies;
 
wenzelm 
parents: 
51293 
diff
changeset
 | 
31  | 
keywords: keywords};  | 
| 
46938
 
cda018294515
some support for outer syntax keyword declarations within theory header;
 
wenzelm 
parents: 
46737 
diff
changeset
 | 
32  | 
|
| 
51294
 
0850d43cb355
discontinued obsolete header "files" -- these are loaded explicitly after exploring dependencies;
 
wenzelm 
parents: 
51293 
diff
changeset
 | 
33  | 
fun make name imports keywords : header =  | 
| 
 
0850d43cb355
discontinued obsolete header "files" -- these are loaded explicitly after exploring dependencies;
 
wenzelm 
parents: 
51293 
diff
changeset
 | 
34  | 
  {name = name, imports = imports, keywords = keywords};
 | 
| 
46938
 
cda018294515
some support for outer syntax keyword declarations within theory header;
 
wenzelm 
parents: 
46737 
diff
changeset
 | 
35  | 
|
| 
 
cda018294515
some support for outer syntax keyword declarations within theory header;
 
wenzelm 
parents: 
46737 
diff
changeset
 | 
36  | 
|
| 
 
cda018294515
some support for outer syntax keyword declarations within theory header;
 
wenzelm 
parents: 
46737 
diff
changeset
 | 
37  | 
|
| 
 
cda018294515
some support for outer syntax keyword declarations within theory header;
 
wenzelm 
parents: 
46737 
diff
changeset
 | 
38  | 
(** keyword declarations **)  | 
| 
 
cda018294515
some support for outer syntax keyword declarations within theory header;
 
wenzelm 
parents: 
46737 
diff
changeset
 | 
39  | 
|
| 
46958
 
0ec8f04e753a
define keywords early when processing the theory header, before running the body commands;
 
wenzelm 
parents: 
46950 
diff
changeset
 | 
40  | 
fun define_keywords ({keywords, ...}: header) =
 | 
| 
46961
 
5c6955f487e5
outer syntax command definitions based on formal command_spec derived from theory header declarations;
 
wenzelm 
parents: 
46958 
diff
changeset
 | 
41  | 
List.app (Keyword.define o apsnd (Option.map Keyword.spec)) keywords;  | 
| 
46958
 
0ec8f04e753a
define keywords early when processing the theory header, before running the body commands;
 
wenzelm 
parents: 
46950 
diff
changeset
 | 
42  | 
|
| 
48864
 
3ee314ae1e0a
added keyword kind "thy_load" (with optional list of file extensions);
 
wenzelm 
parents: 
48707 
diff
changeset
 | 
43  | 
fun err_dup name = error ("Duplicate declaration of outer syntax keyword " ^ quote name);
 | 
| 
46938
 
cda018294515
some support for outer syntax keyword declarations within theory header;
 
wenzelm 
parents: 
46737 
diff
changeset
 | 
44  | 
|
| 
 
cda018294515
some support for outer syntax keyword declarations within theory header;
 
wenzelm 
parents: 
46737 
diff
changeset
 | 
45  | 
structure Data = Theory_Data  | 
| 
 
cda018294515
some support for outer syntax keyword declarations within theory header;
 
wenzelm 
parents: 
46737 
diff
changeset
 | 
46  | 
(  | 
| 
46961
 
5c6955f487e5
outer syntax command definitions based on formal command_spec derived from theory header declarations;
 
wenzelm 
parents: 
46958 
diff
changeset
 | 
47  | 
type T = Keyword.spec option Symtab.table;  | 
| 
46938
 
cda018294515
some support for outer syntax keyword declarations within theory header;
 
wenzelm 
parents: 
46737 
diff
changeset
 | 
48  | 
val empty = Symtab.empty;  | 
| 
 
cda018294515
some support for outer syntax keyword declarations within theory header;
 
wenzelm 
parents: 
46737 
diff
changeset
 | 
49  | 
val extend = I;  | 
| 
 
cda018294515
some support for outer syntax keyword declarations within theory header;
 
wenzelm 
parents: 
46737 
diff
changeset
 | 
50  | 
fun merge data : T = Symtab.merge (op =) data handle Symtab.DUP name => err_dup name;  | 
| 
 
cda018294515
some support for outer syntax keyword declarations within theory header;
 
wenzelm 
parents: 
46737 
diff
changeset
 | 
51  | 
);  | 
| 
 
cda018294515
some support for outer syntax keyword declarations within theory header;
 
wenzelm 
parents: 
46737 
diff
changeset
 | 
52  | 
|
| 
46961
 
5c6955f487e5
outer syntax command definitions based on formal command_spec derived from theory header declarations;
 
wenzelm 
parents: 
46958 
diff
changeset
 | 
53  | 
fun declare_keyword (name, spec) =  | 
| 
 
5c6955f487e5
outer syntax command definitions based on formal command_spec derived from theory header declarations;
 
wenzelm 
parents: 
46958 
diff
changeset
 | 
54  | 
Data.map (fn data =>  | 
| 
 
5c6955f487e5
outer syntax command definitions based on formal command_spec derived from theory header declarations;
 
wenzelm 
parents: 
46958 
diff
changeset
 | 
55  | 
(Option.map Keyword.spec spec;  | 
| 
 
5c6955f487e5
outer syntax command definitions based on formal command_spec derived from theory header declarations;
 
wenzelm 
parents: 
46958 
diff
changeset
 | 
56  | 
Symtab.update_new (name, spec) data handle Symtab.DUP dup => err_dup dup));  | 
| 
46938
 
cda018294515
some support for outer syntax keyword declarations within theory header;
 
wenzelm 
parents: 
46737 
diff
changeset
 | 
57  | 
|
| 
 
cda018294515
some support for outer syntax keyword declarations within theory header;
 
wenzelm 
parents: 
46737 
diff
changeset
 | 
58  | 
fun the_keyword thy name =  | 
| 
 
cda018294515
some support for outer syntax keyword declarations within theory header;
 
wenzelm 
parents: 
46737 
diff
changeset
 | 
59  | 
(case Symtab.lookup (Data.get thy) name of  | 
| 
46961
 
5c6955f487e5
outer syntax command definitions based on formal command_spec derived from theory header declarations;
 
wenzelm 
parents: 
46958 
diff
changeset
 | 
60  | 
SOME spec => spec  | 
| 
 
5c6955f487e5
outer syntax command definitions based on formal command_spec derived from theory header declarations;
 
wenzelm 
parents: 
46958 
diff
changeset
 | 
61  | 
  | NONE => error ("Undeclared outer syntax keyword " ^ quote name));
 | 
| 
46938
 
cda018294515
some support for outer syntax keyword declarations within theory header;
 
wenzelm 
parents: 
46737 
diff
changeset
 | 
62  | 
|
| 
 
cda018294515
some support for outer syntax keyword declarations within theory header;
 
wenzelm 
parents: 
46737 
diff
changeset
 | 
63  | 
|
| 
 
cda018294515
some support for outer syntax keyword declarations within theory header;
 
wenzelm 
parents: 
46737 
diff
changeset
 | 
64  | 
|
| 
 
cda018294515
some support for outer syntax keyword declarations within theory header;
 
wenzelm 
parents: 
46737 
diff
changeset
 | 
65  | 
(** concrete syntax **)  | 
| 
 
cda018294515
some support for outer syntax keyword declarations within theory header;
 
wenzelm 
parents: 
46737 
diff
changeset
 | 
66  | 
|
| 
 
cda018294515
some support for outer syntax keyword declarations within theory header;
 
wenzelm 
parents: 
46737 
diff
changeset
 | 
67  | 
(* header keywords *)  | 
| 22106 | 68  | 
|
69  | 
val headerN = "header";  | 
|
70  | 
val theoryN = "theory";  | 
|
71  | 
val importsN = "imports";  | 
|
| 
46938
 
cda018294515
some support for outer syntax keyword declarations within theory header;
 
wenzelm 
parents: 
46737 
diff
changeset
 | 
72  | 
val keywordsN = "keywords";  | 
| 22106 | 73  | 
val beginN = "begin";  | 
74  | 
||
| 
48927
 
ef462b5558eb
theory def/ref position reports, which enable hyperlinks etc.;
 
wenzelm 
parents: 
48881 
diff
changeset
 | 
75  | 
val header_lexicons =  | 
| 
 
ef462b5558eb
theory def/ref position reports, which enable hyperlinks etc.;
 
wenzelm 
parents: 
48881 
diff
changeset
 | 
76  | 
pairself (Scan.make_lexicon o map Symbol.explode)  | 
| 
51293
 
05b1bbae748d
discontinued obsolete 'uses' within theory header;
 
wenzelm 
parents: 
50128 
diff
changeset
 | 
77  | 
   (["%", "(", ")", ",", "::", ";", "==", "and", beginN, importsN, keywordsN],
 | 
| 
48927
 
ef462b5558eb
theory def/ref position reports, which enable hyperlinks etc.;
 
wenzelm 
parents: 
48881 
diff
changeset
 | 
78  | 
[headerN, theoryN]);  | 
| 22106 | 79  | 
|
80  | 
||
81  | 
(* header args *)  | 
|
82  | 
||
| 
46938
 
cda018294515
some support for outer syntax keyword declarations within theory header;
 
wenzelm 
parents: 
46737 
diff
changeset
 | 
83  | 
local  | 
| 
 
cda018294515
some support for outer syntax keyword declarations within theory header;
 
wenzelm 
parents: 
46737 
diff
changeset
 | 
84  | 
|
| 
48927
 
ef462b5558eb
theory def/ref position reports, which enable hyperlinks etc.;
 
wenzelm 
parents: 
48881 
diff
changeset
 | 
85  | 
val theory_name = Parse.group (fn () => "theory name") (Parse.position Parse.name);  | 
| 
27890
 
62ac62e30ab1
args: explicit groups for file_name, theory_name;
 
wenzelm 
parents: 
27872 
diff
changeset
 | 
86  | 
|
| 
48864
 
3ee314ae1e0a
added keyword kind "thy_load" (with optional list of file extensions);
 
wenzelm 
parents: 
48707 
diff
changeset
 | 
87  | 
val opt_files =  | 
| 
 
3ee314ae1e0a
added keyword kind "thy_load" (with optional list of file extensions);
 
wenzelm 
parents: 
48707 
diff
changeset
 | 
88  | 
  Scan.optional (Parse.$$$ "(" |-- Parse.!!! (Parse.list1 Parse.name) --| Parse.$$$ ")") [];
 | 
| 
50128
 
599c935aac82
alternative completion for outer syntax keywords;
 
wenzelm 
parents: 
48992 
diff
changeset
 | 
89  | 
|
| 
48864
 
3ee314ae1e0a
added keyword kind "thy_load" (with optional list of file extensions);
 
wenzelm 
parents: 
48707 
diff
changeset
 | 
90  | 
val keyword_spec =  | 
| 
 
3ee314ae1e0a
added keyword kind "thy_load" (with optional list of file extensions);
 
wenzelm 
parents: 
48707 
diff
changeset
 | 
91  | 
Parse.group (fn () => "outer syntax keyword specification")  | 
| 
 
3ee314ae1e0a
added keyword kind "thy_load" (with optional list of file extensions);
 
wenzelm 
parents: 
48707 
diff
changeset
 | 
92  | 
(Parse.name -- opt_files -- Parse.tags);  | 
| 
 
3ee314ae1e0a
added keyword kind "thy_load" (with optional list of file extensions);
 
wenzelm 
parents: 
48707 
diff
changeset
 | 
93  | 
|
| 
50128
 
599c935aac82
alternative completion for outer syntax keywords;
 
wenzelm 
parents: 
48992 
diff
changeset
 | 
94  | 
val keyword_compl =  | 
| 
 
599c935aac82
alternative completion for outer syntax keywords;
 
wenzelm 
parents: 
48992 
diff
changeset
 | 
95  | 
Parse.group (fn () => "outer syntax keyword completion") Parse.name;  | 
| 
 
599c935aac82
alternative completion for outer syntax keywords;
 
wenzelm 
parents: 
48992 
diff
changeset
 | 
96  | 
|
| 46939 | 97  | 
val keyword_decl =  | 
| 
50128
 
599c935aac82
alternative completion for outer syntax keywords;
 
wenzelm 
parents: 
48992 
diff
changeset
 | 
98  | 
Scan.repeat1 Parse.string --  | 
| 
 
599c935aac82
alternative completion for outer syntax keywords;
 
wenzelm 
parents: 
48992 
diff
changeset
 | 
99  | 
Scan.option (Parse.$$$ "::" |-- Parse.!!! keyword_spec) --  | 
| 
 
599c935aac82
alternative completion for outer syntax keywords;
 
wenzelm 
parents: 
48992 
diff
changeset
 | 
100  | 
Scan.option (Parse.$$$ "==" |-- Parse.!!! keyword_compl)  | 
| 
 
599c935aac82
alternative completion for outer syntax keywords;
 
wenzelm 
parents: 
48992 
diff
changeset
 | 
101  | 
>> (fn ((names, spec), _) => map (rpair spec) names);  | 
| 
 
599c935aac82
alternative completion for outer syntax keywords;
 
wenzelm 
parents: 
48992 
diff
changeset
 | 
102  | 
|
| 46939 | 103  | 
val keyword_decls = Parse.and_list1 keyword_decl >> flat;  | 
| 
46938
 
cda018294515
some support for outer syntax keyword declarations within theory header;
 
wenzelm 
parents: 
46737 
diff
changeset
 | 
104  | 
|
| 
 
cda018294515
some support for outer syntax keyword declarations within theory header;
 
wenzelm 
parents: 
46737 
diff
changeset
 | 
105  | 
in  | 
| 22106 | 106  | 
|
107  | 
val args =  | 
|
| 
46938
 
cda018294515
some support for outer syntax keyword declarations within theory header;
 
wenzelm 
parents: 
46737 
diff
changeset
 | 
108  | 
theory_name --  | 
| 48638 | 109  | 
Scan.optional (Parse.$$$ importsN |-- Parse.!!! (Scan.repeat1 theory_name)) [] --  | 
| 
51293
 
05b1bbae748d
discontinued obsolete 'uses' within theory header;
 
wenzelm 
parents: 
50128 
diff
changeset
 | 
110  | 
Scan.optional (Parse.$$$ keywordsN |-- Parse.!!! keyword_decls) [] --|  | 
| 
46938
 
cda018294515
some support for outer syntax keyword declarations within theory header;
 
wenzelm 
parents: 
46737 
diff
changeset
 | 
111  | 
Parse.$$$ beginN >>  | 
| 
51294
 
0850d43cb355
discontinued obsolete header "files" -- these are loaded explicitly after exploring dependencies;
 
wenzelm 
parents: 
51293 
diff
changeset
 | 
112  | 
(fn ((name, imports), keywords) => make name imports keywords);  | 
| 
46938
 
cda018294515
some support for outer syntax keyword declarations within theory header;
 
wenzelm 
parents: 
46737 
diff
changeset
 | 
113  | 
|
| 
 
cda018294515
some support for outer syntax keyword declarations within theory header;
 
wenzelm 
parents: 
46737 
diff
changeset
 | 
114  | 
end;  | 
| 22106 | 115  | 
|
116  | 
||
117  | 
(* read header *)  | 
|
118  | 
||
119  | 
val header =  | 
|
| 
48927
 
ef462b5558eb
theory def/ref position reports, which enable hyperlinks etc.;
 
wenzelm 
parents: 
48881 
diff
changeset
 | 
120  | 
(Parse.command_name headerN -- Parse.tags) |--  | 
| 
51627
 
589daaf48dba
tuned signature -- agree with markup terminology;
 
wenzelm 
parents: 
51294 
diff
changeset
 | 
121  | 
(Parse.!!! (Parse.document_source -- Scan.repeat Parse.semicolon --  | 
| 
48927
 
ef462b5558eb
theory def/ref position reports, which enable hyperlinks etc.;
 
wenzelm 
parents: 
48881 
diff
changeset
 | 
122  | 
(Parse.command_name theoryN -- Parse.tags) |-- args)) ||  | 
| 
 
ef462b5558eb
theory def/ref position reports, which enable hyperlinks etc.;
 
wenzelm 
parents: 
48881 
diff
changeset
 | 
123  | 
(Parse.command_name theoryN -- Parse.tags) |-- Parse.!!! args;  | 
| 22106 | 124  | 
|
| 
48927
 
ef462b5558eb
theory def/ref position reports, which enable hyperlinks etc.;
 
wenzelm 
parents: 
48881 
diff
changeset
 | 
125  | 
fun token_source pos str =  | 
| 
 
ef462b5558eb
theory def/ref position reports, which enable hyperlinks etc.;
 
wenzelm 
parents: 
48881 
diff
changeset
 | 
126  | 
str  | 
| 
 
ef462b5558eb
theory def/ref position reports, which enable hyperlinks etc.;
 
wenzelm 
parents: 
48881 
diff
changeset
 | 
127  | 
|> Source.of_string_limited 8000  | 
| 
 
ef462b5558eb
theory def/ref position reports, which enable hyperlinks etc.;
 
wenzelm 
parents: 
48881 
diff
changeset
 | 
128  | 
|> Symbol.source  | 
| 
 
ef462b5558eb
theory def/ref position reports, which enable hyperlinks etc.;
 
wenzelm 
parents: 
48881 
diff
changeset
 | 
129  | 
  |> Token.source {do_recover = NONE} (K header_lexicons) pos;
 | 
| 
 
ef462b5558eb
theory def/ref position reports, which enable hyperlinks etc.;
 
wenzelm 
parents: 
48881 
diff
changeset
 | 
130  | 
|
| 
 
ef462b5558eb
theory def/ref position reports, which enable hyperlinks etc.;
 
wenzelm 
parents: 
48881 
diff
changeset
 | 
131  | 
fun read_source pos source =  | 
| 22106 | 132  | 
let val res =  | 
| 
48927
 
ef462b5558eb
theory def/ref position reports, which enable hyperlinks etc.;
 
wenzelm 
parents: 
48881 
diff
changeset
 | 
133  | 
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
 | 
134  | 
|> 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
 | 
135  | 
|> Source.source Token.stopper (Scan.single (Scan.error (Parse.!!! header))) NONE  | 
| 22106 | 136  | 
|> Source.get_single;  | 
137  | 
in  | 
|
| 
42003
 
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
 
wenzelm 
parents: 
41944 
diff
changeset
 | 
138  | 
(case res of  | 
| 
46938
 
cda018294515
some support for outer syntax keyword declarations within theory header;
 
wenzelm 
parents: 
46737 
diff
changeset
 | 
139  | 
SOME (h, _) => h  | 
| 48992 | 140  | 
    | NONE => error ("Unexpected end of input" ^ Position.here pos))
 | 
| 22106 | 141  | 
end;  | 
142  | 
||
| 
48927
 
ef462b5558eb
theory def/ref position reports, which enable hyperlinks etc.;
 
wenzelm 
parents: 
48881 
diff
changeset
 | 
143  | 
fun read pos str = read_source pos (token_source pos str);  | 
| 
 
ef462b5558eb
theory def/ref position reports, which enable hyperlinks etc.;
 
wenzelm 
parents: 
48881 
diff
changeset
 | 
144  | 
fun read_tokens toks = read_source Position.none (Source.of_list toks);  | 
| 
 
ef462b5558eb
theory def/ref position reports, which enable hyperlinks etc.;
 
wenzelm 
parents: 
48881 
diff
changeset
 | 
145  | 
|
| 22106 | 146  | 
end;  |