doc-src/antiquote_setup.ML
author wenzelm
Tue May 03 21:44:05 2011 +0200 (2011-05-03)
changeset 42666 fee67c099d03
parent 42361 23f352990944
child 43563 aeabb735883a
permissions -rw-r--r--
use existing \<hyphen>;
wenzelm@30394
     1
(*  Title:      doc-src/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@40801
     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@26897
    17
    | "<" => "$<$"
wenzelm@26768
    18
    | ">" => "$>$"
wenzelm@26768
    19
    | "{" => "\\{"
wenzelm@30120
    20
    | "|" => "$\\mid$"
wenzelm@26768
    21
    | "}" => "\\}"
wenzelm@42666
    22
    | "\<hyphen>" => "-"
wenzelm@26768
    23
    | c => c);
wenzelm@26751
    24
wenzelm@31546
    25
fun clean_name "\<dots>" = "dots"
wenzelm@26897
    26
  | clean_name ".." = "ddot"
wenzelm@26897
    27
  | clean_name "." = "dot"
wenzelm@26910
    28
  | clean_name "_" = "underscore"
wenzelm@26897
    29
  | clean_name "{" = "braceleft"
wenzelm@26897
    30
  | clean_name "}" = "braceright"
wenzelm@42666
    31
  | clean_name s = s |> translate (fn "_" => "-" | "\<hyphen>" => "-" | c => c);
wenzelm@26897
    32
wenzelm@26742
    33
wenzelm@26742
    34
(* verbatim text *)
wenzelm@26742
    35
wenzelm@26742
    36
val verbatim = space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|";
wenzelm@26742
    37
wenzelm@37216
    38
