1 (* Title: Doc/antiquote_setup.ML
4 Auxiliary antiquotations for the Isabelle manuals.
7 structure Antiquote_Setup: sig end =
12 fun translate f = Symbol.explode #> map f #> implode;
14 val clean_string = translate
27 fun clean_name "\<dots>" = "dots"
28 | clean_name ".." = "ddot"
29 | clean_name "." = "dot"
30 | clean_name "_" = "underscore"
31 | clean_name "{" = "braceleft"
32 | clean_name "}" = "braceright"
33 | clean_name s = s |> translate (fn "_" => "-" | "\<hyphen>" => "-" | c => c);
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 ");";
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 ");";
48 fun ml_type (toks1, []) = ML_Lex.read "val _ = NONE : (" @ toks1 @ ML_Lex.read ") option;"
49 | ml_type (toks1, toks2) =
50 ML_Lex.read "val _ = [NONE : (" @ toks1 @ ML_Lex.read ") option, NONE : (" @
51 toks2 @ ML_Lex.read ") option];";
53 fun ml_exception (toks1, []) = ML_Lex.read "fn _ => (" @ toks1 @ ML_Lex.read " : exn);"
54 | ml_exception (toks1, toks2) =
55 ML_Lex.read "fn _ => (" @ toks1 @ ML_Lex.read " : " @ toks2 @ ML_Lex.read " -> exn);";
57 fun ml_structure (toks, _) =
58 ML_Lex.read "functor XXX() = struct structure XX = " @ toks @ ML_Lex.read " end;";
60 fun ml_functor (Antiquote.Text tok :: _, _) =
61 ML_Lex.read "ML_Env.check_functor " @
62 ML_Lex.read (ML_Syntax.print_string (ML_Lex.content_of tok))
63 | ml_functor _ = raise Fail "Bad ML functor specification";
66 ML_Lex.kind_of #> (fn kind => kind = ML_Lex.Ident orelse kind = ML_Lex.Long_Ident);
69 (case filter is_name (ML_Lex.tokenize txt) of
70 toks as [_] => ML_Lex.flatten toks
71 | _ => error ("Single ML name expected in input: " ^ quote txt));
74 (Input.source_content source, ML_Lex.read_source false source);
76 fun index_ml name kind ml = Thy_Output.antiquotation name
77 (Scan.lift (Args.text_input -- Scan.option (Args.colon |-- Args.text_input)))
78 (fn {context = ctxt, ...} => fn (source1, opt_source2) =>
80 val (txt1, toks1) = prep_ml source1;
83 SOME source => prep_ml source
87 if txt2 = "" then txt1
88 else if kind = "type" then txt1 ^ " = " ^ txt2
89 else if kind = "exception" then txt1 ^ " of " ^ txt2
90 else if Symbol_Pos.is_identifier (Long_Name.base_name (ml_name txt1))
91 then txt1 ^ ": " ^ txt2
92 else txt1 ^ " : " ^ txt2;
93 val txt' = if kind = "" then txt else kind ^ " " ^ txt;
95 val pos = Input.pos_of source1;
97 ML_Context.eval_in (SOME ctxt) ML_Compiler.flags pos (ml (toks1, toks2))
98 handle ERROR msg => error (msg ^ Position.here pos);
99 val kind' = if kind = "" then "ML" else "ML " ^ kind;
101 "\\indexdef{}{" ^ kind' ^ "}{" ^ clean_string (ml_name txt1) ^ "}" ^
102 (Thy_Output.verbatim_text ctxt txt')
109 (index_ml @{binding index_ML} "" ml_val #>
110 index_ml @{binding index_ML_op} "infix" ml_op #>
111 index_ml @{binding index_ML_type} "type" ml_type #>
112 index_ml @{binding index_ML_exception} "exception" ml_exception #>
113 index_ml @{binding index_ML_structure} "structure" ml_structure #>
114 index_ml @{binding index_ML_functor} "functor" ml_functor);
122 Theory.setup (Thy_Output.antiquotation @{binding named_thms}
123 (Scan.repeat (Attrib.thm -- Scan.lift (Args.parens Args.name)))
124 (fn {context = ctxt, ...} =>
125 map (apfst (Thy_Output.pretty_thm ctxt))
126 #> (if Config.get ctxt Thy_Output.quotes then map (apfst Pretty.quote) else I)
127 #> (if Config.get ctxt Thy_Output.display
131 (Thy_Output.string_of_margin ctxt
132 (Pretty.indent (Config.get ctxt Thy_Output.indent) p)) ^
134 Output.output (Pretty.unformatted_string_of (Thy_Output.pretty_text ctxt name)) ^ "}")
135 #> space_implode "\\par\\smallskip%\n"
136 #> Latex.environment "isabelle"
139 Output.output (Pretty.unformatted_string_of p) ^
141 Output.output (Pretty.unformatted_string_of (Thy_Output.pretty_text ctxt name)) ^ "}")
142 #> space_implode "\\par\\smallskip%\n"
143 #> enclose "\\isa{" "}")));
146 (* Isabelle/Isar entities (with index) *)
150 fun no_check (_: Proof.context) (name, _: Position.T) = name;
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);
156 fun check_system_option ctxt (name, pos) =
157 (Context_Position.report ctxt pos (Options.default_markup (name, pos)); true)
158 handle ERROR _ => false;
160 val arg = enclose "{" "}" o clean_string;
162 fun entity check markup binding index =
163 Thy_Output.antiquotation
164 (binding |> Binding.map_name (fn name => name ^
165 (case index of NONE => "" | SOME true => "_def" | SOME false => "_ref")))
166 (Scan.lift (Scan.optional (Args.parens Args.name) "" -- Parse.position Args.name))
167 (fn {context = ctxt, ...} => fn (logic, (name, pos)) =>
169 val kind = translate (fn "_" => " " | c => c) (Binding.name_of binding);
171 "{" ^ Long_Name.append kind (Long_Name.append logic (clean_name name)) ^ "}";
173 enclose ("\\hyperlink" ^ hyper_name ^ "{") "}" #>
174 index = SOME true ? enclose ("\\hypertarget" ^ hyper_name ^ "{") "}";
179 "\\index" ^ (if is_def then "def" else "ref") ^ arg logic ^ arg kind ^ arg name);
180 val _ = check ctxt (name, pos);
184 |> (if markup = "" then I else enclose ("\\" ^ markup ^ "{") "}")
185 |> hyper o enclose "\\mbox{\\isa{" "}}")
188 fun entity_antiqs check markup kind =
189 entity check markup kind NONE #>
190 entity check markup kind (SOME true) #>
191 entity check markup kind (SOME false);
197 (entity_antiqs no_check "" @{binding syntax} #>
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} #>
203 entity_antiqs no_check "" @{binding fact} #>
204 entity_antiqs no_check "" @{binding variable} #>
205 entity_antiqs no_check "" @{binding case} #>
206 entity_antiqs Thy_Output.check_command "" @{binding antiquotation} #>
207 entity_antiqs Thy_Output.check_option "" @{binding antiquotation_option} #>
208 entity_antiqs no_check "isasystem" @{binding setting} #>
209 entity_antiqs check_system_option "isasystem" @{binding system_option} #>
210 entity_antiqs no_check "" @{binding inference} #>
211 entity_antiqs no_check "isasystem" @{binding executable} #>
212 entity_antiqs no_check "isatool" @{binding tool} #>
213 entity_antiqs ML_Context.check_antiquotation "" @{binding ML_antiquotation} #>
214 entity_antiqs (K JEdit.check_action) "isasystem" @{binding action});