src/Doc/antiquote_setup.ML
author wenzelm
Tue Aug 12 20:18:27 2014 +0200 (2014-08-12)
changeset 57918 f5d73caba4e5
parent 57477 c3b5cb53ea79
child 58069 0255436b3d85
permissions -rw-r--r--
tuned signature according to Scala version -- prefer explicit argument;
     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 pos = #pos source1;
   103       val _ =
   104         ML_Context.eval_in (SOME ctxt) ML_Compiler.flags pos (ml (toks1, toks2))
   105           handle ERROR msg => error (msg ^ Position.here pos);
   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
   112           then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
   113           else split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))
   114     end);
   115 
   116 in
   117 
   118 val _ =
   119   Theory.setup
   120    (index_ml @{binding index_ML} "" ml_val #>
   121     index_ml @{binding index_ML_op} "infix" ml_op #>
   122     index_ml @{binding index_ML_type} "type" ml_type #>
   123     index_ml @{binding index_ML_exception} "exception" ml_exception #>
   124     index_ml @{binding index_ML_structure} "structure" ml_structure #>
   125     index_ml @{binding index_ML_functor} "functor" ml_functor);
   126 
   127 end;
   128 
   129 
   130 (* named theorems *)
   131 
   132 val _ =
   133   Theory.setup (Thy_Output.antiquotation @{binding named_thms}
   134     (Scan.repeat (Attrib.thm -- Scan.lift (Args.parens Args.name)))
   135     (fn {context = ctxt, ...} =>
   136       map (apfst (Thy_Output.pretty_thm ctxt))
   137       #> (if Config.get ctxt Thy_Output.quotes then map (apfst Pretty.quote) else I)
   138       #> (if Config.get ctxt Thy_Output.display
   139           then
   140             map (fn (p, name) =>
   141               Output.output (Pretty.string_of (Pretty.indent (Config.get ctxt Thy_Output.indent) p)) ^
   142               "\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}")
   143             #> space_implode "\\par\\smallskip%\n"
   144             #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
   145           else
   146             map (fn (p, name) =>
   147               Output.output (Pretty.str_of p) ^
   148               "\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}")
   149             #> space_implode "\\par\\smallskip%\n"
   150             #> enclose "\\isa{" "}")));
   151 
   152 
   153 (* theory file *)
   154 
   155 val _ =
   156   Theory.setup (Thy_Output.antiquotation @{binding thy_file} (Scan.lift Args.name)
   157     (fn {context = ctxt, ...} =>
   158       fn name => (Resources.check_thy Path.current name; Thy_Output.output ctxt [Pretty.str name])));
   159 
   160 
   161 (* Isabelle/jEdit elements *)
   162 
   163 local
   164 
   165 fun parse_named a (XML.Elem ((b, props), _)) =
   166       (case Properties.get props "NAME" of
   167         SOME name => if a = b then [name] else []
   168       | NONE => [])
   169   | parse_named _ _ = [];
   170 
   171 val isabelle_jedit_actions =
   172   (case XML.parse (File.read @{path "~~/src/Tools/jEdit/src/actions.xml"}) of
   173     XML.Elem (("ACTIONS", _), body) => maps (parse_named "ACTION") body
   174   | _ => []);
   175 
   176 val isabelle_jedit_dockables =
   177   (case XML.parse (File.read @{path "~~/src/Tools/jEdit/src/dockables.xml"}) of
   178     XML.Elem (("DOCKABLES", _), body) => maps (parse_named "DOCKABLE") body
   179   | _ => []);
   180 
   181 val jedit_actions =
   182   Lazy.lazy (fn () =>
   183     (case Isabelle_System.bash_output
   184       "unzip -p \"$JEDIT_HOME/dist/jedit.jar\" org/gjt/sp/jedit/actions.xml" of
   185       (txt, 0) =>
   186         (case XML.parse txt of
   187           XML.Elem (("ACTIONS", _), body) => maps (parse_named "ACTION") body
   188         | _ => [])
   189     | (_, rc) => error ("Cannot unzip jedit.jar\nreturn code = " ^ string_of_int rc)));
   190 
   191 in
   192 
   193 fun is_action a =
   194   member (op =) isabelle_jedit_actions a orelse
   195   member (op =) isabelle_jedit_dockables a orelse
   196   member (op =) (Lazy.force jedit_actions) a;
   197 
   198 end;
   199 
   200 
   201 (* Isabelle/Isar entities (with index) *)
   202 
   203 local
   204 
   205 fun no_check _ _ = true;
   206 
   207 fun check_command ctxt (name, pos) =
   208   is_some (Keyword.command_keyword name) andalso
   209     let
   210       val markup =
   211         Outer_Syntax.scan (Keyword.get_lexicons ()) Position.none name
   212         |> maps (Outer_Syntax.command_reports (#2 (Outer_Syntax.get_syntax ())))
   213         |> map (snd o fst);
   214       val _ = Context_Position.reports ctxt (map (pair pos) markup);
   215     in true end;
   216 
   217 fun check_system_option ctxt (name, pos) =
   218   (Context_Position.report ctxt pos (Options.default_markup (name, pos)); true)
   219     handle ERROR _ => false;
   220 
   221 fun check_tool ctxt (name, pos) =
   222   let
   223     fun tool dir =
   224       let val path = Path.append dir (Path.basic name)
   225       in if File.exists path then SOME path else NONE end;
   226   in
   227     (case get_first tool (Path.split (getenv "ISABELLE_TOOLS")) of
   228       NONE => false
   229     | SOME path => (Context_Position.report ctxt pos (Markup.path (Path.implode path)); true))
   230   end;
   231 
   232 val arg = enclose "{" "}" o clean_string;
   233 
   234 fun entity check markup binding index =
   235   Thy_Output.antiquotation
   236     (binding |> Binding.map_name (fn name => name ^
   237       (case index of NONE => "" | SOME true => "_def" | SOME false => "_ref")))
   238     (Scan.lift (Scan.optional (Args.parens Args.name) "" -- Parse.position Args.name))
   239     (fn {context = ctxt, ...} => fn (logic, (name, pos)) =>
   240       let
   241         val kind = translate (fn "_" => " " | c => c) (Binding.name_of binding);
   242         val hyper_name =
   243           "{" ^ Long_Name.append kind (Long_Name.append logic (clean_name name)) ^ "}";
   244         val hyper =
   245           enclose ("\\hyperlink" ^ hyper_name ^ "{") "}" #>
   246           index = SOME true ? enclose ("\\hypertarget" ^ hyper_name ^ "{") "}";
   247         val idx =
   248           (case index of
   249             NONE => ""
   250           | SOME is_def =>
   251               "\\index" ^ (if is_def then "def" else "ref") ^ arg logic ^ arg kind ^ arg name);
   252       in
   253         if check ctxt (name, pos) then
   254           idx ^
   255           (Output.output name
   256             |> (if markup = "" then I else enclose ("\\" ^ markup ^ "{") "}")
   257             |> (if Config.get ctxt Thy_Output.quotes then quote else I)
   258             |> (if Config.get ctxt Thy_Output.display
   259                 then enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
   260                 else hyper o enclose "\\mbox{\\isa{" "}}"))
   261         else error ("Bad " ^ kind ^ " " ^ quote name ^ Position.here pos)
   262       end);
   263 
   264 fun entity_antiqs check markup kind =
   265   entity check markup kind NONE #>
   266   entity check markup kind (SOME true) #>
   267   entity check markup kind (SOME false);
   268 
   269 in
   270 
   271 val _ =
   272   Theory.setup
   273    (entity_antiqs no_check "" @{binding syntax} #>
   274     entity_antiqs check_command "isacommand" @{binding command} #>
   275     entity_antiqs (K (Keyword.is_keyword o #1)) "isakeyword" @{binding keyword} #>
   276     entity_antiqs (K (Keyword.is_keyword o #1)) "isakeyword" @{binding element} #>
   277     entity_antiqs (can o Method.check_name) "" @{binding method} #>
   278     entity_antiqs (can o Attrib.check_name) "" @{binding attribute} #>
   279     entity_antiqs no_check "" @{binding fact} #>
   280     entity_antiqs no_check "" @{binding variable} #>
   281     entity_antiqs no_check "" @{binding case} #>
   282     entity_antiqs (can o Thy_Output.check_command) "" @{binding antiquotation} #>
   283     entity_antiqs (can o Thy_Output.check_option) "" @{binding antiquotation_option} #>
   284     entity_antiqs no_check "isatt" @{binding setting} #>
   285     entity_antiqs check_system_option "isatt" @{binding system_option} #>
   286     entity_antiqs no_check "" @{binding inference} #>
   287     entity_antiqs no_check "isatt" @{binding executable} #>
   288     entity_antiqs check_tool "isatool" @{binding tool} #>
   289     entity_antiqs (can o ML_Context.check_antiquotation) "" @{binding ML_antiquotation} #>
   290     entity_antiqs (K (is_action o #1)) "isatt" @{binding action});
   291 
   292 end;
   293 
   294 end;