src/Doc/antiquote_setup.ML
author wenzelm
Tue Nov 10 21:52:18 2015 +0100 (2015-11-10)
changeset 61620 01db1bed4487
parent 61618 27af754f50ca
child 61621 70b8085f51b4
permissions -rw-r--r--
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
(* ML text *)
wenzelm@26742
    37
wenzelm@26742
    38
local
wenzelm@26742
    39
wenzelm@59067
    40
fun ml_val (toks1, []) = ML_Lex.read "fn _ => (" @ toks1 @ ML_Lex.read ");"
wenzelm@59067
    41
  | ml_val (toks1, toks2) =
wenzelm@59067
    42
      ML_Lex.read "fn _ => (" @ toks1 @ ML_Lex.read " : " @ toks2 @ ML_Lex.read ");";
wenzelm@21375
    43
wenzelm@59067
    44
fun ml_op (toks1, []) = ML_Lex.read "fn _ => (op " @ toks1 @ ML_Lex.read ");"
wenzelm@59067
    45
  | ml_op (toks1, toks2) =
wenzelm@59067
    46
      ML_Lex.read "fn _ => (op " @ toks1 @ ML_Lex.read " : " @ toks2 @ ML_Lex.read ");";
wenzelm@46261
    47
wenzelm@59067
    48
fun ml_type (toks1, []) = ML_Lex.read "val _ = NONE : (" @ toks1 @ ML_Lex.read ") option;"
wenzelm@55831
    49
  | ml_type (toks1, toks2) =
wenzelm@59067
    50
      ML_Lex.read "val _ = [NONE : (" @ toks1 @ ML_Lex.read ") option, NONE : (" @
wenzelm@59067
    51
        toks2 @ ML_Lex.read ") option];";
haftmann@22289
    52
wenzelm@59067
    53
fun ml_exception (toks1, []) = ML_Lex.read "fn _ => (" @ toks1 @ ML_Lex.read " : exn);"
wenzelm@55837
    54
  | ml_exception (toks1, toks2) =
wenzelm@59067
    55
      ML_Lex.read "fn _ => (" @ toks1 @ ML_Lex.read " : " @ toks2 @ ML_Lex.read " -> exn);";
haftmann@22289
    56
wenzelm@55831
    57
fun ml_structure (toks, _) =
wenzelm@59067
    58
  ML_Lex.read "functor XXX() = struct structure XX = " @ toks @ ML_Lex.read " end;";
wenzelm@21375
    59
wenzelm@55831
    60
fun ml_functor (Antiquote.Text tok :: _, _) =
wenzelm@59067
    61
      ML_Lex.read "ML_Env.check_functor " @
wenzelm@59067
    62
      ML_Lex.read (ML_Syntax.print_string (ML_Lex.content_of tok))
wenzelm@55831
    63
  | ml_functor _ = raise Fail "Bad ML functor specification";
wenzelm@21375
    64
wenzelm@59082
    65
val is_name =
wenzelm@59082
    66
  ML_Lex.kind_of #> (fn kind => kind = ML_Lex.Ident orelse kind = ML_Lex.Long_Ident);
wenzelm@39869
    67
wenzelm@39869
    68
fun ml_name txt =
wenzelm@39869
    69
  (case filter is_name (ML_Lex.tokenize txt) of
wenzelm@39869
    70
    toks as [_] => ML_Lex.flatten toks
wenzelm@39869
    71
  | _ => error ("Single ML name expected in input: " ^ quote txt));
wenzelm@39869
    72
wenzelm@55831
    73
fun prep_ml source =
wenzelm@59066
    74
  (Input.source_content source, ML_Lex.read_source false source);
wenzelm@55831
    75
wenzelm@37216
    76
fun index_ml name kind ml = Thy_Output.antiquotation name
wenzelm@59809
    77
  (Scan.lift (Args.text_input -- Scan.option (Args.colon |-- Args.text_input)))
