Applied, thanks. julia
On Sun, 2 Jun 2019, Wen Yang wrote: > Extending the comment support to python > > Signed-off-by: Wen Yang <[email protected]> > --- > demos/comments.cocci | 28 +++++++++++++++++++++++++++- > python/coccilib/elems.py | 6 ++++++ > python/yes_pycocci.ml | 22 +++++++++++++++++++++- > 3 files changed, 54 insertions(+), 2 deletions(-) > > diff --git a/demos/comments.cocci b/demos/comments.cocci > index cdca955b..abfcc6b8 100644 > --- a/demos/comments.cocci > +++ b/demos/comments.cocci > @@ -33,4 +33,30 @@ Printf.printf "c3a: %s\n\n" (String.concat " " c3a); > Printf.printf "c4b: %s\n" (String.concat " " c4b); > Printf.printf "c4m: %s\n" (String.concat " " c4m); > Printf.printf "c4a: %s\n\n" (String.concat " " c4a); > -Printf.printf "-------------------\n" > \ No newline at end of file > +Printf.printf "-------------------\n" > + > +@script:python@ > +c1 << r.c1; > +c2 << r.c2; > +c3 << r.c3; > +c4 << r.c4; > +@@ > + > +print("python test begin ...") > +print("c1b: ", c1[0].before) > +print("c1m: ", c1[0].middle) > +print("c1a: ", c1[0].after) > + > +print("c2b: ", c2[0].before) > +print("c2m: ", c2[0].middle) > +print("c2a: ", c2[0].after) > + > +print("c3b: ", c3[0].before) > +print("c3m: ", c3[0].middle) > +print("c3a: ", c3[0].after) > + > +print("c4b: ", c4[0].before) > +print("c4m: ", c4[0].middle) > +print("c4a: ", c4[0].after) > + > +print("python test end \n") > diff --git a/python/coccilib/elems.py b/python/coccilib/elems.py > index ddb508b0..4d11a79a 100644 > --- a/python/coccilib/elems.py > +++ b/python/coccilib/elems.py > @@ -11,6 +11,12 @@ class Location: > self.line_end = line_end > self.column_end = column_end > > +class Comment: > + def __init__(self, comment_before, comment_middle, comment_after): > + self.before = comment_before > + self.middle = comment_middle > + self.after = comment_after > + > class ElemBase: > def __init__(self): > pass > diff --git a/python/yes_pycocci.ml b/python/yes_pycocci.ml > index 0e0e654c..038ad2a9 100644 > --- a/python/yes_pycocci.ml > +++ b/python/yes_pycocci.ml > @@ -366,7 +366,27 @@ let construct_variables mv e = > let pylocs = Py.Tuple.of_list locs in > let _ = build_variable py pylocs in > () > - | Some (_, Ast_c.MetaComValList l) -> failwith "comments not yet > supported" > + | Some (_, Ast_c.MetaComValList l) -> > + let coms = > + List.map > + (function (bef, mid, aft) -> > + let com_strings l = > + List.rev > + (List.fold_left > + (fun prev cur -> > + match cur with > + (Token_c.TComment,_) -> (Token_c.str_of_token cur) :: > prev > + | (Token_c.TCommentCpp _,_) -> (Token_c.str_of_token > cur) :: prev > + | _ -> prev) > + [] l) in > + pycocci_instantiate_class "coccilib.elems.Comment" > + (Py.Tuple.of_list_map Py.String.of_string > + [String.concat "\n" (com_strings bef); > + String.concat "\n" (com_strings mid); > + String.concat "\n" (com_strings aft)])) l in > + let pycoms = Py.Tuple.of_list coms in > + let _ = build_variable py pycoms in > + () > | Some (_,binding) -> > let _ = > build_variable py > -- > 2.21.0 > > _______________________________________________ Cocci mailing list [email protected] https://systeme.lip6.fr/mailman/listinfo/cocci
