wenzelm@50000
|
1 |
(* Title: Doc/antiquote_setup.ML
|
wenzelm@21375
|
2 |
Author: Makarius
|
wenzelm@21375
|
3 |
|
wenzelm@26742
|
4 |
Auxiliary antiquotations for the Isabelle manuals.
|
wenzelm@21375
|
5 |
*)
|
wenzelm@21375
|
6 |
|
wenzelm@44450
|
7 |
signature ANTIQUOTE_SETUP =
|
wenzelm@44450
|
8 |
sig
|
wenzelm@44450
|
9 |
val setup: theory -> theory
|
wenzelm@44450
|
10 |
end;
|
wenzelm@44450
|
11 |
|
wenzelm@44450
|
12 |
structure Antiquote_Setup: ANTIQUOTE_SETUP =
|
wenzelm@26742
|
13 |
struct
|
wenzelm@21375
|
14 |
|
wenzelm@26742
|
15 |
(* misc utils *)
|
wenzelm@26742
|
16 |
|
wenzelm@30062
|
17 |
fun translate f = Symbol.explode #> map f #> implode;
|
wenzelm@30062
|
18 |
|
wenzelm@30062
|
19 |
val clean_string = translate
|
wenzelm@26853
|
20 |
(fn "_" => "\\_"
|
wenzelm@30120
|
21 |
| "#" => "\\#"
|
wenzelm@26897
|
22 |
| "<" => "$<$"
|
wenzelm@26768
|
23 |
| ">" => "$>$"
|
wenzelm@26768
|
24 |
| "{" => "\\{"
|
wenzelm@30120
|
25 |
| "|" => "$\\mid$"
|
wenzelm@26768
|
26 |
| "}" => "\\}"
|
wenzelm@43539
|
27 |
| "\<hyphen>" => "-"
|
wenzelm@26768
|
28 |
| c => c);
|
wenzelm@26751
|
29 |
|
wenzelm@31549
|
30 |
fun clean_name "\<dots>" = "dots"
|
wenzelm@26897
|
31 |
| clean_name ".." = "ddot"
|
wenzelm@26897
|
32 |
| clean_name "." = "dot"
|
wenzelm@26910
|
33 |
| clean_name "_" = "underscore"
|
wenzelm@26897
|
34 |
| clean_name "{" = "braceleft"
|
wenzelm@26897
|
35 |
| clean_name "}" = "braceright"
|
wenzelm@43539
|
36 |
| clean_name s = s |> translate (fn "_" => "-" | "\<hyphen>" => "-" | c => c);
|
wenzelm@26897
|
37 |
|
wenzelm@26742
|
38 |
|
wenzelm@26742
|
39 |
(* verbatim text *)
|
wenzelm@26742
|
40 |
|
wenzelm@26742
|
41 |
val verbatim = space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|";
|
wenzelm@26742
|
42 |
|
wenzelm@44450
|
43 |
val verbatim_setup =
|
wenzelm@44450
|
44 |
Thy_Output.antiquotation @{binding verbatim} (Scan.lift Args.name)
|
wenzelm@44450
|
45 |
(K (split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"));
|
wenzelm@26742
|
46 |
|
wenzelm@26742
|
47 |
|
wenzelm@26742
|
48 |
(* ML text *)
|
wenzelm@26742
|
49 |
|
wenzelm@26742
|
50 |
local
|
wenzelm@26742
|
51 |
|
wenzelm@21375
|
52 |
fun ml_val (txt1, "") = "fn _ => (" ^ txt1 ^ ");"
|
wenzelm@40297
|
53 |
| ml_val (txt1, txt2) = "fn _ => (" ^ txt1 ^ " : " ^ txt2 ^ ");";
|
wenzelm@21375
|
54 |
|
wenzelm@47292
|
55 |
fun ml_op (txt1, "") = "fn _ => (op " ^ txt1 ^ ");"
|
wenzelm@47292
|
56 |
| ml_op (txt1, txt2) = "fn _ => (op " ^ txt1 ^ " : " ^ txt2 ^ ");";
|
wenzelm@47292
|
57 |
|
wenzelm@21375
|
58 |
fun ml_type (txt1, "") = "val _ = NONE : (" ^ txt1 ^ ") option;"
|
wenzelm@21375
|
59 |
| ml_type (txt1, txt2) = "val _ = [NONE : (" ^ txt1 ^ ") option, NONE : (" ^ txt2 ^ ") option];";
|
haftmann@22289
|
60 |
|
wenzelm@40297
|
61 |
fun ml_exn (txt1, "") = "fn _ => (" ^ txt1 ^ " : exn);"
|
wenzelm@40297
|
62 |
| ml_exn (txt1, txt2) = "fn _ => (" ^ txt1 ^ " : " ^ txt2 ^ " -> exn);";
|
haftmann@22289
|
63 |
|
wenzelm@30394
|
64 |
fun ml_structure (txt, _) = "functor XXX() = struct structure XX = " ^ txt ^ " end;";
|
wenzelm@21375
|
65 |
|
wenzelm@36163
|
66 |
fun ml_functor (txt, _) = "ML_Env.check_functor " ^ ML_Syntax.print_string txt;
|
wenzelm@21375
|
67 |
|
wenzelm@40308
|
68 |
val is_name = ML_Lex.kind_of #> (fn kind => kind = ML_Lex.Ident orelse kind = ML_Lex.LongIdent);
|
wenzelm@40308
|
69 |
|
wenzelm@40308
|
70 |
fun ml_name txt =
|
wenzelm@40308
|
71 |
(case filter is_name (ML_Lex.tokenize txt) of
|
wenzelm@40308
|
72 |
toks as [_] => ML_Lex.flatten toks
|
wenzelm@40308
|
73 |
| _ => error ("Single ML name expected in input: " ^ quote txt));
|
wenzelm@40308
|
74 |
|
wenzelm@37216
|
75 |
fun index_ml name kind ml = Thy_Output.antiquotation name
|
wenzelm@30394
|
76 |
(Scan.lift (Args.name -- Scan.optional (Args.colon |-- Args.name) ""))
|
wenzelm@30394
|
77 |
(fn {context = ctxt, ...} => fn (txt1, txt2) =>
|
wenzelm@30394
|
78 |
let
|
wenzelm@30394
|
79 |
val txt =
|
wenzelm@30394
|
80 |
if txt2 = "" then txt1
|
wenzelm@30394
|
81 |
else if kind = "type" then txt1 ^ " = " ^ txt2
|
wenzelm@30394
|
82 |
else if kind = "exception" then txt1 ^ " of " ^ txt2
|
wenzelm@51254
|
83 |
else if Symbol_Pos.is_identifier (Long_Name.base_name (ml_name txt1))
|
wenzelm@43162
|
84 |
then txt1 ^ ": " ^ txt2
|
wenzelm@40297
|
85 |
else txt1 ^ " : " ^ txt2;
|
wenzelm@30394
|
86 |
val txt' = if kind = "" then txt else kind ^ " " ^ txt;
|
wenzelm@37199
|
87 |
val _ = ML_Context.eval_text_in (SOME ctxt) false Position.none (ml (txt1, txt2)); (* ML_Lex.read (!?) *)
|
wenzelm@30394
|
88 |
val kind' = if kind = "" then "ML" else "ML " ^ kind;
|
wenzelm@30394
|
89 |
in
|
wenzelm@40308
|
90 |
"\\indexdef{}{" ^ kind' ^ "}{" ^ clean_string (ml_name txt1) ^ "}" ^
|
wenzelm@30394
|
91 |
(txt'
|
wenzelm@39042
|
92 |
|> (if Config.get ctxt Thy_Output.quotes then quote else I)
|
wenzelm@39042
|
93 |
|> (if Config.get ctxt Thy_Output.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
|
wenzelm@30394
|
94 |
else split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))
|
wenzelm@30394
|
95 |
end);
|
wenzelm@21375
|
96 |
|
wenzelm@26742
|
97 |
in
|
wenzelm@21375
|
98 |
|
wenzelm@44450
|
99 |
val index_ml_setup =
|
wenzelm@44450
|
100 |
index_ml @{binding index_ML} "" ml_val #>
|
wenzelm@47292
|
101 |
index_ml @{binding index_ML_op} "infix" ml_op #>
|
wenzelm@44450
|
102 |
index_ml @{binding index_ML_type} "type" ml_type #>
|
wenzelm@44450
|
103 |
index_ml @{binding index_ML_exn} "exception" ml_exn #>
|
wenzelm@44450
|
104 |
index_ml @{binding index_ML_structure} "structure" ml_structure #>
|
wenzelm@44450
|
105 |
index_ml @{binding index_ML_functor} "functor" ml_functor;
|
wenzelm@26742
|
106 |
|
wenzelm@26742
|
107 |
end;
|
wenzelm@21375
|
108 |
|
berghofe@23846
|
109 |
|
wenzelm@30394
|
110 |
(* named theorems *)
|
berghofe@23846
|
111 |
|
wenzelm@44450
|
112 |
val named_thms_setup =
|
wenzelm@44450
|
113 |
Thy_Output.antiquotation @{binding named_thms}
|
wenzelm@44450
|
114 |
(Scan.repeat (Attrib.thm -- Scan.lift (Args.parens Args.name)))
|
wenzelm@44450
|
115 |
(fn {context = ctxt, ...} =>
|
wenzelm@44450
|
116 |
map (apfst (Thy_Output.pretty_thm ctxt))
|
wenzelm@44450
|
117 |
#> (if Config.get ctxt Thy_Output.quotes then map (apfst Pretty.quote) else I)
|
wenzelm@44450
|
118 |
#> (if Config.get ctxt Thy_Output.display
|
wenzelm@44450
|
119 |
then
|
wenzelm@44450
|
120 |
map (fn (p, name) =>
|
wenzelm@44450
|
121 |
Output.output (Pretty.string_of (Pretty.indent (Config.get ctxt Thy_Output.indent) p)) ^
|
wenzelm@44450
|
122 |
"\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}")
|
wenzelm@44450
|
123 |
#> space_implode "\\par\\smallskip%\n"
|
wenzelm@44450
|
124 |
#> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
|
wenzelm@44450
|
125 |
else
|
wenzelm@44450
|
126 |
map (fn (p, name) =>
|
wenzelm@44450
|
127 |
Output.output (Pretty.str_of p) ^
|
wenzelm@44450
|
128 |
"\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}")
|
wenzelm@44450
|
129 |
#> space_implode "\\par\\smallskip%\n"
|
wenzelm@44450
|
130 |
#> enclose "\\isa{" "}"));
|
berghofe@23846
|
131 |
|
berghofe@23846
|
132 |
|
wenzelm@30394
|
133 |
(* theory file *)
|
wenzelm@21375
|
134 |
|
wenzelm@44450
|
135 |
val thy_file_setup =
|
wenzelm@44450
|
136 |
Thy_Output.antiquotation @{binding thy_file} (Scan.lift Args.name)
|
wenzelm@44450
|
137 |
(fn {context = ctxt, ...} =>
|
wenzelm@49914
|
138 |
fn name => (Thy_Load.check_thy Path.current name; Thy_Output.output ctxt [Pretty.str name]));
|
wenzelm@21375
|
139 |
|
wenzelm@26742
|
140 |
|
wenzelm@30394
|
141 |
(* Isabelle/Isar entities (with index) *)
|
wenzelm@26751
|
142 |
|
wenzelm@26751
|
143 |
local
|
wenzelm@26751
|
144 |
|
wenzelm@26894
|
145 |
fun no_check _ _ = true;
|
wenzelm@26894
|
146 |
|
wenzelm@26894
|
147 |
fun thy_check intern defined ctxt =
|
wenzelm@43232
|
148 |
let val thy = Proof_Context.theory_of ctxt
|
wenzelm@26894
|
149 |
in defined thy o intern thy end;
|
wenzelm@26894
|
150 |
|
wenzelm@28237
|
151 |
fun check_tool name =
|
wenzelm@48696
|
152 |
let val tool_dirs = map Path.explode ["~~/lib/Tools", "~~/src/Tools/jEdit/lib/Tools"]
|
wenzelm@48696
|
153 |
in exists (fn dir => File.exists (Path.append dir (Path.basic name))) tool_dirs end;
|
wenzelm@28237
|
154 |
|
wenzelm@26751
|
155 |
val arg = enclose "{" "}" o clean_string;
|
wenzelm@26751
|
156 |
|
wenzelm@30394
|
157 |
fun entity check markup kind index =
|
wenzelm@37216
|
158 |
Thy_Output.antiquotation
|
wenzelm@44450
|
159 |
(Binding.name (translate (fn " " => "_" | c => c) kind ^
|
wenzelm@44450
|
160 |
(case index of NONE => "" | SOME true => "_def" | SOME false => "_ref")))
|
wenzelm@30394
|
161 |
(Scan.lift (Scan.optional (Args.parens Args.name) "" -- Args.name))
|
wenzelm@30394
|
162 |
(fn {context = ctxt, ...} => fn (logic, name) =>
|
wenzelm@30394
|
163 |
let
|
wenzelm@30394
|
164 |
val hyper_name =
|
wenzelm@30394
|
165 |
"{" ^ Long_Name.append kind (Long_Name.append logic (clean_name name)) ^ "}";
|
wenzelm@30394
|
166 |
val hyper =
|
wenzelm@30394
|
167 |
enclose ("\\hyperlink" ^ hyper_name ^ "{") "}" #>
|
wenzelm@30394
|
168 |
index = SOME true ? enclose ("\\hypertarget" ^ hyper_name ^ "{") "}";
|
wenzelm@30394
|
169 |
val idx =
|
wenzelm@30394
|
170 |
(case index of
|
wenzelm@30394
|
171 |
NONE => ""
|
wenzelm@30394
|
172 |
| SOME is_def =>
|
wenzelm@30394
|
173 |
"\\index" ^ (if is_def then "def" else "ref") ^ arg logic ^ arg kind ^ arg name);
|
wenzelm@30394
|
174 |
in
|
wenzelm@30394
|
175 |
if check ctxt name then
|
wenzelm@30394
|
176 |
idx ^
|
wenzelm@30394
|
177 |
(Output.output name
|
wenzelm@30394
|
178 |
|> (if markup = "" then I else enclose ("\\" ^ markup ^ "{") "}")
|
wenzelm@39042
|
179 |
|> (if Config.get ctxt Thy_Output.quotes then quote else I)
|
wenzelm@39042
|
180 |
|> (if Config.get ctxt Thy_Output.display
|
wenzelm@39042
|
181 |
then enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
|
wenzelm@30394
|
182 |
else hyper o enclose "\\mbox{\\isa{" "}}"))
|
wenzelm@30394
|
183 |
else error ("Bad " ^ kind ^ " " ^ quote name)
|
wenzelm@30394
|
184 |
end);
|
wenzelm@26897
|
185 |
|
wenzelm@26894
|
186 |
fun entity_antiqs check markup kind =
|
wenzelm@44450
|
187 |
entity check markup kind NONE #>
|
wenzelm@44450
|
188 |
entity check markup kind (SOME true) #>
|
wenzelm@44450
|
189 |
entity check markup kind (SOME false);
|
wenzelm@26751
|
190 |
|
wenzelm@26751
|
191 |
in
|
wenzelm@26751
|
192 |
|
wenzelm@44450
|
193 |
val entity_setup =
|
wenzelm@44450
|
194 |
entity_antiqs no_check "" "syntax" #>
|
wenzelm@44450
|
195 |
entity_antiqs (K (is_some o Keyword.command_keyword)) "isacommand" "command" #>
|
wenzelm@44450
|
196 |
entity_antiqs (K Keyword.is_keyword) "isakeyword" "keyword" #>
|
wenzelm@44450
|
197 |
entity_antiqs (K Keyword.is_keyword) "isakeyword" "element" #>
|
wenzelm@44450
|
198 |
entity_antiqs (thy_check Method.intern Method.defined) "" "method" #>
|
wenzelm@44450
|
199 |
entity_antiqs (thy_check Attrib.intern Attrib.defined) "" "attribute" #>
|
wenzelm@44450
|
200 |
entity_antiqs no_check "" "fact" #>
|
wenzelm@44450
|
201 |
entity_antiqs no_check "" "variable" #>
|
wenzelm@44450
|
202 |
entity_antiqs no_check "" "case" #>
|
wenzelm@44450
|
203 |
entity_antiqs (thy_check Thy_Output.intern_command Thy_Output.defined_command)
|
wenzelm@44450
|
204 |
"" "antiquotation" #>
|
wenzelm@44450
|
205 |
entity_antiqs (thy_check Thy_Output.intern_option Thy_Output.defined_option)
|
wenzelm@44450
|
206 |
"" "antiquotation option" #>
|
wenzelm@49569
|
207 |
entity_antiqs no_check "isatt" "setting" #>
|
wenzelm@49593
|
208 |
entity_antiqs no_check "isatt" "system option" #>
|
wenzelm@44450
|
209 |
entity_antiqs no_check "" "inference" #>
|
wenzelm@44450
|
210 |
entity_antiqs no_check "isatt" "executable" #>
|
wenzelm@49617
|
211 |
entity_antiqs (K check_tool) "isatool" "tool" #>
|
wenzelm@44450
|
212 |
entity_antiqs (thy_check ML_Context.intern_antiq ML_Context.defined_antiq)
|
wenzelm@51216
|
213 |
"" Markup.ML_antiquotationN;
|
wenzelm@26751
|
214 |
|
wenzelm@26742
|
215 |
end;
|
wenzelm@26751
|
216 |
|
wenzelm@44450
|
217 |
|
wenzelm@44450
|
218 |
(* theory setup *)
|
wenzelm@44450
|
219 |
|
wenzelm@44450
|
220 |
val setup =
|
wenzelm@44450
|
221 |
verbatim_setup #>
|
wenzelm@44450
|
222 |
index_ml_setup #>
|
wenzelm@44450
|
223 |
named_thms_setup #>
|
wenzelm@44450
|
224 |
thy_file_setup #>
|
wenzelm@44450
|
225 |
entity_setup;
|
wenzelm@44450
|
226 |
|
wenzelm@26751
|
227 |
end;
|