src/Doc/antiquote_setup.ML
author wenzelm
Wed Mar 26 14:41:52 2014 +0100 (2014-03-26)
changeset 56294 85911b8a6868
parent 56278 2576d3a40ed6
child 56304 40274e4f5ebf
permissions -rw-r--r--
prefer Context_Position where a context is available;
prefer explicit Context_Position.is_visible -- avoid redundant message composition;
tuned signature;
wenzelm@48985
     1
(*  Title:      Doc/antiquote_setup.ML
wenzelm@21375
     2
    Author:     Makarius
wenzelm@21375
     3
wenzelm@26742
     4
Auxiliary antiquotations for the Isabelle manuals.
wenzelm@21375
     5
*)
wenzelm@21375
     6
wenzelm@56059
     7
structure Antiquote_Setup: sig end =
wenzelm@26742
     8
struct
wenzelm@21375
     9
wenzelm@26742
    10
(* misc utils *)
wenzelm@26742
    11
wenzelm@29736
    12
fun translate f = Symbol.explode #> map f #> implode;
wenzelm@29736
    13
wenzelm@29736
    14
val clean_string = translate
wenzelm@26853
    15
  (fn "_" => "\\_"
wenzelm@30120
    16
    | "#" => "\\#"
wenzelm@52408
    17
    | "$" => "\\$"
wenzelm@52408
    18
    | "%" => "\\%"
wenzelm@26897
    19
    | "<" => "$<$"
wenzelm@26768
    20
    | ">" => "$>$"
wenzelm@26768
    21
    | "{" => "\\{"
wenzelm@30120
    22
    | "|" => "$\\mid$"
wenzelm@26768
    23
    | "}" => "\\}"
wenzelm@42666
    24
    | "\<hyphen>" => "-"
wenzelm@26768
    25
    | c => c);
wenzelm@26751
    26
wenzelm@31546
    27
fun clean_name "\<dots>" = "dots"
wenzelm@26897
    28
  | clean_name ".." = "ddot"
wenzelm@26897
    29
  | clean_name "." = "dot"
wenzelm@26910
    30
  | clean_name "_" = "underscore"
wenzelm@26897
    31
  | clean_name "{" = "braceleft"
wenzelm@26897
    32
  | clean_name "}" = "braceright"
wenzelm@42666
    33
  | clean_name s = s |> translate (fn "_" => "-" | "\<hyphen>" => "-" | c => c);
wenzelm@26897
    34
wenzelm@26742
    35
wenzelm@26742
    36
(* verbatim text *)
wenzelm@26742
    37
wenzelm@26742
    38
val verbatim = space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|";
wenzelm@26742
    39
wenzelm@56059
    40
