use of org.eclipse.xtext.Alternatives in project xtext-core by eclipse.
the class XtextValidationTest method testNegatedTokenNotEOF_2.
@Test
public void testNegatedTokenNotEOF_2() throws Exception {
String grammarAsText = "grammar test with org.eclipse.xtext.common.Terminals\n" + "generate test 'http://test'\n" + "A: foo=DUMMY;\n" + "terminal DUMMY: !(EOF | ID);";
Grammar grammar = (Grammar) getModel(grammarAsText);
XtextValidator validator = get(XtextValidator.class);
ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(null, true, false);
TerminalRule terminal = (TerminalRule) grammar.getRules().get(1);
NegatedToken token = (NegatedToken) terminal.getAlternatives();
messageAcceptor.expectedContext(((Alternatives) token.getTerminal()).getElements().get(0));
configureValidator(validator, messageAcceptor, token);
validator.checkNegatedTokenNotEOF(token);
messageAcceptor.validate();
}
use of org.eclipse.xtext.Alternatives in project xtext-core by eclipse.
the class KeywordAlternativeConverter method setRule.
/**
* @throws IllegalArgumentException if the rule is not a datatype rule or does not match
* the pattern <pre>RuleName: 'keyword' | 'other';</pre>
*/
@Override
public void setRule(AbstractRule rule) {
if (!GrammarUtil.isDatatypeRule(rule))
throw new IllegalArgumentException(rule.getName() + " is not a data type rule");
if (!(rule.getAlternatives() instanceof Alternatives)) {
if (rule.getAlternatives() instanceof RuleCall) {
delegateRule = ((RuleCall) rule.getAlternatives()).getRule();
keywords = Collections.emptySet();
return;
}
throw mismatchedRuleBody(rule);
}
Alternatives alternatives = (Alternatives) rule.getAlternatives();
ImmutableSet.Builder<String> builder = ImmutableSet.builder();
for (AbstractElement element : alternatives.getElements()) {
processElement(element, rule, builder);
}
keywords = builder.build();
}
use of org.eclipse.xtext.Alternatives in project xtext-core by eclipse.
the class XtextValidationTest method testCrossReferenceNotInAlternatives.
@Test
public void testCrossReferenceNotInAlternatives() throws Exception {
String grammarAsText = "grammar test with org.eclipse.xtext.common.Terminals\n" + "generate test 'http://test'\n" + "A: foo=([A|ID]|[A|STRING]);";
Grammar grammar = (Grammar) getModel(grammarAsText);
XtextValidator validator = get(XtextValidator.class);
ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(null, true, false);
Assignment assignment = (Assignment) grammar.getRules().get(0).getAlternatives();
Alternatives alternatives = (Alternatives) assignment.getTerminal();
messageAcceptor.expectedContext(alternatives);
configureValidator(validator, messageAcceptor, alternatives);
validator.checkCrossReferenceNotInAlternatives(alternatives);
messageAcceptor.validate();
}
use of org.eclipse.xtext.Alternatives in project xtext-core by eclipse.
the class AntlrGrammarGenUtil method doGetPredicatedElement.
private static AbstractElement doGetPredicatedElement(final AbstractElement element) {
if (element instanceof Alternatives || element instanceof Group) {
EcoreUtil.Copier copier = new EcoreUtil.Copier(true, true) {
private static final long serialVersionUID = 1L;
@Override
public EObject copy(EObject eObject) {
if (eObject instanceof AbstractElement) {
if (((AbstractElement) eObject).isFirstSetPredicated() && isLastInGroup(eObject)) {
Collection<AbstractElement> firstSet = EcoreUtil.copyAll(getFirstSet((AbstractElement) eObject));
if (firstSet.size() == 1) {
AbstractElement result = firstSet.iterator().next();
result.setCardinality(((AbstractElement) eObject).getCardinality());
return result;
}
Alternatives result = XtextFactory.eINSTANCE.createAlternatives();
for (AbstractElement firstElement : firstSet) {
firstElement.setCardinality(null);
}
result.getElements().addAll(firstSet);
result.setCardinality(((AbstractElement) eObject).getCardinality());
return result;
}
}
return super.copy(eObject);
}
private boolean isLastInGroup(EObject eObject) {
if (eObject == element) {
return true;
}
EObject container = eObject.eContainer();
if (container instanceof Group) {
List<AbstractElement> siblings = ((Group) container).getElements();
if (siblings.get(siblings.size() - 1) == eObject) {
return isLastInGroup(container);
}
}
if (container instanceof Alternatives) {
return isLastInGroup(container);
}
return false;
}
};
AbstractElement clone = (AbstractElement) copier.copy(element);
copier.copyReferences();
return clone;
}
return element;
}
use of org.eclipse.xtext.Alternatives in project xtext-core by eclipse.
the class Xtext2EcoreTransformer method deriveFeatures.
private Xtext2EcoreInterpretationContext deriveFeatures(final Xtext2EcoreInterpretationContext context, AbstractElement element) {
XtextSwitch<Xtext2EcoreInterpretationContext> visitor = new XtextSwitch<Xtext2EcoreInterpretationContext>() {
/*
* Used for Alternatives and UnorderedGroups
*/
@Override
public Xtext2EcoreInterpretationContext caseCompoundElement(CompoundElement object) {
List<Xtext2EcoreInterpretationContext> contexts = new ArrayList<Xtext2EcoreInterpretationContext>();
for (AbstractElement group : object.getElements()) {
contexts.add(deriveFeatures(context, group));
}
Xtext2EcoreInterpretationContext result = context;
if (!contexts.isEmpty()) {
if (GrammarUtil.isOptionalCardinality(object)) {
contexts.add(0, result);
} else {
result = contexts.get(0);
}
result = result.mergeSpawnedContexts(contexts);
}
return result;
}
@Override
public Xtext2EcoreInterpretationContext caseAssignment(Assignment object) {
try {
context.addFeature(object);
} catch (TransformationException ex) {
reportError(ex);
}
return context;
}
@Override
public Xtext2EcoreInterpretationContext caseGroup(Group object) {
return visitElements(object, object.getElements());
}
private Xtext2EcoreInterpretationContext visitElements(AbstractElement caller, List<AbstractElement> elementsToProcess) {
Xtext2EcoreInterpretationContext result = deriveFeatures(context.spawnContextForGroup(), elementsToProcess);
if (GrammarUtil.isMultipleCardinality(caller)) {
result = deriveFeatures(result.spawnContextForGroup(), elementsToProcess);
}
if (GrammarUtil.isOptionalCardinality(caller)) {
result = result.mergeSpawnedContexts(Arrays.asList(context, result));
}
return result;
}
@Override
public Xtext2EcoreInterpretationContext caseAlternatives(Alternatives object) {
List<Xtext2EcoreInterpretationContext> contexts = newArrayList();
if (GrammarUtil.isOptionalCardinality(object)) {
contexts.add(context);
}
for (AbstractElement alternative : object.getElements()) {
contexts.add(deriveFeatures(context.spawnContextForGroup(), alternative));
}
Xtext2EcoreInterpretationContext result = context.mergeSpawnedContexts(contexts);
if (GrammarUtil.isMultipleCardinality(object)) {
for (AbstractElement alternative : object.getElements()) {
deriveFeatures(result.spawnContextForGroup(), alternative);
}
}
return result;
}
@Override
public Xtext2EcoreInterpretationContext caseRuleCall(RuleCall object) {
AbstractRule calledRule = object.getRule();
if (isWildcardFragment(calledRule)) {
AbstractElement ruleBody = calledRule.getAlternatives();
if (ruleBody != null) {
return visitElements(object, Collections.singletonList(ruleBody));
}
return context;
}
if (isParserRuleFragment(calledRule)) {
return context;
}
if (!GrammarUtil.isOptionalCardinality(object)) {
// announced during the first iteration
if (calledRule != null && calledRule instanceof ParserRule && !GrammarUtil.isDatatypeRule((ParserRule) calledRule)) {
try {
EClassifierInfo eClassifierInfo = findOrCreateEClassifierInfo(calledRule);
return context.spawnContextWithCalledRule(eClassifierInfo, object);
} catch (TransformationException e) {
reportError(e);
}
}
}
return context;
}
@Override
public Xtext2EcoreInterpretationContext caseAction(Action object) {
try {
TypeRef actionTypeRef = object.getType();
EClassifierInfo actionType = findOrCreateEClassifierInfo(actionTypeRef, null, true);
EClassifierInfo currentCompatibleType = context.getCurrentCompatibleType();
Xtext2EcoreInterpretationContext ctx = context.spawnContextWithReferencedType(actionType, object);
if (object.getFeature() != null) {
ctx.addFeature(object.getFeature(), currentCompatibleType, GrammarUtil.isMultipleAssignment(object), true, object);
}
return ctx;
} catch (TransformationException e) {
reportError(e);
}
return context;
}
@Override
public Xtext2EcoreInterpretationContext defaultCase(EObject object) {
return context;
}
};
return visitor.doSwitch(element);
}
Aggregations