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 13, 2024
1 parent e8e6e6d commit 760ab69
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
package org.eclipse.xtext.parser.unorderedGroups;

import java.io.InputStream;
import java.util.HashMap;

import org.eclipse.emf.common.util.BasicDiagnostic;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.AbstractElement;
import org.eclipse.xtext.Action;
import org.eclipse.xtext.Alternatives;
Expand All @@ -22,6 +25,8 @@
import org.eclipse.xtext.XtextStandaloneSetup;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.testing.AbstractXtextTests;
import org.eclipse.xtext.xtext.XtextValidator;
import org.junit.Assert;
import org.junit.Test;

/**
Expand Down Expand Up @@ -126,6 +131,21 @@ 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);
Iterable<EObject> contents = () -> resource.getAllContents();
XtextValidator validator = get(XtextValidator.class);
boolean result = true;
for (final EObject eObject : contents) {
result &= validator.validate(eObject, new BasicDiagnostic(), new HashMap<>());
}
Assert.assertTrue(result);
}

@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 760ab69

Please sign in to comment.