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