author | wenzelm |
Sat, 12 Oct 2024 19:21:47 +0200 | |
changeset 81157 | fbe44afbd659 |
parent 81155 | 1e7b60cb7694 |
child 81158 | 06461d0d46e1 |
permissions | -rw-r--r-- |
18 | 1 |
(* Title: Pure/Syntax/printer.ML |
0 | 2 |
Author: Tobias Nipkow and Markus Wenzel, TU Muenchen |
18 | 3 |
|
4 |
Pretty printing of asts, terms, types and print (ast) translation. |
|
0 | 5 |
*) |
6 |
||
42289
dafae095d733
discontinued special status of structure Printer;
wenzelm
parents:
42288
diff
changeset
|
7 |
signature BASIC_PRINTER = |
2384 | 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 | 15 |
val pretty_priority: int Config.T |
2384 | 16 |
end; |
0 | 17 |
|
18 |
signature PRINTER = |
|
2384 | 19 |
sig |
42289
dafae095d733
discontinued special status of structure Printer;
wenzelm
parents:
42288
diff
changeset
|
20 |
include BASIC_PRINTER |
49657
40e4feac2921
turn constraints into Isabelle_Markup.typing, depending on show_markup options;
wenzelm
parents:
49655
diff
changeset
|
21 |
val show_markup_default: bool Unsynchronized.ref |
52210
0226035df99d
more explicit Printer.type_emphasis, depending on show_type_emphasis;
wenzelm
parents:
52185
diff
changeset
|
22 |
val show_type_emphasis: bool Config.T |
0226035df99d
more explicit Printer.type_emphasis, depending on show_type_emphasis;
wenzelm
parents:
52185
diff
changeset
|
23 |
val type_emphasis: Proof.context -> typ -> bool |
2200
2538977e94fa
added print_mode: string list ref (order of printer tables);
wenzelm
parents:
1509
diff
changeset
|
24 |
type prtabs |
69071
3ef82592dc22
clarified get_infix: avoid old ASCII input syntax;
wenzelm
parents:
67704
diff
changeset
|
25 |
datatype assoc = No_Assoc | Left_Assoc | Right_Assoc |
69078
a5e904112ea9
more accurate syntax: e.g. avoid brackets as prefix notation;
wenzelm
parents:
69077
diff
changeset
|
26 |
val get_prefix: prtabs -> Symtab.key -> string option |
a5e904112ea9
more accurate syntax: e.g. avoid brackets as prefix notation;
wenzelm
parents:
69077
diff
changeset
|
27 |
val get_binder: prtabs -> Symtab.key -> string option |
69071
3ef82592dc22
clarified get_infix: avoid old ASCII input syntax;
wenzelm
parents:
67704
diff
changeset
|
28 |
val get_infix: prtabs -> string -> {assoc: assoc, delim: string, pri: int} option |
2200
2538977e94fa
added print_mode: string list ref (order of printer tables);
wenzelm
parents:
1509
diff
changeset
|
29 |
val empty_prtabs: prtabs |
42288
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42284
diff
changeset
|
30 |
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
|
31 |
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
|
32 |
val merge_prtabs: prtabs -> prtabs -> prtabs |
42267
9566078ad905
simplified printer context: uniform externing and static token translations;
wenzelm
parents:
42262
diff
changeset
|
33 |
val pretty_term_ast: bool -> Proof.context -> prtabs -> |
42254 | 34 |
(string -> Proof.context -> Ast.ast list -> Ast.ast) -> |
49655 | 35 |
(string -> Ast.ast list -> Pretty.T option) -> |
80743 | 36 |
((string -> Markup.T list) * (string -> string)) -> |
42267
9566078ad905
simplified printer context: uniform externing and static token translations;
wenzelm
parents:
42262
diff
changeset
|
37 |
Ast.ast -> Pretty.T list |
9566078ad905
simplified printer context: uniform externing and static token translations;
wenzelm
parents:
42262
diff
changeset
|
38 |
val pretty_typ_ast: Proof.context -> prtabs -> |
42254 | 39 |
(string -> Proof.context -> Ast.ast list -> Ast.ast) -> |
49655 | 40 |
(string -> Ast.ast list -> Pretty.T option) -> |
80743 | 41 |
((string -> Markup.T list) * (string -> string)) -> |
42 |
Ast.ast -> Pretty.T list |
|
2384 | 43 |
end; |
0 | 44 |
|
2365 | 45 |
structure Printer: PRINTER = |
0 | 46 |
struct |
2200
2538977e94fa
added print_mode: string list ref (order of printer tables);
wenzelm
parents:
1509
diff
changeset
|
47 |
|
0 | 48 |
(** options for printing **) |
49 |
||
69575 | 50 |
val show_brackets = Config.declare_option_bool ("show_brackets", \<^here>); |
51 |
val show_types = Config.declare_option_bool ("show_types", \<^here>); |
|
52 |
val show_sorts = Config.declare_option_bool ("show_sorts", \<^here>); |
|
49657
40e4feac2921
turn constraints into Isabelle_Markup.typing, depending on show_markup options;
wenzelm
parents:
49655
diff
changeset
|
53 |
val show_markup_default = Unsynchronized.ref false; |
69575 | 54 |
val show_markup = Config.declare_bool ("show_markup", \<^here>) (fn _ => ! show_markup_default); |
55 |
val show_structs = Config.declare_bool ("show_structs", \<^here>) (K false); |
|
56 |
val show_question_marks = Config.declare_option_bool ("show_question_marks", \<^here>); |
|
57 |
val show_type_emphasis = Config.declare_bool ("show_type_emphasis", \<^here>) (K true); |
|
52210
0226035df99d
more explicit Printer.type_emphasis, depending on show_type_emphasis;
wenzelm
parents:
52185
diff
changeset
|
58 |
|
0226035df99d
more explicit Printer.type_emphasis, depending on show_type_emphasis;
wenzelm
parents:
52185
diff
changeset
|
59 |
fun type_emphasis ctxt T = |
0226035df99d
more explicit Printer.type_emphasis, depending on show_type_emphasis;
wenzelm
parents:
52185
diff
changeset
|
60 |
T <> dummyT andalso |
0226035df99d
more explicit Printer.type_emphasis, depending on show_type_emphasis;
wenzelm
parents:
52185
diff
changeset
|
61 |
(Config.get ctxt show_types orelse Config.get ctxt show_markup orelse |
67704 | 62 |
Config.get ctxt show_type_emphasis andalso not (is_Type T)); |
49690
a6814de45b69
more explicit show_type_constraint, show_sort_constraint;
wenzelm
parents:
49657
diff
changeset
|
63 |
|
14837
827c68f8267c
added pp_show_brackets; support unbreakable blocks;
wenzelm
parents:
14783
diff
changeset
|
64 |
|
0 | 65 |
|
2200
2538977e94fa
added print_mode: string list ref (order of printer tables);
wenzelm
parents:
1509
diff
changeset
|
66 |
(** type prtabs **) |
0 | 67 |
|
68 |
datatype symb = |
|
69 |
Arg of int | |
|
70 |
TypArg of int | |
|
80952
a61ed25ba155
clarified persistent datatype: more direct literal_markup, which also serves as a flag;
wenzelm
parents:
80951
diff
changeset
|
71 |
String of Markup.T list * string | |
0 | 72 |
Break of int | |
80893 | 73 |
Block of Syntax_Ext.block * symb list; |
0 | 74 |
|
2200
2538977e94fa
added print_mode: string list ref (order of printer tables);
wenzelm
parents:
1509
diff
changeset
|
75 |
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
|
76 |
|
69071
3ef82592dc22
clarified get_infix: avoid old ASCII input syntax;
wenzelm
parents:
67704
diff
changeset
|
77 |
fun mode_tab (prtabs: prtabs) mode = the_default Symtab.empty (AList.lookup (op =) prtabs mode); |
3ef82592dc22
clarified get_infix: avoid old ASCII input syntax;
wenzelm
parents:
67704
diff
changeset
|
78 |
fun mode_tabs (prtabs: prtabs) modes = map_filter (AList.lookup (op =) prtabs) (modes @ [""]); |
3ef82592dc22
clarified get_infix: avoid old ASCII input syntax;
wenzelm
parents:
67704
diff
changeset
|
79 |
|
69078
a5e904112ea9
more accurate syntax: e.g. avoid brackets as prefix notation;
wenzelm
parents:
69077
diff
changeset
|
80 |
fun lookup_default prtabs = Symtab.lookup_list (mode_tab prtabs ""); |
a5e904112ea9
more accurate syntax: e.g. avoid brackets as prefix notation;
wenzelm
parents:
69077
diff
changeset
|
81 |
|
69071
3ef82592dc22
clarified get_infix: avoid old ASCII input syntax;
wenzelm
parents:
67704
diff
changeset
|
82 |
|
69077
11529ae45786
more approximative prefix syntax, including binder;
wenzelm
parents:
69071
diff
changeset
|
83 |
(* approximative syntax *) |
69071
3ef82592dc22
clarified get_infix: avoid old ASCII input syntax;
wenzelm
parents:
67704
diff
changeset
|
84 |
|
3ef82592dc22
clarified get_infix: avoid old ASCII input syntax;
wenzelm
parents:
67704
diff
changeset
|
85 |
datatype assoc = No_Assoc | Left_Assoc | Right_Assoc; |
3ef82592dc22
clarified get_infix: avoid old ASCII input syntax;
wenzelm
parents:
67704
diff
changeset
|
86 |
|
69077
11529ae45786
more approximative prefix syntax, including binder;
wenzelm
parents:
69071
diff
changeset
|
87 |
local |
11529ae45786
more approximative prefix syntax, including binder;
wenzelm
parents:
69071
diff
changeset
|
88 |
|
11529ae45786
more approximative prefix syntax, including binder;
wenzelm
parents:
69071
diff
changeset
|
89 |
fun is_arg (Arg _) = true |
11529ae45786
more approximative prefix syntax, including binder;
wenzelm
parents:
69071
diff
changeset
|
90 |
| is_arg (TypArg _) = true |
11529ae45786
more approximative prefix syntax, including binder;
wenzelm
parents:
69071
diff
changeset
|
91 |
| is_arg _ = false; |
11529ae45786
more approximative prefix syntax, including binder;
wenzelm
parents:
69071
diff
changeset
|
92 |
|
69071
3ef82592dc22
clarified get_infix: avoid old ASCII input syntax;
wenzelm
parents:
67704
diff
changeset
|
93 |
fun is_space str = forall_string (fn s => s = " ") str; |
69077
11529ae45786
more approximative prefix syntax, including binder;
wenzelm
parents:
69071
diff
changeset
|
94 |
|
69079
fedacfd60fdb
more liberal: detect free-form infixes as well, e.g. Orderings.ord_class.less_eq;
wenzelm
parents:
69078
diff
changeset
|
95 |
fun clean symbs = symbs |> maps |
fedacfd60fdb
more liberal: detect free-form infixes as well, e.g. Orderings.ord_class.less_eq;
wenzelm
parents:
69078
diff
changeset
|
96 |
(fn Block (_, body) => clean body |
fedacfd60fdb
more liberal: detect free-form infixes as well, e.g. Orderings.ord_class.less_eq;
wenzelm
parents:
69078
diff
changeset
|
97 |
| symb as String (_, s) => if is_space s then [] else [symb] |
fedacfd60fdb
more liberal: detect free-form infixes as well, e.g. Orderings.ord_class.less_eq;
wenzelm
parents:
69078
diff
changeset
|
98 |
| symb => if is_arg symb then [symb] else []); |
69077
11529ae45786
more approximative prefix syntax, including binder;
wenzelm
parents:
69071
diff
changeset
|
99 |
|
11529ae45786
more approximative prefix syntax, including binder;
wenzelm
parents:
69071
diff
changeset
|
100 |
in |
69071
3ef82592dc22
clarified get_infix: avoid old ASCII input syntax;
wenzelm
parents:
67704
diff
changeset
|
101 |
|
69078
a5e904112ea9
more accurate syntax: e.g. avoid brackets as prefix notation;
wenzelm
parents:
69077
diff
changeset
|
102 |
fun get_prefix prtabs c = |
a5e904112ea9
more accurate syntax: e.g. avoid brackets as prefix notation;
wenzelm
parents:
69077
diff
changeset
|
103 |
lookup_default prtabs c |
a5e904112ea9
more accurate syntax: e.g. avoid brackets as prefix notation;
wenzelm
parents:
69077
diff
changeset
|
104 |
|> get_first (fn (symbs, _, _) => |
69079
fedacfd60fdb
more liberal: detect free-form infixes as well, e.g. Orderings.ord_class.less_eq;
wenzelm
parents:
69078
diff
changeset
|
105 |
(case clean symbs of |
69078
a5e904112ea9
more accurate syntax: e.g. avoid brackets as prefix notation;
wenzelm
parents:
69077
diff
changeset
|
106 |
String (_, d) :: rest => if forall is_arg rest then SOME d else NONE |
a5e904112ea9
more accurate syntax: e.g. avoid brackets as prefix notation;
wenzelm
parents:
69077
diff
changeset
|
107 |
| _ => NONE)); |
a5e904112ea9
more accurate syntax: e.g. avoid brackets as prefix notation;
wenzelm
parents:
69077
diff
changeset
|
108 |
|
a5e904112ea9
more accurate syntax: e.g. avoid brackets as prefix notation;
wenzelm
parents:
69077
diff
changeset
|
109 |
fun get_binder prtabs c = |
a5e904112ea9
more accurate syntax: e.g. avoid brackets as prefix notation;
wenzelm
parents:
69077
diff
changeset
|
110 |
lookup_default prtabs (Mixfix.binder_name c) |
a5e904112ea9
more accurate syntax: e.g. avoid brackets as prefix notation;
wenzelm
parents:
69077
diff
changeset
|
111 |
|> get_first (fn (symbs, _, _) => |
69079
fedacfd60fdb
more liberal: detect free-form infixes as well, e.g. Orderings.ord_class.less_eq;
wenzelm
parents:
69078
diff
changeset
|
112 |
(case clean symbs of |
fedacfd60fdb
more liberal: detect free-form infixes as well, e.g. Orderings.ord_class.less_eq;
wenzelm
parents:
69078
diff
changeset
|
113 |
String (_, d) :: _ => SOME d |
69078
a5e904112ea9
more accurate syntax: e.g. avoid brackets as prefix notation;
wenzelm
parents:
69077
diff
changeset
|
114 |
| _ => NONE)); |
a5e904112ea9
more accurate syntax: e.g. avoid brackets as prefix notation;
wenzelm
parents:
69077
diff
changeset
|
115 |
|
69071
3ef82592dc22
clarified get_infix: avoid old ASCII input syntax;
wenzelm
parents:
67704
diff
changeset
|
116 |
fun get_infix prtabs c = |
69078
a5e904112ea9
more accurate syntax: e.g. avoid brackets as prefix notation;
wenzelm
parents:
69077
diff
changeset
|
117 |
lookup_default prtabs c |
69071
3ef82592dc22
clarified get_infix: avoid old ASCII input syntax;
wenzelm
parents:
67704
diff
changeset
|
118 |
|> map_filter (fn (symbs, _, p) => |
69079
fedacfd60fdb
more liberal: detect free-form infixes as well, e.g. Orderings.ord_class.less_eq;
wenzelm
parents:
69078
diff
changeset
|
119 |
(case clean symbs of |
fedacfd60fdb
more liberal: detect free-form infixes as well, e.g. Orderings.ord_class.less_eq;
wenzelm
parents:
69078
diff
changeset
|
120 |
[Arg p1, String (_, d), Arg p2] => SOME (p1, p2, d, p) |
fedacfd60fdb
more liberal: detect free-form infixes as well, e.g. Orderings.ord_class.less_eq;
wenzelm
parents:
69078
diff
changeset
|
121 |
| [TypArg p1, String (_, d), TypArg p2] => SOME (p1, p2, d, p) |
69071
3ef82592dc22
clarified get_infix: avoid old ASCII input syntax;
wenzelm
parents:
67704
diff
changeset
|
122 |
| _ => NONE)) |
3ef82592dc22
clarified get_infix: avoid old ASCII input syntax;
wenzelm
parents:
67704
diff
changeset
|
123 |
|> get_first (fn (p1, p2, d, p) => |
3ef82592dc22
clarified get_infix: avoid old ASCII input syntax;
wenzelm
parents:
67704
diff
changeset
|
124 |
if p1 = p + 1 andalso p2 = p + 1 then SOME {assoc = No_Assoc, delim = d, pri = p} |
3ef82592dc22
clarified get_infix: avoid old ASCII input syntax;
wenzelm
parents:
67704
diff
changeset
|
125 |
else if p1 = p andalso p2 = p + 1 then SOME {assoc = Left_Assoc, delim = d, pri = p} |
3ef82592dc22
clarified get_infix: avoid old ASCII input syntax;
wenzelm
parents:
67704
diff
changeset
|
126 |
else if p1 = p + 1 andalso p2 = p then SOME {assoc = Right_Assoc, delim = d, pri = p} |
3ef82592dc22
clarified get_infix: avoid old ASCII input syntax;
wenzelm
parents:
67704
diff
changeset
|
127 |
else NONE); |
2200
2538977e94fa
added print_mode: string list ref (order of printer tables);
wenzelm
parents:
1509
diff
changeset
|
128 |
|
69077
11529ae45786
more approximative prefix syntax, including binder;
wenzelm
parents:
69071
diff
changeset
|
129 |
end; |
11529ae45786
more approximative prefix syntax, including binder;
wenzelm
parents:
69071
diff
changeset
|
130 |
|
18 | 131 |
|
3816 | 132 |
(* xprod_to_fmt *) |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
133 |
|
80962 | 134 |
fun make_string s = String ([], s); |
135 |
fun make_literal s = String (Lexicon.literal_markup s, s); |
|
80954 | 136 |
|
137 |
fun make_arg (s, p) = |
|
138 |
(if s = "type" then TypArg else Arg) |
|
139 |
(if Lexicon.is_terminal s then 1000 else p); |
|
140 |
||
42288
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42284
diff
changeset
|
141 |
fun xprod_to_fmt (Syntax_Ext.XProd (_, _, "", _)) = NONE |
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42284
diff
changeset
|
142 |
| 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
|
143 |
let |
80954 | 144 |
fun make_symbs (Syntax_Ext.Delim s :: xsyms) = make_symbs xsyms |>> cons (make_literal s) |
145 |
| make_symbs (Syntax_Ext.Argument a :: xsyms) = make_symbs xsyms |>> cons (make_arg a) |
|
146 |
| make_symbs (Syntax_Ext.Space s :: xsyms) = make_symbs xsyms |>> cons (make_string s) |
|
80951 | 147 |
| make_symbs (Syntax_Ext.Bg block :: xsyms) = |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
148 |
let |
80951 | 149 |
val (bsyms, xsyms') = make_symbs xsyms; |
150 |
val (syms, xsyms'') = make_symbs xsyms'; |
|
80954 | 151 |
in (Block (block, bsyms) :: syms, xsyms'') end |
152 |
| make_symbs (Syntax_Ext.Brk i :: xsyms) = make_symbs xsyms |>> cons (Break i) |
|
80951 | 153 |
| make_symbs (Syntax_Ext.En :: xsyms) = ([], xsyms) |
154 |
| make_symbs [] = ([], []); |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
155 |
|
80954 | 156 |
fun count_args (Arg _ :: syms) = Integer.add 1 #> count_args syms |
157 |
| count_args (TypArg _ :: syms) = Integer.add 1 #> count_args syms |
|
158 |
| count_args (String _ :: syms) = count_args syms |
|
159 |
| count_args (Break _ :: syms) = count_args syms |
|
160 |
| count_args (Block (_, bsyms) :: syms) = count_args syms #> count_args bsyms |
|
161 |
| count_args [] = I; |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
162 |
in |
80951 | 163 |
(case make_symbs xsymbs of |
80954 | 164 |
(symbs, []) => SOME (const, (symbs, count_args symbs 0, 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
|
165 |
| _ => raise Fail "Unbalanced pretty-printing blocks") |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
166 |
end; |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
167 |
|
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
168 |
|
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
169 |
(* empty, extend, merge prtabs *) |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
170 |
|
2200
2538977e94fa
added print_mode: string list ref (order of printer tables);
wenzelm
parents:
1509
diff
changeset
|
171 |
val empty_prtabs = []; |
18 | 172 |
|
25393
0856e0141062
replaced extend_prtabs by update_prtabs (absorb duplicates);
wenzelm
parents:
25386
diff
changeset
|
173 |
fun update_prtabs mode xprods prtabs = |
2200
2538977e94fa
added print_mode: string list ref (order of printer tables);
wenzelm
parents:
1509
diff
changeset
|
174 |
let |
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19374
diff
changeset
|
175 |
val fmts = map_filter xprod_to_fmt xprods; |
25393
0856e0141062
replaced extend_prtabs by update_prtabs (absorb duplicates);
wenzelm
parents:
25386
diff
changeset
|
176 |
val tab' = fold (Symtab.update_list (op =)) fmts (mode_tab prtabs mode); |
25386 | 177 |
in AList.update (op =) (mode, tab') prtabs end; |
15753 | 178 |
|
25386 | 179 |
fun remove_prtabs mode xprods prtabs = |
180 |
let |
|
181 |
val tab = mode_tab prtabs mode; |
|
42288
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42284
diff
changeset
|
182 |
val fmts = map_filter (fn xprod as Syntax_Ext.XProd (_, _, c, _) => |
25386 | 183 |
if null (Symtab.lookup_list tab c) then NONE |
184 |
else xprod_to_fmt xprod) xprods; |
|
25393
0856e0141062
replaced extend_prtabs by update_prtabs (absorb duplicates);
wenzelm
parents:
25386
diff
changeset
|
185 |
val tab' = fold (Symtab.remove_list (op =)) fmts tab; |
0856e0141062
replaced extend_prtabs by update_prtabs (absorb duplicates);
wenzelm
parents:
25386
diff
changeset
|
186 |
in AList.update (op =) (mode, tab') prtabs end; |
0 | 187 |
|
2200
2538977e94fa
added print_mode: string list ref (order of printer tables);
wenzelm
parents:
1509
diff
changeset
|
188 |
fun merge_prtabs prtabs1 prtabs2 = |
2538977e94fa
added print_mode: string list ref (order of printer tables);
wenzelm
parents:
1509
diff
changeset
|
189 |
let |
19046
bc5c6c9b114e
removed distinct, renamed gen_distinct to distinct;
wenzelm
parents:
18977
diff
changeset
|
190 |
val modes = distinct (op =) (map fst (prtabs1 @ prtabs2)); |
18957 | 191 |
fun merge m = (m, Symtab.merge_list (op =) (mode_tab prtabs1 m, mode_tab prtabs2 m)); |
12292 | 192 |
in map merge modes end; |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
193 |
|
0 | 194 |
|
195 |
||
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
196 |
(** pretty term or typ asts **) |
0 | 197 |
|
2200
2538977e94fa
added print_mode: string list ref (order of printer tables);
wenzelm
parents:
1509
diff
changeset
|
198 |
fun is_chain [Block (_, pr)] = is_chain pr |
2538977e94fa
added print_mode: string list ref (order of printer tables);
wenzelm
parents:
1509
diff
changeset
|
199 |
| is_chain [Arg _] = true |
2538977e94fa
added print_mode: string list ref (order of printer tables);
wenzelm
parents:
1509
diff
changeset
|
200 |
| is_chain _ = false; |
506
e0ca460d6e51
improved show_brackets again - Trueprop does not create () any more.
nipkow
parents:
505
diff
changeset
|
201 |
|
80741 | 202 |
val pretty_type_mode = Config.declare_bool ("Syntax.pretty_type_mode", \<^here>) (K false); |
203 |
val pretty_curried = Config.declare_bool ("Syntax.pretty_curried", \<^here>) (K false); |
|
69575 | 204 |
val pretty_priority = Config.declare_int ("Syntax.pretty_priority", \<^here>) (K 0); |
40956 | 205 |
|
80955 | 206 |
local |
207 |
||
208 |
val par_block = Syntax_Ext.block_indent 1; |
|
80956 | 209 |
val par_bg = make_literal "("; |
210 |
val par_en = make_literal ")"; |
|
80955 | 211 |
|
212 |
in |
|
213 |
||
80741 | 214 |
fun pretty ctxt tabs trf markup_trans markup_extern ast0 = |
0 | 215 |
let |
80741 | 216 |
val type_mode = Config.get ctxt pretty_type_mode; |
217 |
val curried = Config.get ctxt pretty_curried; |
|
39137
ccb53edd59f0
turned show_brackets into proper configuration option;
wenzelm
parents:
39134
diff
changeset
|
218 |
val show_brackets = Config.get ctxt show_brackets; |
35429
afa8cf9e63d8
authentic syntax for classes and type constructors;
wenzelm
parents:
35262
diff
changeset
|
219 |
|
81157 | 220 |
val application = |
42284 | 221 |
if type_mode then Syntax_Trans.tappl_ast_tr' |
222 |
else if curried then Syntax_Trans.applC_ast_tr' |
|
223 |
else Syntax_Trans.appl_ast_tr'; |
|
18 | 224 |
|
81157 | 225 |
fun syntax _ ([], args) = ([], args) |
226 |
| syntax m (Arg p :: symbs, t :: args) = |
|
227 |
let val (Ts, args') = syntax m (symbs, args); |
|
228 |
in (main (t, p) @ Ts, args') end |
|
229 |
| syntax m (TypArg p :: symbs, t :: args) = |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
230 |
let |
81157 | 231 |
val (Ts, args') = syntax m (symbs, args); |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
232 |
in |
81157 | 233 |
if type_mode then (main (t, p) @ Ts, args') |
40956 | 234 |
else |
80741 | 235 |
let val ctxt' = ctxt |
236 |
|> Config.put pretty_type_mode true |
|
237 |
|> Config.put pretty_priority p |
|
238 |
in (pretty ctxt' tabs trf markup_trans markup_extern t @ Ts, args') end |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
239 |
end |
81157 | 240 |
| syntax m (String (literal_markup, 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
|
241 |
let |
81157 | 242 |
val (Ts, args') = syntax m (symbs, args); |
80952
a61ed25ba155
clarified persistent datatype: more direct literal_markup, which also serves as a flag;
wenzelm
parents:
80951
diff
changeset
|
243 |
val T = Pretty.marks_str (if null literal_markup then [] else m @ literal_markup, 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
|
244 |
in (T :: Ts, args') end |
81157 | 245 |
| syntax m (Block (block, bsymbs) :: symbs, args) = |
0 | 246 |
let |
81121
7cacedbddba7
support for pretty blocks that are "open" and thus have no impact on formatting, only on markup;
wenzelm
parents:
80962
diff
changeset
|
247 |
val {markup, open_block, consistent, unbreakable, indent} = block; |
81157 | 248 |
val (bTs, args') = syntax m (bsymbs, args); |
249 |
val (Ts, args'') = syntax m (symbs, args'); |
|
81121
7cacedbddba7
support for pretty blocks that are "open" and thus have no impact on formatting, only on markup;
wenzelm
parents:
80962
diff
changeset
|
250 |
val prt_block = |
7cacedbddba7
support for pretty blocks that are "open" and thus have no impact on formatting, only on markup;
wenzelm
parents:
80962
diff
changeset
|
251 |
{markup = markup, open_block = open_block, consistent = consistent, indent = indent}; |
7cacedbddba7
support for pretty blocks that are "open" and thus have no impact on formatting, only on markup;
wenzelm
parents:
80962
diff
changeset
|
252 |
val T = Pretty.markup_blocks prt_block bTs |> unbreakable ? Pretty.unbreakable; |
14837
827c68f8267c
added pp_show_brackets; support unbreakable blocks;
wenzelm
parents:
14783
diff
changeset
|
253 |
in (T :: Ts, args'') end |
81157 | 254 |
| syntax m (Break i :: symbs, args) = |
14837
827c68f8267c
added pp_show_brackets; support unbreakable blocks;
wenzelm
parents:
14783
diff
changeset
|
255 |
let |
81157 | 256 |
val (Ts, args') = syntax m (symbs, args); |
14837
827c68f8267c
added pp_show_brackets; support unbreakable blocks;
wenzelm
parents:
14783
diff
changeset
|
257 |
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
|
258 |
in (T :: Ts, args') end |
0 | 259 |
|
81157 | 260 |
and parens m (pr, args, p, p') = #1 (syntax m |
80955 | 261 |
(if p > p' orelse (show_brackets andalso p' <> 1000 andalso not (is_chain pr)) |
262 |
then [Block (par_block, par_bg :: pr @ [par_en])] else pr, args)) |
|
0 | 263 |
|
81157 | 264 |
and split_args 0 ([x], ys) = (x, ys) |
265 |
| split_args 0 (rev_xs, ys) = (Ast.Appl (rev rev_xs), ys) |
|
266 |
| split_args n (rev_xs, y :: ys) = split_args (n - 1) (y :: rev_xs, ys) |
|
19374
ae4a225e0c1f
pretty: late externing of consts (support authentic syntax);
wenzelm
parents:
19046
diff
changeset
|
267 |
|
81157 | 268 |
and translation a args p = |
81154 | 269 |
(case markup_trans a args of |
270 |
SOME prt => SOME [prt] |
|
81157 | 271 |
| NONE => Option.map main (SOME (trf a ctxt args, p) handle Match => NONE)) |
81154 | 272 |
|
81157 | 273 |
and combination c a args p = |
274 |
(case translation a args p of |
|
81154 | 275 |
SOME res => res |
276 |
| NONE => |
|
81155 | 277 |
(*find matching table entry, or print as prefix / postfix*) |
81154 | 278 |
let |
279 |
val nargs = length args; |
|
81155 | 280 |
val entry = |
281 |
tabs |> get_first (fn tab => |
|
282 |
Symtab.lookup_list tab a |> find_first (fn (_, n, _) => |
|
283 |
nargs = n orelse nargs > n andalso not type_mode)); |
|
284 |
in |
|
285 |
(case entry of |
|
286 |
NONE => |
|
81157 | 287 |
if nargs = 0 then [Pretty.marks_str (#1 markup_extern a, #2 markup_extern a)] |
288 |
else main (application (c, args), p) |
|
81155 | 289 |
| SOME (pr, n, p') => |
81157 | 290 |
if nargs = n then parens (#1 markup_extern a) (pr, args, p, p') |
291 |
else main (application (split_args n ([c], args)), p)) |
|
81155 | 292 |
end) |
2200
2538977e94fa
added print_mode: string list ref (order of printer tables);
wenzelm
parents:
1509
diff
changeset
|
293 |
|
81157 | 294 |
and main (Ast.Variable x, _) = [Ast.pretty_var x] |
295 |
| main (c as Ast.Constant a, p) = combination c a [] p |
|
296 |
| main (Ast.Appl ((c as Ast.Constant a) :: (args as _ :: _)), p) = combination c a args p |
|
297 |
| main (Ast.Appl (f :: (args as _ :: _)), p) = main (application (f, args), p) |
|
298 |
| main (ast as Ast.Appl _, _) = raise Ast.AST ("pretty: malformed ast", [ast]); |
|
299 |
in main (ast0, Config.get ctxt pretty_priority) end; |
|
0 | 300 |
|
80955 | 301 |
end; |
302 |
||
0 | 303 |
|
304 |
(* pretty_term_ast *) |
|
305 |
||
80743 | 306 |
fun pretty_term_ast curried ctxt prtabs trf markup_trans markup_extern ast = |
80741 | 307 |
let val ctxt' = ctxt |
308 |
|> Config.put pretty_type_mode false |
|
309 |
|> Config.put pretty_curried curried |
|
80743 | 310 |
in pretty ctxt' (mode_tabs prtabs (print_mode_value ())) trf markup_trans markup_extern ast end; |
0 | 311 |
|
312 |
||
313 |
(* pretty_typ_ast *) |
|
314 |
||
80743 | 315 |
fun pretty_typ_ast ctxt prtabs trf markup_trans markup_extern ast = |
80741 | 316 |
let val ctxt' = ctxt |
317 |
|> Config.put pretty_type_mode true |
|
318 |
|> Config.put pretty_curried false |
|
80743 | 319 |
in pretty ctxt' (mode_tabs prtabs (print_mode_value ())) trf markup_trans markup_extern ast end; |
0 | 320 |
|
321 |
end; |
|
42289
dafae095d733
discontinued special status of structure Printer;
wenzelm
parents:
42288
diff
changeset
|
322 |
|
dafae095d733
discontinued special status of structure Printer;
wenzelm
parents:
42288
diff
changeset
|
323 |
structure Basic_Printer: BASIC_PRINTER = Printer; |
dafae095d733
discontinued special status of structure Printer;
wenzelm
parents:
42288
diff
changeset
|
324 |
open Basic_Printer; |
dafae095d733
discontinued special status of structure Printer;
wenzelm
parents:
42288
diff
changeset
|
325 |