[pypy-commit] cffi cpy-extension: Remove comments.
arigo
noreply at buildbot.pypy.org
Thu Jun 14 13:16:31 CEST 2012
Author: Armin Rigo <arigo at tunes.org>
Branch: cpy-extension
Changeset: r330:7caa8ac321f2
Date: 2012-06-14 13:16 +0200
http://bitbucket.org/cffi/cffi/changeset/7caa8ac321f2/
Log: Remove comments.
diff --git a/cffi/cparser.py b/cffi/cparser.py
--- a/cffi/cparser.py
+++ b/cffi/cparser.py
@@ -2,6 +2,7 @@
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
@@ -13,12 +14,19 @@
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):
diff --git a/testing/test_parsing.py b/testing/test_parsing.py
--- a/testing/test_parsing.py
+++ b/testing/test_parsing.py
@@ -122,3 +122,20 @@
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>'
More information about the pypy-commit
mailing list