Report errors that occur while lexing a grammar (fixes #262)

This commit is contained in:
Sam Harwell 2013-05-26 13:43:15 -05:00
parent 611e5ebf02
commit d3af4a2f3a
4 changed files with 29 additions and 3 deletions

View File

@ -764,8 +764,12 @@ WSNLCHARS
ERRCHAR
: .
{
// TODO: Issue error message
//
Token t = new CommonToken(input, state.type, state.channel, state.tokenStartCharIndex, getCharIndex()-1);
t.setLine(state.tokenStartLine);
t.setText(state.text);
t.setCharPositionInLine(state.tokenStartCharPositionInLine);
String msg = getTokenErrorDisplay(t) + " came as a complete surprise to me";
grammarError(ErrorType.SYNTAX_ERROR, t, msg);
skip();
}
;

View File

@ -31,6 +31,7 @@
package org.antlr.v4.parse;
import org.antlr.runtime.CharStream;
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.Token;
import org.antlr.v4.Tool;
import org.antlr.v4.tool.ErrorType;
@ -43,6 +44,12 @@ public class ToolANTLRLexer extends ANTLRLexer {
this.tool = tool;
}
@Override
public void displayRecognitionError(String[] tokenNames, RecognitionException e) {
String msg = getErrorMessage(e, tokenNames);
tool.errMgr.syntaxError(ErrorType.SYNTAX_ERROR, getSourceName(), e.token, e, msg);
}
@Override
public void grammarError(ErrorType etype, Token token, Object... args) {
tool.errMgr.grammarError(etype, getSourceName(), token, args);

View File

@ -38,7 +38,7 @@ public class TestBasicSemanticErrors extends BaseTest {
static String[] U = {
// INPUT
"parser grammar U;\n" +
"options { foo=bar; k=\"3\";}\n" +
"options { foo=bar; k=3;}\n" +
"tokens {\n" +
" ID,\n" +
" f,\n" +

View File

@ -172,6 +172,21 @@ public class TestToolSyntaxErrors extends BaseTest {
super.testErrors(pair, true);
}
/**
* This is a regression test for antlr/antlr4#262
* "Parser Rule Name Starting With an Underscore"
* https://github.com/antlr/antlr4/issues/262
*/
@Test public void testParserRuleNameStartingWithUnderscore() {
String[] pair = new String[] {
"grammar A;\n" +
"_a : 'x' ;\n",
"error(" + ErrorType.SYNTAX_ERROR.code + "): A.g4:2:0: syntax error: '_' came as a complete surprise to me\n"
};
super.testErrors(pair, true);
}
/**
* This is a regression test for antlr/antlr4#194
* "NullPointerException on 'options{}' in grammar file"