Hi all,

I hope this is the right place to send this to, but here we go. For our project, we wanted support for some modern C++-features in the "bind" egg:

1. nested namespace definitions: instead of namespace A { namespace B {
   ... } }, C++17 allows writing namespace A::B { ... }
2. typed and scoped enums: C++11 allows specifying the underlying type
   of an enum: enum C: int16_t { ... }. It also added scoped enums,
   that put all enumerators into a new scope: enum class D { E, F },
   which then need to be referenced as D::E and D::F.

Since bind is a thin layer over some basic C and C++ constructs and mostly ignores namespaces, I've extended it to accept nested namespace definitions and scoped and typed enums. The underlying type for the latter are simply ignored by bind as well.

These changes would really help reusing declarations between C++ code called by CHICKEN and bindings automatically generated with the bind module and chicken-bind CLI tools.

Disclaimer: Even though I have almost 20 years of professional coding experience, I'm still pretty new to both Scheme and Lisp in general. Feedback of any kind is welcome!


Cheers
- rnlf
Index: bind-translator.scm
===================================================================
--- bind-translator.scm	(revision 41054)
+++ bind-translator.scm	(working copy)
@@ -267,17 +267,25 @@
 			 (begin
 			   (set! dc #t)
 			   (loop more) ) ) ]
-		    [('enum ('scope more))
-		     (parse-enum-def #f (subst-macros more)) ]
-		    [('enum ('id name) ('scope more))
-		     (parse-enum-def name (subst-macros more)) ]
+		    [('enum ('scope more))
+		     (parse-enum-def #f (subst-macros more)) ]
+		    [('enum ('op ":") (or ('id type)) ('scope more))
+		     (parse-enum-def #f (subst-macros more)) ]
+		    [('enum ('id name) ('scope more))
+		     (parse-enum-def name (subst-macros more)) ]
+		    [('enum ('id name) ('op ":") . more)
+		     (parse-typed-enum-def name (subst-macros more)) ]
+		    [('enum (or 'class 'struct) ('id name) ('scope more))
+		     (parse-enum-def name (subst-macros more)) ]
+		    [('enum (or 'class 'struct) ('id name) ('op ":") . more)
+		     (parse-typed-enum-def name (subst-macros more)) ]
 		    [('class . more)
 		     (parse-class-def more ab) ]
 		    [((or 'union 'struct) ('id name) ('scope . more))
 		     (parse-struct-def (car c) name ab (subst-macros more)) ]
 		    [((or 'union 'struct) ('id name)) #f]
-		    [('namespace ('id name) (scope . more))
-		     (for-each parse more) ]
+		    [('namespace . more)
+		     (parse-namespace more) ]
 		    [('typedef . more)
 		     (parse-typedef more) ]
 		    [(and more (('id name) . _))
@@ -285,6 +293,14 @@
 		    [more
 		     (parse-prototype more cb sp dc ds)] ) ) ) ] ) ) ] ) )
 
+(define (parse-namespace c)
+  (match c
+         [(('id name) ('op "::") . more)
+          (parse-namespace more)]
+         [(('id name) (scope . more))
+          (for-each parse more)]
+         [more (parsing-error "unexpected tokens" more)] ) )
+
 (define parse-again parse)
 
 (define parse-type-rec
@@ -556,6 +572,13 @@
 	       [('comma . more) (loop more i items)]
 	       [_ (parsing-error "syntax error in enum form" more)] ) ) ) ] ) ) )
 
+(define (parse-typed-enum-def ename ts)
+  (let-values (((type c) (parse-type ts #t)))
+    (match c
+           [(('scope more))
+            (parse-enum-def ename (subst-macros more))]
+           [_ (parsing-error "syntax error in enum form" c) ] ) ) )
+
 (define (parse-enum-item ts i items)
   (match ts
     [(('id name) ('op "=") ('id name2) . more)

Reply via email to