Search in sources :

Example 11 with IConstraint

use of org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraint in project xtext-core by eclipse.

the class ContextFinder method getConstraints.

protected Multimap<IConstraint, ISerializationContext> getConstraints(EObject sem) {
    EClass type = sem == null ? null : sem.eClass();
    Multimap<IConstraint, ISerializationContext> result = ArrayListMultimap.create();
    for (SerializationContextMap.Entry<IConstraint> e : constraints.values()) {
        IConstraint constraint = e.getValue();
        for (ISerializationContext context : e.getContexts()) {
            if (constraint.getType() == type) {
                result.put(constraint, context);
            }
        }
    }
    return result;
}
Also used : EClass(org.eclipse.emf.ecore.EClass) SerializationContextMap(org.eclipse.xtext.serializer.analysis.SerializationContextMap) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) IConstraint(org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraint)

Example 12 with IConstraint

use of org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraint 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 13 with IConstraint

use of org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraint in project xtext-core by eclipse.

the class SequencerDiagnosticProvider method createBacktrackingFailedDiagnostic.

@Override
public ISerializationDiagnostic createBacktrackingFailedDiagnostic(SerializableObject sem, ISerializationContext ctx, IConstraint constraint) {
    EClass type = constraint.getType();
    List<String> hints = Lists.newArrayList();
    for (IFeatureInfo feature : constraint.getFeatures()) {
        int featureID = type.getFeatureID(feature.getFeature());
        int count = sem.getValueCount(featureID);
        String name = feature.getFeature().getEContainingClass().getName() + "." + feature.getFeature().getName();
        if (!sem.isOptional(featureID)) {
            int upperBound = feature.getUpperBound();
            if (count > upperBound) {
                if (feature.getFeature().isMany()) {
                    hints.add(name + " violates the upper bound: It holds " + count + " values, but only " + upperBound + " are allowed.");
                } else {
                    hints.add(name + " is not allowed to have a value, but it does.");
                }
            }
        }
        int lowerBound = feature.getLowerBound();
        if (count < lowerBound) {
            if (feature.getFeature().isMany()) {
                hints.add(name + " violates the lower bound: It holds " + count + " values, but at least " + lowerBound + " are required.");
            } else {
                hints.add(name + " is required to have a value, but it does not.");
            }
        }
    }
    if (!hints.isEmpty()) {
        StringBuilder msg = new StringBuilder();
        msg.append("Could not serialize " + type.getName() + ":\n");
        msg.append(Joiner.on("\n").join(hints));
        return new SerializationDiagnostic(BACKTRACKING_FAILED, sem.getEObject(), ctx, grammarAccess.getGrammar(), msg.toString());
    } else {
        StringBuilder msg = new StringBuilder();
        msg.append("Could not serialize " + type.getName() + " via backtracking.\n");
        msg.append("Constraint: " + constraint + "\n");
        msg.append(sem.getValuesString());
        return new SerializationDiagnostic(BACKTRACKING_FAILED, sem.getEObject(), ctx, grammarAccess.getGrammar(), msg.toString());
    }
}
Also used : EClass(org.eclipse.emf.ecore.EClass) IConstraint(org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraint) IFeatureInfo(org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IFeatureInfo)

Example 14 with IConstraint

use of org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraint in project xtext-core by eclipse.

the class SequencerDiagnosticProvider method createBacktrackingFailedDiagnostic.

@Override
@Deprecated
public ISerializationDiagnostic createBacktrackingFailedDiagnostic(SerializableObject semanticObject, EObject context, Nfa<ISemState> nfa) {
    ISerializationContext ctx = SerializationContext.fromEObject(context, semanticObject.getEObject());
    SerializationContextMap<IConstraint> constraints = grammarConstraints.getConstraints(grammarAccess.getGrammar());
    return createBacktrackingFailedDiagnostic(semanticObject, ctx, constraints.get(ctx));
}
Also used : ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) IConstraint(org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraint)

Example 15 with IConstraint

use of org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraint in project xtext-core by eclipse.

the class PartialSerializer method trySerializeEObject.

protected List<SerializationStrategy> trySerializeEObject(EObjectChange change, ITextRegionDiffBuilder result, SerializationContextMap<IConstraint> constraints) {
    List<SerializationStrategy> strategies = Lists.newArrayList();
    EObject obj = change.getEObject();
    IEObjectRegion originalEObjectRegion = result.getOriginalTextRegionAccess().regionForEObject(obj);
    ISerializationContext modified = getSerializationContext(obj);
    if (originalEObjectRegion == null) {
        return null;
    }
    ISerializationContext original = getSerializationContext(originalEObjectRegion);
    if (!original.equals(modified)) {
        strategies.add(new SerializeRecursiveStrategy(originalEObjectRegion, obj, modified));
        return strategies;
    }
    IConstraint constraint = constraints.get(modified);
    List<SerializationStrategy> features = trySerializeIndividualFeatures(change, originalEObjectRegion, modified, constraint);
    if (features == null) {
        strategies.add(new SerializeRecursiveStrategy(originalEObjectRegion, obj, modified));
        return strategies;
    }
    strategies.addAll(features);
    for (EObjectChange child : change.getChildren()) {
        List<SerializationStrategy> c = trySerializeEObject(child, result, constraints);
        if (c == null) {
            return Collections.singletonList(new SerializeRecursiveStrategy(originalEObjectRegion, obj, modified));
        } else {
            strategies.addAll(c);
        }
    }
    return strategies;
}
Also used : IEObjectRegion(org.eclipse.xtext.formatting2.regionaccess.IEObjectRegion) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) EObject(org.eclipse.emf.ecore.EObject) IConstraint(org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraint) EObjectChange(org.eclipse.xtext.ide.serializer.impl.ChangeTreeProvider.EObjectChange)

Aggregations

IConstraint (org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraint)16 ISerializationContext (org.eclipse.xtext.serializer.ISerializationContext)9 List (java.util.List)4 AbstractElement (org.eclipse.xtext.AbstractElement)4 IFeatureInfo (org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IFeatureInfo)4 EClass (org.eclipse.emf.ecore.EClass)3 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)3 Grammar (org.eclipse.xtext.Grammar)3 IGrammarConstraintProvider (org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider)3 SerializationContextMap (org.eclipse.xtext.serializer.analysis.SerializationContextMap)3 Collection (java.util.Collection)2 EObject (org.eclipse.emf.ecore.EObject)2 EObjectChange (org.eclipse.xtext.ide.serializer.impl.ChangeTreeProvider.EObjectChange)2 IConstraintElement (org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraintElement)2 Joiner (com.google.common.base.Joiner)1 ImmutableList (com.google.common.collect.ImmutableList)1 Lists (com.google.common.collect.Lists)1 Inject (com.google.inject.Inject)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1