src/Pure/Syntax/printer.ML
author wenzelm
Wed, 06 Apr 2011 13:33:46 +0200
changeset 42247 12fe41a92cd5
parent 42245 29e3967550d5
child 42254 f427c9890c46
permissions -rw-r--r--
typed_print_translation: discontinued show_sorts argument;
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
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     7
signature PRINTER0 =
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_default: bool Unsynchronized.ref
39163
4d701c0388c3 more explicit indication of Config.raw options, which are only needed for bootstrapping Pure;
wenzelm
parents: 39137
diff changeset
    10
  val show_brackets_raw: Config.raw
39137
ccb53edd59f0 turned show_brackets into proper configuration option;
wenzelm
parents: 39134
diff changeset
    11
  val show_brackets: bool Config.T
ccb53edd59f0 turned show_brackets into proper configuration option;
wenzelm
parents: 39134
diff changeset
    12
  val show_types_default: bool Unsynchronized.ref
39163
4d701c0388c3 more explicit indication of Config.raw options, which are only needed for bootstrapping Pure;
wenzelm
parents: 39137
diff changeset
    13
  val show_types_raw: Config.raw
39137
ccb53edd59f0 turned show_brackets into proper configuration option;
wenzelm
parents: 39134
diff changeset
    14
  val show_types: bool Config.T
39134
917b4b6ba3d2 turned show_sorts/show_types into proper configuration options;
wenzelm
parents: 39128
diff changeset
    15
  val show_sorts_default: bool Unsynchronized.ref
39163
4d701c0388c3 more explicit indication of Config.raw options, which are only needed for bootstrapping Pure;
wenzelm
parents: 39137
diff changeset
    16
  val show_sorts_raw: Config.raw
39134
917b4b6ba3d2 turned show_sorts/show_types into proper configuration options;
wenzelm
parents: 39128
diff changeset
    17
  val show_sorts: bool Config.T
39115
a00da1674c1c turned show_no_free_types into proper configuration option show_free_types, with flipped polarity;
wenzelm
parents: 38980
diff changeset
    18
  val show_free_types: bool Config.T
39118
12f3788be67b turned show_all_types into proper configuration option;
wenzelm
parents: 39117
diff changeset
    19
  val show_all_types: bool Config.T
39163
4d701c0388c3 more explicit indication of Config.raw options, which are only needed for bootstrapping Pure;
wenzelm
parents: 39137
diff changeset
    20
  val show_structs_raw: Config.raw
39127
e7ecbe86d22e turned show_structs into proper configuration option;
wenzelm
parents: 39118
diff changeset
    21
  val show_structs: bool Config.T
38980
af73cf0dc31f turned show_question_marks into proper configuration option;
wenzelm
parents: 37852
diff changeset
    22
  val show_question_marks_default: bool Unsynchronized.ref
39163
4d701c0388c3 more explicit indication of Config.raw options, which are only needed for bootstrapping Pure;
wenzelm
parents: 39137
diff changeset
    23
  val show_question_marks_raw: Config.raw
38980
af73cf0dc31f turned show_question_marks into proper configuration option;
wenzelm
parents: 37852
diff changeset
    24
  val show_question_marks: bool Config.T
40956
95fe8598c0c9 added Syntax.pretty_priority;
wenzelm
parents: 39163
diff changeset
    25
  val pretty_priority: int Config.T
42245
29e3967550d5 moved unparse material to syntax_phases.ML;
wenzelm
parents: 42048
diff changeset
    26
  val apply_trans: 'a -> ('a -> 'b -> 'c) list -> 'b -> 'c
2384
d360b395766e removed chartrans_of;
wenzelm
parents: 2365
diff changeset
    27
end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    28
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    29
signature PRINTER =
2384
d360b395766e removed chartrans_of;
wenzelm
parents: 2365
diff changeset
    30
sig
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    31
  include PRINTER0
2200
2538977e94fa added print_mode: string list ref (order of printer tables);
wenzelm
parents: 1509
diff changeset
    32
  type prtabs
2538977e94fa added print_mode: string list ref (order of printer tables);
wenzelm
parents: 1509
diff changeset
    33
  val empty_prtabs: prtabs
37216
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 35435
diff changeset
    34
  val update_prtabs: string -> Syn_Ext.xprod list -> prtabs -> prtabs
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 35435
diff changeset
    35
  val remove_prtabs: string -> Syn_Ext.xprod list -> prtabs -> prtabs
2200
2538977e94fa added print_mode: string list ref (order of printer tables);
wenzelm
parents: 1509
diff changeset
    36
  val merge_prtabs: prtabs -> prtabs -> prtabs
