comparison contrib/byteify-strings.py @ 42704:c9fd8163131f

byteify-strings: add helpers to check for item access or method call These helpers will be used in a future patch, split for ease of review.
author Rapha?l Gom?s <rgomes@octobus.net>
date Fri, 02 Aug 2019 10:10:23 +0200
parents b9a200477edf
children f95b59ffc307
comparison
equal deleted inserted replaced
42703:b9a200477edf 42704:c9fd8163131f
89 sysstrtokens.add(currtoken) 89 sysstrtokens.add(currtoken)
90 try: 90 try:
91 currtoken = tokens[k] 91 currtoken = tokens[k]
92 except IndexError: 92 except IndexError:
93 break 93 break
94
95 def _isitemaccess(j):
96 """Assert the next tokens form an item access on `tokens[j]` and that
97 `tokens[j]` is a name.
98 """
99 try:
100 return (
101 tokens[j].type == token.NAME
102 and _isop(j + 1, '[')
103 and tokens[j + 2].type == token.STRING
104 and _isop(j + 3, ']')
105 )
106 except IndexError:
107 return False
108
109 def _ismethodcall(j, *methodnames):
110 """Assert the next tokens form a call to `methodname` with a string
111 as first argument on `tokens[j]` and that `tokens[j]` is a name.
112 """
113 try:
114 return (
115 tokens[j].type == token.NAME
116 and _isop(j + 1, '.')
117 and tokens[j + 2].type == token.NAME
118 and tokens[j + 2].string in methodnames
119 and _isop(j + 3, '(')
120 and tokens[j + 4].type == token.STRING
121 )
122 except IndexError:
123 return False
94 124
95 coldelta = 0 # column increment for new opening parens 125 coldelta = 0 # column increment for new opening parens
96 coloffset = -1 # column offset for the current line (-1: TBD) 126 coloffset = -1 # column offset for the current line (-1: TBD)
97 parens = [(0, 0, 0)] # stack of (line, end-column, column-offset) 127 parens = [(0, 0, 0)] # stack of (line, end-column, column-offset)
98 ignorenextline = False # don't transform the next line 128 ignorenextline = False # don't transform the next line