title: Macro expansion in Antlr4
body: <p>We are developing a modeling tool in which the user can insert small pieces of code in various places. The user can add macros to this code, which we need to expand before parsing the final code. For example, when I parse the following code...</p>
  
  <pre><code>FOR i := 6 TO 1 DO 
      j := 6;
      IF SM_MCP.State = oMcpApplication.UNDEFINED
      THEN
          Macro1(MsgNotInZeroPosition);
      END_IF;
  END_FOR;
  </code></pre>
  
  <p>... I just want to handle the macro in a visitor function. The result would be:</p>
  
  <pre><code>FOR i := 6 TO 1 DO 
      j := 6;
      IF SM_MCP.State = oMcpApplication.UNDEFINED
      THEN
          &lt;ClassName&gt;.MessageQueue := MsgNotInZeroPosition;
      END_IF;
  END_FOR;
  </code></pre>
  
  <p>(This is just a made up example.) I have tried to create a simple grammar that parses the macros and simply copies everything around it. But, for some reason I cannot get it working properly. </p>
  
  <p>I have tried several grammars, they all looked something like this:</p>
  
  <pre><code>grammar Macros;
  
  source : (code macro )* code ;
  
  macro : macro1 | macro2 | macro3 | macro4 ;
  macro1 : Macro1  '(' parameters ')' ;
  macro2 : Macro2  '(' parameters ')' ;
  macro3 : Macro3  '(' parameters ')' ;
  macro4 : Macro4  '(' parameters ')' ;
  
  code : OTHER*  ;
  parameters : OTHER*  ;
  
  // Lexer rules
  
  Macro1: 'Macro1';
  Macro2: 'Macro2';
  Macro3: 'Macro3';
  Macro4: 'Macro4';
  
  OTHER : . ;
  </code></pre>
  
  <p>I have tried non-greedy matching too, but most of the times everything ended up in one token or the parser crashed. Can anyone point me in the proper direction?</p>