src/Doc/antiquote_setup.ML
author wenzelm
Thu Nov 06 15:47:04 2014 +0100 (2014-11-06)
changeset 58923 cb9b69cca999
parent 58919 82a71046dce8
child 58928 23d0ffd48006
permissions -rw-r--r--
more explicit Keyword.keywords;
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
(* ML text *)
wenzelm@26742
    37
wenzelm@26742
    38
local
wenzelm@26742
    39
wenzelm@55831
    40
val ml_toks = ML_Lex.read Position.none;
wenzelm@21375
    41
wenzelm@55831
    42
fun ml_val (toks1, []) = ml_toks "fn _ => (" @ toks1 @ ml_toks ");"
wenzelm@55831
    43
  | ml_val (toks1, toks2) = ml_toks "fn _ => (" @ toks1 @ ml_toks " : " @ toks2 @ ml_toks ");";
wenzelm@55831
    44
wenzelm@55831
    45
fun ml_op (toks1, []) = ml_toks "fn _ => (op " @ toks1 @ ml_toks ");"
wenzelm@55831
    46
  | ml_op (toks1, toks2) = ml_toks "fn _ => (op " @ toks1 @ ml_toks " : " @ toks2 @ ml_toks ");";
wenzelm@46261
    47
wenzelm@55831
    48
fun ml_type (toks1, []) = ml_toks "val _ = NONE : (" @ toks1 @ ml_toks ") option;"
wenzelm@55831
    49
  | ml_type (toks1, toks2) =
wenzelm@55831
    50
      ml_toks "val _ = [NONE : (" @ toks1 @ ml_toks ") option, NONE : (" @
wenzelm@55831
    51
        toks2 @ ml_toks ") option];";
haftmann@22289
    52
wenzelm@55837
    53
fun ml_exception (toks1, []) = ml_toks "fn _ => (" @ toks1 @ ml_toks " : exn);"
wenzelm@55837
    54
  | ml_exception (toks1, toks2) =
wenzelm@55831
    55
      ml_toks "fn _ => (" @ toks1 @ ml_toks " : " @ toks2 @ ml_toks " -> exn);";
haftmann@22289
    56
wenzelm@55831
    57
fun ml_structure (toks, _) =
wenzelm@55831
    58
  ml_toks "functor XXX() = struct structure XX = " @ toks @ ml_toks " end;";
wenzelm@21375
    59
wenzelm@55831
    60
fun ml_functor (Antiquote.Text tok :: _, _) =
wenzelm@55831
    61
      ml_toks "ML_Env.check_functor " @ ml_toks (ML_Syntax.print_string (ML_Lex.content_of tok))
wenzelm@55831
    62
  | ml_functor _ = raise Fail "Bad ML functor specification";
wenzelm@21375
    63
wenzelm@39869
    64
val is_name = ML_Lex.kind_of #> (fn kind => kind = ML_Lex.Ident orelse kind = ML_Lex.LongIdent);
wenzelm@39869
    65
wenzelm@39869
    66
fun ml_name txt =
wenzelm@39869
    67
  (case filter is_name (ML_Lex.tokenize txt) of
wenzelm@39869
    68
    toks as [_] => ML_Lex.flatten toks
wenzelm@39869
    69
  | _ => error ("Single ML name expected in input: " ^ quote txt));
wenzelm@39869
    70
wenzelm@55831
    71
