author | haftmann |
Wed, 14 Jul 2010 14:16:12 +0200 | |
changeset 37818 | dd65033fed78 |
parent 37216 | 3165bc303f66 |
child 37982 | 111ce9651564 |
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 |
||
26742 | 7 |
structure AntiquoteSetup: sig end = |
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 |
| "}" => "\\}" |
31546 | 22 |
| "\<dash>" => "-" |
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" |
|
31546 | 31 |
| clean_name s = s |> translate (fn "_" => "-" | "\<dash>" => "-" | 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 ^ ");" |
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 |
|
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 |
|
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37198
diff
changeset
|
59 |
fun index_ml name kind ml = Thy_Output.antiquotation name |
30394 | 60 |
(Scan.lift (Args.name -- Scan.optional (Args.colon |-- Args.name) "")) |
61 |
(fn {context = ctxt, ...} => fn (txt1, txt2) => |
|
62 |
let |
|
63 |
val txt = |
|
64 |
if txt2 = "" then txt1 |
|
65 |
else if kind = "type" then txt1 ^ " = " ^ txt2 |
|
66 |
else if kind = "exception" then txt1 ^ " of " ^ txt2 |
|
67 |
else txt1 ^ ": " ^ txt2; |
|
68 |
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
|
69 |
val _ = ML_Context.eval_text_in (SOME ctxt) false Position.none (ml (txt1, txt2)); (* ML_Lex.read (!?) *) |
30394 | 70 |
val kind' = if kind = "" then "ML" else "ML " ^ kind; |
71 |
in |
|
72 |
"\\indexdef{}{" ^ kind' ^ "}{" ^ clean_string txt1 ^ "}" ^ |
|
73 |
(txt' |
|
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37198
diff
changeset
|
74 |
|> (if ! Thy_Output.quotes then quote else I) |
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37198
diff
changeset
|
75 |
|> (if ! Thy_Output.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}" |
30394 | 76 |
else split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n")) |
77 |
end); |
|
26742 | 78 |
|
79 |
in |
|
21375 | 80 |
|
30394 | 81 |
val _ = index_ml "index_ML" "" ml_val; |
82 |
val _ = index_ml "index_ML_type" "type" ml_type; |
|
83 |
val _ = index_ml "index_ML_exn" "exception" ml_exn; |
|
84 |
val _ = index_ml "index_ML_structure" "structure" ml_structure; |
|
85 |
val _ = index_ml "index_ML_functor" "functor" ml_functor; |
|
21375 | 86 |
|
26742 | 87 |
end; |
21375 | 88 |
|
23846 | 89 |
|
30394 | 90 |
(* named theorems *) |
23846 | 91 |
|
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37198
diff
changeset
|
92 |
val _ = Thy_Output.antiquotation "named_thms" |
30394 | 93 |
(Scan.repeat (Attrib.thm -- Scan.lift (Args.parens Args.name))) |
94 |
(fn {context = ctxt, ...} => |
|
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37198
diff
changeset
|
95 |
map (apfst (Thy_Output.pretty_thm ctxt)) |
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37198
diff
changeset
|
96 |
#> (if ! Thy_Output.quotes then map (apfst Pretty.quote) else I) |
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37198
diff
changeset
|
97 |
#> (if ! Thy_Output.display |
30394 | 98 |
then |
99 |
map (fn (p, name) => |
|
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37198
diff
changeset
|
100 |
Output.output (Pretty.string_of (Pretty.indent (! Thy_Output.indent) p)) ^ |
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37198
diff
changeset
|
101 |
"\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text name)) ^ "}") |
30394 | 102 |
#> space_implode "\\par\\smallskip%\n" |
103 |
#> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}" |
|
104 |
else |
|
105 |
map (fn (p, name) => |
|
106 |
Output.output (Pretty.str_of p) ^ |
|
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37198
diff
changeset
|
107 |
"\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text name)) ^ "}") |
30394 | 108 |
#> space_implode "\\par\\smallskip%\n" |
109 |
#> enclose "\\isa{" "}")); |
|
26742 | 110 |
|
111 |
||
30394 | 112 |
(* theory file *) |
26742 | 113 |
|
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37198
diff
changeset
|
114 |
val _ = Thy_Output.antiquotation "thy_file" (Scan.lift Args.name) |
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37198
diff
changeset
|
115 |
(fn _ => fn name => (Thy_Load.check_thy Path.current name; Thy_Output.output [Pretty.str name])); |
26742 | 116 |
|
26751 | 117 |
|
30394 | 118 |
(* Isabelle/Isar entities (with index) *) |
26751 | 119 |
|
120 |
local |
|
121 |
||
26894 | 122 |
fun no_check _ _ = true; |
123 |
||
124 |
fun thy_check intern defined ctxt = |
|
125 |
let val thy = ProofContext.theory_of ctxt |
|
126 |
in defined thy o intern thy end; |
|
127 |
||
28237 | 128 |
fun check_tool name = |
129 |
File.exists (Path.append (Path.explode "~~/lib/Tools") (Path.basic name)); |
|
130 |
||
26751 | 131 |
val arg = enclose "{" "}" o clean_string; |
132 |
||
30394 | 133 |
fun entity check markup kind index = |
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37198
diff
changeset
|
134 |
Thy_Output.antiquotation |
30396 | 135 |
(translate (fn " " => "_" | c => c) kind ^ |
136 |
(case index of NONE => "" | SOME true => "_def" | SOME false => "_ref")) |
|
30394 | 137 |
(Scan.lift (Scan.optional (Args.parens Args.name) "" -- Args.name)) |
138 |
(fn {context = ctxt, ...} => fn (logic, name) => |
|
139 |
let |
|
140 |
val hyper_name = |
|
141 |
"{" ^ Long_Name.append kind (Long_Name.append logic (clean_name name)) ^ "}"; |
|
142 |
val hyper = |
|
143 |
enclose ("\\hyperlink" ^ hyper_name ^ "{") "}" #> |
|
144 |
index = SOME true ? enclose ("\\hypertarget" ^ hyper_name ^ "{") "}"; |
|
145 |
val idx = |
|
146 |
(case index of |
|
147 |
NONE => "" |
|
148 |
| SOME is_def => |
|
149 |
"\\index" ^ (if is_def then "def" else "ref") ^ arg logic ^ arg kind ^ arg name); |
|
150 |
in |
|
151 |
if check ctxt name then |
|
152 |
idx ^ |
|
153 |
(Output.output name |
|
154 |
|> (if markup = "" then I else enclose ("\\" ^ markup ^ "{") "}") |
|
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37198
diff
changeset
|
155 |
|> (if ! Thy_Output.quotes then quote else I) |
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37198
diff
changeset
|
156 |
|> (if ! Thy_Output.display then enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}" |
30394 | 157 |
else hyper o enclose "\\mbox{\\isa{" "}}")) |
158 |
else error ("Bad " ^ kind ^ " " ^ quote name) |
|
159 |
end); |
|
26897 | 160 |
|
26894 | 161 |
fun entity_antiqs check markup kind = |
31297 | 162 |
((entity check markup kind NONE); |
163 |
(entity check markup kind (SOME true)); |
|
164 |
(entity check markup kind (SOME false))); |
|
26751 | 165 |
|
166 |
in |
|
167 |
||
30394 | 168 |
val _ = entity_antiqs no_check "" "syntax"; |
36973 | 169 |
val _ = entity_antiqs (K (is_some o Keyword.command_keyword)) "isacommand" "command"; |
170 |
val _ = entity_antiqs (K Keyword.is_keyword) "isakeyword" "keyword"; |
|
171 |
val _ = entity_antiqs (K Keyword.is_keyword) "isakeyword" "element"; |
|
30394 | 172 |
val _ = entity_antiqs (thy_check Method.intern Method.defined) "" "method"; |
173 |
val _ = entity_antiqs (thy_check Attrib.intern Attrib.defined) "" "attribute"; |
|
174 |
val _ = entity_antiqs no_check "" "fact"; |
|
175 |
val _ = entity_antiqs no_check "" "variable"; |
|
176 |
val _ = entity_antiqs no_check "" "case"; |
|
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37198
diff
changeset
|
177 |
val _ = entity_antiqs (K Thy_Output.defined_command) "" "antiquotation"; |
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37198
diff
changeset
|
178 |
val _ = entity_antiqs (K Thy_Output.defined_option) "" "antiquotation option"; |
30394 | 179 |
val _ = entity_antiqs (fn _ => fn name => is_some (OS.Process.getEnv name)) "isatt" "setting"; |
180 |
val _ = entity_antiqs no_check "" "inference"; |
|
181 |
val _ = entity_antiqs no_check "isatt" "executable"; |
|
182 |
val _ = entity_antiqs (K check_tool) "isatt" "tool"; |
|
183 |
val _ = entity_antiqs (K (File.exists o Path.explode)) "isatt" "file"; |
|
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37198
diff
changeset
|
184 |
val _ = entity_antiqs (K Thy_Info.known_thy) "" "theory"; |
26751 | 185 |
|
26742 | 186 |
end; |
26751 | 187 |
|
188 |
end; |