src/Doc/antiquote_setup.ML
author wenzelm
Thu Mar 27 17:56:13 2014 +0100 (2014-03-27)
changeset 56304 40274e4f5ebf
parent 56294 85911b8a6868
child 56467 8d7d6f17c6a7
permissions -rw-r--r--
redirect ML_Compiler reports more directly: only the (big) parse tree report is deferred via Execution.print (NB: this does not work for asynchronous "diag" commands);
more explicit ML_Compiler.flags;
     1 (*  Title:      Doc/antiquote_setup.ML
     2     Author:     Makarius
     3 
     4 Auxiliary antiquotations for the Isabelle manuals.
     5 *)
     6 
     7 structure Antiquote_Setup: 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     | ">" => "$>$"
    21     | "{" => "\\{"
    22     | "|" => "$\\mid$"
    23     | "}" => "\\}"
    24     | "\<hyphen>" => "-"
    25     | c => c);
    26 
    27 fun clean_name "\<dots>" = "dots"
    28   | clean_name ".." = "ddot"
    29   | clean_name "." = "dot"
    30   | clean_name "_" = "underscore"
    31   | clean_name "{" = "braceleft"
    32   | clean_name "}" = "braceright"
    33   | clean_name s = s |> translate (fn "_" => "-" | "\<hyphen>" => "-" | c => c);
    34 
    35 
    36 (* verbatim text *)
    37 
    38 val verbatim = space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|";
    39 
    40 val _ =
    41   Theory.setup (Thy_Output.antiquotation @{binding verbatim} (Scan.lift Args.name)
    42     (K (split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n")));
    43 
    44 
    45 (* ML text *)
    46 
    47 local
    48 
    49 val ml_toks = ML_Lex.read Position.none;
    50 
    51 fun ml_val (toks1, []) = ml_toks "fn _ => (" @ toks1 @ ml_toks ");"
    52   | ml_val (toks1, toks2) = ml_toks "fn _ => (" @ toks1 @ ml_toks " : " @ toks2 @ ml_toks ");";
    53 
    54 fun ml_op (toks1, []) = ml_toks "fn _ => (op " @ toks1 @ ml_toks ");"
    55   | ml_op (toks1, toks2) = ml_toks "fn _ => (op " @ toks1 @ ml_toks " : " @ toks2 @ ml_toks ");";
    56 
    57 fun ml_type (toks1, []) = ml_toks "val _ = NONE : (" @ toks1 @ ml_toks ") option;"
    58   | ml_type (toks1, toks2) =
    59       ml_toks "val _ = [NONE : (" @ toks1 @ ml_toks ") option, NONE : (" @
    60         toks2 @ ml_toks ") option];";
    61 
    62 fun ml_exception (toks1, []) = ml_toks "fn _ => (" @ toks1 @ ml_toks " : exn);"
    63   | ml_exception (toks1, toks2) =
    64       ml_toks "fn _ => (" @ toks1 @ ml_toks " : " @ toks2 @ ml_toks " -> exn);";
    65 
    66 fun ml_structure (toks, _) =
    67   ml_toks "functor XXX() = struct structure XX = " @ toks @ ml_toks " end;";
    68 
    69 fun ml_functor (Antiquote.Text tok :: _, _) =
    70       ml_toks "ML_Env.check_functor " @ ml_toks (ML_Syntax.print_string (ML_Lex.content_of tok))
    71   | ml_functor _ = raise Fail "Bad ML functor specification";
    72 
    73 val is_name = ML_Lex.kind_of #> (fn kind => kind = ML_Lex.Ident orelse kind = ML_Lex.LongIdent);
    74 
    75 fun ml_name txt =
    76   (case filter is_name (ML_Lex.tokenize txt) of
    77     toks as [_] => ML_Lex.flatten toks
    78   | _ => error ("Single ML name expected in input: " ^ quote txt));
    79 
    80 fun prep_ml source =
    81   (#1 (Symbol_Pos.source_content source), ML_Lex.read_source false source);
    82 
    83 fun index_ml name kind ml = Thy_Output.antiquotation name
    84   (Scan.lift (Args.name_source_position -- Scan.option (Args.colon |-- Args.name_source_position)))
    85   (fn {context = ctxt, ...} => fn (source1, opt_source2) =>
    86     let
    87       val (txt1, toks1) = prep_ml source1;
    88       val (txt2, toks2) =
    89         (case opt_source2 of
    90           SOME source => prep_ml source
    91         | NONE => ("", []));
    92 
    93       val txt =
    94         if txt2 = "" then txt1
    95         else if kind = "type" then txt1 ^ " = " ^ txt2
    96         else if kind = "exception" then txt1 ^ " of " ^ txt2
    97         else if Symbol_Pos.is_identifier (Long_Name.base_name (ml_name txt1))
    98         then txt1 ^ ": " ^ txt2
    99         else txt1 ^ " : " ^ txt2;
   100       val txt' = if kind = "" then txt else kind ^ " " ^ txt;
   101 
   102       val _ =
   103         ML_Context.eval_in (SOME ctxt) ML_Compiler.flags (#pos source1) (ml (toks1, toks2));
   104       val kind' = if kind = "" then "ML" else "ML " ^ kind;
   105     in
   106       "\\indexdef{}{" ^ kind' ^ "}{" ^ clean_string (ml_name txt1) ^ "}" ^
   107       (txt'
   108       |> (if Config.get ctxt Thy_Output.quotes then quote else I)
   109       |> (if Config.get ctxt Thy_Output.display
   110           then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
   111           else split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))
   112     end);
   113 
   114 in
   115 
   116 val _ =
   117   Theory.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_exception} "exception" ml_exception #>
   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 _ =
   131   Theory.setup (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 _ =
   154   Theory.setup (Thy_Output.antiquotation @{binding thy_file} (Scan.lift Args.name)
   155     (fn {context = ctxt, ...} =>
   156       fn name => (Resources.check_thy Path.current name; Thy_Output.output ctxt [Pretty.str name])));
   157 
   158 
   159 (* Isabelle/jEdit elements *)
   160 
   161 local
   162 
   163 fun parse_named a (XML.Elem ((b, props), _)) =
   164       (case Properties.get props "NAME" of
   165         SOME name => if a = b then [name] else []
   166       | NONE => [])
   167   | parse_named _ _ = [];
   168 
   169 val jedit_actions =
   170   (case XML.parse (File.read @{path "~~/src/Tools/jEdit/src/actions.xml"}) of
   171     XML.Elem (("ACTIONS", _), body) => maps (parse_named "ACTION") body
   172   | _ => []);
   173 
   174 val jedit_dockables =
   175   (case XML.parse (File.read @{path "~~/src/Tools/jEdit/src/dockables.xml"}) of
   176     XML.Elem (("DOCKABLES", _), body) => maps (parse_named "DOCKABLE") body
   177   | _ => []);
   178 
   179 val all_actions = jedit_actions @ jedit_dockables;
   180 
   181 in
   182 
   183 val is_action = member (op =) all_actions;
   184 
   185 end;
   186 
   187 
   188 (* Isabelle/Isar entities (with index) *)
   189 
   190 local
   191 
   192 fun no_check _ _ = true;
   193 
   194 fun check_command ctxt (name, pos) =
   195   is_some (Keyword.command_keyword name) andalso
   196     let
   197       val markup =
   198         Outer_Syntax.scan Position.none name
   199         |> maps (Outer_Syntax.command_reports (#2 (Outer_Syntax.get_syntax ())))
   200         |> map (snd o fst);
   201       val _ = Context_Position.reports ctxt (map (pair pos) markup);
   202     in true end;
   203 
   204 fun check_tool ctxt (name, pos) =
   205   let
   206     fun tool dir =
   207       let val path = Path.append dir (Path.basic name)
   208       in if File.exists path then SOME path else NONE end;
   209   in
   210     (case get_first tool (Path.split (getenv "ISABELLE_TOOLS")) of
   211       NONE => false
   212     | SOME path => (Context_Position.report ctxt pos (Markup.path (Path.implode path)); true))
   213   end;
   214 
   215 val arg = enclose "{" "}" o clean_string;
   216 
   217 fun entity check markup binding index =
   218   Thy_Output.antiquotation
   219     (binding |> Binding.map_name (fn name => name ^
   220       (case index of NONE => "" | SOME true => "_def" | SOME false => "_ref")))
   221     (Scan.lift (Scan.optional (Args.parens Args.name) "" -- Parse.position Args.name))
   222     (fn {context = ctxt, ...} => fn (logic, (name, pos)) =>
   223       let
   224         val kind = translate (fn "_" => " " | c => c) (Binding.name_of binding);
   225         val hyper_name =
   226           "{" ^ Long_Name.append kind (Long_Name.append logic (clean_name name)) ^ "}";
   227         val hyper =
   228           enclose ("\\hyperlink" ^ hyper_name ^ "{") "}" #>
   229           index = SOME true ? enclose ("\\hypertarget" ^ hyper_name ^ "{") "}";
   230         val idx =
   231           (case index of
   232             NONE => ""
   233           | SOME is_def =>
   234               "\\index" ^ (if is_def then "def" else "ref") ^ arg logic ^ arg kind ^ arg name);
   235       in
   236         if check ctxt (name, pos) then
   237           idx ^
   238           (Output.output name
   239             |> (if markup = "" then I else enclose ("\\" ^ markup ^ "{") "}")
   240             |> (if Config.get ctxt Thy_Output.quotes then quote else I)
   241             |> (if Config.get ctxt Thy_Output.display
   242                 then enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
   243                 else hyper o enclose "\\mbox{\\isa{" "}}"))
   244         else error ("Bad " ^ kind ^ " " ^ quote name ^ Position.here pos)
   245       end);
   246 
   247 fun entity_antiqs check markup kind =
   248   entity check markup kind NONE #>
   249   entity check markup kind (SOME true) #>
   250   entity check markup kind (SOME false);
   251 
   252 in
   253 
   254 val _ =
   255   Theory.setup
   256    (entity_antiqs no_check "" @{binding syntax} #>
   257     entity_antiqs check_command "isacommand" @{binding command} #>
   258     entity_antiqs (K (Keyword.is_keyword o #1)) "isakeyword" @{binding keyword} #>
   259     entity_antiqs (K (Keyword.is_keyword o #1)) "isakeyword" @{binding element} #>
   260     entity_antiqs (can o Method.check_name) "" @{binding method} #>
   261     entity_antiqs (can o Attrib.check_name) "" @{binding attribute} #>
   262     entity_antiqs no_check "" @{binding fact} #>
   263     entity_antiqs no_check "" @{binding variable} #>
   264     entity_antiqs no_check "" @{binding case} #>
   265     entity_antiqs (can o Thy_Output.check_command) "" @{binding antiquotation} #>
   266     entity_antiqs (can o Thy_Output.check_option) "" @{binding antiquotation_option} #>
   267     entity_antiqs no_check "isatt" @{binding setting} #>
   268     entity_antiqs no_check "isatt" @{binding system_option} #>
   269     entity_antiqs no_check "" @{binding inference} #>
   270     entity_antiqs no_check "isatt" @{binding executable} #>
   271     entity_antiqs check_tool "isatool" @{binding tool} #>
   272     entity_antiqs (can o ML_Context.check_antiquotation) "" @{binding ML_antiquotation} #>
   273     entity_antiqs (K (is_action o #1)) "isatt" @{binding action});
   274 
   275 end;
   276 
   277 end;