Because this is awkward in C, the lexer maintains a pointer to the token start
line in the token, and it is changed on encountering '\n' (by default, you can
change the character).
The pointer is a direct pointer to the input stream, so you need to print a
limited amount of the text there and possibly recalculate how far in to the
line you want to start printing.
theToken = (pANTLR3_COMMON_TOKEN)(recognizer->state->exception->token);
theToken->lineStart; // Gives a pointer the start of the text line where the
token resides.
theToken->charPosition; // The offset in the current line (via
getCharPositionInLine(theToken) )
So you want to calculate a window and get something like this as output.
Fred.jimcode(34,134) : error : Expected to see one of 'junk', 'foo', 'bar'
...FR DLKN LKN LKN lk LKN LKN LKNKL lkN LKN Lkn LKNLN LKKNLKN LK...
^
If your lines are always short though, then you could just scan to the end of
the current line, work out how many characters there are and:
printf("%*s\n", lineLen, theToken->lineStart);
Jim
From: [email protected] [mailto:[email protected]] On
Behalf Of Goins, John C (IS)
Sent: Monday, March 08, 2010 3:26 PM
To: [email protected]
Subject: [antlr-dev] C Runtime Error Handling
Hi -
I am trying to print the filename, line number, and the actual line that has an
error. Getting the filename and line number is relatively easy, however I have
not found a good way to get the actual line that caused the error. The
original implementation of displayRecognitionError(..) does not print the line.
TIA
Here is what I have:
.g file
----------
@parser::apifuncs
{
RECOGNIZER->displayRecognitionError = AntlrErrorHandler;
}
.c file
----------
void AntlrErrorHandler (pANTLR3_BASE_RECOGNIZER recognizer, pANTLR3_UINT8 *
tokenNames)
{
ANTLR3_FPRINTF(stderr, "File: %s\r\n",
recognizer->state->exception->streamName->chars);
ANTLR3_FPRINTF(stderr, "Line number: %s\r\n",
recognizer->state->exception->line);
// how do I get the actual line?
// ...
}
_______________________________________________
antlr-dev mailing list
[email protected]
http://www.antlr.org/mailman/listinfo/antlr-dev