doc-src/antiquote_setup.ML
author wenzelm
Tue Oct 19 21:13:10 2010 +0100 (2010-10-19)
changeset 39873 b70cd46e8e44
parent 39869 c269f6bd0a1f
child 40801 6cfacec435e6
permissions -rw-r--r--
more robust treatment of "op IDENT";
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@26742
     7
structure AntiquoteSetup: 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@31546
    22
    | "\<dash>" => "-"
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@31546
    31
  | clean_name s = s |> translate (fn "_" => "-" | "\<dash>" => "-" | 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@39873
    74
        else if Syntax.is_identifier (Long_Name.base_name (ml_name txt1)) then txt1 ^ ": " ^ txt2
wenzelm@39858
    75
        else txt1 ^ " : " ^ txt2;
wenzelm@30394
    76
      val txt' = if kind = "" then txt else kind ^ " " ^ txt;
wenzelm@37198
    77
      val _ = ML_Context.eval_text_in (SOME ctxt) false Position.none (ml (txt1, txt2));  (* ML_Lex.read (!?) *)
wenzelm@30394
    78
      val kind' = if kind = "" then "ML" else "ML " ^ kind;
wenzelm@30394
    79
    in
wenzelm@39869
    80
      "\\indexdef{}{" ^ kind' ^ "}{" ^ clean_string (ml_name txt1) ^ "}" ^
wenzelm@30394
    81
      (txt'
wenzelm@38767
    82
      |> (if Config.get ctxt Thy_Output.quotes then quote else I)
wenzelm@38767
    83
      |> (if Config.get ctxt Thy_Output.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
wenzelm@30394
    84
          else split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))
wenzelm@30394
    85
    end);
wenzelm@26742
    86
wenzelm@26742
    87
in
wenzelm@21375
    88
wenzelm@30394
    89
val _ = index_ml "index_ML" "" ml_val;
wenzelm@30394
    90
val _ = index_ml "index_ML_type" "type" ml_type;
wenzelm@30394
    91
val _ = index_ml "index_ML_exn" "exception" ml_exn;
wenzelm@30394
    92
val _ = index_ml "index_ML_structure" "structure" ml_structure;
wenzelm@30394
    93
val _ = index_ml "index_ML_functor" "functor" ml_functor;
wenzelm@21375
    94
wenzelm@26742
    95
end;
wenzelm@21375
    96
berghofe@23846
    97
wenzelm@30394
    98
(* named theorems *)
berghofe@23846
    99
wenzelm@37216
   100
val _ = Thy_Output.antiquotation "named_thms"
wenzelm@30394
   101
  (Scan.repeat (Attrib.thm -- Scan.lift (Args.parens Args.name)))
