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