1. Python CFFI
  2. Untitled project
  3. cffi

Commits

Armin Rigo  committed 7caa8ac

Remove comments.

  • Participants
  • Parent commits 9d652c0
  • Branches cpy-extension

Comments (0)

Files changed (2)

File cffi/cparser.py

View file
  • Ignore whitespace
 from . import api, model
 import pycparser, weakref, re
 
+_r_comment = re.compile(r"/\*.*?\*/|//.*?$", re.DOTALL | re.MULTILINE)
 _r_partial_enum = re.compile(r"\.\.\.\s*\}")
 _r_enum_dotdotdot = re.compile(r"__dotdotdot\d+__$")
 _parser_cache = None
     return _parser_cache
 
 def _preprocess(csource):
+    # Remove comments
+    csource = _r_comment.sub(' ', csource)
+    # Replace "...}" with "__dotdotdotNUM__}".  This construction should
+    # occur only at the end of enums; at the end of structs we have "...;}"
+    # and at the end of vararg functions "...);"
     matches = list(_r_partial_enum.finditer(csource))
     for number, match in enumerate(reversed(matches)):
         p = match.start()
         assert csource[p:p+3] == '...'
         csource = '%s __dotdotdot%d__ %s' % (csource[:p], number,
                                              csource[p+3:])
+    # Replace all remaining "..." with the same name, "__dotdotdot__",
+    # which is declared with a typedef for the purpose of C parsing.
     return csource.replace('...', ' __dotdotdot__ ')
 
 class Parser(object):

File testing/test_parsing.py

View file
  • Ignore whitespace
     type = ffi._parser.parse_type("fn_t")
     BType = ffi._get_cached_btype(type)
     assert BType == '<func (<pointer to <int>>), <int>, False>'
+
+def test_remove_comments():
+    ffi = FFI(backend=FakeBackend())
+    ffi.cdef("""
+        double /*comment here*/ sin   // blah blah
+        /* multi-
+           line-
+           //comment */  (
+        // foo
+        double // bar      /* <- ignored, because it's in a comment itself
+        x, double/*several*//*comment*/y) /*on the same line*/
+        ;
+    """)
+    m = ffi.rawload("m")
+    func = m.sin
+    assert func.name == 'sin'
+    assert func.BType == '<func (<double>, <double>), <double>, False>'