[hawkmoth] [PATCH] parser: simplify macro arguments extraction

  • From: Bruno Santos <brunomanuelsantos@xxxxxxxxxxxxxxxxxx>
  • To: hawkmoth mailing list <hawkmoth@xxxxxxxxxxxxx>, Jani Nikula <jani@xxxxxxxxxx>
  • Date: Tue, 24 Sep 2019 23:56:21 +0200

Previous solution required an extra dependency on itertools and it was
arguably uglier than what can be achieved in idiomatic Python. This
patch doesn't change behaviour in any way and it's purely stylistic.
---
Just a minor clean up I had laying around. Hope it's tasteful enough ;)

 hawkmoth/parser.py | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/hawkmoth/parser.py b/hawkmoth/parser.py
index 9c1ec58..c4c137c 100644
--- a/hawkmoth/parser.py
+++ b/hawkmoth/parser.py
@@ -34,7 +34,6 @@
 """
 
 import enum
-import itertools
 import sys
 
 from clang.cindex import CursorKind, TypeKind
@@ -129,16 +128,16 @@ def _get_macro_args(cursor):
     if cursor.kind != CursorKind.MACRO_DEFINITION:
         return None
 
+    tokens = cursor.get_tokens()
+
     # Use the first two tokens to make sure this starts with 'IDENTIFIER('
-    x = [token for token in itertools.islice(cursor.get_tokens(), 2)]
-    if (len(x) != 2 or x[0].spelling != cursor.spelling or
-        x[1].spelling != '(' or x[0].extent.end != x[1].extent.start):
+    if next(tokens).spelling != cursor.spelling or next(tokens).spelling != 
'(':
         return None
 
     # Naïve parsing of macro arguments
     # FIXME: This doesn't handle GCC named vararg extension FOO(vararg...)
     args = []
-    for token in itertools.islice(cursor.get_tokens(), 2, None):
+    for token in tokens:
         if token.spelling == ')':
             return args
         elif token.spelling == ',':
-- 
2.23.0


Other related posts: