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