35429
afa8cf9e63d8 authentic syntax for classes and type constructors;
wenzelm
parents: 35262
diff changeset
    37
  val pretty_term_ast: {extern_class: string -> xstring, extern_type: string -> xstring,
afa8cf9e63d8 authentic syntax for classes and type constructors;
wenzelm
parents: 35262
diff changeset
    38
      extern_const: string -> xstring} -> Proof.context -> bool -> prtabs ->
afa8cf9e63d8 authentic syntax for classes and type constructors;
wenzelm
parents: 35262
diff changeset
    39
    (string -> (Proof.context -> Ast.ast list -> Ast.ast) list) ->
afa8cf9e63d8 authentic syntax for classes and type constructors;
wenzelm
parents: 35262
diff changeset
    40
    (string -> (Proof.context -> string -> Pretty.T) option) -> Ast.ast -> Pretty.T list
afa8cf9e63d8 authentic syntax for classes and type constructors;
wenzelm
parents: 35262
diff changeset
    41
  val pretty_typ_ast: {extern_class: string -> xstring, extern_type: string -> xstring} ->
afa8cf9e63d8 authentic syntax for classes and type constructors;
wenzelm
parents: 35262
diff changeset
    42
    Proof.context -> bool -> prtabs ->
afa8cf9e63d8 authentic syntax for classes and type constructors;
wenzelm
parents: 35262
diff changeset
    43
    (string -> (Proof.context -> Ast.ast list -> Ast.ast) list) ->
afa8cf9e63d8 authentic syntax for classes and type constructors;
wenzelm
parents: 35262
diff changeset
    44
    (string -> (Proof.context -> string -> Pretty.T) option) -> Ast.ast -> Pretty.T list
2384
d360b395766e removed chartrans_of;
wenzelm
parents: 2365
diff changeset
    45
end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    46
2365
38295260a740 added chartrans_of;
wenzelm
parents: 2229
diff changeset
    47
