Merge pull request #399 from sharwell/fix-398

Fix 398
This commit is contained in:
Sam Harwell 2014-01-09 04:56:35 -08:00
commit f51aaae2f5
2 changed files with 43 additions and 3 deletions

View File

@ -290,9 +290,12 @@ public class LexerATNSimulator extends ATNSimulator {
getReachableConfigSet(input, s.configs, reach, t);
if ( reach.isEmpty() ) { // we got nowhere on t from s
if (!reach.hasSemanticContext) {
// we got nowhere on t, don't throw out this knowledge; it'd
// cause a failover from DFA later.
addDFAEdge(s, t, ERROR);
}
// stop when we can't match any more char
return ERROR;
}

View File

@ -740,6 +740,43 @@ public class TestLexerExec extends BaseTest {
assertEquals(expecting, found);
}
/**
* This is a regression test for antlr/antlr4#398 "Lexer: literal matches
* while negated char set fail to match"
* https://github.com/antlr/antlr4/issues/398
*/
@Test
public void testFailingPredicateEvalIsNotCached() {
String grammar =
"lexer grammar TestLexer;\n" +
"\n" +
"fragment WS: [ \\t]+;\n" +
"fragment EOL: '\\r'? '\\n';\n" +
"\n" +
"LINE: WS? ~[\\r\\n]* EOL { !getText().trim().startsWith(\"Item:\") }?;\n" +
"ITEM: WS? 'Item:' -> pushMode(ITEM_HEADING_MODE);\n" +
"\n" +
"mode ITEM_HEADING_MODE;\n" +
"\n" +
"NAME: ~[\\r\\n]+;\n" +
"SECTION_HEADING_END: EOL -> popMode;\n";
String input =
"A line here.\n" +
"Item: name of item\n" +
"Another line.\n" +
"More line.\n";
String found = execLexer("TestLexer.g4", grammar, "TestLexer", input);
String expecting =
"[@0,0:12='A line here.\\n',<1>,1:0]\n" +
"[@1,13:17='Item:',<2>,2:0]\n" +
"[@2,18:30=' name of item',<3>,2:5]\n" +
"[@3,31:31='\\n',<4>,2:18]\n" +
"[@4,32:45='Another line.\\n',<1>,3:0]\n" +
"[@5,46:56='More line.\\n',<1>,4:0]\n" +
"[@6,57:56='<EOF>',<-1>,5:11]\n";
assertEquals(expecting, found);
}
protected String load(String fileName, @Nullable String encoding)
throws IOException
{