Release coccinelle-0.2.0rc1
[bpt/coccinelle.git] / parsing_cocci / unparse_ast0.ml
1 open Format
2 module Ast0 = Ast0_cocci
3 module U = Pretty_print_cocci
4
5 let quiet = ref true (* true = no decoration on - context, etc *)
6
7 let full_ids = ref false (* true = print rule name as well *)
8
9 let start_block str =
10 force_newline(); print_string " "; open_box 0
11
12 let end_block str =
13 close_box(); force_newline ()
14
15 let print_option = Common.do_option
16 let print_between = Common.print_between
17
18 (* --------------------------------------------------------------------- *)
19 (* Positions *)
20
21 let meta_pos = function
22 Ast0.MetaPos(name,_,_) ->
23 print_string "@";
24 let (_,name) = Ast0.unwrap_mcode name in
25 print_string name
26 | Ast0.NoMetaPos -> ()
27
28 (* --------------------------------------------------------------------- *)
29 (* Modified code *)
30
31 let mcodekind brackets fn x info mc =
32 List.iter (function (s,_) -> print_string s) info.Ast0.strings_before;
33 (match mc with
34 Ast0.MINUS(plus_stream) ->
35 let (lb,rb) =
36 if !quiet
37 then ("","")
38 else
39 match brackets with
40 Some x -> ("[","]^"^(string_of_int x))
41 | None -> ("","") in
42 let (plus_stream,_) = !plus_stream in
43 if !quiet
44 then fn x
45 else (print_string "-";
46 print_string lb; fn x; print_string rb);
47 U.print_anything ">>> " plus_stream
48 | Ast0.CONTEXT(plus_streams) ->
49 let (lb,rb) =
50 if !quiet
51 then ("","")
52 else
53 match brackets with
54 Some x -> ("[",("]^"^(string_of_int x))) | None -> ("","") in
55 let (plus_streams,t1,t2) = !plus_streams in
56 U.print_around
57 (function x ->
58 print_string lb; fn x; print_string rb)
59 x plus_streams
60 | Ast0.PLUS _ -> print_int (info.Ast0.pos_info.Ast0.column); fn x
61 | Ast0.MIXED(plus_streams) ->
62 let (lb,rb) =
63 if !quiet
64 then ("","")
65 else
66 let n =
67 match brackets with Some x -> "^"^(string_of_int x) | None -> "" in
68 ("§","½"^n) in
69 let (plus_streams,_,_) = !plus_streams in
70 U.print_around (function x -> print_string lb; fn x; print_string rb)
71 x plus_streams);
72 List.iter (function (s,_) -> print_string s) info.Ast0.strings_after
73
74 let mcode fn (x,_,info,mc,pos,adj) =
75 let fn x = fn x; meta_pos !pos in
76 mcodekind (Some info.Ast0.pos_info.Ast0.line_start)(*None*) fn x info mc
77
78 let print_context x fn =
79 mcodekind (Some (Ast0.get_line x)) fn () (Ast0.get_info x)
80 (Ast0.get_mcodekind x)
81
82 let print_meta (ctx,name) =
83 if !full_ids then (print_string ctx; print_string ":");
84 print_string name
85
86 (* --------------------------------------------------------------------- *)
87 (* --------------------------------------------------------------------- *)
88 (* Dots *)
89
90 let dots between fn d =
91 print_context d
92 (function _ ->
93 match Ast0.unwrap d with
94 Ast0.DOTS(l) -> print_between between fn l
95 | Ast0.CIRCLES(l) -> print_between between fn l
96 | Ast0.STARS(l) -> print_between between fn l)
97
98 (* --------------------------------------------------------------------- *)
99
100 let print_types = function
101 None -> ()
102 | Some ty ->
103 print_string "/* ";
104 Format.print_flush();
105 print_between (function _ -> print_string ", ") Type_cocci.typeC ty;
106 Format.print_flush();
107 print_string " */"
108
109 (* --------------------------------------------------------------------- *)
110 (* Identifier *)
111
112 let rec ident i =
113 print_context i
114 (function _ ->
115 match Ast0.unwrap i with
116 Ast0.Id(name) -> mcode print_string name
117 | Ast0.MetaId(name,_,_) -> mcode print_meta name
118 | Ast0.MetaFunc(name,_,_) -> mcode print_meta name
119 | Ast0.MetaLocalFunc(name,_,_) -> mcode print_meta name
120 | Ast0.OptIdent(id) -> print_string "?"; ident id
121 | Ast0.UniqueIdent(id) -> print_string "!"; ident id)
122
123 (* --------------------------------------------------------------------- *)
124 (* Expression *)
125
126 let print_string_box s = print_string s; open_box 0
127
128 let rec expression e =
129 print_option Type_cocci.typeC (Ast0.get_type e);
130 print_context e
131 (function _ ->
132 match Ast0.unwrap e with
133 Ast0.Ident(id) -> ident id
134 | Ast0.Constant(const) -> mcode U.constant const
135 | Ast0.FunCall(fn,lp,args,rp) ->
136 expression fn; mcode print_string_box lp;
137 let _ = dots (function _ -> ()) expression args in
138 close_box(); mcode print_string rp
139 | Ast0.Assignment(left,op,right,_) ->
140 expression left; print_string " "; mcode U.assignOp op;
141 print_string " "; expression right
142 | Ast0.CondExpr(exp1,why,exp2,colon,exp3) ->
143 expression exp1; print_string " "; mcode print_string why;
144 print_option (function e -> print_string " "; expression e) exp2;
145 print_string " "; mcode print_string colon; expression exp3
146 | Ast0.Postfix(exp,op) -> expression exp; mcode U.fixOp op
147 | Ast0.Infix(exp,op) -> mcode U.fixOp op; expression exp
148 | Ast0.Unary(exp,op) -> mcode U.unaryOp op; expression exp
149 | Ast0.Binary(left,op,right) ->
150 print_string "(";
151 expression left; print_string " "; mcode U.binaryOp op;
152 print_string " "; expression right;
153 print_string ")"
154 | Ast0.Nested(left,op,right) ->
155 print_string "(";
156 expression left; print_string " "; mcode U.binaryOp op;
157 print_string " "; expression right;
158 print_string ")"
159 | Ast0.Paren(lp,exp,rp) ->
160 mcode print_string_box lp; expression exp; close_box();
161 mcode print_string rp
162 | Ast0.ArrayAccess(exp1,lb,exp2,rb) ->
163 expression exp1; mcode print_string_box lb; expression exp2;
164 close_box(); mcode print_string rb
165 | Ast0.RecordAccess(exp,pt,field) ->
166 expression exp; mcode print_string pt; ident field
167 | Ast0.RecordPtAccess(exp,ar,field) ->
168 expression exp; mcode print_string ar; ident field
169 | Ast0.Cast(lp,ty,rp,exp) ->
170 mcode print_string_box lp; typeC ty; close_box();
171 mcode print_string rp; expression exp
172 | Ast0.SizeOfExpr(szf,exp) ->
173 mcode print_string szf; expression exp
174 | Ast0.SizeOfType(szf,lp,ty,rp) ->
175 mcode print_string szf;
176 mcode print_string_box lp; typeC ty; close_box();
177 mcode print_string rp
178 | Ast0.TypeExp(ty) -> typeC ty
179 | Ast0.MetaErr(name,_,_) -> mcode print_meta name
180 | Ast0.MetaExpr(name,_,ty,_,pure) ->
181 mcode print_meta name; print_types ty(*;
182 print_string "^";
183 (match pure with
184 Ast0.Pure -> print_string "pure"
185 | Ast0.Impure -> print_string "impure"
186 | Ast0.Context -> print_string "context"
187 | Ast0.PureContext -> print_string "pure_context")*)
188 | Ast0.MetaExprList(name,_,_) -> mcode print_meta name
189 | Ast0.EComma(cm) -> mcode print_string cm; print_space()
190 | Ast0.DisjExpr(_,exp_list,_,_) ->
191 print_string "\n("; force_newline();
192 print_between
193 (function _ -> print_string "\n|"; force_newline())
194 expression exp_list;
195 print_string "\n)"
196 | Ast0.NestExpr(starter,expr_dots,ender,None,multi) ->
197 mcode print_string starter;
198 start_block(); dots force_newline expression expr_dots; end_block();
199 mcode print_string ender
200 | Ast0.NestExpr(starter,expr_dots,ender,Some whencode,multi) ->
201 mcode print_string starter; print_string " WHEN != ";
202 expression whencode;
203 start_block(); dots force_newline expression expr_dots; end_block();
204 mcode print_string ender
205 | Ast0.Edots(dots,Some whencode)
206 | Ast0.Ecircles(dots,Some whencode)
207 | Ast0.Estars(dots,Some whencode) ->
208 mcode print_string dots; print_string " WHEN != ";
209 expression whencode
210 | Ast0.Edots(dots,None)
211 | Ast0.Ecircles(dots,None)
212 | Ast0.Estars(dots,None) -> mcode print_string dots
213 | Ast0.OptExp(exp) -> print_string "?"; expression exp
214 | Ast0.UniqueExp(exp) -> print_string "!"; expression exp)
215
216 and expression_dots x = dots (function _ -> ()) expression x
217
218 (* --------------------------------------------------------------------- *)
219 (* Types *)
220
221 and print_function_pointer (ty,lp1,star,rp1,lp2,params,rp2) fn =
222 typeC ty; mcode print_string lp1; mcode print_string star; fn();
223 mcode print_string rp1; mcode print_string lp2;
224 parameter_list params; mcode print_string rp2
225
226 and print_function_type (ty,lp1,params,rp1) fn =
227 print_option typeC ty; fn(); mcode print_string lp1;
228 parameter_list params; mcode print_string rp1
229
230 and typeC t =
231 print_context t
232 (function _ ->
233 match Ast0.unwrap t with
234 Ast0.ConstVol(cv,ty) ->
235 mcode U.const_vol cv; print_string " "; typeC ty
236 | Ast0.BaseType(ty,strings) ->
237 List.iter (function s -> mcode print_string s; print_string " ")
238 strings
239 | Ast0.Signed(sgn,ty) -> mcode U.sign sgn; print_option typeC ty
240 | Ast0.Pointer(ty,star) -> typeC ty; mcode print_string star
241 | Ast0.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) ->
242 print_function_pointer (ty,lp1,star,rp1,lp2,params,rp2)
243 (function _ -> ())
244 | Ast0.FunctionType(ty,lp1,params,rp1) ->
245 print_function_type (ty,lp1,params,rp1) (function _ -> ())
246 | Ast0.Array(ty,lb,size,rb) ->
247 typeC ty; mcode print_string lb; print_option expression size;
248 mcode print_string rb
249 | Ast0.EnumName(kind,name) -> mcode print_string kind; print_string " ";
250 ident name
251 | Ast0.StructUnionName(kind,name) ->
252 mcode U.structUnion kind;
253 print_option (function x -> ident x; print_string " ") name
254 | Ast0.StructUnionDef(ty,lb,decls,rb) ->
255 typeC ty; mcode print_string lb;
256 dots force_newline declaration decls;
257 mcode print_string rb
258 | Ast0.TypeName(name)-> mcode print_string name; print_string " "
259 | Ast0.MetaType(name,_)-> mcode print_meta name; print_string " "
260 | Ast0.DisjType(lp,types,mids,rp) ->
261 print_string "\n"; mcode print_string lp; force_newline();
262 print_between
263 (function _ -> print_string "\n|"; force_newline())
264 typeC types;
265 print_string "\n"; mcode print_string rp
266 | Ast0.OptType(ty) -> print_string "?"; typeC ty
267 | Ast0.UniqueType(ty) -> print_string "!"; typeC ty)
268
269 (* --------------------------------------------------------------------- *)
270 (* Variable declaration *)
271 (* Even if the Cocci program specifies a list of declarations, they are
272 split out into multiple declarations of a single variable each. *)
273
274 and print_named_type ty id =
275 match Ast0.unwrap ty with
276 Ast0.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) ->
277 print_function_pointer (ty,lp1,star,rp1,lp2,params,rp2)
278 (function _ -> print_string " "; ident id)
279 | Ast0.FunctionType(ty,lp1,params,rp1) ->
280 print_function_type (ty,lp1,params,rp1)
281 (function _ -> print_string " "; ident id)
282 | Ast0.Array(ty,lb,size,rb) ->
283 let rec loop ty k =
284 match Ast0.unwrap ty with
285 Ast0.Array(ty,lb,size,rb) ->
286 loop ty
287 (function _ ->
288 k ();
289 mcode print_string lb;
290 print_option expression size;
291 mcode print_string rb)
292 | _ -> typeC ty; ident id; k () in
293 loop ty (function _ -> ())
294 | _ -> typeC ty; ident id
295
296 and declaration d =
297 print_context d
298 (function _ ->
299 match Ast0.unwrap d with
300 Ast0.Init(stg,ty,id,eq,ini,sem) ->
301 print_option (mcode U.storage) stg;
302 print_named_type ty id;
303 print_string " ";
304 mcode print_string eq; print_string " "; initialiser ini;
305 mcode print_string sem
306 | Ast0.UnInit(stg,ty,id,sem) ->
307 print_option (mcode U.storage) stg; print_named_type ty id;
308 mcode print_string sem
309 | Ast0.MacroDecl(name,lp,args,rp,sem) ->
310 ident name; mcode print_string_box lp;
311 let _ = dots (function _ -> ()) expression args in
312 close_box(); mcode print_string rp; mcode print_string sem
313 | Ast0.TyDecl(ty,sem) -> typeC ty; mcode print_string sem
314 | Ast0.Typedef(stg,ty,id,sem) ->
315 mcode print_string stg; typeC ty; typeC id;
316 mcode print_string sem
317 | Ast0.DisjDecl(_,decls,_,_) ->
318 print_string "\n("; force_newline();
319 print_between
320 (function _ -> print_string "\n|"; force_newline())
321 declaration decls;
322 print_string "\n)"
323 | Ast0.Ddots(dots,Some whencode) ->
324 mcode print_string dots; print_string " when != ";
325 declaration whencode
326 | Ast0.Ddots(dots,None) -> mcode print_string dots
327 | Ast0.OptDecl(decl) -> print_string "?"; declaration decl
328 | Ast0.UniqueDecl(decl) -> print_string "!"; declaration decl)
329
330 and declaration_dots l = dots (function _ -> ()) declaration l
331
332 (* --------------------------------------------------------------------- *)
333 (* Initialiser *)
334
335 and initialiser i =
336 print_context i
337 (function _ ->
338 match Ast0.unwrap i with
339 Ast0.MetaInit(name,_)-> mcode print_meta name; print_string " "
340 | Ast0.InitExpr(exp) -> expression exp
341 | Ast0.InitList(lb,initlist,rb) ->
342 mcode print_string lb; open_box 0;
343 let _ = dots (function _ -> ()) initialiser initlist in
344 close_box(); mcode print_string rb
345 | Ast0.InitGccExt(designators,eq,ini) ->
346 List.iter designator designators; print_string " ";
347 mcode print_string eq; print_string " "; initialiser ini
348 | Ast0.InitGccName(name,eq,ini) ->
349 ident name; mcode print_string eq; initialiser ini
350 | Ast0.IComma(cm) -> mcode print_string cm; force_newline()
351 | Ast0.Idots(d,Some whencode) ->
352 mcode print_string d; print_string " WHEN != ";
353 initialiser whencode
354 | Ast0.Idots(d,None) -> mcode print_string d
355 | Ast0.OptIni(ini) -> print_string "?"; initialiser ini
356 | Ast0.UniqueIni(ini) -> print_string "!"; initialiser ini)
357
358 and designator = function
359 Ast0.DesignatorField(dot,id) -> mcode print_string dot; ident id
360 | Ast0.DesignatorIndex(lb,exp,rb) ->
361 mcode print_string lb; expression exp; mcode print_string rb
362 | Ast0.DesignatorRange(lb,min,dots,max,rb) ->
363 mcode print_string lb; expression min; mcode print_string dots;
364 expression max; mcode print_string rb
365
366 and initialiser_list l = dots (function _ -> ()) initialiser l
367
368 (* --------------------------------------------------------------------- *)
369 (* Parameter *)
370
371 and parameterTypeDef p =
372 print_context p
373 (function _ ->
374 match Ast0.unwrap p with
375 Ast0.VoidParam(ty) -> typeC ty
376 | Ast0.Param(ty,Some id) -> print_named_type ty id
377 | Ast0.Param(ty,None) -> typeC ty
378 | Ast0.MetaParam(name,_) -> mcode print_meta name
379 | Ast0.MetaParamList(name,_,_) -> mcode print_meta name
380 | Ast0.PComma(cm) -> mcode print_string cm; print_space()
381 | Ast0.Pdots(dots) -> mcode print_string dots
382 | Ast0.Pcircles(dots) -> mcode print_string dots
383 | Ast0.OptParam(param) -> print_string "?"; parameterTypeDef param
384 | Ast0.UniqueParam(param) -> print_string "!"; parameterTypeDef param)
385
386 and parameter_list l = dots (function _ -> ()) parameterTypeDef l
387
388 (* --------------------------------------------------------------------- *)
389 (* Top-level code *)
390
391 and statement arity s =
392 print_context s
393 (function _ ->
394 match Ast0.unwrap s with
395 Ast0.FunDecl(_,fninfo,name,lp,params,rp,lbrace,body,rbrace) ->
396 print_string arity;
397 List.iter print_fninfo fninfo;
398 ident name; mcode print_string_box lp;
399 parameter_list params; close_box(); mcode print_string rp;
400 print_string " ";
401 print_string arity; mcode print_string lbrace; start_block();
402 dots force_newline (statement arity) body;
403 end_block(); print_string arity; mcode print_string rbrace
404 | Ast0.Decl(_,decl) -> print_string arity; declaration decl
405 | Ast0.Seq(lbrace,body,rbrace) ->
406 print_string arity; mcode print_string lbrace; start_block();
407 dots force_newline (statement arity) body;
408 end_block(); print_string arity; mcode print_string rbrace
409 | Ast0.ExprStatement(exp,sem) ->
410 print_string arity; expression exp; mcode print_string sem
411 | Ast0.IfThen(iff,lp,exp,rp,branch1,(info,aft)) ->
412 print_string arity;
413 mcode print_string iff; print_string " "; mcode print_string_box lp;
414 expression exp; close_box(); mcode print_string rp; print_string " ";
415 statement arity branch1;
416 mcode (function _ -> ()) ((),(),info,aft,ref Ast0.NoMetaPos,-1)
417 | Ast0.IfThenElse(iff,lp,exp,rp,branch1,els,branch2,(info,aft)) ->
418 print_string arity;
419 mcode print_string iff; print_string " "; mcode print_string_box lp;
420 expression exp; close_box(); mcode print_string rp; print_string " ";
421 statement arity branch1;
422 print_string arity; mcode print_string els; print_string " ";
423 statement arity branch2;
424 mcode (function _ -> ()) ((),(),info,aft,ref Ast0.NoMetaPos,-1)
425 | Ast0.While(whl,lp,exp,rp,body,(info,aft)) ->
426 print_string arity;
427 mcode print_string whl; print_string " "; mcode print_string_box lp;
428 expression exp; close_box(); mcode print_string rp; print_string " ";
429 statement arity body;
430 mcode (function _ -> ()) ((),(),info,aft,ref Ast0.NoMetaPos,-1)
431 | Ast0.Do(d,body,whl,lp,exp,rp,sem) ->
432 print_string arity; mcode print_string d; print_string " ";
433 statement arity body;
434 print_string arity;
435 mcode print_string whl; print_string " "; mcode print_string_box lp;
436 expression exp; close_box(); mcode print_string rp;
437 mcode print_string sem
438 | Ast0.For(fr,lp,e1,sem1,e2,sem2,e3,rp,body,(info,aft)) ->
439 print_string arity;
440 mcode print_string fr; mcode print_string_box lp;
441 print_option expression e1; mcode print_string sem1;
442 print_option expression e2; mcode print_string sem2;
443 print_option expression e3; close_box();
444 mcode print_string rp; print_string " "; statement arity body;
445 mcode (function _ -> ()) ((),(),info,aft,ref Ast0.NoMetaPos,-1)
446 | Ast0.Iterator(nm,lp,args,rp,body,(info,aft)) ->
447 print_string arity;
448 ident nm; print_string " "; mcode print_string_box lp;
449 let _ = dots (function _ -> ()) expression args in
450 close_box(); mcode print_string rp; print_string " ";
451 statement arity body;
452 mcode (function _ -> ()) ((),(),info,aft,ref Ast0.NoMetaPos,-1)
453 | Ast0.Switch(switch,lp,exp,rp,lb,decls,cases,rb) ->
454 print_string arity;
455 mcode print_string switch; print_string " ";
456 mcode print_string_box lp; expression exp; close_box();
457 mcode print_string rp; print_string " "; mcode print_string lb;
458 dots force_newline (statement arity) decls;
459 dots force_newline (case_line arity) cases;
460 mcode print_string rb
461 | Ast0.Break(br,sem) ->
462 print_string arity; mcode print_string br; mcode print_string sem
463 | Ast0.Continue(cont,sem) ->
464 print_string arity; mcode print_string cont; mcode print_string sem
465 | Ast0.Label(l,dd) -> ident l; print_string ":"
466 | Ast0.Goto(goto,l,sem) ->
467 mcode print_string goto; ident l; mcode print_string sem
468 | Ast0.Return(ret,sem) ->
469 print_string arity; mcode print_string ret; mcode print_string sem
470 | Ast0.ReturnExpr(ret,exp,sem) ->
471 print_string arity; mcode print_string ret; print_string " ";
472 expression exp; mcode print_string sem
473 | Ast0.MetaStmt(name,pure) ->
474 print_string arity; mcode print_meta name;(*
475 print_string "^";
476 (match pure with
477 Ast0.Pure -> print_string "pure"
478 | Ast0.Impure -> print_string "impure"
479 | Ast0.Context -> print_string "context"
480 | Ast0.PureContext -> print_string "pure_context")*)
481 | Ast0.MetaStmtList(name,_) ->
482 print_string arity; mcode print_meta name
483 | Ast0.Disj(starter,statement_dots_list,_,ender) ->
484 print_string arity;
485 print_string "\n"; mcode print_string starter; force_newline();
486 print_between
487 (function _ -> print_string "\n|"; force_newline())
488 (dots force_newline (statement arity))
489 statement_dots_list;
490 print_string "\n"; mcode print_string ender
491 | Ast0.Nest(starter,stmt_dots,ender,whn,multi) ->
492 print_string arity;
493 mcode print_string starter;
494 open_box 0;
495 List.iter
496 (whencode (dots force_newline (statement "")) (statement ""))
497 whn;
498 close_box();
499 start_block();
500 dots force_newline (statement arity) stmt_dots;
501 end_block();
502 mcode print_string ender
503 | Ast0.Exp(exp) -> print_string arity; expression exp
504 | Ast0.TopExp(exp) -> print_string arity; expression exp
505 | Ast0.Ty(ty) -> print_string arity; typeC ty
506 | Ast0.TopInit(init) -> initialiser init
507 | Ast0.Dots(d,whn) | Ast0.Circles(d,whn) | Ast0.Stars(d,whn) ->
508 print_string arity; mcode print_string d;
509 List.iter
510 (whencode (dots force_newline (statement "")) (statement ""))
511 whn
512 | Ast0.Include(inc,s) ->
513 mcode print_string inc; print_string " "; mcode U.inc_file s
514 | Ast0.Define(def,id,params,body) ->
515 mcode print_string def; print_string " "; ident id;
516 print_define_parameters params;
517 print_string " ";
518 dots force_newline (statement arity) body
519 | Ast0.OptStm(re) -> statement "?" re
520 | Ast0.UniqueStm(re) -> statement "!" re)
521
522 and print_define_parameters params =
523 match Ast0.unwrap params with
524 Ast0.NoParams -> ()
525 | Ast0.DParams(lp,params,rp) ->
526 mcode print_string lp;
527 dots (function _ -> ()) print_define_param params; mcode print_string rp
528
529 and print_define_param param =
530 match Ast0.unwrap param with
531 Ast0.DParam(id) -> ident id
532 | Ast0.DPComma(comma) -> mcode print_string comma
533 | Ast0.DPdots(dots) -> mcode print_string dots
534 | Ast0.DPcircles(circles) -> mcode print_string circles
535 | Ast0.OptDParam(dp) -> print_string "?"; print_define_param dp
536 | Ast0.UniqueDParam(dp) -> print_string "!"; print_define_param dp
537
538 and print_fninfo = function
539 Ast0.FStorage(stg) -> mcode U.storage stg
540 | Ast0.FType(ty) -> typeC ty
541 | Ast0.FInline(inline) -> mcode print_string inline
542 | Ast0.FAttr(attr) -> mcode print_string attr
543
544 and whencode notfn alwaysfn = function
545 Ast0.WhenNot a ->
546 print_string " WHEN != "; open_box 0; notfn a; close_box()
547 | Ast0.WhenAlways a ->
548 print_string " WHEN = "; open_box 0; alwaysfn a; close_box()
549 | Ast0.WhenModifier x -> print_string " WHEN "; U.print_when_modif x
550 | Ast0.WhenNotTrue a ->
551 print_string " WHEN != TRUE "; open_box 0; expression a; close_box()
552 | Ast0.WhenNotFalse a ->
553 print_string " WHEN != FALSE "; open_box 0; expression a; close_box()
554
555 and case_line arity c =
556 print_context c
557 (function _ ->
558 match Ast0.unwrap c with
559 Ast0.Default(def,colon,code) ->
560 print_string arity;
561 mcode print_string def; mcode print_string colon; print_string " ";
562 dots force_newline (statement arity) code
563 | Ast0.Case(case,exp,colon,code) ->
564 print_string arity;
565 mcode print_string case; print_string " "; expression exp;
566 mcode print_string colon; print_string " ";
567 dots force_newline (statement arity) code
568 | Ast0.DisjCase(starter,case_lines,mids,ender) ->
569 print_string "\n("; force_newline();
570 print_between
571 (function _ -> print_string "\n|"; force_newline())
572 (case_line arity) case_lines;
573 print_string "\n)"
574 | Ast0.OptCase(case) -> case_line "?" case)
575
576 and statement_dots l = dots (function _ -> ()) (statement "") l
577 and case_dots l = dots (function _ -> ()) (case_line "") l
578
579 (* --------------------------------------------------------------------- *)
580 (* Top level code *)
581
582 let top_level t =
583 print_context t
584 (function _ ->
585 match Ast0.unwrap t with
586 Ast0.FILEINFO(old_file,new_file) ->
587 print_string "--- "; mcode print_string old_file; force_newline();
588 print_string "+++ "; mcode print_string new_file
589 | Ast0.DECL(stmt) -> statement "" stmt
590 | Ast0.CODE(stmt_dots) ->
591 dots force_newline (statement "") stmt_dots
592 | Ast0.ERRORWORDS(exps) ->
593 print_string "error words = [";
594 print_between (function _ -> print_string ", ") expression exps;
595 print_string "]"
596 | Ast0.OTHER(s) ->
597 print_string "OTHER("; statement "" s; print_string ")")
598
599 let rule =
600 print_between (function _ -> force_newline(); force_newline()) top_level
601
602 let unparse_anything x =
603 let q = !quiet in
604 quiet := true;
605 (match x with
606 Ast0.DotsExprTag(d) ->
607 print_string "ExpDots:"; force_newline();
608 expression_dots d
609 | Ast0.DotsParamTag(d) ->
610 parameter_list d
611 | Ast0.DotsInitTag(d) ->
612 initialiser_list d
613 | Ast0.DotsStmtTag(d) ->
614 print_string "StmDots:"; force_newline();
615 statement_dots d
616 | Ast0.DotsDeclTag(d) ->
617 declaration_dots d
618 | Ast0.DotsCaseTag(d) ->
619 case_dots d
620 | Ast0.IdentTag(d) ->
621 ident d
622 | Ast0.ExprTag(d) | Ast0.ArgExprTag(d) | Ast0.TestExprTag(d) ->
623 print_string "Exp:"; force_newline();
624 expression d
625 | Ast0.TypeCTag(d) ->
626 typeC d
627 | Ast0.ParamTag(d) ->
628 parameterTypeDef d
629 | Ast0.InitTag(d) ->
630 initialiser d
631 | Ast0.DeclTag(d) ->
632 declaration d
633 | Ast0.StmtTag(d) ->
634 print_string "Stm:"; force_newline();
635 statement "" d
636 | Ast0.CaseLineTag(d) ->
637 case_line "" d
638 | Ast0.TopTag(d) ->
639 top_level d
640 | Ast0.IsoWhenTag(x) -> U.print_when_modif x
641 | Ast0.IsoWhenTTag(e) -> expression e
642 | Ast0.IsoWhenFTag(e) -> expression e
643 | Ast0.MetaPosTag(var) -> meta_pos var);
644 quiet := q;
645 print_newline()
646
647 let unparse x =
648 print_string "\n@@\n@@";
649 force_newline();
650 force_newline();
651 rule x;
652 print_newline()
653
654 let unparse_to_string x = Common.format_to_string (function _ -> unparse x)