Search in sources :

Example 16 with ISerializationContext

use of org.eclipse.xtext.serializer.ISerializationContext in project xtext-core by eclipse.

the class AssignmentFinder method findValidAssignmentsForContainmentRef.

protected Set<AbstractElement> findValidAssignmentsForContainmentRef(EObject semanticObj, Multimap<AbstractElement, ISerializationContext> assignments, EObject value) {
    Multimap<ISerializationContext, AbstractElement> children = ArrayListMultimap.create();
    for (Entry<AbstractElement, Collection<ISerializationContext>> e : assignments.asMap().entrySet()) {
        AbstractElement ele = e.getKey();
        if (ele instanceof RuleCall) {
            EClassifier classifier = ((RuleCall) ele).getRule().getType().getClassifier();
            if (!classifier.isInstance(value))
                continue;
        }
        for (ISerializationContext container : e.getValue()) {
            ISerializationContext child = SerializationContext.forChild(container, ele, value);
            children.put(child, ele);
        }
    }
    if (children.size() < 2)
        return Sets.newHashSet(children.values());
    Set<ISerializationContext> found = contextFinder.findByContents(value, children.keySet());
    Set<AbstractElement> result = Sets.newLinkedHashSet();
    for (ISerializationContext ctx : children.keySet()) if (found.contains(ctx))
        result.addAll(children.get(ctx));
    return result;
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) Collection(java.util.Collection) EClassifier(org.eclipse.emf.ecore.EClassifier) RuleCall(org.eclipse.xtext.RuleCall)

Example 17 with ISerializationContext

use of org.eclipse.xtext.serializer.ISerializationContext 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 18 with ISerializationContext

use of org.eclipse.xtext.serializer.ISerializationContext 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 19 with ISerializationContext

use of org.eclipse.xtext.serializer.ISerializationContext in project xtext-core by eclipse.

the class NamedSerializationContextProvider method getSignificantGrammarElement.

public String getSignificantGrammarElement(final Iterable<ISerializationContext> contexts) {
    ParserRule rule = null;
    int index = Integer.MAX_VALUE;
    for (final ISerializationContext ctx : contexts) {
        {
            ParserRule pr = ctx.getParserRule();
            if ((pr == null)) {
                final Action action = ctx.getAssignedAction();
                if ((action != null)) {
                    pr = GrammarUtil.containingParserRule(action);
                }
            }
            if ((pr != null)) {
                final Integer i = this.rules.get(pr);
                if (((i).intValue() < index)) {
                    index = (i).intValue();
                    rule = pr;
                }
            }
        }
    }
    if ((rule != null)) {
        return rule.getName();
    }
    return "unknown";
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) Action(org.eclipse.xtext.Action) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext)

Example 20 with ISerializationContext

use of org.eclipse.xtext.serializer.ISerializationContext in project xtext-core by eclipse.

the class SerializerFragment2 method genMethodSequenceComment.

private StringConcatenationClient genMethodSequenceComment(final IGrammarConstraintProvider.IConstraint c) {
    StringConcatenationClient _client = new StringConcatenationClient() {

        @Override
        protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
            _builder.append("// This method is commented out because it has the same signature as another method in this class.");
            _builder.newLine();
            _builder.append("// This is probably a bug in Xtext\'s serializer, please report it here: ");
            _builder.newLine();
            _builder.append("// https://bugs.eclipse.org/bugs/enter_bug.cgi?product=TMF");
            _builder.newLine();
            _builder.append("//");
            _builder.newLine();
            _builder.append("// Contexts:");
            _builder.newLine();
            _builder.append("//     ");
            String _replaceAll = IterableExtensions.join(IterableExtensions.<ISerializationContext>sort(c.getContexts()), "\n").replaceAll("\\n", "\n//     ");
            _builder.append(_replaceAll);
            _builder.newLineIfNotEmpty();
            _builder.append("//");
            _builder.newLine();
            _builder.append("// Constraint:");
            _builder.newLine();
            _builder.append("//     ");
            {
                IGrammarConstraintProvider.IConstraintElement _body = c.getBody();
                boolean _tripleEquals = (_body == null);
                if (_tripleEquals) {
                    _builder.append("{");
                    String _name = c.getType().getName();
                    _builder.append(_name);
                    _builder.append("}");
                } else {
                    String _replaceAll_1 = c.getBody().toString().replaceAll("\\n", "\n//     ");
                    _builder.append(_replaceAll_1);
                }
            }
            _builder.newLineIfNotEmpty();
            _builder.append("//");
            _builder.newLine();
            _builder.append("// protected void sequence_");
            String _simpleName = c.getSimpleName();
            _builder.append(_simpleName);
            _builder.append("(");
            _builder.append(ISerializationContext.class);
            _builder.append(" context, ");
            EClass _type = c.getType();
            _builder.append(_type);
            _builder.append(" semanticObject) { }");
            _builder.newLineIfNotEmpty();
        }
    };
    return _client;
}
Also used : EClass(org.eclipse.emf.ecore.EClass) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) StringConcatenationClient(org.eclipse.xtend2.lib.StringConcatenationClient)

Aggregations

ISerializationContext (org.eclipse.xtext.serializer.ISerializationContext)39 EClass (org.eclipse.emf.ecore.EClass)12 IConstraint (org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraint)8 EObject (org.eclipse.emf.ecore.EObject)7 RuleCall (org.eclipse.xtext.RuleCall)7 Parameter (org.eclipse.xtext.Parameter)6 List (java.util.List)5 StringConcatenationClient (org.eclipse.xtend2.lib.StringConcatenationClient)5 AbstractElement (org.eclipse.xtext.AbstractElement)5 ParserRule (org.eclipse.xtext.ParserRule)5 Pda (org.eclipse.xtext.util.formallang.Pda)5 Collection (java.util.Collection)4 ISerializationDiagnostic (org.eclipse.xtext.serializer.diagnostic.ISerializationDiagnostic)4 Map (java.util.Map)3 Action (org.eclipse.xtext.Action)3 ISynAbsorberState (org.eclipse.xtext.serializer.analysis.ISyntacticSequencerPDAProvider.ISynAbsorberState)3 SerializationContextMap (org.eclipse.xtext.serializer.analysis.SerializationContextMap)3 LinkedHashMultimap (com.google.common.collect.LinkedHashMultimap)2 EReference (org.eclipse.emf.ecore.EReference)2 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)2