author | Christian Urban <urbanc@in.tum.de> |
Wed, 14 Jan 2009 16:46:07 +0000 | |
changeset 68 | e7519207c2b7 |
parent 57 | 065f472c09ab |
child 90 | b071a0b88298 |
permissions | -rw-r--r-- |
24 | 1 |
structure Chunks = |
2 |
struct |
|
3 |
||
4 |
(** theory data **) |
|
5 |
||
6 |
structure ChunkData = TheoryDataFun |
|
7 |
( |
|
8 |
type T = (ML_Lex.token list * stamp) NameSpace.table |
|
9 |
val empty = NameSpace.empty_table; |
|
10 |
val copy = I; |
|
11 |
val extend = I; |
|
12 |
fun merge _ tables : T = NameSpace.merge_tables (eq_snd (op =)) tables handle Symtab.DUP dup => |
|
13 |
error ("Attempt to merge different versions of ML chunk " ^ quote dup); |
|
14 |
); |
|
15 |
||
16 |
fun declare_chunk name thy = |
|
55 | 17 |
(Sign.full_bname thy name, |
18 |
ChunkData.map (snd o NameSpace.bind (Sign.naming_of thy) |
|
19 |
(Binding.name name, ([], stamp ()))) thy |
|
24 | 20 |
handle Symtab.DUP _ => error ("Duplicate definition of ML chunk " ^ quote name)); |
21 |
||
22 |
fun augment_chunk tok name = |
|
23 |
ChunkData.map (apsnd (Symtab.map_entry name (apfst (cons tok)))); |
|
24 |
||
25 |
fun the_chunk thy name = |
|
26 |
let val (space, tab) = ChunkData.get thy |
|
27 |
in case Symtab.lookup tab (NameSpace.intern space name) of |
|
28 |
NONE => error ("Unknown ML chunk " ^ quote name) |
|
29 |
| SOME (toks, _) => rev toks |
|
30 |
end; |
|
31 |
||
32 |
||
33 |
(** parsers **) |
|
34 |
||
35 |
val not_eof = ML_Lex.stopper |> Scan.is_stopper #> not; |
|
36 |
||
37 |
val scan_space = Scan.many Symbol.is_blank; |
|
38 |
||
39 |
fun scan_cmt scan = |
|
40 |
Scan.one (ML_Lex.kind_of #> equal ML_Lex.Comment) >> |
|
41 |
(ML_Lex.content_of #> Symbol.explode #> Scan.finite Symbol.stopper scan #> fst); |
|
42 |
||
43 |
val scan_chunks = Scan.depend (fn (open_chunks, thy) => |
|
44 |
scan_cmt ((Scan.this_string "(*" -- scan_space -- Scan.this_string "@chunk" -- |
|
45 |
scan_space) |-- Symbol.scan_id --| |
|
46 |
(scan_space -- Scan.this_string "*)") >> (fn name => |
|
47 |
let val (name', thy') = declare_chunk name thy |
|
48 |
in ((name' :: open_chunks, thy'), "") end)) |
|
49 |
|| |
|
50 |
scan_cmt (Scan.this_string "(*" -- scan_space -- Scan.this_string "@end" -- |
|
51 |
scan_space -- Scan.this_string "*)" >> (fn _ => |
|
52 |
(case open_chunks of |
|
53 |
[] => error "end without matching chunk encountered" |
|
54 |
| _ :: open_chunks' => ((open_chunks', thy), "")))) |
|
55 |
|| |
|
56 |
Scan.one not_eof >> (fn tok => |
|
57 |
((open_chunks, fold (augment_chunk tok) open_chunks thy), ML_Lex.content_of tok))); |
|
58 |
||
59 |
fun chunks_of s thy = |
|
60 |
let val toks = Source.exhaust (ML_Lex.source (Source.of_string s)) |
|
61 |
in case Scan.finite' ML_Lex.stopper (Scan.repeat (Scan.error scan_chunks)) |
|
62 |
(([], thy), toks) of |
|
63 |
(toks', (([], thy'), _)) => (implode toks', thy') |
|
64 |
| (_, ((open_chunks, _), _)) => |
|
65 |
error ("Open chunks at end of text: " ^ commas_quote open_chunks) |
|
66 |
end; |
|
67 |
||
68 |
||
69 |
(** use command **) |
|
70 |
||
71 |
fun load_ml dir raw_path thy = |
|
72 |
(case ThyLoad.check_ml dir raw_path of |
|
73 |
NONE => error ("Could not find ML file " ^ quote (Path.implode raw_path)) |
|
74 |
| SOME (path, _) => |
|
75 |
let val (s, thy') = chunks_of (File.read path) thy |
|
76 |
in |
|
77 |
Context.theory_map (ThyInfo.exec_file false raw_path) thy' |
|
78 |
end); |
|
79 |
||
80 |
fun use_chunks path thy = |
|
81 |
load_ml (ThyInfo.master_directory (Context.theory_name thy)) path thy; |
|
82 |
||
83 |
val _ = |
|
84 |
OuterSyntax.command "use_chunks" "eval ML text from file" |
|
85 |
(OuterKeyword.tag_ml OuterKeyword.thy_decl) |
|
86 |
(OuterParse.path >> (Toplevel.theory o use_chunks)); |
|
87 |
||
88 |
||
89 |
(** antiquotation **) |
|
90 |
||
91 |
val str_of_source = space_implode " " o map OuterLex.unparse o #2 o #1 o Args.dest_src; |
|
92 |
||
93 |
val verbatim = space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|"; |
|
94 |
||
95 |
val transform_cmts = |
|
96 |
Scan.finite ML_Lex.stopper (Scan.repeat |
|
97 |
( scan_cmt |
|
98 |
(Scan.this_string "(*{" |-- |
|
99 |
Scan.repeat (Scan.unless (Scan.this_string "}*)") |
|
100 |
(Scan.one (not o Symbol.is_eof))) --| |
|
101 |
Scan.this_string "}*)" >> |
|
57
065f472c09ab
Repaired output of marginal comments in ML antiquotation.
berghofe
parents:
55
diff
changeset
|
102 |
(Symbol.encode_raw o enclose "\\hfill\\textrm{\\textit{" "}}" o implode)) |
24 | 103 |
|| Scan.one not_eof >> ML_Lex.content_of)) #> |
104 |
fst; |
|
105 |
||
106 |
fun output_chunk src ctxt name = |
|
107 |
let |
|
108 |
val toks = the_chunk (ProofContext.theory_of ctxt) name; |
|
109 |
val (spc, toks') = (case toks of |
|
110 |
[] => ("", []) |
|
111 |
| [tok] => ("", [tok]) |
|
112 |
| tok :: toks' => |
|
113 |
let |
|
114 |
val (toks'', tok') = split_last toks' |
|
115 |
val toks''' = |
|
116 |
if ML_Lex.kind_of tok' = ML_Lex.Space then toks'' |
|
117 |
else toks' |
|
118 |
in |
|
119 |
if ML_Lex.kind_of tok = ML_Lex.Space then |
|
120 |
(tok |> ML_Lex.content_of |> Symbol.explode |> |
|
121 |
take_suffix (equal " ") |> snd |> implode, |
|
122 |
toks''') |
|
123 |
else ("", tok :: toks''') |
|
124 |
end) |
|
125 |
val txt = spc ^ implode (transform_cmts toks') |
|
126 |
in |
|
57
065f472c09ab
Repaired output of marginal comments in ML antiquotation.
berghofe
parents:
55
diff
changeset
|
127 |
txt |> split_lines |> |
065f472c09ab
Repaired output of marginal comments in ML antiquotation.
berghofe
parents:
55
diff
changeset
|
128 |
ThyOutput.output_list (fn _ => fn s => Pretty.str s) src ctxt |
24 | 129 |
end; |
130 |
||
131 |
val _ = ThyOutput.add_commands |
|
132 |
[("ML_chunk", ThyOutput.args (Scan.lift Args.name) output_chunk)]; |
|
133 |
||
134 |
end; |