From 072372c39c24dedce086875632e83ded45a25373 Mon Sep 17 00:00:00 2001
From: Andreas Kloeckner <inform@tiker.net>
Date: Thu, 7 May 2015 13:23:08 -0500
Subject: [PATCH] Optionally allow running a C preprocessor on Fortran source

---
 bin/loopy                          |  5 +++--
 examples/fortran/run-floopy.sh     |  2 +-
 loopy/frontend/fortran/__init__.py | 32 +++++++++++++++++++++++++++++-
 requirements.txt                   |  3 +++
 4 files changed, 38 insertions(+), 4 deletions(-)

diff --git a/bin/loopy b/bin/loopy
index ed8f0ff16..2a657174d 100644
--- a/bin/loopy
+++ b/bin/loopy
@@ -116,7 +116,7 @@ def main():
 
         kernels = [kernel]
 
-    elif args.lang in ["fortran", "floopy"]:
+    elif args.lang in ["fortran", "floopy", "fpp"]:
         pre_transform_code = None
         if args.transform:
             with open(args.transform, "r") as xform_fd:
@@ -132,7 +132,8 @@ def main():
                         + pre_transform_code)
 
         from loopy.frontend.fortran import f2loopy
-        kernels = f2loopy(infile_content, pre_transform_code=pre_transform_code)
+        kernels = f2loopy(infile_content, pre_transform_code=pre_transform_code,
+                use_c_preprocessor=(args.lang == "fpp"))
 
         if args.name is not None:
             kernels = [kernel for kernel in kernels
diff --git a/examples/fortran/run-floopy.sh b/examples/fortran/run-floopy.sh
index 6a718bb69..fcea2c8b5 100755
--- a/examples/fortran/run-floopy.sh
+++ b/examples/fortran/run-floopy.sh
@@ -3,4 +3,4 @@
 NAME="$1"
 shift
 
-python $(which loopy) --lang=floopy "$NAME" - "$@"
+python $(which loopy) --lang=fpp "$NAME" - "$@"
diff --git a/loopy/frontend/fortran/__init__.py b/loopy/frontend/fortran/__init__.py
index f169eeef5..aec2d4314 100644
--- a/loopy/frontend/fortran/__init__.py
+++ b/loopy/frontend/fortran/__init__.py
@@ -22,9 +22,39 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 """
 
+from loopy.diagnostic import LoopyError
+
 
 def f2loopy(source, free_form=True, strict=True,
-        pre_transform_code=None):
+        pre_transform_code=None, use_c_preprocessor=False,
+        file_name="<floopy code>"):
+    if use_c_preprocessor:
+        try:
+            import ply.lex as lex
+            import ply.cpp as cpp
+        except ImportError:
+            raise LoopyError("Using the C preprocessor requires PLY to be installed")
+
+        lexer = lex.lex(cpp)
+
+        from ply.cpp import Preprocessor
+        p = Preprocessor(lexer)
+        p.parse(source, file_name)
+
+        tokens = []
+        while True:
+            tok = p.token()
+
+            if not tok:
+                break
+
+            if tok.type == "CPP_COMMENT":
+                continue
+
+            tokens.append(tok.value)
+
+        source = "".join(tokens)
+
     from fparser import api
     tree = api.parse(source, isfree=free_form, isstrict=strict,
             analyze=False, ignore_comments=False)
diff --git a/requirements.txt b/requirements.txt
index 708b72c8d..b833d6dd0 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -6,3 +6,6 @@ git+git://github.com/pyopencl/pyopencl
 git+git://github.com/inducer/pymbolic
 
 hg+https://bitbucket.org/inducer/f2py
+
+# Optional, needed for using the C preprocessor on Fortran
+ply>=3.6
-- 
GitLab