ANTLR antlrWorks error messages are not displayed to the output console
When enter the following input with an error at the third line:
SELECT entity_one, entity_two FROM myTable;
first_table, extra_table as estable, tineda as cam;
asteroid tenga, tenta as myName, new_eNoal as coble
I debugged it with antlrWorks and found that the error message corresponding to the third line gets shown on the debugger output window:
output/__Test___input.txt line 3:8 required (...)+ loop did not match anything at input ' ' output/__Test___input.txt line 3:9 missing END_COMMAND at 'tenga'
but when I run the application by 开发者_Python百科itself these error messages are not being displayed at the console.
The error messages get displayed on the console whenever the error is on the first line like:
asteroid tenga, tenta as myName, new_eNoal as coble
SELECT entity_one, entity_two FROM myTable;
first_table, extra_table as estable, tineda as cam;
console output:
inputSql.rst line 1:8 required (...)+ loop did not match anything at input ' ' inputSql.rst line 1:9 missing END_COMMAND at 'tenga'
How could I have them displayed on the console too when the errors are not located at the 1st line?
UserRequest.g
grammar UserRequest;
tokens{
COMMA = ',' ;
WS = ' ' ;
END_COMMAND = ';' ;
}
@header {
package com.linktechnology.input;
}
@lexer::header {
package com.linktechnology.input;
}
@members{
public static void main(String[] args) throws Exception {
UserRequestLexer lex = new UserRequestLexer(new ANTLRFileStream(args[0]));
CommonTokenStream tokens = new CommonTokenStream(lex);
UserRequestParser parser = new UserRequestParser(tokens);
try {
parser.request();
} catch (RecognitionException e) {
e.printStackTrace();
}
}
}
/*------------------------------------------------------------------
* PARSER RULES
*------------------------------------------------------------------*/
process : request* EOF ;
request : (sqlsentence | create) END_COMMAND ;
sqlsentence : SELECT fields tableName ;
fields : tableName (COMMA tableName)* FROM ;
create : tableName (COMMA tableName)+ ;
tableName : WS* NAME (ALIAS NAME)? ;
/*------------------------------------------------------------------
* LEXER RULES
*------------------------------------------------------------------*/
NAME : LETTER ( LETTER |DIGIT | '-' | '_' )* ;
fragment LETTER: LOWER | UPPER;
fragment LOWER: 'a'..'z';
fragment UPPER: 'A'..'Z';
fragment DIGIT: '0'..'9';
SELECT : ('SELECT ' |'select ' ) ;
FROM : (' FROM '|' from ') ;
ALIAS : ( ' AS ' |' as ' ) ;
WHITESPACE : ( '\r' | '\n' | '\t' | WS | '\u000C' )+ { $channel = HIDDEN; } ;
That is because in your main
method, you invoke parser.request()
while when debugging, you choose the process
rule as the starting point. And since request
consumes a single (sqlsentence | create) END_COMMAND
from your input, it produces no error.
Change the main
method into:
@members{
public static void main(String[] args) throws Exception {
UserRequestLexer lex = new UserRequestLexer(new ANTLRFileStream(args[0]));
CommonTokenStream tokens = new CommonTokenStream(lex);
UserRequestParser parser = new UserRequestParser(tokens);
try {
parser.process();
} catch (RecognitionException e) {
e.printStackTrace();
}
}
}
and you'll see the same errors on the console since process
forces the parser to consume the entire input, all the way to EOF
.
精彩评论