doc-src/antiquote_setup.ML
author wenzelm
Sat, 16 Oct 2010 21:23:34 +0100
changeset 40297 5be7a57c3b4e
parent 40268 f5ea50decd9f
child 40308 c269f6bd0a1f
permissions -rw-r--r--
more robust treatment of symbolic indentifiers (which may contain colons);
     1 (*  Title:      doc-src/antiquote_setup.ML
     2     Author:     Makarius
     3 
     4 Auxiliary antiquotations for the Isabelle manuals.
     5 *)
     6 
     7 structure AntiquoteSetup: sig end =
     8 struct
     9 
    10 (* misc utils *)
    11 
    12 fun translate f = Symbol.explode #> map f #> implode;
    13 
    14 val clean_string = translate
    15   (fn "_" => "\\_"
    16     | "#" => "\\#"
    17     | "<" => "$<$"
    18     | ">" => "$>$"
    19     | "{" => "\\{"
    20     | "|" => "$\\mid$"
    21     | "}" => "\\}"
    22     | "\<dash>" => "-"
    23     | c => c);
    24 
    25 fun clean_name "\<dots>" = "dots"
    26   | clean_name ".." = "ddot"
    27   | clean_name "." = "dot"
    28   | clean_name "_" = "underscore"
    29   | clean_name "{" = "braceleft"
    30   | clean_name "}" = "braceright"
    31   | clean_name s = s |> translate (fn "_" => "-" | "\<dash>" => "-" | c => c);
    32 
    33 
    34 (* verbatim text *)
    35 
    36 val verbatim = space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|";
    37 
    38 val _ = Thy_Output.antiquotation "verbatim" (Scan.lift Args.name)
    39   (K (split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"));
    40 
    41 
    42 (* ML text *)
    43 
    44 local
    45 
    46 fun ml_val (txt1, "") = "fn _ => (" ^ txt1 ^ ");"
    47   | ml_val (txt1, txt2) = "fn _ => (" ^ txt1 ^ " : " ^ txt2 ^ ");";
    48 
    49 fun ml_type (txt1, "") = "val _ = NONE : (" ^ txt1 ^ ") option;"
    50   | ml_type (txt1, txt2) = "val _ = [NONE : (" ^ txt1 ^ ") option, NONE : (" ^ txt2 ^ ") option];";
    51 
    52 fun ml_exn (txt1, "") = "fn _ => (" ^ txt1 ^ " : exn);"
    53   | ml_exn (txt1, txt2) = "fn _ => (" ^ txt1 ^ " : " ^ txt2 ^ " -> exn);";
    54 
    55 fun ml_structure (txt, _) = "functor XXX() = struct structure XX = " ^ txt ^ " end;";
    56 
    57 fun ml_functor (txt, _) = "ML_Env.check_functor " ^ ML_Syntax.print_string txt;
    58 
    59 fun index_ml name kind ml = Thy_Output.antiquotation name
    60   (Scan.lift (Args.name -- Scan.optional (Args.colon |-- Args.name) ""))
    61   (fn {context = ctxt, ...} => fn (txt1, txt2) =>
    62     let
    63       val txt =
    64         if txt2 = "" then txt1
    65         else if kind = "type" then txt1 ^ " = " ^ txt2
    66         else if kind = "exception" then txt1 ^ " of " ^ txt2
    67         else if Syntax.is_identifier (Long_Name.base_name txt1) then txt1 ^ ": " ^ txt2
    68         else txt1 ^ " : " ^ txt2;
    69       val txt' = if kind = "" then txt else kind ^ " " ^ txt;
    70       val _ = ML_Context.eval_text_in (SOME ctxt) false Position.none (ml (txt1, txt2));  (* ML_Lex.read (!?) *)
    71       val kind' = if kind = "" then "ML" else "ML " ^ kind;
    72     in
    73       "\\indexdef{}{" ^ kind' ^ "}{" ^ clean_string txt1 ^ "}" ^
    74       (txt'
    75       |> (if Config.get ctxt Thy_Output.quotes then quote else I)
    76       |> (if Config.get ctxt Thy_Output.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
    77           else split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))
    78     end);
    79 
    80 in
    81 
    82 val _ = index_ml "index_ML" "" ml_val;
    83 val _ = index_ml "index_ML_type" "type" ml_type;
    84 val _ = index_ml "index_ML_exn" "exception" ml_exn;
    85 val _ = index_ml "index_ML_structure" "structure" ml_structure;
    86 val _ = index_ml "index_ML_functor" "functor" ml_functor;
    87 
    88 end;
    89 
    90 
    91 (* named theorems *)
    92 
    93 val _ = Thy_Output.antiquotation "named_thms"
    94   (Scan.repeat (Attrib.thm -- Scan.lift (Args.parens Args.name)))
    95   (fn {context = ctxt, ...} =>
    96     map (apfst (Thy_Output.pretty_thm ctxt))
    97     #> (if Config.get ctxt Thy_Output.quotes then map (apfst Pretty.quote) else I)
    98     #> (if Config.get ctxt Thy_Output.display
    99         then
   100           map (fn (p, name) =>
   101             Output.output (Pretty.string_of (Pretty.indent (Config.get ctxt Thy_Output.indent) p)) ^
   102             "\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}")
   103           #> space_implode "\\par\\smallskip%\n"
   104           #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
   105         else
   106           map (fn (p, name) =>
   107             Output.output (Pretty.str_of p) ^
   108             "\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}")
   109           #> space_implode "\\par\\smallskip%\n"
   110           #> enclose "\\isa{" "}"));
   111 
   112 
   113 (* theory file *)
   114 
   115 val _ = Thy_Output.antiquotation "thy_file" (Scan.lift Args.name)
   116   (fn {context = ctxt, ...} =>
   117     fn name => (Thy_Load.check_thy Path.current name; Thy_Output.output ctxt [Pretty.str name]));
   118 
   119 
   120 (* Isabelle/Isar entities (with index) *)
   121 
   122 local
   123 
   124 fun no_check _ _ = true;
   125 
   126 fun thy_check intern defined ctxt =
   127   let val thy = ProofContext.theory_of ctxt
   128   in defined thy o intern thy end;
   129 
   130 fun check_tool name =
   131   File.exists (Path.append (Path.explode "~~/lib/Tools") (Path.basic name));
   132 
   133 val arg = enclose "{" "}" o clean_string;
   134 
   135 fun entity check markup kind index =
   136   Thy_Output.antiquotation
   137     (translate (fn " " => "_" | c => c) kind ^
   138       (case index of NONE => "" | SOME true => "_def" | SOME false => "_ref"))
   139     (Scan.lift (Scan.optional (Args.parens Args.name) "" -- Args.name))
   140     (fn {context = ctxt, ...} => fn (logic, name) =>
   141       let
   142         val hyper_name =
   143           "{" ^ Long_Name.append kind (Long_Name.append logic (clean_name name)) ^ "}";
   144         val hyper =
   145           enclose ("\\hyperlink" ^ hyper_name ^ "{") "}" #>
   146           index = SOME true ? enclose ("\\hypertarget" ^ hyper_name ^ "{") "}";
   147         val idx =
   148           (case index of
   149             NONE => ""
   150           | SOME is_def =>
   151               "\\index" ^ (if is_def then "def" else "ref") ^ arg logic ^ arg kind ^ arg name);
   152       in
   153         if check ctxt name then
   154           idx ^
   155           (Output.output name
   156             |> (if markup = "" then I else enclose ("\\" ^ markup ^ "{") "}")
   157             |> (if Config.get ctxt Thy_Output.quotes then quote else I)
   158             |> (if Config.get ctxt Thy_Output.display
   159                 then enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
   160                 else hyper o enclose "\\mbox{\\isa{" "}}"))
   161         else error ("Bad " ^ kind ^ " " ^ quote name)
   162       end);
   163 
   164 fun entity_antiqs check markup kind =
   165  ((entity check markup kind NONE);
   166   (entity check markup kind (SOME true));
   167   (entity check markup kind (SOME false)));
   168 
   169 in
   170 
   171 val _ = entity_antiqs no_check "" "syntax";
   172 val _ = entity_antiqs (K (is_some o Keyword.command_keyword)) "isacommand" "command";
   173 val _ = entity_antiqs (K Keyword.is_keyword) "isakeyword" "keyword";
   174 val _ = entity_antiqs (K Keyword.is_keyword) "isakeyword" "element";
   175 val _ = entity_antiqs (thy_check Method.intern Method.defined) "" "method";
   176 val _ = entity_antiqs (thy_check Attrib.intern Attrib.defined) "" "attribute";
   177 val _ = entity_antiqs no_check "" "fact";
   178 val _ = entity_antiqs no_check "" "variable";
   179 val _ = entity_antiqs no_check "" "case";
   180 val _ = entity_antiqs (K Thy_Output.defined_command) "" "antiquotation";
   181 val _ = entity_antiqs (K Thy_Output.defined_option) "" "antiquotation option";
   182 val _ = entity_antiqs (fn _ => fn name => is_some (OS.Process.getEnv name)) "isatt" "setting";
   183 val _ = entity_antiqs no_check "" "inference";
   184 val _ = entity_antiqs no_check "isatt" "executable";
   185 val _ = entity_antiqs (K check_tool) "isatt" "tool";
   186 val _ = entity_antiqs (K (File.exists o Path.explode)) "isatt" "file";
   187 val _ = entity_antiqs (K (can Thy_Info.get_theory)) "" "theory";
   188 val _ = entity_antiqs no_check "" "ML antiquotation";  (* FIXME proper check *)
   189 
   190 end;
   191 
   192 end;