Skip to content

Commit

Permalink
xtext: XtextValidator: Allow fragments in unordered groups
Browse files Browse the repository at this point in the history
Fixes eclipse-xtext#3068

Signed-off-by: Tommaso Fonda <[email protected]>
  • Loading branch information
tfonda-fbk committed Aug 14, 2024
1 parent e8e6e6d commit a2383ce
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.eclipse.xtext.XtextStandaloneSetup;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.testing.AbstractXtextTests;
import org.junit.Assert;
import org.junit.Test;

/**
Expand Down Expand Up @@ -126,6 +127,15 @@ public void setUp() throws Exception {
getModelAndExpect("name=(ID & STRING)", 2);
}

@Test public void testFragments() throws Exception {
String grammar =
"MyRule;\n" +
"MyRule: IntFeature & 'keyword';\n" +
"fragment IntFeature: myFeature=INT\n";
XtextResource resource = getResourceFromString(grammar);
Assert.assertTrue(resource.getErrors().isEmpty());
}

@Override
protected InputStream getAsStream(String model) {
return super.getAsStream(surroundWithGrammar(model));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1342,6 +1342,23 @@ public boolean apply(Diagnostic input) {
messageAcceptor.validate();
}

@Test public void testCheckFragmentCallInUnorderedGroup() throws Exception {
XtextValidator validator = get(XtextValidator.class);
UnorderedGroup unorderedGroup = XtextFactory.eINSTANCE.createUnorderedGroup();
RuleCall ruleCall = XtextFactory.eINSTANCE.createRuleCall();
TypeRef typeRef = XtextFactory.eINSTANCE.createTypeRef();
typeRef.setClassifier(EcorePackage.Literals.EOBJECT);
ParserRule parserRule = XtextFactory.eINSTANCE.createParserRule();
parserRule.setType(typeRef);
parserRule.setFragment(true);
ruleCall.setRule(parserRule);
unorderedGroup.getElements().add(ruleCall);
ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(null, false, false);
validator.setMessageAcceptor(messageAcceptor);
validator.checkRuleCallInUnorderedGroup(ruleCall);
messageAcceptor.validate();
}

@Test public void testCheckRuleCallInUnorderedGroup_01() throws Exception {
XtextValidator validator = get(XtextValidator.class);
UnorderedGroup unorderedGroup = XtextFactory.eINSTANCE.createUnorderedGroup();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,8 @@ public void checkRuleCallInUnorderedGroup(final RuleCall call) {
return;
if (GrammarUtil.isAssigned(call))
return;
if (GrammarUtil.isEObjectFragmentRuleCall(call))
return;
if (EcoreUtil2.getContainerOfType(call, UnorderedGroup.class) != null)
error(
"Unassigned rule calls may not be used in unordered groups.",
Expand Down

0 comments on commit a2383ce

Please sign in to comment.