src/Doc/antiquote_setup.ML
changeset 48985 5386df44a037
parent 48899 92da8a8380da
child 50201 c26369c9eda6
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/Doc/antiquote_setup.ML	Tue Aug 28 18:57:32 2012 +0200
     1.3 @@ -0,0 +1,227 @@
     1.4 +(*  Title:      Doc/antiquote_setup.ML
     1.5 +    Author:     Makarius
     1.6 +
     1.7 +Auxiliary antiquotations for the Isabelle manuals.
     1.8 +*)
     1.9 +
    1.10 +signature ANTIQUOTE_SETUP =
    1.11 +sig
    1.12 +  val setup: theory -> theory
    1.13 +end;
    1.14 +
    1.15 +structure Antiquote_Setup: ANTIQUOTE_SETUP =
    1.16 +struct
    1.17 +
    1.18 +(* misc utils *)
    1.19 +
    1.20 +fun translate f = Symbol.explode #> map f #> implode;
    1.21 +
    1.22 +val clean_string = translate
    1.23 +  (fn "_" => "\\_"
    1.24 +    | "#" => "\\#"
    1.25 +    | "<" => "$<$"
    1.26 +    | ">" => "$>$"
    1.27 +    | "{" => "\\{"
    1.28 +    | "|" => "$\\mid$"
    1.29 +    | "}" => "\\}"
    1.30 +    | "\<hyphen>" => "-"
    1.31 +    | c => c);
    1.32 +
    1.33 +fun clean_name "\<dots>" = "dots"
    1.34 +  | clean_name ".." = "ddot"
    1.35 +  | clean_name "." = "dot"
    1.36 +  | clean_name "_" = "underscore"
    1.37 +  | clean_name "{" = "braceleft"
    1.38 +  | clean_name "}" = "braceright"
    1.39 +  | clean_name s = s |> translate (fn "_" => "-" | "\<hyphen>" => "-" | c => c);
    1.40 +
    1.41 +
    1.42 +(* verbatim text *)
    1.43 +
    1.44 +val verbatim = space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|";
    1.45 +
    1.46 +val verbatim_setup =
    1.47 +  Thy_Output.antiquotation @{binding verbatim} (Scan.lift Args.name)
    1.48 +    (K (split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"));
    1.49 +
    1.50 +
    1.51 +(* ML text *)
    1.52 +
    1.53 +local
    1.54 +
    1.55 +fun ml_val (txt1, "") = "fn _ => (" ^ txt1 ^ ");"
    1.56 +  | ml_val (txt1, txt2) = "fn _ => (" ^ txt1 ^ " : " ^ txt2 ^ ");";
    1.57 +
    1.58 +fun ml_op (txt1, "") = "fn _ => (op " ^ txt1 ^ ");"
    1.59 +  | ml_op (txt1, txt2) = "fn _ => (op " ^ txt1 ^ " : " ^ txt2 ^ ");";
    1.60 +
    1.61 +fun ml_type (txt1, "") = "val _ = NONE : (" ^ txt1 ^ ") option;"
    1.62 +  | ml_type (txt1, txt2) = "val _ = [NONE : (" ^ txt1 ^ ") option, NONE : (" ^ txt2 ^ ") option];";
    1.63 +
    1.64 +fun ml_exn (txt1, "") = "fn _ => (" ^ txt1 ^ " : exn);"
    1.65 +  | ml_exn (txt1, txt2) = "fn _ => (" ^ txt1 ^ " : " ^ txt2 ^ " -> exn);";
    1.66 +
    1.67 +fun ml_structure (txt, _) = "functor XXX() = struct structure XX = " ^ txt ^ " end;";
    1.68 +
    1.69 +fun ml_functor (txt, _) = "ML_Env.check_functor " ^ ML_Syntax.print_string txt;
    1.70 +
    1.71 +val is_name = ML_Lex.kind_of #> (fn kind => kind = ML_Lex.Ident orelse kind = ML_Lex.LongIdent);
    1.72 +
    1.73 +fun ml_name txt =
    1.74 +  (case filter is_name (ML_Lex.tokenize txt) of
    1.75 +    toks as [_] => ML_Lex.flatten toks
    1.76 +  | _ => error ("Single ML name expected in input: " ^ quote txt));
    1.77 +
    1.78 +fun index_ml name kind ml = Thy_Output.antiquotation name
    1.79 +  (Scan.lift (Args.name -- Scan.optional (Args.colon |-- Args.name) ""))
    1.80 +  (fn {context = ctxt, ...} => fn (txt1, txt2) =>
    1.81 +    let
    1.82 +      val txt =
    1.83 +        if txt2 = "" then txt1
    1.84 +        else if kind = "type" then txt1 ^ " = " ^ txt2
    1.85 +        else if kind = "exception" then txt1 ^ " of " ^ txt2
    1.86 +        else if Lexicon.is_identifier (Long_Name.base_name (ml_name txt1))
    1.87 +        then txt1 ^ ": " ^ txt2
    1.88 +        else txt1 ^ " : " ^ txt2;
    1.89 +      val txt' = if kind = "" then txt else kind ^ " " ^ txt;
    1.90 +      val _ = ML_Context.eval_text_in (SOME ctxt) false Position.none (ml (txt1, txt2));  (* ML_Lex.read (!?) *)
    1.91 +      val kind' = if kind = "" then "ML" else "ML " ^ kind;
    1.92 +    in
    1.93 +      "\\indexdef{}{" ^ kind' ^ "}{" ^ clean_string (ml_name txt1) ^ "}" ^
    1.94 +      (txt'
    1.95 +      |> (if Config.get ctxt Thy_Output.quotes then quote else I)
    1.96 +      |> (if Config.get ctxt Thy_Output.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
    1.97 +          else split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))
    1.98 +    end);
    1.99 +
   1.100 +in
   1.101 +
   1.102 +val index_ml_setup =
   1.103 +  index_ml @{binding index_ML} "" ml_val #>
   1.104 +  index_ml @{binding index_ML_op} "infix" ml_op #>
   1.105 +  index_ml @{binding index_ML_type} "type" ml_type #>
   1.106 +  index_ml @{binding index_ML_exn} "exception" ml_exn #>
   1.107 +  index_ml @{binding index_ML_structure} "structure" ml_structure #>
   1.108 +  index_ml @{binding index_ML_functor} "functor" ml_functor;
   1.109 +
   1.110 +end;
   1.111 +
   1.112 +
   1.113 +(* named theorems *)
   1.114 +
   1.115 +val named_thms_setup =
   1.116 +  Thy_Output.antiquotation @{binding named_thms}
   1.117 +    (Scan.repeat (Attrib.thm -- Scan.lift (Args.parens Args.name)))
   1.118 +    (fn {context = ctxt, ...} =>
   1.119 +      map (apfst (Thy_Output.pretty_thm ctxt))
   1.120 +      #> (if Config.get ctxt Thy_Output.quotes then map (apfst Pretty.quote) else I)
   1.121 +      #> (if Config.get ctxt Thy_Output.display
   1.122 +          then
   1.123 +            map (fn (p, name) =>
   1.124 +              Output.output (Pretty.string_of (Pretty.indent (Config.get ctxt Thy_Output.indent) p)) ^
   1.125 +              "\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}")
   1.126 +            #> space_implode "\\par\\smallskip%\n"
   1.127 +            #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
   1.128 +          else
   1.129 +            map (fn (p, name) =>
   1.130 +              Output.output (Pretty.str_of p) ^
   1.131 +              "\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}")
   1.132 +            #> space_implode "\\par\\smallskip%\n"
   1.133 +            #> enclose "\\isa{" "}"));
   1.134 +
   1.135 +
   1.136 +(* theory file *)
   1.137 +
   1.138 +val thy_file_setup =
   1.139 +  Thy_Output.antiquotation @{binding thy_file} (Scan.lift Args.name)
   1.140 +    (fn {context = ctxt, ...} =>
   1.141 +      fn name => (Thy_Load.check_thy Path.current name; Thy_Output.output ctxt [Pretty.str name]));
   1.142 +
   1.143 +
   1.144 +(* Isabelle/Isar entities (with index) *)
   1.145 +
   1.146 +local
   1.147 +
   1.148 +fun no_check _ _ = true;
   1.149 +
   1.150 +fun thy_check intern defined ctxt =
   1.151 +  let val thy = Proof_Context.theory_of ctxt
   1.152 +  in defined thy o intern thy end;
   1.153 +
   1.154 +fun check_tool name =
   1.155 +  let val tool_dirs = map Path.explode ["~~/lib/Tools", "~~/src/Tools/jEdit/lib/Tools"]
   1.156 +  in exists (fn dir => File.exists (Path.append dir (Path.basic name))) tool_dirs end;
   1.157 +
   1.158 +val arg = enclose "{" "}" o clean_string;
   1.159 +
   1.160 +fun entity check markup kind index =
   1.161 +  Thy_Output.antiquotation
   1.162 +    (Binding.name (translate (fn " " => "_" | c => c) kind ^
   1.163 +      (case index of NONE => "" | SOME true => "_def" | SOME false => "_ref")))
   1.164 +    (Scan.lift (Scan.optional (Args.parens Args.name) "" -- Args.name))
   1.165 +    (fn {context = ctxt, ...} => fn (logic, name) =>
   1.166 +      let
   1.167 +        val hyper_name =
   1.168 +          "{" ^ Long_Name.append kind (Long_Name.append logic (clean_name name)) ^ "}";
   1.169 +        val hyper =
   1.170 +          enclose ("\\hyperlink" ^ hyper_name ^ "{") "}" #>
   1.171 +          index = SOME true ? enclose ("\\hypertarget" ^ hyper_name ^ "{") "}";
   1.172 +        val idx =
   1.173 +          (case index of
   1.174 +            NONE => ""
   1.175 +          | SOME is_def =>
   1.176 +              "\\index" ^ (if is_def then "def" else "ref") ^ arg logic ^ arg kind ^ arg name);
   1.177 +      in
   1.178 +        if check ctxt name then
   1.179 +          idx ^
   1.180 +          (Output.output name
   1.181 +            |> (if markup = "" then I else enclose ("\\" ^ markup ^ "{") "}")
   1.182 +            |> (if Config.get ctxt Thy_Output.quotes then quote else I)
   1.183 +            |> (if Config.get ctxt Thy_Output.display
   1.184 +                then enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
   1.185 +                else hyper o enclose "\\mbox{\\isa{" "}}"))
   1.186 +        else error ("Bad " ^ kind ^ " " ^ quote name)
   1.187 +      end);
   1.188 +
   1.189 +fun entity_antiqs check markup kind =
   1.190 +  entity check markup kind NONE #>
   1.191 +  entity check markup kind (SOME true) #>
   1.192 +  entity check markup kind (SOME false);
   1.193 +
   1.194 +in
   1.195 +
   1.196 +val entity_setup =
   1.197 +  entity_antiqs no_check "" "syntax" #>
   1.198 +  entity_antiqs (K (is_some o Keyword.command_keyword)) "isacommand" "command" #>
   1.199 +  entity_antiqs (K Keyword.is_keyword) "isakeyword" "keyword" #>
   1.200 +  entity_antiqs (K Keyword.is_keyword) "isakeyword" "element" #>
   1.201 +  entity_antiqs (thy_check Method.intern Method.defined) "" "method" #>
   1.202 +  entity_antiqs (thy_check Attrib.intern Attrib.defined) "" "attribute" #>
   1.203 +  entity_antiqs no_check "" "fact" #>
   1.204 +  entity_antiqs no_check "" "variable" #>
   1.205 +  entity_antiqs no_check "" "case" #>
   1.206 +  entity_antiqs (thy_check Thy_Output.intern_command Thy_Output.defined_command)
   1.207 +    "" "antiquotation" #>
   1.208 +  entity_antiqs (thy_check Thy_Output.intern_option Thy_Output.defined_option)
   1.209 +    "" "antiquotation option" #>
   1.210 +  entity_antiqs no_check "isatt" "setting" #>
   1.211 +  entity_antiqs no_check "isatt" "system option" #>
   1.212 +  entity_antiqs no_check "" "inference" #>
   1.213 +  entity_antiqs no_check "isatt" "executable" #>
   1.214 +  entity_antiqs (K check_tool) "isatool" "tool" #>
   1.215 +  entity_antiqs (thy_check ML_Context.intern_antiq ML_Context.defined_antiq)
   1.216 +    "" Isabelle_Markup.ML_antiquotationN;
   1.217 +
   1.218 +end;
   1.219 +
   1.220 +
   1.221 +(* theory setup *)
   1.222 +
   1.223 +val setup =
   1.224 +  verbatim_setup #>
   1.225 +  index_ml_setup #>
   1.226 +  named_thms_setup #>
   1.227 +  thy_file_setup #>
   1.228 +  entity_setup;
   1.229 +
   1.230 +end;