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