[hawkmoth] Re: [PATCH 04/13] hawkmoth: trivial code simplifications

  • From: Jani Nikula <jani@xxxxxxxxxx>
  • To: Bruno Santos <brunomanuelsantos@xxxxxxxxxxxxxxxxxx>, hawkmoth@xxxxxxxxxxxxx
  • Date: Tue, 08 Jan 2019 22:04:19 +0200

On Tue, 08 Jan 2019, Bruno Santos <brunomanuelsantos@xxxxxxxxxxxxxxxxxx> wrote:

The parse function is currently very long and messy, which is getting in
the way of fixing all those FIXMEs as well. Anything that removes lines
of code from this function is therefore good and this is a tiny tiny
start.

This should probably be before patch 3 in the series.

---
 hawkmoth/hawkmoth.py | 35 +++++++++++++++++------------------
 1 file changed, 17 insertions(+), 18 deletions(-)

diff --git a/hawkmoth/hawkmoth.py b/hawkmoth/hawkmoth.py
index 92b8740..a21e3d0 100755
--- a/hawkmoth/hawkmoth.py
+++ b/hawkmoth/hawkmoth.py
@@ -66,7 +66,16 @@ def indent(string, prefix):
 def wrap_blank_lines(string):
     return '\n' + string + '\n'
 
-def pass1(tu, top_level_comments, comments):
+def comment_extract(tu):
+
+    # FIXME: How to handle top level comments above a cursor that it does 
*not*
+    # describe? Parsing @file or @doc at this stage would not be a clean 
design.
+    # One idea is to use '/***' to denote them, but that might throw off 
editor
+    # highlighting. The workaround is to follow the top level comment with an
+    # empty '/**/' comment that gets attached to the cursor.
+
+    top_level_comments = []
+    comments = {}
     cursor = None
     current_comment = None
     for token in tu.get_tokens(extent=tu.cursor.extent):
@@ -100,6 +109,8 @@ def pass1(tu, top_level_comments, comments):
     if current_comment:
         top_level_comments.append(current_comment)
 
+    return top_level_comments, comments
+
 # Return None for simple macros, a potentially empty list of arguments for
 # function-like macros
 def get_macro_args(cursor):
@@ -142,11 +153,7 @@ def parse(filename, **options):
                      TranslationUnit.PARSE_DETAILED_PROCESSING_RECORD |
                      TranslationUnit.PARSE_SKIP_FUNCTION_BODIES)
 
-    top_level_comments = []
-    comments = {}
-
-    # parse the file, get comments
-    pass1(tu, top_level_comments, comments)
+    top_level_comments, comments = comment_extract(tu)
 
     # FIXME: strip_comment, doccompat.convert, and the C Domain directive all
     # change the number of lines in output. This impacts the error reporting 
via
@@ -174,13 +181,6 @@ def parse(filename, **options):
 
         doc_comment = strip_comment(comment.spelling)
 
-        # FIXME: How to handle top level comments above a cursor that it does
-        # *not* describe? Parsing @file or @doc at this stage would not be a
-        # clean design. One idea is to use '/***' to denote them, but that 
might
-        # throw off editor highlighting. The workaround is to follow the top
-        # level comment with an empty '/**/' comment that gets attached to 
the
-        # cursor.
-
         if cursor.kind == CursorKind.MACRO_DEFINITION:
             args = get_macro_args(cursor)
             if args is None:
@@ -196,11 +196,10 @@ def parse(filename, **options):
                 name=cursor.spelling)
         elif cursor.kind == CursorKind.TYPEDEF_DECL:
             cdom = '.. c:type:: {name}\n'.format(name=cursor.spelling)
-        elif cursor.kind == CursorKind.STRUCT_DECL:
-            cdom = '.. c:type:: {name}\n'.format(name=cursor.type.spelling)
-        elif cursor.kind == CursorKind.UNION_DECL:
-            cdom = '.. c:type:: {name}\n'.format(name=cursor.type.spelling)
-        elif cursor.kind == CursorKind.ENUM_DECL:
+
+        elif cursor.kind == CursorKind.STRUCT_DECL or \
+             cursor.kind == CursorKind.UNION_DECL or \
+             cursor.kind == CursorKind.ENUM_DECL:

I know there are some backslash line continuations in there already, but
I think I'd prefer consolidating on using (). Like so:

    elif (cursor.kind == CursorKind.STRUCT_DECL or
          cursor.kind == CursorKind.UNION_DECL or
          cursor.kind == CursorKind.ENUM_DECL):

Otherwise LGTM.

BR,
Jani.


             cdom = '.. c:type:: {name}\n'.format(name=cursor.type.spelling)
         elif cursor.kind == CursorKind.FUNCTION_DECL:
             # FIXME: check args against doc_comment
-- 
2.20.1

Other related posts: