author | wenzelm |
Sat, 29 May 2004 15:07:29 +0200 | |
changeset 14837 | 827c68f8267c |
parent 14783 | e7f7ed4c06f2 |
child 14975 | 2736b0984253 |
permissions | -rw-r--r-- |
18 | 1 |
(* Title: Pure/Syntax/printer.ML |
0 | 2 |
ID: $Id$ |
3 |
Author: Tobias Nipkow and Markus Wenzel, TU Muenchen |
|
12785 | 4 |
License: GPL (GNU GENERAL PUBLIC LICENSE) |
18 | 5 |
|
6 |
Pretty printing of asts, terms, types and print (ast) translation. |
|
0 | 7 |
*) |
8 |
||
9 |
signature PRINTER0 = |
|
2384 | 10 |
sig |
504
a4f09493d929
added flag show_brackets for printinmg fully bracketed terms.
nipkow
parents:
381
diff
changeset
|
11 |
val show_brackets: bool ref |
a4f09493d929
added flag show_brackets for printinmg fully bracketed terms.
nipkow
parents:
381
diff
changeset
|
12 |
val show_sorts: bool ref |
0 | 13 |
val show_types: bool ref |
617 | 14 |
val show_no_free_types: bool ref |
14176
716fec31f9ea
Added show_all_types flag, such that all type information in the term
skalberg
parents:
12785
diff
changeset
|
15 |
val show_all_types: bool ref |
14837
827c68f8267c
added pp_show_brackets; support unbreakable blocks;
wenzelm
parents:
14783
diff
changeset
|
16 |
val pp_show_brackets: Pretty.pp -> Pretty.pp |
2384 | 17 |
end; |
0 | 18 |
|
19 |
signature PRINTER = |
|
2384 | 20 |
sig |
0 | 21 |
include PRINTER0 |
5691 | 22 |
val term_to_ast: (string -> (bool -> typ -> term list -> term) list) -> term -> Ast.ast |
23 |
val typ_to_ast: (string -> (bool -> typ -> term list -> term) list) -> typ -> Ast.ast |
|
24 |
val sort_to_ast: (string -> (bool -> typ -> term list -> term) list) -> sort -> Ast.ast |
|
2200
2538977e94fa
added print_mode: string list ref (order of printer tables);
wenzelm
parents:
1509
diff
changeset
|
25 |
type prtabs |
2538977e94fa
added print_mode: string list ref (order of printer tables);
wenzelm
parents:
1509
diff
changeset
|
26 |
val empty_prtabs: prtabs |
2538977e94fa
added print_mode: string list ref (order of printer tables);
wenzelm
parents:
1509
diff
changeset
|
27 |
val extend_prtabs: prtabs -> string -> SynExt.xprod list -> prtabs |
2538977e94fa
added print_mode: string list ref (order of printer tables);
wenzelm
parents:
1509
diff
changeset
|
28 |
val merge_prtabs: prtabs -> prtabs -> prtabs |
2538977e94fa
added print_mode: string list ref (order of printer tables);
wenzelm
parents:
1509
diff
changeset
|
29 |
val pretty_term_ast: bool -> prtabs |
5691 | 30 |
-> (string -> (Ast.ast list -> Ast.ast) list) |
6322 | 31 |
-> (string -> (string -> string * real) option) -> Ast.ast -> Pretty.T |
2200
2538977e94fa
added print_mode: string list ref (order of printer tables);
wenzelm
parents:
1509
diff
changeset
|
32 |
val pretty_typ_ast: bool -> prtabs |
5691 | 33 |
-> (string -> (Ast.ast list -> Ast.ast) list) |
6322 | 34 |
-> (string -> (string -> string * real) option) -> Ast.ast -> Pretty.T |
2384 | 35 |
end; |
0 | 36 |
|
2365 | 37 |
structure Printer: PRINTER = |
0 | 38 |
struct |
2200
2538977e94fa
added print_mode: string list ref (order of printer tables);
wenzelm
parents:
1509
diff
changeset
|
39 |
|
2538977e94fa
added print_mode: string list ref (order of printer tables);
wenzelm
parents:
1509
diff
changeset
|
40 |
|
0 | 41 |
(** options for printing **) |
42 |
||
43 |
val show_types = ref false; |
|
44 |
val show_sorts = ref false; |
|
504
a4f09493d929
added flag show_brackets for printinmg fully bracketed terms.
nipkow
parents:
381
diff
changeset
|
45 |
val show_brackets = ref false; |
617 | 46 |
val show_no_free_types = ref false; |
14176
716fec31f9ea
Added show_all_types flag, such that all type information in the term
skalberg
parents:
12785
diff
changeset
|
47 |
val show_all_types = ref false; |
0 | 48 |
|
14837
827c68f8267c
added pp_show_brackets; support unbreakable blocks;
wenzelm
parents:
14783
diff
changeset
|
49 |
fun pp_show_brackets pp = Pretty.pp (setmp show_brackets true (Pretty.term pp)) |
827c68f8267c
added pp_show_brackets; support unbreakable blocks;
wenzelm
parents:
14783
diff
changeset
|
50 |
(Pretty.typ pp) (Pretty.sort pp) (Pretty.classrel pp) (Pretty.arity pp); |
827c68f8267c
added pp_show_brackets; support unbreakable blocks;
wenzelm
parents:
14783
diff
changeset
|
51 |
|
827c68f8267c
added pp_show_brackets; support unbreakable blocks;
wenzelm
parents:
14783
diff
changeset
|
52 |
|
0 | 53 |
|
2701 | 54 |
(** misc utils **) |
55 |
||
56 |
(* apply print (ast) translation function *) |
|
0 | 57 |
|
12292 | 58 |
fun apply_first [] x = raise Match |
59 |
| apply_first (f :: fs) x = f x handle Match => apply_first fs x; |
|
5691 | 60 |
|
61 |
fun apply_trans name a fs args = |
|
12292 | 62 |
(apply_first fs args handle |
0 | 63 |
Match => raise Match |
9910
f025cf787554
internalize error "insufficient syntax for prefix application";
wenzelm
parents:
8457
diff
changeset
|
64 |
| exn => (priority ("Error in " ^ name ^ " for " ^ quote a); raise exn)); |
0 | 65 |
|
5691 | 66 |
fun apply_typed x y fs = map (fn f => f x y) fs; |
4147 | 67 |
|
0 | 68 |
|
2701 | 69 |
(* simple_ast_of *) |
70 |
||
5691 | 71 |
fun simple_ast_of (Const (c, _)) = Ast.Constant c |
72 |
| simple_ast_of (Free (x, _)) = Ast.Variable x |
|
14783 | 73 |
| simple_ast_of (Var (xi, _)) = Ast.Variable (Term.string_of_vname xi) |
2701 | 74 |
| simple_ast_of (t as _ $ _) = |
75 |
let val (f, args) = strip_comb t in |
|
5691 | 76 |
Ast.mk_appl (simple_ast_of f) (map simple_ast_of args) |
2701 | 77 |
end |
5691 | 78 |
| simple_ast_of (Bound i) = Ast.Variable ("B." ^ string_of_int i) |
2701 | 79 |
| simple_ast_of (Abs _) = sys_error "simple_ast_of: Abs"; |
80 |
||
81 |
||
82 |
||
3776 | 83 |
(** sort_to_ast, typ_to_ast **) |
2701 | 84 |
|
85 |
fun ast_of_termT trf tm = |
|
0 | 86 |
let |
2701 | 87 |
fun ast_of (t as Const ("_class", _) $ Free _) = simple_ast_of t |
88 |
| ast_of (t as Const ("_tfree", _) $ Free _) = simple_ast_of t |
|
89 |
| ast_of (t as Const ("_tvar", _) $ Var _) = simple_ast_of t |
|
90 |
| ast_of (Const (a, _)) = trans a [] |
|
91 |
| ast_of (t as _ $ _) = |
|
92 |
(case strip_comb t of |
|
93 |
(Const (a, _), args) => trans a args |
|
5691 | 94 |
| (f, args) => Ast.Appl (map ast_of (f :: args))) |
95 |
| ast_of t = simple_ast_of t |
|
2701 | 96 |
and trans a args = |
5691 | 97 |
ast_of (apply_trans "print translation" a (apply_typed false dummyT (trf a)) args) |
98 |
handle Match => Ast.mk_appl (Ast.Constant a) (map ast_of args); |
|
99 |
in ast_of tm end; |
|
617 | 100 |
|
5691 | 101 |
fun sort_to_ast trf S = ast_of_termT trf (TypeExt.term_of_sort S); |
102 |
fun typ_to_ast trf T = ast_of_termT trf (TypeExt.term_of_typ (! show_sorts) T); |
|
2701 | 103 |
|
104 |
||
105 |
||
106 |
(** term_to_ast **) |
|
107 |
||
11795 | 108 |
fun mark_freevars ((t as Const (c, _)) $ u) = |
109 |
if c mem_string TokenTrans.standard_token_markers then (t $ u) |
|
110 |
else t $ mark_freevars u |
|
111 |
| mark_freevars (t $ u) = mark_freevars t $ mark_freevars u |
|
2701 | 112 |
| mark_freevars (Abs (x, T, t)) = Abs (x, T, mark_freevars t) |
5691 | 113 |
| mark_freevars (t as Free _) = Lexicon.const "_free" $ t |
6767 | 114 |
| mark_freevars (t as Var (xi, T)) = |
115 |
if xi = SynExt.dddot_indexname then Const ("_DDDOT", T) |
|
116 |
else Lexicon.const "_var" $ t |
|
2701 | 117 |
| mark_freevars a = a; |
118 |
||
14696 | 119 |
fun ast_of_term trf show_all_types no_freeTs show_types show_sorts tm = |
2701 | 120 |
let |
0 | 121 |
fun prune_typs (t_seen as (Const _, _)) = t_seen |
122 |
| prune_typs (t as Free (x, ty), seen) = |
|
123 |
if ty = dummyT then (t, seen) |
|
5691 | 124 |
else if no_freeTs orelse t mem seen then (Lexicon.free x, seen) |
0 | 125 |
else (t, t :: seen) |
126 |
| prune_typs (t as Var (xi, ty), seen) = |
|
127 |
if ty = dummyT then (t, seen) |
|
5691 | 128 |
else if no_freeTs orelse t mem seen then (Lexicon.var xi, seen) |
0 | 129 |
else (t, t :: seen) |
130 |
| prune_typs (t_seen as (Bound _, _)) = t_seen |
|
131 |
| prune_typs (Abs (x, ty, t), seen) = |
|
132 |
let val (t', seen') = prune_typs (t, seen); |
|
133 |
in (Abs (x, ty, t'), seen') end |
|
134 |
| prune_typs (t1 $ t2, seen) = |
|
135 |
let |
|
136 |
val (t1', seen') = prune_typs (t1, seen); |
|
137 |
val (t2', seen'') = prune_typs (t2, seen'); |
|
6767 | 138 |
in (t1' $ t2', seen'') end; |
0 | 139 |
|
2701 | 140 |
fun ast_of tm = |
141 |
(case strip_comb tm of |
|
5691 | 142 |
(t as Abs _, ts) => Ast.mk_appl (ast_of (SynTrans.abs_tr' t)) (map ast_of ts) |
2701 | 143 |
| ((c as Const ("_free", _)), Free (x, T) :: ts) => |
5691 | 144 |
Ast.mk_appl (constrain (c $ Lexicon.free x) T) (map ast_of ts) |
2701 | 145 |
| ((c as Const ("_bound", _)), Free (x, T) :: ts) => |
5691 | 146 |
Ast.mk_appl (constrain (c $ Lexicon.free x) T) (map ast_of ts) |
2701 | 147 |
| ((c as Const ("_var", _)), Var (xi, T) :: ts) => |
5691 | 148 |
Ast.mk_appl (constrain (c $ Lexicon.var xi) T) (map ast_of ts) |
14176
716fec31f9ea
Added show_all_types flag, such that all type information in the term
skalberg
parents:
12785
diff
changeset
|
149 |
| (c' as Const (c, T), ts) => |
14696 | 150 |
if show_all_types |
151 |
then Ast.mk_appl (constrain c' T) (map ast_of ts) |
|
152 |
else trans c T ts |
|
5691 | 153 |
| (t, ts) => Ast.mk_appl (simple_ast_of t) (map ast_of ts)) |
0 | 154 |
|
2384 | 155 |
and trans a T args = |
5691 | 156 |
ast_of (apply_trans "print translation" a (apply_typed show_sorts T (trf a)) args) |
157 |
handle Match => Ast.mk_appl (Ast.Constant a) (map ast_of args) |
|
0 | 158 |
|
2701 | 159 |
and constrain t T = |
160 |
if show_types andalso T <> dummyT then |
|
5691 | 161 |
Ast.Appl [Ast.Constant SynExt.constrainC, simple_ast_of t, |
162 |
ast_of_termT trf (TypeExt.term_of_typ show_sorts T)] |
|
2701 | 163 |
else simple_ast_of t |
0 | 164 |
in |
2701 | 165 |
tm |
5691 | 166 |
|> SynTrans.prop_tr' |
2701 | 167 |
|> (if show_types then #1 o prune_typs o rpair [] else I) |
168 |
|> mark_freevars |
|
169 |
|> ast_of |
|
0 | 170 |
end; |
171 |
||
172 |
fun term_to_ast trf tm = |
|
14696 | 173 |
ast_of_term trf (! show_all_types) (! show_no_free_types) |
174 |
(! show_types orelse ! show_sorts orelse ! show_all_types) (! show_sorts) tm; |
|
0 | 175 |
|
176 |
||
177 |
||
2200
2538977e94fa
added print_mode: string list ref (order of printer tables);
wenzelm
parents:
1509
diff
changeset
|
178 |
(** type prtabs **) |
0 | 179 |
|
180 |
datatype symb = |
|
181 |
Arg of int | |
|
182 |
TypArg of int | |
|
183 |
String of string | |
|
184 |
Break of int | |
|
185 |
Block of int * symb list; |
|
186 |
||
2200
2538977e94fa
added print_mode: string list ref (order of printer tables);
wenzelm
parents:
1509
diff
changeset
|
187 |
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
|
188 |
|
3816 | 189 |
|
2200
2538977e94fa
added print_mode: string list ref (order of printer tables);
wenzelm
parents:
1509
diff
changeset
|
190 |
(*find tab for mode*) |
2538977e94fa
added print_mode: string list ref (order of printer tables);
wenzelm
parents:
1509
diff
changeset
|
191 |
fun get_tab prtabs mode = |
4487 | 192 |
if_none (assoc (prtabs, mode)) Symtab.empty; |
2200
2538977e94fa
added print_mode: string list ref (order of printer tables);
wenzelm
parents:
1509
diff
changeset
|
193 |
|
2538977e94fa
added print_mode: string list ref (order of printer tables);
wenzelm
parents:
1509
diff
changeset
|
194 |
(*collect tabs for mode hierarchy (default "")*) |
2538977e94fa
added print_mode: string list ref (order of printer tables);
wenzelm
parents:
1509
diff
changeset
|
195 |
fun tabs_of prtabs modes = |
2538977e94fa
added print_mode: string list ref (order of printer tables);
wenzelm
parents:
1509
diff
changeset
|
196 |
mapfilter (fn mode => assoc (prtabs, mode)) (modes @ [""]); |
2538977e94fa
added print_mode: string list ref (order of printer tables);
wenzelm
parents:
1509
diff
changeset
|
197 |
|
18 | 198 |
|
3816 | 199 |
(* xprod_to_fmt *) |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
200 |
|
5691 | 201 |
fun xprod_to_fmt (SynExt.XProd (_, _, "", _)) = None |
202 |
| xprod_to_fmt (SynExt.XProd (_, xsymbs, const, pri)) = |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
203 |
let |
2384 | 204 |
fun cons_str s (String s' :: syms) = String (s ^ s') :: syms |
205 |
| cons_str s syms = String s :: syms; |
|
206 |
||
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
207 |
fun arg (s, p) = |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
208 |
(if s = "type" then TypArg else Arg) |
5691 | 209 |
(if Lexicon.is_terminal s then SynExt.max_pri else p); |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
210 |
|
5691 | 211 |
fun xsyms_to_syms (SynExt.Delim s :: xsyms) = |
6273 | 212 |
apfst (cons_str s) (xsyms_to_syms xsyms) |
5691 | 213 |
| xsyms_to_syms (SynExt.Argument s_p :: xsyms) = |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
214 |
apfst (cons (arg s_p)) (xsyms_to_syms xsyms) |
5691 | 215 |
| xsyms_to_syms (SynExt.Space s :: xsyms) = |
6273 | 216 |
apfst (cons_str s) (xsyms_to_syms xsyms) |
5691 | 217 |
| xsyms_to_syms (SynExt.Bg i :: xsyms) = |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
218 |
let |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
219 |
val (bsyms, xsyms') = xsyms_to_syms xsyms; |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
220 |
val (syms, xsyms'') = xsyms_to_syms xsyms'; |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
221 |
in |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
222 |
(Block (i, bsyms) :: syms, xsyms'') |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
223 |
end |
5691 | 224 |
| xsyms_to_syms (SynExt.Brk i :: xsyms) = |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
225 |
apfst (cons (Break i)) (xsyms_to_syms xsyms) |
5691 | 226 |
| xsyms_to_syms (SynExt.En :: xsyms) = ([], xsyms) |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
227 |
| xsyms_to_syms [] = ([], []); |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
228 |
|
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
229 |
fun nargs (Arg _ :: syms) = nargs syms + 1 |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
230 |
| nargs (TypArg _ :: syms) = nargs syms + 1 |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
231 |
| nargs (String _ :: syms) = nargs syms |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
232 |
| nargs (Break _ :: syms) = nargs syms |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
233 |
| nargs (Block (_, bsyms) :: syms) = nargs syms + nargs bsyms |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
234 |
| nargs [] = 0; |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
235 |
in |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
236 |
(case xsyms_to_syms xsymbs of |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
237 |
(symbs, []) => Some (const, (symbs, nargs symbs, pri)) |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
238 |
| _ => sys_error "xprod_to_fmt: unbalanced blocks") |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
239 |
end; |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
240 |
|
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
241 |
|
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
242 |
(* empty, extend, merge prtabs *) |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
243 |
|
2200
2538977e94fa
added print_mode: string list ref (order of printer tables);
wenzelm
parents:
1509
diff
changeset
|
244 |
val empty_prtabs = []; |
18 | 245 |
|
2200
2538977e94fa
added print_mode: string list ref (order of printer tables);
wenzelm
parents:
1509
diff
changeset
|
246 |
fun extend_prtabs prtabs mode xprods = |
2538977e94fa
added print_mode: string list ref (order of printer tables);
wenzelm
parents:
1509
diff
changeset
|
247 |
let |
12292 | 248 |
val fmts = mapfilter xprod_to_fmt xprods; |
2200
2538977e94fa
added print_mode: string list ref (order of printer tables);
wenzelm
parents:
1509
diff
changeset
|
249 |
val tab = get_tab prtabs mode; |
12292 | 250 |
val new_tab = foldr Symtab.update_multi (rev fmts, tab); |
2200
2538977e94fa
added print_mode: string list ref (order of printer tables);
wenzelm
parents:
1509
diff
changeset
|
251 |
in overwrite (prtabs, (mode, new_tab)) end; |
0 | 252 |
|
2200
2538977e94fa
added print_mode: string list ref (order of printer tables);
wenzelm
parents:
1509
diff
changeset
|
253 |
fun merge_prtabs prtabs1 prtabs2 = |
2538977e94fa
added print_mode: string list ref (order of printer tables);
wenzelm
parents:
1509
diff
changeset
|
254 |
let |
2538977e94fa
added print_mode: string list ref (order of printer tables);
wenzelm
parents:
1509
diff
changeset
|
255 |
val modes = distinct (map fst (prtabs1 @ prtabs2)); |
12292 | 256 |
fun merge m = (m, Symtab.merge_multi' (op =) (get_tab prtabs1 m, get_tab prtabs2 m)); |
257 |
in map merge modes end; |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
258 |
|
0 | 259 |
|
260 |
||
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
261 |
(** pretty term or typ asts **) |
0 | 262 |
|
2200
2538977e94fa
added print_mode: string list ref (order of printer tables);
wenzelm
parents:
1509
diff
changeset
|
263 |
fun is_chain [Block (_, pr)] = is_chain pr |
2538977e94fa
added print_mode: string list ref (order of printer tables);
wenzelm
parents:
1509
diff
changeset
|
264 |
| is_chain [Arg _] = true |
2538977e94fa
added print_mode: string list ref (order of printer tables);
wenzelm
parents:
1509
diff
changeset
|
265 |
| is_chain _ = false; |
506
e0ca460d6e51
improved show_brackets again - Trueprop does not create () any more.
nipkow
parents:
505
diff
changeset
|
266 |
|
2701 | 267 |
fun pretty tabs trf tokentrf type_mode curried ast0 p0 = |
0 | 268 |
let |
18 | 269 |
val trans = apply_trans "print ast translation"; |
270 |
||
5691 | 271 |
fun token_trans c [Ast.Variable x] = |
2701 | 272 |
(case tokentrf c of |
273 |
None => None |
|
274 |
| Some f => Some (f x)) |
|
275 |
| token_trans _ _ = None; |
|
276 |
||
2200
2538977e94fa
added print_mode: string list ref (order of printer tables);
wenzelm
parents:
1509
diff
changeset
|
277 |
(*default applications: prefix / postfix*) |
2538977e94fa
added print_mode: string list ref (order of printer tables);
wenzelm
parents:
1509
diff
changeset
|
278 |
val appT = |
5691 | 279 |
if type_mode then TypeExt.tappl_ast_tr' |
280 |
else if curried then SynTrans.applC_ast_tr' |
|
281 |
else SynTrans.appl_ast_tr'; |
|
18 | 282 |
|
0 | 283 |
fun synT ([], args) = ([], args) |
284 |
| synT (Arg p :: symbs, t :: args) = |
|
18 | 285 |
let val (Ts, args') = synT (symbs, args); |
0 | 286 |
in (astT (t, p) @ Ts, args') end |
287 |
| synT (TypArg p :: symbs, t :: args) = |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
288 |
let |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
289 |
val (Ts, args') = synT (symbs, args); |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
290 |
in |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
291 |
if type_mode then (astT (t, p) @ Ts, args') |
2701 | 292 |
else (pretty tabs trf tokentrf true curried t p @ Ts, args') |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
293 |
end |
0 | 294 |
| synT (String s :: symbs, args) = |
18 | 295 |
let val (Ts, args') = synT (symbs, args); |
8457 | 296 |
in (Pretty.str s :: Ts, args') end |
0 | 297 |
| synT (Block (i, bsymbs) :: symbs, args) = |
298 |
let |
|
299 |
val (bTs, args') = synT (bsymbs, args); |
|
300 |
val (Ts, args'') = synT (symbs, args'); |
|
14837
827c68f8267c
added pp_show_brackets; support unbreakable blocks;
wenzelm
parents:
14783
diff
changeset
|
301 |
val T = |
827c68f8267c
added pp_show_brackets; support unbreakable blocks;
wenzelm
parents:
14783
diff
changeset
|
302 |
if i < 0 then Pretty.unbreakable (Pretty.block bTs) |
827c68f8267c
added pp_show_brackets; support unbreakable blocks;
wenzelm
parents:
14783
diff
changeset
|
303 |
else Pretty.blk (i, bTs); |
827c68f8267c
added pp_show_brackets; support unbreakable blocks;
wenzelm
parents:
14783
diff
changeset
|
304 |
in (T :: Ts, args'') end |
0 | 305 |
| synT (Break i :: symbs, args) = |
14837
827c68f8267c
added pp_show_brackets; support unbreakable blocks;
wenzelm
parents:
14783
diff
changeset
|
306 |
let |
827c68f8267c
added pp_show_brackets; support unbreakable blocks;
wenzelm
parents:
14783
diff
changeset
|
307 |
val (Ts, args') = synT (symbs, args); |
827c68f8267c
added pp_show_brackets; support unbreakable blocks;
wenzelm
parents:
14783
diff
changeset
|
308 |
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
|
309 |
in (T :: Ts, args') end |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
310 |
| synT (_ :: _, []) = sys_error "synT" |
0 | 311 |
|
554 | 312 |
and parT (pr, args, p, p': int) = #1 (synT |
313 |
(if p > p' orelse |
|
5691 | 314 |
(! show_brackets andalso p' <> SynExt.max_pri andalso not (is_chain pr)) |
554 | 315 |
then [Block (1, String "(" :: pr @ [String ")"])] |
316 |
else pr, args)) |
|
0 | 317 |
|
8457 | 318 |
and prefixT (_, a, [], _) = [Pretty.str a] |
2384 | 319 |
| prefixT (c, _, args, p) = |
5691 | 320 |
if c = Ast.Constant "_appl" orelse c = Ast.Constant "_applC" then |
9910
f025cf787554
internalize error "insufficient syntax for prefix application";
wenzelm
parents:
8457
diff
changeset
|
321 |
[Pretty.str "*** INSUFFICIENT SYNTAX FOR PREFIX APPLICATION ***"] |
2384 | 322 |
else astT (appT (c, args), p) |
0 | 323 |
|
18 | 324 |
and splitT 0 ([x], ys) = (x, ys) |
5691 | 325 |
| splitT 0 (rev_xs, ys) = (Ast.Appl (rev rev_xs), ys) |
18 | 326 |
| splitT n (rev_xs, y :: ys) = splitT (n - 1) (y :: rev_xs, ys) |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
62
diff
changeset
|
327 |
| splitT _ _ = sys_error "splitT" |
18 | 328 |
|
0 | 329 |
and combT (tup as (c, a, args, p)) = |
330 |
let |
|
331 |
val nargs = length args; |
|
18 | 332 |
|
2701 | 333 |
(*find matching table entry, or print as prefix / postfix*) |
6280 | 334 |
fun prnt ([], []) = prefixT tup |
335 |
| prnt ([], tb :: tbs) = prnt (Symtab.lookup_multi (tb, a), tbs) |
|
336 |
| prnt ((pr, n, p') :: prnps, tbs) = |
|
2200
2538977e94fa
added print_mode: string list ref (order of printer tables);
wenzelm
parents:
1509
diff
changeset
|
337 |
if nargs = n then parT (pr, args, p, p') |
2538977e94fa
added print_mode: string list ref (order of printer tables);
wenzelm
parents:
1509
diff
changeset
|
338 |
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
|
339 |
astT (appT (splitT n ([c], args)), p) |
6280 | 340 |
else prnt (prnps, tbs); |
2200
2538977e94fa
added print_mode: string list ref (order of printer tables);
wenzelm
parents:
1509
diff
changeset
|
341 |
in |
2701 | 342 |
(case token_trans a args of (*try token translation function*) |
8457 | 343 |
Some s_len => [Pretty.raw_str s_len] |
14696 | 344 |
| None => (*try print translation functions*) |
6280 | 345 |
astT (trans a (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
|
346 |
end |
2538977e94fa
added print_mode: string list ref (order of printer tables);
wenzelm
parents:
1509
diff
changeset
|
347 |
|
5691 | 348 |
and astT (c as Ast.Constant a, p) = combT (c, a, [], p) |
8457 | 349 |
| astT (Ast.Variable x, _) = [Pretty.str x] |
6280 | 350 |
| astT (Ast.Appl ((c as Ast.Constant a) :: (args as _ :: _)), p) = combT (c, a, args, p) |
5691 | 351 |
| astT (Ast.Appl (f :: (args as _ :: _)), p) = astT (appT (f, args), p) |
352 |
| astT (ast as Ast.Appl _, _) = raise Ast.AST ("pretty: malformed ast", [ast]); |
|
6280 | 353 |
in astT (ast0, p0) end; |
0 | 354 |
|
355 |
||
356 |
(* pretty_term_ast *) |
|
357 |
||
2701 | 358 |
fun pretty_term_ast curried prtabs trf tokentrf ast = |
359 |
Pretty.blk (0, pretty (tabs_of prtabs (! print_mode)) |
|
360 |
trf tokentrf false curried ast 0); |
|
0 | 361 |
|
362 |
||
363 |
(* pretty_typ_ast *) |
|
364 |
||
2701 | 365 |
fun pretty_typ_ast _ prtabs trf tokentrf ast = |
366 |
Pretty.blk (0, pretty (tabs_of prtabs (! print_mode)) |
|
367 |
trf tokentrf true false ast 0); |
|
0 | 368 |
|
369 |
end; |