31325
|
1 |
(* Title: Pure/ML/ml_env.ML
|
|
2 |
Author: Makarius
|
|
3 |
|
31328
|
4 |
Local environment of ML sources and results.
|
31325
|
5 |
*)
|
|
6 |
|
|
7 |
signature ML_ENV =
|
|
8 |
sig
|
|
9 |
val inherit: Context.generic -> Context.generic -> Context.generic
|
31328
|
10 |
val register_tokens: ML_Lex.token list -> Context.generic ->
|
|
11 |
(serial * ML_Lex.token) list * Context.generic
|
31331
|
12 |
val token_position: Context.generic -> serial -> Position.T option
|
31325
|
13 |
val name_space: ML_Name_Space.T
|
|
14 |
val local_context: use_context
|
|
15 |
end
|
|
16 |
|
|
17 |
structure ML_Env: ML_ENV =
|
|
18 |
struct
|
|
19 |
|
31328
|
20 |
(* context data *)
|
|
21 |
|
31325
|
22 |
structure Env = GenericDataFun
|
|
23 |
(
|
|
24 |
type T =
|
31328
|
25 |
Position.T Inttab.table *
|
|
26 |
(ML_Name_Space.valueVal Symtab.table *
|
|
27 |
ML_Name_Space.typeVal Symtab.table *
|
|
28 |
ML_Name_Space.fixityVal Symtab.table *
|
|
29 |
ML_Name_Space.structureVal Symtab.table *
|
|
30 |
ML_Name_Space.signatureVal Symtab.table *
|
|
31 |
ML_Name_Space.functorVal Symtab.table);
|
|
32 |
val empty =
|
|
33 |
(Inttab.empty,
|
|
34 |
(Symtab.empty, Symtab.empty, Symtab.empty, Symtab.empty, Symtab.empty, Symtab.empty));
|
31325
|
35 |
val extend = I;
|
|
36 |
fun merge _
|
31328
|
37 |
((toks1, (val1, type1, fixity1, structure1, signature1, functor1)),
|
|
38 |
(toks2, (val2, type2, fixity2, structure2, signature2, functor2))) : T =
|
|
39 |
(Inttab.merge (K true) (toks1, toks2),
|
|
40 |
(Symtab.merge (K true) (val1, val2),
|
|
41 |
Symtab.merge (K true) (type1, type2),
|
|
42 |
Symtab.merge (K true) (fixity1, fixity2),
|
|
43 |
Symtab.merge (K true) (structure1, structure2),
|
|
44 |
Symtab.merge (K true) (signature1, signature2),
|
|
45 |
Symtab.merge (K true) (functor1, functor2)));
|
31325
|
46 |
);
|
|
47 |
|
|
48 |
val inherit = Env.put o Env.get;
|
|
49 |
|
31328
|
50 |
|
|
51 |
(* source tokens *)
|
|
52 |
|
|
53 |
fun register_tokens toks context =
|
|
54 |
let
|
|
55 |
val regs = map (fn tok => (serial (), tok)) toks;
|
|
56 |
val context' = context
|
|
57 |
|> Env.map (apfst (fold (fn (i, tok) => Inttab.update (i, ML_Lex.pos_of tok)) regs));
|
|
58 |
in (regs, context') end;
|
|
59 |
|
31331
|
60 |
val token_position = Inttab.lookup o #1 o Env.get;
|
31328
|
61 |
|
|
62 |
|
|
63 |
(* results *)
|
|
64 |
|
31325
|
65 |
val name_space: ML_Name_Space.T =
|
|
66 |
let
|
|
67 |
fun lookup sel1 sel2 name =
|
|
68 |
Context.thread_data ()
|
31328
|
69 |
|> (fn NONE => NONE | SOME context => Symtab.lookup (sel1 (#2 (Env.get context))) name)
|
31325
|
70 |
|> (fn NONE => sel2 ML_Name_Space.global name | some => some);
|
|
71 |
|
|
72 |
fun all sel1 sel2 () =
|
|
73 |
Context.thread_data ()
|
31328
|
74 |
|> (fn NONE => [] | SOME context => Symtab.dest (sel1 (#2 (Env.get context))))
|
31325
|
75 |
|> append (sel2 ML_Name_Space.global ())
|
|
76 |
|> sort_distinct (string_ord o pairself #1);
|
|
77 |
|
|
78 |
fun enter ap1 sel2 entry =
|
|
79 |
if is_some (Context.thread_data ()) then
|
31328
|
80 |
Context.>> (Env.map (apsnd (ap1 (Symtab.update entry))))
|
31325
|
81 |
else sel2 ML_Name_Space.global entry;
|
|
82 |
in
|
|
83 |
{lookupVal = lookup #1 #lookupVal,
|
|
84 |
lookupType = lookup #2 #lookupType,
|
|
85 |
lookupFix = lookup #3 #lookupFix,
|
|
86 |
lookupStruct = lookup #4 #lookupStruct,
|
|
87 |
lookupSig = lookup #5 #lookupSig,
|
|
88 |
lookupFunct = lookup #6 #lookupFunct,
|
|
89 |
enterVal = enter (fn h => fn (a, b, c, d, e, f) => (h a, b, c, d, e, f)) #enterVal,
|
|
90 |
enterType = enter (fn h => fn (a, b, c, d, e, f) => (a, h b, c, d, e, f)) #enterType,
|
|
91 |
enterFix = enter (fn h => fn (a, b, c, d, e, f) => (a, b, h c, d, e, f)) #enterFix,
|
|
92 |
enterStruct = enter (fn h => fn (a, b, c, d, e, f) => (a, b, c, h d, e, f)) #enterStruct,
|
|
93 |
enterSig = enter (fn h => fn (a, b, c, d, e, f) => (a, b, c, d, h e, f)) #enterSig,
|
|
94 |
enterFunct = enter (fn h => fn (a, b, c, d, e, f) => (a, b, c, d, e, h f)) #enterFunct,
|
|
95 |
allVal = all #1 #allVal,
|
|
96 |
allType = all #2 #allType,
|
|
97 |
allFix = all #3 #allFix,
|
|
98 |
allStruct = all #4 #allStruct,
|
|
99 |
allSig = all #5 #allSig,
|
|
100 |
allFunct = all #6 #allFunct}
|
|
101 |
end;
|
|
102 |
|
|
103 |
val local_context: use_context =
|
|
104 |
{tune_source = ML_Parse.fix_ints,
|
|
105 |
name_space = name_space,
|
|
106 |
str_of_pos = Position.str_of oo Position.line_file,
|
|
107 |
print = writeln,
|
|
108 |
error = error};
|
|
109 |
|
|
110 |
end;
|
|
111 |
|