use of org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraint in project xtext-core by eclipse.
the class PartialSerializer method serializeChanges.
public void serializeChanges(ResourceRecording changes, ITextRegionDiffBuilder result) {
assertLanguage(changes);
SerializationContextMap<IConstraint> constraints = constraintProvider.getConstraints(grammar.getGrammar());
List<EObjectChange> rootChanges = collectRootChanges(changes.getRootEObjectRecordings());
List<SerializationStrategy> strategies = Lists.newArrayList();
for (EObjectChange change : rootChanges) {
List<SerializationStrategy> strat = trySerializeEObject(change, result, constraints);
if (strat != null) {
strategies.addAll(strat);
}
}
for (SerializationStrategy strategy : strategies) {
strategy.serialize(result);
}
}
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, Iterable<ISerializationContext> contextCandidates) {
EClass type = sem == null ? null : sem.eClass();
Multimap<IConstraint, ISerializationContext> result = ArrayListMultimap.create();
for (ISerializationContext ctx : contextCandidates) {
IConstraint constraint = constraints.get(ctx);
if (constraint != null && constraint.getType() == type) {
result.put(constraint, ctx);
}
}
return result;
}
use of org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraint in project xtext-core by eclipse.
the class ContextFinder method findContextsByContainer.
protected Iterable<ISerializationContext> findContextsByContainer(EObject sem, Iterable<ISerializationContext> contextCandidates) {
if (sem.eResource() != null && sem.eResource().getContents().contains(sem))
return Collections.singleton(getRootContext(sem));
EReference ref = sem.eContainmentFeature();
if (ref == null || (contextCandidates != null && Iterables.size(contextCandidates) < 2))
return contextCandidates;
Multimap<IConstraint, ISerializationContext> containerConstraints = getConstraints(sem.eContainer());
int refID = sem.eContainer().eClass().getFeatureID(ref);
Set<ISerializationContext> childContexts = Sets.newLinkedHashSet();
for (Entry<IConstraint, Collection<ISerializationContext>> e : Lists.newArrayList(containerConstraints.asMap().entrySet())) {
IConstraint constraint = e.getKey();
Collection<ISerializationContext> contexts = e.getValue();
if (constraint.getFeatures()[refID] == null)
containerConstraints.removeAll(constraint);
else {
childContexts.addAll(createContextsForFeatures(contexts, constraint.getFeatures()[refID], sem));
}
}
Set<ISerializationContext> result;
if (contextCandidates != null) {
result = Sets.newLinkedHashSet(contextCandidates);
result.retainAll(childContexts);
} else
result = childContexts;
if (result.size() < 2)
return result;
Iterable<ISerializationContext> filteredContexts = findContextsByContainer(sem.eContainer(), containerConstraints.values());
childContexts = Sets.newLinkedHashSet();
for (Entry<IConstraint, Collection<ISerializationContext>> e : Lists.newArrayList(containerConstraints.asMap().entrySet())) if (intersect(filteredContexts, e.getValue()))
childContexts.addAll(createContextsForFeatures(e.getValue(), e.getKey().getFeatures()[refID], sem));
result.retainAll(childContexts);
return result;
}
use of org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraint in project xtext-core by eclipse.
the class BacktrackingSemanticSequencer method createSequence.
@Override
public void createSequence(ISerializationContext context, EObject obj) {
INodesForEObjectProvider nodes = nodeProvider.getNodesForSemanticObject(obj, null);
SerializationContextMap<IConstraint> constraints = constraintProvider.getConstraints(grammar.getGrammar());
IConstraint constraint = constraints.get(context);
if (constraint == null)
throw new IllegalStateException("Invalid context: " + context);
Nfa<ISemState> nfa = constraint.getNfa();
final SerializableObject object = new SerializableObject(context, obj, nodes);
TraceItem co = new TraceItem(object);
List<TraceItem> trace = new NfaUtil().backtrack(nfa, co, new NfaUtil.BacktrackHandler<ISemState, TraceItem>() {
@Override
public TraceItem handle(ISemState state, TraceItem previous) {
if (!previous.canEnter(state))
return null;
if (state.getFeature() != null) {
return previous.cloneAndConsume(state);
} else
return previous.clone(state);
}
@Override
public boolean isSolution(TraceItem result) {
return result.isConsumed();
}
@Override
public Iterable<ISemState> sortFollowers(TraceItem result, Iterable<ISemState> followers) {
AbstractElement next = result.getNextGrammarElement();
List<ISemState> r = Lists.newArrayList(followers);
Collections.sort(r, createFollowerSorter(object, next));
return r;
}
});
SequenceFeeder feeder = feederProvider.create(context, obj, nodes, masterSequencer, sequenceAcceptor, errorAcceptor);
if (trace != null) {
for (TraceItem ti : trace) if (ti.getState() != null && ti.getState().getFeature() != null)
accept(ti, feeder);
} else if (errorAcceptor != null)
errorAcceptor.accept(diagnosticProvider.createBacktrackingFailedDiagnostic(object, context, constraint));
feeder.finish();
}
use of org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraint in project xtext-core by eclipse.
the class GrammarConstraintProviderAssignedActionTest method getParserRule.
private String getParserRule(String body) throws Exception {
Grammar grammar = (Grammar) getModel(HEADER + body);
IGrammarConstraintProvider gcp = get(IGrammarConstraintProvider.class);
// try {
// new ActionFilter2Dot().draw(grammar, getName() + ".pdf", "-T pdf");
// } catch (IOException e) {
// if (log.isDebugEnabled())
// log.debug(e.getMessage(), e);
// }
SerializationContextMap<IConstraint> constraints = gcp.getConstraints(grammar);
List<String> result = Lists.newArrayList();
for (SerializationContextMap.Entry<IConstraint> r : constraints.sortedCopy().values()) {
result.add(Joiner.on(", ").join(r.getContexts()) + ":");
result.add(" " + r.getValue());
}
return Joiner.on("\n").join(result);
}
Aggregations