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