How to embed C++ code in a xtext DSL?
<p>What is the proper way of handling C++ code blocks in Xtext/ANTLR?</p>

<p>We are writing an Xtext-based eclipse plugin for a DSL that supports adding C++ function-level code within well-defined scopes (mostly <code>serial { /* ... */ }</code> blocks) such as this:</p>

<pre><code>module m {
  chare c {
    entry void foo() {
      serial {
        // C++ code block
      }
    }
  }
}
</code></pre>

<p>See <a href="https://charm.cs.illinois.edu/gerrit/gitweb?p=charm.git;a=blob;f=examples/charm%2B%2B/matmul/matmul.ci;h=3ab426a38f400f27955156ca5324386cc457b04d;hb=HEAD#l24" rel="nofollow">here</a> for a more comprehensive example. This is then handed over to an external tool to handle further compilation/linking steps, so we don't generate any code from eclipse.</p>

<p>The issue here is how to handle these C++ code blocks, especially given that they may contain braces of their own. This is very similar to <a href="https://stackoverflow.com/questions/12196448/how-to-include-java-code-block-in-xtext-dsl">How to include Java Code Block in Xtext DSL?</a> but for now we are content with just ignoring that block (i.e. not having content assist or syntax highlighting is not ideal but acceptable.)</p>

<p>In our bison/flex-based tool this is done by sharing a variable between the parser and lexer that toggles a "C++ parsing mode" within certain grammar rules that makes the lexer return a CPROGRAM token for everything except the relevant delimiters (e.g. braces.) The natural translation seems to have a custom ANTLR lexer that uses semantic predicates to the same effect, e.g.</p>

<pre><code>RULE_NON_BRACES: {in_braces}? ~('{' | '}')+;
</code></pre>

<p>as the first lexing rule, but I cannot find how to access that global variable from the Xtext grammar since there doesn't seem to be a concept of "rule action" as in bison. There are other non-"serial" grammar contexts where C++ code is expected, so there needs to be some coordination between the parser and lexer.</p>