wenzelm@30394
   102
  (fn {context = ctxt, ...} =>
wenzelm@37216
   103
    map (apfst (Thy_Output.pretty_thm ctxt))
wenzelm@38767
   104
    #> (if Config.get ctxt Thy_Output.quotes then map (apfst Pretty.quote) else I)
wenzelm@38767
   105
    #> (if Config.get ctxt Thy_Output.display
wenzelm@30394
   106
        then
wenzelm@30394
   107
          map (fn (p, name) =>
wenzelm@38767
   108
            Output.output (Pretty.string_of (Pretty.indent (Config.get ctxt Thy_Output.indent) p)) ^
wenzelm@38767
   109
            "\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}")
wenzelm@30394
   110
          #> space_implode "\\par\\smallskip%\n"
wenzelm@30394
   111
          #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
wenzelm@30394
   112
        else
wenzelm@30394
   113
          map (fn (p, name) =>
wenzelm@30394
   114
            Output.output (Pretty.str_of p) ^
wenzelm@38767
   115
            "\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}")
wenzelm@30394
   116
          #> space_implode "\\par\\smallskip%\n"
wenzelm@30394
   117
          #> enclose "\\isa{" "}"));
wenzelm@26742
   118
wenzelm@26742
   119
wenzelm@30394
   120
(* theory file *)
wenzelm@26742
   121
wenzelm@37216
   122
val _ = Thy_Output.antiquotation "thy_file" (Scan.lift Args.name)
wenzelm@38767
   123
  (fn {context = ctxt, ...} =>
wenzelm@38767
   124
    fn name => (Thy_Load.check_thy Path.current name; Thy_Output.output ctxt [Pretty.str name]));
wenzelm@26742
   125
wenzelm@26751
   126
wenzelm@30394
   127
(* Isabelle/Isar entities (with index) *)
wenzelm@26751
   128
wenzelm@26751
   129
local
wenzelm@26751
   130
wenzelm@26894
   131
fun no_check _ _ = true;
wenzelm@26894
   132
wenzelm@26894
   133
fun thy_check intern defined ctxt =
wenzelm@26894
   134
  let val thy = ProofContext.theory_of ctxt
wenzelm@26894
   135
  in defined thy o intern thy end;
wenzelm@26894
   136
wenzelm@28237
   137
fun check_tool name =
wenzelm@28237
   138
  File.exists (Path.append (Path.explode "~~/lib/Tools") (Path.basic name));
wenzelm@28237
   139
wenzelm@26751
   140
val arg = enclose "{" "}" o clean_string;
wenzelm@26751
   141
wenzelm@30394
   142
fun entity check markup kind index =
wenzelm@37216
   143
  Thy_Output.antiquotation
wenzelm@30396
   144
    (translate (fn " " => "_" | c => c) kind ^
wenzelm@30396
   145
      (case index of NONE => "" | SOME true => "_def" | SOME false => "_ref"))
wenzelm@30394
   146
    (Scan.lift (Scan.optional (Args.parens Args.name) "" -- Args.name))
wenzelm@30394
   147
    (fn {context = ctxt, ...} => fn (logic, name) =>
wenzelm@30394
   148
      let
wenzelm@30394
   149
        val hyper_name =
wenzelm@30394
   150
          "{" ^ Long_Name.append kind (Long_Name.append logic (clean_name name)) ^ "}";
wenzelm@30394
   151
        val hyper =
wenzelm@30394
   152
          enclose ("\\hyperlink" ^ hyper_name ^ "{") "}" #>
wenzelm@30394
   153
          index = SOME true ? enclose ("\\hypertarget" ^ hyper_name ^ "{") "}";
wenzelm@30394
   154
        val idx =
wenzelm@30394
   155
          (case index of
wenzelm@30394
   156
            NONE => ""
wenzelm@30394
   157
          | SOME is_def =>
wenzelm@30394
   158
              "\\index" ^ (if is_def then "def" else "ref") ^ arg logic ^ arg kind ^ arg name);
wenzelm@30394
   159
      in
wenzelm@30394
   160
        if check ctxt name then
wenzelm@30394
   161
          idx ^
wenzelm@30394
   162
          (Output.output name
wenzelm@30394
   163
            |> (if markup = "" then I else enclose ("\\" ^ markup ^ "{") "}")
wenzelm@38767
   164
            |> (if Config.get ctxt Thy_Output.quotes then quote else I)
wenzelm@38767
   165
            |> (if Config.get ctxt Thy_Output.display
wenzelm@38767
   166
                then enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
wenzelm@30394
   167
                else hyper o enclose "\\mbox{\\isa{" "}}"))
wenzelm@30394
   168
        else error ("Bad " ^ kind ^ " " ^ quote name)
wenzelm@30394
   169
      end);
wenzelm@26897
   170
wenzelm@26894
   171
fun entity_antiqs check markup kind =
wenzelm@31297
   172
 ((entity check markup kind NONE);
wenzelm@31297
   173
  (entity check markup kind (SOME true));
wenzelm@31297
   174
  (entity check markup kind (SOME false)));
wenzelm@26751
   175
wenzelm@26751
   176
in
wenzelm@26751
   177
wenzelm@30394
   178
val _ = entity_antiqs no_check "" "syntax";
wenzelm@36973
   179
val _ = entity_antiqs (K (is_some o Keyword.command_keyword)) "isacommand" "command";
wenzelm@36973
   180
val _ = entity_antiqs (K Keyword.is_keyword) "isakeyword" "keyword";
wenzelm@36973
   181
val _ = entity_antiqs (K Keyword.is_keyword) "isakeyword" "element";
wenzelm@30394
   182
val _ = entity_antiqs (thy_check Method.intern Method.defined) "" "method";
wenzelm@30394
   183
val _ = entity_antiqs (thy_check Attrib.intern Attrib.defined) "" "attribute";
wenzelm@30394
   184
val _ = entity_antiqs no_check "" "fact";
wenzelm@30394
   185
val _ = entity_antiqs no_check "" "variable";
wenzelm@30394
   186
val _ = entity_antiqs no_check "" "case";
wenzelm@37216
   187
val _ = entity_antiqs (K Thy_Output.defined_command) "" "antiquotation";
wenzelm@37216
   188
val _ = entity_antiqs (K Thy_Output.defined_option) "" "antiquotation option";
wenzelm@30394
   189
val _ = entity_antiqs (fn _ => fn name => is_some (OS.Process.getEnv name)) "isatt" "setting";
wenzelm@30394
   190
val _ = entity_antiqs no_check "" "inference";
wenzelm@30394
   191
val _ = entity_antiqs no_check "isatt" "executable";
wenzelm@30394
   192
val _ = entity_antiqs (K check_tool) "isatt" "tool";
wenzelm@30394
   193
val _ = entity_antiqs (K (File.exists o Path.explode)) "isatt" "file";
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;