Hi Bill,
I might have misunderstood your question, but the following is what I
did in an old app of mine - but it works to give me the line numbers and
column numbers for the input file correctly.
It might not be the best or most efficient way, but, from an old posting
of mine on this list ...
I've done this in my utility to parse an Oracle tnsnames.ora file. The
source is on GitHub here
https://github.com/NormanDunbar/Tnsnames_checker the relevant part being:
//----------------------------------------------------------------
// EVERY RULE
//----------------------------------------------------------------
// As we enter every rule, extract the line and column positions.
// Use them to build a location string for the start of this rule.
//----------------------------------------------------------------
@Override
public void enterEveryRule(ParserRuleContext ctx)
{
Token startToken = ctx.getStart();
if (startToken != null)
{
// Lines number from 1.
this.lineNumber = startToken.getLine();
// Characters from zero. Adjust.
this.charPosition = 1 + startToken.getCharPositionInLine();
}
else
{
// Just in case Token can ever be null.
this.lineNumber = 0;
this.charPosition = 0;
}
// Build a location string for error messages etc.
this.whereAmI = "\tLine " + lineNumber + ":" + charPosition + " ";
}
Apologies for the Java - I'm not a Java developer! Formatting might also
have been "amended" by the posting process.
Lines count from 1 but characters on the line count from 0 with ANTLR
(or is it Java?)
HTH
Disclaimer: I'm not a compiler writer, nor do I play one on TV.
Cheers,
Norm.