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