Merge pull request #425 from sharwell/fix-299

Fix 299
This commit is contained in:
Sam Harwell 2014-01-16 18:38:45 -08:00
commit ef0b38e2bb
2 changed files with 54 additions and 1 deletions

View File

@ -307,13 +307,23 @@ public class RuleFunction extends OutputModelObject {
* Common
*/
/**
* Generate a frequency set as the union of two input sets. If an
* element is contained in both sets, the value for the output will be
* the maximum of the two input values.
*
* @param a The first set.
* @param b The second set.
* @return The union of the two sets, with the maximum value chosen
* whenever both sets contain the same key.
*/
protected static FrequencySet<String> combineMax(FrequencySet<String> a, FrequencySet<String> b) {
FrequencySet<String> result = combineAndClip(a, b, 1);
for (Map.Entry<String, MutableInt> entry : a.entrySet()) {
result.get(entry.getKey()).v = entry.getValue().v;
}
for (Map.Entry<String, MutableInt> entry : a.entrySet()) {
for (Map.Entry<String, MutableInt> entry : b.entrySet()) {
MutableInt slot = result.get(entry.getKey());
slot.v = Math.max(slot.v, entry.getValue().v);
}
@ -321,6 +331,18 @@ public class RuleFunction extends OutputModelObject {
return result;
}
/**
* Generate a frequency set as the union of two input sets, with the
* values clipped to a specified maximum value. If an element is
* contained in both sets, the value for the output, prior to clipping,
* will be the sum of the two input values.
*
* @param a The first set.
* @param b The second set.
* @param clip The maximum value to allow for any output.
* @return The sum of the two sets, with the individual elements clipped
* to the maximum value gived by {@code clip}.
*/
protected static FrequencySet<String> combineAndClip(FrequencySet<String> a, FrequencySet<String> b, int clip) {
FrequencySet<String> result = new FrequencySet<String>();
for (Map.Entry<String, MutableInt> entry : a.entrySet()) {

View File

@ -377,4 +377,35 @@ public class TestParserExec extends BaseTest {
assertEquals("(file (item a) <EOF>)\n", found);
assertNull(stderrDuringParse);
}
/**
* This is a regressino test for antlr/antlr4#299 "Repeating subtree not
* accessible in visitor".
* https://github.com/antlr/antlr4/issues/299
*/
@Test public void testListLabelForClosureContext() throws Exception {
String grammar =
"grammar T;\n" +
"ifStatement\n" +
"@after { List<? extends ElseIfStatementContext> items = $ctx.elseIfStatement(); }\n" +
" : 'if' expression\n" +
" ( ( 'then'\n" +
" executableStatement*\n" +
" elseIfStatement* // <--- problem is here\n" +
" elseStatement?\n" +
" 'end' 'if'\n" +
" ) | executableStatement )\n" +
" ;\n" +
"\n" +
"elseIfStatement\n" +
" : 'else' 'if' expression 'then' executableStatement*\n" +
" ;\n"
+ "expression : 'a' ;\n"
+ "executableStatement : 'a' ;\n"
+ "elseStatement : 'a' ;\n";
String input = "a";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "expression", input, false);
assertEquals("", found);
assertNull(stderrDuringParse);
}
}