- Try to do better pretty printing when array elements are individually
[bpt/coccinelle.git] / python / pycocci_aux.ml
1 (*
2 * Copyright 2012, INRIA
3 * Julia Lawall, Gilles Muller
4 * Copyright 2010-2011, INRIA, University of Copenhagen
5 * Julia Lawall, Rene Rydhof Hansen, Gilles Muller, Nicolas Palix
6 * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
7 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
8 * This file is part of Coccinelle.
9 *
10 * Coccinelle is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, according to version 2 of the License.
13 *
14 * Coccinelle is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
21 *
22 * The authors reserve the right to distribute this or future versions of
23 * Coccinelle under other licenses.
24 *)
25
26
27 # 0 "./pycocci_aux.ml"
28 open Ast_c
29 open Common
30
31 let call_pretty f a =
32 let str = ref ([] : string list) in
33 let pr_elem info = str := (Ast_c.str_of_info info) :: !str in
34 let pr_sp _ = () in
35 f ~pr_elem ~pr_space:pr_sp a;
36 String.concat " " (List.rev !str)
37
38 let exprrep = call_pretty Pretty_print_c.pp_expression_gen
39
40 let commalistrep list_printer elem_printer comma_printer x =
41 (call_pretty list_printer x,
42 List.map
43 (function x ->
44 call_pretty elem_printer (comma_printer x) (* drop commas *))
45 x)
46
47 let exprlistrep =
48 commalistrep Pretty_print_c.pp_arg_list_gen Pretty_print_c.pp_arg_gen
49 Ast_c.unwrap
50
51 let paramlistrep =
52 commalistrep Pretty_print_c.pp_param_list_gen Pretty_print_c.pp_param_gen
53 Ast_c.unwrap
54
55 let initlistrep =
56 commalistrep Pretty_print_c.pp_init_list_gen Pretty_print_c.pp_init_gen
57 Ast_c.unwrap
58
59 let fieldlistrep =
60 commalistrep Pretty_print_c.pp_field_list_gen Pretty_print_c.pp_field_gen
61 (function x -> x)
62
63 let stringrep = function
64 Ast_c.MetaIdVal (s,_) -> s
65 | Ast_c.MetaFuncVal s -> s
66 | Ast_c.MetaLocalFuncVal s -> s
67 | Ast_c.MetaExprVal (expr,_) -> exprrep expr
68 | Ast_c.MetaExprListVal expr_list ->
69 call_pretty Pretty_print_c.pp_arg_list_gen expr_list
70 | Ast_c.MetaTypeVal typ -> call_pretty Pretty_print_c.pp_type_gen typ
71 | Ast_c.MetaInitVal ini -> call_pretty Pretty_print_c.pp_init_gen ini
72 | Ast_c.MetaInitListVal ini -> call_pretty Pretty_print_c.pp_init_list_gen ini
73 | Ast_c.MetaDeclVal decl ->
74 call_pretty Pretty_print_c.pp_decl_gen decl
75 | Ast_c.MetaFieldVal field ->
76 call_pretty Pretty_print_c.pp_field_gen field
77 | Ast_c.MetaFieldListVal field ->
78 call_pretty Pretty_print_c.pp_field_list_gen field
79 | Ast_c.MetaStmtVal statement ->
80 call_pretty Pretty_print_c.pp_statement_gen statement
81 | Ast_c.MetaParamVal param ->
82 call_pretty Pretty_print_c.pp_param_gen param
83 | Ast_c.MetaParamListVal params ->
84 call_pretty Pretty_print_c.pp_param_list_gen params
85 | Ast_c.MetaListlenVal n -> string_of_int n
86 | Ast_c.MetaPosVal (pos1, pos2) ->
87 let print_pos = function
88 Ast_cocci.Real x -> string_of_int x
89 | Ast_cocci.Virt(x,off) -> Printf.sprintf "%d+%d" x off in
90 Common.sprintf ("pos(%s,%s)") (print_pos pos1) (print_pos pos2)
91 | Ast_c.MetaPosValList positions -> "TODO: <<postvallist>>"
92