src/Doc/antiquote_setup.ML
author wenzelm
Tue Jul 01 17:59:56 2014 +0200 (2014-07-01)
changeset 57477 c3b5cb53ea79
parent 57334 54c6b73774d2
child 57918 f5d73caba4e5
permissions -rw-r--r--
redundant error position, to ensure the message is attached somewhere, despite the distortion of positions due to glued tokens;
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@57477
   102
      val pos = #pos source1;
wenzelm@56275
   103
      val _ =
wenzelm@57477
   104
        ML_Context.eval_in (SOME ctxt) ML_Compiler.flags pos (ml (toks1, toks2))
wenzelm@57477
   105
          handle ERROR msg => error (msg ^ Position.here pos);
wenzelm@30394
   106
      val kind' = if kind = "" then "ML" else "ML " ^ kind;
wenzelm@30394
   107
    in
wenzelm@39869
   108
      "\\indexdef{}{" ^ kind' ^ "}{" ^ clean_string (ml_name txt1) ^ "}" ^
wenzelm@30394
   109
      (txt'
wenzelm@38767
   110
      |> (if Config.get ctxt Thy_Output.quotes then quote else I)
wenzelm@55831
   111
      |> (if Config.get ctxt Thy_Output.display
wenzelm@55831
   112
          then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
wenzelm@30394
   113
          else split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))
wenzelm@30394
   114
    end);
wenzelm@26742
   115
wenzelm@26742
   116
in
wenzelm@21375
   117
wenzelm@56059
   118
val _ =
wenzelm@56059
   119
  Theory.setup
wenzelm@56059
   120
   (index_ml @{binding index_ML} "" ml_val #>
wenzelm@56059
   121
    index_ml @{binding index_ML_op} "infix" ml_op #>
wenzelm@56059
   122
    index_ml @{binding index_ML_type} "type" ml_type #>
wenzelm@56059
   123
    index_ml @{binding index_ML_exception} "exception" ml_exception #>
wenzelm@56059
   124
    index_ml @{binding index_ML_structure} "structure" ml_structure #>
wenzelm@56059
   125
    index_ml @{binding index_ML_functor} "functor" ml_functor);
wenzelm@21375
   126
wenzelm@26742
   127
end;
wenzelm@21375
   128
berghofe@23846
   129
wenzelm@30394
   130
(* named theorems *)
berghofe@23846
   131
wenzelm@56059
   132
