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