| author | paulson <lp15@cam.ac.uk> | 
| Fri, 25 Aug 2017 23:30:36 +0100 | |
| changeset 66512 | 89b6455b63b6 | 
| parent 62829 | 4141c2a8458b | 
| child 66682 | c4cbe609f6a8 | 
| 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 = | 
| 59066 | 74 | (Input.source_content source, ML_Lex.read_source false source); | 
| 55831 | 75 | |
| 37216 
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
 wenzelm parents: 
37198diff
changeset | 76 | fun index_ml name kind ml = Thy_Output.antiquotation name | 
| 59809 | 77 | (Scan.lift (Args.text_input -- Scan.option (Args.colon |-- Args.text_input))) | 
| 55831 | 78 |   (fn {context = 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 | |
| 39869 
c269f6bd0a1f
more robust index_ML antiquotations: guess name from text (affects infixes and type constructors in particular);
 wenzelm parents: 
39858diff
changeset | 101 |       "\\indexdef{}{" ^ kind' ^ "}{" ^ clean_string (ml_name txt1) ^ "}" ^
 | 
| 58716 
23a380cc45f4
official support for "tt" style variants, avoid fragile \verb in LaTeX;
 wenzelm parents: 
58069diff
changeset | 102 | (Thy_Output.verbatim_text ctxt txt') | 
| 30394 | 103 | end); | 
| 26742 | 104 | |
| 105 | in | |
| 21375 | 106 | |
| 56059 
2390391584c2
some document antiquotations for Isabelle/jEdit elements;
 wenzelm parents: 
55997diff
changeset | 107 | val _ = | 
| 
2390391584c2
some document antiquotations for Isabelle/jEdit elements;
 wenzelm parents: 
55997diff
changeset | 108 | Theory.setup | 
| 
2390391584c2
some document antiquotations for Isabelle/jEdit elements;
 wenzelm parents: 
55997diff
changeset | 109 |    (index_ml @{binding index_ML} "" ml_val #>
 | 
| 
2390391584c2
some document antiquotations for Isabelle/jEdit elements;
 wenzelm parents: 
55997diff
changeset | 110 |     index_ml @{binding index_ML_op} "infix" ml_op #>
 | 
| 
2390391584c2
some document antiquotations for Isabelle/jEdit elements;
 wenzelm parents: 
55997diff
changeset | 111 |     index_ml @{binding index_ML_type} "type" ml_type #>
 | 
| 
2390391584c2
some document antiquotations for Isabelle/jEdit elements;
 wenzelm parents: 
55997diff
changeset | 112 |     index_ml @{binding index_ML_exception} "exception" ml_exception #>
 | 
| 
2390391584c2
some document antiquotations for Isabelle/jEdit elements;
 wenzelm parents: 
55997diff
changeset | 113 |     index_ml @{binding index_ML_structure} "structure" ml_structure #>
 | 
| 
2390391584c2
some document antiquotations for Isabelle/jEdit elements;
 wenzelm parents: 
55997diff
changeset | 114 |     index_ml @{binding index_ML_functor} "functor" ml_functor);
 | 
| 21375 | 115 | |
| 26742 | 116 | end; | 
| 21375 | 117 | |
| 23846 | 118 | |
| 30394 | 119 | (* named theorems *) | 
| 23846 | 120 | |
| 56059 
2390391584c2
some document antiquotations for Isabelle/jEdit elements;
 wenzelm parents: 
55997diff
changeset | 121 | val _ = | 
| 
2390391584c2
some document antiquotations for Isabelle/jEdit elements;
 wenzelm parents: 
55997diff
changeset | 122 |   Theory.setup (Thy_Output.antiquotation @{binding named_thms}
 | 
| 43564 
9864182c6bad
document antiquotations are managed as theory data, with proper name space and entity markup;
 wenzelm parents: 
43563diff
changeset | 123 | (Scan.repeat (Attrib.thm -- Scan.lift (Args.parens Args.name))) | 
| 
9864182c6bad
document antiquotations are managed as theory data, with proper name space and entity markup;
 wenzelm parents: 
43563diff
changeset | 124 |     (fn {context = ctxt, ...} =>
 | 
| 
9864182c6bad
document antiquotations are managed as theory data, with proper name space and entity markup;
 wenzelm parents: 
43563diff
changeset | 125 | map (apfst (Thy_Output.pretty_thm ctxt)) | 
| 
9864182c6bad
document antiquotations are managed as theory data, with proper name space and entity markup;
 wenzelm parents: 
43563diff
changeset | 126 | #> (if Config.get ctxt Thy_Output.quotes then map (apfst Pretty.quote) else I) | 
| 
9864182c6bad
document antiquotations are managed as theory data, with proper name space and entity markup;
 wenzelm parents: 
43563diff
changeset | 127 | #> (if Config.get ctxt Thy_Output.display | 
| 
9864182c6bad
document antiquotations are managed as theory data, with proper name space and entity markup;
 wenzelm parents: 
43563diff
changeset | 128 | then | 
| 
9864182c6bad
document antiquotations are managed as theory data, with proper name space and entity markup;
 wenzelm parents: 
43563diff
changeset | 129 | map (fn (p, name) => | 
| 59175 
bf465f335e85
system option "pretty_margin" is superseded by "thy_output_margin";
 wenzelm parents: 
59083diff
changeset | 130 | Output.output | 
| 
bf465f335e85
system option "pretty_margin" is superseded by "thy_output_margin";
 wenzelm parents: 
59083diff
changeset | 131 | (Thy_Output.string_of_margin ctxt | 
| 
bf465f335e85
system option "pretty_margin" is superseded by "thy_output_margin";
 wenzelm parents: 
59083diff
changeset | 132 | (Pretty.indent (Config.get ctxt Thy_Output.indent) p)) ^ | 
| 61877 
276ad4354069
renamed Pretty.str_of to Pretty.unformatted_string_of to emphasize its meaning;
 wenzelm parents: 
61621diff
changeset | 133 |               "\\rulename{" ^
 | 
| 
276ad4354069
renamed Pretty.str_of to Pretty.unformatted_string_of to emphasize its meaning;
 wenzelm parents: 
61621diff
changeset | 134 | Output.output (Pretty.unformatted_string_of (Thy_Output.pretty_text ctxt name)) ^ "}") | 
| 43564 
9864182c6bad
document antiquotations are managed as theory data, with proper name space and entity markup;
 wenzelm parents: 
43563diff
changeset | 135 | #> space_implode "\\par\\smallskip%\n" | 
| 61462 | 136 | #> Latex.environment "isabelle" | 
| 43564 
9864182c6bad
document antiquotations are managed as theory data, with proper name space and entity markup;
 wenzelm parents: 
43563diff
changeset | 137 | else | 
| 
9864182c6bad
document antiquotations are managed as theory data, with proper name space and entity markup;
 wenzelm parents: 
43563diff
changeset | 138 | map (fn (p, name) => | 
| 61877 
276ad4354069
renamed Pretty.str_of to Pretty.unformatted_string_of to emphasize its meaning;
 wenzelm parents: 
61621diff
changeset | 139 | Output.output (Pretty.unformatted_string_of p) ^ | 
| 
276ad4354069
renamed Pretty.str_of to Pretty.unformatted_string_of to emphasize its meaning;
 wenzelm parents: 
61621diff
changeset | 140 |               "\\rulename{" ^
 | 
| 
276ad4354069
renamed Pretty.str_of to Pretty.unformatted_string_of to emphasize its meaning;
 wenzelm parents: 
61621diff
changeset | 141 | Output.output (Pretty.unformatted_string_of (Thy_Output.pretty_text ctxt name)) ^ "}") | 
| 43564 
9864182c6bad
document antiquotations are managed as theory data, with proper name space and entity markup;
 wenzelm parents: 
43563diff
changeset | 142 | #> space_implode "\\par\\smallskip%\n" | 
| 56059 
2390391584c2
some document antiquotations for Isabelle/jEdit elements;
 wenzelm parents: 
55997diff
changeset | 143 |             #> enclose "\\isa{" "}")));
 | 
| 26742 | 144 | |
| 145 | ||
| 30394 | 146 | (* Isabelle/Isar entities (with index) *) | 
| 26751 | 147 | |
| 148 | local | |
| 149 | ||
| 61620 | 150 | fun no_check (_: Proof.context) (name, _: Position.T) = name; | 
| 26894 | 151 | |
| 61620 | 152 | fun check_keyword ctxt (name, pos) = | 
| 153 | if Keyword.is_keyword (Thy_Header.get_keywords' ctxt) name then name | |
| 154 |   else error ("Bad outer syntax keyword " ^ quote name ^ Position.here pos);
 | |
| 53061 | 155 | |
| 56467 | 156 | fun check_system_option ctxt (name, pos) = | 
| 157 | (Context_Position.report ctxt pos (Options.default_markup (name, pos)); true) | |
| 158 | handle ERROR _ => false; | |
| 159 | ||
| 26751 | 160 | val arg = enclose "{" "}" o clean_string;
 | 
| 161 | ||
| 56185 | 162 | fun entity check markup binding index = | 
| 37216 
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
 wenzelm parents: 
37198diff
changeset | 163 | Thy_Output.antiquotation | 
| 56185 | 164 | (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 | 165 | (case index of NONE => "" | SOME true => "_def" | SOME false => "_ref"))) | 
| 53044 | 166 | (Scan.lift (Scan.optional (Args.parens Args.name) "" -- Parse.position Args.name)) | 
| 167 |     (fn {context = ctxt, ...} => fn (logic, (name, pos)) =>
 | |
| 30394 | 168 | let | 
| 56185 | 169 | val kind = translate (fn "_" => " " | c => c) (Binding.name_of binding); | 
| 30394 | 170 | val hyper_name = | 
| 171 |           "{" ^ Long_Name.append kind (Long_Name.append logic (clean_name name)) ^ "}";
 | |
| 172 | val hyper = | |
| 173 |           enclose ("\\hyperlink" ^ hyper_name ^ "{") "}" #>
 | |
| 174 |           index = SOME true ? enclose ("\\hypertarget" ^ hyper_name ^ "{") "}";
 | |
| 175 | val idx = | |
| 176 | (case index of | |
| 177 | NONE => "" | |
| 178 | | SOME is_def => | |
| 179 | "\\index" ^ (if is_def then "def" else "ref") ^ arg logic ^ arg kind ^ arg name); | |
| 61620 | 180 | val _ = check ctxt (name, pos); | 
| 30394 | 181 | in | 
| 61620 | 182 | idx ^ | 
| 183 | (Output.output name | |
| 184 |           |> (if markup = "" then I else enclose ("\\" ^ markup ^ "{") "}")
 | |
| 185 |           |> hyper o enclose "\\mbox{\\isa{" "}}")
 | |
| 30394 | 186 | end); | 
| 26897 | 187 | |
| 26894 | 188 | 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 | 189 | entity check markup kind NONE #> | 
| 
9864182c6bad
document antiquotations are managed as theory data, with proper name space and entity markup;
 wenzelm parents: 
43563diff
changeset | 190 | 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 | 191 | entity check markup kind (SOME false); | 
| 26751 | 192 | |
| 193 | in | |
| 194 | ||
| 56059 
2390391584c2
some document antiquotations for Isabelle/jEdit elements;
 wenzelm parents: 
55997diff
changeset | 195 | val _ = | 
| 
2390391584c2
some document antiquotations for Isabelle/jEdit elements;
 wenzelm parents: 
55997diff
changeset | 196 | Theory.setup | 
| 56185 | 197 |    (entity_antiqs no_check "" @{binding syntax} #>
 | 
| 61620 | 198 |     entity_antiqs Outer_Syntax.check_command "isacommand" @{binding command} #>
 | 
| 199 |     entity_antiqs check_keyword "isakeyword" @{binding keyword} #>
 | |
| 200 |     entity_antiqs check_keyword "isakeyword" @{binding element} #>
 | |
| 201 |     entity_antiqs Method.check_name "" @{binding method} #>
 | |
| 202 |     entity_antiqs Attrib.check_name "" @{binding attribute} #>
 | |
| 56185 | 203 |     entity_antiqs no_check "" @{binding fact} #>
 | 
| 204 |     entity_antiqs no_check "" @{binding variable} #>
 | |
| 205 |     entity_antiqs no_check "" @{binding case} #>
 | |
| 61620 | 206 |     entity_antiqs Thy_Output.check_command "" @{binding antiquotation} #>
 | 
| 207 |     entity_antiqs Thy_Output.check_option "" @{binding antiquotation_option} #>
 | |
| 58716 
23a380cc45f4
official support for "tt" style variants, avoid fragile \verb in LaTeX;
 wenzelm parents: 
58069diff
changeset | 208 |     entity_antiqs no_check "isasystem" @{binding setting} #>
 | 
| 
23a380cc45f4
official support for "tt" style variants, avoid fragile \verb in LaTeX;
 wenzelm parents: 
58069diff
changeset | 209 |     entity_antiqs check_system_option "isasystem" @{binding system_option} #>
 | 
| 56185 | 210 |     entity_antiqs no_check "" @{binding inference} #>
 | 
| 58716 
23a380cc45f4
official support for "tt" style variants, avoid fragile \verb in LaTeX;
 wenzelm parents: 
58069diff
changeset | 211 |     entity_antiqs no_check "isasystem" @{binding executable} #>
 | 
| 62829 
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
 wenzelm parents: 
61877diff
changeset | 212 |     entity_antiqs no_check "isatool" @{binding tool} #>
 | 
| 61620 | 213 |     entity_antiqs ML_Context.check_antiquotation "" @{binding ML_antiquotation} #>
 | 
| 61621 
70b8085f51b4
more thorough check_action, including completion;
 wenzelm parents: 
61620diff
changeset | 214 |     entity_antiqs (K JEdit.check_action) "isasystem" @{binding action});
 | 
| 26751 | 215 | |
| 26742 | 216 | end; | 
| 26751 | 217 | |
| 218 | end; |