val _ =
wenzelm@56059
   133
  Theory.setup (Thy_Output.antiquotation @{binding named_thms}
wenzelm@43564
   134
    (Scan.repeat (Attrib.thm -- Scan.lift (Args.parens Args.name)))
wenzelm@43564
   135
    (fn {context = ctxt, ...} =>
wenzelm@43564
   136
      map (apfst (Thy_Output.pretty_thm ctxt))
wenzelm@43564
   137
      #> (if Config.get ctxt Thy_Output.quotes then map (apfst Pretty.quote) else I)
wenzelm@43564
   138
      #> (if Config.get ctxt Thy_Output.display
wenzelm@43564
   139
          then
wenzelm@43564
   140
            map (fn (p, name) =>
wenzelm@43564
   141
              Output.output (Pretty.string_of (Pretty.indent (Config.get ctxt Thy_Output.indent) p)) ^
wenzelm@43564
   142
              "\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}")
wenzelm@43564
   143
            #> space_implode "\\par\\smallskip%\n"
wenzelm@43564
   144
            #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
wenzelm@43564
   145
          else
wenzelm@43564
   146
            map (fn (p, name) =>
wenzelm@43564
   147
              Output.output (Pretty.str_of p) ^
wenzelm@43564
   148
              "\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}")
wenzelm@43564
   149
            #> space_implode "\\par\\smallskip%\n"
wenzelm@56059
   150
            #> enclose "\\isa{" "}")));
wenzelm@26742
   151
wenzelm@26742
   152
wenzelm@30394
   153
(* theory file *)
wenzelm@26742
   154
wenzelm@56059
   155
val _ =
wenzelm@56059
   156
  Theory.setup (Thy_Output.antiquotation @{binding thy_file} (Scan.lift Args.name)
wenzelm@43564
   157
    (fn {context = ctxt, ...} =>
wenzelm@56208
   158
      fn name => (Resources.check_thy Path.current name; Thy_Output.output ctxt [Pretty.str name])));
wenzelm@56059
   159
wenzelm@56059
   160
wenzelm@56059
   161
(* Isabelle/jEdit elements *)
wenzelm@56059
   162
wenzelm@56059
   163
local
wenzelm@56059
   164
wenzelm@56059
   165
fun parse_named a (XML.Elem ((b, props), _)) =
wenzelm@56059
   166
      (case Properties.get props "NAME" of
wenzelm@56059
   167
        SOME name => if a = b then [name] else []
wenzelm@56059
   168
      | NONE => [])
wenzelm@56059
   169
  | parse_named _ _ = [];
wenzelm@56059
   170
wenzelm@57334
   171
val isabelle_jedit_actions =
wenzelm@56135
   172
  (case XML.parse (File.read @{path "~~/src/Tools/jEdit/src/actions.xml"}) of
wenzelm@56059
   173
    XML.Elem (("ACTIONS", _), body) => maps (parse_named "ACTION") body
wenzelm@56059
   174
  | _ => []);
wenzelm@56059
   175
wenzelm@57334
   176
val isabelle_jedit_dockables =
wenzelm@56135
   177
  (case XML.parse (File.read @{path "~~/src/Tools/jEdit/src/dockables.xml"}) of
wenzelm@56059
   178
    XML.Elem (("DOCKABLES", _), body) => maps (parse_named "DOCKABLE") body
wenzelm@56059
   179
  | _ => []);
wenzelm@56059
   180
wenzelm@57334
   181
val jedit_actions =
wenzelm@57334
   182
  Lazy.lazy (fn () =>
wenzelm@57334
   183
    (case Isabelle_System.bash_output
wenzelm@57334
   184
      "unzip -p \"$JEDIT_HOME/dist/jedit.jar\" org/gjt/sp/jedit/actions.xml" of
wenzelm@57334
   185
      (txt, 0) =>
wenzelm@57334
   186
        (case XML.parse txt of
wenzelm@57334
   187
          XML.Elem (("ACTIONS", _), body) => maps (parse_named "ACTION") body
wenzelm@57334
   188
        | _ => [])
wenzelm@57334
   189
    | (_, rc) => error ("Cannot unzip jedit.jar\nreturn code = " ^ string_of_int rc)));
wenzelm@56059
   190
wenzelm@56059
   191
in
wenzelm@56059
   192
wenzelm@57334
   193
fun is_action a =
wenzelm@57334
   194
  member (op =) isabelle_jedit_actions a orelse
wenzelm@57334
   195
  member (op =) isabelle_jedit_dockables a orelse
wenzelm@57334
   196
  member (op =) (Lazy.force jedit_actions) a;
wenzelm@56059
   197
wenzelm@56059
   198
end;
wenzelm@26742
   199
wenzelm@26751
   200
wenzelm@30394
   201
(* Isabelle/Isar entities (with index) *)
wenzelm@26751
   202
wenzelm@26751
   203
local
wenzelm@26751
   204
wenzelm@26894
   205
fun no_check _ _ = true;
wenzelm@26894
   206
wenzelm@56294
   207
fun check_command ctxt (name, pos) =
wenzelm@53061
   208
  is_some (Keyword.command_keyword name) andalso
wenzelm@53061
   209
    let
wenzelm@53061
   210
      val markup =
wenzelm@53061
   211
        Outer_Syntax.scan Position.none name
wenzelm@53061
   212
        |> maps (Outer_Syntax.command_reports (#2 (Outer_Syntax.get_syntax ())))
wenzelm@53061
   213
        |> map (snd o fst);
wenzelm@56294
   214
      val _ = Context_Position.reports ctxt (map (pair pos) markup);
wenzelm@53061
   215
    in true end;
wenzelm@53061
   216
wenzelm@56467
   217
fun check_system_option ctxt (name, pos) =
wenzelm@56467
   218
  (Context_Position.report ctxt pos (Options.default_markup (name, pos)); true)
wenzelm@56467
   219
    handle ERROR _ => false;
wenzelm@56467
   220
wenzelm@56294
   221
fun check_tool ctxt (name, pos) =
wenzelm@53044
   222
  let
wenzelm@53044
   223
    fun tool dir =
wenzelm@53044
   224
      let val path = Path.append dir (Path.basic name)
wenzelm@53044
   225
      in if File.exists path then SOME path else NONE end;
wenzelm@53044
   226
  in
wenzelm@53045
   227
    (case get_first tool (Path.split (getenv "ISABELLE_TOOLS")) of
wenzelm@53044
   228
      NONE => false
wenzelm@56294
   229
    | SOME path => (Context_Position.report ctxt pos (Markup.path (Path.implode path)); true))
wenzelm@53044
   230
  end;
wenzelm@28237
   231
wenzelm@26751
   232
val arg = enclose "{" "}" o clean_string;
wenzelm@26751
   233
wenzelm@56185
   234
fun entity check markup binding index =
wenzelm@37216
   235
  Thy_Output.antiquotation
wenzelm@56185
   236
    (binding |> Binding.map_name (fn name => name ^
wenzelm@43564
   237
      (case index of NONE => "" | SOME true => "_def" | SOME false => "_ref")))
wenzelm@53044
   238
    (Scan.lift (Scan.optional (Args.parens Args.name) "" -- Parse.position Args.name))
wenzelm@53044
   239
    (fn {context = ctxt, ...} => fn (logic, (name, pos)) =>
wenzelm@30394
   240
      let
wenzelm@56185
   241
        val kind = translate (fn "_" => " " | c => c) (Binding.name_of binding);
wenzelm@30394
   242
        val hyper_name =
wenzelm@30394
   243
          "{" ^ Long_Name.append kind (Long_Name.append logic (clean_name name)) ^ "}";
wenzelm@30394
   244
        val hyper =
wenzelm@30394
   245
          enclose ("\\hyperlink" ^ hyper_name ^ "{") "}" #>
wenzelm@30394
   246
          index = SOME true ? enclose ("\\hypertarget" ^ hyper_name ^ "{") "}";
wenzelm@30394
   247
        val idx =
wenzelm@30394
   248
          (case index of
wenzelm@30394
   249
            NONE => ""
wenzelm@30394
   250
          | SOME is_def =>
wenzelm@30394
   251
              "\\index" ^ (if is_def then "def" else "ref") ^ arg logic ^ arg kind ^ arg name);
wenzelm@30394
   252
      in
wenzelm@53044
   253
        if check ctxt (name, pos) then
wenzelm@30394
   254
          idx ^
wenzelm@30394
   255
          (Output.output name
wenzelm@30394
   256
            |> (if markup = "" then I else enclose ("\\" ^ markup ^ "{") "}")
wenzelm@38767
   257
            |> (if Config.get ctxt Thy_Output.quotes then quote else I)
wenzelm@38767
   258
            |> (if Config.get ctxt Thy_Output.display
wenzelm@38767
   259
                then enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
wenzelm@30394
   260
                else hyper o enclose "\\mbox{\\isa{" "}}"))
wenzelm@53044
   261
        else error ("Bad " ^ kind ^ " " ^ quote name ^ Position.here pos)
wenzelm@30394
   262
      end);
wenzelm@26897
   263
wenzelm@26894
   264
fun entity_antiqs check markup kind =
wenzelm@43564
   265
  entity check markup kind NONE #>
wenzelm@43564
   266
  entity check markup kind (SOME true) #>
wenzelm@43564
   267
  entity check markup kind (SOME false);
wenzelm@26751
   268
wenzelm@26751
   269
in
wenzelm@26751
   270
wenzelm@56059
   271
val _ =
wenzelm@56059
   272
  Theory.setup
wenzelm@56185
   273
   (entity_antiqs no_check "" @{binding syntax} #>
wenzelm@56294
   274
    entity_antiqs check_command "isacommand" @{binding command} #>
wenzelm@56185
   275
    entity_antiqs (K (Keyword.is_keyword o #1)) "isakeyword" @{binding keyword} #>
wenzelm@56185
   276
    entity_antiqs (K (Keyword.is_keyword o #1)) "isakeyword" @{binding element} #>
wenzelm@56185
   277
    entity_antiqs (can o Method.check_name) "" @{binding method} #>
wenzelm@56185
   278
    entity_antiqs (can o Attrib.check_name) "" @{binding attribute} #>
wenzelm@56185
   279
    entity_antiqs no_check "" @{binding fact} #>
wenzelm@56185
   280
    entity_antiqs no_check "" @{binding variable} #>
wenzelm@56185
   281
    entity_antiqs no_check "" @{binding case} #>
wenzelm@56185
   282
    entity_antiqs (can o Thy_Output.check_command) "" @{binding antiquotation} #>
wenzelm@56185
   283
    entity_antiqs (can o Thy_Output.check_option) "" @{binding antiquotation_option} #>
wenzelm@56185
   284
    entity_antiqs no_check "isatt" @{binding setting} #>
wenzelm@56467
   285
    entity_antiqs check_system_option "isatt" @{binding system_option} #>
wenzelm@56185
   286
    entity_antiqs no_check "" @{binding inference} #>
wenzelm@56185
   287
    entity_antiqs no_check "isatt" @{binding executable} #>
wenzelm@56294
   288
    entity_antiqs check_tool "isatool" @{binding tool} #>
wenzelm@56185
   289
    entity_antiqs (can o ML_Context.check_antiquotation) "" @{binding ML_antiquotation} #>
wenzelm@56185
   290
    entity_antiqs (K (is_action o #1)) "isatt" @{binding action});
wenzelm@26751
   291
wenzelm@26742
   292
end;
wenzelm@26751
   293
wenzelm@26751
   294
end;