在第三行输入以下错误时:
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
我用antlrWorks调试了一下,发现调试器输出窗口显示了第三行对应的错误信息:
output/__Test___input.txt line 3:8 required (...)+ loop does not match any at input '' output/__Test___input.txt line 3:9 缺少 END_COMMAND at 'tenga'
但是当我自己运行应用程序时,这些错误消息不会显示在控制台上。
每当错误出现在第一行时,错误消息就会显示在控制台上,例如:
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;
控制台输出:
inputSql.rst 行 1:8 需要 (...)+ 循环与输入“”处的任何内容都不匹配 inputSql.rst 行 1:9 在“tenga”处缺少 END_COMMAND
当错误不在第一行时,我怎么能让它们也显示在控制台上?
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; } ;