src/Doc/antiquote_setup.ML
author wenzelm
Mon Dec 22 16:44:24 2014 +0100 (2014-12-22)
changeset 59175 bf465f335e85
parent 59083 88b0b1f28adc
child 59809 87641097d0f3
permissions -rw-r--r--
system option "pretty_margin" is superseded by "thy_output_margin";
     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 (* ML text *)
    37 
    38 local
    39 
    40 fun ml_val (toks1, []) = ML_Lex.read "fn _ => (" @ toks1 @ ML_Lex.read ");"
    41   | ml_val (toks1, toks2) =
    42       ML_Lex.read "fn _ => (" @ toks1 @ ML_Lex.read " : " @ toks2 @ ML_Lex.read ");";
    43 
    44 fun ml_op (toks1, []) = ML_Lex.read "fn _ => (op " @ toks1 @ ML_Lex.read ");"
    45   | ml_op (toks1, toks2) =
    46       ML_Lex.read "fn _ => (op " @ toks1 @ ML_Lex.read " : " @ toks2 @ ML_Lex.read ");";
    47 
    48 fun ml_type (toks1, []) = ML_Lex.read "val _ = NONE : (" @ toks1 @ ML_Lex.read ") option;"
    49   | ml_type (toks1, toks2) =
    50       ML_Lex.read "val _ = [NONE : (" @ toks1 @ ML_Lex.read ") option, NONE : (" @
    51         toks2 @ ML_Lex.read ") option];";
    52 
    53 fun ml_exception (toks1, []) = ML_Lex.read "fn _ => (" @ toks1 @ ML_Lex.read " : exn);"
    54   | ml_exception (toks1, toks2) =
    55       ML_Lex.read "fn _ => (" @ toks1 @ ML_Lex.read " : " @ toks2 @ ML_Lex.read " -> exn);";
    56 
    57 fun ml_structure (toks, _) =
    58   ML_Lex.read "functor XXX() = struct structure XX = " @ toks @ ML_Lex.read " end;";
    59 
    60 fun ml_functor (Antiquote.Text tok :: _, _) =
    61       ML_Lex.read "ML_Env.check_functor " @
    62       ML_Lex.read (ML_Syntax.print_string (ML_Lex.content_of tok))
    63   | ml_functor _ = raise Fail "Bad ML functor specification";
    64 
    65 val is_name =
    66   ML_Lex.kind_of #> (fn kind => kind = ML_Lex.Ident orelse kind = ML_Lex.Long_Ident);
    67 
    68 fun ml_name txt =
    69   (case filter is_name (ML_Lex.tokenize txt) of
    70     toks as [_] => ML_Lex.flatten toks
    71   | _ => error ("Single ML name expected in input: " ^ quote txt));
    72 
    73 fun prep_ml source =
    74   (Input.source_content source, ML_Lex.read_source false source);
    75 
    76 fun index_ml name kind ml = Thy_Output.antiquotation name
    77   (Scan.lift (Args.text_source_position -- Scan.option (Args.colon |-- Args.text_source_position)))
    78   (fn {context = ctxt, ...} => fn (source1, opt_source2) =>
    79     let
    80       val (txt1, toks1) = prep_ml source1;
    81       val (txt2, toks2) =
    82         (case opt_source2 of
    83           SOME source => prep_ml source
    84         | NONE => ("", []));
    85 
    86       val txt =
    87         if txt2 = "" then txt1
    88         else if kind = "type" then txt1 ^ " = " ^ txt2
    89         else if kind = "exception" then txt1 ^ " of " ^ txt2
    90         else if Symbol_Pos.is_identifier (Long_Name.base_name (ml_name txt1))
    91         then txt1 ^ ": " ^ txt2
    92         else txt1 ^ " : " ^ txt2;
    93       val txt' = if kind = "" then txt else kind ^ " " ^ txt;
    94 
    95       val pos = Input.pos_of source1;
    96       val _ =
    97         ML_Context.eval_in (SOME ctxt) ML_Compiler.flags pos (ml (toks1, toks2))
    98           handle ERROR msg => error (msg ^ Position.here pos);
    99       val kind' = if kind = "" then "ML" else "ML " ^ kind;
   100     in
   101       "\\indexdef{}{" ^ kind' ^ "}{" ^ clean_string (ml_name txt1) ^ "}" ^
   102       (Thy_Output.verbatim_text ctxt txt')
   103     end);
   104 
   105 in
   106 
   107 val _ =
   108   Theory.setup
   109    (index_ml @{binding index_ML} "" ml_val #>
   110     index_ml @{binding index_ML_op} "infix" ml_op #>
   111     index_ml @{binding index_ML_type} "type" ml_type #>
   112     index_ml @{binding index_ML_exception} "exception" ml_exception #>
   113     index_ml @{binding index_ML_structure} "structure" ml_structure #>
   114     index_ml @{binding index_ML_functor} "functor" ml_functor);
   115 
   116 end;
   117 
   118 
   119 (* named theorems *)
   120 
   121 val _ =
   122   Theory.setup (Thy_Output.antiquotation @{binding named_thms}
   123     (Scan.repeat (Attrib.thm -- Scan.lift (Args.parens Args.name)))
   124     (fn {context = ctxt, ...} =>
   125       map (apfst (Thy_Output.pretty_thm ctxt))
   126       #> (if Config.get ctxt Thy_Output.quotes then map (apfst Pretty.quote) else I)
   127       #> (if Config.get ctxt Thy_Output.display
   128           then
   129             map (fn (p, name) =>
   130               Output.output
   131                 (Thy_Output.string_of_margin ctxt
   132                   (Pretty.indent (Config.get ctxt Thy_Output.indent) p)) ^
   133               "\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}")
   134             #> space_implode "\\par\\smallskip%\n"
   135             #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
   136           else
   137             map (fn (p, name) =>
   138               Output.output (Pretty.str_of p) ^
   139               "\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}")
   140             #> space_implode "\\par\\smallskip%\n"
   141             #> enclose "\\isa{" "}")));
   142 
   143 
   144 (* theory file *)
   145 
   146 val _ =
   147   Theory.setup (Thy_Output.antiquotation @{binding thy_file} (Scan.lift Args.name)
   148     (fn {context = ctxt, ...} =>
   149       fn name => (Resources.check_thy Path.current name; Thy_Output.output ctxt [Pretty.str name])));
   150 
   151 
   152 (* Isabelle/jEdit elements *)
   153 
   154 local
   155 
   156 fun parse_named a (XML.Elem ((b, props), _)) =
   157       (case Properties.get props "NAME" of
   158         SOME name => if a = b then [name] else []
   159       | NONE => [])
   160   | parse_named _ _ = [];
   161 
   162 val isabelle_jedit_actions =
   163   (case XML.parse (File.read @{path "~~/src/Tools/jEdit/src/actions.xml"}) of
   164     XML.Elem (("ACTIONS", _), body) => maps (parse_named "ACTION") body
   165   | _ => []);
   166 
   167 val isabelle_jedit_dockables =
   168   (case XML.parse (File.read @{path "~~/src/Tools/jEdit/src/dockables.xml"}) of
   169     XML.Elem (("DOCKABLES", _), body) => maps (parse_named "DOCKABLE") body
   170   | _ => []);
   171 
   172 val jedit_actions =
   173   Lazy.lazy (fn () =>
   174     (case Isabelle_System.bash_output
   175       "unzip -p \"$JEDIT_HOME/dist/jedit.jar\" org/gjt/sp/jedit/actions.xml" of
   176       (txt, 0) =>
   177         (case XML.parse txt of
   178           XML.Elem (("ACTIONS", _), body) => maps (parse_named "ACTION") body
   179         | _ => [])
   180     | (_, rc) => error ("Cannot unzip jedit.jar\nreturn code = " ^ string_of_int rc)));
   181 
   182 in
   183 
   184 fun is_action a =
   185   member (op =) isabelle_jedit_actions a orelse
   186   member (op =) isabelle_jedit_dockables a orelse
   187   member (op =) (Lazy.force jedit_actions) a;
   188 
   189 end;
   190 
   191 
   192 (* Isabelle/Isar entities (with index) *)
   193 
   194 local
   195 
   196 fun no_check _ _ = true;
   197 
   198 fun is_keyword ctxt (name, _) =
   199   Keyword.is_keyword (Thy_Header.get_keywords' ctxt) name;
   200 
   201 fun check_command ctxt (name, pos) =
   202   let
   203     val thy = Proof_Context.theory_of ctxt;
   204     val keywords = Thy_Header.get_keywords thy;
   205   in
   206     Keyword.is_command keywords name andalso
   207       let
   208         val markup =
   209           Token.explode keywords Position.none name
   210           |> maps (Outer_Syntax.command_reports thy)
   211           |> map (snd o fst);
   212         val _ = Context_Position.reports ctxt (map (pair pos) markup);
   213       in true end
   214   end;
   215 
   216 fun check_system_option ctxt (name, pos) =
   217   (Context_Position.report ctxt pos (Options.default_markup (name, pos)); true)
   218     handle ERROR _ => false;
   219 
   220 fun check_tool ctxt (name, pos) =
   221   let
   222     fun tool dir =
   223       let val path = Path.append dir (Path.basic name)
   224       in if File.exists path then SOME path else NONE end;
   225   in
   226     (case get_first tool (Path.split (getenv "ISABELLE_TOOLS")) of
   227       NONE => false
   228     | SOME path => (Context_Position.report ctxt pos (Markup.path (Path.implode path)); true))
   229   end;
   230 
   231 val arg = enclose "{" "}" o clean_string;
   232 
   233 fun entity check markup binding index =
   234   Thy_Output.antiquotation
   235     (binding |> Binding.map_name (fn name => name ^
   236       (case index of NONE => "" | SOME true => "_def" | SOME false => "_ref")))
   237     (Scan.lift (Scan.optional (Args.parens Args.name) "" -- Parse.position Args.name))
   238     (fn {context = ctxt, ...} => fn (logic, (name, pos)) =>
   239       let
   240         val kind = translate (fn "_" => " " | c => c) (Binding.name_of binding);
   241         val hyper_name =
   242           "{" ^ Long_Name.append kind (Long_Name.append logic (clean_name name)) ^ "}";
   243         val hyper =
   244           enclose ("\\hyperlink" ^ hyper_name ^ "{") "}" #>
   245           index = SOME true ? enclose ("\\hypertarget" ^ hyper_name ^ "{") "}";
   246         val idx =
   247           (case index of
   248             NONE => ""
   249           | SOME is_def =>
   250               "\\index" ^ (if is_def then "def" else "ref") ^ arg logic ^ arg kind ^ arg name);
   251       in
   252         if check ctxt (name, pos) then
   253           idx ^
   254           (Output.output name
   255             |> (if markup = "" then I else enclose ("\\" ^ markup ^ "{") "}")
   256             |> (if Config.get ctxt Thy_Output.quotes then quote else I)
   257             |> (if Config.get ctxt Thy_Output.display
   258                 then enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
   259                 else hyper o enclose "\\mbox{\\isa{" "}}"))
   260         else error ("Bad " ^ kind ^ " " ^ quote name ^ Position.here pos)
   261       end);
   262 
   263 fun entity_antiqs check markup kind =
   264   entity check markup kind NONE #>
   265   entity check markup kind (SOME true) #>
   266   entity check markup kind (SOME false);
   267 
   268 in
   269 
   270 val _ =
   271   Theory.setup
   272    (entity_antiqs no_check "" @{binding syntax} #>
   273     entity_antiqs check_command "isacommand" @{binding command} #>
   274     entity_antiqs is_keyword "isakeyword" @{binding keyword} #>
   275     entity_antiqs is_keyword "isakeyword" @{binding element} #>
   276     entity_antiqs (can o Method.check_name) "" @{binding method} #>
   277     entity_antiqs (can o Attrib.check_name) "" @{binding attribute} #>
   278     entity_antiqs no_check "" @{binding fact} #>
   279     entity_antiqs no_check "" @{binding variable} #>
   280     entity_antiqs no_check "" @{binding case} #>
   281     entity_antiqs (can o Thy_Output.check_command) "" @{binding antiquotation} #>
   282     entity_antiqs (can o Thy_Output.check_option) "" @{binding antiquotation_option} #>
   283     entity_antiqs no_check "isasystem" @{binding setting} #>
   284     entity_antiqs check_system_option "isasystem" @{binding system_option} #>
   285     entity_antiqs no_check "" @{binding inference} #>
   286     entity_antiqs no_check "isasystem" @{binding executable} #>
   287     entity_antiqs check_tool "isatool" @{binding tool} #>
   288     entity_antiqs (can o ML_Context.check_antiquotation) "" @{binding ML_antiquotation} #>
   289     entity_antiqs (K (is_action o #1)) "isasystem" @{binding action});
   290 
   291 end;
   292 
   293 end;