home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 July / maximum-cd-2011-07.iso / DiscContents / LibO_3.3.2_Win_x86_install_multi.exe / libreoffice1.cab / fix_paren.py < prev    next >
Encoding:
Python Source  |  2011-03-15  |  1.2 KB  |  43 lines

  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())
  43.