src/Tools/Code/code_printer.ML
changeset 39034 ebeb48fd653b
parent 38923 79d7f2b4cf71
child 39055 81e0368812ad
equal deleted inserted replaced
39033:e8b68ec3bb9c 39034:ebeb48fd653b
    23   val enum: string -> string -> string -> Pretty.T list -> Pretty.T
    23   val enum: string -> string -> string -> Pretty.T list -> Pretty.T
    24   val enum_default: string -> string -> string -> string -> Pretty.T list -> Pretty.T
    24   val enum_default: string -> string -> string -> string -> Pretty.T list -> Pretty.T
    25   val semicolon: Pretty.T list -> Pretty.T
    25   val semicolon: Pretty.T list -> Pretty.T
    26   val doublesemicolon: Pretty.T list -> Pretty.T
    26   val doublesemicolon: Pretty.T list -> Pretty.T
    27   val indent: int -> Pretty.T -> Pretty.T
    27   val indent: int -> Pretty.T -> Pretty.T
    28   val string_of_pretty: int -> Pretty.T -> string
    28   val markup_stmt: string -> Pretty.T list -> Pretty.T
    29   val writeln_pretty: int -> Pretty.T -> unit
    29   val format: bool -> int -> Pretty.T -> string
    30 
    30 
    31   val first_upper: string -> string
    31   val first_upper: string -> string
    32   val first_lower: string -> string
    32   val first_lower: string -> string
    33   type var_ctxt
    33   type var_ctxt
    34   val make_vars: string list -> var_ctxt
    34   val make_vars: string list -> var_ctxt
   102 structure Code_Printer : CODE_PRINTER =
   102 structure Code_Printer : CODE_PRINTER =
   103 struct
   103 struct
   104 
   104 
   105 open Code_Thingol;
   105 open Code_Thingol;
   106 
   106 
       
   107 (** generic nonsense *)
       
   108 
   107 fun eqn_error (SOME thm) s = error (s ^ ",\nin equation " ^ Display.string_of_thm_without_context thm)
   109 fun eqn_error (SOME thm) s = error (s ^ ",\nin equation " ^ Display.string_of_thm_without_context thm)
   108   | eqn_error NONE s = error s;
   110   | eqn_error NONE s = error s;
       
   111 
       
   112 val code_presentationN = "code_presentation";
       
   113 val _ = Output.add_mode code_presentationN;
       
   114 val _ = Markup.add_mode code_presentationN YXML.output_markup;
       
   115 
   109 
   116 
   110 (** assembling and printing text pieces **)
   117 (** assembling and printing text pieces **)
   111 
   118 
   112 infixr 5 @@;
   119 infixr 5 @@;
   113 infixr 5 @|;
   120 infixr 5 @|;
   123   | enum_default default sep l r xs = enum sep l r xs;
   130   | enum_default default sep l r xs = enum sep l r xs;
   124 fun semicolon ps = Pretty.block [concat ps, str ";"];
   131 fun semicolon ps = Pretty.block [concat ps, str ";"];
   125 fun doublesemicolon ps = Pretty.block [concat ps, str ";;"];
   132 fun doublesemicolon ps = Pretty.block [concat ps, str ";;"];
   126 fun indent i = Print_Mode.setmp [] (Pretty.indent i);
   133 fun indent i = Print_Mode.setmp [] (Pretty.indent i);
   127 
   134 
   128 fun string_of_pretty width p = Print_Mode.setmp [] (Pretty.string_of_margin width) p ^ "\n";
   135 val stmt_nameN = "stmt_name";
   129 fun writeln_pretty width p = writeln (string_of_pretty width p);
   136 fun markup_stmt name = Pretty.markup (code_presentationN, [(stmt_nameN, name)]);
       
   137 fun filter_presentation selected (XML.Elem ((name, _), xs)) =
       
   138       implode (map (filter_presentation (selected orelse name = code_presentationN)) xs)
       
   139   | filter_presentation selected (XML.Text s) =
       
   140       if selected then s else "";
       
   141 
       
   142 fun format presentation_only width p =
       
   143   if presentation_only then
       
   144     Print_Mode.setmp [code_presentationN] (Pretty.string_of_margin width) p
       
   145     |> YXML.parse_body
       
   146     |> map (filter_presentation false)
       
   147     |> implode
       
   148     |> suffix "\n"
       
   149   else Print_Mode.setmp [] (Pretty.string_of_margin width) p ^ "\n";
   130 
   150 
   131 
   151 
   132 (** names and variable name contexts **)
   152 (** names and variable name contexts **)
   133 
   153 
   134 type var_ctxt = string Symtab.table * Name.context;
   154 type var_ctxt = string Symtab.table * Name.context;