title: Pushing/Poping Lexical States in JavaCC
body: <p>I'm trying to refactor a JavaCC DSL parser that is written using mostly only one lexical state.</p>
  
  <p>My goal is to introduce a new keyword that is context sensitive to not invalidate older configurations using the older DSL.</p>
  
  <p>The idea was to change lexical state and introduce the new keyword so it is only valid during a very specific context. Making backward compatible with earlier releases.</p>
  
  <p>Problem: Comments already change lexical state, to change back to DEFAULT after end of comment. Changing back to DEFAULT is "hardcoded", but now I would need Comments to instead change back to last active lexical state.</p>
  
  <p>From what I understand keeping states in a stack, pushing and poping them, would help me achieve this (Think this is called DPDA). Is this possible in JavaCC?</p>
  
  <p>[Edit, after searching some more, this is who far I got:]</p>
  
  <pre><code>TOKEN_MGR_DECLS : {
      Stack lexicalStateStack = new Stack();
  }
  
  SKIP : {
     " "
  |  "\t"
  |  "\n"
  |  "\r"
  }
  
  MORE :
  {
    "/*" { lexicalStateStack.push(curLexState); } : IN_COMMENT
  }
  
  SPECIAL_TOKEN :
  {
    &lt;IN_COMMENT : "*/" &gt; { SwitchTo((int)lexicalStateStack.pop()); }
  }
  </code></pre>
  
  <p>Seems to be doing what I want, but is it correct? (Thinking of lookaheads here)</p>