doc-src/antiquote_setup.ML
author haftmann
Sat, 18 Feb 2012 20:50:11 +0100
changeset 47395 8d51b375e926
parent 47389 11b6471c2f50
child 48696 4f25960417ae
permissions -rw-r--r--
avoid redefinition of @{theory} antiquotation
     1 (*  Title:      doc-src/antiquote_setup.ML
     2     Author:     Makarius
     3 
     4 Auxiliary antiquotations for the Isabelle manuals.
     5 *)
     6 
     7 signature ANTIQUOTE_SETUP =
     8 sig
     9   val setup: theory -> theory
    10 end;
    11 
    12 structure Antiquote_Setup: ANTIQUOTE_SETUP =
    13 struct
    14 
    15 (* misc utils *)
    16 
    17 fun translate f = Symbol.explode #> map f #> implode;
    18 
    19 val clean_string = translate
    20   (fn "_" => "\\_"
    21     | "#" => "\\#"
    22     | "<" => "$<$"
    23     | ">" => "$>$"
    24     | "{" => "\\{"
    25     | "|" => "$\\mid$"
    26     | "}" => "\\}"
    27     | "\<hyphen>" => "-"
    28     | c => c);
    29 
    30 fun clean_name "\<dots>" = "dots"
    31   | clean_name ".." = "ddot"
    32   | clean_name "." = "dot"
    33   | clean_name "_" = "underscore"
    34   | clean_name "{" = "braceleft"
    35   | clean_name "}" = "braceright"
    36   | clean_name s = s |> translate (fn "_" => "-" | "\<hyphen>" => "-" | c => c);
    37 
    38 
    39 (* verbatim text *)
    40 
    41 val verbatim = space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|";
    42 
    43 val verbatim_setup =
    44   Thy_Output.antiquotation @{binding verbatim} (Scan.lift Args.name)
    45     (K (split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"));
    46 
    47 
    48 (* ML text *)
    49 
    50 local
    51 
    52 fun ml_val (txt1, "") = "fn _ => (" ^ txt1 ^ ");"
    53   | ml_val (txt1, txt2) = "fn _ => (" ^ txt1 ^ " : " ^ txt2 ^ ");";
    54 
    55 fun ml_op (txt1, "") = "fn _ => (op " ^ txt1 ^ ");"
    56   | ml_op (txt1, txt2) = "fn _ => (op " ^ txt1 ^ " : " ^ txt2 ^ ");";
    57 
    58 fun ml_type (txt1, "") = "val _ = NONE : (" ^ txt1 ^ ") option;"
    59   | ml_type (txt1, txt2) = "val _ = [NONE : (" ^ txt1 ^ ") option, NONE : (" ^ txt2 ^ ") option];";
    60 
    61 fun ml_exn (txt1, "") = "fn _ => (" ^ txt1 ^ " : exn);"
    62   | ml_exn (txt1, txt2) = "fn _ => (" ^ txt1 ^ " : " ^ txt2 ^ " -> exn);";
    63 
    64 fun ml_structure (txt, _) = "functor XXX() = struct structure XX = " ^ txt ^ " end;";
    65 
    66 fun ml_functor (txt, _) = "ML_Env.check_functor " ^ ML_Syntax.print_string txt;
    67 
    68 val is_name = ML_Lex.kind_of #> (fn kind => kind = ML_Lex.Ident orelse kind = ML_Lex.LongIdent);
    69 
    70 fun ml_name txt =
    71   (case filter is_name (ML_Lex.tokenize txt) of
    72     toks as [_] => ML_Lex.flatten toks
    73   | _ => error ("Single ML name expected in input: " ^ quote txt));
    74 
    75 fun index_ml name kind ml = Thy_Output.antiquotation name
    76   (Scan.lift (Args.name -- Scan.optional (Args.colon |-- Args.name) ""))
    77   (fn {context = ctxt, ...} => fn (txt1, txt2) =>
    78     let
    79       val txt =
    80         if txt2 = "" then txt1
    81         else if kind = "type" then txt1 ^ " = " ^ txt2
    82         else if kind = "exception" then txt1 ^ " of " ^ txt2
    83         else if Lexicon.is_identifier (Long_Name.base_name (ml_name txt1))
    84         then txt1 ^ ": " ^ txt2
    85         else txt1 ^ " : " ^ txt2;
    86       val txt' = if kind = "" then txt else kind ^ " " ^ txt;
    87       val _ = ML_Context.eval_text_in (SOME ctxt) false Position.none (ml (txt1, txt2));  (* ML_Lex.read (!?) *)
    88       val kind' = if kind = "" then "ML" else "ML " ^ kind;
    89     in
    90       "\\indexdef{}{" ^ kind' ^ "}{" ^ clean_string (ml_name txt1) ^ "}" ^
    91       (txt'
    92       |> (if Config.get ctxt Thy_Output.quotes then quote else I)
    93       |> (if Config.get ctxt Thy_Output.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
    94           else split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))
    95     end);
    96 
    97 in
    98 
    99 val index_ml_setup =
   100   index_ml @{binding index_ML} "" ml_val #>
   101   index_ml @{binding index_ML_op} "infix" ml_op #>
   102   index_ml @{binding index_ML_type} "type" ml_type #>
   103   index_ml @{binding index_ML_exn} "exception" ml_exn #>
   104   index_ml @{binding index_ML_structure} "structure" ml_structure #>
   105   index_ml @{binding index_ML_functor} "functor" ml_functor;
   106 
   107 end;
   108 
   109 
   110 (* named theorems *)
   111 
   112 val named_thms_setup =
   113   Thy_Output.antiquotation @{binding named_thms}
   114     (Scan.repeat (Attrib.thm -- Scan.lift (Args.parens Args.name)))
   115     (fn {context = ctxt, ...} =>
   116       map (apfst (Thy_Output.pretty_thm ctxt))
   117       #> (if Config.get ctxt Thy_Output.quotes then map (apfst Pretty.quote) else I)
   118       #> (if Config.get ctxt Thy_Output.display
   119           then
   120             map (fn (p, name) =>
   121               Output.output (Pretty.string_of (Pretty.indent (Config.get ctxt Thy_Output.indent) p)) ^
   122               "\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}")
   123             #> space_implode "\\par\\smallskip%\n"
   124             #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
   125           else
   126             map (fn (p, name) =>
   127               Output.output (Pretty.str_of p) ^
   128               "\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}")
   129             #> space_implode "\\par\\smallskip%\n"
   130             #> enclose "\\isa{" "}"));
   131 
   132 
   133 (* theory file *)
   134 
   135 val thy_file_setup =
   136   Thy_Output.antiquotation @{binding thy_file} (Scan.lift Args.name)
   137     (fn {context = ctxt, ...} =>
   138       fn name => (Thy_Load.check_thy Path.current name; Thy_Output.output ctxt [Pretty.str name]));
   139 
   140 
   141 (* Isabelle/Isar entities (with index) *)
   142 
   143 local
   144 
   145 fun no_check _ _ = true;
   146 
   147 fun thy_check intern defined ctxt =
   148   let val thy = Proof_Context.theory_of ctxt
   149   in defined thy o intern thy end;
   150 
   151 fun check_tool name =
   152   File.exists (Path.append (Path.explode "~~/lib/Tools") (Path.basic name));
   153 
   154 val arg = enclose "{" "}" o clean_string;
   155 
   156 fun entity check markup kind index =
   157   Thy_Output.antiquotation
   158     (Binding.name (translate (fn " " => "_" | c => c) kind ^
   159       (case index of NONE => "" | SOME true => "_def" | SOME false => "_ref")))
   160     (Scan.lift (Scan.optional (Args.parens Args.name) "" -- Args.name))
   161     (fn {context = ctxt, ...} => fn (logic, name) =>
   162       let
   163         val hyper_name =
   164           "{" ^ Long_Name.append kind (Long_Name.append logic (clean_name name)) ^ "}";
   165         val hyper =
   166           enclose ("\\hyperlink" ^ hyper_name ^ "{") "}" #>
   167           index = SOME true ? enclose ("\\hypertarget" ^ hyper_name ^ "{") "}";
   168         val idx =
   169           (case index of
   170             NONE => ""
   171           | SOME is_def =>
   172               "\\index" ^ (if is_def then "def" else "ref") ^ arg logic ^ arg kind ^ arg name);
   173       in
   174         if check ctxt name then
   175           idx ^
   176           (Output.output name
   177             |> (if markup = "" then I else enclose ("\\" ^ markup ^ "{") "}")
   178             |> (if Config.get ctxt Thy_Output.quotes then quote else I)
   179             |> (if Config.get ctxt Thy_Output.display
   180                 then enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
   181                 else hyper o enclose "\\mbox{\\isa{" "}}"))
   182         else error ("Bad " ^ kind ^ " " ^ quote name)
   183       end);
   184 
   185 fun entity_antiqs check markup kind =
   186   entity check markup kind NONE #>
   187   entity check markup kind (SOME true) #>
   188   entity check markup kind (SOME false);
   189 
   190 in
   191 
   192 val entity_setup =
   193   entity_antiqs no_check "" "syntax" #>
   194   entity_antiqs (K (is_some o Keyword.command_keyword)) "isacommand" "command" #>
   195   entity_antiqs (K Keyword.is_keyword) "isakeyword" "keyword" #>
   196   entity_antiqs (K Keyword.is_keyword) "isakeyword" "element" #>
   197   entity_antiqs (thy_check Method.intern Method.defined) "" "method" #>
   198   entity_antiqs (thy_check Attrib.intern Attrib.defined) "" "attribute" #>
   199   entity_antiqs no_check "" "fact" #>
   200   entity_antiqs no_check "" "variable" #>
   201   entity_antiqs no_check "" "case" #>
   202   entity_antiqs (thy_check Thy_Output.intern_command Thy_Output.defined_command)
   203     "" "antiquotation" #>
   204   entity_antiqs (thy_check Thy_Output.intern_option Thy_Output.defined_option)
   205     "" "antiquotation option" #>
   206   entity_antiqs (fn _ => fn name => is_some (OS.Process.getEnv name)) "isatt" "setting" #>
   207   entity_antiqs no_check "" "inference" #>
   208   entity_antiqs no_check "isatt" "executable" #>
   209   entity_antiqs (K check_tool) "isatt" "tool" #>
   210   entity_antiqs (thy_check ML_Context.intern_antiq ML_Context.defined_antiq)
   211     "" Isabelle_Markup.ML_antiquotationN;
   212 
   213 end;
   214 
   215 
   216 (* theory setup *)
   217 
   218 val setup =
   219   verbatim_setup #>
   220   index_ml_setup #>
   221   named_thms_setup #>
   222   thy_file_setup #>
   223   entity_setup;
   224 
   225 end;