650 val set_identifiers_cmd = gen_set_identifiers read_name_decls; |
650 val set_identifiers_cmd = gen_set_identifiers read_name_decls; |
651 |
651 |
652 |
652 |
653 (* custom printings *) |
653 (* custom printings *) |
654 |
654 |
655 fun arrange_printings prep_syms ctxt = |
655 datatype target_source = Literal of string | File of Path.T |
|
656 |
|
657 local |
|
658 |
|
659 val format_source = Pretty.block0 o Pretty.fbreaks o map Code_Printer.str o split_lines; |
|
660 |
|
661 fun eval_source (Literal s) = s |
|
662 | eval_source (File p) = File.read p; |
|
663 |
|
664 fun arrange_printings prep_syms prep_source ctxt = |
656 let |
665 let |
657 val thy = Proof_Context.theory_of ctxt; |
666 val thy = Proof_Context.theory_of ctxt; |
658 fun arrange check (sym, target_syns) = |
667 fun arrange check (sym, target_syns) = |
659 map (fn (target_name, some_syn) => |
668 map (fn (target_name, some_syn) => |
660 (assert_target thy target_name, (sym, Option.map (check ctxt target_name sym) some_syn))) target_syns; |
669 (assert_target thy target_name, (sym, Option.map (check ctxt target_name sym) some_syn))) target_syns; |
661 in |
670 in |
662 Code_Symbol.maps_attr' |
671 Code_Symbol.maps_attr' |
663 (arrange check_const_syntax) (arrange check_tyco_syntax) |
672 (arrange check_const_syntax) (arrange check_tyco_syntax) |
664 (arrange ((K o K o K) I)) (arrange ((K o K o K) I)) (arrange ((K o K o K) I)) |
673 (arrange ((K o K o K) I)) (arrange ((K o K o K) I)) (arrange ((K o K o K) I)) |
665 (arrange (fn ctxt => fn _ => fn _ => fn (raw_content, raw_syms) => |
674 (arrange (fn ctxt => fn _ => fn _ => fn (source, raw_syms) => |
666 (Pretty.block0 (Pretty.fbreaks (map Code_Printer.str (split_lines raw_content))), |
675 (format_source (prep_source source), map (prep_syms ctxt) raw_syms))) |
667 map (prep_syms ctxt) raw_syms))) |
676 end; |
668 end; |
677 |
669 |
678 fun cert_printings ctxt = cert_syms' ctxt #> arrange_printings cert_syms I ctxt; |
670 fun cert_printings ctxt = cert_syms' ctxt #> arrange_printings cert_syms ctxt; |
679 |
671 |
680 fun read_printings ctxt = read_syms' ctxt #> arrange_printings read_syms eval_source ctxt; |
672 fun read_printings ctxt = read_syms' ctxt #> arrange_printings read_syms ctxt; |
|
673 |
681 |
674 fun set_printing (target_name, sym_syn) = map_printings target_name (Code_Symbol.set_data sym_syn); |
682 fun set_printing (target_name, sym_syn) = map_printings target_name (Code_Symbol.set_data sym_syn); |
675 |
683 |
676 fun gen_set_printings prep_print_decl raw_print_decls thy = |
684 fun gen_set_printings prep_print_decl raw_print_decls thy = |
677 fold set_printing (prep_print_decl (Proof_Context.init_global thy) raw_print_decls) thy; |
685 fold set_printing (prep_print_decl (Proof_Context.init_global thy) raw_print_decls) thy; |
678 |
686 |
|
687 in |
|
688 |
679 val set_printings = gen_set_printings cert_printings; |
689 val set_printings = gen_set_printings cert_printings; |
680 val set_printings_cmd = gen_set_printings read_printings; |
690 val set_printings_cmd = gen_set_printings read_printings; |
|
691 |
|
692 end; |
681 |
693 |
682 |
694 |
683 (* concrete syntax *) |
695 (* concrete syntax *) |
684 |
696 |
685 fun parse_args f args = |
697 fun parse_args f args = |
729 |
741 |
730 fun parse_symbol_pragmas parse_const parse_tyco parse_class parse_classrel parse_inst parse_module = |
742 fun parse_symbol_pragmas parse_const parse_tyco parse_class parse_classrel parse_inst parse_module = |
731 Parse.enum1 "|" (Parse.group (fn () => "code symbol pragma") |
743 Parse.enum1 "|" (Parse.group (fn () => "code symbol pragma") |
732 (parse_symbol_pragma parse_const parse_tyco parse_class parse_classrel parse_inst parse_module)); |
744 (parse_symbol_pragma parse_const parse_tyco parse_class parse_classrel parse_inst parse_module)); |
733 |
745 |
|
746 val parse_target_source = Code_Printer.parse_target_source >> Literal |
|
747 || \<^keyword>\<open>file\<close> |-- Parse.path >> (File o Path.explode); |
|
748 |
734 val code_expr_argsP = Scan.optional (\<^keyword>\<open>(\<close> |-- Parse.args --| \<^keyword>\<open>)\<close>) []; |
749 val code_expr_argsP = Scan.optional (\<^keyword>\<open>(\<close> |-- Parse.args --| \<^keyword>\<open>)\<close>) []; |
735 |
750 |
736 fun code_expr_inP (all_public, raw_cs) = |
751 fun code_expr_inP (all_public, raw_cs) = |
737 Scan.repeat (\<^keyword>\<open>in\<close> |-- Parse.!!! (Parse.name |
752 Scan.repeat (\<^keyword>\<open>in\<close> |-- Parse.!!! (Parse.name |
738 -- Scan.optional (\<^keyword>\<open>module_name\<close> |-- Parse.name) "" |
753 -- Scan.optional (\<^keyword>\<open>module_name\<close> |-- Parse.name) "" |
761 |
776 |
762 val _ = |
777 val _ = |
763 Outer_Syntax.command \<^command_keyword>\<open>code_printing\<close> "declare dedicated printing for code symbols" |
778 Outer_Syntax.command \<^command_keyword>\<open>code_printing\<close> "declare dedicated printing for code symbols" |
764 (parse_symbol_pragmas (Code_Printer.parse_const_syntax) (Code_Printer.parse_tyco_syntax) |
779 (parse_symbol_pragmas (Code_Printer.parse_const_syntax) (Code_Printer.parse_tyco_syntax) |
765 Code_Printer.parse_target_source (Parse.minus >> K ()) (Parse.minus >> K ()) |
780 Code_Printer.parse_target_source (Parse.minus >> K ()) (Parse.minus >> K ()) |
766 (Code_Printer.parse_target_source -- Scan.optional (\<^keyword>\<open>for\<close> |-- parse_simple_symbols) []) |
781 (parse_target_source -- Scan.optional (\<^keyword>\<open>for\<close> |-- parse_simple_symbols) []) |
767 >> (Toplevel.theory o fold set_printings_cmd)); |
782 >> (Toplevel.theory o fold set_printings_cmd)); |
768 |
783 |
769 val _ = |
784 val _ = |
770 Outer_Syntax.local_theory \<^command_keyword>\<open>export_code\<close> "generate executable code for constants" |
785 Outer_Syntax.local_theory \<^command_keyword>\<open>export_code\<close> "generate executable code for constants" |
771 (Scan.optional (\<^keyword>\<open>open\<close> >> K true) false -- Scan.repeat1 Parse.term |
786 (Scan.optional (\<^keyword>\<open>open\<close> >> K true) false -- Scan.repeat1 Parse.term |