src/Doc/antiquote_setup.ML
author wenzelm
Sun Mar 02 00:05:35 2014 +0100 (2014-03-02)
changeset 55831 3a9386b32211
parent 55828 42ac3cfb89f6
child 55837 154855d9a564
permissions -rw-r--r--
more markup for ML source;
     1 (*  Title:      Doc/antiquote_setup.ML
     2     Author:     Makarius
     3 
     4 Auxiliary antiquotations for the Isabelle manuals.
     5 *)
     6 
     7 signature ANTIQUOTE_SETUP =
     8 sig
     9   val setup: theory -> theory
    10 end;
    11 
    12 structure Antiquote_Setup: ANTIQUOTE_SETUP =
    13 struct
    14 
    15 (* misc utils *)
    16 
    17 fun translate f = Symbol.explode #> map f #> implode;
    18 
    19 val clean_string = translate
    20   (fn "_" => "\\_"
    21     | "#" => "\\#"
    22     | "$" => "\\$"
    23     | "%" => "\\%"
    24     | "<" => "$<$"
    25     | ">" => "$>$"
    26     | "{" => "\\{"
    27     | "|" => "$\\mid$"
    28     | "}" => "\\}"
    29     | "\<hyphen>" => "-"
    30     | c => c);
    31 
    32 fun clean_name "\<dots>" = "dots"
    33   | clean_name ".." = "ddot"
    34   | clean_name "." = "dot"
    35   | clean_name "_" = "underscore"
    36   | clean_name "{" = "braceleft"
    37   | clean_name "}" = "braceright"
    38   | clean_name s = s |> translate (fn "_" => "-" | "\<hyphen>" => "-" | c => c);
    39 
    40 
    41 (* verbatim text *)
    42 
    43 val verbatim = space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|";
    44 
    45 val verbatim_setup =
    46   Thy_Output.antiquotation @{binding verbatim} (Scan.lift Args.name)
    47     (K (split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"));
    48 
    49 
    50 (* ML text *)
    51 
    52 local
    53 
    54 val ml_toks = ML_Lex.read Position.none;
    55 
    56 fun ml_val (toks1, []) = ml_toks "fn _ => (" @ toks1 @ ml_toks ");"
    57   | ml_val (toks1, toks2) = ml_toks "fn _ => (" @ toks1 @ ml_toks " : " @ toks2 @ ml_toks ");";
    58 
    59 fun ml_op (toks1, []) = ml_toks "fn _ => (op " @ toks1 @ ml_toks ");"
    60   | ml_op (toks1, toks2) = ml_toks "fn _ => (op " @ toks1 @ ml_toks " : " @ toks2 @ ml_toks ");";
    61 
    62 fun ml_type (toks1, []) = ml_toks "val _ = NONE : (" @ toks1 @ ml_toks ") option;"
    63   | ml_type (toks1, toks2) =
    64       ml_toks "val _ = [NONE : (" @ toks1 @ ml_toks ") option, NONE : (" @
    65         toks2 @ ml_toks ") option];";
    66 
    67 fun ml_exn (toks1, []) = ml_toks "fn _ => (" @ toks1 @ ml_toks " : exn);"
    68   | ml_exn (toks1, toks2) =
    69       ml_toks "fn _ => (" @ toks1 @ ml_toks " : " @ toks2 @ ml_toks " -> exn);";
    70 
    71 fun ml_structure (toks, _) =
    72   ml_toks "functor XXX() = struct structure XX = " @ toks @ ml_toks " end;";
    73 
    74 fun ml_functor (Antiquote.Text tok :: _, _) =
    75       ml_toks "ML_Env.check_functor " @ ml_toks (ML_Syntax.print_string (ML_Lex.content_of tok))
    76   | ml_functor _ = raise Fail "Bad ML functor specification";
    77 
    78 val is_name = ML_Lex.kind_of #> (fn kind => kind = ML_Lex.Ident orelse kind = ML_Lex.LongIdent);
    79 
    80 fun ml_name txt =
    81   (case filter is_name (ML_Lex.tokenize txt) of
    82     toks as [_] => ML_Lex.flatten toks
    83   | _ => error ("Single ML name expected in input: " ^ quote txt));
    84 
    85 fun prep_ml source =
    86   (#1 (Symbol_Pos.source_content source), ML_Lex.read_source source);
    87 
    88 fun index_ml name kind ml = Thy_Output.antiquotation name
    89   (Scan.lift (Args.name_source_position -- Scan.option (Args.colon |-- Args.name_source_position)))
    90   (fn {context = ctxt, ...} => fn (source1, opt_source2) =>
    91     let
    92       val (txt1, toks1) = prep_ml source1;
    93       val (txt2, toks2) =
    94         (case opt_source2 of
    95           SOME source => prep_ml source
    96         | NONE => ("", []));
    97 
    98       val txt =
    99         if txt2 = "" then txt1
   100         else if kind = "type" then txt1 ^ " = " ^ txt2
   101         else if kind = "exception" then txt1 ^ " of " ^ txt2
   102         else if Symbol_Pos.is_identifier (Long_Name.base_name (ml_name txt1))
   103         then txt1 ^ ": " ^ txt2
   104         else txt1 ^ " : " ^ txt2;
   105       val txt' = if kind = "" then txt else kind ^ " " ^ txt;
   106 
   107       val _ = ML_Context.eval_in (SOME ctxt) false (#pos source1) (ml (toks1, toks2));
   108       val kind' = if kind = "" then "ML" else "ML " ^ kind;
   109     in
   110       "\\indexdef{}{" ^ kind' ^ "}{" ^ clean_string (ml_name txt1) ^ "}" ^
   111       (txt'
   112       |> (if Config.get ctxt Thy_Output.quotes then quote else I)
   113       |> (if Config.get ctxt Thy_Output.display
   114           then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
   115           else split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))
   116     end);
   117 
   118 in
   119 
   120 val index_ml_setup =
   121   index_ml @{binding index_ML} "" ml_val #>
   122   index_ml @{binding index_ML_op} "infix" ml_op #>
   123   index_ml @{binding index_ML_type} "type" ml_type #>
   124   index_ml @{binding index_ML_exn} "exception" ml_exn #>
   125   index_ml @{binding index_ML_struct} "structure" ml_structure #>
   126   index_ml @{binding index_ML_functor} "functor" ml_functor;
   127 
   128 end;
   129 
   130 
   131 (* named theorems *)
   132 
   133 val named_thms_setup =
   134   Thy_Output.antiquotation @{binding named_thms}
   135     (Scan.repeat (Attrib.thm -- Scan.lift (Args.parens Args.name)))
   136     (fn {context = ctxt, ...} =>
   137       map (apfst (Thy_Output.pretty_thm ctxt))
   138       #> (if Config.get ctxt Thy_Output.quotes then map (apfst Pretty.quote) else I)
   139       #> (if Config.get ctxt Thy_Output.display
   140           then
   141             map (fn (p, name) =>
   142               Output.output (Pretty.string_of (Pretty.indent (Config.get ctxt Thy_Output.indent) p)) ^
   143               "\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}")
   144             #> space_implode "\\par\\smallskip%\n"
   145             #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
   146           else
   147             map (fn (p, name) =>
   148               Output.output (Pretty.str_of p) ^
   149               "\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}")
   150             #> space_implode "\\par\\smallskip%\n"
   151             #> enclose "\\isa{" "}"));
   152 
   153 
   154 (* theory file *)
   155 
   156 val thy_file_setup =
   157   Thy_Output.antiquotation @{binding thy_file} (Scan.lift Args.name)
   158     (fn {context = ctxt, ...} =>
   159       fn name => (Thy_Load.check_thy Path.current name; Thy_Output.output ctxt [Pretty.str name]));
   160 
   161 
   162 (* Isabelle/Isar entities (with index) *)
   163 
   164 local
   165 
   166 fun no_check _ _ = true;
   167 
   168 fun command_check (name, pos) =
   169   is_some (Keyword.command_keyword name) andalso
   170     let
   171       val markup =
   172         Outer_Syntax.scan Position.none name
   173         |> maps (Outer_Syntax.command_reports (#2 (Outer_Syntax.get_syntax ())))
   174         |> map (snd o fst);
   175       val _ = List.app (Position.report pos) markup;
   176     in true end;
   177 
   178 fun check_tool (name, pos) =
   179   let
   180     fun tool dir =
   181       let val path = Path.append dir (Path.basic name)
   182       in if File.exists path then SOME path else NONE end;
   183   in
   184     (case get_first tool (Path.split (getenv "ISABELLE_TOOLS")) of
   185       NONE => false
   186     | SOME path => (Position.report pos (Markup.path (Path.implode path)); true))
   187   end;
   188 
   189 val arg = enclose "{" "}" o clean_string;
   190 
   191 fun entity check markup kind index =
   192   Thy_Output.antiquotation
   193     (Binding.name (translate (fn " " => "_" | c => c) kind ^
   194       (case index of NONE => "" | SOME true => "_def" | SOME false => "_ref")))
   195     (Scan.lift (Scan.optional (Args.parens Args.name) "" -- Parse.position Args.name))
   196     (fn {context = ctxt, ...} => fn (logic, (name, pos)) =>
   197       let
   198         val hyper_name =
   199           "{" ^ Long_Name.append kind (Long_Name.append logic (clean_name name)) ^ "}";
   200         val hyper =
   201           enclose ("\\hyperlink" ^ hyper_name ^ "{") "}" #>
   202           index = SOME true ? enclose ("\\hypertarget" ^ hyper_name ^ "{") "}";
   203         val idx =
   204           (case index of
   205             NONE => ""
   206           | SOME is_def =>
   207               "\\index" ^ (if is_def then "def" else "ref") ^ arg logic ^ arg kind ^ arg name);
   208       in
   209         if check ctxt (name, pos) then
   210           idx ^
   211           (Output.output name
   212             |> (if markup = "" then I else enclose ("\\" ^ markup ^ "{") "}")
   213             |> (if Config.get ctxt Thy_Output.quotes then quote else I)
   214             |> (if Config.get ctxt Thy_Output.display
   215                 then enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
   216                 else hyper o enclose "\\mbox{\\isa{" "}}"))
   217         else error ("Bad " ^ kind ^ " " ^ quote name ^ Position.here pos)
   218       end);
   219 
   220 fun entity_antiqs check markup kind =
   221   entity check markup kind NONE #>
   222   entity check markup kind (SOME true) #>
   223   entity check markup kind (SOME false);
   224 
   225 in
   226 
   227 val entity_setup =
   228   entity_antiqs no_check "" "syntax" #>
   229   entity_antiqs (K command_check) "isacommand" "command" #>
   230   entity_antiqs (K (Keyword.is_keyword o #1)) "isakeyword" "keyword" #>
   231   entity_antiqs (K (Keyword.is_keyword o #1)) "isakeyword" "element" #>
   232   entity_antiqs (can o Method.check_name) "" "method" #>
   233   entity_antiqs (can o Attrib.check o Proof_Context.theory_of) "" "attribute" #>
   234   entity_antiqs no_check "" "fact" #>
   235   entity_antiqs no_check "" "variable" #>
   236   entity_antiqs no_check "" "case" #>
   237   entity_antiqs (can o Thy_Output.check_command) "" "antiquotation" #>
   238   entity_antiqs (can o Thy_Output.check_option) "" "antiquotation option" #>
   239   entity_antiqs no_check "isatt" "setting" #>
   240   entity_antiqs no_check "isatt" "system option" #>
   241   entity_antiqs no_check "" "inference" #>
   242   entity_antiqs no_check "isatt" "executable" #>
   243   entity_antiqs (K check_tool) "isatool" "tool" #>
   244   entity_antiqs (can o ML_Context.check_antiq) "" Markup.ML_antiquotationN;
   245 
   246 end;
   247 
   248 
   249 (* theory setup *)
   250 
   251 val setup =
   252   verbatim_setup #>
   253   index_ml_setup #>
   254   named_thms_setup #>
   255   thy_file_setup #>
   256   entity_setup;
   257 
   258 end;