mirror of
https://github.com/sigmasternchen/xtext-core
synced 2025-03-15 08:18:55 +00:00
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:
parent
83232e5a43
commit
8d34bc2a23
2 changed files with 25 additions and 1 deletions
|
@ -225,6 +225,23 @@ public class HoistingProcessorTest extends AbstractXtextTests {
|
||||||
assertTrue(guard.hasTerminal());
|
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
|
@Test
|
||||||
public void testCardinalityPlusPredicate_expectPredicateAfterGroupNotInGuard() throws Exception {
|
public void testCardinalityPlusPredicate_expectPredicateAfterGroupNotInGuard() throws Exception {
|
||||||
// @formatter:off
|
// @formatter:off
|
||||||
|
|
|
@ -31,8 +31,15 @@ public class GroupGuard implements HoistingGuard {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void add(Guard guard) {
|
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);
|
elementGuards.add(guard);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setHasTerminal() {
|
public void setHasTerminal() {
|
||||||
|
|
Loading…
Reference in a new issue