author | haftmann |
Mon, 29 Sep 2008 12:31:56 +0200 | |
changeset 28399 | b11b1ca701e5 |
parent 28273 | 17f6aa02ded3 |
child 28644 | e2ae4a6cf166 |
permissions | -rw-r--r-- |
24584 | 1 |
(* Title: Doc/antiquote_setup.ML |
21375 | 2 |
ID: $Id$ |
3 |
Author: Makarius |
|
4 |
||
26742 | 5 |
Auxiliary antiquotations for the Isabelle manuals. |
21375 | 6 |
*) |
7 |
||
26742 | 8 |
structure AntiquoteSetup: sig end = |
9 |
struct |
|
21375 | 10 |
|
22094 | 11 |
structure O = ThyOutput; |
21375 | 12 |
|
26742 | 13 |
|
14 |
(* misc utils *) |
|
15 |
||
26768 | 16 |
val clean_string = translate_string |
26853
52cb0e965041
clean_string: map "_" to "\\_" (best used with underscore.sty);
wenzelm
parents:
26843
diff
changeset
|
17 |
(fn "_" => "\\_" |
26897 | 18 |
| "<" => "$<$" |
26768 | 19 |
| ">" => "$>$" |
20 |
| "#" => "\\#" |
|
21 |
| "{" => "\\{" |
|
22 |
| "}" => "\\}" |
|
23 |
| c => c); |
|
26751 | 24 |
|
26897 | 25 |
fun clean_name "\\<dots>" = "dots" |
26 |
| clean_name ".." = "ddot" |
|
27 |
| clean_name "." = "dot" |
|
26910 | 28 |
| clean_name "_" = "underscore" |
26897 | 29 |
| clean_name "{" = "braceleft" |
30 |
| clean_name "}" = "braceright" |
|
26903 | 31 |
| clean_name s = s |> translate_string (fn "_" => "-" | c => c); |
26897 | 32 |
|
27809
a1e409db516b
unified Args.T with OuterLex.token, renamed some operations;
wenzelm
parents:
27353
diff
changeset
|
33 |
val str_of_source = space_implode " " o map OuterLex.unparse o #2 o #1 o Args.dest_src; |
21375 | 34 |
|
26742 | 35 |
fun tweak_line s = |
36 |
if ! O.display then s else Symbol.strip_blanks s; |
|
37 |
||
38 |
val pretty_text = Pretty.chunks o map Pretty.str o map tweak_line o Library.split_lines; |
|
39 |
||
40 |
fun pretty_term ctxt t = Syntax.pretty_term (Variable.auto_fixes t ctxt) t; |
|
41 |
fun pretty_thm ctxt = pretty_term ctxt o Thm.full_prop_of; |
|
42 |
||
43 |
||
44 |
(* verbatim text *) |
|
45 |
||
46 |
val verbatim = space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|"; |
|
47 |
||
48 |
val _ = O.add_commands |
|
49 |
[("verbatim", O.args (Scan.lift Args.name) (fn _ => fn _ => |
|
50 |
split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))]; |
|
51 |
||
52 |
||
53 |
(* ML text *) |
|
54 |
||
55 |
local |
|
56 |
||
21375 | 57 |
fun ml_val (txt1, "") = "fn _ => (" ^ txt1 ^ ");" |
58 |
| ml_val (txt1, txt2) = "fn _ => (" ^ txt1 ^ ": " ^ txt2 ^ ");"; |
|
59 |
||
60 |
fun ml_type (txt1, "") = "val _ = NONE : (" ^ txt1 ^ ") option;" |
|
61 |
| ml_type (txt1, txt2) = "val _ = [NONE : (" ^ txt1 ^ ") option, NONE : (" ^ txt2 ^ ") option];"; |
|
22289 | 62 |
|
23651 | 63 |
fun ml_exn (txt1, "") = "fn _ => (" ^ txt1 ^ ": exn);" |
64 |
| ml_exn (txt1, txt2) = "fn _ => (" ^ txt1 ^ ": " ^ txt2 ^ " -> exn);"; |
|
22289 | 65 |
|
21375 | 66 |
fun ml_structure (txt, _) = "functor DUMMY_FUNCTOR() = struct structure DUMMY = " ^ txt ^ " end;" |
67 |
||
68 |
fun ml_functor _ = "val _ = ();"; (*no check!*) |
|
69 |
||
70 |
fun index_ml kind ml src ctxt (txt1, txt2) = |
|
71 |
let |
|
72 |
val txt = if txt2 = "" then txt1 else |
|
26742 | 73 |
if kind = "type" then txt1 ^ " = " ^ txt2 |
74 |
else if kind = "exception" then txt1 ^ " of " ^ txt2 |
|
22289 | 75 |
else txt1 ^ ": " ^ txt2; |
21375 | 76 |
val txt' = if kind = "" then txt else kind ^ " " ^ txt; |
22289 | 77 |
val _ = writeln (ml (txt1, txt2)); |
28273
17f6aa02ded3
simplified ML_Context.eval_in -- expect immutable Proof.context value;
wenzelm
parents:
28237
diff
changeset
|
78 |
val _ = ML_Context.eval_in (SOME ctxt) false Position.none (ml (txt1, txt2)); |
21375 | 79 |
in |
26751 | 80 |
"\\indexml" ^ kind ^ enclose "{" "}" (clean_string txt1) ^ |
21375 | 81 |
((if ! O.source then str_of_source src else txt') |
82 |
|> (if ! O.quotes then quote else I) |
|
83 |
|> (if ! O.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}" |
|
26751 | 84 |
else split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n")) |
21375 | 85 |
end; |
86 |
||
26461 | 87 |
fun output_ml ctxt src = |
88 |
if ! O.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}" |
|
26742 | 89 |
else |
26461 | 90 |
split_lines |
91 |
#> map (space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|") |
|
92 |
#> space_implode "\\isasep\\isanewline%\n"; |
|
93 |
||
26742 | 94 |
fun ml_args x = O.args (Scan.lift (Args.name -- Scan.optional (Args.colon |-- Args.name) "")) x; |
95 |
||
96 |
in |
|
21375 | 97 |
|
26742 | 98 |
val _ = O.add_commands |
99 |
[("index_ML", ml_args (index_ml "" ml_val)), |
|
100 |
("index_ML_type", ml_args (index_ml "type" ml_type)), |
|
101 |
("index_ML_exn", ml_args (index_ml "exception" ml_exn)), |
|
102 |
("index_ML_structure", ml_args (index_ml "structure" ml_structure)), |
|
103 |
("index_ML_functor", ml_args (index_ml "functor" ml_functor)), |
|
104 |
("ML_functor", O.args (Scan.lift Args.name) output_ml), |
|
105 |
("ML_text", O.args (Scan.lift Args.name) output_ml)]; |
|
21375 | 106 |
|
26742 | 107 |
end; |
21375 | 108 |
|
23846 | 109 |
|
110 |
(* theorems with names *) |
|
111 |
||
26742 | 112 |
local |
23846 | 113 |
|
26742 | 114 |
fun output_named_thms src ctxt xs = |
115 |
map (apfst (pretty_thm ctxt)) xs (*always pretty in order to exhibit errors!*) |
|
23846 | 116 |
|> (if ! O.quotes then map (apfst Pretty.quote) else I) |
117 |
|> (if ! O.display then |
|
118 |
map (fn (p, name) => |
|
119 |
Output.output (Pretty.string_of (Pretty.indent (! O.indent) p)) ^ |
|
120 |
"\\rulename{" ^ Output.output (Pretty.str_of (pretty_text name)) ^ "}") |
|
121 |
#> space_implode "\\par\\smallskip%\n" |
|
122 |
#> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}" |
|
123 |
else |
|
124 |
map (fn (p, name) => |
|
125 |
Output.output (Pretty.str_of p) ^ |
|
126 |
"\\rulename{" ^ Output.output (Pretty.str_of (pretty_text name)) ^ "}") |
|
127 |
#> space_implode "\\par\\smallskip%\n" |
|
128 |
#> enclose "\\isa{" "}"); |
|
129 |
||
21375 | 130 |
in |
131 |
||
132 |
val _ = O.add_commands |
|
26742 | 133 |
[("named_thms", O.args (Scan.repeat (Attrib.thm -- |
26751 | 134 |
Scan.lift (Args.parens Args.name))) output_named_thms)]; |
21375 | 135 |
|
136 |
end; |
|
26742 | 137 |
|
138 |
||
139 |
(* theory files *) |
|
140 |
||
141 |
val _ = O.add_commands |
|
142 |
[("thy_file", O.args (Scan.lift Args.name) (O.output (fn _ => fn name => |
|
143 |
(ThyLoad.check_thy Path.current name; Pretty.str name))))]; |
|
144 |
||
26751 | 145 |
|
28218 | 146 |
(* Isabelle entities (with index) *) |
26751 | 147 |
|
148 |
local |
|
149 |
||
26894 | 150 |
fun no_check _ _ = true; |
151 |
||
152 |
fun thy_check intern defined ctxt = |
|
153 |
let val thy = ProofContext.theory_of ctxt |
|
154 |
in defined thy o intern thy end; |
|
155 |
||
28237 | 156 |
fun check_tool name = |
157 |
File.exists (Path.append (Path.explode "~~/lib/Tools") (Path.basic name)); |
|
158 |
||
26751 | 159 |
val arg = enclose "{" "}" o clean_string; |
160 |
||
26894 | 161 |
fun output_entity check markup index kind ctxt (logic, name) = |
26897 | 162 |
let |
163 |
val hyper_name = "{" ^ NameSpace.append kind (NameSpace.append logic (clean_name name)) ^ "}"; |
|
164 |
val hyper = |
|
165 |
enclose ("\\hyperlink" ^ hyper_name ^ "{") "}" #> |
|
166 |
index = SOME true ? enclose ("\\hypertarget" ^ hyper_name ^ "{") "}"; |
|
167 |
val idx = |
|
168 |
(case index of |
|
169 |
NONE => "" |
|
170 |
| SOME is_def => |
|
171 |
"\\index" ^ (if is_def then "def" else "ref") ^ arg logic ^ arg kind ^ arg name); |
|
172 |
in |
|
173 |
if check ctxt name then |
|
174 |
idx ^ |
|
175 |
(Output.output name |
|
176 |
|> (if markup = "" then I else enclose ("\\" ^ markup ^ "{") "}") |
|
177 |
|> (if ! O.quotes then quote else I) |
|
178 |
|> (if ! O.display then enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}" |
|
179 |
else hyper o enclose "\\mbox{\\isa{" "}}")) |
|
28237 | 180 |
else error ("Bad " ^ kind ^ " " ^ quote name) |
26897 | 181 |
end; |
26751 | 182 |
|
26894 | 183 |
fun entity check markup index kind = |
26751 | 184 |
O.args (Scan.lift (Scan.optional (Args.parens Args.name) "" -- Args.name)) |
26894 | 185 |
(K (output_entity check markup index kind)); |
26897 | 186 |
|
26894 | 187 |
fun entity_antiqs check markup kind = |
188 |
[(kind, entity check markup NONE kind), |
|
26897 | 189 |
(kind ^ "_def", entity check markup (SOME true) kind), |
26894 | 190 |
(kind ^ "_ref", entity check markup (SOME false) kind)]; |
26751 | 191 |
|
192 |
in |
|
193 |
||
194 |
val _ = O.add_commands |
|
26894 | 195 |
(entity_antiqs no_check "" "syntax" @ |
27353
71c4dd53d4cb
moved global keywords from OuterSyntax to OuterKeyword, tuned interfaces;
wenzelm
parents:
26910
diff
changeset
|
196 |
entity_antiqs (K (is_some o OuterKeyword.command_keyword)) "isacommand" "command" @ |
71c4dd53d4cb
moved global keywords from OuterSyntax to OuterKeyword, tuned interfaces;
wenzelm
parents:
26910
diff
changeset
|
197 |
entity_antiqs (K OuterKeyword.is_keyword) "isakeyword" "keyword" @ |
71c4dd53d4cb
moved global keywords from OuterSyntax to OuterKeyword, tuned interfaces;
wenzelm
parents:
26910
diff
changeset
|
198 |
entity_antiqs (K OuterKeyword.is_keyword) "isakeyword" "element" @ |
26894 | 199 |
entity_antiqs (thy_check Method.intern Method.defined) "" "method" @ |
200 |
entity_antiqs (thy_check Attrib.intern Attrib.defined) "" "attribute" @ |
|
201 |
entity_antiqs no_check "" "fact" @ |
|
202 |
entity_antiqs no_check "" "variable" @ |
|
203 |
entity_antiqs no_check "" "case" @ |
|
28217
21f0c2de0a38
added formal markup for setting, executable, tool;
wenzelm
parents:
27809
diff
changeset
|
204 |
entity_antiqs (K ThyOutput.defined_command) "" "antiquotation" @ |
28237 | 205 |
entity_antiqs (fn _ => fn name => is_some (OS.Process.getEnv name)) "isatt" "setting" @ |
28217
21f0c2de0a38
added formal markup for setting, executable, tool;
wenzelm
parents:
27809
diff
changeset
|
206 |
entity_antiqs no_check "isatt" "executable" @ |
28237 | 207 |
entity_antiqs (K check_tool) "isatt" "tool" @ |
28399 | 208 |
entity_antiqs (K (File.exists o Path.explode)) "isatt" "file" @ |
209 |
entity_antiqs (K ThyInfo.known_thy) "" "theory"); |
|
26751 | 210 |
|
26742 | 211 |
end; |
26751 | 212 |
|
213 |
end; |