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