title: Code substitution (a la #define). Lexer? or Parser?
body: <p><strong>tl;dr:</strong> How do you emulate the equivalent of C's <code>#define</code> with jison without doing a pre-processing step?</p>
  
  <p>I am working on a relatively simple grammar with a feature to assign an identifier to a chunk of code that can then be re-used later for brevity.  Example:</p>
  
  <pre><code># Valid grammar with various elements of different types
  foo x.3 y.4 z.5
  
  # Assign an id to a chunk of code. Everything after -&gt; is assigned to id
  fill_1-&gt; bar a.1 b.2 c.3
  
  # Use chunk of code later
  # Should be equivalent to parsing: "baz d.4 bar a.1 b.2 c.3 e.5"
  baz d.4 ["fill_1"] e.5
  </code></pre>
  
  <p>So far, I've got my parser setup to correctly identify assignment lines of code and store the part to the right of the '->' in a dictionary available to other parser actions.  Code related to the define action provided below:</p>
  
  <pre><code>// Lexer
  HSPC                      [ \t]
  ID                        [a-zA-Z_][a-zA-Z0-9_]*
  %%
  {ID}{HSPC}*"-&gt;"       {
                          this.begin("FINISHLINE"); 
                          yytext = yytext.replace("-&gt;", "").trim();
                          return "DEFINE";
                        }
  
  ('"'{ID}'"')          { 
                          yytext = yytext.slice(1,-1);
                          return "QUOTED_ID"; 
                        }
  
  &lt;FINISHLINE&gt;.*        {
                          this.begin("INITIAL");
                          yytext = yytext.trim();
                          return "REST_OF_LINE";
                        }
  
  %%
  
  // Parser
  statement
    : "[" QUOTED_ID "]"
      { $$ = (defines[$2] ? defines[$2] : ""); }
    | DEFINE  REST_OF_LINE
      { 
        defines[$1] = $2;
      }
    ;
  
  %% 
  var defines = {};
  </code></pre>
  
  <p>How can I get jison to actually tokenize and parse that saved snippet of code?  Do I need to take an AST approach? Is there a way to inject the code into the parser?  Should this happen in the lexing stage or the parsing stage?  Would love to hear multiple strategies one could take with short example snippets.  </p>
  
  <p>Thanks!</p>