fixed non-optimal guard with nested groups

nested groups produce non-optimal guard (redundant parentheses)
-> group guard now adds elements of groups instead of the group itself

added test case
This commit is contained in:
overflowerror 2021-12-05 18:31:57 +01:00
parent 83232e5a43
commit 8d34bc2a23
2 changed files with 25 additions and 1 deletions

View file

@ -225,6 +225,23 @@ public class HoistingProcessorTest extends AbstractXtextTests {
assertTrue(guard.hasTerminal());
}
@Test
public void testGroupInGroup_expectMinimalParentheses() throws Exception {
// @formatter:off
String model =
MODEL_PREAMBLE +
"S: $$ p0 $$?=> ($$ p1 $$?=> ($$ p2 $$?=> 'a')); ";
// @formatter:off
XtextResource resource = getResourceFromString(model);
Grammar grammar = ((Grammar) resource.getContents().get(0));
AbstractRule rule = getRule(grammar, "S");
HoistingGuard guard = hoistingProcessor.findHoistingGuard(rule.getAlternatives());
assertFalse(guard.isTrivial());
assertTrue(guard.hasTerminal());
assertEquals("((p0) && (p1) && (p2))", guard.render());
}
@Test
public void testCardinalityPlusPredicate_expectPredicateAfterGroupNotInGuard() throws Exception {
// @formatter:off

View file

@ -31,8 +31,15 @@ public class GroupGuard implements HoistingGuard {
}
public void add(Guard guard) {
if (!guard.isTrivial())
if (guard.isTrivial()) {
return;
}
if (guard instanceof GroupGuard) {
((GroupGuard) guard).elementGuards.forEach(this::add);
} else {
elementGuards.add(guard);
}
}
public void setHasTerminal() {