title: antlr4: get index of next (optional) token
body: <p>I have language with optional clauses (<code>CL1</code>, <code>CL2</code>).  </p>
  
  <p>grammar rule:<br>
  <code>func : FUNC ID "(" (CL1 (ID | CL11 ID))? ")" (CL2 (ID | CL21 ID))? EOS ;</code></p>
  
  <p>Because of optionality, I can't even use <code>getChild(i).getText()</code> since "i" becomes non-deterministic.  </p>
  
  <p>While running code generation, I need to pick <code>ID</code> after <code>CL1</code> or <code>CL2</code>.<br>
  If I do not have index of <code>CL1</code>, I haven't got one for <code>ID</code>, that follows it.  </p>
  
  <p><code>function fnFoo () meni attrBar ;</code><br>
  will have different token positions compared to<br>
  <code>function fnFoo (arg pBar) meni attrBar ;</code></p>
  
  <p>I used another approach of:<br>
  a. first detecting if token of this type exists <code>ctx.getTokens(LangParser.CL1)[0]</code> or <code>ctx.CL1()</code><br>
  b. picking its tokenIndex <code>ctx.CL1().getSymbol().tokenIndex</code><br>
  c. advancing <code>tokenIndex + 1</code> to next locate <code>ID</code></p>
  
  <p>But at this point, I realize, that this <code>tokenIndex</code> is not the same index used in <code>getChild(i)</code> API.<br>
  And there is no API to pick token text, using <code>tokenIndex</code> to begin with.</p>
  
  <p>How would one handle this situation?</p>
  
  <p>ps: I use nodejs runtime with visitors, using ANTLR 4.8  </p>