Search in sources :

Example 1 with IConstraint

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);
    }
}
Also used : IConstraint(org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraint) EObjectChange(org.eclipse.xtext.ide.serializer.impl.ChangeTreeProvider.EObjectChange)

Example 2 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, 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;
}
Also used : EClass(org.eclipse.emf.ecore.EClass) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) IConstraint(org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraint)

Example 3 with IConstraint

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;
}
Also used : ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) IConstraint(org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraint) Collection(java.util.Collection) EReference(org.eclipse.emf.ecore.EReference) IConstraint(org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraint)

Example 4 with IConstraint

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();
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement) IConstraint(org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraint) SequenceFeeder(org.eclipse.xtext.serializer.acceptor.SequenceFeeder) INodesForEObjectProvider(org.eclipse.xtext.serializer.sequencer.ISemanticNodeProvider.INodesForEObjectProvider) NfaUtil(org.eclipse.xtext.util.formallang.NfaUtil) InternalEList(org.eclipse.emf.ecore.util.InternalEList) List(java.util.List) ISemState(org.eclipse.xtext.serializer.analysis.ISemanticSequencerNfaProvider.ISemState)

Example 5 with IConstraint

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);
}
Also used : SerializationContextMap(org.eclipse.xtext.serializer.analysis.SerializationContextMap) IGrammarConstraintProvider(org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider) IConstraint(org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraint) Grammar(org.eclipse.xtext.Grammar)

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