| author | wenzelm | 
| Wed, 19 May 2021 13:00:42 +0200 | |
| changeset 73741 | 941915a3b811 | 
| parent 73734 | f7f0d516df0c | 
| child 73760 | f4be1b0d7a51 | 
| permissions | -rw-r--r-- | 
| 48985 | 1 | (* Title: Doc/antiquote_setup.ML | 
| 21375 | 2 | Author: Makarius | 
| 3 | ||
| 26742 | 4 | Auxiliary antiquotations for the Isabelle manuals. | 
| 21375 | 5 | *) | 
| 6 | ||
| 56059 
2390391584c2
some document antiquotations for Isabelle/jEdit elements;
 wenzelm parents: 
55997diff
changeset | 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: 
29726diff
changeset | 12 | fun translate f = Symbol.explode #> map f #> implode; | 
| 
ec3fc818b82e
clean_string/clean_name: proper treatment of \<dash>;
 wenzelm parents: 
29726diff
changeset | 13 | |
| 
ec3fc818b82e
clean_string/clean_name: proper treatment of \<dash>;
 wenzelm parents: 
29726diff
changeset | 14 | val clean_string = translate | 
| 26853 
52cb0e965041
clean_string: map "_" to "\\_" (best used with underscore.sty);
 wenzelm parents: 
26843diff
changeset | 15 | (fn "_" => "\\_" | 
| 30120 
aaa4667285c8
uniform treatment of ML indexing, using general \indexdef macro for formal Isabelle/Isar entities;
 wenzelm parents: 
29736diff
changeset | 16 | | "#" => "\\#" | 
| 52408 | 17 | | "$" => "\\$" | 
| 18 | | "%" => "\\%" | |
| 26897 | 19 | | "<" => "$<$" | 
| 26768 | 20 | | ">" => "$>$" | 
| 21 |     | "{" => "\\{"
 | |
| 30120 
aaa4667285c8
uniform treatment of ML indexing, using general \indexdef macro for formal Isabelle/Isar entities;
 wenzelm parents: 
29736diff
changeset | 22 | | "|" => "$\\mid$" | 
| 26768 | 23 | | "}" => "\\}" | 
| 42666 | 24 | | "\<hyphen>" => "-" | 
| 26768 | 25 | | c => c); | 
| 26751 | 26 | |
| 31546 | 27 | fun clean_name "\<dots>" = "dots" | 
| 26897 | 28 | | clean_name ".." = "ddot" | 
| 29 | | clean_name "." = "dot" | |
| 26910 | 30 | | clean_name "_" = "underscore" | 
| 26897 | 31 |   | clean_name "{" = "braceleft"
 | 
| 32 | | clean_name "}" = "braceright" | |
| 42666 | 33 | | clean_name s = s |> translate (fn "_" => "-" | "\<hyphen>" => "-" | c => c); | 
| 26897 | 34 | |
| 26742 | 35 | |
| 36 | (* ML text *) | |
| 37 | ||
| 38 | local | |
| 39 | ||
| 59067 | 40 | fun ml_val (toks1, []) = ML_Lex.read "fn _ => (" @ toks1 @ ML_Lex.read ");"
 | 
| 41 | | ml_val (toks1, toks2) = | |
| 42 |       ML_Lex.read "fn _ => (" @ toks1 @ ML_Lex.read " : " @ toks2 @ ML_Lex.read ");";
 | |
| 21375 | 43 | |
| 59067 | 44 | fun ml_op (toks1, []) = ML_Lex.read "fn _ => (op " @ toks1 @ ML_Lex.read ");" | 
| 45 | | ml_op (toks1, toks2) = | |
| 46 | ML_Lex.read "fn _ => (op " @ toks1 @ ML_Lex.read " : " @ toks2 @ ML_Lex.read ");"; | |
| 46261 | 47 | |
| 59067 | 48 | fun ml_type (toks1, []) = ML_Lex.read "val _ = NONE : (" @ toks1 @ ML_Lex.read ") option;"
 | 
| 55831 | 49 | | ml_type (toks1, toks2) = | 
| 59067 | 50 |       ML_Lex.read "val _ = [NONE : (" @ toks1 @ ML_Lex.read ") option, NONE : (" @
 | 