structure Printer: PRINTER =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    48
struct
2200
2538977e94fa added print_mode: string list ref (order of printer tables);
wenzelm
parents: 1509
diff changeset
    49
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    50
(** options for printing **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    51
39137
ccb53edd59f0 turned show_brackets into proper configuration option;
wenzelm
parents: 39134
diff changeset
    52
val show_brackets_default = Unsynchronized.ref false;
39163
4d701c0388c3 more explicit indication of Config.raw options, which are only needed for bootstrapping Pure;
wenzelm
parents: 39137
diff changeset
    53
val show_brackets_raw =
39137
ccb53edd59f0 turned show_brackets into proper configuration option;
wenzelm
parents: 39134
diff changeset
    54
  Config.declare "show_brackets" (fn _ => Config.Bool (! show_brackets_default));
39163
4d701c0388c3 more explicit indication of Config.raw options, which are only needed for bootstrapping Pure;
wenzelm
parents: 39137
diff changeset
    55
val show_brackets = Config.bool show_brackets_raw;
39137
ccb53edd59f0 turned show_brackets into proper configuration option;
wenzelm
parents: 39134
diff changeset
    56
39134
917b4b6ba3d2 turned show_sorts/show_types into proper configuration options;
wenzelm
parents: 39128
diff changeset
    57
val show_types_default = Unsynchronized.ref false;
39163
4d701c0388c3 more explicit indication of Config.raw options, which are only needed for bootstrapping Pure;
wenzelm
parents: 39137
diff changeset
    58
val show_types_raw = Config.declare "show_types" (fn _ => Config.Bool (! show_types_default));
4d701c0388c3 more explicit indication of Config.raw options, which are only needed for bootstrapping Pure;
wenzelm
parents: 39137
diff changeset
    59
val show_types = Config.bool show_types_raw;
39134
917b4b6ba3d2 turned show_sorts/show_types into proper configuration options;
wenzelm
parents: 39128
diff changeset
    60
917b4b6ba3d2 turned show_sorts/show_types into proper configuration options;
wenzelm
parents: 39128
diff changeset
    61
val show_sorts_default = Unsynchronized.ref false;
39163
4d701c0388c3 more explicit indication of Config.raw options, which are only needed for bootstrapping Pure;
wenzelm
parents: 39137
diff changeset
    62
val show_sorts_raw = Config.declare "show_sorts" (fn _ => Config.Bool (! show_sorts_default));
4d701c0388c3 more explicit indication of Config.raw options, which are only needed for bootstrapping Pure;
wenzelm
parents: 39137
diff changeset
    63
val show_sorts = Config.bool show_sorts_raw;
39134
917b4b6ba3d2 turned show_sorts/show_types into proper configuration options;
wenzelm
parents: 39128
diff changeset
    64
39118
12f3788be67b turned show_all_types into proper configuration option;
wenzelm
parents: 39117
diff changeset
    65
val show_free_types = Config.bool (Config.declare "show_free_types" (fn _ => Config.Bool true));
12f3788be67b turned show_all_types into proper configuration option;
wenzelm
parents: 39117
diff changeset
    66
val show_all_types = Config.bool (Config.declare "show_all_types" (fn _ => Config.Bool false));
39127
e7ecbe86d22e turned show_structs into proper configuration option;
wenzelm
parents: 39118
diff changeset
    67
39163
4d701c0388c3 more explicit indication of Config.raw options, which are only needed for bootstrapping Pure;
wenzelm
parents: 39137
diff changeset
    68
val show_structs_raw = Config.declare "show_structs" (fn _ => Config.Bool false);
4d701c0388c3 more explicit indication of Config.raw options, which are only needed for bootstrapping Pure;
wenzelm
parents: 39137
diff changeset
    69
val show_structs = Config.bool show_structs_raw;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    70
38980
af73cf0dc31f turned show_question_marks into proper configuration option;
wenzelm
parents: 37852
diff changeset
    71
val show_question_marks_default = Unsynchronized.ref true;
39163
4d701c0388c3 more explicit indication of Config.raw options, which are only needed for bootstrapping Pure;
wenzelm
parents: 39137
diff changeset
    72
val show_question_marks_raw =
39116
f14735a88886 more explicit Config.declare vs. Config.declare_global;
wenzelm
parents: 39115
diff changeset
    73
  Config.declare "show_question_marks" (fn _ => Config.Bool (! show_question_marks_default));
39163
4d701c0388c3 more explicit indication of Config.raw options, which are only needed for bootstrapping Pure;
wenzelm
parents: 39137
diff changeset
    74
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
    75
14837
827c68f8267c added pp_show_brackets; support unbreakable blocks;
wenzelm
parents: 14783
diff changeset
    76
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    77
2200
2538977e94fa added print_mode: string list ref (order of printer tables);
wenzelm
parents: 1509
diff changeset
    78
(** type prtabs **)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    79
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    80
datatype symb =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    81
  Arg of int |
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    82
  TypArg of int |
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    83
  String of string |
23630
bc22daeed49e pretty: markup for syntax/name of authentic consts;
wenzelm
parents: 23615
diff changeset
    84
  Space of string |
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    85
  Break of int |
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    86
  Block of int * symb list;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    87
2200
2538977e94fa added print_mode: string list ref (order of printer tables);
wenzelm
parents: 1509
diff changeset
    88
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
    89
18957
8c3abd63bce3 TableFun: renamed xxx_multi to xxx_list;
wenzelm
parents: 18857
diff changeset
    90
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
    91
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
    92
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    93
3816
7e1b695bcc5e changed preference order of prtab entries;
wenzelm
parents: 3776
diff changeset
    94
(* xprod_to_fmt *)
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
    95
37216
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 35435
diff changeset
    96
fun xprod_to_fmt (Syn_Ext.XProd (_, _, "", _)) = NONE
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 35435
diff changeset
    97
  | xprod_to_fmt (Syn_Ext.XProd (_, xsymbs, const, pri)) =
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
    98
      let
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
    99
        fun arg (s, p) =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   100
          (if s = "type" then TypArg else Arg)
37216
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 35435
diff changeset
   101
          (if Lexicon.is_terminal s then Syn_Ext.max_pri else p);
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   102
37216
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 35435
diff changeset
   103
        fun xsyms_to_syms (Syn_Ext.Delim s :: xsyms) =
23630
bc22daeed49e pretty: markup for syntax/name of authentic consts;
wenzelm
parents: 23615
diff changeset
   104
              apfst (cons (String s)) (xsyms_to_syms xsyms)
37216
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 35435
diff changeset
   105
          | xsyms_to_syms (Syn_Ext.Argument s_p :: xsyms) =
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   106
              apfst (cons (arg s_p)) (xsyms_to_syms xsyms)
37216
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 35435
diff changeset
   107
          | xsyms_to_syms (Syn_Ext.Space s :: xsyms) =
23630
bc22daeed49e pretty: markup for syntax/name of authentic consts;
wenzelm
parents: 23615
diff changeset
   108
              apfst (cons (Space s)) (xsyms_to_syms xsyms)
37216
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 35435
diff changeset
   109
          | xsyms_to_syms (Syn_Ext.Bg i :: xsyms) =
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   110
              let
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   111
                val (bsyms, xsyms') = xsyms_to_syms xsyms;
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   112
                val (syms, xsyms'') = xsyms_to_syms xsyms';
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   113
              in
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   114
                (Block (i, bsyms) :: syms, xsyms'')
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   115
              end
37216
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 35435
diff changeset
   116
          | xsyms_to_syms (Syn_Ext.Brk i :: xsyms) =
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   117
              apfst (cons (Break i)) (xsyms_to_syms xsyms)
37216
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 35435
diff changeset
   118
          | xsyms_to_syms (Syn_Ext.En :: xsyms) = ([], xsyms)
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   119
          | xsyms_to_syms [] = ([], []);
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   120
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   121
        fun nargs (Arg _ :: syms) = nargs syms + 1
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   122
          | nargs (TypArg _ :: syms) = nargs syms + 1
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   123
          | nargs (String _ :: syms) = nargs syms
23630
bc22daeed49e pretty: markup for syntax/name of authentic consts;
wenzelm
parents: 23615
diff changeset
   124
          | nargs (Space _ :: syms) = nargs syms
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   125
          | nargs (Break _ :: syms) = nargs syms
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   126
          | nargs (Block (_, bsyms) :: syms) = nargs syms + nargs bsyms
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   127
          | nargs [] = 0;
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   128
      in
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   129
        (case xsyms_to_syms xsymbs of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   130
          (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
   131
        | _ => raise Fail "Unbalanced pretty-printing blocks")
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   132
      end;
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   133
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   134
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   135
(* empty, extend, merge prtabs *)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   136
2200
2538977e94fa added print_mode: string list ref (order of printer tables);
wenzelm
parents: 1509
diff changeset
   137
val empty_prtabs = [];
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   138
25393
0856e0141062 replaced extend_prtabs by update_prtabs (absorb duplicates);
wenzelm
parents: 25386
diff changeset
   139
fun update_prtabs mode xprods prtabs =
2200
2538977e94fa added print_mode: string list ref (order of printer tables);
wenzelm
parents: 1509
diff changeset
   140
  let
19482
9f11af8f7ef9 tuned basic list operators (flat, maps, map_filter);
wenzelm
parents: 19374
diff changeset
   141
    val fmts = map_filter xprod_to_fmt xprods;
25393
0856e0141062 replaced extend_prtabs by update_prtabs (absorb duplicates);
wenzelm
parents: 25386
diff changeset
   142
    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
   143
  in AList.update (op =) (mode, tab') prtabs end;
15753
eb014dfc57ee tuned extend_prtabs;
wenzelm
parents: 15574
diff changeset
   144
25386
82b62fe11d7a remove_prtabs: tuned, avoid excessive garbage;
wenzelm
parents: 24612
diff changeset
   145
fun remove_prtabs mode xprods prtabs =
82b62fe11d7a remove_prtabs: tuned, avoid excessive garbage;
wenzelm
parents: 24612
diff changeset
   146
  let
82b62fe11d7a remove_prtabs: tuned, avoid excessive garbage;
wenzelm
parents: 24612
diff changeset
   147
    val tab = mode_tab prtabs mode;
37216
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 35435
diff changeset
   148
    val fmts = map_filter (fn xprod as Syn_Ext.XProd (_, _, c, _) =>
25386
82b62fe11d7a remove_prtabs: tuned, avoid excessive garbage;
wenzelm
parents: 24612
diff changeset
   149
      if null (Symtab.lookup_list tab c) then NONE
82b62fe11d7a remove_prtabs: tuned, avoid excessive garbage;
wenzelm
parents: 24612
diff changeset
   150
      else xprod_to_fmt xprod) xprods;
25393
0856e0141062 replaced extend_prtabs by update_prtabs (absorb duplicates);
wenzelm
parents: 25386
diff changeset
   151
    val tab' = fold (Symtab.remove_list (op =)) fmts tab;
0856e0141062 replaced extend_prtabs by update_prtabs (absorb duplicates);
wenzelm
parents: 25386
diff changeset
   152
  in AList.update (op =) (mode, tab') prtabs end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   153
2200
2538977e94fa added print_mode: string list ref (order of printer tables);
wenzelm
parents: 1509
diff changeset
   154
fun merge_prtabs prtabs1 prtabs2 =
2538977e94fa added print_mode: string list ref (order of printer tables);
wenzelm
parents: 1509
diff changeset
   155
  let
19046
bc5c6c9b114e removed distinct, renamed gen_distinct to distinct;
wenzelm
parents: 18977
diff changeset
   156
    val modes = distinct (op =) (map fst (prtabs1 @ prtabs2));
18957
8c3abd63bce3 TableFun: renamed xxx_multi to xxx_list;
wenzelm
parents: 18857
diff changeset
   157
    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
   158
  in map merge modes end;
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   159
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   160
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   161
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   162
(** pretty term or typ asts **)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   163
42245
29e3967550d5 moved unparse material to syntax_phases.ML;
wenzelm
parents: 42048
diff changeset
   164
fun apply_trans ctxt fns args =
29e3967550d5 moved unparse material to syntax_phases.ML;
wenzelm
parents: 42048
diff changeset
   165
  let
29e3967550d5 moved unparse material to syntax_phases.ML;
wenzelm
parents: 42048
diff changeset
   166
    fun app_first [] = raise Match
29e3967550d5 moved unparse material to syntax_phases.ML;
wenzelm
parents: 42048
diff changeset
   167
      | app_first (f :: fs) = f ctxt args handle Match => app_first fs;
29e3967550d5 moved unparse material to syntax_phases.ML;
wenzelm
parents: 42048
diff changeset
   168
  in app_first fns end;
29e3967550d5 moved unparse material to syntax_phases.ML;
wenzelm
parents: 42048
diff changeset
   169
2200
2538977e94fa added print_mode: string list ref (order of printer tables);
wenzelm
parents: 1509
diff changeset
   170
fun is_chain [Block (_, pr)] = is_chain pr
2538977e94fa added print_mode: string list ref (order of printer tables);
wenzelm
parents: 1509
diff changeset
   171
  | is_chain [Arg _] = true
2538977e94fa added print_mode: string list ref (order of printer tables);
wenzelm
parents: 1509
diff changeset
   172
  | is_chain _  = false;
506
e0ca460d6e51 improved show_brackets again - Trueprop does not create () any more.
nipkow
parents: 505
diff changeset
   173
40956
95fe8598c0c9 added Syntax.pretty_priority;
wenzelm
parents: 39163
diff changeset
   174
val pretty_priority = Config.int (Config.declare "Syntax.pretty_priority" (K (Config.Int 0)));
95fe8598c0c9 added Syntax.pretty_priority;
wenzelm
parents: 39163
diff changeset
   175
95fe8598c0c9 added Syntax.pretty_priority;
wenzelm
parents: 39163
diff changeset
   176
fun pretty extern ctxt tabs trf tokentrf type_mode curried ast0 =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   177
  let
35429
afa8cf9e63d8 authentic syntax for classes and type constructors;
wenzelm
parents: 35262
diff changeset
   178
    val {extern_class, extern_type, extern_const} = extern;
39137
ccb53edd59f0 turned show_brackets into proper configuration option;
wenzelm
parents: 39134
diff changeset
   179
    val show_brackets = Config.get ctxt show_brackets;
35429
afa8cf9e63d8 authentic syntax for classes and type constructors;
wenzelm
parents: 35262
diff changeset
   180
26707
ddf6bab64b96 token translations: context dependent, result Pretty.T;
wenzelm
parents: 25393
diff changeset
   181
    fun token_trans a x =
ddf6bab64b96 token translations: context dependent, result Pretty.T;
wenzelm
parents: 25393
diff changeset
   182
      (case tokentrf a of
ddf6bab64b96 token translations: context dependent, result Pretty.T;
wenzelm
parents: 25393
diff changeset
   183
        NONE =>
37216
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 35435
diff changeset
   184
          if member (op =) Syn_Ext.standard_token_classes a
26707
ddf6bab64b96 token translations: context dependent, result Pretty.T;
wenzelm
parents: 25393
diff changeset
   185
          then SOME (Pretty.str x) else NONE
ddf6bab64b96 token translations: context dependent, result Pretty.T;
wenzelm
parents: 25393
diff changeset
   186
      | SOME f => SOME (f ctxt x));
2701
348ec44248df split ast_of_term(T);
wenzelm
parents: 2507
diff changeset
   187
2200
2538977e94fa added print_mode: string list ref (order of printer tables);
wenzelm
parents: 1509
diff changeset
   188
    (*default applications: prefix / postfix*)
2538977e94fa added print_mode: string list ref (order of printer tables);
wenzelm
parents: 1509
diff changeset
   189
    val appT =
37216
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 35435
diff changeset
   190
      if type_mode then Type_Ext.tappl_ast_tr'
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 35435
diff changeset
   191
      else if curried then Syn_Trans.applC_ast_tr'
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 35435
diff changeset
   192
      else Syn_Trans.appl_ast_tr';
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   193
23630
bc22daeed49e pretty: markup for syntax/name of authentic consts;
wenzelm
parents: 23615
diff changeset
   194
    fun synT _ ([], args) = ([], args)
bc22daeed49e pretty: markup for syntax/name of authentic consts;
wenzelm
parents: 23615
diff changeset
   195
      | synT markup (Arg p :: symbs, t :: args) =
bc22daeed49e pretty: markup for syntax/name of authentic consts;
wenzelm
parents: 23615
diff changeset
   196
          let val (Ts, args') = synT markup (symbs, args);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   197
          in (astT (t, p) @ Ts, args') end
23630
bc22daeed49e pretty: markup for syntax/name of authentic consts;
wenzelm
parents: 23615
diff changeset
   198
      | synT markup (TypArg p :: symbs, t :: args) =
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   199
          let
23630
bc22daeed49e pretty: markup for syntax/name of authentic consts;
wenzelm
parents: 23615
diff changeset
   200
            val (Ts, args') = synT markup (symbs, args);
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   201
          in
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   202
            if type_mode then (astT (t, p) @ Ts, args')
40956
95fe8598c0c9 added Syntax.pretty_priority;
wenzelm
parents: 39163
diff changeset
   203
            else
95fe8598c0c9 added Syntax.pretty_priority;
wenzelm
parents: 39163
diff changeset
   204
              (pretty extern (Config.put pretty_priority p ctxt)
95fe8598c0c9 added Syntax.pretty_priority;
wenzelm
parents: 39163
diff changeset
   205
                tabs trf tokentrf true curried t @ Ts, args')
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 62
diff changeset
   206
          end
23630
bc22daeed49e pretty: markup for syntax/name of authentic consts;
wenzelm
parents: 23615
diff changeset
   207
      | synT markup (String s :: symbs, args) =
bc22daeed49e pretty: markup for syntax/name of authentic consts;
wenzelm
parents: 23615
diff changeset
   208
          let val (Ts, args') = synT markup (symbs, args);
bc22daeed49e pretty: markup for syntax/name of authentic consts;
wenzelm
parents: 23615
diff changeset
   209
          in (markup (Pretty.str s) :: Ts, args') end
bc22daeed49e pretty: markup for syntax/name of authentic consts;
wenzelm
parents: 23615
diff changeset
   210
      | synT markup (Space s :: symbs, args) =
bc22daeed49e pretty: markup for syntax/name of authentic consts;
wenzelm
parents: 23615
diff changeset
   211
          let val (Ts, args') = synT markup (symbs, args);
8457
c5eb14ba754c use Pretty.str / Pretty.raw_str;
wenzelm
parents: 6767
diff changeset
   212
          in (Pretty.str s :: Ts, args') end
23630
bc22daeed49e pretty: markup for syntax/name of authentic consts;
wenzelm
parents: 23615
diff changeset
   213
      | synT markup (Block (i, bsymbs) :: symbs, args) =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   214
          let
23630
bc22daeed49e pretty: markup for syntax/name of authentic consts;
wenzelm
parents: 23615
diff changeset
   215
            val (bTs, args') = synT markup (bsymbs, args);
bc22daeed49e pretty: markup for syntax/name of authentic consts;
wenzelm
parents: 23615
diff changeset
   216
            val (Ts, args'') = synT markup (symbs, args');
14837
827c68f8267c added pp_show_brackets; support unbreakable blocks;
wenzelm
parents: 14783
diff changeset
   217
            val T =
827c68f8267c added pp_show_brackets; support unbreakable blocks;
wenzelm
parents: 14783
diff changeset
   218
              if i < 0 then Pretty.unbreakable (Pretty.block bTs)
827c68f8267c added pp_show_brackets; support unbreakable blocks;
wenzelm
parents: 14783
diff changeset
   219
              else Pretty.blk (i, bTs);
827c68f8267c added pp_show_brackets; support unbreakable blocks;
wenzelm
parents: 14783
diff changeset
   220
          in (T :: Ts, args'') end
23630
bc22daeed49e pretty: markup for syntax/name of authentic consts;
wenzelm
parents: 23615
diff changeset
   221
      | synT markup (Break i :: symbs, args) =
14837
827c68f8267c added pp_show_brackets; support unbreakable blocks;
wenzelm
parents: 14783
diff changeset
   222
          let
23630
bc22daeed49e pretty: markup for syntax/name of authentic consts;
wenzelm
parents: 23615
diff changeset
   223
            val (Ts, args') = synT markup (symbs, args);
14837
827c68f8267c added pp_show_brackets; support unbreakable blocks;
wenzelm
parents: 14783
diff changeset
   224
            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
   225
          in (T :: Ts, args') end
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   226
23630
bc22daeed49e pretty: markup for syntax/name of authentic consts;
wenzelm
parents: 23615
diff changeset
   227
    and parT markup (pr, args, p, p': int) = #1 (synT markup
39137
ccb53edd59f0 turned show_brackets into proper configuration option;
wenzelm
parents: 39134
diff changeset
   228
          (if p > p' orelse (show_brackets andalso p' <> Syn_Ext.max_pri andalso not (is_chain pr))
23630
bc22daeed49e pretty: markup for syntax/name of authentic consts;
wenzelm
parents: 23615
diff changeset
   229
            then [Block (1, Space "(" :: pr @ [Space ")"])]
554
c7d9018cc9e6 various minor internal changes;
wenzelm
parents: 506
diff changeset
   230
            else pr, args))
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   231
35429
afa8cf9e63d8 authentic syntax for classes and type constructors;
wenzelm
parents: 35262
diff changeset
   232
    and atomT a = a |> Lexicon.unmark
afa8cf9e63d8 authentic syntax for classes and type constructors;
wenzelm
parents: 35262
diff changeset
   233
     {case_class = fn c => Pretty.mark (Markup.tclass c) (Pretty.str (extern_class c)),
afa8cf9e63d8 authentic syntax for classes and type constructors;
wenzelm
parents: 35262
diff changeset
   234
      case_type = fn c => Pretty.mark (Markup.tycon c) (Pretty.str (extern_type c)),
afa8cf9e63d8 authentic syntax for classes and type constructors;
wenzelm
parents: 35262
diff changeset
   235
      case_const = fn c => Pretty.mark (Markup.const c) (Pretty.str (extern_const c)),
afa8cf9e63d8 authentic syntax for classes and type constructors;
wenzelm
parents: 35262
diff changeset
   236
      case_fixed = fn x => the (token_trans "_free" x),
afa8cf9e63d8 authentic syntax for classes and type constructors;
wenzelm
parents: 35262
diff changeset
   237
      case_default = Pretty.str}
19374
ae4a225e0c1f pretty: late externing of consts (support authentic syntax);
wenzelm
parents: 19046
diff changeset
   238
ae4a225e0c1f pretty: late externing of consts (support authentic syntax);
wenzelm
parents: 19046
diff changeset
   239
    and prefixT (_, a, [], _) = [atomT a]
16611
edb368e2878f proper treatment of advanced trfuns: pass thy argument;
wenzelm
parents: 15973
diff changeset
   240
      | prefixT (c, _, args, p) = astT (appT (c, args), p)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   241
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   242
    and splitT 0 ([x], ys) = (x, ys)
5691
3a6de95c09d0 no open;
wenzelm
parents: 4699
diff changeset
   243
      | splitT 0 (rev_xs, ys) = (Ast.Appl (rev rev_xs), ys)
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   244
      | 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
   245
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   246
    and combT (tup as (c, a, args, p)) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   247
      let
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   248
        val nargs = length args;
35429
afa8cf9e63d8 authentic syntax for classes and type constructors;
wenzelm
parents: 35262
diff changeset
   249
        val markup = a |> Lexicon.unmark
afa8cf9e63d8 authentic syntax for classes and type constructors;
wenzelm
parents: 35262
diff changeset
   250
         {case_class = Pretty.mark o Markup.tclass,
afa8cf9e63d8 authentic syntax for classes and type constructors;
wenzelm
parents: 35262
diff changeset
   251
          case_type = Pretty.mark o Markup.tycon,
afa8cf9e63d8 authentic syntax for classes and type constructors;
wenzelm
parents: 35262
diff changeset
   252
          case_const = Pretty.mark o Markup.const,
afa8cf9e63d8 authentic syntax for classes and type constructors;
wenzelm
parents: 35262
diff changeset
   253
          case_fixed = Pretty.mark o Markup.fixed,
afa8cf9e63d8 authentic syntax for classes and type constructors;
wenzelm
parents: 35262
diff changeset
   254
          case_default = K I};
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   255
2701
348ec44248df split ast_of_term(T);
wenzelm
parents: 2507
diff changeset
   256
        (*find matching table entry, or print as prefix / postfix*)
6280
218733fb6987 tuned pretty format lookup;
wenzelm
parents: 6273
diff changeset
   257
        fun prnt ([], []) = prefixT tup
18957
8c3abd63bce3 TableFun: renamed xxx_multi to xxx_list;
wenzelm
parents: 18857
diff changeset
   258
          | prnt ([], tb :: tbs) = prnt (Symtab.lookup_list tb a, tbs)
6280
218733fb6987 tuned pretty format lookup;
wenzelm
parents: 6273
diff changeset
   259
          | prnt ((pr, n, p') :: prnps, tbs) =
23630
bc22daeed49e pretty: markup for syntax/name of authentic consts;
wenzelm
parents: 23615
diff changeset
   260
              if nargs = n then parT markup (pr, args, p, p')
2200
2538977e94fa added print_mode: string list ref (order of printer tables);
wenzelm
parents: 1509
diff changeset
   261
              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
   262
                astT (appT (splitT n ([c], args)), p)
6280
218733fb6987 tuned pretty format lookup;
wenzelm
parents: 6273
diff changeset
   263
              else prnt (prnps, tbs);
2200
2538977e94fa added print_mode: string list ref (order of printer tables);
wenzelm
parents: 1509
diff changeset
   264
      in
26707
ddf6bab64b96 token translations: context dependent, result Pretty.T;
wenzelm
parents: 25393
diff changeset
   265
        (case tokentrT a args of
ddf6bab64b96 token translations: context dependent, result Pretty.T;
wenzelm
parents: 25393
diff changeset
   266
          SOME prt => [prt]
ddf6bab64b96 token translations: context dependent, result Pretty.T;
wenzelm
parents: 25393
diff changeset
   267
        | NONE => astT (apply_trans ctxt (trf a) args, p) handle Match => prnt ([], tabs))
2200
2538977e94fa added print_mode: string list ref (order of printer tables);
wenzelm
parents: 1509
diff changeset
   268
      end
2538977e94fa added print_mode: string list ref (order of printer tables);
wenzelm
parents: 1509
diff changeset
   269
26707
ddf6bab64b96 token translations: context dependent, result Pretty.T;
wenzelm
parents: 25393
diff changeset
   270
    and tokentrT a [Ast.Variable x] = token_trans a x
ddf6bab64b96 token translations: context dependent, result Pretty.T;
wenzelm
parents: 25393
diff changeset
   271
      | tokentrT _ _ = NONE
ddf6bab64b96 token translations: context dependent, result Pretty.T;
wenzelm
parents: 25393
diff changeset
   272
5691
3a6de95c09d0 no open;
wenzelm
parents: 4699
diff changeset
   273
    and astT (c as Ast.Constant a, p) = combT (c, a, [], p)
42048
afd11ca8e018 support for encoded positions (for id_position, longid_position) as pseudo type-constraints -- still inactive;
wenzelm
parents: 40956
diff changeset
   274
      | astT (ast as Ast.Variable x, _) = [Ast.pretty_ast ast]
6280
218733fb6987 tuned pretty format lookup;
wenzelm
parents: 6273
diff changeset
   275
      | 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
   276
      | astT (Ast.Appl (f :: (args as _ :: _)), p) = astT (appT (f, args), p)
3a6de95c09d0 no open;
wenzelm
parents: 4699
diff changeset
   277
      | astT (ast as Ast.Appl _, _) = raise Ast.AST ("pretty: malformed ast", [ast]);
40956
95fe8598c0c9 added Syntax.pretty_priority;
wenzelm
parents: 39163
diff changeset
   278
  in astT (ast0, Config.get ctxt pretty_priority) end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   279
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   280
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   281
(* pretty_term_ast *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   282
35429
afa8cf9e63d8 authentic syntax for classes and type constructors;
wenzelm
parents: 35262
diff changeset
   283
fun pretty_term_ast extern ctxt curried prtabs trf tokentrf ast =
40956
95fe8598c0c9 added Syntax.pretty_priority;
wenzelm
parents: 39163
diff changeset
   284
  pretty extern ctxt (mode_tabs prtabs (print_mode_value ())) trf tokentrf false curried ast;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   285
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   286
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   287
(* pretty_typ_ast *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   288
35429
afa8cf9e63d8 authentic syntax for classes and type constructors;
wenzelm
parents: 35262
diff changeset
   289
fun pretty_typ_ast {extern_class, extern_type} ctxt _ prtabs trf tokentrf ast =
afa8cf9e63d8 authentic syntax for classes and type constructors;
wenzelm
parents: 35262
diff changeset
   290
  pretty {extern_class = extern_class, extern_type = extern_type, extern_const = I}
40956
95fe8598c0c9 added Syntax.pretty_priority;
wenzelm
parents: 39163
diff changeset
   291
    ctxt (mode_tabs prtabs (print_mode_value ())) trf tokentrf true false ast;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   292
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   293
end;