wenzelm@30394: (* Title: doc-src/antiquote_setup.ML wenzelm@21375: Author: Makarius wenzelm@21375: wenzelm@26742: Auxiliary antiquotations for the Isabelle manuals. wenzelm@21375: *) wenzelm@21375: wenzelm@40801: structure Antiquote_Setup: sig end = wenzelm@26742: struct wenzelm@21375: wenzelm@26742: (* misc utils *) wenzelm@26742: wenzelm@29736: fun translate f = Symbol.explode #> map f #> implode; wenzelm@29736: wenzelm@29736: val clean_string = translate wenzelm@26853: (fn "_" => "\\_" wenzelm@30120: | "#" => "\\#" wenzelm@26897: | "<" => "$<$" wenzelm@26768: | ">" => "$>$" wenzelm@26768: | "{" => "\\{" wenzelm@30120: | "|" => "$\\mid$" wenzelm@26768: | "}" => "\\}" wenzelm@42666: | "\" => "-" wenzelm@26768: | c => c); wenzelm@26751: wenzelm@31546: fun clean_name "\" = "dots" wenzelm@26897: | clean_name ".." = "ddot" wenzelm@26897: | clean_name "." = "dot" wenzelm@26910: | clean_name "_" = "underscore" wenzelm@26897: | clean_name "{" = "braceleft" wenzelm@26897: | clean_name "}" = "braceright" wenzelm@42666: | clean_name s = s |> translate (fn "_" => "-" | "\" => "-" | c => c); wenzelm@26897: wenzelm@26742: wenzelm@26742: (* verbatim text *) wenzelm@26742: wenzelm@26742: val verbatim = space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|"; wenzelm@26742: wenzelm@37216: val _ = Thy_Output.antiquotation "verbatim" (Scan.lift Args.name) wenzelm@30382: (K (split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n")); wenzelm@26742: wenzelm@26742: wenzelm@26742: (* ML text *) wenzelm@26742: wenzelm@26742: local wenzelm@26742: wenzelm@21375: fun ml_val (txt1, "") = "fn _ => (" ^ txt1 ^ ");" wenzelm@39858: | ml_val (txt1, txt2) = "fn _ => (" ^ txt1 ^ " : " ^ txt2 ^ ");"; wenzelm@21375: wenzelm@21375: fun ml_type (txt1, "") = "val _ = NONE : (" ^ txt1 ^ ") option;" wenzelm@21375: | ml_type (txt1, txt2) = "val _ = [NONE : (" ^ txt1 ^ ") option, NONE : (" ^ txt2 ^ ") option];"; haftmann@22289: wenzelm@39858: fun ml_exn (txt1, "") = "fn _ => (" ^ txt1 ^ " : exn);" wenzelm@39858: | ml_exn (txt1, txt2) = "fn _ => (" ^ txt1 ^ " : " ^ txt2 ^ " -> exn);"; haftmann@22289: wenzelm@30394: fun ml_structure (txt, _) = "functor XXX() = struct structure XX = " ^ txt ^ " end;"; wenzelm@21375: wenzelm@36163: fun ml_functor (txt, _) = "ML_Env.check_functor " ^ ML_Syntax.print_string txt; wenzelm@21375: wenzelm@39869: val is_name = ML_Lex.kind_of #> (fn kind => kind = ML_Lex.Ident orelse kind = ML_Lex.LongIdent); wenzelm@39869: wenzelm@39869: fun ml_name txt = wenzelm@39869: (case filter is_name (ML_Lex.tokenize txt) of wenzelm@39869: toks as [_] => ML_Lex.flatten toks wenzelm@39869: | _ => error ("Single ML name expected in input: " ^ quote txt)); wenzelm@39869: wenzelm@37216: fun index_ml name kind ml = Thy_Output.antiquotation name wenzelm@30394: (Scan.lift (Args.name -- Scan.optional (Args.colon |-- Args.name) "")) wenzelm@30394: (fn {context = ctxt, ...} => fn (txt1, txt2) => wenzelm@30394: let wenzelm@30394: val txt = wenzelm@30394: if txt2 = "" then txt1 wenzelm@30394: else if kind = "type" then txt1 ^ " = " ^ txt2 wenzelm@30394: else if kind = "exception" then txt1 ^ " of " ^ txt2 wenzelm@42290: else if Lexicon.is_identifier (Long_Name.base_name (ml_name txt1)) wenzelm@42290: then txt1 ^ ": " ^ txt2 wenzelm@39858: else txt1 ^ " : " ^ txt2; wenzelm@30394: val txt' = if kind = "" then txt else kind ^ " " ^ txt; wenzelm@37198: val _ = ML_Context.eval_text_in (SOME ctxt) false Position.none (ml (txt1, txt2)); (* ML_Lex.read (!?) *) wenzelm@30394: val kind' = if kind = "" then "ML" else "ML " ^ kind; wenzelm@30394: in wenzelm@39869: "\\indexdef{}{" ^ kind' ^ "}{" ^ clean_string (ml_name txt1) ^ "}" ^ wenzelm@30394: (txt' wenzelm@38767: |> (if Config.get ctxt Thy_Output.quotes then quote else I) wenzelm@38767: |> (if Config.get ctxt Thy_Output.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}" wenzelm@30394: else split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n")) wenzelm@30394: end); wenzelm@26742: wenzelm@26742: in wenzelm@21375: wenzelm@30394: val _ = index_ml "index_ML" "" ml_val; wenzelm@30394: val _ = index_ml "index_ML_type" "type" ml_type; wenzelm@30394: val _ = index_ml "index_ML_exn" "exception" ml_exn; wenzelm@30394: val _ = index_ml "index_ML_structure" "structure" ml_structure; wenzelm@30394: val _ = index_ml "index_ML_functor" "functor" ml_functor; wenzelm@21375: wenzelm@26742: end; wenzelm@21375: berghofe@23846: wenzelm@30394: (* named theorems *) berghofe@23846: wenzelm@37216: val _ = Thy_Output.antiquotation "named_thms" wenzelm@30394: (Scan.repeat (Attrib.thm -- Scan.lift (Args.parens Args.name))) wenzelm@30394: (fn {context = ctxt, ...} => wenzelm@37216: map (apfst (Thy_Output.pretty_thm ctxt)) wenzelm@38767: #> (if Config.get ctxt Thy_Output.quotes then map (apfst Pretty.quote) else I) wenzelm@38767: #> (if Config.get ctxt Thy_Output.display wenzelm@30394: then wenzelm@30394: map (fn (p, name) => wenzelm@38767: Output.output (Pretty.string_of (Pretty.indent (Config.get ctxt Thy_Output.indent) p)) ^ wenzelm@38767: "\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}") wenzelm@30394: #> space_implode "\\par\\smallskip%\n" wenzelm@30394: #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}" wenzelm@30394: else wenzelm@30394: map (fn (p, name) => wenzelm@30394: Output.output (Pretty.str_of p) ^ wenzelm@38767: "\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}") wenzelm@30394: #> space_implode "\\par\\smallskip%\n" wenzelm@30394: #> enclose "\\isa{" "}")); wenzelm@26742: wenzelm@26742: wenzelm@30394: (* theory file *) wenzelm@26742: wenzelm@37216: val _ = Thy_Output.antiquotation "thy_file" (Scan.lift Args.name) wenzelm@38767: (fn {context = ctxt, ...} => wenzelm@38767: fn name => (Thy_Load.check_thy Path.current name; Thy_Output.output ctxt [Pretty.str name])); wenzelm@26742: wenzelm@26751: wenzelm@30394: (* Isabelle/Isar entities (with index) *) wenzelm@26751: wenzelm@26751: local wenzelm@26751: wenzelm@26894: fun no_check _ _ = true; wenzelm@26894: wenzelm@26894: fun thy_check intern defined ctxt = wenzelm@42361: let val thy = Proof_Context.theory_of ctxt wenzelm@26894: in defined thy o intern thy end; wenzelm@26894: wenzelm@28237: fun check_tool name = wenzelm@28237: File.exists (Path.append (Path.explode "~~/lib/Tools") (Path.basic name)); wenzelm@28237: wenzelm@26751: val arg = enclose "{" "}" o clean_string; wenzelm@26751: wenzelm@30394: fun entity check markup kind index = wenzelm@37216: Thy_Output.antiquotation wenzelm@30396: (translate (fn " " => "_" | c => c) kind ^ wenzelm@30396: (case index of NONE => "" | SOME true => "_def" | SOME false => "_ref")) wenzelm@30394: (Scan.lift (Scan.optional (Args.parens Args.name) "" -- Args.name)) wenzelm@30394: (fn {context = ctxt, ...} => fn (logic, name) => wenzelm@30394: let wenzelm@30394: val hyper_name = wenzelm@30394: "{" ^ Long_Name.append kind (Long_Name.append logic (clean_name name)) ^ "}"; wenzelm@30394: val hyper = wenzelm@30394: enclose ("\\hyperlink" ^ hyper_name ^ "{") "}" #> wenzelm@30394: index = SOME true ? enclose ("\\hypertarget" ^ hyper_name ^ "{") "}"; wenzelm@30394: val idx = wenzelm@30394: (case index of wenzelm@30394: NONE => "" wenzelm@30394: | SOME is_def => wenzelm@30394: "\\index" ^ (if is_def then "def" else "ref") ^ arg logic ^ arg kind ^ arg name); wenzelm@30394: in wenzelm@30394: if check ctxt name then wenzelm@30394: idx ^ wenzelm@30394: (Output.output name wenzelm@30394: |> (if markup = "" then I else enclose ("\\" ^ markup ^ "{") "}") wenzelm@38767: |> (if Config.get ctxt Thy_Output.quotes then quote else I) wenzelm@38767: |> (if Config.get ctxt Thy_Output.display wenzelm@38767: then enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}" wenzelm@30394: else hyper o enclose "\\mbox{\\isa{" "}}")) wenzelm@30394: else error ("Bad " ^ kind ^ " " ^ quote name) wenzelm@30394: end); wenzelm@26897: wenzelm@26894: fun entity_antiqs check markup kind = wenzelm@31297: ((entity check markup kind NONE); wenzelm@31297: (entity check markup kind (SOME true)); wenzelm@31297: (entity check markup kind (SOME false))); wenzelm@26751: wenzelm@26751: in wenzelm@26751: wenzelm@30394: val _ = entity_antiqs no_check "" "syntax"; wenzelm@36973: val _ = entity_antiqs (K (is_some o Keyword.command_keyword)) "isacommand" "command"; wenzelm@36973: val _ = entity_antiqs (K Keyword.is_keyword) "isakeyword" "keyword"; wenzelm@36973: val _ = entity_antiqs (K Keyword.is_keyword) "isakeyword" "element"; wenzelm@30394: val _ = entity_antiqs (thy_check Method.intern Method.defined) "" "method"; wenzelm@30394: val _ = entity_antiqs (thy_check Attrib.intern Attrib.defined) "" "attribute"; wenzelm@30394: val _ = entity_antiqs no_check "" "fact"; wenzelm@30394: val _ = entity_antiqs no_check "" "variable"; wenzelm@30394: val _ = entity_antiqs no_check "" "case"; wenzelm@37216: val _ = entity_antiqs (K Thy_Output.defined_command) "" "antiquotation"; wenzelm@37216: val _ = entity_antiqs (K Thy_Output.defined_option) "" "antiquotation option"; wenzelm@30394: val _ = entity_antiqs (fn _ => fn name => is_some (OS.Process.getEnv name)) "isatt" "setting"; wenzelm@30394: val _ = entity_antiqs no_check "" "inference"; wenzelm@30394: val _ = entity_antiqs no_check "isatt" "executable"; wenzelm@30394: val _ = entity_antiqs (K check_tool) "isatt" "tool"; wenzelm@37982: val _ = entity_antiqs (K (can Thy_Info.get_theory)) "" "theory"; wenzelm@39829: val _ = entity_antiqs no_check "" "ML antiquotation"; (* FIXME proper check *) wenzelm@26751: wenzelm@26742: end; wenzelm@26751: wenzelm@26751: end;