[Python-3000] 2to3 pattern syntax

Guido van Rossum guido at python.org
Wed Jan 10 22:39:22 CET 2007


On 1/10/07, Brian L. Troutwine <goofyheadedpunk at gmail.com> wrote:
> As the refactoring tool for the 2 to 3 transition was mentioned some time ago
> on this list I hope that this is the correct list to post questions about the
> tool. I apologize if this isn't, of course.
>
> My questions are entirely concerned with the pattern matching syntax. The
> problem is, it would seem, I don't understand a thing about them.
>
> Is the following incorrect for matching pass statements? If so, why?
>
>     pass_stmt< 'pass' >

The match should be just

    'pass'

or

    any<'pass'>

The reason is that any non-leaf node in the parse tree that has
exactly one child disappears and is replaced with its (only) child. So
the pass_stmt node disappears and all you're left with is a NAME node
whose contents is 'pass'.

The optimization is done to vastly reduce the parse tree size; the
grammar as specified has lots of redundant levels. See the print fixer
for a wrinkle caused by this tree optimization.

> If I wanted to append to each matched pass statement, say, the string "# This
> is a comment" how would the transformation have to be structured?

You'd have to the parent node and set the comment as the prefix on the
NEWLINE node. The pattern would be this.

    simple_stmt <'pass' NEWLINE>

> If I wanted to match a function structure what would the proper pattern be?

   funcdef <[(not 'def') any] 'def' NAME any ':' any>

> How about classes?

    classdef <'class' NAME ['(' any ')'] ':' suite

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-3000 mailing list