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;
}
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;
}
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());
}
}
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));
}
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;
}
Aggregations