coccinelle release 1.0.0-rc2
[bpt/coccinelle.git] / popl09 / pretty_print_popl.ml
1 (*
2 * Copyright 2010, INRIA, University of Copenhagen
3 * Julia Lawall, Rene Rydhof Hansen, Gilles Muller, Nicolas Palix
4 * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
5 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
6 * This file is part of Coccinelle.
7 *
8 * Coccinelle is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, according to version 2 of the License.
11 *
12 * Coccinelle is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
19 *
20 * The authors reserve the right to distribute this or future versions of
21 * Coccinelle under other licenses.
22 *)
23
24
25 open Format
26 module Past = Ast_popl
27 module Ast = Ast_cocci
28
29 let start_block str =
30 force_newline(); print_string " "; open_box 0
31
32 let end_block str =
33 close_box(); force_newline ()
34
35 (* --------------------------------------------------------------------- *)
36
37 let print_around printer term = function
38 Ast.NOTHING -> printer term
39 | Ast.BEFORE(bef,_) ->
40 Pretty_print_cocci.print_anything "<<< " bef; printer term
41 | Ast.AFTER(aft,_) ->
42 printer term; Pretty_print_cocci.print_anything ">>> " aft
43 | Ast.BEFOREAFTER(bef,aft,_) ->
44 Pretty_print_cocci.print_anything "<<< " bef; printer term;
45 Pretty_print_cocci.print_anything ">>> " aft
46
47 let mcode fn = function
48 (x, _, Ast.MINUS(_,_,_,plus_stream), pos) ->
49 print_string "-"; fn x;
50 (match plus_stream with
51 Ast.NOREPLACEMENT -> ()
52 | Ast.REPLACEMENT(plus_stream,_) ->
53 Pretty_print_cocci.print_anything ">>> " plus_stream)
54 | (x, _, Ast.CONTEXT(_,plus_streams), pos) ->
55 print_around fn x plus_streams
56 | (x, info, Ast.PLUS _, pos) -> fn x
57
58 (* --------------------------------------------------------------------- *)
59
60 let rec print_sequence = function
61 Past.Seq(e,seq) -> print_element e; force_newline(); print_sequence seq
62 | Past.Empty -> ()
63 | Past.SExists((_,v),seq) -> print_string "exists "; print_string v;
64 print_string " ."; force_newline(); print_sequence seq
65
66 and print_term = function
67 Past.Atomic(term) -> Pretty_print_cocci.rule_elem "" term
68 | Past.IfThen(test,thn,(_,_,_,aft)) ->
69 print_term test; print_term thn;
70 mcode (function _ -> ()) ((),Ast.no_info,aft,[])
71 | Past.TExists((_,v),term) -> print_string "exists "; print_string v;
72 print_string " ."; force_newline(); print_term term
73
74 and print_element = function
75 Past.Term(term,_) -> print_term term
76 | Past.Or(seq1,seq2) ->
77 force_newline(); print_string "("; force_newline(); print_sequence seq1;
78 print_string "|"; force_newline(); print_sequence seq2; print_string ")"
79 | Past.DInfo(dots) ->
80 start_block();
81 print_dots dots;
82 end_block()
83 | Past.EExists((_,v),elem) -> print_string "exists "; print_string v;
84 print_string " ."; force_newline(); print_element elem
85
86 and print_dots = function
87 Past.Dots -> print_string "..."
88 | Past.Nest(seq)-> print_string "<..."; start_block(); print_sequence seq;
89 end_block(); print_string "...>"
90 | Past.When(dots,seq) -> print_dots dots; print_string " when != ";
91 open_box 0; print_sequence seq; close_box()
92
93 (* --------------------------------------------------------------------- *)
94
95 let pretty_print_e e =
96 print_element e;
97 print_newline()
98
99 let pretty_print sl =
100 print_sequence sl;
101 print_newline()
102