src/Pure/Thy/document_antiquotations.ML
changeset 73769 08db0a06e131
parent 73768 c73c22c62d08
child 73780 466fae6bf22e
equal deleted inserted replaced
73768:c73c22c62d08 73769:08db0a06e131
   325 fun test_functor (Antiquote.Text tok :: _, _) =
   325 fun test_functor (Antiquote.Text tok :: _, _) =
   326       ML_Lex.read "ML_Env.check_functor " @
   326       ML_Lex.read "ML_Env.check_functor " @
   327       ML_Lex.read (ML_Syntax.print_string (ML_Lex.content_of tok))
   327       ML_Lex.read (ML_Syntax.print_string (ML_Lex.content_of tok))
   328   | test_functor _ = raise Fail "Bad ML functor specification";
   328   | test_functor _ = raise Fail "Bad ML functor specification";
   329 
   329 
   330 val parse_ml0 = Args.text_input >> rpair Input.empty;
   330 val parse_ml0 = Args.text_input >> (fn source => ("", (source, Input.empty)));
   331 val parse_ml = Args.text_input -- Scan.optional (Args.colon |-- Args.text_input) Input.empty;
   331 
   332 val parse_type = Args.text_input -- Scan.optional (Args.$$$ "=" |-- Args.text_input) Input.empty;
   332 val parse_ml =
   333 val parse_exn = Args.text_input -- Scan.optional (Args.$$$ "of" |-- Args.text_input) Input.empty;
   333   Args.text_input -- Scan.optional (Args.colon |-- Args.text_input) Input.empty >> pair "";
       
   334 
       
   335 val parse_exn =
       
   336   Args.text_input -- Scan.optional (Args.$$$ "of" |-- Args.text_input) Input.empty >> pair "";
       
   337 
       
   338 val parse_type =
       
   339   (Parse.type_args >> (fn [] => "" | [a] => a ^ " " | bs => enclose "(" ") " (commas bs))) --
       
   340     (Args.text_input -- Scan.optional (Args.$$$ "=" |-- Args.text_input) Input.empty);
   334 
   341 
   335 fun eval ctxt pos ml =
   342 fun eval ctxt pos ml =
   336   ML_Context.eval_in (SOME ctxt) ML_Compiler.flags pos ml
   343   ML_Context.eval_in (SOME ctxt) ML_Compiler.flags pos ml
   337     handle ERROR msg => error (msg ^ Position.here pos);
   344     handle ERROR msg => error (msg ^ Position.here pos);
   338 
   345 
   349       else txt1 ^ " " ^ sep ^ " " ^ txt2
   356       else txt1 ^ " " ^ sep ^ " " ^ txt2
   350   in (txt, txt1) end;
   357   in (txt, txt1) end;
   351 
   358 
   352 fun antiquotation_ml parse test kind show_kind binding index =
   359 fun antiquotation_ml parse test kind show_kind binding index =
   353   Document_Output.antiquotation_raw binding (Scan.lift parse)
   360   Document_Output.antiquotation_raw binding (Scan.lift parse)
   354     (fn ctxt => fn sources =>
   361     (fn ctxt => fn (txt0, sources) =>
   355       let
   362       let
   356         val _ = apply2 ML_Lex.read_source sources |> test |> eval ctxt (Input.pos_of (#1 sources));
   363         val (ml1, ml2) = apply2 ML_Lex.read_source sources;
       
   364         val ml0 = ML_Lex.read_source (Input.string txt0);
       
   365         val _ = test (ml0 @ ml1, ml2) |> eval ctxt (Input.pos_of (#1 sources));
   357 
   366 
   358         val sep = if kind = "type" then "=" else if kind = "exception" then "of" else ":";
   367         val sep = if kind = "type" then "=" else if kind = "exception" then "of" else ":";
   359         val (txt, idx) = make_text sep sources;
   368         val (txt, idx) = make_text sep sources;
   360 
   369 
   361         val main_text =
   370         val main_text =
   362           Document_Output.verbatim ctxt
   371           Document_Output.verbatim ctxt
   363             (if kind = "" orelse not show_kind then txt else kind ^ " " ^ txt);
   372             ((if kind = "" orelse not show_kind then "" else kind ^ " ") ^ txt0 ^ txt);
   364 
   373 
   365         val index_text =
   374         val index_text =
   366           index |> Option.map (fn def =>
   375           index |> Option.map (fn def =>
   367             let
   376             let
   368               val ctxt' = Config.put Document_Antiquotation.thy_output_display false ctxt;
   377               val ctxt' = Config.put Document_Antiquotation.thy_output_display false ctxt;