src/Pure/Syntax/printer.ML
author paulson
Tue, 29 Aug 2017 17:41:27 +0100
changeset 66553 6ab32ffb2bdd
parent 64556 851ae0e7b09c
child 67704 23d46836a5ac
permissions -rw-r--r--
merged
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
     1
(*  Title:      Pure/Syntax/printer.ML
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     2
    Author:     Tobias Nipkow and Markus Wenzel, TU Muenchen
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
     3
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
     4
Pretty printing of asts, terms, types and print (ast) translation.
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     5
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     6
42289
dafae095d733 discontinued special status of structure Printer;
wenzelm
parents: 42288
diff changeset
     7
signature BASIC_PRINTER =
2384
d360b395766e removed chartrans_of;
wenzelm
parents: 2365
diff changeset
     8
sig
39137
ccb53edd59f0 turned show_brackets into proper configuration option;
wenzelm
parents: 39134
diff changeset
     9
  val show_brackets: bool Config.T
ccb53edd59f0 turned show_brackets into proper configuration option;
wenzelm
parents: 39134
diff changeset
    10
  val show_types: bool Config.T
39134
917b4b6ba3d2 turned show_sorts/show_types into proper configuration options;
wenzelm
parents: 39128
diff changeset
    11
  val show_sorts: bool Config.T
49657
40e4feac2921 turn constraints into Isabelle_Markup.typing, depending on show_markup options;
wenzelm
parents: 49655
diff changeset
    12
  val show_markup: bool Config.T
39127
e7ecbe86d22e turned show_structs into proper configuration option;
wenzelm
parents: 39118
diff changeset
    13
  val show_structs: bool Config.T
38980
af73cf0dc31f turned show_question_marks into proper configuration option;
wenzelm
parents: 37852
diff changeset
    14
  val show_question_marks: bool Config.T
40956
95fe8598c0c9 added Syntax.pretty_priority;
wenzelm
parents: 39163
diff changeset
    15
  val pretty_priority: int Config.T
2384
d360b395766e removed chartrans_of;
wenzelm
parents: 2365
diff changeset
    16
end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    17
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    18
signature PRINTER =
2384
d360b395766e removed chartrans_of;
wenzelm
parents: 2365
diff changeset
    19
sig
42289
dafae095d733 discontinued special status of structure Printer;
wenzelm
parents: 42288
diff changeset
    20
  include BASIC_PRINTER
dafae095d733 discontinued special status of structure Printer;
wenzelm
parents: 42288
diff changeset
    21
  val show_brackets_raw: Config.raw
dafae095d733 discontinued special status of structure Printer;
wenzelm
parents: 42288
diff changeset
    22
  val show_types_raw: Config.raw
dafae095d733 discontinued special status of structure Printer;
wenzelm
parents: 42288
diff changeset
    23
  val show_sorts_raw: Config.raw
49657
40e4feac2921 turn constraints into Isabelle_Markup.typing, depending on show_markup options;
wenzelm
parents: 49655
diff changeset
    24
  val show_markup_default: bool Unsynchronized.ref
40e4feac2921 turn constraints into Isabelle_Markup.typing, depending on show_markup options;
wenzelm
parents: 49655
diff changeset
    25
  val show_markup_raw: Config.raw
42289
dafae095d733 discontinued special status of structure Printer;
wenzelm
parents: 42288
diff changeset
    26
  val show_structs_raw: Config.raw
dafae095d733 discontinued special status of structure Printer;
wenzelm
parents: 42288
diff changeset
    27
  val show_question_marks_raw: Config.raw
52210
0226035df99d more explicit Printer.type_emphasis, depending on show_type_emphasis;
wenzelm
parents: 52185
diff changeset
    28
  val show_type_emphasis: bool Config.T
0226035df99d more explicit Printer.type_emphasis, depending on show_type_emphasis;
wenzelm
parents: 52185
diff changeset
    29
  val type_emphasis: Proof.context -> typ -> bool
2200
2538977e94fa added print_mode: string list ref (order of printer tables);
wenzelm
parents: 1509
diff changeset
    30
  type prtabs
2538977e94fa added print_mode: string list ref (order of printer tables);
wenzelm
parents: 1509
diff changeset
    31
  val empty_prtabs: prtabs
42288
2074b31650e6 discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents: 42284
diff changeset
    32
  val update_prtabs: string -> Syntax_Ext.xprod list -> prtabs -> prtabs
2074b31650e6 discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents: 42284
diff changeset
    33
  val remove_prtabs: string -> Syntax_Ext.xprod list -> prtabs -> prtabs
2200
2538977e94fa added print_mode: string list ref (order of printer tables);
wenzelm
parents: 1509
diff changeset
    34
  val merge_prtabs: prtabs -> prtabs -> prtabs
42267
9566078ad905 simplified printer context: uniform externing and static token translations;
wenzelm
parents: 42262
diff changeset
    35
  val pretty_term_ast: bool -> Proof.context -> prtabs ->
42254
f427c9890c46 more abstract print translation;
wenzelm
parents: 42245
diff changeset
    36
    (string -> Proof.context -> Ast.ast list -> Ast.ast) ->
49655
6642e559f165 tuned signature;
wenzelm
parents: 47850
diff changeset
    37
    (string -> Ast.ast list -> Pretty.T option) ->
42382
dcd983ee2c29 provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents: 42297
diff changeset
    38
    (string -> Markup.T list * string) ->
42267
9566078ad905 simplified printer context: uniform externing and static token translations;
wenzelm
parents: 42262
diff changeset
    39
    Ast.ast -> Pretty.T list
9566078ad905 simplified printer context: uniform externing and static token translations;
wenzelm
parents: 42262
diff changeset
    40
  val pretty_typ_ast: Proof.context -> prtabs ->
42254
f427c9890c46 more abstract print translation;
wenzelm
parents: 42245
diff changeset
    41
    (string -> Proof.context -> Ast.ast list -> Ast.ast) ->
49655
6642e559f165 tuned signature;
wenzelm
parents: 47850
diff changeset
    42
    (string -> Ast.ast list -> Pretty.T option) ->
42382
dcd983ee2c29 provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents: 42297
diff changeset
    43
    (string -> Markup.T list * string) -> Ast.ast -> Pretty.T list
2384
d360b395766e removed chartrans_of;
wenzelm
parents: 2365
diff changeset
    44
end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    45
2365
38295260a740 added chartrans_of;
wenzelm
parents: 2229
diff changeset
    46
structure Printer: PRINTER =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    47
struct
2200
2538977e94fa added print_mode: string list ref (order of printer tables);
wenzelm
parents: 1509
diff changeset
    48
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    49
(** options for printing **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    50
64556
851ae0e7b09c more symbols;
wenzelm
parents: 62789
diff changeset
    51
val show_brackets_raw = Config.declare_option ("show_brackets", \<^here>);
39163
4d701c0388c3 more explicit indication of Config.raw options, which are only needed for bootstrapping Pure;
wenzelm
parents: 39137
diff changeset
    52
val show_brackets = Config.bool show_brackets_raw;
39137
ccb53edd59f0 turned show_brackets into proper configuration option;
wenzelm
parents: 39134
diff changeset
    53
64556
851ae0e7b09c more symbols;
wenzelm
parents: 62789
diff changeset
    54
val show_types_raw = Config.declare_option ("show_types", \<^here>);
39163
4d701c0388c3 more explicit indication of Config.raw options, which are only needed for bootstrapping Pure;
wenzelm
parents: 39137
diff changeset
    55
val show_types = Config.bool show_types_raw;
39134
917b4b6ba3d2 turned show_sorts/show_types into proper configuration options;
wenzelm
parents: 39128
diff changeset
    56
64556
851ae0e7b09c more symbols;
wenzelm
parents: 62789
diff changeset
    57
val show_sorts_raw = Config.declare_option ("show_sorts", \<^here>);
39163
4d701c0388c3 more explicit indication of Config.raw options, which are only needed for bootstrapping Pure;
wenzelm
parents: 39137
diff changeset
    58
val show_sorts = Config.bool show_sorts_raw;
39134
917b4b6ba3d2 turned show_sorts/show_types into proper configuration options;
wenzelm
parents: 39128
diff changeset
    59
49657
40e4feac2921 turn constraints into Isabelle_Markup.typing, depending on show_markup options;
wenzelm
parents: 49655
diff changeset
    60
val show_markup_default = Unsynchronized.ref false;
56438
7f6b2634d853 more source positions;
wenzelm
parents: 52211
diff changeset
    61
val show_markup_raw =
64556
851ae0e7b09c more symbols;
wenzelm
parents: 62789
diff changeset
    62
  Config.declare ("show_markup", \<^here>) (fn _ => Config.Bool (! show_markup_default));
49657
40e4feac2921 turn constraints into Isabelle_Markup.typing, depending on show_markup options;
wenzelm
parents: 49655
diff changeset
    63
val show_markup = Config.bool show_markup_raw;
40e4feac2921 turn constraints into Isabelle_Markup.typing, depending on show_markup options;
wenzelm
parents: 49655
diff changeset
    64
56438
7f6b2634d853 more source positions;
wenzelm
parents: 52211
diff changeset
    65
val show_structs_raw =
64556
851ae0e7b09c more symbols;
wenzelm
parents: 62789
diff changeset
    66
  Config.declare ("show_structs", \<^here>) (fn _ => Config.Bool false);
39163
4d701c0388c3 more explicit indication of Config.raw options, which are only needed for bootstrapping Pure;
wenzelm
parents: 39137
diff changeset
    67
val show_structs = Config.bool show_structs_raw;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    68
64556
851ae0e7b09c more symbols;
wenzelm
parents: 62789
diff changeset
    69
val show_question_marks_raw = Config.declare_option ("show_question_marks", \<^here>);
39163
4d701c0388c3 more explicit indication of Config.raw options, which are only needed for bootstrapping Pure;
wenzelm
parents: 39137
diff changeset
    70
val show_question_marks = Config.bool show_question_marks_raw;
38980
af73cf0dc31f turned show_question_marks into proper configuration option;
wenzelm
parents: 37852
diff changeset
    71
52210
0226035df99d more explicit Printer.type_emphasis, depending on show_type_emphasis;
wenzelm
parents: 52185
diff changeset
    72
val show_type_emphasis =
64556
851ae0e7b09c more symbols;
wenzelm
parents: 62789
diff changeset
    73
  Config.bool (Config.declare ("show_type_emphasis", \<^here>) (fn _ => Config.Bool true));
52210
0226035df99d more explicit Printer.type_emphasis, depending on show_type_emphasis;
wenzelm
parents: 52185
diff changeset
    74
0226035df99d more explicit Printer.type_emphasis, depending on show_type_emphasis;
wenzelm
parents: 52185
diff changeset
    75
fun type_emphasis ctxt T =
0226035df99d more explicit Printer.type_emphasis, depending on show_type_emphasis;
wenzelm
parents: 52185
diff changeset
    76
  T <> dummyT andalso
0226035df99d more explicit Printer.type_emphasis, depending on show_type_emphasis;
wenzelm
parents: 52185
diff changeset
    77
    (Config.get ctxt show_types orelse Config.get ctxt show_markup orelse
0226035df99d more explicit Printer.type_emphasis, depending on show_type_emphasis;
wenzelm
parents: 52185
diff changeset
    78
      Config.get ctxt show_type_emphasis andalso not (can dest_Type T));
49690
a6814de45b69 more explicit show_type_constraint, show_sort_constraint;
wenzelm
parents: 49657
diff changeset
    79
14837
827c68f8267c added pp_show_brackets; support unbreakable blocks;
wenzelm
parents: 14783
diff changeset
    80
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    81
2200
2538977e94fa added print_mode: string list ref (order of printer tables);
wenzelm
parents: 1509
diff changeset
    82
(** type prtabs **)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    83
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    84
datatype symb =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    85
  Arg of int |
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    86
  TypArg of int |
49821
d15fe10593ff clarified output token markup (see also bc22daeed49e);
wenzelm
parents: 49690
diff changeset
    87
  String of bool * string |
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    88
  Break of int |
62789
ce15dd971965 explicit property for unbreakable block;
wenzelm
parents: 62783
diff changeset
    89
  Block of Syntax_Ext.block_info * symb list;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    90
2200
2538977e94fa added print_mode: string list ref (order of printer tables);
wenzelm
parents: 1509
diff changeset
    91
type prtabs = (string * ((symb list * int * int) list) Symtab.table) list;
2538977e94fa added print_mode: string list ref (order of printer tables);
wenzelm
parents: 1509
diff changeset
    92
18957
8c3abd63bce3 TableFun: renamed xxx_multi to xxx_list;
wenzelm
parents: 18857
diff changeset
    93
fun mode_tab prtabs mode = the_default Symtab.empty (AList.lookup (op =) prtabs mode);
19482
9f11af8f7ef9 tuned basic list operators (flat, maps, map_filter);
wenzelm
parents: 19374
diff changeset
    94
fun mode_tabs prtabs modes = map_filter (AList.lookup (op =) prtabs) (modes @ [""]);
2200
2538977e94fa added print_mode: string list ref (order of printer tables);
wenzelm
parents: 1509
diff changeset
    95
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    96
3816
7e1b695bcc5e changed preference order of prtab entries;
wenzelm
parents: 3776
diff changeset
    97
(* xprod_to_fmt *)
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
    98
42288
2074b31650e6 discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents: 42284
diff changeset
    99
fun xprod_to_fmt (Syntax_Ext.XProd (_, _, "", _)) = NONE
2074b31650e6 discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents: 42284
diff changeset
   100
  | xprod_to_fmt (Syntax_Ext.XProd (_, xsymbs, const, pri)) =
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   101
      let
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   102
        fun arg (s, p) =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   103
          (if s = "type" then TypArg else Arg)
42297
140f283266b7 discontinued Syntax.max_pri, which is not really a symbolic parameter;
wenzelm
parents: 42289
diff changeset
   104
          (if Lexicon.is_terminal s then 1000 else p);
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   105
42288
2074b31650e6 discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents: 42284
diff changeset
   106
        fun xsyms_to_syms (Syntax_Ext.Delim s :: xsyms) =
49821
d15fe10593ff clarified output token markup (see also bc22daeed49e);
wenzelm
parents: 49690
diff changeset
   107
              apfst (cons (String (not (exists Symbol.is_block_ctrl (Symbol.explode s)), s)))
d15fe10593ff clarified output token markup (see also bc22daeed49e);
wenzelm
parents: 49690
diff changeset
   108
                (xsyms_to_syms xsyms)
42288
2074b31650e6 discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents: 42284
diff changeset
   109
          | xsyms_to_syms (Syntax_Ext.Argument s_p :: xsyms) =
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   110
              apfst (cons (arg s_p)) (xsyms_to_syms xsyms)
42288
2074b31650e6 discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents: 42284
diff changeset
   111
          | xsyms_to_syms (Syntax_Ext.Space s :: xsyms) =
49821
d15fe10593ff clarified output token markup (see also bc22daeed49e);
wenzelm
parents: 49690
diff changeset
   112
              apfst (cons (String (false, s))) (xsyms_to_syms xsyms)
62783
75ee05386b90 explicit mixfix block properties;
wenzelm
parents: 56438
diff changeset
   113
          | xsyms_to_syms (Syntax_Ext.Bg info :: xsyms) =
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   114
              let
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   115
                val (bsyms, xsyms') = xsyms_to_syms xsyms;
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   116
                val (syms, xsyms'') = xsyms_to_syms xsyms';
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   117
              in
62783
75ee05386b90 explicit mixfix block properties;
wenzelm
parents: 56438
diff changeset
   118
                (Block (info, bsyms) :: syms, xsyms'')
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   119
              end
42288
2074b31650e6 discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents: 42284
diff changeset
   120
          | xsyms_to_syms (Syntax_Ext.Brk i :: xsyms) =
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   121
              apfst (cons (Break i)) (xsyms_to_syms xsyms)
42288
2074b31650e6 discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents: 42284
diff changeset
   122
          | xsyms_to_syms (Syntax_Ext.En :: xsyms) = ([], xsyms)
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   123
          | xsyms_to_syms [] = ([], []);
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   124
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   125
        fun nargs (Arg _ :: syms) = nargs syms + 1
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   126
          | nargs (TypArg _ :: syms) = nargs syms + 1
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   127
          | nargs (String _ :: syms) = nargs syms
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   128
          | nargs (Break _ :: syms) = nargs syms
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   129
          | nargs (Block (_, bsyms) :: syms) = nargs syms + nargs bsyms
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   130
          | nargs [] = 0;
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   131
      in
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   132
        (case xsyms_to_syms xsymbs of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   133
          (symbs, []) => SOME (const, (symbs, nargs symbs, pri))
37852
a902f158b4fc eliminated old-style sys_error/SYS_ERROR in favour of exception Fail -- after careful checking that there is no overlap with existing handling of that;
wenzelm
parents: 37216
diff changeset
   134
        | _ => raise Fail "Unbalanced pretty-printing blocks")
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   135
      end;
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   136
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   137
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   138
(* empty, extend, merge prtabs *)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   139
2200
2538977e94fa added print_mode: string list ref (order of printer tables);
wenzelm
parents: 1509
diff changeset
   140
val empty_prtabs = [];
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   141
25393
0856e0141062 replaced extend_prtabs by update_prtabs (absorb duplicates);
wenzelm
parents: 25386
diff changeset
   142
fun update_prtabs mode xprods prtabs =
2200
2538977e94fa added print_mode: string list ref (order of printer tables);
wenzelm
parents: 1509
diff changeset
   143
  let
19482
9f11af8f7ef9 tuned basic list operators (flat, maps, map_filter);
wenzelm
parents: 19374
diff changeset
   144
    val fmts = map_filter xprod_to_fmt xprods;
25393
0856e0141062 replaced extend_prtabs by update_prtabs (absorb duplicates);
wenzelm
parents: 25386
diff changeset
   145
    val tab' = fold (Symtab.update_list (op =)) fmts (mode_tab prtabs mode);
25386
82b62fe11d7a remove_prtabs: tuned, avoid excessive garbage;
wenzelm
parents: 24612
diff changeset
   146
  in AList.update (op =) (mode, tab') prtabs end;
15753
eb014dfc57ee tuned extend_prtabs;
wenzelm
parents: 15574
diff changeset
   147
25386
82b62fe11d7a remove_prtabs: tuned, avoid excessive garbage;
wenzelm
parents: 24612
diff changeset
   148
fun remove_prtabs mode xprods prtabs =
82b62fe11d7a remove_prtabs: tuned, avoid excessive garbage;
wenzelm
parents: 24612
diff changeset
   149
  let
82b62fe11d7a remove_prtabs: tuned, avoid excessive garbage;
wenzelm
parents: 24612
diff changeset
   150
    val tab = mode_tab prtabs mode;
42288
2074b31650e6 discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents: 42284
diff changeset
   151
    val fmts = map_filter (fn xprod as Syntax_Ext.XProd (_, _, c, _) =>
25386
82b62fe11d7a remove_prtabs: tuned, avoid excessive garbage;
wenzelm
parents: 24612
diff changeset
   152
      if null (Symtab.lookup_list tab c) then NONE
82b62fe11d7a remove_prtabs: tuned, avoid excessive garbage;
wenzelm
parents: 24612
diff changeset
   153
      else xprod_to_fmt xprod) xprods;
25393
0856e0141062 replaced extend_prtabs by update_prtabs (absorb duplicates);
wenzelm
parents: 25386
diff changeset
   154
    val tab' = fold (Symtab.remove_list (op =)) fmts tab;
0856e0141062 replaced extend_prtabs by update_prtabs (absorb duplicates);
wenzelm
parents: 25386
diff changeset
   155
  in AList.update (op =) (mode, tab') prtabs end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   156
2200
2538977e94fa added print_mode: string list ref (order of printer tables);
wenzelm
parents: 1509
diff changeset
   157
fun merge_prtabs prtabs1 prtabs2 =
2538977e94fa added print_mode: string list ref (order of printer tables);
wenzelm
parents: 1509
diff changeset
   158
  let
19046
bc5c6c9b114e removed distinct, renamed gen_distinct to distinct;
wenzelm
parents: 18977
diff changeset
   159
    val modes = distinct (op =) (map fst (prtabs1 @ prtabs2));
18957
8c3abd63bce3 TableFun: renamed xxx_multi to xxx_list;
wenzelm
parents: 18857
diff changeset
   160
    fun merge m = (m, Symtab.merge_list (op =) (mode_tab prtabs1 m, mode_tab prtabs2 m));
12292
c4090cc2aa15 Symtab.merge_multi';
wenzelm
parents: 12252
diff changeset
   161
  in map merge modes end;
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   162
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   163
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   164
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   165
(** pretty term or typ asts **)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   166
2200
2538977e94fa added print_mode: string list ref (order of printer tables);
wenzelm
parents: 1509
diff changeset
   167
fun is_chain [Block (_, pr)] = is_chain pr
2538977e94fa added print_mode: string list ref (order of printer tables);
wenzelm
parents: 1509
diff changeset
   168
  | is_chain [Arg _] = true
2538977e94fa added print_mode: string list ref (order of printer tables);
wenzelm
parents: 1509
diff changeset
   169
  | is_chain _  = false;
506
e0ca460d6e51 improved show_brackets again - Trueprop does not create () any more.
nipkow
parents: 505
diff changeset
   170
56438
7f6b2634d853 more source positions;
wenzelm
parents: 52211
diff changeset
   171
val pretty_priority =
64556
851ae0e7b09c more symbols;
wenzelm
parents: 62789
diff changeset
   172
  Config.int (Config.declare ("Syntax.pretty_priority", \<^here>) (K (Config.Int 0)));
40956
95fe8598c0c9 added Syntax.pretty_priority;
wenzelm
parents: 39163
diff changeset
   173
49655
6642e559f165 tuned signature;
wenzelm
parents: 47850
diff changeset
   174
fun pretty type_mode curried ctxt tabs trf markup_trans markup_extern ast0 =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   175
  let
39137
ccb53edd59f0 turned show_brackets into proper configuration option;
wenzelm
parents: 39134
diff changeset
   176
    val show_brackets = Config.get ctxt show_brackets;
35429
afa8cf9e63d8 authentic syntax for classes and type constructors;
wenzelm
parents: 35262
diff changeset
   177
2200
2538977e94fa added print_mode: string list ref (order of printer tables);
wenzelm
parents: 1509
diff changeset
   178
    (*default applications: prefix / postfix*)
2538977e94fa added print_mode: string list ref (order of printer tables);
wenzelm
parents: 1509
diff changeset
   179
    val appT =
42284
326f57825e1a explicit structure Syntax_Trans;
wenzelm
parents: 42267
diff changeset
   180
      if type_mode then Syntax_Trans.tappl_ast_tr'
326f57825e1a explicit structure Syntax_Trans;
wenzelm
parents: 42267
diff changeset
   181
      else if curried then Syntax_Trans.applC_ast_tr'
326f57825e1a explicit structure Syntax_Trans;
wenzelm
parents: 42267
diff changeset
   182
      else Syntax_Trans.appl_ast_tr';
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   183
23630
bc22daeed49e pretty: markup for syntax/name of authentic consts;
wenzelm
parents: 23615
diff changeset
   184
    fun synT _ ([], args) = ([], args)
42267
9566078ad905 simplified printer context: uniform externing and static token translations;
wenzelm
parents: 42262
diff changeset
   185
      | synT m (Arg p :: symbs, t :: args) =
9566078ad905 simplified printer context: uniform externing and static token translations;
wenzelm
parents: 42262
diff changeset
   186
          let val (Ts, args') = synT m (symbs, args);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   187
          in (astT (t, p) @ Ts, args') end
42267
9566078ad905 simplified printer context: uniform externing and static token translations;
wenzelm
parents: 42262
diff changeset
   188
      | synT m (TypArg p :: symbs, t :: args) =
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   189
          let
42267
9566078ad905 simplified printer context: uniform externing and static token translations;
wenzelm
parents: 42262
diff changeset
   190
            val (Ts, args') = synT m (symbs, args);
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   191
          in
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   192
            if type_mode then (astT (t, p) @ Ts, args')
40956
95fe8598c0c9 added Syntax.pretty_priority;
wenzelm
parents: 39163
diff changeset
   193
            else
42267
9566078ad905 simplified printer context: uniform externing and static token translations;
wenzelm
parents: 42262
diff changeset
   194
              (pretty true curried (Config.put pretty_priority p ctxt)
49655
6642e559f165 tuned signature;
wenzelm
parents: 47850
diff changeset
   195
                tabs trf markup_trans markup_extern t @ Ts, args')
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   196
          end
49821
d15fe10593ff clarified output token markup (see also bc22daeed49e);
wenzelm
parents: 49690
diff changeset
   197
      | synT m (String (do_mark, s) :: symbs, args) =
47850
c638127b4653 avoid interference of markup for literal tokens, which may contain slightly odd \<^bsub> \<^esub> counted as pseudo-markup (especially relevant for HTML output, e.g. of thm power3_eq_cube);
wenzelm
parents: 42382
diff changeset
   198
          let
c638127b4653 avoid interference of markup for literal tokens, which may contain slightly odd \<^bsub> \<^esub> counted as pseudo-markup (especially relevant for HTML output, e.g. of thm power3_eq_cube);
wenzelm
parents: 42382
diff changeset
   199
            val (Ts, args') = synT m (symbs, args);
c638127b4653 avoid interference of markup for literal tokens, which may contain slightly odd \<^bsub> \<^esub> counted as pseudo-markup (especially relevant for HTML output, e.g. of thm power3_eq_cube);
wenzelm
parents: 42382
diff changeset
   200
            val T =
49821
d15fe10593ff clarified output token markup (see also bc22daeed49e);
wenzelm
parents: 49690
diff changeset
   201
              if do_mark
d15fe10593ff clarified output token markup (see also bc22daeed49e);
wenzelm
parents: 49690
diff changeset
   202
              then Pretty.marks_str (m @ [Lexicon.literal_markup s], s)
d15fe10593ff clarified output token markup (see also bc22daeed49e);
wenzelm
parents: 49690
diff changeset
   203
              else Pretty.str s;
47850
c638127b4653 avoid interference of markup for literal tokens, which may contain slightly odd \<^bsub> \<^esub> counted as pseudo-markup (especially relevant for HTML output, e.g. of thm power3_eq_cube);
wenzelm
parents: 42382
diff changeset
   204
          in (T :: Ts, args') end
62789
ce15dd971965 explicit property for unbreakable block;
wenzelm
parents: 62783
diff changeset
   205
      | synT m (Block ({markup, consistent, unbreakable, indent}, bsymbs) :: symbs, args) =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   206
          let
42267
9566078ad905 simplified printer context: uniform externing and static token translations;
wenzelm
parents: 42262
diff changeset
   207
            val (bTs, args') = synT m (bsymbs, args);
9566078ad905 simplified printer context: uniform externing and static token translations;
wenzelm
parents: 42262
diff changeset
   208
            val (Ts, args'') = synT m (symbs, args');
62789
ce15dd971965 explicit property for unbreakable block;
wenzelm
parents: 62783
diff changeset
   209
            val T =
ce15dd971965 explicit property for unbreakable block;
wenzelm
parents: 62783
diff changeset
   210
              Pretty.markup_block {markup = markup, consistent = consistent, indent = indent} bTs
ce15dd971965 explicit property for unbreakable block;
wenzelm
parents: 62783
diff changeset
   211
              |> unbreakable ? Pretty.unbreakable;
14837
827c68f8267c added pp_show_brackets; support unbreakable blocks;
wenzelm
parents: 14783
diff changeset
   212
          in (T :: Ts, args'') end
42267
9566078ad905 simplified printer context: uniform externing and static token translations;
wenzelm
parents: 42262
diff changeset
   213
      | synT m (Break i :: symbs, args) =
14837
827c68f8267c added pp_show_brackets; support unbreakable blocks;
wenzelm
parents: 14783
diff changeset
   214
          let
42267
9566078ad905 simplified printer context: uniform externing and static token translations;
wenzelm
parents: 42262
diff changeset
   215
            val (Ts, args') = synT m (symbs, args);
14837
827c68f8267c added pp_show_brackets; support unbreakable blocks;
wenzelm
parents: 14783
diff changeset
   216
            val T = if i < 0 then Pretty.fbrk else Pretty.brk i;
827c68f8267c added pp_show_brackets; support unbreakable blocks;
wenzelm
parents: 14783
diff changeset
   217
          in (T :: Ts, args') end
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   218
42297
140f283266b7 discontinued Syntax.max_pri, which is not really a symbolic parameter;
wenzelm
parents: 42289
diff changeset
   219
    and parT m (pr, args, p, p': int) = #1 (synT m
62783
75ee05386b90 explicit mixfix block properties;
wenzelm
parents: 56438
diff changeset
   220
          (if p > p' orelse (show_brackets andalso p' <> 1000 andalso not (is_chain pr)) then
75ee05386b90 explicit mixfix block properties;
wenzelm
parents: 56438
diff changeset
   221
            [Block (Syntax_Ext.block_indent 1, String (false, "(") :: pr @ [String (false, ")")])]
42288
2074b31650e6 discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents: 42284
diff changeset
   222
           else pr, args))
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   223
42267
9566078ad905 simplified printer context: uniform externing and static token translations;
wenzelm
parents: 42262
diff changeset
   224
    and atomT a = Pretty.marks_str (markup_extern a)
19374
ae4a225e0c1f pretty: late externing of consts (support authentic syntax);
wenzelm
parents: 19046
diff changeset
   225
ae4a225e0c1f pretty: late externing of consts (support authentic syntax);
wenzelm
parents: 19046
diff changeset
   226
    and prefixT (_, a, [], _) = [atomT a]
16611
edb368e2878f proper treatment of advanced trfuns: pass thy argument;
wenzelm
parents: 15973
diff changeset
   227
      | prefixT (c, _, args, p) = astT (appT (c, args), p)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   228
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   229
    and splitT 0 ([x], ys) = (x, ys)
5691
3a6de95c09d0 no open;
wenzelm
parents: 4699
diff changeset
   230
      | splitT 0 (rev_xs, ys) = (Ast.Appl (rev rev_xs), ys)
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   231
      | splitT n (rev_xs, y :: ys) = splitT (n - 1) (y :: rev_xs, ys)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   232
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   233
    and combT (tup as (c, a, args, p)) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   234
      let
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   235
        val nargs = length args;
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   236
2701
348ec44248df split ast_of_term(T);
wenzelm
parents: 2507
diff changeset
   237
        (*find matching table entry, or print as prefix / postfix*)
6280
218733fb6987 tuned pretty format lookup;
wenzelm
parents: 6273
diff changeset
   238
        fun prnt ([], []) = prefixT tup
18957
8c3abd63bce3 TableFun: renamed xxx_multi to xxx_list;
wenzelm
parents: 18857
diff changeset
   239
          | prnt ([], tb :: tbs) = prnt (Symtab.lookup_list tb a, tbs)
6280
218733fb6987 tuned pretty format lookup;
wenzelm
parents: 6273
diff changeset
   240
          | prnt ((pr, n, p') :: prnps, tbs) =
42267
9566078ad905 simplified printer context: uniform externing and static token translations;
wenzelm
parents: 42262
diff changeset
   241
              if nargs = n then parT (#1 (markup_extern a)) (pr, args, p, p')
2200
2538977e94fa added print_mode: string list ref (order of printer tables);
wenzelm
parents: 1509
diff changeset
   242
              else if nargs > n andalso not type_mode then
2538977e94fa added print_mode: string list ref (order of printer tables);
wenzelm
parents: 1509
diff changeset
   243
                astT (appT (splitT n ([c], args)), p)
6280
218733fb6987 tuned pretty format lookup;
wenzelm
parents: 6273
diff changeset
   244
              else prnt (prnps, tbs);
2200
2538977e94fa added print_mode: string list ref (order of printer tables);
wenzelm
parents: 1509
diff changeset
   245
      in
49655
6642e559f165 tuned signature;
wenzelm
parents: 47850
diff changeset
   246
        (case markup_trans a args of
26707
ddf6bab64b96 token translations: context dependent, result Pretty.T;
wenzelm
parents: 25393
diff changeset
   247
          SOME prt => [prt]
42254
f427c9890c46 more abstract print translation;
wenzelm
parents: 42245
diff changeset
   248
        | NONE => astT (trf a ctxt args, p) handle Match => prnt ([], tabs))
2200
2538977e94fa added print_mode: string list ref (order of printer tables);
wenzelm
parents: 1509
diff changeset
   249
      end
2538977e94fa added print_mode: string list ref (order of printer tables);
wenzelm
parents: 1509
diff changeset
   250
5691
3a6de95c09d0 no open;
wenzelm
parents: 4699
diff changeset
   251
    and astT (c as Ast.Constant a, p) = combT (c, a, [], p)
42267
9566078ad905 simplified printer context: uniform externing and static token translations;
wenzelm
parents: 42262
diff changeset
   252
      | astT (ast as Ast.Variable _, _) = [Ast.pretty_ast ast]
6280
218733fb6987 tuned pretty format lookup;
wenzelm
parents: 6273
diff changeset
   253
      | astT (Ast.Appl ((c as Ast.Constant a) :: (args as _ :: _)), p) = combT (c, a, args, p)
5691
3a6de95c09d0 no open;
wenzelm
parents: 4699
diff changeset
   254
      | astT (Ast.Appl (f :: (args as _ :: _)), p) = astT (appT (f, args), p)
3a6de95c09d0 no open;
wenzelm
parents: 4699
diff changeset
   255
      | astT (ast as Ast.Appl _, _) = raise Ast.AST ("pretty: malformed ast", [ast]);
40956
95fe8598c0c9 added Syntax.pretty_priority;
wenzelm
parents: 39163
diff changeset
   256
  in astT (ast0, Config.get ctxt pretty_priority) end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   257
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   258
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   259
(* pretty_term_ast *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   260
49655
6642e559f165 tuned signature;
wenzelm
parents: 47850
diff changeset
   261
fun pretty_term_ast curried ctxt prtabs trf markup_trans extern ast =
6642e559f165 tuned signature;
wenzelm
parents: 47850
diff changeset
   262
  pretty false curried ctxt (mode_tabs prtabs (print_mode_value ())) trf markup_trans extern ast;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   263
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   264
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   265
(* pretty_typ_ast *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   266
49655
6642e559f165 tuned signature;
wenzelm
parents: 47850
diff changeset
   267
fun pretty_typ_ast ctxt prtabs trf markup_trans extern ast =
6642e559f165 tuned signature;
wenzelm
parents: 47850
diff changeset
   268
  pretty true false ctxt (mode_tabs prtabs (print_mode_value ())) trf markup_trans extern ast;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   269
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   270
end;
42289
dafae095d733 discontinued special status of structure Printer;
wenzelm
parents: 42288
diff changeset
   271
dafae095d733 discontinued special status of structure Printer;
wenzelm
parents: 42288
diff changeset
   272
structure Basic_Printer: BASIC_PRINTER = Printer;
dafae095d733 discontinued special status of structure Printer;
wenzelm
parents: 42288
diff changeset
   273
open Basic_Printer;
dafae095d733 discontinued special status of structure Printer;
wenzelm
parents: 42288
diff changeset
   274