wenzelm@55831
    78
  (fn {context = ctxt, ...} => fn (source1, opt_source2) =>
wenzelm@30394
    79
    let
wenzelm@55831
    80
      val (txt1, toks1) = prep_ml source1;
wenzelm@55831
    81
      val (txt2, toks2) =
wenzelm@55831
    82
        (case opt_source2 of
wenzelm@55831
    83
          SOME source => prep_ml source
wenzelm@55831
    84
        | NONE => ("", []));
wenzelm@55831
    85
wenzelm@30394
    86
      val txt =
wenzelm@30394
    87
        if txt2 = "" then txt1
wenzelm@30394
    88
        else if kind = "type" then txt1 ^ " = " ^ txt2
wenzelm@30394
    89
        else if kind = "exception" then txt1 ^ " of " ^ txt2
wenzelm@50239
    90
        else if Symbol_Pos.is_identifier (Long_Name.base_name (ml_name txt1))
wenzelm@42290
    91
        then txt1 ^ ": " ^ txt2
wenzelm@39858
    92
        else txt1 ^ " : " ^ txt2;
wenzelm@30394
    93
      val txt' = if kind = "" then txt else kind ^ " " ^ txt;
wenzelm@55831
    94
wenzelm@59064
    95
      val pos = Input.pos_of source1;
wenzelm@56275
    96
      val _ =
wenzelm@57477
    97
        ML_Context.eval_in (SOME ctxt) ML_Compiler.flags pos (ml (toks1, toks2))
wenzelm@57477
    98
          handle ERROR msg => error (msg ^ Position.here pos);
wenzelm@30394
    99
      val kind' = if kind = "" then "ML" else "ML " ^ kind;
wenzelm@30394
   100
    in
wenzelm@39869
   101
      "\\indexdef{}{" ^ kind' ^ "}{" ^ clean_string (ml_name txt1) ^ "}" ^
wenzelm@58716
   102
      (Thy_Output.verbatim_text ctxt txt')
wenzelm@30394
   103
    end);
wenzelm@26742
   104
wenzelm@26742
   105
in
wenzelm@21375
   106
wenzelm@56059
   107
val _ =
wenzelm@56059
   108
  Theory.setup
wenzelm@56059
   109
   (index_ml @{binding index_ML} "" ml_val #>
wenzelm@56059
   110
    index_ml @{binding index_ML_op} "infix" ml_op #>
wenzelm@56059
   111
    index_ml @{binding index_ML_type} "type" ml_type #>
wenzelm@56059
   112
    index_ml @{binding index_ML_exception} "exception" ml_exception #>
wenzelm@56059
   113
    index_ml @{binding index_ML_structure} "structure" ml_structure #>
wenzelm@56059
   114
    index_ml @{binding index_ML_functor} "functor" ml_functor);
wenzelm@21375
   115
wenzelm@26742
   116
end;
wenzelm@21375
   117
berghofe@23846
   118
wenzelm@30394
   119
(* named theorems *)
berghofe@23846
   120
wenzelm@56059
   121
val _ =
wenzelm@56059
   122
  Theory.setup (Thy_Output.antiquotation @{binding named_thms}
wenzelm@43564
   123
    (Scan.repeat (Attrib.thm -- Scan.lift (Args.parens Args.name)))
wenzelm@43564
   124
    (fn {context = ctxt, ...} =>
wenzelm@43564
   125
      map (apfst (Thy_Output.pretty_thm ctxt))
wenzelm@43564
   126
      #> (if Config.get ctxt Thy_Output.quotes then map (apfst Pretty.quote) else I)
wenzelm@43564
   127
      #> (if Config.get ctxt Thy_Output.display
wenzelm@43564
   128
          then
wenzelm@43564
   129
            map (fn (p, name) =>
wenzelm@59175
   130
              Output.output
wenzelm@59175
   131
                (Thy_Output.string_of_margin ctxt
wenzelm@59175
   132
                  (Pretty.indent (Config.get ctxt Thy_Output.indent) p)) ^
wenzelm@43564
   133
              "\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}")
wenzelm@43564
   134
            #> space_implode "\\par\\smallskip%\n"
wenzelm@61462
   135
            #> Latex.environment "isabelle"
wenzelm@43564
   136
          else
wenzelm@43564
   137
            map (fn (p, name) =>
wenzelm@43564
   138
              Output.output (Pretty.str_of p) ^
wenzelm@43564
   139
              "\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}")
wenzelm@43564
   140
            #> space_implode "\\par\\smallskip%\n"
wenzelm@56059
   141
            #> enclose "\\isa{" "}")));
wenzelm@26742
   142
wenzelm@26742
   143
wenzelm@30394
   144
(* Isabelle/Isar entities (with index) *)
wenzelm@26751
   145
wenzelm@26751
   146
local
wenzelm@26751
   147
wenzelm@61620
   148
fun no_check (_: Proof.context) (name, _: Position.T) = name;
wenzelm@26894
   149
wenzelm@61620
   150
fun check_keyword ctxt (name, pos) =
wenzelm@61620
   151
  if Keyword.is_keyword (Thy_Header.get_keywords' ctxt) name then name
wenzelm@61620
   152
  else error ("Bad outer syntax keyword " ^ quote name ^ Position.here pos);
wenzelm@53061
   153
wenzelm@56467
   154
fun check_system_option ctxt (name, pos) =
wenzelm@56467
   155
  (Context_Position.report ctxt pos (Options.default_markup (name, pos)); true)
wenzelm@56467
   156
    handle ERROR _ => false;
wenzelm@56467
   157
wenzelm@56294
   158
fun check_tool ctxt (name, pos) =
wenzelm@53044
   159
  let
wenzelm@53044
   160
    fun tool dir =
wenzelm@53044
   161
      let val path = Path.append dir (Path.basic name)
wenzelm@53044
   162
      in if File.exists path then SOME path else NONE end;
wenzelm@53044
   163
  in
wenzelm@53045
   164
    (case get_first tool (Path.split (getenv "ISABELLE_TOOLS")) of
wenzelm@53044
   165
      NONE => false
wenzelm@56294
   166
    | SOME path => (Context_Position.report ctxt pos (Markup.path (Path.implode path)); true))
wenzelm@53044
   167
  end;
wenzelm@28237
   168
wenzelm@61620
   169
fun check_action _ = can JEdit.check_action;
wenzelm@61620
   170
wenzelm@26751
   171
val arg = enclose "{" "}" o clean_string;
wenzelm@26751
   172
wenzelm@56185
   173
fun entity check markup binding index =
wenzelm@37216
   174
  Thy_Output.antiquotation
wenzelm@56185
   175
    (binding |> Binding.map_name (fn name => name ^
wenzelm@43564
   176
      (case index of NONE => "" | SOME true => "_def" | SOME false => "_ref")))
wenzelm@53044
   177
    (Scan.lift (Scan.optional (Args.parens Args.name) "" -- Parse.position Args.name))
wenzelm@53044
   178
    (fn {context = ctxt, ...} => fn (logic, (name, pos)) =>
wenzelm@30394
   179
      let
wenzelm@56185
   180
        val kind = translate (fn "_" => " " | c => c) (Binding.name_of binding);
wenzelm@30394
   181
        val hyper_name =
wenzelm@30394
   182
          "{" ^ Long_Name.append kind (Long_Name.append logic (clean_name name)) ^ "}";
wenzelm@30394
   183
        val hyper =
wenzelm@30394
   184
          enclose ("\\hyperlink" ^ hyper_name ^ "{") "}" #>
wenzelm@30394
   185
          index = SOME true ? enclose ("\\hypertarget" ^ hyper_name ^ "{") "}";
wenzelm@30394
   186
        val idx =
wenzelm@30394
   187
          (case index of
wenzelm@30394
   188
            NONE => ""
wenzelm@30394
   189
          | SOME is_def =>
wenzelm@30394
   190
              "\\index" ^ (if is_def then "def" else "ref") ^ arg logic ^ arg kind ^ arg name);
wenzelm@61620
   191
        val _ = check ctxt (name, pos);
wenzelm@30394
   192
      in
wenzelm@61620
   193
        idx ^
wenzelm@61620
   194
        (Output.output name
wenzelm@61620
   195
          |> (if markup = "" then I else enclose ("\\" ^ markup ^ "{") "}")
wenzelm@61620
   196
          |> hyper o enclose "\\mbox{\\isa{" "}}")
wenzelm@30394
   197
      end);
wenzelm@26897
   198
wenzelm@26894
   199
fun entity_antiqs check markup kind =
wenzelm@43564
   200
  entity check markup kind NONE #>
wenzelm@43564
   201
  entity check markup kind (SOME true) #>
wenzelm@43564
   202
  entity check markup kind (SOME false);
wenzelm@26751
   203
wenzelm@26751
   204
in
wenzelm@26751
   205
wenzelm@56059
   206
val _ =
wenzelm@56059
   207
  Theory.setup
wenzelm@56185
   208
   (entity_antiqs no_check "" @{binding syntax} #>
wenzelm@61620
   209
    entity_antiqs Outer_Syntax.check_command "isacommand" @{binding command} #>
wenzelm@61620
   210
    entity_antiqs check_keyword "isakeyword" @{binding keyword} #>
wenzelm@61620
   211
    entity_antiqs check_keyword "isakeyword" @{binding element} #>
wenzelm@61620
   212
    entity_antiqs Method.check_name "" @{binding method} #>
wenzelm@61620
   213
    entity_antiqs Attrib.check_name "" @{binding attribute} #>
wenzelm@56185
   214
    entity_antiqs no_check "" @{binding fact} #>
wenzelm@56185
   215
    entity_antiqs no_check "" @{binding variable} #>
wenzelm@56185
   216
    entity_antiqs no_check "" @{binding case} #>
wenzelm@61620
   217
    entity_antiqs Thy_Output.check_command "" @{binding antiquotation} #>
wenzelm@61620
   218
    entity_antiqs Thy_Output.check_option "" @{binding antiquotation_option} #>
wenzelm@58716
   219
    entity_antiqs no_check "isasystem" @{binding setting} #>
wenzelm@58716
   220
    entity_antiqs check_system_option "isasystem" @{binding system_option} #>
wenzelm@56185
   221
    entity_antiqs no_check "" @{binding inference} #>
wenzelm@58716
   222
    entity_antiqs no_check "isasystem" @{binding executable} #>
wenzelm@56294
   223
    entity_antiqs check_tool "isatool" @{binding tool} #>
wenzelm@61620
   224
    entity_antiqs ML_Context.check_antiquotation "" @{binding ML_antiquotation} #>
wenzelm@61620
   225
    entity_antiqs check_action "isasystem" @{binding action});
wenzelm@26751
   226
wenzelm@26742
   227
end;
wenzelm@26751
   228
wenzelm@26751
   229
end;