fun prep_ml source =
wenzelm@56278
    72
  (#1 (Symbol_Pos.source_content source), ML_Lex.read_source false source);
wenzelm@55831
    73
wenzelm@37216
    74
fun index_ml name kind ml = Thy_Output.antiquotation name
wenzelm@58069
    75
  (Scan.lift (Args.text_source_position -- Scan.option (Args.colon |-- Args.text_source_position)))
wenzelm@55831
    76
  (fn {context = ctxt, ...} => fn (source1, opt_source2) =>
wenzelm@30394
    77
    let
wenzelm@55831
    78
      val (txt1, toks1) = prep_ml source1;
wenzelm@55831
    79
      val (txt2, toks2) =
wenzelm@55831
    80
        (case opt_source2 of
wenzelm@55831
    81
          SOME source => prep_ml source
wenzelm@55831
    82
        | NONE => ("", []));
wenzelm@55831
    83
wenzelm@30394
    84
      val txt =
wenzelm@30394
    85
        if txt2 = "" then txt1
wenzelm@30394
    86
        else if kind = "type" then txt1 ^ " = " ^ txt2
wenzelm@30394
    87
        else if kind = "exception" then txt1 ^ " of " ^ txt2
wenzelm@50239
    88
        else if Symbol_Pos.is_identifier (Long_Name.base_name (ml_name txt1))
wenzelm@42290
    89
        then txt1 ^ ": " ^ txt2
wenzelm@39858
    90
        else txt1 ^ " : " ^ txt2;
wenzelm@30394
    91
      val txt' = if kind = "" then txt else kind ^ " " ^ txt;
wenzelm@55831
    92
wenzelm@57477
    93
      val pos = #pos source1;
wenzelm@56275
    94
      val _ =
wenzelm@57477
    95
        ML_Context.eval_in (SOME ctxt) ML_Compiler.flags pos (ml (toks1, toks2))
wenzelm@57477
    96
          handle ERROR msg => error (msg ^ Position.here pos);
wenzelm@30394
    97
      val kind' = if kind = "" then "ML" else "ML " ^ kind;
wenzelm@30394
    98
    in
wenzelm@39869
    99
      "\\indexdef{}{" ^ kind' ^ "}{" ^ clean_string (ml_name txt1) ^ "}" ^
wenzelm@58716
   100
      (Thy_Output.verbatim_text ctxt txt')
wenzelm@30394
   101
    end);
wenzelm@26742
   102
wenzelm@26742
   103
in
wenzelm@21375
   104
wenzelm@56059
   105
val _ =
wenzelm@56059
   106
  Theory.setup
wenzelm@56059
   107
   (index_ml @{binding index_ML} "" ml_val #>
wenzelm@56059
   108
    index_ml @{binding index_ML_op} "infix" ml_op #>
wenzelm@56059
   109
    index_ml @{binding index_ML_type} "type" ml_type #>
wenzelm@56059
   110
    index_ml @{binding index_ML_exception} "exception" ml_exception #>
wenzelm@56059
   111
    index_ml @{binding index_ML_structure} "structure" ml_structure #>
wenzelm@56059
   112
    index_ml @{binding index_ML_functor} "functor" ml_functor);
wenzelm@21375
   113
wenzelm@26742
   114
end;
wenzelm@21375
   115
berghofe@23846
   116
wenzelm@30394
   117
(* named theorems *)
berghofe@23846
   118
wenzelm@56059
   119
val _ =
wenzelm@56059
   120
  Theory.setup (Thy_Output.antiquotation @{binding named_thms}
wenzelm@43564
   121
    (Scan.repeat (Attrib.thm -- Scan.lift (Args.parens Args.name)))
wenzelm@43564
   122
    (fn {context = ctxt, ...} =>
wenzelm@43564
   123
      map (apfst (Thy_Output.pretty_thm ctxt))
wenzelm@43564
   124
      #> (if Config.get ctxt Thy_Output.quotes then map (apfst Pretty.quote) else I)
wenzelm@43564
   125
      #> (if Config.get ctxt Thy_Output.display
wenzelm@43564
   126
          then
wenzelm@43564
   127
            map (fn (p, name) =>
wenzelm@43564
   128
              Output.output (Pretty.string_of (Pretty.indent (Config.get ctxt Thy_Output.indent) p)) ^
wenzelm@43564
   129
              "\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}")
wenzelm@43564
   130
            #> space_implode "\\par\\smallskip%\n"
wenzelm@43564
   131
            #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
wenzelm@43564
   132
          else
wenzelm@43564
   133
            map (fn (p, name) =>
wenzelm@43564
   134
              Output.output (Pretty.str_of p) ^
wenzelm@43564
   135
              "\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}")
wenzelm@43564
   136
            #> space_implode "\\par\\smallskip%\n"
wenzelm@56059
   137
            #> enclose "\\isa{" "}")));
wenzelm@26742
   138
wenzelm@26742
   139
wenzelm@30394
   140
(* theory file *)
wenzelm@26742
   141
wenzelm@56059
   142
val _ =
wenzelm@56059
   143
  Theory.setup (Thy_Output.antiquotation @{binding thy_file} (Scan.lift Args.name)
wenzelm@43564
   144
    (fn {context = ctxt, ...} =>
wenzelm@56208
   145
      fn name => (Resources.check_thy Path.current name; Thy_Output.output ctxt [Pretty.str name])));
wenzelm@56059
   146
wenzelm@56059
   147
wenzelm@56059
   148
(* Isabelle/jEdit elements *)
wenzelm@56059
   149
wenzelm@56059
   150
local
wenzelm@56059
   151
wenzelm@56059
   152
fun parse_named a (XML.Elem ((b, props), _)) =
wenzelm@56059
   153
      (case Properties.get props "NAME" of
wenzelm@56059
   154
        SOME name => if a = b then [name] else []
wenzelm@56059
   155
      | NONE => [])
wenzelm@56059
   156
  | parse_named _ _ = [];
wenzelm@56059
   157
wenzelm@57334
   158
val isabelle_jedit_actions =
wenzelm@56135
   159
  (case XML.parse (File.read @{path "~~/src/Tools/jEdit/src/actions.xml"}) of
wenzelm@56059
   160
    XML.Elem (("ACTIONS", _), body) => maps (parse_named "ACTION") body
wenzelm@56059
   161
  | _ => []);
wenzelm@56059
   162
wenzelm@57334
   163
val isabelle_jedit_dockables =
wenzelm@56135
   164
  (case XML.parse (File.read @{path "~~/src/Tools/jEdit/src/dockables.xml"}) of
wenzelm@56059
   165
    XML.Elem (("DOCKABLES", _), body) => maps (parse_named "DOCKABLE") body
wenzelm@56059
   166
  | _ => []);
wenzelm@56059
   167
wenzelm@57334
   168
val jedit_actions =
wenzelm@57334
   169
  Lazy.lazy (fn () =>
wenzelm@57334
   170
    (case Isabelle_System.bash_output
wenzelm@57334
   171
      "unzip -p \"$JEDIT_HOME/dist/jedit.jar\" org/gjt/sp/jedit/actions.xml" of
wenzelm@57334
   172
      (txt, 0) =>
wenzelm@57334
   173
        (case XML.parse txt of
wenzelm@57334
   174
          XML.Elem (("ACTIONS", _), body) => maps (parse_named "ACTION") body
wenzelm@57334
   175
        | _ => [])
wenzelm@57334
   176
    | (_, rc) => error ("Cannot unzip jedit.jar\nreturn code = " ^ string_of_int rc)));
wenzelm@56059
   177
wenzelm@56059
   178
in
wenzelm@56059
   179
wenzelm@57334
   180
fun is_action a =
wenzelm@57334
   181
  member (op =) isabelle_jedit_actions a orelse
wenzelm@57334
   182
  member (op =) isabelle_jedit_dockables a orelse
wenzelm@57334
   183
  member (op =) (Lazy.force jedit_actions) a;
wenzelm@56059
   184
wenzelm@56059
   185
end;
wenzelm@26742
   186
wenzelm@26751
   187
wenzelm@30394
   188
(* Isabelle/Isar entities (with index) *)
wenzelm@26751
   189
wenzelm@26751
   190
local
wenzelm@26751
   191
wenzelm@26894
   192
fun no_check _ _ = true;
wenzelm@26894
   193
wenzelm@58919
   194
fun is_keyword _ (name, _) =
wenzelm@58919
   195
  Keyword.is_keyword (Keyword.get_keywords ()) name;
wenzelm@58919
   196
wenzelm@56294
   197
fun check_command ctxt (name, pos) =
wenzelm@58923
   198
  let val keywords = Keyword.get_keywords () in
wenzelm@58923
   199
    is_some (Keyword.command_keyword keywords name) andalso
wenzelm@58923
   200
      let
wenzelm@58923
   201
        val markup =
wenzelm@58923
   202
          Outer_Syntax.scan keywords Position.none name
wenzelm@58923
   203
          |> maps (Outer_Syntax.command_reports (#2 (Outer_Syntax.get_syntax ())))
wenzelm@58923
   204
          |> map (snd o fst);
wenzelm@58923
   205
        val _ = Context_Position.reports ctxt (map (pair pos) markup);
wenzelm@58923
   206
      in true end
wenzelm@58923
   207
  end;
wenzelm@53061
   208
wenzelm@56467
   209
fun check_system_option ctxt (name, pos) =
wenzelm@56467
   210
  (Context_Position.report ctxt pos (Options.default_markup (name, pos)); true)
wenzelm@56467
   211
    handle ERROR _ => false;
wenzelm@56467
   212
wenzelm@56294
   213
fun check_tool ctxt (name, pos) =
wenzelm@53044
   214
  let
wenzelm@53044
   215
    fun tool dir =
wenzelm@53044
   216
      let val path = Path.append dir (Path.basic name)
wenzelm@53044
   217
      in if File.exists path then SOME path else NONE end;
wenzelm@53044
   218
  in
wenzelm@53045
   219
    (case get_first tool (Path.split (getenv "ISABELLE_TOOLS")) of
wenzelm@53044
   220
      NONE => false
wenzelm@56294
   221
    | SOME path => (Context_Position.report ctxt pos (Markup.path (Path.implode path)); true))
wenzelm@53044
   222
  end;
wenzelm@28237
   223
wenzelm@26751
   224
val arg = enclose "{" "}" o clean_string;
wenzelm@26751
   225
wenzelm@56185
   226
fun entity check markup binding index =
wenzelm@37216
   227
  Thy_Output.antiquotation
wenzelm@56185
   228
    (binding |> Binding.map_name (fn name => name ^
wenzelm@43564
   229
      (case index of NONE => "" | SOME true => "_def" | SOME false => "_ref")))
wenzelm@53044
   230
    (Scan.lift (Scan.optional (Args.parens Args.name) "" -- Parse.position Args.name))
wenzelm@53044
   231
    (fn {context = ctxt, ...} => fn (logic, (name, pos)) =>
wenzelm@30394
   232
      let
wenzelm@56185
   233
        val kind = translate (fn "_" => " " | c => c) (Binding.name_of binding);
wenzelm@30394
   234
        val hyper_name =
wenzelm@30394
   235
          "{" ^ Long_Name.append kind (Long_Name.append logic (clean_name name)) ^ "}";
wenzelm@30394
   236
        val hyper =
wenzelm@30394
   237
          enclose ("\\hyperlink" ^ hyper_name ^ "{") "}" #>
wenzelm@30394
   238
          index = SOME true ? enclose ("\\hypertarget" ^ hyper_name ^ "{") "}";
wenzelm@30394
   239
        val idx =
wenzelm@30394
   240
          (case index of
wenzelm@30394
   241
            NONE => ""
wenzelm@30394
   242
          | SOME is_def =>
wenzelm@30394
   243
              "\\index" ^ (if is_def then "def" else "ref") ^ arg logic ^ arg kind ^ arg name);
wenzelm@30394
   244
      in
wenzelm@53044
   245
        if check ctxt (name, pos) then
wenzelm@30394
   246
          idx ^
wenzelm@30394
   247
          (Output.output name
wenzelm@30394
   248
            |> (if markup = "" then I else enclose ("\\" ^ markup ^ "{") "}")
wenzelm@38767
   249
            |> (if Config.get ctxt Thy_Output.quotes then quote else I)
wenzelm@38767
   250
            |> (if Config.get ctxt Thy_Output.display
wenzelm@38767
   251
                then enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
wenzelm@30394
   252
                else hyper o enclose "\\mbox{\\isa{" "}}"))
wenzelm@53044
   253
        else error ("Bad " ^ kind ^ " " ^ quote name ^ Position.here pos)
wenzelm@30394
   254
      end);
wenzelm@26897
   255
wenzelm@26894
   256
fun entity_antiqs check markup kind =
wenzelm@43564
   257
  entity check markup kind NONE #>
wenzelm@43564
   258
  entity check markup kind (SOME true) #>
wenzelm@43564
   259
  entity check markup kind (SOME false);
wenzelm@26751
   260
wenzelm@26751
   261
in
wenzelm@26751
   262
wenzelm@56059
   263
val _ =
wenzelm@56059
   264
  Theory.setup
wenzelm@56185
   265
   (entity_antiqs no_check "" @{binding syntax} #>
wenzelm@56294
   266
    entity_antiqs check_command "isacommand" @{binding command} #>
wenzelm@58919
   267
    entity_antiqs is_keyword "isakeyword" @{binding keyword} #>
wenzelm@58919
   268
    entity_antiqs is_keyword "isakeyword" @{binding element} #>
wenzelm@56185
   269
    entity_antiqs (can o Method.check_name) "" @{binding method} #>
wenzelm@56185
   270
    entity_antiqs (can o Attrib.check_name) "" @{binding attribute} #>
wenzelm@56185
   271
    entity_antiqs no_check "" @{binding fact} #>
wenzelm@56185
   272
    entity_antiqs no_check "" @{binding variable} #>
wenzelm@56185
   273
    entity_antiqs no_check "" @{binding case} #>
wenzelm@56185
   274
    entity_antiqs (can o Thy_Output.check_command) "" @{binding antiquotation} #>
wenzelm@56185
   275
    entity_antiqs (can o Thy_Output.check_option) "" @{binding antiquotation_option} #>
wenzelm@58716
   276
    entity_antiqs no_check "isasystem" @{binding setting} #>
wenzelm@58716
   277
    entity_antiqs check_system_option "isasystem" @{binding system_option} #>
wenzelm@56185
   278
    entity_antiqs no_check "" @{binding inference} #>
wenzelm@58716
   279
    entity_antiqs no_check "isasystem" @{binding executable} #>
wenzelm@56294
   280
    entity_antiqs check_tool "isatool" @{binding tool} #>
wenzelm@56185
   281
    entity_antiqs (can o ML_Context.check_antiquotation) "" @{binding ML_antiquotation} #>
wenzelm@58716
   282
    entity_antiqs (K (is_action o #1)) "isasystem" @{binding action});
wenzelm@26751
   283
wenzelm@26742
   284
end;
wenzelm@26751
   285
wenzelm@26751
   286
end;