mirror of
https://github.com/sigmasternchen/xtext-core
synced 2025-03-15 08:18:55 +00:00
hoisting in unordered groups uses one or more cardinality if it contains
non-optional elements TODO: fix special case in anlternatives hoisting
This commit is contained in:
parent
2303cce7c4
commit
1d4b649755
2 changed files with 25 additions and 2 deletions
|
@ -287,6 +287,23 @@ public class HoistingProcessorTest extends AbstractXtextTests {
|
|||
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("((" + getSyntaxForKeywordToken("a", 1) + " || (p0)) && (" + getSyntaxForKeywordToken("b", 1) + " || (p1)))", guard.render());
|
||||
}
|
||||
|
||||
//@Test
|
||||
public void testUnorderedGroupsWithoutMandatoryContent() throws Exception {
|
||||
// @formatter:off
|
||||
String model =
|
||||
MODEL_PREAMBLE +
|
||||
"S: (($$ p0 $$?=> 'a')? & ($$ p1 $$?=> 'b')?) $$ p2 $$?=> 's';";
|
||||
// @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());
|
||||
|
@ -307,7 +324,7 @@ public class HoistingProcessorTest extends AbstractXtextTests {
|
|||
HoistingGuard guard = hoistingProcessor.findHoistingGuard(rule.getAlternatives());
|
||||
assertFalse(guard.isTrivial());
|
||||
assertTrue(guard.hasTerminal());
|
||||
assertEquals("((" + getSyntaxForKeywordToken("s", 1) + " || (p2)) && (" + getSyntaxForKeywordToken("a", 1) + " || (p0)) && (" + getSyntaxForKeywordToken("b", 1) + " || (p1)))", guard.render());
|
||||
assertEquals("((" + getSyntaxForKeywordToken("a", 1) + " || (p0)) && (" + getSyntaxForKeywordToken("b", 1) + " || (p1)))", guard.render());
|
||||
|
||||
// check sizes of groups in unordered group
|
||||
Group group = (Group) rule.getAlternatives();
|
||||
|
|
|
@ -25,6 +25,7 @@ import org.eclipse.xtext.Action;
|
|||
import org.eclipse.xtext.Alternatives;
|
||||
import org.eclipse.xtext.Assignment;
|
||||
import org.eclipse.xtext.CompoundElement;
|
||||
import org.eclipse.xtext.GrammarUtil;
|
||||
import org.eclipse.xtext.Group;
|
||||
import org.eclipse.xtext.JavaAction;
|
||||
import org.eclipse.xtext.RuleCall;
|
||||
|
@ -152,7 +153,12 @@ public class HoistingProcessor {
|
|||
|
||||
Alternatives virtualAlternatives = XtextFactory.eINSTANCE.createAlternatives();
|
||||
addElementsToCompoundElement(virtualAlternatives, ((UnorderedGroup) element).getElements());
|
||||
virtualAlternatives.setCardinality("*");
|
||||
if (((UnorderedGroup) element).getElements().stream().allMatch(GrammarUtil::isOptionalCardinality)) {
|
||||
virtualAlternatives.setCardinality("*");
|
||||
// TODO: alternatives analysis needs special case
|
||||
} else {
|
||||
virtualAlternatives.setCardinality("+");
|
||||
}
|
||||
|
||||
elements.set(i, virtualAlternatives);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue