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