| 51 | toks2 @ ML_Lex.read ") option];"; | |
| 22289 | 52 | |
| 59067 | 53 | fun ml_exception (toks1, []) = ML_Lex.read "fn _ => (" @ toks1 @ ML_Lex.read " : exn);"
 | 
| 55837 | 54 | | ml_exception (toks1, toks2) = | 
| 59067 | 55 |       ML_Lex.read "fn _ => (" @ toks1 @ ML_Lex.read " : " @ toks2 @ ML_Lex.read " -> exn);";
 | 
| 22289 | 56 | |
| 55831 | 57 | fun ml_structure (toks, _) = | 
| 59067 | 58 | ML_Lex.read "functor XXX() = struct structure XX = " @ toks @ ML_Lex.read " end;"; | 
| 21375 | 59 | |
| 55831 | 60 | fun ml_functor (Antiquote.Text tok :: _, _) = | 
| 59067 | 61 | ML_Lex.read "ML_Env.check_functor " @ | 
| 62 | ML_Lex.read (ML_Syntax.print_string (ML_Lex.content_of tok)) | |
| 55831 | 63 | | ml_functor _ = raise Fail "Bad ML functor specification"; | 
| 21375 | 64 | |
| 59082 | 65 | val is_name = | 
| 66 | ML_Lex.kind_of #> (fn kind => kind = ML_Lex.Ident orelse kind = ML_Lex.Long_Ident); | |
| 39869 
c269f6bd0a1f
more robust index_ML antiquotations: guess name from text (affects infixes and type constructors in particular);
 wenzelm parents: 
39858diff
changeset | 67 | |
| 
c269f6bd0a1f
more robust index_ML antiquotations: guess name from text (affects infixes and type constructors in particular);
 wenzelm parents: 
39858diff
changeset | 68 | fun ml_name txt = | 
| 
c269f6bd0a1f
more robust index_ML antiquotations: guess name from text (affects infixes and type constructors in particular);
 wenzelm parents: 
