author | bulwahn |
Thu, 02 Jun 2011 10:13:46 +0200 | |
changeset 43149 | 9675d631df3d |
parent 42666 | fee67c099d03 |
child 43563 | aeabb735883a |
permissions | -rw-r--r-- |
30394 | 1 |
(* Title: doc-src/antiquote_setup.ML |
21375 | 2 |
Author: Makarius |
3 |
||
26742 | 4 |
Auxiliary antiquotations for the Isabelle manuals. |
21375 | 5 |
*) |
6 |
||
40801 | 7 |
structure Antiquote_Setup: sig end = |
26742 | 8 |
struct |
21375 | 9 |
|
26742 | 10 |
(* misc utils *) |
11 |
||
29736
ec3fc818b82e
clean_string/clean_name: proper treatment of \<dash>;
wenzelm
parents:
29726
diff
changeset
|
12 |
fun translate f = Symbol.explode #> map f #> implode; |
ec3fc818b82e
clean_string/clean_name: proper treatment of \<dash>;
wenzelm
parents:
29726
diff
changeset
|
13 |
|
ec3fc818b82e
clean_string/clean_name: proper treatment of \<dash>;
wenzelm
parents:
29726
diff
changeset
|
14 |
val clean_string = translate |
26853
52cb0e965041
clean_string: map "_" to "\\_" (best used with underscore.sty);
wenzelm
parents:
26843
diff
changeset
|
15 |
(fn "_" => "\\_" |
30120
aaa4667285c8
uniform treatment of ML indexing, using general \indexdef macro for formal Isabelle/Isar entities;
wenzelm
parents:
29736
diff
changeset
|
16 |
| "#" => "\\#" |
26897 | 17 |
| "<" => "$<$" |
26768 | 18 |
| ">" => "$>$" |
19 |
| "{" => "\\{" |
|
30120
aaa4667285c8
uniform treatment of ML indexing, using general \indexdef macro for formal Isabelle/Isar entities;
wenzelm
parents:
29736
diff
changeset
|
20 |
| "|" => "$\\mid$" |
26768 | 21 |
| "}" => "\\}" |
42666 | 22 |
| "\<hyphen>" => "-" |
26768 | 23 |
| c => c); |
26751 | 24 |
|
31546 | 25 |
fun clean_name "\<dots>" = "dots" |
26897 | 26 |
| clean_name ".." = "ddot" |
27 |
| clean_name "." = "dot" |
|
26910 | 28 |
| clean_name "_" = "underscore" |
26897 | 29 |
| clean_name "{" = "braceleft" |
30 |
| clean_name "}" = "braceright" |
|
42666 | 31 |
| clean_name s = s |> translate (fn "_" => "-" | "\<hyphen>" => "-" | c => c); |
26897 | 32 |
|
26742 | 33 |
|
34 |
(* verbatim text *) |
|
35 |
||
36 |
val verbatim = space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|"; |
|
37 |
||
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37198
diff
changeset
|
38 |
val _ = Thy_Output.antiquotation "verbatim" (Scan.lift Args.name) |
30382 | 39 |
(K (split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n")); |
26742 | 40 |
|
41 |
||
42 |
(* ML text *) |
|
43 |
||
44 |
local |
|
45 |
||
21375 | 46 |
fun ml_val (txt1, "") = "fn _ => (" ^ txt1 ^ ");" |
39858
5be7a57c3b4e
more robust treatment of symbolic indentifiers (which may contain colons);
wenzelm
parents:
39829
diff
changeset
|
47 |
| ml_val (txt1, txt2) = "fn _ => (" ^ txt1 ^ " : " ^ txt2 ^ ");"; |
21375 | 48 |
|
49 |
fun ml_type (txt1, "") = "val _ = NONE : (" ^ txt1 ^ ") option;" |
|
50 |
| ml_type (txt1, txt2) = "val _ = [NONE : (" ^ txt1 ^ ") option, NONE : (" ^ txt2 ^ ") option];"; |
|
22289 | 51 |
|
39858
5be7a57c3b4e
more robust treatment of symbolic indentifiers (which may contain colons);
wenzelm
parents:
39829
diff
changeset
|
52 |
fun ml_exn (txt1, "") = "fn _ => (" ^ txt1 ^ " : exn);" |
5be7a57c3b4e
more robust treatment of symbolic indentifiers (which may contain colons);
wenzelm
parents:
39829
diff
changeset
|
53 |
| ml_exn (txt1, txt2) = "fn _ => (" ^ txt1 ^ " : " ^ txt2 ^ " -> exn);"; |
22289 | 54 |
|
30394 | 55 |
fun ml_structure (txt, _) = "functor XXX() = struct structure XX = " ^ txt ^ " end;"; |
21375 | 56 |
|
36163
823c9400eb62
proper checking of ML functors (in Poly/ML 5.2 or later);
wenzelm
parents:
31546
diff
changeset
|
57 |
fun ml_functor (txt, _) = "ML_Env.check_functor " ^ ML_Syntax.print_string txt; |
21375 | 58 |
|
39869
c269f6bd0a1f
more robust index_ML antiquotations: guess name from text (affects infixes and type constructors in particular);
wenzelm
parents:
39858
diff
changeset
|
59 |
val is_name = ML_Lex.kind_of #> (fn kind => kind = ML_Lex.Ident orelse kind = ML_Lex.LongIdent); |
c269f6bd0a1f
more robust index_ML antiquotations: guess name from text (affects infixes and type constructors in particular);
wenzelm
parents:
39858
diff
changeset
|
60 |
|
c269f6bd0a1f
more robust index_ML antiquotations: guess name from text (affects infixes and type constructors in particular);
wenzelm
parents:
39858
diff
changeset
|
61 |
fun ml_name txt = |
c269f6bd0a1f
more robust index_ML antiquotations: guess name from text (affects infixes and type constructors in particular);
wenzelm
parents:
39858
diff
changeset
|
62 |
(case filter is_name (ML_Lex.tokenize txt) of |
c269f6bd0a1f
more robust index_ML antiquotations: guess name from text (affects infixes and type constructors in particular);
wenzelm
parents:
39858
diff
changeset
|
63 |
toks as [_] => ML_Lex.flatten toks |
c269f6bd0a1f
more robust index_ML antiquotations: guess name from text (affects infixes and type constructors in particular);
wenzelm
parents:
39858
diff
changeset
|
64 |
| _ => error ("Single ML name expected in input: " ^ quote txt)); |
c269f6bd0a1f
more robust index_ML antiquotations: guess name from text (affects infixes and type constructors in particular);
wenzelm
parents:
39858
diff
changeset
|
65 |
|
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37198
diff
changeset
|
66 |
fun index_ml name kind ml = Thy_Output.antiquotation name |
30394 | 67 |
(Scan.lift (Args.name -- Scan.optional (Args.colon |-- Args.name) "")) |
68 |
(fn {context = ctxt, ...} => fn (txt1, txt2) => |
|
69 |
let |
|
70 |
val txt = |
|
71 |
if txt2 = "" then txt1 |
|
72 |
else if kind = "type" then txt1 ^ " = " ^ txt2 |
|
73 |
else if kind = "exception" then txt1 ^ " of " ^ txt2 |
|
42290
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
40801
diff
changeset
|
74 |
else if Lexicon.is_identifier (Long_Name.base_name (ml_name txt1)) |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
40801
diff
changeset
|
75 |
then txt1 ^ ": " ^ txt2 |
39858
5be7a57c3b4e
more robust treatment of symbolic indentifiers (which may contain colons);
wenzelm
parents:
39829
diff
changeset
|
76 |
else txt1 ^ " : " ^ txt2; |
30394 | 77 |
val txt' = if kind = "" then txt else kind ^ " " ^ txt; |
37198
3af985b10550
replaced ML_Lex.read_antiq by more concise ML_Lex.read, which includes full read/report with explicit position information;
wenzelm
parents:
36973
diff
changeset
|
78 |
val _ = ML_Context.eval_text_in (SOME ctxt) false Position.none (ml (txt1, txt2)); (* ML_Lex.read (!?) *) |
30394 | 79 |
val kind' = if kind = "" then "ML" else "ML " ^ kind; |
80 |
in |
|
39869
c269f6bd0a1f
more robust index_ML antiquotations: guess name from text (affects infixes and type constructors in particular);
wenzelm
parents:
39858
diff
changeset
|
81 |
"\\indexdef{}{" ^ kind' ^ "}{" ^ clean_string (ml_name txt1) ^ "}" ^ |
30394 | 82 |
(txt' |
38767
d8da44a8dd25
proper context for various Thy_Output options, via official configuration options in ML and Isar;
wenzelm
parents:
37982
diff
changeset
|
83 |
|> (if Config.get ctxt Thy_Output.quotes then quote else I) |
d8da44a8dd25
proper context for various Thy_Output options, via official configuration options in ML and Isar;
wenzelm
parents:
37982
diff
changeset
|
84 |
|> (if Config.get ctxt Thy_Output.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}" |
30394 | 85 |
else split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n")) |
86 |
end); |
|
26742 | 87 |
|
88 |
in |
|
21375 | 89 |
|
30394 | 90 |
val _ = index_ml "index_ML" "" ml_val; |
91 |
val _ = index_ml "index_ML_type" "type" ml_type; |
|
92 |
val _ = index_ml "index_ML_exn" "exception" ml_exn; |
|
93 |
val _ = index_ml "index_ML_structure" "structure" ml_structure; |
|
94 |
val _ = index_ml "index_ML_functor" "functor" ml_functor; |
|
21375 | 95 |
|
26742 | 96 |
end; |
21375 | 97 |
|
23846 | 98 |
|
30394 | 99 |
(* named theorems *) |
23846 | 100 |
|
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37198
diff
changeset
|
101 |
val _ = Thy_Output.antiquotation "named_thms" |
30394 | 102 |
(Scan.repeat (Attrib.thm -- Scan.lift (Args.parens Args.name))) |
103 |
(fn {context = ctxt, ...} => |
|
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37198
diff
changeset
|
104 |
map (apfst (Thy_Output.pretty_thm ctxt)) |
38767
d8da44a8dd25
proper context for various Thy_Output options, via official configuration options in ML and Isar;
wenzelm
parents:
37982
diff
changeset
|
105 |
#> (if Config.get ctxt Thy_Output.quotes then map (apfst Pretty.quote) else I) |
d8da44a8dd25
proper context for various Thy_Output options, via official configuration options in ML and Isar;
wenzelm
parents:
37982
diff
changeset
|
106 |
#> (if Config.get ctxt Thy_Output.display |
30394 | 107 |
then |
108 |
map (fn (p, name) => |
|
38767
d8da44a8dd25
proper context for various Thy_Output options, via official configuration options in ML and Isar;
wenzelm
parents:
37982
diff
changeset
|
109 |
Output.output (Pretty.string_of (Pretty.indent (Config.get ctxt Thy_Output.indent) p)) ^ |
d8da44a8dd25
proper context for various Thy_Output options, via official configuration options in ML and Isar;
wenzelm
parents:
37982
diff
changeset
|
110 |
"\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}") |
30394 | 111 |
#> space_implode "\\par\\smallskip%\n" |
112 |
#> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}" |
|
113 |
else |
|
114 |
map (fn (p, name) => |
|
115 |
Output.output (Pretty.str_of p) ^ |
|
38767
d8da44a8dd25
proper context for various Thy_Output options, via official configuration options in ML and Isar;
wenzelm
parents:
37982
diff
changeset
|
116 |
"\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}") |
30394 | 117 |
#> space_implode "\\par\\smallskip%\n" |
118 |
#> enclose "\\isa{" "}")); |
|
26742 | 119 |
|
120 |
||
30394 | 121 |
(* theory file *) |
26742 | 122 |
|
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37198
diff
changeset
|
123 |
val _ = Thy_Output.antiquotation "thy_file" (Scan.lift Args.name) |
38767
d8da44a8dd25
proper context for various Thy_Output options, via official configuration options in ML and Isar;
wenzelm
parents:
37982
diff
changeset
|
124 |
(fn {context = ctxt, ...} => |
d8da44a8dd25
proper context for various Thy_Output options, via official configuration options in ML and Isar;
wenzelm
parents:
37982
diff
changeset
|
125 |
fn name => (Thy_Load.check_thy Path.current name; Thy_Output.output ctxt [Pretty.str name])); |
26742 | 126 |
|
26751 | 127 |
|
30394 | 128 |
(* Isabelle/Isar entities (with index) *) |
26751 | 129 |
|
130 |
local |
|
131 |
||
26894 | 132 |
fun no_check _ _ = true; |
133 |
||
134 |
fun thy_check intern defined ctxt = |
|
42361 | 135 |
let val thy = Proof_Context.theory_of ctxt |
26894 | 136 |
in defined thy o intern thy end; |
137 |
||
28237 | 138 |
fun check_tool name = |
139 |
File.exists (Path.append (Path.explode "~~/lib/Tools") (Path.basic name)); |
|
140 |
||
26751 | 141 |
val arg = enclose "{" "}" o clean_string; |
142 |
||
30394 | 143 |
fun entity check markup kind index = |
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37198
diff
changeset
|
144 |
Thy_Output.antiquotation |
30396 | 145 |
(translate (fn " " => "_" | c => c) kind ^ |
146 |
(case index of NONE => "" | SOME true => "_def" | SOME false => "_ref")) |
|
30394 | 147 |
(Scan.lift (Scan.optional (Args.parens Args.name) "" -- Args.name)) |
148 |
(fn {context = ctxt, ...} => fn (logic, name) => |
|
149 |
let |
|
150 |
val hyper_name = |
|
151 |
"{" ^ Long_Name.append kind (Long_Name.append logic (clean_name name)) ^ "}"; |
|
152 |
val hyper = |
|
153 |
enclose ("\\hyperlink" ^ hyper_name ^ "{") "}" #> |
|
154 |
index = SOME true ? enclose ("\\hypertarget" ^ hyper_name ^ "{") "}"; |
|
155 |
val idx = |
|
156 |
(case index of |
|
157 |
NONE => "" |
|
158 |
| SOME is_def => |
|
159 |
"\\index" ^ (if is_def then "def" else "ref") ^ arg logic ^ arg kind ^ arg name); |
|
160 |
in |
|
161 |
if check ctxt name then |
|
162 |
idx ^ |
|
163 |
(Output.output name |
|
164 |
|> (if markup = "" then I else enclose ("\\" ^ markup ^ "{") "}") |
|
38767
d8da44a8dd25
proper context for various Thy_Output options, via official configuration options in ML and Isar;
wenzelm
parents:
37982
diff
changeset
|
165 |
|> (if Config.get ctxt Thy_Output.quotes then quote else I) |
d8da44a8dd25
proper context for various Thy_Output options, via official configuration options in ML and Isar;
wenzelm
parents:
37982
diff
changeset
|
166 |
|> (if Config.get ctxt Thy_Output.display |
d8da44a8dd25
proper context for various Thy_Output options, via official configuration options in ML and Isar;
wenzelm
parents:
37982
diff
changeset
|
167 |
then enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}" |
30394 | 168 |
else hyper o enclose "\\mbox{\\isa{" "}}")) |
169 |
else error ("Bad " ^ kind ^ " " ^ quote name) |
|
170 |
end); |
|
26897 | 171 |
|
26894 | 172 |
fun entity_antiqs check markup kind = |
31297 | 173 |
((entity check markup kind NONE); |
174 |
(entity check markup kind (SOME true)); |
|
175 |
(entity check markup kind (SOME false))); |
|
26751 | 176 |
|
177 |
in |
|
178 |
||
30394 | 179 |
val _ = entity_antiqs no_check "" "syntax"; |
36973 | 180 |
val _ = entity_antiqs (K (is_some o Keyword.command_keyword)) "isacommand" "command"; |
181 |
val _ = entity_antiqs (K Keyword.is_keyword) "isakeyword" "keyword"; |
|
182 |
val _ = entity_antiqs (K Keyword.is_keyword) "isakeyword" "element"; |
|
30394 | 183 |
val _ = entity_antiqs (thy_check Method.intern Method.defined) "" "method"; |
184 |
val _ = entity_antiqs (thy_check Attrib.intern Attrib.defined) "" "attribute"; |
|
185 |
val _ = entity_antiqs no_check "" "fact"; |
|
186 |
val _ = entity_antiqs no_check "" "variable"; |
|
187 |
val _ = entity_antiqs no_check "" "case"; |
|
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37198
diff
changeset
|
188 |
val _ = entity_antiqs (K Thy_Output.defined_command) "" "antiquotation"; |
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37198
diff
changeset
|
189 |
val _ = entity_antiqs (K Thy_Output.defined_option) "" "antiquotation option"; |
30394 | 190 |
val _ = entity_antiqs (fn _ => fn name => is_some (OS.Process.getEnv name)) "isatt" "setting"; |
191 |
val _ = entity_antiqs no_check "" "inference"; |
|
192 |
val _ = entity_antiqs no_check "isatt" "executable"; |
|
193 |
val _ = entity_antiqs (K check_tool) "isatt" "tool"; |
|
37982 | 194 |
val _ = entity_antiqs (K (can Thy_Info.get_theory)) "" "theory"; |
39829 | 195 |
val _ = entity_antiqs no_check "" "ML antiquotation"; (* FIXME proper check *) |
26751 | 196 |
|
26742 | 197 |
end; |
26751 | 198 |
|
199 |
end; |