Wyjątki dotyczące obsługi Antlr
Opracowałem skomplikowaną gramatykę używając Antlr 3 używając drzewa AST. ANTLR generuje Lexer i Parser. Problem polega na tym, że gdy użytkownik wprowadza składnię, która na przykład nie jest poprawna, gramatyka oczekuje „;”. Użytkownik nie wpisuje tego, a następnie w moim środowisku Eclipse IDE otrzymuję następujący wyjątek:
line 1:24 mismatched input '<EOF>' expecting ';'
W jaki sposób można obsługiwać ten wyjątek, ponieważ próbuję złapać ten wyjątek, ale wyjątek nie został przechwycony. Czy to w ogóle wyjątek? Nie rozumiem, dlaczego ten wyjątek nie został złapany. Próbowałem się tego dowiedzieć, ale strona Antlr wydaje się już od jakiegoś czasu wyłączona.
Spojrzałem na następujące:Obsługa wyjątków ANTLR za pomocą „$”, Java i podążyłem za tym przykładem, ale kiedy Lexer generuje kod przez dodanie wyjątku RuntimeException (), otrzymuję nieosiągalny kod.
Nie jestem pewien, co robić.
Kiedy próbuję uzyskać liczbę błędów składniowych z parsera, wyświetla 0.
EDYTOWAĆ:
Znalazłem rozwiązanie, które działa, patrząc na:ANTLR nie rzuca błędów na nieprawidłowe dane wejściowe
Jednak gdy próbuję odzyskać komunikat Exception, ma on wartość NULL. Czy wszystko zostało poprawnie ustawione? Zobacz przykładową gramatykę:
grammar i;
options {
output=AST;
}
@header {
package com.data;
}
@rulecatch {
catch(RecognitionException e) {
throw e;
}
}
// by having these below it makes no difference
/**@parser::members {
@Override
public void reportError(RecognitionException e) {
throw new RuntimeException("Exception : " + " " + e.getMessage());
}
}
@lexer::members {
@Override
public void reportError(RecognitionException e) {
throw new RuntimeException("Exception : " + " " + e.getMessage());
}
}*/
EDYTOWAĆ:
Zobacz co mam do tej pory:
grammar i;
options {
output=AST;
}
@header {
package com.data;
}
@rulecatch {
// ANTLR does not generate its normal rule try/catch
catch(RecognitionException e) {
throw e;
}
}
@parser::members {
@Override
public void displayRecognitionError(String[] tokenNames, RecognitionException e) {
String hdr = getErrorHeader(e);
String msg = getErrorMessage(e, tokenNames);
throw new RuntimeException(hdr + ":" + msg);
}
}
@lexer::members {
@Override
public void displayRecognitionError(String[] tokenNames, RecognitionException e) {
String hdr = getErrorHeader(e);
String msg = getErrorMessage(e, tokenNames);
throw new RuntimeException(hdr + ":" + msg);
}
}
operatorLogic : 'AND' | 'OR';
value : STRING;
query : (select)*;
select : 'SELECT'^ functions 'FROM table' filters?';';
operator : '=' | '!=' | '<' | '>' | '<=' | '>=';
filters : 'WHERE'^ conditions;
members : STRING operator value;
conditions : (members (operatorLogic members)*);
functions : '*';
STRING : ('a'..'z'|'A'..'Z')+;
WS : (' '|'\t'|'\f'|'\n'|'\r')+ {skip();}; // handle white space between keywords
public class Processor {
public Processor() {
}
/**
* This method builds the MQL Parser.
* @param args the args.
* @return the built IParser.
*/
private IParser buildMQLParser(String query) {
CharStream cs = new ANTLRStringStream(query);
// the input needs to be lexed
ILexer lexer = new ILexer(cs);
CommonTokenStream tokens = new CommonTokenStream();
IParser parser = new IParser(tokens);
tokens.setTokenSource(lexer);
// use the ASTTreeAdaptor so that the grammar is aware to build tree in AST format
parser.setTreeAdaptor((TreeAdaptor) new ASTTreeAdaptor().getASTTreeAdaptor());
return parser;
}
/**
* This method parses the MQL query.
* @param query the query.
*/
public void parseMQL(String query) {
IParser parser = buildMQLParser(query);
CommonTree commonTree = null;
try {
commonTree = (CommonTree) parser.query().getTree();
}
catch(Exception e) {
System.out.println("Exception :" + " " + e.getMessage());
}
}
}
public class ASTTreeAdaptor {
public ASTTreeAdaptor() {
}
/**
* This method is used to create a TreeAdaptor.
* @return a treeAdaptor.
*/
public Object getASTTreeAdaptor() {
TreeAdaptor treeAdaptor = new CommonTreeAdaptor() {
public Object create(Token payload) {
return new CommonTree(payload);
}
};
return treeAdaptor;
}
}
Więc kiedy wprowadzę następujące polecenie: SELECT * FROM table
bez ';' Otrzymuję wyjątek MismatchedTokenException:
catch(Exception e) {
System.out.println("Exception : " + " " e);
}
Kiedy próbuję:
e.getMessage();
zwraca wartość null.