src/Doc/antiquote_setup.ML
author wenzelm
Tue Mar 25 16:11:00 2014 +0100 (2014-03-25)
changeset 56278 2576d3a40ed6
parent 56275 600f432ab556
child 56294 85911b8a6868
permissions -rw-r--r--
separate tokenization and language context for SML: no symbols, no antiquotes;
     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) {SML = false, verbose = false} (#pos source1)
   104           (ml (toks1, toks2));
   105       val kind' = if kind = "" then "ML" else "ML " ^ kind;
   106     in
   107       "\\indexdef{}{" ^ kind' ^ "}{" ^ clean_string (ml_name txt1) ^ "}" ^
   108       (txt'
   109       |> (if Config.get ctxt Thy_Output.quotes then quote else I)
   110       |> (if Config.get ctxt Thy_Output.display
   111           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 _ =
   118   Theory.setup
   119    (index_ml @{binding index_ML} "" ml_val #>
   120     index_ml @{binding index_ML_op} "infix" ml_op #>
   121     index_ml @{binding index_ML_type} "type" ml_type #>
   122     index_ml @{binding index_ML_exception} "exception" ml_exception #>
   123     index_ml @{binding index_ML_structure} "structure" ml_structure #>
   124     index_ml @{binding index_ML_functor} "functor" ml_functor);
   125 
   126 end;
   127 
   128 
   129 (* named theorems *)
   130 
   131 val _ =
   132   Theory.setup (Thy_Output.antiquotation @{binding named_thms}
   133     (Scan.repeat (Attrib.thm -- Scan.lift (Args.parens Args.name)))
   134     (fn {context = ctxt, ...} =>
   135       map (apfst (Thy_Output.pretty_thm ctxt))
   136       #> (if Config.get ctxt Thy_Output.quotes then map (apfst Pretty.quote) else I)
   137       #> (if Config.get ctxt Thy_Output.display
   138           then
   139             map (fn (p, name) =>
   140               Output.output (Pretty.string_of (Pretty.indent (Config.get ctxt Thy_Output.indent) p)) ^
   141               "\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}")
   142             #> space_implode "\\par\\smallskip%\n"
   143             #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
   144           else
   145             map (fn (p, name) =>
   146               Output.output (Pretty.str_of p) ^
   147               "\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}")
   148             #> space_implode "\\par\\smallskip%\n"
   149             #> enclose "\\isa{" "}")));
   150 
   151 
   152 (* theory file *)
   153 
   154 val _ =
   155   Theory.setup (Thy_Output.antiquotation @{binding thy_file} (Scan.lift Args.name)
   156     (fn {context = ctxt, ...} =>
   157       fn name => (Resources.check_thy Path.current name; Thy_Output.output ctxt [Pretty.str name])));
   158 
   159 
   160 (* Isabelle/jEdit elements *)
   161 
   162 local
   163 
   164 fun parse_named a (XML.Elem ((b, props), _)) =
   165       (case Properties.get props "NAME" of
   166         SOME name => if a = b then [name] else []
   167       | NONE => [])
   168   | parse_named _ _ = [];
   169 
   170 val jedit_actions =
   171   (case XML.parse (File.read @{path "~~/src/Tools/jEdit/src/actions.xml"}) of
   172     XML.Elem (("ACTIONS", _), body) => maps (parse_named "ACTION") body
   173   | _ => []);
   174 
   175 val jedit_dockables =
   176   (case XML.parse (File.read @{path "~~/src/Tools/jEdit/src/dockables.xml"}) of
   177     XML.Elem (("DOCKABLES", _), body) => maps (parse_named "DOCKABLE") body
   178   | _ => []);
   179 
   180 val all_actions = jedit_actions @ jedit_dockables;
   181 
   182 in
   183 
   184 val is_action = member (op =) all_actions;
   185 
   186 end;
   187 
   188 
   189 (* Isabelle/Isar entities (with index) *)
   190 
   191 local
   192 
   193 fun no_check _ _ = true;
   194 
   195 fun check_command (name, pos) =
   196   is_some (Keyword.command_keyword name) andalso
   197     let
   198       val markup =
   199         Outer_Syntax.scan Position.none name
   200         |> maps (Outer_Syntax.command_reports (#2 (Outer_Syntax.get_syntax ())))
   201         |> map (snd o fst);
   202       val _ = List.app (Position.report pos) markup;
   203     in true end;
   204 
   205 fun check_tool (name, pos) =
   206   let
   207     fun tool dir =
   208       let val path = Path.append dir (Path.basic name)
   209       in if File.exists path then SOME path else NONE end;
   210   in
   211     (case get_first tool (Path.split (getenv "ISABELLE_TOOLS")) of
   212       NONE => false
   213     | SOME path => (Position.report pos (Markup.path (Path.implode path)); true))
   214   end;
   215 
   216 val arg = enclose "{" "}" o clean_string;
   217 
   218 fun entity check markup binding index =
   219   Thy_Output.antiquotation
   220     (binding |> Binding.map_name (fn name => name ^
   221       (case index of NONE => "" | SOME true => "_def" | SOME false => "_ref")))
   222     (Scan.lift (Scan.optional (Args.parens Args.name) "" -- Parse.position Args.name))
   223     (fn {context = ctxt, ...} => fn (logic, (name, pos)) =>
   224       let
   225         val kind = translate (fn "_" => " " | c => c) (Binding.name_of binding);
   226         val hyper_name =
   227           "{" ^ Long_Name.append kind (Long_Name.append logic (clean_name name)) ^ "}";
   228         val hyper =
   229           enclose ("\\hyperlink" ^ hyper_name ^ "{") "}" #>
   230           index = SOME true ? enclose ("\\hypertarget" ^ hyper_name ^ "{") "}";
   231         val idx =
   232           (case index of
   233             NONE => ""
   234           | SOME is_def =>
   235               "\\index" ^ (if is_def then "def" else "ref") ^ arg logic ^ arg kind ^ arg name);
   236       in
   237         if check ctxt (name, pos) then
   238           idx ^
   239           (Output.output name
   240             |> (if markup = "" then I else enclose ("\\" ^ markup ^ "{") "}")
   241             |> (if Config.get ctxt Thy_Output.quotes then quote else I)
   242             |> (if Config.get ctxt Thy_Output.display
   243                 then enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
   244                 else hyper o enclose "\\mbox{\\isa{" "}}"))
   245         else error ("Bad " ^ kind ^ " " ^ quote name ^ Position.here pos)
   246       end);
   247 
   248 fun entity_antiqs check markup kind =
   249   entity check markup kind NONE #>
   250   entity check markup kind (SOME true) #>
   251   entity check markup kind (SOME false);
   252 
   253 in
   254 
   255 val _ =
   256   Theory.setup
   257    (entity_antiqs no_check "" @{binding syntax} #>
   258     entity_antiqs (K check_command) "isacommand" @{binding command} #>
   259     entity_antiqs (K (Keyword.is_keyword o #1)) "isakeyword" @{binding keyword} #>
   260     entity_antiqs (K (Keyword.is_keyword o #1)) "isakeyword" @{binding element} #>
   261     entity_antiqs (can o Method.check_name) "" @{binding method} #>
   262     entity_antiqs (can o Attrib.check_name) "" @{binding attribute} #>
   263     entity_antiqs no_check "" @{binding fact} #>
   264     entity_antiqs no_check "" @{binding variable} #>
   265     entity_antiqs no_check "" @{binding case} #>
   266     entity_antiqs (can o Thy_Output.check_command) "" @{binding antiquotation} #>
   267     entity_antiqs (can o Thy_Output.check_option) "" @{binding antiquotation_option} #>
   268     entity_antiqs no_check "isatt" @{binding setting} #>
   269     entity_antiqs no_check "isatt" @{binding system_option} #>
   270     entity_antiqs no_check "" @{binding inference} #>
   271     entity_antiqs no_check "isatt" @{binding executable} #>
   272     entity_antiqs (K check_tool) "isatool" @{binding tool} #>
   273     entity_antiqs (can o ML_Context.check_antiquotation) "" @{binding ML_antiquotation} #>
   274     entity_antiqs (K (is_action o #1)) "isatt" @{binding action});
   275 
   276 end;
   277 
   278 end;