Search in sources :

Example 41 with AbstractElement

use of org.eclipse.xtext.AbstractElement in project xtext-core by eclipse.

the class CrossReferenceImpl method basicSetTerminal.

/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
public NotificationChain basicSetTerminal(AbstractElement newTerminal, NotificationChain msgs) {
    AbstractElement oldTerminal = terminal;
    terminal = newTerminal;
    if (eNotificationRequired()) {
        ENotificationImpl notification = new ENotificationImpl(this, Notification.SET, XtextPackage.CROSS_REFERENCE__TERMINAL, oldTerminal, newTerminal);
        if (msgs == null)
            msgs = notification;
        else
            msgs.add(notification);
    }
    return msgs;
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement) ENotificationImpl(org.eclipse.emf.ecore.impl.ENotificationImpl)

Example 42 with AbstractElement

use of org.eclipse.xtext.AbstractElement 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);
}
Also used : Group(org.eclipse.xtext.Group) ParserRule(org.eclipse.xtext.ParserRule) Action(org.eclipse.xtext.Action) AbstractElement(org.eclipse.xtext.AbstractElement) TypeRef(org.eclipse.xtext.TypeRef) ArrayList(java.util.ArrayList) Alternatives(org.eclipse.xtext.Alternatives) RuleCall(org.eclipse.xtext.RuleCall) Assignment(org.eclipse.xtext.Assignment) EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject) List(java.util.List) ArrayList(java.util.ArrayList) XtextSwitch(org.eclipse.xtext.util.XtextSwitch) CompoundElement(org.eclipse.xtext.CompoundElement) AbstractRule(org.eclipse.xtext.AbstractRule)

Example 43 with AbstractElement

use of org.eclipse.xtext.AbstractElement in project xtext-core by eclipse.

the class CardinalityAwareEcoreFactory method set.

@Override
public void set(EObject object, String feature, Object value, String ruleName, INode node) throws ValueConverterException {
    if (object instanceof AbstractElement && XtextPackage.Literals.ABSTRACT_ELEMENT__CARDINALITY.getName().equals(feature)) {
        AbstractElement casted = (AbstractElement) object;
        String knownCardinality = casted.getCardinality();
        if (knownCardinality != null) {
            String newCardinality = String.valueOf(getTokenAsStringIfPossible(value));
            if ("*".equals(newCardinality)) {
                casted.setCardinality("*");
            } else if ("*".equals(knownCardinality)) {
            // nothing to do
            } else if ("+".equals(knownCardinality)) {
                if ("?".equals(newCardinality)) {
                    casted.setCardinality("*");
                }
            } else if ("?".equals(knownCardinality)) {
                if ("+".equals(newCardinality)) {
                    casted.setCardinality("*");
                }
            }
            throw new MoreThanOneCardinalityException(newCardinality, casted.getCardinality(), node);
        }
    }
    super.set(object, feature, value, ruleName, node);
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement)

Example 44 with AbstractElement

use of org.eclipse.xtext.AbstractElement in project xtext-core by eclipse.

the class Context2NameFunction method getUniqueActionName.

public String getUniqueActionName(Action action) {
    List<Integer> indices = Lists.newArrayList();
    EObject current = action;
    while (current.eContainer() instanceof AbstractElement) {
        indices.add(current.eContainer().eContents().indexOf(current));
        current = current.eContainer();
    }
    Collections.reverse(indices);
    return action.getType().getClassifier().getName() + "_" + Joiner.on('_').join(indices);
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement) EObject(org.eclipse.emf.ecore.EObject)

Example 45 with AbstractElement

use of org.eclipse.xtext.AbstractElement in project xtext-core by eclipse.

the class SemanticSequencerNfaProvider method initContentValidationNeeded.

protected void initContentValidationNeeded(EClass clazz, Nfa<ISemState> nfa) {
    Multimap<EStructuralFeature, AbstractElement> assignments = LinkedHashMultimap.create();
    Set<ISemState> states = new NfaUtil().collect(nfa);
    for (ISemState state : states) if (state.getFeature() != null)
        assignments.put(state.getFeature(), state.getAssignedGrammarElement());
    boolean[] validationNeeded = new boolean[clazz.getFeatureCount()];
    for (EStructuralFeature feature : clazz.getEAllStructuralFeatures()) validationNeeded[clazz.getFeatureID(feature)] = isContentValidationNeeded(assignments.get(feature));
    for (ISemState state : states) if (state.getFeature() != null && validationNeeded[state.getFeatureID()])
        ((SemState) state).contentValidationNeeded = Lists.newArrayList(assignments.get(state.getFeature()));
    else
        ((SemState) state).contentValidationNeeded = Collections.emptyList();
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) NfaUtil(org.eclipse.xtext.util.formallang.NfaUtil)

Aggregations

AbstractElement (org.eclipse.xtext.AbstractElement)126 EObject (org.eclipse.emf.ecore.EObject)39 AbstractRule (org.eclipse.xtext.AbstractRule)34 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)31 RuleCall (org.eclipse.xtext.RuleCall)26 ParserRule (org.eclipse.xtext.ParserRule)18 Grammar (org.eclipse.xtext.Grammar)17 EList (org.eclipse.emf.common.util.EList)15 Assignment (org.eclipse.xtext.Assignment)15 Test (org.junit.Test)14 Group (org.eclipse.xtext.Group)13 UnorderedGroup (org.eclipse.xtext.UnorderedGroup)13 List (java.util.List)12 ArrayList (java.util.ArrayList)11 XtextResource (org.eclipse.xtext.resource.XtextResource)11 Action (org.eclipse.xtext.Action)10 TerminalRule (org.eclipse.xtext.TerminalRule)10 Alternatives (org.eclipse.xtext.Alternatives)9 EClassifier (org.eclipse.emf.ecore.EClassifier)8 InternalEObject (org.eclipse.emf.ecore.InternalEObject)8