Fix handling of empty options{} block in grammar (fixes #194)

This commit is contained in:
Sam Harwell 2013-03-26 23:29:03 -05:00
parent 507f331bd0
commit b4413e8656
4 changed files with 64 additions and 2 deletions

View File

@ -756,7 +756,7 @@ public class Grammar implements AttributeResolver {
*/
public static void setNodeOptions(GrammarAST node, GrammarAST options) {
GrammarASTWithOptions t = (GrammarASTWithOptions)node;
if ( t.getChildCount()==0 ) return;
if ( t.getChildCount()==0 || options.getChildCount()==0 ) return;
for (Object o : options.getChildren()) {
GrammarAST c = (GrammarAST)o;
if ( c.getType()==ANTLRParser.ASSIGN ) {

View File

@ -293,7 +293,7 @@ public class GrammarTransformPipeline {
// COPY OPTIONS
GrammarAST optionsRoot =
(GrammarAST)combinedAST.getFirstChildWithType(ANTLRParser.OPTIONS);
if ( optionsRoot!=null ) {
if ( optionsRoot!=null && optionsRoot.getChildCount()!=0 ) {
GrammarAST lexerOptionsRoot = (GrammarAST)adaptor.dupNode(optionsRoot);
lexerAST.addChild(lexerOptionsRoot);
GrammarAST[] options = optionsRoot.getChildren().toArray(new GrammarAST[0]);

View File

@ -664,4 +664,21 @@ public class TestCompositeGrammars extends BaseTest {
assertEquals("", found);
}
@Test public void testImportedGrammarWithEmptyOptions() throws Exception {
String slave =
"parser grammar S;\n" +
"options {}\n" +
"a : B ;\n";
mkdir(tmpdir);
writeFile(tmpdir, "S.g4", slave);
String master =
"grammar M;\n" +
"import S;\n" +
"s : a ;\n" +
"B : 'b' ;" +
"WS : (' '|'\\n') -> skip ;\n" ;
String found = execParser("M.g4", master, "MParser", "MLexer",
"s", "b", debug);
assertEquals("", found);
}
}

View File

@ -172,4 +172,49 @@ public class TestToolSyntaxErrors extends BaseTest {
super.testErrors(pair, true);
}
/**
* This is a regression test for antlr/antlr4#194
* "NullPointerException on 'options{}' in grammar file"
* https://github.com/antlr/antlr4/issues/194
*/
@Test public void testEmptyGrammarOptions() {
String[] pair = new String[] {
"grammar A;\n" +
"options {}\n" +
"a : 'x' ;\n",
""
};
super.testErrors(pair, true);
}
/**
* This is a "related" regression test for antlr/antlr4#194
* "NullPointerException on 'options{}' in grammar file"
* https://github.com/antlr/antlr4/issues/194
*/
@Test public void testEmptyRuleOptions() {
String[] pair = new String[] {
"grammar A;\n" +
"a options{} : 'x' ;\n",
""
};
super.testErrors(pair, true);
}
/**
* This is a "related" regression test for antlr/antlr4#194
* "NullPointerException on 'options{}' in grammar file"
* https://github.com/antlr/antlr4/issues/194
*/
@Test public void testEmptyBlockOptions() {
String[] pair = new String[] {
"grammar A;\n" +
"a : (options{} : 'x') ;\n",
""
};
super.testErrors(pair, true);
}
}