You need to move your calculation involving spaces
to either the end of the LEADING_WS
rule or your implementation of nextToken
. At the end of LEADING_WS
it could look like the following.
LEADING_WS
: {getColumn()==1}?
// match spaces or tabs, tracking indentation count
[ \t]+
{spaces = computeSpaces(_input.getText());}
;