Benjamin Peterson | 8 Apr 2009 01:24
Favicon
Gravatar

Re: 2to3 pattern matching help needed.

2009/4/7 Lennart Regebro <regebro <at> gmail.com>:
> Help! I need a pattern to match the following code:
>
>  <at> implementor(IFoo)
> class C(object):
>    implements(IFoo)
>    classProvides(IFooFactory
>
> find_pattern claims that this code should match:
>
> "decorated< decorator< ' <at> ' 'implementer' '(' 'IFoo' ')' '\n' >
> classdef< 'class' 'C' '(' 'object' ')' ':' suite< '\n' '    '
> simple_stmt< power< 'implements' trailer< '(' 'IFoo' ')' > > '\n' >
> simple_stmt< power< 'classProvides' trailer< '(' 'IFooFactory' ')' > >
> '\n' > '' > > >"
>
> It does not match.

2to3 has a hard time working with more than a single statements and
lines of code especially classes. You can't use the pattern matcher
for everything, unfortunately. This pattern matches a class with the
decorator you want:

decorated< decorator< ' <at> ' 'implementor' '(' 'IFoo' ')' any >
class=classdef< any* > >

Once you that matches, I suggest you traverse the classdef node
programmatically to find simple_stmts containing the lines you're
interested in. This is similar to what lib2to3/fixes/fix_metaclass.py
does.

--

-- 
Regards,
Benjamin
_______________________________________________
Python-porting mailing list
Python-porting <at> python.org
http://mail.python.org/mailman/listinfo/python-porting

Gmane