One of the things that can be really annoying about using string mixin's, especially when there is a lot of code, is that the compile complains about syntax errors on a line within the mixin that doesn't exist in the code.

While at the D Conference this issue was mention along with the file/line feature of function calls. It got me thinking about a code builder I had been using in ProtocolBuffer[1]. I just checked and found that the use of the #line language feature combined with the file/line feature allowed for providing better information about where a mixin went wrong. So I pulled out the code and packaged it for dub[2].

Documentation is lacking in examples, but is used with ProtocolBuffer[3]. It uses a put/push/pop approach to provide some freedom in organizing the code; but I'll admit doesn't help readability (the call to finalize will pop all code elements push to the stack).

This package doesn't do too much to help with building code, one of the main parts it provides is pretty printing (indenting) which isn't important for a mixin. So please feel free to send of suggestions from experience trying to use it, I won't consider the API stable at this point.

Since ProtocolBuffer support D1 and D2, so does this library.

Instead of -- test.d-mixin-4(4): Error: semicolon expected, not '}'

I get -- test.d(11): Error: semicolon expected, not '}'

---------------------
   import codebuilder.structure;

   void main() {
      mixin(genCode); // Line 4
   }

   auto genCode() {
      auto cb = CodeBuilder(1);

      cb.put("struct m {");
      cb.push("}"); // Line 11
      cb.put("int a"); // Missing semicolon

      return cb.finalize;
   }
---------------------

1. https://github.com/JesseKPhillips/ProtocolBuffer/
2. http://code.dlang.org/packages/codebuilder
3. https://github.com/JesseKPhillips/ProtocolBuffer/blob/master/conversion/dlang.d

Reply via email to