~pythonregexp2.7/python/issue2636-11

« back to all changes in this revision

Viewing changes to Lib/lib2to3/fixes/fix_paren.py

  • Committer: Jeffrey C. "The TimeHorse" Jacobs
  • Date: 2008-09-21 17:53:26 UTC
  • mfrom: (39025.1.14 Regexp-2.7)
  • Revision ID: darklord@timehorse.com-20080921175326-92vaej2hc3yuecxb
Merged in changes from the core Regexp branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""Fixer that addes parentheses where they are required
 
2
 
 
3
This converts ``[x for x in 1, 2]`` to ``[x for x in (1, 2)]``."""
 
4
 
 
5
# By Taek Joo Kim and Benjamin Peterson
 
6
 
 
7
# Local imports
 
8
from .. import fixer_base
 
9
from ..fixer_util import LParen, RParen
 
10
 
 
11
# XXX This doesn't support nested for loops like [x for x in 1, 2 for x in 1, 2]
 
12
class FixParen(fixer_base.BaseFix):
 
13
    PATTERN = """
 
14
        atom< ('[' | '(')
 
15
            (listmaker< any
 
16
                comp_for<
 
17
                    'for' NAME 'in'
 
18
                    target=testlist_safe< any (',' any)+ [',']
 
19
                     >
 
20
                    [any]
 
21
                >
 
22
            >
 
23
            |
 
24
            testlist_gexp< any
 
25
                comp_for<
 
26
                    'for' NAME 'in'
 
27
                    target=testlist_safe< any (',' any)+ [',']
 
28
                     >
 
29
                    [any]
 
30
                >
 
31
            >)
 
32
        (']' | ')') >
 
33
    """
 
34
 
 
35
    def transform(self, node, results):
 
36
        target = results["target"]
 
37
 
 
38
        lparen = LParen()
 
39
        lparen.set_prefix(target.get_prefix())
 
40
        target.set_prefix("") # Make it hug the parentheses
 
41
        target.insert_child(0, lparen)
 
42
        target.append_child(RParen())