Here is a patch that imports expression lists into python as an instance
of the class ExpressionList, having the methods __getitem__ and __str__.
you can thus write eg:
@r@
expression list es;
@@
f(es)
@script:python@
es << r.es;
@@
print "%s" % es
print "%s" % es[1]
--------------------
The elements of the expression list are stored in the "elements" field of
the ExpressionList structure, and thus is it also possible to say eg
es.elements[1]. The elements list does not contain the commas between the
elements. They have disappeared completely. The elements are just
ordinary strings, not Expression structures. Maybe making them Expression
structures would be a good idea. But maybe getting rid of Expression
structures would be better, since they don't offer any information other
than the string representation.
Suggestions are welcome. If this seems to be what is desired, I will do
the same for the other list types.
julia
----------
diff --git a/parsing_c/pretty_print_c.ml b/parsing_c/pretty_print_c.ml
index aeaf84d..7214714 100644
--- a/parsing_c/pretty_print_c.ml
+++ b/parsing_c/pretty_print_c.ml
@@ -38,6 +38,7 @@ type 'a printer = 'a -> unit
type pretty_printers = {
expression : Ast_c.expression printer;
arg_list : (Ast_c.argument Ast_c.wrap2 list) printer;
+ arg : Ast_c.argument printer;
statement : Ast_c.statement printer;
decl : Ast_c.declaration printer;
field : Ast_c.field printer;
@@ -1311,6 +1312,7 @@ and pp_init (init, iinit) =
{ expression = pp_expression;
arg_list = pp_arg_list;
+ arg = pp_argument;
statement = pp_statement;
decl = pp_decl;
field = pp_field;
@@ -1376,6 +1378,9 @@ let pp_expression_gen ~pr_elem ~pr_space =
let pp_arg_list_gen ~pr_elem ~pr_space =
(pp_elem_sp pr_elem pr_space).arg_list
+let pp_arg_gen ~pr_elem ~pr_space =
+ (pp_elem_sp pr_elem pr_space).arg
+
let pp_statement_gen ~pr_elem ~pr_space =
(pp_elem_sp pr_elem pr_space).statement
diff --git a/parsing_c/pretty_print_c.mli b/parsing_c/pretty_print_c.mli
index 0ea3d2e..b9434f0 100644
--- a/parsing_c/pretty_print_c.mli
+++ b/parsing_c/pretty_print_c.mli
@@ -9,6 +9,7 @@ type 'a printer = 'a -> unit
type pretty_printers = {
expression : Ast_c.expression printer;
arg_list : (Ast_c.argument Ast_c.wrap2 list) printer;
+ arg : Ast_c.argument printer;
statement : Ast_c.statement printer;
decl : Ast_c.declaration printer;
field : Ast_c.field printer;
@@ -37,6 +38,8 @@ val pp_expression_gen: pr_elem:Ast_c.info printer -> pr_space:
unit printer ->
Ast_c.expression printer
val pp_arg_list_gen: pr_elem:Ast_c.info printer -> pr_space: unit printer ->
(Ast_c.argument Ast_c.wrap2 list) printer
+val pp_arg_gen: pr_elem:Ast_c.info printer -> pr_space: unit printer ->
+ Ast_c.argument printer
val pp_decl_gen: pr_elem:Ast_c.info printer -> pr_space: unit printer ->
Ast_c.declaration printer
val pp_field_gen: pr_elem:Ast_c.info printer -> pr_space: unit printer ->
diff --git a/python/coccilib/elems.py b/python/coccilib/elems.py
index 0e794c9..d62d2c9 100644
--- a/python/coccilib/elems.py
+++ b/python/coccilib/elems.py
@@ -11,7 +11,6 @@ class ElemBase:
def __init__(self):
pass
-
class Expression(ElemBase):
def __init__(self, expr):
ElemBase.__init__(self)
@@ -20,6 +19,18 @@ class Expression(ElemBase):
def __str__(self):
return self.expr
+class ExpressionList(ElemBase):
+ def __init__(self, expr, elements):
+ ElemBase.__init__(self)
+ self.expr = expr
+ self.elements = elements
+
+ def __getitem__(self,n):
+ return self.elements[n]
+
+ def __str__(self):
+ return self.expr
+
class Identifier(ElemBase):
def __init__(self, ident):
ElemBase.__init__(self)
diff --git a/python/pycocci_aux.ml b/python/pycocci_aux.ml
index a4b84ef..af03db7 100644
--- a/python/pycocci_aux.ml
+++ b/python/pycocci_aux.ml
@@ -34,6 +34,14 @@ let call_pretty f a =
let exprrep = call_pretty Pretty_print_c.pp_expression_gen
+let exprlistrep x =
+ (call_pretty Pretty_print_c.pp_arg_list_gen x,
+ List.map
+ (function x ->
+ call_pretty Pretty_print_c.pp_arg_gen
+ (Ast_c.unwrap x) (* drop commas *))
+ x)
+
let stringrep = function
Ast_c.MetaIdVal (s,_) -> s
| Ast_c.MetaFuncVal s -> s
diff --git a/python/pycocci_aux.mli b/python/pycocci_aux.mli
index 1630a94..ca1dc53 100644
--- a/python/pycocci_aux.mli
+++ b/python/pycocci_aux.mli
@@ -25,2 +25,3 @@
val exprrep : Ast_c.expression -> string
+val exprlistrep : Ast_c.argument Ast_c.wrap2 list -> string * string list
val stringrep : Ast_c.metavar_binding_kind -> string
diff --git a/python/yes_pycocci.ml b/python/yes_pycocci.ml
index b9be757..11c8cae 100644
--- a/python/yes_pycocci.ml
+++ b/python/yes_pycocci.ml
@@ -257,6 +257,16 @@ let construct_variables mv e =
(pytuple_fromsingle (str))
in
+ let instantiate_ExpressionList(x) =
+ let (str,elements) = Pycocci_aux.exprlistrep x in
+ let str = pystring_fromstring str in
+ let elements =
+ pytuple_fromarray
+ (Array.of_list (List.map pystring_fromstring elements)) in
+ pycocci_instantiate_class "coccilib.elems.ExpressionList"
+ (pytuple_fromarray (Array.of_list [str;elements]))
+ in
+
let instantiate_Identifier(x) =
let str = pystring_fromstring x in
pycocci_instantiate_class "coccilib.elems.Identifier"
@@ -270,6 +270,10 @@ let construct_variables mv e =
let expr_repr = instantiate_Expression(expr) in
let _ = build_variable py expr_repr in
()
+ | Some (_, Ast_c.MetaExprListVal (exprlist)) ->
+ let exprlist_repr = instantiate_ExpressionList(exprlist) in
+ let _ = build_variable py exprlist_repr in
+ ()
| Some (_, Ast_c.MetaIdVal (id,_)) ->
let id_repr = instantiate_Identifier(id) in
let _ = build_variable py id_repr in
_______________________________________________
Cocci mailing list
[email protected]
http://lists.diku.dk/mailman/listinfo/cocci
(Web access from inside DIKUs LAN only)