title: How to get line number in ANTLR3 tree-parser @init action
body: <p>In ANTLR, version 3, how can the line number be obtained in the @init action of a high-level tree-parser rule?</p>
  
  <p>For example, in the @init action below, I'd like to push the line number along with the sentence text.</p>
  
  <pre><code>sentence
      @init { myNodeVisitor.pushScriptContext( new MyScriptContext( $sentence.text )); }
      : assignCommand 
      | actionCommand;
      finally {
          m_nodeVisitor.popScriptContext();
      }
  </code></pre>
  
  <p>I need to push the context <em>before</em> the execution of the actions associated with symbols in the rules.</p>
  
  <p>Some things that <em>don't</em> work:</p>
  
  <ul>
  <li>Using <code>$sentence.line</code> -- it's not defined, even though <code>$sentence.text</code> is.</li>
  <li>Moving the paraphrase push into the rule actions. Placed before the rule, no token in the rule is available. Placed after the rule, the action happens after actions associated with the rule symbols. </li>
  <li>Using this expression in the @init action, which compiles but returns the value 0: <code>getTreeNodeStream().getTreeAdaptor().getToken( $sentence.start ).getLine()</code>. <strong>EDIT:</strong> <em>Actually, this does work, if $sentence.start is either a real token or an imaginary with a reference -- see Bart Kiers answer below.</em></li>
  </ul>
  
  <p>It seems like if I can easily get, in the @init rule, the matched text and the first matched token, there should be an easy way to get the line number as well.</p>