title: Using getCharPositionInLine() with leading spaces in ANTLR4
body: <p>I am writing grammar for a script which is based on VBScript.
  In the script, the variable assignment is done in the usual manner of <code>i=10</code> and in addition a variation with: <code>Set i=10</code></p>
  
  <p>The method calls can be done in several ways along with calling methods on objects, like:
  <code>Another(10).Call(20).Chain(30)</code></p>
  
  <p>I consider 'Set' as a keyword in my grammar. However, in some pre-defined calsses, the developer is allowed to name the method as 'Set', so, there maybe calls like (let me mark this as <code>line A</code>):
  <code>Another(10).Call(20,30).Set 40,50</code></p>
  
  <p>my grammar:</p>
  
  <pre><code>definition: body EOF;
  body: NL_WS* bodyElement NL_WS*;
  bodyElement: statement (NL_WS+ statement)* ;
  
  statement: assignment | chainCall;
  assignment: (START_SET)? IDENTIFIER WS? EQUALS WS? (chainCall | VALID_NUMBER) ;
  
  chainCall: methodCall ('.' methodCall)* ;
  methodCall: IDENTIFIER WS? LPAREN? WS? argumentList? WS? RPAREN?;
  
  argumentList: VALID_NUMBER (WS? COMMA WS? VALID_NUMBER)* ;
  START_SET: 'Set' WS;
  VALID_NUMBER: [1-9] NUMBER? ;
  IDENTIFIER: LETTER LETTER_OR_DIGIT*;
  
  LETTER: [a-zA-Z_];
  NUMBER: [0-9];
  LETTER_OR_DIGIT: [a-zA-Z0-9_];
  EQUALS: '=' ;
  LPAREN: '(';
  RPAREN: ')';
  COMMA: ',';
  
  NL_WS: WS? NEWLINE WS?;
  NEWLINE: [\r\n];
  WS: [ \t]+;
  </code></pre>
  
  <p>This fails in what I have marked as <code>line A</code> (where Set is a method call inside an object):
  <code>line 10:24 mismatched input 'Set ' expecting IDENTIFIER</code></p>
  
  <p>1) I am not able to understand why. My thinking is that as in the <code>assignment</code> rule, the <code>(START_SET)?</code> is defined at the beginning, it should expect Set at the beginning and so, the method call at the end should match with <code>IDENTIFIER</code>.</p>
  
  <p>2) When I try with <code>getCharPositionInLine</code>, like:</p>
  
  <p><code>START_SET: {getCharPositionInLine() == 0}? 'Set' WS;</code></p>
  
  <p>it works fine, but, I have to deal with another problem. That is, there maybe leading whitespaces before the 'Set' assignment, like:
  <code>'    Set k=10'</code></p>
  
  <p>and in such cases, it fails saying:</p>
  
  <p><code>line 16:8 mismatched input 'k' expecting {&lt;EOF&gt;, '.', NL_WS}</code></p>
  
  <p>(in this case, I think it matches with <code>chainCall</code> and not <code>assignment</code> which is understandable as it is not the first character in line).</p>
  
  <p>So, is there an alternate method which will be like 'first character in line minus spaces'? </p>
  
  <p>I also tried,<code>START_SET: {getCharPositionInLine() == 0}? WS? 'Set' WS;</code>
  thinking that the initial <code>WS?</code> will cover the first character in line, but I get the same error.</p>
  
  <p>Any help is appreciated.</p>