changeset 42677: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
files contrib/byteify-strings.py
diffstat 1 files changed, 30 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/contrib/byteify-strings.py	Fri Aug 02 09:55:32 2019 +0200
+++ b/contrib/byteify-strings.py	Fri Aug 02 10:10:23 2019 +0200
@@ -92,6 +92,36 @@
             except IndexError:
                 break
 
+    def _isitemaccess(j):
+        """Assert the next tokens form an item access on `tokens[j]` and that
+        `tokens[j]` is a name.
+        """
+        try:
+            return (
+                tokens[j].type == token.NAME
+                and _isop(j + 1, '[')
+                and tokens[j + 2].type == token.STRING
+                and _isop(j + 3, ']')
+            )
+        except IndexError:
+            return False
+
+    def _ismethodcall(j, *methodnames):
+        """Assert the next tokens form a call to `methodname` with a string
+        as first argument on `tokens[j]` and that `tokens[j]` is a name.
+        """
+        try:
+            return (
+                tokens[j].type == token.NAME
+                and _isop(j + 1, '.')
+                and tokens[j + 2].type == token.NAME
+                and tokens[j + 2].string in methodnames
+                and _isop(j + 3, '(')
+                and tokens[j + 4].type == token.STRING
+            )
+        except IndexError:
+            return False
+
     coldelta = 0  # column increment for new opening parens
     coloffset = -1  # column offset for the current line (-1: TBD)
     parens = [(0, 0, 0)]  # stack of (line, end-column, column-offset)