Fixes #823. Report unknown rule error in proper file.

This commit is contained in:
Terence Parr 2015-05-19 17:27:04 -07:00
parent f491a1a84f
commit 2ad9ff6b9d
2 changed files with 24 additions and 2 deletions

View File

@ -480,18 +480,18 @@ public class Tool {
@Override @Override
public void ruleRef(GrammarAST ref, ActionAST arg) { public void ruleRef(GrammarAST ref, ActionAST arg) {
RuleAST ruleAST = ruleToAST.get(ref.getText()); RuleAST ruleAST = ruleToAST.get(ref.getText());
String fileName = ref.getToken().getInputStream().getSourceName();
if (Character.isUpperCase(currentRuleName.charAt(0)) && if (Character.isUpperCase(currentRuleName.charAt(0)) &&
Character.isLowerCase(ref.getText().charAt(0))) Character.isLowerCase(ref.getText().charAt(0)))
{ {
badref = true; badref = true;
String fileName = ref.getToken().getInputStream().getSourceName();
errMgr.grammarError(ErrorType.PARSER_RULE_REF_IN_LEXER_RULE, errMgr.grammarError(ErrorType.PARSER_RULE_REF_IN_LEXER_RULE,
fileName, ref.getToken(), ref.getText(), currentRuleName); fileName, ref.getToken(), ref.getText(), currentRuleName);
} }
else if ( ruleAST==null ) { else if ( ruleAST==null ) {
badref = true; badref = true;
errMgr.grammarError(ErrorType.UNDEFINED_RULE_REF, errMgr.grammarError(ErrorType.UNDEFINED_RULE_REF,
g.fileName, ref.token, ref.getText()); fileName, ref.token, ref.getText());
} }
} }
@Override @Override

View File

@ -30,11 +30,14 @@
package org.antlr.v4.test.tool; package org.antlr.v4.test.tool;
import org.antlr.v4.tool.ANTLRMessage;
import org.antlr.v4.tool.ErrorType; import org.antlr.v4.tool.ErrorType;
import org.antlr.v4.tool.Grammar; import org.antlr.v4.tool.Grammar;
import org.antlr.v4.tool.GrammarSemanticsMessage; import org.antlr.v4.tool.GrammarSemanticsMessage;
import org.junit.Test; import org.junit.Test;
import java.io.File;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
@ -60,6 +63,25 @@ public class TestCompositeGrammars extends BaseTest {
assertEquals(equeue.size(), 0); assertEquals(equeue.size(), 0);
} }
@Test public void testErrorInImportedGetsRightFilename() throws Exception {
String slave =
"parser grammar S;\n" +
"a : 'a' | c;\n";
mkdir(tmpdir);
writeFile(tmpdir, "S.g4", slave);
String master =
"grammar M;\n" +
"import S;\n";
writeFile(tmpdir, "M.g4", master);
ErrorQueue equeue = antlr("M.g4", false, "-lib", tmpdir);
ANTLRMessage msg = equeue.errors.get(0);
assertEquals(ErrorType.UNDEFINED_RULE_REF, msg.getErrorType());
assertEquals("c", msg.getArgs()[0]);
assertEquals(2, msg.line);
assertEquals(10, msg.charPosition);
assertEquals("S.g4", new File(msg.fileName).getName());
}
@Test public void testImportFileNotSearchedForInOutputDir() throws Exception { @Test public void testImportFileNotSearchedForInOutputDir() throws Exception {
String slave = String slave =
"parser grammar S;\n" + "parser grammar S;\n" +