src/Doc/antiquote_setup.ML
author wenzelm
Thu Nov 07 13:34:04 2013 +0100 (2013-11-07)
changeset 54372 2d61935eed4a
parent 53061 417cb0f713e0
child 54705 0dff3326d12a
permissions -rw-r--r--
misc tuning;
     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 (* unchecked file *)
    51 
    52 val file_unchecked_setup =
    53   Thy_Output.antiquotation @{binding file_unchecked} (Scan.lift (Parse.position Parse.path))
    54     (fn {context = ctxt, ...} => fn (name, pos) =>
    55       let
    56         val dir = Thy_Load.master_directory (Proof_Context.theory_of ctxt);
    57         val path = Path.append dir (Path.explode name);
    58         val _ = Position.report pos (Markup.path name);
    59       in
    60         space_explode "/" name
    61         |> map Thy_Output.verb_text
    62         |> space_implode (Thy_Output.verb_text "/" ^ "\\discretionary{}{}{}")
    63       end);
    64 
    65 
    66 (* ML text *)
    67 
    68 local
    69 
    70 fun ml_val (txt1, "") = "fn _ => (" ^ txt1 ^ ");"
    71   | ml_val (txt1, txt2) = "fn _ => (" ^ txt1 ^ " : " ^ txt2 ^ ");";
    72 
    73 fun ml_op (txt1, "") = "fn _ => (op " ^ txt1 ^ ");"
    74   | ml_op (txt1, txt2) = "fn _ => (op " ^ txt1 ^ " : " ^ txt2 ^ ");";
    75 
    76 fun ml_type (txt1, "") = "val _ = NONE : (" ^ txt1 ^ ") option;"
    77   | ml_type (txt1, txt2) = "val _ = [NONE : (" ^ txt1 ^ ") option, NONE : (" ^ txt2 ^ ") option];";
    78 
    79 fun ml_exn (txt1, "") = "fn _ => (" ^ txt1 ^ " : exn);"
    80   | ml_exn (txt1, txt2) = "fn _ => (" ^ txt1 ^ " : " ^ txt2 ^ " -> exn);";
    81 
    82 fun ml_structure (txt, _) = "functor XXX() = struct structure XX = " ^ txt ^ " end;";
    83 
    84 fun ml_functor (txt, _) = "ML_Env.check_functor " ^ ML_Syntax.print_string txt;
    85 
    86 val is_name = ML_Lex.kind_of #> (fn kind => kind = ML_Lex.Ident orelse kind = ML_Lex.LongIdent);
    87 
    88 fun ml_name txt =
    89   (case filter is_name (ML_Lex.tokenize txt) of
    90     toks as [_] => ML_Lex.flatten toks
    91   | _ => error ("Single ML name expected in input: " ^ quote txt));
    92 
    93 fun index_ml name kind ml = Thy_Output.antiquotation name
    94   (Scan.lift (Args.name -- Scan.optional (Args.colon |-- Args.name) ""))
    95   (fn {context = ctxt, ...} => fn (txt1, txt2) =>
    96     let
    97       val txt =
    98         if txt2 = "" then txt1
    99         else if kind = "type" then txt1 ^ " = " ^ txt2
   100         else if kind = "exception" then txt1 ^ " of " ^ txt2
   101         else if Symbol_Pos.is_identifier (Long_Name.base_name (ml_name txt1))
   102         then txt1 ^ ": " ^ txt2
   103         else txt1 ^ " : " ^ txt2;
   104       val txt' = if kind = "" then txt else kind ^ " " ^ txt;
   105       val _ = ML_Context.eval_text_in (SOME ctxt) false Position.none (ml (txt1, txt2));  (* ML_Lex.read (!?) *)
   106       val kind' = if kind = "" then "ML" else "ML " ^ kind;
   107     in
   108       "\\indexdef{}{" ^ kind' ^ "}{" ^ clean_string (ml_name txt1) ^ "}" ^
   109       (txt'
   110       |> (if Config.get ctxt Thy_Output.quotes then quote else I)
   111       |> (if Config.get ctxt Thy_Output.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
   112           else split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))
   113     end);
   114 
   115 in
   116 
   117 val index_ml_setup =
   118   index_ml @{binding index_ML} "" ml_val #>
   119   index_ml @{binding index_ML_op} "infix" ml_op #>
   120   index_ml @{binding index_ML_type} "type" ml_type #>
   121   index_ml @{binding index_ML_exn} "exception" ml_exn #>
   122   index_ml @{binding index_ML_structure} "structure" ml_structure #>
   123   index_ml @{binding index_ML_functor} "functor" ml_functor;
   124 
   125 end;
   126 
   127 
   128 (* named theorems *)
   129 
   130 val named_thms_setup =
   131   Thy_Output.antiquotation @{binding named_thms}
   132     (Scan.repeat (Attrib.thm -- Scan.lift (Args.parens Args.name)))
   133     (fn {context = ctxt, ...} =>
   134       map (apfst (Thy_Output.pretty_thm ctxt))
   135       #> (if Config.get ctxt Thy_Output.quotes then map (apfst Pretty.quote) else I)
   136       #> (if Config.get ctxt Thy_Output.display
   137           then
   138             map (fn (p, name) =>
   139               Output.output (Pretty.string_of (Pretty.indent (Config.get ctxt Thy_Output.indent) p)) ^
   140               "\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}")
   141             #> space_implode "\\par\\smallskip%\n"
   142             #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
   143           else
   144             map (fn (p, name) =>
   145               Output.output (Pretty.str_of p) ^
   146               "\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}")
   147             #> space_implode "\\par\\smallskip%\n"
   148             #> enclose "\\isa{" "}"));
   149 
   150 
   151 (* theory file *)
   152 
   153 val thy_file_setup =
   154   Thy_Output.antiquotation @{binding thy_file} (Scan.lift Args.name)
   155     (fn {context = ctxt, ...} =>
   156       fn name => (Thy_Load.check_thy Path.current name; Thy_Output.output ctxt [Pretty.str name]));
   157 
   158 
   159 (* Isabelle/Isar entities (with index) *)
   160 
   161 local
   162 
   163 fun no_check _ _ = true;
   164 
   165 fun command_check (name, pos) =
   166   is_some (Keyword.command_keyword name) andalso
   167     let
   168       val markup =
   169         Outer_Syntax.scan Position.none name
   170         |> maps (Outer_Syntax.command_reports (#2 (Outer_Syntax.get_syntax ())))
   171         |> map (snd o fst);
   172       val _ = List.app (Position.report pos) markup;
   173     in true end;
   174 
   175 fun thy_check check ctxt = can (check (Proof_Context.theory_of ctxt));
   176 
   177 fun check_tool (name, pos) =
   178   let
   179     fun tool dir =
   180       let val path = Path.append dir (Path.basic name)
   181       in if File.exists path then SOME path else NONE end;
   182   in
   183     (case get_first tool (Path.split (getenv "ISABELLE_TOOLS")) of
   184       NONE => false
   185     | SOME path => (Position.report pos (Markup.path (Path.implode path)); true))
   186   end;
   187 
   188 val arg = enclose "{" "}" o clean_string;
   189 
   190 fun entity check markup kind index =
   191   Thy_Output.antiquotation
   192     (Binding.name (translate (fn " " => "_" | c => c) kind ^
   193       (case index of NONE => "" | SOME true => "_def" | SOME false => "_ref")))
   194     (Scan.lift (Scan.optional (Args.parens Args.name) "" -- Parse.position Args.name))
   195     (fn {context = ctxt, ...} => fn (logic, (name, pos)) =>
   196       let
   197         val hyper_name =
   198           "{" ^ Long_Name.append kind (Long_Name.append logic (clean_name name)) ^ "}";
   199         val hyper =
   200           enclose ("\\hyperlink" ^ hyper_name ^ "{") "}" #>
   201           index = SOME true ? enclose ("\\hypertarget" ^ hyper_name ^ "{") "}";
   202         val idx =
   203           (case index of
   204             NONE => ""
   205           | SOME is_def =>
   206               "\\index" ^ (if is_def then "def" else "ref") ^ arg logic ^ arg kind ^ arg name);
   207       in
   208         if check ctxt (name, pos) then
   209           idx ^
   210           (Output.output name
   211             |> (if markup = "" then I else enclose ("\\" ^ markup ^ "{") "}")
   212             |> (if Config.get ctxt Thy_Output.quotes then quote else I)
   213             |> (if Config.get ctxt Thy_Output.display
   214                 then enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
   215                 else hyper o enclose "\\mbox{\\isa{" "}}"))
   216         else error ("Bad " ^ kind ^ " " ^ quote name ^ Position.here pos)
   217       end);
   218 
   219 fun entity_antiqs check markup kind =
   220   entity check markup kind NONE #>
   221   entity check markup kind (SOME true) #>
   222   entity check markup kind (SOME false);
   223 
   224 in
   225 
   226 val entity_setup =
   227   entity_antiqs no_check "" "syntax" #>
   228   entity_antiqs (K command_check) "isacommand" "command" #>
   229   entity_antiqs (K (Keyword.is_keyword o #1)) "isakeyword" "keyword" #>
   230   entity_antiqs (K (Keyword.is_keyword o #1)) "isakeyword" "element" #>
   231   entity_antiqs (thy_check Method.check) "" "method" #>
   232   entity_antiqs (thy_check Attrib.check) "" "attribute" #>
   233   entity_antiqs no_check "" "fact" #>
   234   entity_antiqs no_check "" "variable" #>
   235   entity_antiqs no_check "" "case" #>
   236   entity_antiqs (thy_check Thy_Output.check_command) "" "antiquotation" #>
   237   entity_antiqs (thy_check Thy_Output.check_option) "" "antiquotation option" #>
   238   entity_antiqs no_check "isatt" "setting" #>
   239   entity_antiqs no_check "isatt" "system option" #>
   240   entity_antiqs no_check "" "inference" #>
   241   entity_antiqs no_check "isatt" "executable" #>
   242   entity_antiqs (K check_tool) "isatool" "tool" #>
   243   entity_antiqs (thy_check ML_Context.check_antiq) "" Markup.ML_antiquotationN;
   244 
   245 end;
   246 
   247 
   248 (* theory setup *)
   249 
   250 val setup =
   251   verbatim_setup #>
   252   file_unchecked_setup #>
   253   index_ml_setup #>
   254   named_thms_setup #>
   255   thy_file_setup #>
   256   entity_setup;
   257 
   258 end;