src/Doc/antiquote_setup.ML
author wenzelm
Sat Mar 01 22:46:31 2014 +0100 (2014-03-01)
changeset 55828 42ac3cfb89f6
parent 55743 225a060e7445
child 55831 3a9386b32211
permissions -rw-r--r--
clarified language markup: added "delimited" property;
type Symbol_Pos.source preserves information about delimited outer tokens (e.g string, cartouche);
observe Completion.Language_Context only for delimited languages, which is important to complete keywords after undelimited inner tokens, e.g. "lemma A pro";
     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 fun ml_val (txt1, "") = "fn _ => (" ^ txt1 ^ ");"
    55   | ml_val (txt1, txt2) = "fn _ => (" ^ txt1 ^ " : " ^ txt2 ^ ");";
    56 
    57 fun ml_op (txt1, "") = "fn _ => (op " ^ txt1 ^ ");"
    58   | ml_op (txt1, txt2) = "fn _ => (op " ^ txt1 ^ " : " ^ txt2 ^ ");";
    59 
    60 fun ml_type (txt1, "") = "val _ = NONE : (" ^ txt1 ^ ") option;"
    61   | ml_type (txt1, txt2) = "val _ = [NONE : (" ^ txt1 ^ ") option, NONE : (" ^ txt2 ^ ") option];";
    62 
    63 fun ml_exn (txt1, "") = "fn _ => (" ^ txt1 ^ " : exn);"
    64   | ml_exn (txt1, txt2) = "fn _ => (" ^ txt1 ^ " : " ^ txt2 ^ " -> exn);";
    65 
    66 fun ml_structure (txt, _) = "functor XXX() = struct structure XX = " ^ txt ^ " end;";
    67 
    68 fun ml_functor (txt, _) = "ML_Env.check_functor " ^ ML_Syntax.print_string txt;
    69 
    70 val is_name = ML_Lex.kind_of #> (fn kind => kind = ML_Lex.Ident orelse kind = ML_Lex.LongIdent);
    71 
    72 fun ml_name txt =
    73   (case filter is_name (ML_Lex.tokenize txt) of
    74     toks as [_] => ML_Lex.flatten toks
    75   | _ => error ("Single ML name expected in input: " ^ quote txt));
    76 
    77 fun index_ml name kind ml = Thy_Output.antiquotation name
    78   (Scan.lift (Args.name -- Scan.optional (Args.colon |-- Args.name) ""))
    79   (fn {context = ctxt, ...} => fn (txt1, txt2) =>
    80     let
    81       val txt =
    82         if txt2 = "" then txt1
    83         else if kind = "type" then txt1 ^ " = " ^ txt2
    84         else if kind = "exception" then txt1 ^ " of " ^ txt2
    85         else if Symbol_Pos.is_identifier (Long_Name.base_name (ml_name txt1))
    86         then txt1 ^ ": " ^ txt2
    87         else txt1 ^ " : " ^ txt2;
    88       val txt' = if kind = "" then txt else kind ^ " " ^ txt;
    89       val _ =  (* ML_Lex.read (!?) *)
    90         ML_Context.eval_source_in (SOME ctxt) false
    91           {delimited = false, text = ml (txt1, txt2), pos = Position.none};
    92       val kind' = if kind = "" then "ML" else "ML " ^ kind;
    93     in
    94       "\\indexdef{}{" ^ kind' ^ "}{" ^ clean_string (ml_name txt1) ^ "}" ^
    95       (txt'
    96       |> (if Config.get ctxt Thy_Output.quotes then quote else I)
    97       |> (if Config.get ctxt Thy_Output.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
    98           else split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))
    99     end);
   100 
   101 in
   102 
   103 val index_ml_setup =
   104   index_ml @{binding index_ML} "" ml_val #>
   105   index_ml @{binding index_ML_op} "infix" ml_op #>
   106   index_ml @{binding index_ML_type} "type" ml_type #>
   107   index_ml @{binding index_ML_exn} "exception" ml_exn #>
   108   index_ml @{binding index_ML_structure} "structure" ml_structure #>
   109   index_ml @{binding index_ML_functor} "functor" ml_functor;
   110 
   111 end;
   112 
   113 
   114 (* named theorems *)
   115 
   116 val named_thms_setup =
   117   Thy_Output.antiquotation @{binding named_thms}
   118     (Scan.repeat (Attrib.thm -- Scan.lift (Args.parens Args.name)))
   119     (fn {context = ctxt, ...} =>
   120       map (apfst (Thy_Output.pretty_thm ctxt))
   121       #> (if Config.get ctxt Thy_Output.quotes then map (apfst Pretty.quote) else I)
   122       #> (if Config.get ctxt Thy_Output.display
   123           then
   124             map (fn (p, name) =>
   125               Output.output (Pretty.string_of (Pretty.indent (Config.get ctxt Thy_Output.indent) p)) ^
   126               "\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}")
   127             #> space_implode "\\par\\smallskip%\n"
   128             #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
   129           else
   130             map (fn (p, name) =>
   131               Output.output (Pretty.str_of p) ^
   132               "\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}")
   133             #> space_implode "\\par\\smallskip%\n"
   134             #> enclose "\\isa{" "}"));
   135 
   136 
   137 (* theory file *)
   138 
   139 val thy_file_setup =
   140   Thy_Output.antiquotation @{binding thy_file} (Scan.lift Args.name)
   141     (fn {context = ctxt, ...} =>
   142       fn name => (Thy_Load.check_thy Path.current name; Thy_Output.output ctxt [Pretty.str name]));
   143 
   144 
   145 (* Isabelle/Isar entities (with index) *)
   146 
   147 local
   148 
   149 fun no_check _ _ = true;
   150 
   151 fun command_check (name, pos) =
   152   is_some (Keyword.command_keyword name) andalso
   153     let
   154       val markup =
   155         Outer_Syntax.scan Position.none name
   156         |> maps (Outer_Syntax.command_reports (#2 (Outer_Syntax.get_syntax ())))
   157         |> map (snd o fst);
   158       val _ = List.app (Position.report pos) markup;
   159     in true end;
   160 
   161 fun check_tool (name, pos) =
   162   let
   163     fun tool dir =
   164       let val path = Path.append dir (Path.basic name)
   165       in if File.exists path then SOME path else NONE end;
   166   in
   167     (case get_first tool (Path.split (getenv "ISABELLE_TOOLS")) of
   168       NONE => false
   169     | SOME path => (Position.report pos (Markup.path (Path.implode path)); true))
   170   end;
   171 
   172 val arg = enclose "{" "}" o clean_string;
   173 
   174 fun entity check markup kind index =
   175   Thy_Output.antiquotation
   176     (Binding.name (translate (fn " " => "_" | c => c) kind ^
   177       (case index of NONE => "" | SOME true => "_def" | SOME false => "_ref")))
   178     (Scan.lift (Scan.optional (Args.parens Args.name) "" -- Parse.position Args.name))
   179     (fn {context = ctxt, ...} => fn (logic, (name, pos)) =>
   180       let
   181         val hyper_name =
   182           "{" ^ Long_Name.append kind (Long_Name.append logic (clean_name name)) ^ "}";
   183         val hyper =
   184           enclose ("\\hyperlink" ^ hyper_name ^ "{") "}" #>
   185           index = SOME true ? enclose ("\\hypertarget" ^ hyper_name ^ "{") "}";
   186         val idx =
   187           (case index of
   188             NONE => ""
   189           | SOME is_def =>
   190               "\\index" ^ (if is_def then "def" else "ref") ^ arg logic ^ arg kind ^ arg name);
   191       in
   192         if check ctxt (name, pos) then
   193           idx ^
   194           (Output.output name
   195             |> (if markup = "" then I else enclose ("\\" ^ markup ^ "{") "}")
   196             |> (if Config.get ctxt Thy_Output.quotes then quote else I)
   197             |> (if Config.get ctxt Thy_Output.display
   198                 then enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
   199                 else hyper o enclose "\\mbox{\\isa{" "}}"))
   200         else error ("Bad " ^ kind ^ " " ^ quote name ^ Position.here pos)
   201       end);
   202 
   203 fun entity_antiqs check markup kind =
   204   entity check markup kind NONE #>
   205   entity check markup kind (SOME true) #>
   206   entity check markup kind (SOME false);
   207 
   208 in
   209 
   210 val entity_setup =
   211   entity_antiqs no_check "" "syntax" #>
   212   entity_antiqs (K command_check) "isacommand" "command" #>
   213   entity_antiqs (K (Keyword.is_keyword o #1)) "isakeyword" "keyword" #>
   214   entity_antiqs (K (Keyword.is_keyword o #1)) "isakeyword" "element" #>
   215   entity_antiqs (can o Method.check_name) "" "method" #>
   216   entity_antiqs (can o Attrib.check o Proof_Context.theory_of) "" "attribute" #>
   217   entity_antiqs no_check "" "fact" #>
   218   entity_antiqs no_check "" "variable" #>
   219   entity_antiqs no_check "" "case" #>
   220   entity_antiqs (can o Thy_Output.check_command) "" "antiquotation" #>
   221   entity_antiqs (can o Thy_Output.check_option) "" "antiquotation option" #>
   222   entity_antiqs no_check "isatt" "setting" #>
   223   entity_antiqs no_check "isatt" "system option" #>
   224   entity_antiqs no_check "" "inference" #>
   225   entity_antiqs no_check "isatt" "executable" #>
   226   entity_antiqs (K check_tool) "isatool" "tool" #>
   227   entity_antiqs (can o ML_Context.check_antiq) "" Markup.ML_antiquotationN;
   228 
   229 end;
   230 
   231 
   232 (* theory setup *)
   233 
   234 val setup =
   235   verbatim_setup #>
   236   index_ml_setup #>
   237   named_thms_setup #>
   238   thy_file_setup #>
   239   entity_setup;
   240 
   241 end;