Fix handling of empty options{} block in grammar (fixes #194)
This commit is contained in:
parent
507f331bd0
commit
b4413e8656
|
@ -756,7 +756,7 @@ public class Grammar implements AttributeResolver {
|
||||||
*/
|
*/
|
||||||
public static void setNodeOptions(GrammarAST node, GrammarAST options) {
|
public static void setNodeOptions(GrammarAST node, GrammarAST options) {
|
||||||
GrammarASTWithOptions t = (GrammarASTWithOptions)node;
|
GrammarASTWithOptions t = (GrammarASTWithOptions)node;
|
||||||
if ( t.getChildCount()==0 ) return;
|
if ( t.getChildCount()==0 || options.getChildCount()==0 ) return;
|
||||||
for (Object o : options.getChildren()) {
|
for (Object o : options.getChildren()) {
|
||||||
GrammarAST c = (GrammarAST)o;
|
GrammarAST c = (GrammarAST)o;
|
||||||
if ( c.getType()==ANTLRParser.ASSIGN ) {
|
if ( c.getType()==ANTLRParser.ASSIGN ) {
|
||||||
|
|
|
@ -293,7 +293,7 @@ public class GrammarTransformPipeline {
|
||||||
// COPY OPTIONS
|
// COPY OPTIONS
|
||||||
GrammarAST optionsRoot =
|
GrammarAST optionsRoot =
|
||||||
(GrammarAST)combinedAST.getFirstChildWithType(ANTLRParser.OPTIONS);
|
(GrammarAST)combinedAST.getFirstChildWithType(ANTLRParser.OPTIONS);
|
||||||
if ( optionsRoot!=null ) {
|
if ( optionsRoot!=null && optionsRoot.getChildCount()!=0 ) {
|
||||||
GrammarAST lexerOptionsRoot = (GrammarAST)adaptor.dupNode(optionsRoot);
|
GrammarAST lexerOptionsRoot = (GrammarAST)adaptor.dupNode(optionsRoot);
|
||||||
lexerAST.addChild(lexerOptionsRoot);
|
lexerAST.addChild(lexerOptionsRoot);
|
||||||
GrammarAST[] options = optionsRoot.getChildren().toArray(new GrammarAST[0]);
|
GrammarAST[] options = optionsRoot.getChildren().toArray(new GrammarAST[0]);
|
||||||
|
|
|
@ -664,4 +664,21 @@ public class TestCompositeGrammars extends BaseTest {
|
||||||
assertEquals("", found);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -172,4 +172,49 @@ public class TestToolSyntaxErrors extends BaseTest {
|
||||||
super.testErrors(pair, true);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue