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