Release coccinelle-0.2.3rc1
[bpt/coccinelle.git] / parsing_c / unparse_cocci.ml
CommitLineData
ae4735db
C
1(*
2 * Copyright (C) 2010, University of Copenhagen DIKU and INRIA.
3 * Copyright (C) 2006, 2007 Julia Lawall
978fd7e5
C
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License (GPL)
7 * version 2 as published by the Free Software Foundation.
951c7801 8 *
978fd7e5
C
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * file license.txt for more details.
951c7801 13 *
978fd7e5
C
14 * This file was part of Coccinelle.
15 *)
34e49164
C
16open Common
17
18(*****************************************************************************)
951c7801 19(* mostly a copy paste of parsing_cocci/pretty_print_cocci.ml
34e49164
C
20 * todo?: try to factorize ?
21 *)
22(*****************************************************************************)
23
24module Ast = Ast_cocci
25
26let term s = Ast.unwrap_mcode s
27
951c7801
C
28(* or perhaps can have in plus, for instance a Disj, but those Disj must be
29 * handled by interactive tool (by proposing alternatives)
34e49164
C
30 *)
31exception CantBeInPlus
32
33(*****************************************************************************)
34
35type pos = Before | After | InPlace
5636bb2c 36type nlhint = StartBox | EndBox | SpaceOrNewline of string ref
34e49164 37
708f4980
C
38let unknown = -1
39
951c7801 40let rec do_all
708f4980
C
41 (env, pr, pr_celem, pr_cspace, pr_space, pr_arity, pr_barrier,
42 indent, unindent)
faf9a90c 43 generating xxs before =
34e49164
C
44
45(* Just to be able to copy paste the code from pretty_print_cocci.ml. *)
708f4980
C
46let print_string s line lcol =
47 let rcol = if lcol = unknown then unknown else lcol + (String.length s) in
5636bb2c
C
48 pr s line lcol rcol None in
49let print_string_with_hint hint s line lcol =
50 let rcol = if lcol = unknown then unknown else lcol + (String.length s) in
51 pr s line lcol rcol (Some hint) in
52let print_text s = pr s unknown unknown unknown None in
34e49164 53let close_box _ = () in
fc1ad971 54let force_newline _ = print_text "\n" in
34e49164
C
55
56let start_block () = force_newline(); indent() in
c3e37e97 57let end_block () = unindent true; force_newline () in
34e49164
C
58let print_string_box s = print_string s in
59
60let print_option = Common.do_option in
002099fc
C
61let print_option_prespace fn = function
62 None -> ()
63 | Some x -> pr_space(); fn x in
64let print_option_space fn = function
65 None -> ()
66 | Some x -> fn x; pr_space() in
34e49164
C
67let print_between = Common.print_between in
68
113803cf
C
69let outdent _ = () (* should go to leftmost col, does nothing now *) in
70
71let pretty_print_c =
978fd7e5 72 Pretty_print_c.mk_pretty_printers pr_celem pr_cspace
c3e37e97 73 force_newline indent outdent (function _ -> unindent true) in
113803cf 74
34e49164 75(* --------------------------------------------------------------------- *)
faf9a90c 76(* Only for make_hrule, print plus code, unbound metavariables *)
34e49164 77
faf9a90c
C
78(* avoid polyvariance problems *)
79let anything : (Ast.anything -> unit) ref = ref (function _ -> ()) in
34e49164 80
faf9a90c
C
81let rec print_anything = function
82 [] -> ()
83 | stream ->
84 start_block();
85 print_between force_newline print_anything_list stream;
86 end_block()
87
88and print_anything_list = function
34e49164 89 [] -> ()
faf9a90c
C
90 | [x] -> !anything x
91 | bef::((aft::_) as rest) ->
92 !anything bef;
93 let space =
94 (match bef with
95 Ast.Rule_elemTag(_) | Ast.AssignOpTag(_) | Ast.BinaryOpTag(_)
96 | Ast.ArithOpTag(_) | Ast.LogicalOpTag(_)
97 | Ast.Token("if",_) | Ast.Token("while",_) -> true | _ -> false) or
98 (match aft with
99 Ast.Rule_elemTag(_) | Ast.AssignOpTag(_) | Ast.BinaryOpTag(_)
100 | Ast.ArithOpTag(_) | Ast.LogicalOpTag(_) | Ast.Token("{",_) -> true
101 | _ -> false) in
708f4980 102 if space then pr_space ();
faf9a90c
C
103 print_anything_list rest in
104
105let print_around printer term = function
106 Ast.NOTHING -> printer term
951c7801
C
107 | Ast.BEFORE(bef,_) -> print_anything bef; printer term
108 | Ast.AFTER(aft,_) -> printer term; print_anything aft
109 | Ast.BEFOREAFTER(bef,aft,_) ->
faf9a90c
C
110 print_anything bef; printer term; print_anything aft in
111
0708f913 112let print_string_befaft fn fn1 x info =
c3e37e97
C
113 let print ln col =
114 function Ast.Noindent s | Ast.Indent s -> print_string s ln col in
708f4980 115 List.iter
c3e37e97 116 (function (s,ln,col) -> fn1(); print ln col s; force_newline())
faf9a90c
C
117 info.Ast.strbef;
118 fn x;
708f4980 119 List.iter
c3e37e97 120 (function (s,ln,col) -> force_newline(); fn1(); print ln col s)
faf9a90c 121 info.Ast.straft in
708f4980 122let print_meta (r,x) = print_text x in
faf9a90c
C
123
124let print_pos = function
125 Ast.MetaPos(name,_,_,_,_) ->
126 let name = Ast.unwrap_mcode name in
708f4980 127 print_text "@"; print_meta name
faf9a90c
C
128 | _ -> () in
129
130(* --------------------------------------------------------------------- *)
131
708f4980
C
132let mcode fn (s,info,mc,pos) =
133 let line = info.Ast.line in
134 let lcol = info.Ast.column in
135 match (generating,mc) with
136 (false,_) ->
faf9a90c
C
137 (* printing for transformation *)
138 (* Here we don't care about the annotation on s. *)
0708f913
C
139 let print_comments lb comments =
140 List.fold_left
141 (function line_before ->
142 function (str,line,col) ->
143 match line_before with
c3e37e97
C
144 None ->
145 let str =
146 match str with
147 Ast.Noindent s -> unindent false; s
148 | Ast.Indent s -> s in
149 print_string str line col; Some line
708f4980 150 | Some lb when line =|= lb ->
c3e37e97 151 let str = match str with Ast.Noindent s | Ast.Indent s -> s in
708f4980 152 print_string str line col; Some line
c3e37e97 153 | _ ->
aa721442
C
154 force_newline();
155 (* not super elegant to put side-effecting unindent in a let
156 expression... *)
c3e37e97
C
157 let str =
158 match str with
159 Ast.Noindent s -> unindent false; s
160 | Ast.Indent s -> s in
aa721442 161 print_string str line col; Some line)
0708f913
C
162 lb comments in
163 let line_before = print_comments None info.Ast.strbef in
164 (match line_before with
165 None -> ()
b1b2de81 166 | Some lb when lb =|= info.Ast.line -> ()
708f4980
C
167 | _ -> force_newline());
168 fn s line lcol;
0708f913 169 let _ = print_comments (Some info.Ast.line) info.Ast.straft in
fc1ad971
C
170 (* newline after a pragma
171 should really store parsed versions of the strings, but make a cheap
172 effort here
173 print_comments takes care of interior newlines *)
0708f913 174 ()
faf9a90c 175 (* printing for rule generation *)
708f4980
C
176 | (true, Ast.MINUS(_,_,_,plus_stream)) ->
177 force_newline();
178 print_text "- ";
179 fn s line lcol; print_pos pos;
faf9a90c 180 print_anything plus_stream
708f4980
C
181 | (true, Ast.CONTEXT(_,plus_streams)) ->
182 let fn s = force_newline(); fn s line lcol; print_pos pos in
183 print_around fn s plus_streams
951c7801 184 | (true,Ast.PLUS Ast.ONE) ->
708f4980
C
185 let fn s =
186 force_newline(); print_text "+ "; fn s line lcol; print_pos pos in
187 print_string_befaft fn (function _ -> print_text "+ ") s info
951c7801
C
188 | (true,Ast.PLUS Ast.MANY) ->
189 let fn s =
190 force_newline(); print_text "++ "; fn s line lcol; print_pos pos in
191 print_string_befaft fn (function _ -> print_text "++ ") s info
34e49164
C
192in
193
faf9a90c
C
194
195(* --------------------------------------------------------------------- *)
196
197let handle_metavar name fn =
708f4980
C
198 let ((_,b) as s,info,mc,pos) = name in
199 let line = info.Ast.line in
200 let lcol = info.Ast.column in
201 match Common.optionise (fun () -> List.assoc s env) with
202 None ->
faf9a90c
C
203 let name_string (_,s) = s in
204 if generating
708f4980
C
205 then
206 mcode (function _ -> print_string (name_string s)) name
faf9a90c
C
207 else
208 failwith
209 (Printf.sprintf "SP line %d: Not found a value in env for: %s"
708f4980 210 line (name_string s))
faf9a90c 211 | Some e ->
708f4980
C
212 pr_barrier line lcol;
213 (if generating
214 then
215 (* call mcode to preserve the -+ annotation *)
216 mcode (fun _ _ _ -> fn e) name
217 else fn e);
c3e37e97
C
218 let rcol =
219 if lcol = unknown then unknown else lcol + (String.length b) in
708f4980 220 pr_barrier line rcol
faf9a90c 221in
34e49164
C
222(* --------------------------------------------------------------------- *)
223let dots between fn d =
224 match Ast.unwrap d with
225 Ast.DOTS(l) -> print_between between fn l
226 | Ast.CIRCLES(l) -> print_between between fn l
227 | Ast.STARS(l) -> print_between between fn l
228in
229
5636bb2c
C
230let nest_dots starter ender fn f d =
231 mcode print_string starter;
232 f(); start_block();
233 (match Ast.unwrap d with
234 Ast.DOTS(l) -> print_between force_newline fn l
235 | Ast.CIRCLES(l) -> print_between force_newline fn l
236 | Ast.STARS(l) -> print_between force_newline fn l);
237 end_block();
238 mcode print_string ender
faf9a90c 239in
34e49164
C
240
241(* --------------------------------------------------------------------- *)
242(* Identifier *)
243
244let rec ident i =
245 match Ast.unwrap i with
951c7801
C
246 Ast.Id(name) -> mcode print_string name
247 | Ast.MetaId(name,_,_,_) ->
248 handle_metavar name (function
5636bb2c 249 | (Ast_c.MetaIdVal (id,_)) -> print_text id
951c7801
C
250 | _ -> raise Impossible
251 )
252 | Ast.MetaFunc(name,_,_,_) ->
253 handle_metavar name (function
254 | (Ast_c.MetaFuncVal id) -> print_text id
255 | _ -> raise Impossible
256 )
257 | Ast.MetaLocalFunc(name,_,_,_) ->
258 handle_metavar name (function
259 | (Ast_c.MetaLocalFuncVal id) -> print_text id
260 | _ -> raise Impossible
261 )
262
263 | Ast.OptIdent(_) | Ast.UniqueIdent(_) ->
264 raise CantBeInPlus
34e49164
C
265
266in
267
268(* --------------------------------------------------------------------- *)
269(* Expression *)
270
faf9a90c 271let print_disj_list fn l =
708f4980
C
272 print_text "\n(\n";
273 print_between (function _ -> print_text "\n|\n") fn l;
274 print_text "\n)\n" in
faf9a90c 275
34e49164
C
276let rec expression e =
277 match Ast.unwrap e with
278 Ast.Ident(id) -> ident id
34e49164
C
279 | Ast.Constant(const) -> mcode constant const
280 | Ast.FunCall(fn,lp,args,rp) ->
5636bb2c
C
281 expression fn; mcode (print_string_with_hint StartBox) lp;
282 dots (function _ -> ()) arg_expression args;
283 mcode (print_string_with_hint EndBox) rp
34e49164 284 | Ast.Assignment(left,op,right,_) ->
708f4980
C
285 expression left; pr_space(); mcode assignOp op;
286 pr_space(); expression right
34e49164 287 | Ast.CondExpr(exp1,why,exp2,colon,exp3) ->
708f4980
C
288 expression exp1; pr_space(); mcode print_string why;
289 print_option (function e -> pr_space(); expression e) exp2;
290 pr_space(); mcode print_string colon; expression exp3
34e49164
C
291 | Ast.Postfix(exp,op) -> expression exp; mcode fixOp op
292 | Ast.Infix(exp,op) -> mcode fixOp op; expression exp
293 | Ast.Unary(exp,op) -> mcode unaryOp op; expression exp
294 | Ast.Binary(left,op,right) ->
708f4980 295 expression left; pr_space(); mcode binaryOp op; pr_space();
34e49164
C
296 expression right
297 | Ast.Nested(left,op,right) -> failwith "nested only in minus code"
298 | Ast.Paren(lp,exp,rp) ->
299 mcode print_string_box lp; expression exp; close_box();
300 mcode print_string rp
301 | Ast.ArrayAccess(exp1,lb,exp2,rb) ->
302 expression exp1; mcode print_string_box lb; expression exp2; close_box();
303 mcode print_string rb
304 | Ast.RecordAccess(exp,pt,field) ->
305 expression exp; mcode print_string pt; ident field
306 | Ast.RecordPtAccess(exp,ar,field) ->
307 expression exp; mcode print_string ar; ident field
308 | Ast.Cast(lp,ty,rp,exp) ->
309 mcode print_string_box lp; fullType ty; close_box();
310 mcode print_string rp; expression exp
311 | Ast.SizeOfExpr(sizeof,exp) ->
312 mcode print_string sizeof; expression exp
313 | Ast.SizeOfType(sizeof,lp,ty,rp) ->
314 mcode print_string sizeof;
315 mcode print_string_box lp; fullType ty; close_box();
316 mcode print_string rp
317 | Ast.TypeExp(ty) -> fullType ty
318
ae4735db 319 | Ast.MetaErr(name,_,_,_) ->
34e49164
C
320 failwith "metaErr not handled"
321
322 | Ast.MetaExpr (name,_,_,_typedontcare,_formdontcare,_) ->
323 handle_metavar name (function
5636bb2c 324 | Ast_c.MetaExprVal (exp,_) ->
113803cf 325 pretty_print_c.Pretty_print_c.expression exp
34e49164
C
326 | _ -> raise Impossible
327 )
328
ae4735db 329 | Ast.MetaExprList (name,_,_,_) ->
faf9a90c 330 handle_metavar name (function
ae4735db 331 | Ast_c.MetaExprListVal args ->
113803cf 332 pretty_print_c.Pretty_print_c.arg_list args
faf9a90c
C
333 | _ -> raise Impossible
334 )
335
002099fc 336 | Ast.EComma(cm) -> mcode print_string cm
34e49164 337
faf9a90c
C
338 | Ast.DisjExpr(exp_list) ->
339 if generating
340 then print_disj_list expression exp_list
341 else raise CantBeInPlus
5636bb2c
C
342 | Ast.NestExpr(starter,expr_dots,ender,Some whencode,multi)
343 when generating ->
344 nest_dots starter ender expression
708f4980 345 (function _ -> print_text " when != "; expression whencode)
faf9a90c 346 expr_dots
5636bb2c
C
347 | Ast.NestExpr(starter,expr_dots,ender,None,multi) when generating ->
348 nest_dots starter ender expression (function _ -> ()) expr_dots
349 | Ast.NestExpr _ -> raise CantBeInPlus
faf9a90c
C
350 | Ast.Edots(dots,Some whencode)
351 | Ast.Ecircles(dots,Some whencode)
352 | Ast.Estars(dots,Some whencode) ->
353 if generating
354 then
355 (mcode print_string dots;
708f4980 356 print_text " when != ";
faf9a90c
C
357 expression whencode)
358 else raise CantBeInPlus
359 | Ast.Edots(dots,None)
360 | Ast.Ecircles(dots,None)
361 | Ast.Estars(dots,None) ->
362 if generating
363 then mcode print_string dots
364 else raise CantBeInPlus
34e49164 365
ae4735db 366 | Ast.OptExp(exp) | Ast.UniqueExp(exp) ->
34e49164
C
367 raise CantBeInPlus
368
5636bb2c
C
369and arg_expression e =
370 match Ast.unwrap e with
371 Ast.EComma(cm) ->
372 (* space is only used by add_newline, and only if not using SMPL
373 spacing. pr_cspace uses a " " in unparse_c.ml. Not so nice... *)
374 mcode (print_string_with_hint (SpaceOrNewline (ref " "))) cm
375 | _ -> expression e
376
34e49164
C
377and unaryOp = function
378 Ast.GetRef -> print_string "&"
379 | Ast.DeRef -> print_string "*"
380 | Ast.UnPlus -> print_string "+"
381 | Ast.UnMinus -> print_string "-"
382 | Ast.Tilde -> print_string "~"
383 | Ast.Not -> print_string "!"
384
385and assignOp = function
386 Ast.SimpleAssign -> print_string "="
708f4980
C
387 | Ast.OpAssign(aop) ->
388 (function line -> function lcol ->
389 arithOp aop line lcol; print_string "=" line lcol)
34e49164
C
390
391and fixOp = function
392 Ast.Dec -> print_string "--"
393 | Ast.Inc -> print_string "++"
394
395and binaryOp = function
396 Ast.Arith(aop) -> arithOp aop
397 | Ast.Logical(lop) -> logicalOp lop
398
399and arithOp = function
400 Ast.Plus -> print_string "+"
401 | Ast.Minus -> print_string "-"
402 | Ast.Mul -> print_string "*"
403 | Ast.Div -> print_string "/"
404 | Ast.Mod -> print_string "%"
405 | Ast.DecLeft -> print_string "<<"
406 | Ast.DecRight -> print_string ">>"
407 | Ast.And -> print_string "&"
408 | Ast.Or -> print_string "|"
409 | Ast.Xor -> print_string "^"
410
411and logicalOp = function
412 Ast.Inf -> print_string "<"
413 | Ast.Sup -> print_string ">"
414 | Ast.InfEq -> print_string "<="
415 | Ast.SupEq -> print_string ">="
416 | Ast.Eq -> print_string "=="
417 | Ast.NotEq -> print_string "!="
418 | Ast.AndLog -> print_string "&&"
419 | Ast.OrLog -> print_string "||"
420
421and constant = function
708f4980 422 Ast.String(s) -> print_string ("\""^s^"\"")
978fd7e5 423 | Ast.Char(s) -> print_string ("\'"^s^"\'")
34e49164
C
424 | Ast.Int(s) -> print_string s
425 | Ast.Float(s) -> print_string s
426
427(* --------------------------------------------------------------------- *)
428(* Types *)
429
430
431and fullType ft =
432 match Ast.unwrap ft with
002099fc 433 Ast.Type(cv,ty) -> print_option_space (mcode const_vol) cv; typeC ty
34e49164
C
434 | Ast.DisjType _ -> failwith "can't be in plus"
435 | Ast.OptType(_) | Ast.UniqueType(_) ->
436 raise CantBeInPlus
437
438and print_function_pointer (ty,lp1,star,rp1,lp2,params,rp2) fn =
439 fullType ty; mcode print_string lp1; mcode print_string star; fn();
440 mcode print_string rp1; mcode print_string lp1;
441 parameter_list params; mcode print_string rp2
442
443and print_function_type (ty,lp1,params,rp1) fn =
444 print_option fullType ty; fn(); mcode print_string lp1;
445 parameter_list params; mcode print_string rp1
446
447and typeC ty =
448 match Ast.unwrap ty with
faf9a90c
C
449 Ast.BaseType(ty,strings) ->
450 print_between pr_space (mcode print_string) strings
002099fc 451 | Ast.SignedT(sgn,ty) -> mcode sign sgn; print_option_prespace typeC ty
34e49164
C
452 | Ast.Pointer(ty,star) -> fullType ty; ft_space ty; mcode print_string star
453 | Ast.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) ->
454 print_function_pointer (ty,lp1,star,rp1,lp2,params,rp2)
455 (function _ -> ())
456 | Ast.FunctionType (am,ty,lp1,params,rp1) ->
457 print_function_type (ty,lp1,params,rp1) (function _ -> ())
458 | Ast.Array(ty,lb,size,rb) ->
459 fullType ty; mcode print_string lb; print_option expression size;
460 mcode print_string rb
708f4980 461 | Ast.EnumName(kind,name) -> mcode print_string kind; pr_space();
faf9a90c 462 ident name
34e49164 463 | Ast.StructUnionName(kind,name) ->
002099fc 464 mcode structUnion kind; print_option_prespace ident name
34e49164 465 | Ast.StructUnionDef(ty,lb,decls,rb) ->
708f4980 466 fullType ty; ft_space ty;
34e49164
C
467 mcode print_string lb;
468 dots force_newline declaration decls;
469 mcode print_string rb
470 | Ast.TypeName(name)-> mcode print_string name
ae4735db 471 | Ast.MetaType(name,_,_) ->
34e49164 472 handle_metavar name (function
ae4735db 473 Ast_c.MetaTypeVal exp ->
113803cf 474 pretty_print_c.Pretty_print_c.ty exp
34e49164
C
475 | _ -> raise Impossible)
476
477and baseType = function
478 Ast.VoidType -> print_string "void"
479 | Ast.CharType -> print_string "char"
480 | Ast.ShortType -> print_string "short"
481 | Ast.IntType -> print_string "int"
482 | Ast.DoubleType -> print_string "double"
483 | Ast.FloatType -> print_string "float"
484 | Ast.LongType -> print_string "long"
faf9a90c 485 | Ast.LongLongType -> print_string "long long"
34e49164
C
486
487and structUnion = function
002099fc
C
488 Ast.Struct -> print_string "struct"
489 | Ast.Union -> print_string "union"
34e49164
C
490
491and sign = function
34e49164
C
492 Ast.Signed -> print_string "signed"
493 | Ast.Unsigned -> print_string "unsigned"
494
495
496and const_vol = function
002099fc
C
497 Ast.Const -> print_string "const"
498 | Ast.Volatile -> print_string "volatile"
34e49164
C
499
500(* --------------------------------------------------------------------- *)
501(* Function declaration *)
502
503and storage = function
002099fc
C
504 Ast.Static -> print_string "static"
505 | Ast.Auto -> print_string "auto"
506 | Ast.Register -> print_string "register"
507 | Ast.Extern -> print_string "extern"
34e49164
C
508
509(* --------------------------------------------------------------------- *)
510(* Variable declaration *)
511
512and print_named_type ty id =
513 match Ast.unwrap ty with
514 Ast.Type(None,ty1) ->
515 (match Ast.unwrap ty1 with
516 Ast.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) ->
517 print_function_pointer (ty,lp1,star,rp1,lp2,params,rp2)
708f4980 518 (function _ -> pr_space(); ident id)
34e49164
C
519 | Ast.FunctionType(am,ty,lp1,params,rp1) ->
520 print_function_type (ty,lp1,params,rp1)
708f4980 521 (function _ -> pr_space(); ident id)
34e49164
C
522 | Ast.Array(_,_,_,_) ->
523 let rec loop ty k =
524 match Ast.unwrap ty with
525 Ast.Array(ty,lb,size,rb) ->
526 (match Ast.unwrap ty with
527 Ast.Type(None,ty) ->
528 loop ty
529 (function _ ->
530 k ();
531 mcode print_string lb;
532 print_option expression size;
533 mcode print_string rb)
534 | _ -> failwith "complex array types not supported")
535 | _ -> typeC ty; ty_space ty; ident id; k () in
536 loop ty1 (function _ -> ())
537 (*| should have a case here for pointer to array or function type
538 that would put ( * ) around the variable. This makes one wonder
539 why we really need a special case for function pointer *)
540 | _ -> fullType ty; ft_space ty; ident id)
541 | _ -> fullType ty; ft_space ty; ident id
542
543and ty_space ty =
544 match Ast.unwrap ty with
545 Ast.Pointer(_,_) -> ()
708f4980 546 | _ -> pr_space()
34e49164
C
547
548and ft_space ty =
549 match Ast.unwrap ty with
550 Ast.Type(cv,ty) ->
551 (match Ast.unwrap ty with
552 Ast.Pointer(_,_) -> ()
c3e37e97
C
553 | Ast.MetaType(name,_,_) ->
554 (match List.assoc (Ast.unwrap_mcode name) env with
555 Ast_c.MetaTypeVal (tq,ty) ->
556 (match Ast_c.unwrap ty with
557 Ast_c.Pointer(_,_) -> ()
558 | _ -> pr_space())
559 | _ -> pr_space())
708f4980
C
560 | _ -> pr_space())
561 | _ -> pr_space()
34e49164
C
562
563and declaration d =
564 match Ast.unwrap d with
565 Ast.Init(stg,ty,id,eq,ini,sem) ->
566 print_option (mcode storage) stg;
002099fc 567 print_option (function _ -> pr_space()) stg;
34e49164 568 print_named_type ty id;
708f4980
C
569 pr_space(); mcode print_string eq;
570 pr_space(); initialiser true ini; mcode print_string sem
34e49164
C
571 | Ast.UnInit(stg,ty,id,sem) ->
572 print_option (mcode storage) stg;
002099fc 573 print_option (function _ -> pr_space()) stg;
34e49164
C
574 print_named_type ty id;
575 mcode print_string sem
576 | Ast.MacroDecl(name,lp,args,rp,sem) ->
577 ident name; mcode print_string_box lp;
578 dots (function _ -> ()) expression args;
579 close_box(); mcode print_string rp; mcode print_string sem
580 | Ast.TyDecl(ty,sem) -> fullType ty; mcode print_string sem
581 | Ast.Typedef(stg,ty,id,sem) ->
582 mcode print_string stg;
583 fullType ty; typeC id;
584 mcode print_string sem
585 | Ast.DisjDecl(_) | Ast.MetaDecl(_,_,_) -> raise CantBeInPlus
586 | Ast.Ddots(_,_) -> raise CantBeInPlus
ae4735db 587 | Ast.OptDecl(decl) | Ast.UniqueDecl(decl) ->
34e49164
C
588 raise CantBeInPlus
589
590(* --------------------------------------------------------------------- *)
591(* Initialiser *)
592
593and initialiser nlcomma i =
594 match Ast.unwrap i with
ae4735db 595 Ast.MetaInit(name,_,_) ->
113803cf
C
596 handle_metavar name (function
597 Ast_c.MetaInitVal ini ->
598 pretty_print_c.Pretty_print_c.init ini
599 | _ -> raise Impossible)
600 | Ast.InitExpr(exp) -> expression exp
34e49164
C
601 | Ast.InitList(lb,initlist,rb,[]) ->
602 mcode print_string lb; start_block();
603 (* awkward, because the comma is separate from the initialiser *)
604 let rec loop = function
605 [] -> ()
606 | [x] -> initialiser false x
607 | x::xs -> initialiser nlcomma x; loop xs in
608 loop initlist;
609 end_block(); mcode print_string rb
610 | Ast.InitList(lb,initlist,rb,_) -> failwith "unexpected whencode in plus"
113803cf 611 | Ast.InitGccExt(designators,eq,ini) ->
708f4980
C
612 List.iter designator designators; pr_space();
613 mcode print_string eq; pr_space(); initialiser nlcomma ini
34e49164
C
614 | Ast.InitGccName(name,eq,ini) ->
615 ident name; mcode print_string eq; initialiser nlcomma ini
34e49164
C
616 | Ast.IComma(comma) ->
617 mcode print_string comma;
618 if nlcomma then force_newline()
619 | Ast.OptIni(ini) | Ast.UniqueIni(ini) ->
620 raise CantBeInPlus
621
113803cf
C
622and designator = function
623 Ast.DesignatorField(dot,id) -> mcode print_string dot; ident id
624 | Ast.DesignatorIndex(lb,exp,rb) ->
625 mcode print_string lb; expression exp; mcode print_string rb
626 | Ast.DesignatorRange(lb,min,dots,max,rb) ->
627 mcode print_string lb; expression min; mcode print_string dots;
628 expression max; mcode print_string rb
629
34e49164
C
630(* --------------------------------------------------------------------- *)
631(* Parameter *)
632
633and parameterTypeDef p =
634 match Ast.unwrap p with
635 Ast.VoidParam(ty) -> fullType ty
636 | Ast.Param(ty,Some id) -> print_named_type ty id
637 | Ast.Param(ty,None) -> fullType ty
638
ae4735db 639 | Ast.MetaParam(name,_,_) ->
5636bb2c
C
640 handle_metavar name
641 (function
642 Ast_c.MetaParamVal p ->
643 pretty_print_c.Pretty_print_c.param p
644 | _ -> raise Impossible)
ae4735db 645 | Ast.MetaParamList(name,_,_,_) ->
34e49164
C
646 failwith "not handling MetaParamList"
647
002099fc 648 | Ast.PComma(cm) -> mcode print_string cm
faf9a90c
C
649 | Ast.Pdots(dots) | Ast.Pcircles(dots) when generating ->
650 mcode print_string dots
651 | Ast.Pdots(dots) | Ast.Pcircles(dots) -> raise CantBeInPlus
34e49164
C
652 | Ast.OptParam(param) | Ast.UniqueParam(param) -> raise CantBeInPlus
653
fc1ad971
C
654and parameter_list l =
655 let comma p =
656 parameterTypeDef p;
657 match Ast.unwrap p with
658 Ast.PComma(cm) -> pr_space()
659 | _ -> () in
660 dots (function _ -> ()) comma l
34e49164
C
661in
662
663
664(* --------------------------------------------------------------------- *)
665(* CPP code *)
666
667let rec inc_file = function
668 Ast.Local(elems) ->
708f4980 669 print_string ("\""^(String.concat "/" (List.map inc_elem elems))^"\"")
34e49164 670 | Ast.NonLocal(elems) ->
708f4980 671 print_string ("<"^(String.concat "/" (List.map inc_elem elems))^">")
34e49164
C
672
673and inc_elem = function
708f4980
C
674 Ast.IncPath s -> s
675 | Ast.IncDots -> "..."
34e49164
C
676
677(* --------------------------------------------------------------------- *)
678(* Top-level code *)
679
680and rule_elem arity re =
681 match Ast.unwrap re with
682 Ast.FunHeader(_,_,fninfo,name,lp,params,rp) ->
708f4980 683 pr_arity arity; List.iter print_fninfo fninfo;
34e49164
C
684 ident name; mcode print_string_box lp;
685 parameter_list params; close_box(); mcode print_string rp;
708f4980
C
686 pr_space()
687 | Ast.Decl(_,_,decl) -> pr_arity arity; declaration decl
34e49164
C
688
689 | Ast.SeqStart(brace) ->
708f4980 690 pr_arity arity; mcode print_string brace; start_block()
34e49164 691 | Ast.SeqEnd(brace) ->
708f4980 692 end_block(); pr_arity arity; mcode print_string brace
34e49164
C
693
694 | Ast.ExprStatement(exp,sem) ->
708f4980 695 pr_arity arity; expression exp; mcode print_string sem
34e49164
C
696
697 | Ast.IfHeader(iff,lp,exp,rp) ->
708f4980
C
698 pr_arity arity;
699 mcode print_string iff; pr_space(); mcode print_string_box lp;
113803cf 700 expression exp; close_box(); mcode print_string rp
34e49164 701 | Ast.Else(els) ->
708f4980 702 pr_arity arity; mcode print_string els
34e49164
C
703
704 | Ast.WhileHeader(whl,lp,exp,rp) ->
708f4980
C
705 pr_arity arity;
706 mcode print_string whl; pr_space(); mcode print_string_box lp;
113803cf 707 expression exp; close_box(); mcode print_string rp
34e49164 708 | Ast.DoHeader(d) ->
708f4980 709 pr_arity arity; mcode print_string d
34e49164 710 | Ast.WhileTail(whl,lp,exp,rp,sem) ->
708f4980
C
711 pr_arity arity;
712 mcode print_string whl; pr_space(); mcode print_string_box lp;
34e49164
C
713 expression exp; close_box(); mcode print_string rp;
714 mcode print_string sem
715 | Ast.ForHeader(fr,lp,e1,sem1,e2,sem2,e3,rp) ->
708f4980 716 pr_arity arity;
34e49164
C
717 mcode print_string fr; mcode print_string_box lp;
718 print_option expression e1; mcode print_string sem1;
719 print_option expression e2; mcode print_string sem2;
720 print_option expression e3; close_box();
113803cf 721 mcode print_string rp
34e49164 722 | Ast.IteratorHeader(nm,lp,args,rp) ->
708f4980
C
723 pr_arity arity;
724 ident nm; pr_space(); mcode print_string_box lp;
34e49164 725 dots (function _ -> ()) expression args; close_box();
113803cf 726 mcode print_string rp
34e49164
C
727
728 | Ast.SwitchHeader(switch,lp,exp,rp) ->
708f4980
C
729 pr_arity arity;
730 mcode print_string switch; pr_space(); mcode print_string_box lp;
113803cf 731 expression exp; close_box(); mcode print_string rp
34e49164
C
732
733 | Ast.Break(br,sem) ->
708f4980 734 pr_arity arity; mcode print_string br; mcode print_string sem
34e49164 735 | Ast.Continue(cont,sem) ->
708f4980 736 pr_arity arity; mcode print_string cont; mcode print_string sem
34e49164
C
737 | Ast.Label(l,dd) -> ident l; mcode print_string dd
738 | Ast.Goto(goto,l,sem) ->
739 mcode print_string goto; ident l; mcode print_string sem
740 | Ast.Return(ret,sem) ->
ae4735db 741 pr_arity arity; mcode print_string ret;
34e49164
C
742 mcode print_string sem
743 | Ast.ReturnExpr(ret,exp,sem) ->
708f4980 744 pr_arity arity; mcode print_string ret; pr_space();
34e49164
C
745 expression exp; mcode print_string sem
746
708f4980
C
747 | Ast.Exp(exp) -> pr_arity arity; expression exp
748 | Ast.TopExp(exp) -> pr_arity arity; expression exp
749 | Ast.Ty(ty) -> pr_arity arity; fullType ty
1be43e12 750 | Ast.TopInit(init) -> initialiser false init
34e49164 751 | Ast.Include(inc,s) ->
708f4980 752 mcode print_string inc; print_text " "; mcode inc_file s
34e49164 753 | Ast.DefineHeader(def,id,params) ->
ae4735db 754 mcode print_string def; pr_space(); ident id;
34e49164
C
755 print_define_parameters params
756 | Ast.Default(def,colon) ->
708f4980 757 mcode print_string def; mcode print_string colon; pr_space()
34e49164 758 | Ast.Case(case,exp,colon) ->
708f4980
C
759 mcode print_string case; pr_space(); expression exp;
760 mcode print_string colon; pr_space()
faf9a90c
C
761 | Ast.DisjRuleElem(res) ->
762 if generating
763 then
708f4980
C
764 (pr_arity arity; print_text "\n(\n";
765 print_between (function _ -> print_text "\n|\n") (rule_elem arity)
faf9a90c 766 res;
708f4980 767 print_text "\n)")
faf9a90c 768 else raise CantBeInPlus
34e49164
C
769
770 | Ast.MetaRuleElem(name,_,_) ->
771 raise Impossible
772
773 | Ast.MetaStmt(name,_,_,_) ->
774 handle_metavar name (function
113803cf
C
775 | Ast_c.MetaStmtVal stm ->
776 pretty_print_c.Pretty_print_c.statement stm
34e49164
C
777 | _ -> raise Impossible
778 )
779 | Ast.MetaStmtList(name,_,_) ->
780 failwith
781 "MetaStmtList not supported (not even in ast_c metavars binding)"
782
783and print_define_parameters params =
784 match Ast.unwrap params with
785 Ast.NoParams -> ()
786 | Ast.DParams(lp,params,rp) ->
787 mcode print_string lp;
788 dots (function _ -> ()) print_define_param params; mcode print_string rp
789
790and print_define_param param =
791 match Ast.unwrap param with
792 Ast.DParam(id) -> ident id
793 | Ast.DPComma(comma) -> mcode print_string comma
794 | Ast.DPdots(dots) -> mcode print_string dots
795 | Ast.DPcircles(circles) -> mcode print_string circles
708f4980
C
796 | Ast.OptDParam(dp) -> print_text "?"; print_define_param dp
797 | Ast.UniqueDParam(dp) -> print_text "!"; print_define_param dp
34e49164
C
798
799and print_fninfo = function
800 Ast.FStorage(stg) -> mcode storage stg
801 | Ast.FType(ty) -> fullType ty
708f4980
C
802 | Ast.FInline(inline) -> mcode print_string inline; pr_space()
803 | Ast.FAttr(attr) -> mcode print_string attr; pr_space() in
34e49164 804
faf9a90c
C
805let indent_if_needed s f =
806 match Ast.unwrap s with
708f4980 807 Ast.Seq(lbrace,body,rbrace) -> pr_space(); f()
faf9a90c
C
808 | _ ->
809 (*no newline at the end - someone else will do that*)
c3e37e97 810 start_block(); f(); unindent true in
faf9a90c 811
34e49164
C
812let rec statement arity s =
813 match Ast.unwrap s with
708f4980 814 Ast.Seq(lbrace,body,rbrace) ->
34e49164 815 rule_elem arity lbrace;
34e49164
C
816 dots force_newline (statement arity) body;
817 rule_elem arity rbrace
818
819 | Ast.IfThen(header,branch,_) ->
faf9a90c
C
820 rule_elem arity header;
821 indent_if_needed branch (function _ -> statement arity branch)
34e49164 822 | Ast.IfThenElse(header,branch1,els,branch2,_) ->
faf9a90c
C
823 rule_elem arity header;
824 indent_if_needed branch1 (function _ -> statement arity branch1);
708f4980 825 force_newline();
faf9a90c
C
826 rule_elem arity els;
827 indent_if_needed branch2 (function _ -> statement arity branch2)
34e49164 828 | Ast.While(header,body,_) ->
faf9a90c
C
829 rule_elem arity header;
830 indent_if_needed body (function _ -> statement arity body)
34e49164 831 | Ast.Do(header,body,tail) ->
faf9a90c
C
832 rule_elem arity header;
833 indent_if_needed body (function _ -> statement arity body);
34e49164
C
834 rule_elem arity tail
835 | Ast.For(header,body,_) ->
faf9a90c
C
836 rule_elem arity header;
837 indent_if_needed body (function _ -> statement arity body)
34e49164 838 | Ast.Iterator(header,body,(_,_,_,aft)) ->
faf9a90c
C
839 rule_elem arity header;
840 indent_if_needed body (function _ -> statement arity body);
708f4980 841 mcode (fun _ _ _ -> ()) ((),Ast.no_info,aft,Ast.NoMetaPos)
34e49164 842
fc1ad971 843 | Ast.Switch(header,lb,decls,cases,rb) ->
708f4980 844 rule_elem arity header; pr_space(); rule_elem arity lb;
fc1ad971 845 dots force_newline (statement arity) decls;
34e49164
C
846 List.iter (function x -> case_line arity x; force_newline()) cases;
847 rule_elem arity rb
848
849 | Ast.Atomic(re) -> rule_elem arity re
850
708f4980 851 | Ast.FunDecl(header,lbrace,body,rbrace) ->
34e49164 852 rule_elem arity header; rule_elem arity lbrace;
34e49164
C
853 dots force_newline (statement arity) body; rule_elem arity rbrace
854
855 | Ast.Define(header,body) ->
708f4980 856 rule_elem arity header; pr_space();
34e49164
C
857 dots force_newline (statement arity) body
858
faf9a90c
C
859 | Ast.Disj([stmt_dots]) ->
860 if generating
861 then
708f4980 862 (pr_arity arity;
faf9a90c
C
863 dots force_newline (statement arity) stmt_dots)
864 else raise CantBeInPlus
865 | Ast.Disj(stmt_dots_list) -> (* ignores newline directive for readability *)
866 if generating
867 then
708f4980
C
868 (pr_arity arity; print_text "\n(\n";
869 print_between (function _ -> print_text "\n|\n")
faf9a90c
C
870 (dots force_newline (statement arity))
871 stmt_dots_list;
708f4980 872 print_text "\n)")
faf9a90c 873 else raise CantBeInPlus
5636bb2c 874 | Ast.Nest(starter,stmt_dots,ender,whn,multi,_,_) when generating ->
708f4980 875 pr_arity arity;
5636bb2c 876 nest_dots starter ender (statement arity)
faf9a90c
C
877 (function _ ->
878 print_between force_newline
879 (whencode (dots force_newline (statement "")) (statement "")) whn;
880 force_newline())
881 stmt_dots
882 | Ast.Nest(_) -> raise CantBeInPlus
883 | Ast.Dots(d,whn,_,_) | Ast.Circles(d,whn,_,_) | Ast.Stars(d,whn,_,_) ->
884 if generating
885 then
708f4980 886 (pr_arity arity; mcode print_string d;
faf9a90c
C
887 print_between force_newline
888 (whencode (dots force_newline (statement "")) (statement "")) whn;
889 force_newline())
890 else raise CantBeInPlus
34e49164 891
ae4735db 892 | Ast.OptStm(s) | Ast.UniqueStm(s) ->
34e49164
C
893 raise CantBeInPlus
894
faf9a90c
C
895and whencode notfn alwaysfn = function
896 Ast.WhenNot a ->
708f4980 897 print_text " WHEN != "; notfn a
faf9a90c 898 | Ast.WhenAlways a ->
708f4980
C
899 print_text " WHEN = "; alwaysfn a
900 | Ast.WhenModifier x -> print_text " WHEN "; print_when_modif x
faf9a90c 901 | Ast.WhenNotTrue a ->
708f4980 902 print_text " WHEN != TRUE "; rule_elem "" a
faf9a90c 903 | Ast.WhenNotFalse a ->
708f4980 904 print_text " WHEN != FALSE "; rule_elem "" a
faf9a90c
C
905
906and print_when_modif = function
708f4980
C
907 | Ast.WhenAny -> print_text "ANY"
908 | Ast.WhenStrict -> print_text "STRICT"
909 | Ast.WhenForall -> print_text "FORALL"
910 | Ast.WhenExists -> print_text "EXISTS"
faf9a90c 911
34e49164
C
912and case_line arity c =
913 match Ast.unwrap c with
914 Ast.CaseLine(header,code) ->
708f4980 915 rule_elem arity header; pr_space();
34e49164
C
916 dots force_newline (statement arity) code
917 | Ast.OptCase(case) -> raise CantBeInPlus in
918
919let top_level t =
920 match Ast.unwrap t with
921 Ast.FILEINFO(old_file,new_file) -> raise CantBeInPlus
922 | Ast.DECL(stmt) -> statement "" stmt
923 | Ast.CODE(stmt_dots) -> dots force_newline (statement "") stmt_dots
924 | Ast.ERRORWORDS(exps) -> raise CantBeInPlus
925in
926
ae4735db 927(*
34e49164
C
928let rule =
929 print_between (function _ -> force_newline(); force_newline()) top_level
930in
931*)
932
933let if_open_brace = function "{" -> true | _ -> false in
934
0708f913 935(* boolean result indicates whether an indent is needed *)
34e49164
C
936let rec pp_any = function
937 (* assert: normally there is only CONTEXT NOTHING tokens in any *)
938 Ast.FullTypeTag(x) -> fullType x; false
708f4980
C
939 | Ast.BaseTypeTag(x) -> baseType x unknown unknown; false
940 | Ast.StructUnionTag(x) -> structUnion x unknown unknown; false
941 | Ast.SignTag(x) -> sign x unknown unknown; false
34e49164
C
942
943 | Ast.IdentTag(x) -> ident x; false
944
945 | Ast.ExpressionTag(x) -> expression x; false
946
708f4980
C
947 | Ast.ConstantTag(x) -> constant x unknown unknown; false
948 | Ast.UnaryOpTag(x) -> unaryOp x unknown unknown; false
949 | Ast.AssignOpTag(x) -> assignOp x unknown unknown; false
950 | Ast.FixOpTag(x) -> fixOp x unknown unknown; false
951 | Ast.BinaryOpTag(x) -> binaryOp x unknown unknown; false
952 | Ast.ArithOpTag(x) -> arithOp x unknown unknown; false
953 | Ast.LogicalOpTag(x) -> logicalOp x unknown unknown; false
34e49164
C
954
955 | Ast.InitTag(x) -> initialiser false x; false
956 | Ast.DeclarationTag(x) -> declaration x; false
957
708f4980
C
958 | Ast.StorageTag(x) -> storage x unknown unknown; false
959 | Ast.IncFileTag(x) -> inc_file x unknown unknown; false
34e49164
C
960
961 | Ast.Rule_elemTag(x) -> rule_elem "" x; false
962 | Ast.StatementTag(x) -> statement "" x; false
963 | Ast.CaseLineTag(x) -> case_line "" x; false
964
708f4980 965 | Ast.ConstVolTag(x) -> const_vol x unknown unknown; false
c3e37e97
C
966 | Ast.Pragma(xs) ->
967 let print = function Ast.Noindent s | Ast.Indent s -> print_text s in
968 print_between force_newline print xs; false
708f4980 969 | Ast.Token(x,None) -> print_text x; if_open_brace x
ae4735db 970 | Ast.Token(x,Some info) ->
34e49164 971 mcode
708f4980 972 (fun x line lcol ->
34e49164 973 (match x with
708f4980 974 "else" -> force_newline()
34e49164 975 | _ -> ());
002099fc 976 print_string x line lcol)
faf9a90c
C
977 (let nomcodekind = Ast.CONTEXT(Ast.DontCarePos,Ast.NOTHING) in
978 (x,info,nomcodekind,Ast.NoMetaPos));
34e49164
C
979 if_open_brace x
980
981 | Ast.Code(x) -> let _ = top_level x in false
982
ae4735db 983 (* this is not '...', but a list of expr/statement/params, and
34e49164
C
984 normally there should be no '...' inside them *)
985 | Ast.ExprDotsTag(x) -> dots (function _ -> ()) expression x; false
986 | Ast.ParamDotsTag(x) -> parameter_list x; false
708f4980
C
987 | Ast.StmtDotsTag(x) -> dots force_newline (statement "") x; false
988 | Ast.DeclDotsTag(x) -> dots force_newline declaration x; false
34e49164
C
989
990 | Ast.TypeCTag(x) -> typeC x; false
991 | Ast.ParamTag(x) -> parameterTypeDef x; false
992 | Ast.SgrepStartTag(x) -> failwith "unexpected start tag"
993 | Ast.SgrepEndTag(x) -> failwith "unexpected end tag"
994in
995
faf9a90c
C
996 anything := (function x -> let _ = pp_any x in ());
997
34e49164
C
998 (* todo? imitate what is in pretty_print_cocci ? *)
999 match xxs with
1000 [] -> ()
ae4735db 1001 | x::xs ->
34e49164
C
1002 (* for many tags, we must not do a newline before the first '+' *)
1003 let isfn s =
1004 match Ast.unwrap s with Ast.FunDecl _ -> true | _ -> false in
1005 let unindent_before = function
1006 (* need to get unindent before newline for } *)
1007 (Ast.Token ("}",_)::_) -> true
1008 | _ -> false in
1009 let prnl x =
c3e37e97 1010 (if unindent_before x then unindent true);
708f4980 1011 force_newline() in
34e49164 1012 let newline_before _ =
b1b2de81 1013 if before =*= After
34e49164
C
1014 then
1015 let hd = List.hd xxs in
1016 match hd with
708f4980
C
1017 (Ast.StatementTag s::_) when isfn s ->
1018 force_newline(); force_newline()
0708f913 1019 | (Ast.Pragma _::_)
34e49164
C
1020 | (Ast.Rule_elemTag _::_) | (Ast.StatementTag _::_)
1021 | (Ast.InitTag _::_)
1022 | (Ast.DeclarationTag _::_) | (Ast.Token ("}",_)::_) -> prnl hd
1023 | _ -> () in
1024 let newline_after _ =
b1b2de81 1025 if before =*= Before
34e49164
C
1026 then
1027 match List.rev(List.hd(List.rev xxs)) with
0708f913 1028 (Ast.StatementTag s::_) ->
708f4980
C
1029 (if isfn s then force_newline());
1030 force_newline()
0708f913
C
1031 | (Ast.Pragma _::_)
1032 | (Ast.Rule_elemTag _::_) | (Ast.InitTag _::_)
708f4980
C
1033 | (Ast.DeclarationTag _::_) | (Ast.Token ("{",_)::_) ->
1034 force_newline()
34e49164
C
1035 | _ -> () in
1036 (* print a newline at the beginning, if needed *)
1037 newline_before();
1038 (* print a newline before each of the rest *)
1039 let rec loop leading_newline indent_needed = function
1040 [] -> ()
1041 | x::xs ->
1042 (if leading_newline
1043 then
1044 match (indent_needed,unindent_before x) with
708f4980
C
1045 (true,true) -> force_newline()
1046 | (true,false) -> force_newline(); indent()
c3e37e97 1047 | (false,true) -> unindent true; force_newline()
708f4980 1048 | (false,false) -> force_newline());
002099fc
C
1049 let space_needed_before = function
1050 Ast.ParamTag(x) ->
1051 (match Ast.unwrap x with
1052 Ast.PComma _ -> false
1053 | _ -> true)
1054 | Ast.ExpressionTag(x) ->
1055 (match Ast.unwrap x with
1056 Ast.EComma _ -> false
1057 | _ -> true)
1058 | Ast.InitTag(x) ->
1059 (match Ast.unwrap x with
1060 Ast.IComma _ -> false
1061 | _ -> true)
1062 | Ast.Token(t,_) when List.mem t [",";";";"(";")"] -> false
1063 | _ -> true in
1064 let space_needed_after = function
1065 Ast.Token(t,_) when List.mem t ["("] -> (*never needed*) false
1066 | Ast.Token(t,_) when List.mem t ["if";"for";"while";"do"] ->
1067 (* space always needed *)
1068 pr_space(); false
5636bb2c
C
1069 | Ast.ExpressionTag(e) ->
1070 (match Ast.unwrap e with
1071 Ast.EComma _ ->
1072 (* space always needed *)
1073 pr_space(); false
1074 | _ -> true)
1075 | t -> true in
34e49164 1076 let indent_needed =
002099fc
C
1077 let rec loop space_after indent_needed = function
1078 [] -> indent_needed
1079 | x::xs ->
1080 (if space_after && space_needed_before x
1081 then pr_space());
1082 let indent_needed = pp_any x in
1083 let space_after = space_needed_after x in
1084 loop space_after indent_needed xs in
1085 loop false false x in
34e49164
C
1086 loop true indent_needed xs in
1087 loop false false (x::xs);
1088 (* print a newline at the end, if needed *)
1089 newline_after()
1090
951c7801
C
1091let rec pp_list_list_any (envs, pr, pr_celem, pr_cspace, pr_space, pr_arity,
1092 pr_barrier, indent, unindent)
1093 generating xxs before =
1094 List.iter
1095 (function env ->
1096 do_all (env, pr, pr_celem, pr_cspace, pr_space, pr_arity, pr_barrier,
1097 indent, unindent)
1098 generating xxs before)
1099 envs