val _ = Thy_Output.antiquotation "verbatim" (Scan.lift Args.name)
wenzelm@30382
    39
  (K (split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"));
wenzelm@26742
    40
wenzelm@26742
    41
wenzelm@26742
    42
(* ML text *)
wenzelm@26742
    43
wenzelm@26742
    44
local
wenzelm@26742
    45
wenzelm@21375
    46
fun ml_val (txt1, "") = "fn _ => (" ^ txt1 ^ ");"
wenzelm@39858
    47
  | ml_val (txt1, txt2) = "fn _ => (" ^ txt1 ^ " : " ^ txt2 ^ ");";
wenzelm@21375
    48
wenzelm@21375
    49
fun ml_type (txt1, "") = "val _ = NONE : (" ^ txt1 ^ ") option;"
wenzelm@21375
    50
  | ml_type (txt1, txt2) = "val _ = [NONE : (" ^ txt1 ^ ") option, NONE : (" ^ txt2 ^ ") option];";
haftmann@22289
    51
wenzelm@39858
    52
fun ml_exn (txt1, "") = "fn _ => (" ^ txt1 ^ " : exn);"
wenzelm@39858
    53
  | ml_exn (txt1, txt2) = "fn _ => (" ^ txt1 ^ " : " ^ txt2 ^ " -> exn);";
haftmann@22289
    54
wenzelm@30394
    55
fun ml_structure (txt, _) = "functor XXX() = struct structure XX = " ^ txt ^ " end;";
wenzelm@21375
    56
wenzelm@36163
    57
fun ml_functor (txt, _) = "ML_Env.check_functor " ^ ML_Syntax.print_string txt;
wenzelm@21375
    58
wenzelm@39869
    59
val is_name = ML_Lex.kind_of #> (fn kind => kind = ML_Lex.Ident orelse kind = ML_Lex.LongIdent);
wenzelm@39869
    60
wenzelm@39869
    61
fun ml_name txt =
wenzelm@39869
    62
  (case filter is_name (ML_Lex.tokenize txt) of
wenzelm@39869
    63
    toks as [_] => ML_Lex.flatten toks
wenzelm@39869
    64
  | _ => error ("Single ML name expected in input: " ^ quote txt));
wenzelm@39869
    65
wenzelm@37216
    66
fun index_ml name kind ml = Thy_Output.antiquotation name
wenzelm@30394
    67
  (Scan.lift (Args.name -- Scan.optional (Args.colon |-- Args.name) ""))
wenzelm@30394
    68
  (fn {context = ctxt, ...} => fn (txt1, txt2) =>
wenzelm@30394
    69
    let
wenzelm@30394
    70
      val txt =
wenzelm@30394
    71
        if txt2 = "" then txt1
wenzelm@30394
    72
        else if kind = "type" then txt1 ^ " = " ^ txt2
wenzelm@30394
    73
        else if kind = "exception" then txt1 ^ " of " ^ txt2
wenzelm@42290
    74
        else if Lexicon.is_identifier (Long_Name.base_name (ml_name txt1))
wenzelm@42290
    75
        then txt1 ^ ": " ^ txt2
wenzelm@39858
    76
        else txt1 ^ " : " ^ txt2;
wenzelm@30394
    77
      val txt' = if kind = "" then txt else kind ^ " " ^ txt;
wenzelm@37198
    78
      val _ = ML_Context.eval_text_in (SOME ctxt) false Position.none (ml (txt1, txt2));  (* ML_Lex.read (!?) *)
wenzelm@30394
    79
      val kind' = if kind = "" then "ML" else "ML " ^ kind;
wenzelm@30394
    80
    in
wenzelm@39869
    81
      "\\indexdef{}{" ^ kind' ^ "}{" ^ clean_string (ml_name txt1) ^ "}" ^
wenzelm@30394
    82
      (txt'
wenzelm@38767
    83
      |> (if Config.get ctxt Thy_Output.quotes then quote else I)
wenzelm@38767
    84
      |> (if Config.get ctxt Thy_Output.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
wenzelm@30394
    85
          else split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))
wenzelm@30394
    86
    end);
wenzelm@26742
    87
wenzelm@26742
    88
in
wenzelm@21375
    89
wenzelm@30394
    90
val _ = index_ml "index_ML" "" ml_val;
wenzelm@30394
    91
val _ = index_ml "index_ML_type" "type" ml_type;
wenzelm@30394
    92
val _ = index_ml "index_ML_exn" "exception" ml_exn;
wenzelm@30394
    93
val _ = index_ml "index_ML_structure" "structure" ml_structure;
wenzelm@30394
    94
val _ = index_ml "index_ML_functor" "functor" ml_functor;
wenzelm@21375
    95
wenzelm@26742
    96
end;
wenzelm@21375
    97
berghofe@23846
    98
wenzelm@30394
    99
(* named theorems *)
berghofe@23846
   100
wenzelm@37216
   101
val _ = Thy_Output.antiquotation "named_thms"
wenzelm@30394
   102
  (Scan.repeat (Attrib.thm -- Scan.lift (Args.parens Args.name)))
wenzelm@30394
   103
  (fn {context = ctxt, ...} =>
wenzelm@37216
   104
    map (apfst (Thy_Output.pretty_thm ctxt))
wenzelm@38767
   105
    #> (if Config.get ctxt Thy_Output.quotes then map (apfst Pretty.quote) else I)
wenzelm@38767
   106
    #> (if Config.get ctxt Thy_Output.display
wenzelm@30394
   107
        then
wenzelm@30394
   108
          map (fn (p, name) =>
wenzelm@38767
   109
            Output.output (Pretty.string_of (Pretty.indent (Config.get ctxt Thy_Output.indent) p)) ^
wenzelm@38767
   110
            "\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}")
wenzelm@30394
   111
          #> space_implode "\\par\\smallskip%\n"
wenzelm@30394
   112
          #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
wenzelm@30394
   113
        else
wenzelm@30394
   114
          map (fn (p, name) =>
wenzelm@30394
   115
            Output.output (Pretty.str_of p) ^
wenzelm@38767
   116
            "\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}")
wenzelm@30394
   117
          #> space_implode "\\par\\smallskip%\n"
wenzelm@30394
   118
          #> enclose "\\isa{" "}"));
wenzelm@26742
   119
wenzelm@26742
   120
wenzelm@30394
   121
(* theory file *)
wenzelm@26742
   122
wenzelm@37216
   123
val _ = Thy_Output.antiquotation "thy_file" (Scan.lift Args.name)
wenzelm@38767
   124
  (fn {context = ctxt, ...} =>
wenzelm@38767
   125
    fn name => (Thy_Load.check_thy Path.current name; Thy_Output.output ctxt [Pretty.str name]));
wenzelm@26742
   126
wenzelm@26751
   127
wenzelm@30394
   128
(* Isabelle/Isar entities (with index) *)
wenzelm@26751
   129
wenzelm@26751
   130
local
wenzelm@26751
   131
wenzelm@26894
   132
fun no_check _ _ = true;
wenzelm@26894
   133
wenzelm@26894
   134
fun thy_check intern defined ctxt =
wenzelm@42361
   135
  let val thy = Proof_Context.theory_of ctxt
wenzelm@26894
   136
  in defined thy o intern thy end;
wenzelm@26894
   137
wenzelm@28237
   138
fun check_tool name =
wenzelm@28237
   139
  File.exists (Path.append (Path.explode "~~/lib/Tools") (Path.basic name));
wenzelm@28237
   140
wenzelm@26751
   141
val arg = enclose "{" "}" o clean_string;
wenzelm@26751
   142
wenzelm@30394
   143
fun entity check markup kind index =
wenzelm@37216
   144
  Thy_Output.antiquotation
wenzelm@30396
   145
    (translate (fn " " => "_" | c => c) kind ^
wenzelm@30396
   146
      (case index of NONE => "" | SOME true => "_def" | SOME false => "_ref"))
wenzelm@30394
   147
    (Scan.lift (Scan.optional (Args.parens Args.name) "" -- Args.name))
wenzelm@30394
   148
    (fn {context = ctxt, ...} => fn (logic, name) =>
wenzelm@30394
   149
      let
wenzelm@30394
   150
        val hyper_name =
wenzelm@30394
   151
          "{" ^ Long_Name.append kind (Long_Name.append logic (clean_name name)) ^ "}";
wenzelm@30394
   152
        val hyper =
wenzelm@30394
   153
          enclose ("\\hyperlink" ^ hyper_name ^ "{") "}" #>
wenzelm@30394
   154
          index = SOME true ? enclose ("\\hypertarget" ^ hyper_name ^ "{") "}";
wenzelm@30394
   155
        val idx =
wenzelm@30394
   156
          (case index of
wenzelm@30394
   157
            NONE => ""
wenzelm@30394
   158
          | SOME is_def =>
wenzelm@30394
   159
              "\\index" ^ (if is_def then "def" else "ref") ^ arg logic ^ arg kind ^ arg name);
wenzelm@30394
   160
      in
wenzelm@30394
   161
        if check ctxt name then
wenzelm@30394
   162
          idx ^
wenzelm@30394
   163
          (Output.output name
wenzelm@30394
   164
            |> (if markup = "" then I else enclose ("\\" ^ markup ^ "{") "}")
wenzelm@38767
   165
            |> (if Config.get ctxt Thy_Output.quotes then quote else I)
wenzelm@38767
   166
            |> (if Config.get ctxt Thy_Output.display
wenzelm@38767
   167
                then enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
wenzelm@30394
   168
                else hyper o enclose "\\mbox{\\isa{" "}}"))
wenzelm@30394
   169
        else error ("Bad " ^ kind ^ " " ^ quote name)
wenzelm@30394
   170
      end);
wenzelm@26897
   171
wenzelm@26894
   172
fun entity_antiqs check markup kind =
wenzelm@31297
   173
 ((entity check markup kind NONE);
wenzelm@31297
   174
  (entity check markup kind (SOME true));
wenzelm@31297
   175
  (entity check markup kind (SOME false)));
wenzelm@26751
   176
wenzelm@26751
   177
in
wenzelm@26751
   178
wenzelm@30394
   179
val _ = entity_antiqs no_check "" "syntax";
wenzelm@36973
   180
val _ = entity_antiqs (K (is_some o Keyword.command_keyword)) "isacommand" "command";
wenzelm@36973
   181
val _ = entity_antiqs (K Keyword.is_keyword) "isakeyword" "keyword";
wenzelm@36973
   182
val _ = entity_antiqs (K Keyword.is_keyword) "isakeyword" "element";
wenzelm@30394
   183
val _ = entity_antiqs (thy_check Method.intern Method.defined) "" "method";
wenzelm@30394
   184
val _ = entity_antiqs (thy_check Attrib.intern Attrib.defined) "" "attribute";
wenzelm@30394
   185
val _ = entity_antiqs no_check "" "fact";
wenzelm@30394
   186
val _ = entity_antiqs no_check "" "variable";
wenzelm@30394
   187
val _ = entity_antiqs no_check "" "case";
wenzelm@37216
   188
val _ = entity_antiqs (K Thy_Output.defined_command) "" "antiquotation";
wenzelm@37216
   189
val _ = entity_antiqs (K Thy_Output.defined_option) "" "antiquotation option";
wenzelm@30394
   190
val _ = entity_antiqs (fn _ => fn name => is_some (OS.Process.getEnv name)) "isatt" "setting";
wenzelm@30394
   191
val _ = entity_antiqs no_check "" "inference";
wenzelm@30394
   192
val _ = entity_antiqs no_check "isatt" "executable";
wenzelm@30394
   193
val _ = entity_antiqs (K check_tool) "isatt" "tool";
wenzelm@37982
   194
val _ = entity_antiqs (K (can Thy_Info.get_theory)) "" "theory";
wenzelm@39829
   195
val _ = entity_antiqs no_check "" "ML antiquotation";  (* FIXME proper check *)
wenzelm@26751
   196
wenzelm@26742
   197
end;
wenzelm@26751
   198
wenzelm@26751
   199
end;