Search in sources :

Example 71 with AbstractElement

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

the class SemanticSequencerNfaProvider method isContentValidationNeeded.

protected boolean isContentValidationNeeded(Collection<AbstractElement> ass) {
    if (ass == null || ass.size() < 2)
        return false;
    Iterator<AbstractElement> it = ass.iterator();
    AbstractElement first = it.next();
    CrossReference firstRef = GrammarUtil.containingCrossReference(first);
    while (it.hasNext()) {
        AbstractElement next = it.next();
        if (next instanceof Action)
            return true;
        if (!EcoreUtil.equals(first, next))
            return true;
        if (firstRef != null) {
            CrossReference nextRef = GrammarUtil.containingCrossReference(next);
            if (nextRef != null && nextRef.getType().getClassifier() != firstRef.getType().getClassifier())
                return true;
        }
    }
    return false;
}
Also used : Action(org.eclipse.xtext.Action) AbstractElement(org.eclipse.xtext.AbstractElement) CrossReference(org.eclipse.xtext.CrossReference)

Example 72 with AbstractElement

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

the class AssignmentFinder method findAssignmentsByValue.

@Override
public Set<AbstractElement> findAssignmentsByValue(EObject semanticObj, Multimap<AbstractElement, ISerializationContext> assignments, Object value, INode node) {
    List<AbstractElement> assignedElements = Lists.newArrayList(assignments.keySet());
    EStructuralFeature feature = FeatureFinderUtil.getFeature(assignedElements.iterator().next(), semanticObj.eClass());
    if (feature instanceof EAttribute) {
        Class<?> clazz = feature.getEType().getInstanceClass();
        if (clazz == Boolean.class || clazz == boolean.class)
            return findValidBooleanAssignments(semanticObj, assignedElements, value);
        else
            return findValidValueAssignments(semanticObj, assignedElements, value);
    } else if (feature instanceof EReference) {
        EReference ref = (EReference) feature;
        if (ref.isContainment())
            return findValidAssignmentsForContainmentRef(semanticObj, assignments, (EObject) value);
        else
            return findValidAssignmentsForCrossRef(semanticObj, assignedElements, (EObject) value, node);
    }
    throw new RuntimeException("unknown feature type");
}
Also used : EAttribute(org.eclipse.emf.ecore.EAttribute) AbstractElement(org.eclipse.xtext.AbstractElement) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) EReference(org.eclipse.emf.ecore.EReference)

Example 73 with AbstractElement

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

the class ContextFinder method collectAssignments.

protected Multimap<AbstractElement, ISerializationContext> collectAssignments(Multimap<IConstraint, ISerializationContext> constraints, EStructuralFeature feature) {
    Multimap<AbstractElement, ISerializationContext> result = ArrayListMultimap.create();
    for (Entry<IConstraint, Collection<ISerializationContext>> e : constraints.asMap().entrySet()) {
        IConstraint constraint = e.getKey();
        Collection<ISerializationContext> contexts = e.getValue();
        IFeatureInfo featureInfo = constraint.getFeatures()[constraint.getType().getFeatureID(feature)];
        List<IConstraintElement> assignments = featureInfo.getAssignments();
        for (IConstraintElement assignment : assignments) {
            result.putAll(assignment.getGrammarElement(), contexts);
        }
    }
    return result;
}
Also used : IConstraintElement(org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraintElement) AbstractElement(org.eclipse.xtext.AbstractElement) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) IConstraint(org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraint) Collection(java.util.Collection) IFeatureInfo(org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IFeatureInfo)

Example 74 with AbstractElement

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

the class ContextFinder method findByContents.

@Override
public Set<ISerializationContext> findByContents(EObject semanticObject, Iterable<ISerializationContext> contextCandidates) {
    if (semanticObject == null)
        throw new NullPointerException();
    initConstraints();
    Multimap<IConstraint, ISerializationContext> constraints;
    if (contextCandidates != null)
        constraints = getConstraints(semanticObject, contextCandidates);
    else
        constraints = getConstraints(semanticObject);
    if (constraints.size() < 2)
        return Sets.newLinkedHashSet(constraints.values());
    for (IConstraint cand : Lists.newArrayList(constraints.keySet())) if (!isValidValueQuantity(cand, semanticObject))
        constraints.removeAll(cand);
    if (constraints.size() < 2)
        return Sets.newLinkedHashSet(constraints.values());
    LinkedHashSet<ISerializationContext> result = Sets.newLinkedHashSet(constraints.values());
    for (EStructuralFeature feat : semanticObject.eClass().getEAllStructuralFeatures()) {
        if (transientValueUtil.isTransient(semanticObject, feat) != ValueTransient.NO)
            continue;
        if (feat.isMany() && ((List<?>) semanticObject.eGet(feat)).isEmpty())
            continue;
        Multimap<AbstractElement, ISerializationContext> assignments = collectAssignments(constraints, feat);
        Set<AbstractElement> assignedElements = findAssignedElements(semanticObject, feat, assignments);
        Set<ISerializationContext> keep = Sets.newHashSet();
        for (AbstractElement ele : assignedElements) keep.addAll(assignments.get(ele));
        result.retainAll(keep);
    }
    return result;
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) IConstraint(org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraint)

Example 75 with AbstractElement

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

the class NodeModelSemanticSequencer method createSequence.

@Override
public void createSequence(ISerializationContext context, EObject semanticObject) {
    SemanticNodeIterator ni = new SemanticNodeIterator(semanticObject);
    while (ni.hasNext()) {
        Triple<INode, AbstractElement, EObject> node = ni.next();
        if (node.getSecond() instanceof RuleCall) {
            RuleCall rc = (RuleCall) node.getSecond();
            TypeRef ruleType = rc.getRule().getType();
            if (ruleType == null || ruleType.getClassifier() instanceof EClass)
                acceptSemantic(semanticObject, rc, node.getThird(), node.getFirst());
            else if (GrammarUtil.containingCrossReference(node.getSecond()) != null) {
                EStructuralFeature feature = FeatureFinderUtil.getFeature(node.getSecond(), semanticObject.eClass());
                acceptSemantic(semanticObject, rc, semanticObject.eGet(feature), node.getFirst());
            } else {
                String strVal = NodeModelUtils.getTokenText(node.getFirst());
                Object val = valueConverter.toValue(strVal, ruleNames.getQualifiedName(rc.getRule()), node.getFirst());
                acceptSemantic(semanticObject, rc, val, node.getFirst());
            }
        } else if (node.getSecond() instanceof Keyword)
            acceptSemantic(semanticObject, node.getSecond(), node.getFirst().getText(), node.getFirst());
        else if (node.getSecond() instanceof Action) {
            acceptSemantic(semanticObject, node.getSecond(), semanticObject, node.getFirst());
        }
    }
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) Action(org.eclipse.xtext.Action) AbstractElement(org.eclipse.xtext.AbstractElement) Keyword(org.eclipse.xtext.Keyword) TypeRef(org.eclipse.xtext.TypeRef) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) RuleCall(org.eclipse.xtext.RuleCall) EClass(org.eclipse.emf.ecore.EClass) EObject(org.eclipse.emf.ecore.EObject) EObject(org.eclipse.emf.ecore.EObject)

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