39858diff
changeset | 69 | (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: 
39858diff
changeset | 70 | 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: 
39858diff
changeset | 71 |   | _ => 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: 
39858diff
changeset | 72 | |
| 55831 | 73 | fun prep_ml source = | 
| 69349 
7cef9e386ffe
more accurate positions for "name" (quoted string) and "embedded" (cartouche): refer to content without delimiters, which is e.g. relevant for systematic selection/renaming of scope groups;
 wenzelm parents: 
68823diff
changeset | 74 | (#1 (Input.source_content source), ML_Lex.read_source source); | 
| 55831 | 75 | |
| 67463 | 76 | fun index_ml name kind ml = Thy_Output.antiquotation_raw name | 
| 59809 | 77 | (Scan.lift (Args.text_input -- Scan.option (Args.colon |-- Args.text_input))) | 
| 67463 | 78 | (fn ctxt => fn (source1, opt_source2) => | 
| 30394 | 79 | let | 
| 55831 | 80 | val (txt1, toks1) = prep_ml source1; | 
| 81 | val (txt2, toks2) = | |
| 82 | (case opt_source2 of | |
| 83 | SOME source => prep_ml source | |
| 84 |         | NONE => ("", []));
 | |
| 85 | ||
| 30394 | 86 | val txt = | 
| 87 | if txt2 = "" then txt1 | |
| 88 | else if kind = "type" then txt1 ^ " = " ^ txt2 | |
| 89 | else if kind = "exception" then txt1 ^ " of " ^ txt2 | |
| 50239 | 90 | else if Symbol_Pos.is_identifier (Long_Name.base_name (ml_name txt1)) | 
| 42290 
b1f544c84040
discontinued special treatment of structure Lexicon;
 wenzelm parents: 
40801diff
changeset | 91 | then txt1 ^ ": " ^ txt2 | 
| 39858 
5be7a57c3b4e
more robust treatment of symbolic indentifiers (which may contain colons);
 wenzelm parents: 
39829diff
changeset | 92 | else txt1 ^ " : " ^ txt2; | 
| 30394 | 93 | val txt' = if kind = "" then txt else kind ^ " " ^ txt; | 
| 55831 | 94 | |
| 59064 | 95 | val pos = Input.pos_of source1; | 
| 56275 
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
 wenzelm parents: 
56208diff
changeset | 96 | val _ = | 
| 57477 
c3b5cb53ea79
redundant error position, to ensure the message is attached somewhere, despite the distortion of positions due to glued tokens;
 wenzelm parents: 
57334diff
changeset | 97 | ML_Context.eval_in (SOME ctxt) ML_Compiler.flags pos (ml (toks1, toks2)) | 
| 
c3b5cb53ea79
redundant error position, to ensure the message is attached somewhere, despite the distortion of positions due to glued tokens;
 wenzelm parents: 
57334diff
changeset | 98 | handle ERROR msg => error (msg ^ Position.here pos); | 
| 30394 | 99 | val kind' = if kind = "" then "ML" else "ML " ^ kind; | 
| 100 | in | |
| 67463 | 101 | Latex.block | 
| 102 |        [Latex.string ("\\indexdef{}{" ^ kind' ^ "}{" ^ clean_string (ml_name txt1) ^ "}"),
 | |
| 103 | Thy_Output.verbatim ctxt txt'] | |
| 30394 | 104 | end); | 
| 26742 | 105 | |
| 106 | in | |
| 21375 | 107 | |
| 56059 
2390391584c2
some document antiquotations for Isabelle/jEdit elements;
 wenzelm parents: 
55997diff
changeset | 108 | val _ = | 
| 
2390391584c2
some document antiquotations for Isabelle/jEdit elements;
 wenzelm parents: 
55997diff
changeset | 109 | Theory.setup | 
| 69593 | 110 | (index_ml \<^binding>\<open>index_ML\<close> "" ml_val #> | 
| 111 | index_ml \<^binding>\<open>index_ML_op\<close> "infix" ml_op #> | |
| 112 | index_ml \<^binding>\<open>index_ML_type\<close> "type" ml_type #> | |
| 113 | index_ml \<^binding>\<open>index_ML_exception\<close> "exception" ml_exception #> | |
| 114 | index_ml \<^binding>\<open>index_ML_structure\<close> "structure" ml_structure #> | |
| 115 | index_ml \<^binding>\<open>index_ML_functor\<close> "functor" ml_functor); | |
| 21375 | 116 | |
| 26742 | 117 | end; | 
| 21375 | 118 | |
| 23846 | 119 | |
| 30394 | 120 | (* named theorems *) | 
| 23846 | 121 | |
| 56059 
2390391584c2
some document antiquotations for Isabelle/jEdit elements;
 wenzelm parents: 
55997diff
changeset | 122 | val _ = | 
| 69593 | 123 | Theory.setup (Thy_Output.antiquotation_raw \<^binding>\<open>named_thms\<close> | 
| 43564 
9864182c6bad
document antiquotations are managed as theory data, with proper name space and entity markup;
 wenzelm parents: 
43563diff
changeset | 124 | (Scan.repeat (Attrib.thm -- Scan.lift (Args.parens Args.name))) | 
| 67463 | 125 | (fn ctxt => | 
| 126 | map (fn (thm, name) => | |
| 127 | Output.output | |
| 128 | (Document_Antiquotation.format ctxt | |
| 70121 | 129 | (Document_Antiquotation.delimit ctxt (Thy_Output.pretty_thm ctxt thm))) ^ | 
| 67468 | 130 |         enclose "\\rulename{" "}" (Output.output name))
 | 
| 67463 | 131 | #> space_implode "\\par\\smallskip%\n" | 
| 132 | #> Latex.string #> single | |
| 133 | #> Thy_Output.isabelle ctxt)); | |
| 26742 | 134 | |
| 135 | ||
| 30394 | 136 | (* Isabelle/Isar entities (with index) *) | 
| 26751 | 137 | |
| 138 | local | |
| 139 | ||
| 61620 | 140 | fun no_check (_: Proof.context) (name, _: Position.T) = name; | 
| 26894 | 141 | |
| 61620 | 142 | fun check_keyword ctxt (name, pos) = | 
| 143 | if Keyword.is_keyword (Thy_Header.get_keywords' ctxt) name then name | |
| 144 |   else error ("Bad outer syntax keyword " ^ quote name ^ Position.here pos);
 | |
| 53061 | 145 | |
| 71913 | 146 | fun check_system_option ctxt arg = | 
| 147 | (Completion.check_option (Options.default ()) ctxt arg; true) | |
| 56467 | 148 | handle ERROR _ => false; | 
| 149 | ||
| 26751 | 150 | val arg = enclose "{" "}" o clean_string;
 | 
| 151 | ||
| 56185 | 152 | fun entity check markup binding index = | 
| 67463 | 153 | Thy_Output.antiquotation_raw | 
| 56185 | 154 | (binding |> Binding.map_name (fn name => name ^ | 
| 43564 
9864182c6bad
document antiquotations are managed as theory data, with proper name space and entity markup;
 wenzelm parents: 
43563diff
changeset | 155 | (case index of NONE => "" | SOME true => "_def" | SOME false => "_ref"))) | 
| 69349 
7cef9e386ffe
more accurate positions for "name" (quoted string) and "embedded" (cartouche): refer to content without delimiters, which is e.g. relevant for systematic selection/renaming of scope groups;
 wenzelm parents: 
68823diff
changeset | 156 | (Scan.lift (Scan.optional (Args.parens Args.name) "" -- Args.name_position)) | 
| 67463 | 157 | (fn ctxt => fn (logic, (name, pos)) => | 
| 30394 | 158 | let | 
| 56185 | 159 | val kind = translate (fn "_" => " " | c => c) (Binding.name_of binding); | 
| 30394 | 160 | val hyper_name = | 
| 161 |           "{" ^ Long_Name.append kind (Long_Name.append logic (clean_name name)) ^ "}";
 | |
| 162 | val hyper = | |
| 163 |           enclose ("\\hyperlink" ^ hyper_name ^ "{") "}" #>
 | |
| 164 |           index = SOME true ? enclose ("\\hypertarget" ^ hyper_name ^ "{") "}";
 | |
| 165 | val idx = | |
| 166 | (case index of | |
| 167 | NONE => "" | |
| 168 | | SOME is_def => | |
| 169 | "\\index" ^ (if is_def then "def" else "ref") ^ arg logic ^ arg kind ^ arg name); | |
| 66682 
c4cbe609f6a8
avoid duplicate message for @{action} in particular (see also @{action} within Pure);
 wenzelm parents: 
62829diff
changeset | 170 | val _ = | 
| 
c4cbe609f6a8
avoid duplicate message for @{action} in particular (see also @{action} within Pure);
 wenzelm parents: 
62829diff
changeset | 171 | if Context_Position.is_reported ctxt pos then ignore (check ctxt (name, pos)) else (); | 
| 67463 | 172 | val latex = | 
| 173 | idx ^ | |
| 174 | (Output.output name | |
| 175 |             |> (if markup = "" then I else enclose ("\\" ^ markup ^ "{") "}")
 | |
| 176 |             |> hyper o enclose "\\mbox{\\isa{" "}}");
 | |
| 177 | in Latex.string latex end); | |
| 26897 | 178 | |
| 26894 | 179 | fun entity_antiqs check markup kind = | 
| 43564 
9864182c6bad
document antiquotations are managed as theory data, with proper name space and entity markup;
 wenzelm parents: 
43563diff
changeset | 180 | entity check markup kind NONE #> | 
| 
9864182c6bad
document antiquotations are managed as theory data, with proper name space and entity markup;
 wenzelm parents: 
43563diff
changeset | 181 | entity check markup kind (SOME true) #> | 
| 
9864182c6bad
document antiquotations are managed as theory data, with proper name space and entity markup;
 wenzelm parents: 
43563diff
changeset | 182 | entity check markup kind (SOME false); | 
| 26751 | 183 | |
| 184 | in | |
| 185 | ||
| 56059 
2390391584c2
some document antiquotations for Isabelle/jEdit elements;
 wenzelm parents: 
55997diff
changeset | 186 | val _ = | 
| 
2390391584c2
some document antiquotations for Isabelle/jEdit elements;
 wenzelm parents: 
55997diff
changeset | 187 | Theory.setup | 
| 69593 | 188 | (entity_antiqs no_check "" \<^binding>\<open>syntax\<close> #> | 
| 189 | entity_antiqs Outer_Syntax.check_command "isacommand" \<^binding>\<open>command\<close> #> | |
| 190 | entity_antiqs check_keyword "isakeyword" \<^binding>\<open>keyword\<close> #> | |
| 191 | entity_antiqs check_keyword "isakeyword" \<^binding>\<open>element\<close> #> | |
| 192 | entity_antiqs Method.check_name "" \<^binding>\<open>method\<close> #> | |
| 193 | entity_antiqs Attrib.check_name "" \<^binding>\<open>attribute\<close> #> | |
| 194 | entity_antiqs no_check "" \<^binding>\<open>fact\<close> #> | |
| 195 | entity_antiqs no_check "" \<^binding>\<open>variable\<close> #> | |
| 196 | entity_antiqs no_check "" \<^binding>\<open>case\<close> #> | |
| 197 | entity_antiqs Document_Antiquotation.check "" \<^binding>\<open>antiquotation\<close> #> | |
| 198 | entity_antiqs Document_Antiquotation.check_option "" \<^binding>\<open>antiquotation_option\<close> #> | |
| 69962 
82e945d472d5
documentation of document markers and re-interpreted command tags;
 wenzelm parents: 
69593diff
changeset | 199 | entity_antiqs Document_Marker.check "" \<^binding>\<open>document_marker\<close> #> | 
| 69593 | 200 | entity_antiqs no_check "isasystem" \<^binding>\<open>setting\<close> #> | 
| 201 | entity_antiqs check_system_option "isasystem" \<^binding>\<open>system_option\<close> #> | |
| 202 | entity_antiqs no_check "" \<^binding>\<open>inference\<close> #> | |
| 203 | entity_antiqs no_check "isasystem" \<^binding>\<open>executable\<close> #> | |
| 72763 | 204 | entity_antiqs Isabelle_Tool.check "isatool" \<^binding>\<open>tool\<close> #> | 
| 69593 | 205 | entity_antiqs ML_Context.check_antiquotation "" \<^binding>\<open>ML_antiquotation\<close> #> | 
| 206 | entity_antiqs (K JEdit.check_action) "isasystem" \<^binding>\<open>action\<close>); | |
| 26751 | 207 | |
| 26742 | 208 | end; | 
| 26751 | 209 | |
| 73734 | 210 | |
| 211 | (* show symbols *) | |
| 212 | ||
| 213 | val _ = | |
| 214 | Theory.setup (Thy_Output.antiquotation_raw \<^binding>\<open>show_symbols\<close> (Scan.succeed ()) | |
| 215 | (fn _ => fn _ => | |
| 216 | let | |
| 217 | val symbol_name = | |
| 218 |           unprefix "\\newcommand{\\isasym"
 | |
| 219 | #> raw_explode | |
| 220 | #> take_prefix Symbol.is_ascii_letter | |
| 221 | #> implode; | |
| 222 | ||
| 223 | val symbols = | |
| 224 | File.read \<^file>\<open>~~/lib/texinputs/isabellesym.sty\<close> | |
| 225 | |> split_lines | |
| 226 | |> map_filter (fn line => | |
| 227 | (case try symbol_name line of | |
| 228 | NONE => NONE | |
| 229 | | SOME "" => NONE | |
| 230 |             | SOME name => SOME ("\\verb,\\" ^ "<" ^ name ^ ">, & {\\isasym" ^ name ^ "}")));
 | |
| 231 | ||
| 232 | val eol = "\\\\\n"; | |
| 233 | fun table (a :: b :: rest) = a ^ " & " ^ b ^ eol :: table rest | |
| 234 | | table [a] = [a ^ eol] | |
| 235 | | table [] = []; | |
| 236 | in | |
| 237 | Latex.string | |
| 238 |           ("\\begin{supertabular}{ll@{\\qquad}ll}\n" ^ implode (table symbols) ^
 | |
| 239 |            "\\end{supertabular}\n")
 | |
| 240 | end)) | |
| 241 | ||
| 26751 | 242 | end; |