val _ =
wenzelm@56059
    41
  Theory.setup (Thy_Output.antiquotation @{binding verbatim} (Scan.lift Args.name)
wenzelm@56059
    42
    (K (split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n")));
wenzelm@26742
    43
wenzelm@26742
    44
wenzelm@26742
    45
(* ML text *)
wenzelm@26742
    46
wenzelm@26742
    47
local
wenzelm@26742
    48
wenzelm@55831
    49
val ml_toks = ML_Lex.read Position.none;
wenzelm@21375
    50
wenzelm@55831
    51
fun ml_val (toks1, []) = ml_toks "fn _ => (" @ toks1 @ ml_toks ");"
wenzelm@55831
    52
  | ml_val (toks1, toks2) = ml_toks "fn _ => (" @ toks1 @ ml_toks " : " @ toks2 @ ml_toks ");";
wenzelm@55831
    53
wenzelm@55831
    54
fun ml_op (toks1, []) = ml_toks "fn _ => (op " @ toks1 @ ml_toks ");"
wenzelm@55831
    55
  | ml_op (toks1, toks2) = ml_toks "fn _ => (op " @ toks1 @ ml_toks " : " @ toks2 @ ml_toks ");";
wenzelm@46261
    56
wenzelm@55831
    57
fun ml_type (toks1, []) = ml_toks "val _ = NONE : (" @ toks1 @ ml_toks ") option;"
wenzelm@55831
    58
  | ml_type (toks1, toks2) =
wenzelm@55831
    59
      ml_toks "val _ = [NONE : (" @ toks1 @ ml_toks ") option, NONE : (" @
wenzelm@55831
    60
        toks2 @ ml_toks ") option];";
haftmann@22289
    61
wenzelm@55837
    62
fun ml_exception (toks1, []) = ml_toks "fn _ => (" @ toks1 @ ml_toks " : exn);"
wenzelm@55837
    63
  | ml_exception (toks1, toks2) =
wenzelm@55831
    64
      ml_toks "fn _ => (" @ toks1 @ ml_toks " : " @ toks2 @ ml_toks " -> exn);";
haftmann@22289
    65
wenzelm@55831
    66
fun ml_structure (toks, _) =
wenzelm@55831
    67
  ml_toks "functor XXX() = struct structure XX = " @ toks @ ml_toks " end;";
wenzelm@21375
    68
wenzelm@55831
    69
fun ml_functor (Antiquote.Text tok :: _, _) =
wenzelm@55831
    70
      ml_toks "ML_Env.check_functor " @ ml_toks (ML_Syntax.print_string (ML_Lex.content_of tok))
wenzelm@55831
    71
  | ml_functor _ = raise Fail "Bad ML functor specification";
wenzelm@21375
    72
wenzelm@39869
    73
val is_name = ML_Lex.kind_of #> (fn kind => kind = ML_Lex.Ident orelse kind = ML_Lex.LongIdent);
wenzelm@39869
    74
wenzelm@39869
    75
fun ml_name txt =
wenzelm@39869
    76
  (case filter is_name (ML_Lex.tokenize txt) of
wenzelm@39869
    77
    toks as [_] => ML_Lex.flatten toks
wenzelm@39869
    78
  | _ => error ("Single ML name expected in input: " ^ quote txt));
wenzelm@39869
    79
wenzelm@55831
    80
fun prep_ml source =
wenzelm@56278
    81
  (#1 (Symbol_Pos.source_content source), ML_Lex.read_source false source);
wenzelm@55831
    82
wenzelm@37216
    83
fun index_ml name kind ml = Thy_Output.antiquotation name
wenzelm@55831
    84
  (Scan.lift (Args.name_source_position -- Scan.option (Args.colon |-- Args.name_source_position)))
wenzelm@55831
    85
  (fn {context = ctxt, ...} => fn (source1, opt_source2) =>
wenzelm@30394
    86
    let
wenzelm@55831
    87
      val (txt1, toks1) = prep_ml source1;
wenzelm@55831
    88
      val (txt2, toks2) =
wenzelm@55831
    89
        (case opt_source2 of
wenzelm@55831
    90
          SOME source => prep_ml source
wenzelm@55831
    91
        | NONE => ("", []));
wenzelm@55831
    92
wenzelm@30394
    93
      val txt =
wenzelm@30394
    94
        if txt2 = "" then txt1
wenzelm@30394
    95
        else if kind = "type" then txt1 ^ " = " ^ txt2
wenzelm@30394
    96
        else if kind = "exception" then txt1 ^ " of " ^ txt2
wenzelm@50239
    97
        else if Symbol_Pos.is_identifier (Long_Name.base_name (ml_name txt1))
wenzelm@42290
    98
        then txt1 ^ ": " ^ txt2
wenzelm@39858
    99
        else txt1 ^ " : " ^ txt2;
wenzelm@30394
   100
      val txt' = if kind = "" then txt else kind ^ " " ^ txt;
wenzelm@55831
   101
wenzelm@56275
   102
      val _ =
wenzelm@56275
   103
        ML_Context.eval_in (SOME ctxt) {SML = false, verbose = false} (#pos source1)
wenzelm@56275
   104
          (ml (toks1, toks2));
wenzelm@30394
   105
      val kind' = if kind = "" then "ML" else "ML " ^ kind;
wenzelm@30394
   106
    in
wenzelm@39869
   107
      "\\indexdef{}{" ^ kind' ^ "}{" ^ clean_string (ml_name txt1) ^ "}" ^
wenzelm@30394
   108
      (txt'
wenzelm@38767
   109
      |> (if Config.get ctxt Thy_Output.quotes then quote else I)
wenzelm@55831
   110
      |> (if Config.get ctxt Thy_Output.display
wenzelm@55831
   111
          then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
wenzelm@30394
   112
          else split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))
wenzelm@30394
   113
    end);
wenzelm@26742
   114
wenzelm@26742
   115
in
wenzelm@21375
   116
wenzelm@56059
   117
val _ =
wenzelm@56059
   118
  Theory.setup
wenzelm@56059
   119
   (index_ml @{binding index_ML} "" ml_val #>
wenzelm@56059
   120
    index_ml @{binding index_ML_op} "infix" ml_op #>
wenzelm@56059
   121
    index_ml @{binding index_ML_type} "type" ml_type #>
wenzelm@56059
   122
    index_ml @{binding index_ML_exception} "exception" ml_exception #>
wenzelm@56059
   123
    index_ml @{binding index_ML_structure} "structure" ml_structure #>
wenzelm@56059
   124
    index_ml @{binding index_ML_functor} "functor" ml_functor);
wenzelm@21375
   125
wenzelm@26742
   126
end;
wenzelm@21375
   127
berghofe@23846
   128
wenzelm@30394
   129
(* named theorems *)
berghofe@23846
   130
wenzelm@56059
   131
val _ =
wenzelm@56059
   132
  Theory.setup (Thy_Output.antiquotation @{binding named_thms}
wenzelm@43564
   133
    (Scan.repeat (Attrib.thm -- Scan.lift (Args.parens Args.name)))
wenzelm@43564
   134
    (fn {context = ctxt, ...} =>
wenzelm@43564
   135
      map (apfst (Thy_Output.pretty_thm ctxt))
wenzelm@43564
   136
      #> (if Config.get ctxt Thy_Output.quotes then map (apfst Pretty.quote) else I)
wenzelm@43564
   137
      #> (if Config.get ctxt Thy_Output.display
wenzelm@43564
   138
          then
wenzelm@43564
   139
            map (fn (p, name) =>
wenzelm@43564
   140
              Output.output (Pretty.string_of (Pretty.indent (Config.get ctxt Thy_Output.indent) p)) ^
wenzelm@43564
   141
              "\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}")
wenzelm@43564
   142
            #> space_implode "\\par\\smallskip%\n"
wenzelm@43564
   143
            #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
wenzelm@43564
   144
          else
wenzelm@43564
   145
            map (fn (p, name) =>
wenzelm@43564
   146
              Output.output (Pretty.str_of p) ^
wenzelm@43564
   147
              "\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}")
wenzelm@43564
   148
            #> space_implode "\\par\\smallskip%\n"
wenzelm@56059
   149
            #> enclose "\\isa{" "}")));
wenzelm@26742
   150
wenzelm@26742
   151
wenzelm@30394
   152
(* theory file *)
wenzelm@26742
   153
wenzelm@56059
   154
val _ =
wenzelm@56059
   155
  Theory.setup (Thy_Output.antiquotation @{binding thy_file} (Scan.lift Args.name)
wenzelm@43564
   156
    (fn {context = ctxt, ...} =>
wenzelm@56208
   157
      fn name => (Resources.check_thy Path.current name; Thy_Output.output ctxt [Pretty.str name])));
wenzelm@56059
   158
wenzelm@56059
   159
wenzelm@56059
   160
(* Isabelle/jEdit elements *)
wenzelm@56059
   161
wenzelm@56059
   162
local
wenzelm@56059
   163
wenzelm@56059
   164
fun parse_named a (XML.Elem ((b, props), _)) =
wenzelm@56059
   165
      (case Properties.get props "NAME" of
wenzelm@56059
   166
        SOME name => if a = b then [name] else []
wenzelm@56059
   167
      | NONE => [])
wenzelm@56059
   168
  | parse_named _ _ = [];
wenzelm@56059
   169
wenzelm@56059
   170
val jedit_actions =
wenzelm@56135
   171
  (case XML.parse (File.read @{path "~~/src/Tools/jEdit/src/actions.xml"}) of
wenzelm@56059
   172
    XML.Elem (("ACTIONS", _), body) => maps (parse_named "ACTION") body
wenzelm@56059
   173
  | _ => []);
wenzelm@56059
   174
wenzelm@56059
   175
val jedit_dockables =
wenzelm@56135
   176
  (case XML.parse (File.read @{path "~~/src/Tools/jEdit/src/dockables.xml"}) of
wenzelm@56059
   177
    XML.Elem (("DOCKABLES", _), body) => maps (parse_named "DOCKABLE") body
wenzelm@56059
   178
  | _ => []);
wenzelm@56059
   179
wenzelm@56059
   180
val all_actions = jedit_actions @ jedit_dockables;
wenzelm@56059
   181
wenzelm@56059
   182
in
wenzelm@56059
   183
wenzelm@56060
   184
val is_action = member (op =) all_actions;
wenzelm@56059
   185
wenzelm@56059
   186
end;
wenzelm@26742
   187
wenzelm@26751
   188
wenzelm@30394
   189
(* Isabelle/Isar entities (with index) *)
wenzelm@26751
   190
wenzelm@26751
   191
local
wenzelm@26751
   192
wenzelm@26894
   193
fun no_check _ _ = true;
wenzelm@26894
   194
wenzelm@56294
   195
fun check_command ctxt (name, pos) =
wenzelm@53061
   196
  is_some (Keyword.command_keyword name) andalso
wenzelm@53061
   197
    let
wenzelm@53061
   198
      val markup =
wenzelm@53061
   199
        Outer_Syntax.scan Position.none name
wenzelm@53061
   200
        |> maps (Outer_Syntax.command_reports (#2 (Outer_Syntax.get_syntax ())))
wenzelm@53061
   201
        |> map (snd o fst);
wenzelm@56294
   202
      val _ = Context_Position.reports ctxt (map (pair pos) markup);
wenzelm@53061
   203
    in true end;
wenzelm@53061
   204
wenzelm@56294
   205
fun check_tool ctxt (name, pos) =
wenzelm@53044
   206
  let
wenzelm@53044
   207
    fun tool dir =
wenzelm@53044
   208
      let val path = Path.append dir (Path.basic name)
wenzelm@53044
   209
      in if File.exists path then SOME path else NONE end;
wenzelm@53044
   210
  in
wenzelm@53045
   211
    (case get_first tool (Path.split (getenv "ISABELLE_TOOLS")) of
wenzelm@53044
   212
      NONE => false
wenzelm@56294
   213
    | SOME path => (Context_Position.report ctxt pos (Markup.path (Path.implode path)); true))
wenzelm@53044
   214
  end;
wenzelm@28237
   215
wenzelm@26751
   216
val arg = enclose "{" "}" o clean_string;
wenzelm@26751
   217
wenzelm@56185
   218
fun entity check markup binding index =
wenzelm@37216
   219
  Thy_Output.antiquotation
wenzelm@56185
   220
    (binding |> Binding.map_name (fn name => name ^
wenzelm@43564
   221
      (case index of NONE => "" | SOME true => "_def" | SOME false => "_ref")))
wenzelm@53044
   222
    (Scan.lift (Scan.optional (Args.parens Args.name) "" -- Parse.position Args.name))
wenzelm@53044
   223
    (fn {context = ctxt, ...} => fn (logic, (name, pos)) =>
wenzelm@30394
   224
      let
wenzelm@56185
   225
        val kind = translate (fn "_" => " " | c => c) (Binding.name_of binding);
wenzelm@30394
   226
        val hyper_name =
wenzelm@30394
   227
          "{" ^ Long_Name.append kind (Long_Name.append logic (clean_name name)) ^ "}";
wenzelm@30394
   228
        val hyper =
wenzelm@30394
   229
          enclose ("\\hyperlink" ^ hyper_name ^ "{") "}" #>
wenzelm@30394
   230
          index = SOME true ? enclose ("\\hypertarget" ^ hyper_name ^ "{") "}";
wenzelm@30394
   231
        val idx =
wenzelm@30394
   232
          (case index of
wenzelm@30394
   233
            NONE => ""
wenzelm@30394
   234
          | SOME is_def =>
wenzelm@30394
   235
              "\\index" ^ (if is_def then "def" else "ref") ^ arg logic ^ arg kind ^ arg name);
wenzelm@30394
   236
      in
wenzelm@53044
   237
        if check ctxt (name, pos) then
wenzelm@30394
   238
          idx ^
wenzelm@30394
   239
          (Output.output name
wenzelm@30394
   240
            |> (if markup = "" then I else enclose ("\\" ^ markup ^ "{") "}")
wenzelm@38767
   241
            |> (if Config.get ctxt Thy_Output.quotes then quote else I)
wenzelm@38767
   242
            |> (if Config.get ctxt Thy_Output.display
wenzelm@38767
   243
                then enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
wenzelm@30394
   244
                else hyper o enclose "\\mbox{\\isa{" "}}"))
wenzelm@53044
   245
        else error ("Bad " ^ kind ^ " " ^ quote name ^ Position.here pos)
wenzelm@30394
   246
      end);
wenzelm@26897
   247
wenzelm@26894
   248
fun entity_antiqs check markup kind =
wenzelm@43564
   249
  entity check markup kind NONE #>
wenzelm@43564
   250
  entity check markup kind (SOME true) #>
wenzelm@43564
   251
  entity check markup kind (SOME false);
wenzelm@26751
   252
wenzelm@26751
   253
in
wenzelm@26751
   254
wenzelm@56059
   255
val _ =
wenzelm@56059
   256
  Theory.setup
wenzelm@56185
   257
   (entity_antiqs no_check "" @{binding syntax} #>
wenzelm@56294
   258
    entity_antiqs check_command "isacommand" @{binding command} #>
wenzelm@56185
   259
    entity_antiqs (K (Keyword.is_keyword o #1)) "isakeyword" @{binding keyword} #>
wenzelm@56185
   260
    entity_antiqs (K (Keyword.is_keyword o #1)) "isakeyword" @{binding element} #>
wenzelm@56185
   261
    entity_antiqs (can o Method.check_name) "" @{binding method} #>
wenzelm@56185
   262
    entity_antiqs (can o Attrib.check_name) "" @{binding attribute} #>
wenzelm@56185
   263
    entity_antiqs no_check "" @{binding fact} #>
wenzelm@56185
   264
    entity_antiqs no_check "" @{binding variable} #>
wenzelm@56185
   265
    entity_antiqs no_check "" @{binding case} #>
wenzelm@56185
   266
    entity_antiqs (can o Thy_Output.check_command) "" @{binding antiquotation} #>
wenzelm@56185
   267
    entity_antiqs (can o Thy_Output.check_option) "" @{binding antiquotation_option} #>
wenzelm@56185
   268
    entity_antiqs no_check "isatt" @{binding setting} #>
wenzelm@56185
   269
    entity_antiqs no_check "isatt" @{binding system_option} #>
wenzelm@56185
   270
    entity_antiqs no_check "" @{binding inference} #>
wenzelm@56185
   271
    entity_antiqs no_check "isatt" @{binding executable} #>
wenzelm@56294
   272
    entity_antiqs check_tool "isatool" @{binding tool} #>
wenzelm@56185
   273
    entity_antiqs (can o ML_Context.check_antiquotation) "" @{binding ML_antiquotation} #>
wenzelm@56185
   274
    entity_antiqs (K (is_action o #1)) "isatt" @{binding action});
wenzelm@26751
   275
wenzelm@26742
   276
end;
wenzelm@26751
   277
wenzelm@26751
   278
end;