src/Doc/antiquote_setup.ML
author wenzelm
Mon Mar 25 17:21:26 2019 +0100 (4 months ago)
changeset 69981 3dced198b9ec
parent 69962 82e945d472d5
child 70121 61e26527480e
permissions -rw-r--r--
more strict AFP properties;
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@69349
    74
  (#1 (Input.source_content source), ML_Lex.read_source source);
wenzelm@55831
    75
wenzelm@67463
    76
fun index_ml name kind ml = Thy_Output.antiquotation_raw name
wenzelm@59809
    77
  (Scan.lift (Args.text_input -- Scan.option (Args.colon |-- Args.text_input)))
wenzelm@67463
    78
  (fn 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@67463
   101
      Latex.block
wenzelm@67463
   102
       [Latex.string ("\\indexdef{}{" ^ kind' ^ "}{" ^ clean_string (ml_name txt1) ^ "}"),
wenzelm@67463
   103
        Thy_Output.verbatim ctxt txt']
wenzelm@30394
   104
    end);
wenzelm@26742
   105
wenzelm@26742
   106
in
wenzelm@21375
   107
wenzelm@56059
   108
val _ =
wenzelm@56059
   109
  Theory.setup
wenzelm@69593
   110
   (index_ml \<^binding>\<open>index_ML\<close> "" ml_val #>
wenzelm@69593
   111
    index_ml \<^binding>\<open>index_ML_op\<close> "infix" ml_op #>
wenzelm@69593
   112
    index_ml \<^binding>\<open>index_ML_type\<close> "type" ml_type #>
wenzelm@69593
   113
    index_ml \<^binding>\<open>index_ML_exception\<close> "exception" ml_exception #>
wenzelm@69593
   114
    index_ml \<^binding>\<open>index_ML_structure\<close> "structure" ml_structure #>
wenzelm@69593
   115
    index_ml \<^binding>\<open>index_ML_functor\<close> "functor" ml_functor);
wenzelm@21375
   116
wenzelm@26742
   117
end;
wenzelm@21375
   118
berghofe@23846
   119
wenzelm@30394
   120
(* named theorems *)
berghofe@23846
   121
wenzelm@56059
   122
val _ =
wenzelm@69593
   123
  Theory.setup (Thy_Output.antiquotation_raw \<^binding>\<open>named_thms\<close>
wenzelm@43564
   124
    (Scan.repeat (Attrib.thm -- Scan.lift (Args.parens Args.name)))
wenzelm@67463
   125
    (fn ctxt =>
wenzelm@67463
   126
      map (fn (thm, name) =>
wenzelm@67463
   127
        Output.output
wenzelm@67463
   128
          (Document_Antiquotation.format ctxt
wenzelm@67463
   129
            (Document_Antiquotation.quote ctxt (Thy_Output.pretty_thm ctxt thm))) ^
wenzelm@67468
   130
        enclose "\\rulename{" "}" (Output.output name))
wenzelm@67463
   131
      #> space_implode "\\par\\smallskip%\n"
wenzelm@67463
   132
      #> Latex.string #> single
wenzelm@67463
   133
      #> Thy_Output.isabelle ctxt));
wenzelm@26742
   134
wenzelm@26742
   135
wenzelm@30394
   136
(* Isabelle/Isar entities (with index) *)
wenzelm@26751
   137
wenzelm@26751
   138
local
wenzelm@26751
   139
wenzelm@61620
   140
fun no_check (_: Proof.context) (name, _: Position.T) = name;
wenzelm@26894
   141
wenzelm@61620
   142
fun check_keyword ctxt (name, pos) =
wenzelm@61620
   143
  if Keyword.is_keyword (Thy_Header.get_keywords' ctxt) name then name
wenzelm@61620
   144
  else error ("Bad outer syntax keyword " ^ quote name ^ Position.here pos);
wenzelm@53061
   145
wenzelm@56467
   146
fun check_system_option ctxt (name, pos) =
wenzelm@56467
   147
  (Context_Position.report ctxt pos (Options.default_markup (name, pos)); true)
wenzelm@56467
   148
    handle ERROR _ => false;
wenzelm@56467
   149
wenzelm@26751
   150
val arg = enclose "{" "}" o clean_string;
wenzelm@26751
   151
wenzelm@56185
   152
fun entity check markup binding index =
wenzelm@67463
   153
  Thy_Output.antiquotation_raw
wenzelm@56185
   154
    (binding |> Binding.map_name (fn name => name ^
wenzelm@43564
   155
      (case index of NONE => "" | SOME true => "_def" | SOME false => "_ref")))
wenzelm@69349
   156
    (Scan.lift (Scan.optional (Args.parens Args.name) "" -- Args.name_position))
wenzelm@67463
   157
    (fn ctxt => fn (logic, (name, pos)) =>
wenzelm@30394
   158
      let
wenzelm@56185
   159
        val kind = translate (fn "_" => " " | c => c) (Binding.name_of binding);
wenzelm@30394
   160
        val hyper_name =
wenzelm@30394
   161
          "{" ^ Long_Name.append kind (Long_Name.append logic (clean_name name)) ^ "}";
wenzelm@30394
   162
        val hyper =
wenzelm@30394
   163
          enclose ("\\hyperlink" ^ hyper_name ^ "{") "}" #>
wenzelm@30394
   164
          index = SOME true ? enclose ("\\hypertarget" ^ hyper_name ^ "{") "}";
wenzelm@30394
   165
        val idx =
wenzelm@30394
   166
          (case index of
wenzelm@30394
   167
            NONE => ""
wenzelm@30394
   168
          | SOME is_def =>
wenzelm@30394
   169
              "\\index" ^ (if is_def then "def" else "ref") ^ arg logic ^ arg kind ^ arg name);
wenzelm@66682
   170
        val _ =
wenzelm@66682
   171
          if Context_Position.is_reported ctxt pos then ignore (check ctxt (name, pos)) else ();
wenzelm@67463
   172
        val latex =
wenzelm@67463
   173
          idx ^
wenzelm@67463
   174
          (Output.output name
wenzelm@67463
   175
            |> (if markup = "" then I else enclose ("\\" ^ markup ^ "{") "}")
wenzelm@67463
   176
            |> hyper o enclose "\\mbox{\\isa{" "}}");
wenzelm@67463
   177
      in Latex.string latex end);
wenzelm@26897
   178
wenzelm@26894
   179
fun entity_antiqs check markup kind =
wenzelm@43564
   180
  entity check markup kind NONE #>
wenzelm@43564
   181
  entity check markup kind (SOME true) #>
wenzelm@43564
   182
  entity check markup kind (SOME false);
wenzelm@26751
   183
wenzelm@26751
   184
in
wenzelm@26751
   185
wenzelm@56059
   186
val _ =
wenzelm@56059
   187
  Theory.setup
wenzelm@69593
   188
   (entity_antiqs no_check "" \<^binding>\<open>syntax\<close> #>
wenzelm@69593
   189
    entity_antiqs Outer_Syntax.check_command "isacommand" \<^binding>\<open>command\<close> #>
wenzelm@69593
   190
    entity_antiqs check_keyword "isakeyword" \<^binding>\<open>keyword\<close> #>
wenzelm@69593
   191
    entity_antiqs check_keyword "isakeyword" \<^binding>\<open>element\<close> #>
wenzelm@69593
   192
    entity_antiqs Method.check_name "" \<^binding>\<open>method\<close> #>
wenzelm@69593
   193
    entity_antiqs Attrib.check_name "" \<^binding>\<open>attribute\<close> #>
wenzelm@69593
   194
    entity_antiqs no_check "" \<^binding>\<open>fact\<close> #>
wenzelm@69593
   195
    entity_antiqs no_check "" \<^binding>\<open>variable\<close> #>
wenzelm@69593
   196
    entity_antiqs no_check "" \<^binding>\<open>case\<close> #>
wenzelm@69593
   197
    entity_antiqs Document_Antiquotation.check "" \<^binding>\<open>antiquotation\<close> #>
wenzelm@69593
   198
    entity_antiqs Document_Antiquotation.check_option "" \<^binding>\<open>antiquotation_option\<close> #>
wenzelm@69962
   199
    entity_antiqs Document_Marker.check "" \<^binding>\<open>document_marker\<close> #>
wenzelm@69593
   200
    entity_antiqs no_check "isasystem" \<^binding>\<open>setting\<close> #>
wenzelm@69593
   201
    entity_antiqs check_system_option "isasystem" \<^binding>\<open>system_option\<close> #>
wenzelm@69593
   202
    entity_antiqs no_check "" \<^binding>\<open>inference\<close> #>
wenzelm@69593
   203
    entity_antiqs no_check "isasystem" \<^binding>\<open>executable\<close> #>
wenzelm@69593
   204
    entity_antiqs no_check "isatool" \<^binding>\<open>tool\<close> #>
wenzelm@69593
   205
    entity_antiqs ML_Context.check_antiquotation "" \<^binding>\<open>ML_antiquotation\<close> #>
wenzelm@69593
   206
    entity_antiqs (K JEdit.check_action) "isasystem" \<^binding>\<open>action\<close>);
wenzelm@26751
   207
wenzelm@26742
   208
end;
wenzelm@26751
   209
wenzelm@26751
   210
end;