[PATCH 5 of 6] byteify-strings: add helpers to check for item access or method call

Raphaël Gomès raphael.gomes at octobus.net
Fri Aug 2 04:22:16 EDT 2019


# HG changeset patch
# User Raphaël Gomès <rgomes at octobus.net>
# Date 1564733423 -7200
#      Fri Aug 02 10:10:23 2019 +0200
# Node ID d28c8bac8d4bc1eddc4a899eb081563e4823f6a6
# Parent  edff9054a1a327d3d444d4acfde0879a92742549
# EXP-Topic byteify-strings
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.

diff -r edff9054a1a3 -r d28c8bac8d4b contrib/byteify-strings.py
--- a/contrib/byteify-strings.py	Fri Aug 02 10:03:50 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)


More information about the Mercurial-devel mailing list