Search in sources :

Example 21 with ISerializationContext

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

the class PartialSerializer method updateSingleValue.

protected SerializationStrategy updateSingleValue(EObject object, EStructuralFeature feature, IAstRegion region) {
    Preconditions.checkArgument(!feature.isMany());
    Object value = object.eGet(feature);
    EObject grammarElement = region.getGrammarElement();
    if (feature instanceof EAttribute) {
        if (grammarElement instanceof RuleCall) {
            RuleCall rc = (RuleCall) grammarElement;
            String newValue = valueSerializer.serializeAssignedValue(object, rc, value, null, errorAcceptor);
            if (newValue != null) {
                return new ReplaceRegionStrategy((ISemanticRegion) region, newValue);
            }
        }
        return null;
    } else if (feature instanceof EReference) {
        if (((EReference) feature).isContainment()) {
            IEObjectRegion reg = (IEObjectRegion) region;
            EObject newEObject = (EObject) object.eGet(feature);
            ISerializationContext newContext = getSerializationContext(newEObject);
            ISerializationContext oldContext = getSerializationContext(reg);
            if (!oldContext.equals(newContext)) {
                return null;
            }
            return new SerializeRecursiveStrategy(reg, newEObject, newContext);
        } else {
            CrossReference cr = GrammarUtil.containingCrossReference(grammarElement);
            if (cr != null) {
                EObject target = (EObject) value;
                String newValue = crossRefSerializer.serializeCrossRef(object, cr, target, null, errorAcceptor);
                if (newValue != null) {
                    return new ReplaceRegionStrategy((ISemanticRegion) region, newValue);
                }
            }
        }
        return null;
    }
    return null;
}
Also used : EAttribute(org.eclipse.emf.ecore.EAttribute) IEObjectRegion(org.eclipse.xtext.formatting2.regionaccess.IEObjectRegion) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) EObject(org.eclipse.emf.ecore.EObject) CrossReference(org.eclipse.xtext.CrossReference) EObject(org.eclipse.emf.ecore.EObject) ISemanticRegion(org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion) RuleCall(org.eclipse.xtext.RuleCall) EReference(org.eclipse.emf.ecore.EReference)

Example 22 with ISerializationContext

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

the class PartialSerializer method trySerializeMultiValue.

protected List<SerializationStrategy> trySerializeMultiValue(EObject owner, FeatureChange change, IEObjectRegion ownerRegion, IConstraint constraint) {
    EStructuralFeature feature = change.getFeature();
    List<SerializationStrategy> result = Lists.newArrayList();
    IFeatureInfo featureInfo = constraint.getFeatures()[owner.eClass().getFeatureID(feature)];
    List<IConstraintElement> assignments = featureInfo.getAssignments();
    if (assignments.size() != 1) {
        return null;
    }
    IConstraintElement assignment = assignments.get(0);
    if (!assignment.isMany()) {
        return null;
    }
    List<IAstRegion> originals = findRegions(ownerRegion, change);
    EList<ListChange> listChanges = change.getListChanges();
    if (listChanges.isEmpty() && originals.isEmpty()) {
        ISerializationContext ctx = getSerializationContext(owner);
        AbstractElement ins = assignment.getGrammarElement();
        IHiddenRegion insertAt = insertionPointFinder.findInsertionPoint(ctx, ownerRegion, ins);
        if (insertAt == null) {
            return null;
        }
        for (Object value : (List<?>) owner.eGet(feature)) {
            EObject obj = (EObject) value;
            ISerializationContext context = getSerializationContext(obj);
            result.add(new SerializeRecursiveStrategy(insertAt, obj, context));
        }
        return result;
    }
    // ListTransient listTransient = transientValues.isListTransient(owner, feature);
    List<Object> modifying = Lists.newArrayList(((List<?>) owner.eGet(feature)));
    for (ListChange lc : listChanges) {
        ChangeKind kind = lc.getKind();
        if (kind == ADD_LITERAL) {
            IAstRegion region = originals.get(lc.getIndex());
            result.add(new DeleteRegionStrategy(region));
        } else if (kind == MOVE_LITERAL || kind == REMOVE_LITERAL) {
            if (originals.isEmpty()) {
                return null;
            }
            int index = lc.getIndex();
            IHiddenRegion insertAt;
            if (index >= originals.size()) {
                insertAt = ((ISequentialRegion) originals.get(originals.size() - 1)).getNextHiddenRegion();
            } else {
                insertAt = ((ISequentialRegion) originals.get(index)).getPreviousHiddenRegion();
            }
            EObject value = (EObject) modifying.get(index);
            modifying.remove(index);
            if (kind == REMOVE_LITERAL) {
                ISerializationContext context = getSerializationContext(value);
                result.add(new SerializeRecursiveStrategy(insertAt, value, context));
            } else if (kind == ChangeKind.MOVE_LITERAL) {
                int moveToIndex = lc.getMoveToIndex();
                IAstRegion source = originals.get(moveToIndex);
                result.add(new DeleteRegionStrategy(source));
                result.add(new InsertRegionStrategy(insertAt, source));
            }
        }
    }
    return result;
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) IConstraint(org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraint) IFeatureInfo(org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IFeatureInfo) IConstraintElement(org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraintElement) ISequentialRegion(org.eclipse.xtext.formatting2.regionaccess.ISequentialRegion) IAstRegion(org.eclipse.xtext.formatting2.regionaccess.IAstRegion) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) EObject(org.eclipse.emf.ecore.EObject) IHiddenRegion(org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion) EObject(org.eclipse.emf.ecore.EObject) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) LinkedList(java.util.LinkedList) EList(org.eclipse.emf.common.util.EList) ListChange(org.eclipse.emf.ecore.change.ListChange) ChangeKind(org.eclipse.emf.ecore.change.ChangeKind)

Example 23 with ISerializationContext

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

the class GrammarConstraintProvider method getConstraints.

@Override
public SerializationContextMap<IConstraint> getConstraints(Grammar grammar) {
    SerializationContextMap<IConstraint> cached = cache.get(grammar);
    if (cached != null)
        return cached;
    SerializationContextMap.Builder<IConstraint> builder = SerializationContextMap.builder();
    GrammarElementDeclarationOrder.get(grammar);
    SerializationContextMap<Nfa<ISemState>> nfas = nfaProvider.getSemanticSequencerNFAs(grammar);
    for (Entry<Nfa<ISemState>> e : nfas.values()) {
        Nfa<ISemState> nfa = e.getValue();
        for (EClass type : e.getTypes()) {
            Constraint constraint = new Constraint(grammar, type, nfa);
            List<ISerializationContext> contexts = e.getContexts(type);
            constraint.contexts.addAll(contexts);
            builder.put(contexts, constraint);
        }
    }
    SerializationContextMap<IConstraint> result = builder.create();
    SerializationContextMap<Pda<ISerState, RuleCall>> typePDAs = typeProvider.getContextTypePDAs(grammar);
    for (Entry<IConstraint> e : result.values()) {
        Constraint constraint = (Constraint) e.getValue();
        constraint.setName(findBestConstraintName(grammar, typePDAs, constraint));
    }
    cache.put(grammar, result);
    return result;
}
Also used : Nfa(org.eclipse.xtext.util.formallang.Nfa) Pda(org.eclipse.xtext.util.formallang.Pda) EClass(org.eclipse.emf.ecore.EClass) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) ISemState(org.eclipse.xtext.serializer.analysis.ISemanticSequencerNfaProvider.ISemState)

Example 24 with ISerializationContext

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

the class GrammarConstraintProvider method findBestConstraintName.

protected String findBestConstraintName(Grammar grammar, SerializationContextMap<Pda<ISerState, RuleCall>> typePDAs, IConstraint constraint) {
    Set<ParserRule> relevantRules = Sets.newLinkedHashSet();
    Set<Action> relevantActions = Sets.newLinkedHashSet();
    Set<ParserRule> contextRules = Sets.newLinkedHashSet();
    for (ISerializationContext ctx : constraint.getContexts()) {
        ParserRule rule = ctx.getParserRule();
        if (rule != null) {
            contextRules.add(rule);
        }
    }
    // 1. find relevant rules based on assignments
    for (ISemState s : nfaUtil.collect(constraint.getNfa())) {
        AbstractElement element = s.getAssignedGrammarElement();
        if (element != null)
            relevantRules.add(GrammarUtil.containingParserRule(element));
    }
    // 2. find relevant rules based on unassigned actions
    for (ISerializationContext ctx : constraint.getContexts()) {
        for (ISerState s : nfaUtil.collect(typePDAs.get(ctx))) {
            AbstractElement element = s.getGrammarElement();
            if (element instanceof Action && ((Action) element).getFeature() == null)
                relevantRules.add(GrammarUtil.containingParserRule(element));
        }
    }
    if (relevantRules.isEmpty()) {
        Set<ParserRule> allRules = Sets.newLinkedHashSet(contextRules);
        for (ISerializationContext ctx : constraint.getContexts()) {
            Action action = ctx.getAssignedAction();
            if (action != null)
                allRules.add(GrammarUtil.containingParserRule(action));
        }
        // 3. use all rules, because the constraint returns null.
        relevantRules.addAll(allRules);
    }
    for (ISerializationContext ctx : constraint.getContexts()) {
        Action action = ctx.getAssignedAction();
        if (action != null) {
            ParserRule rule = GrammarUtil.containingParserRule(action);
            if (!contextRules.contains(rule) && relevantRules.contains(rule)) {
                relevantActions.add(action);
            }
        }
    }
    List<String> actions = Lists.newArrayList();
    List<String> rules = Lists.newArrayList();
    Multimap<Parameter, Boolean> parameterValues = collectAllParameterValues(constraint);
    for (Action a : relevantActions) actions.add(context2Name.getUniqueActionName(a));
    for (ParserRule rule : relevantRules) {
        StringBuilder segments = new StringBuilder();
        for (Parameter param : rule.getParameters()) {
            Collection<Boolean> values = parameterValues.get(param);
            if (values.size() == 1) {
                segments.append(param.getName() + "$" + values.iterator().next() + "$");
            }
        }
        if (segments.length() == 0) {
            rules.add(rule.getName());
        } else {
            rules.add(rule.getName() + "$" + segments);
        }
    }
    Collections.sort(rules);
    String result = Joiner.on("_").join(rules);
    if (!actions.isEmpty()) {
        Collections.sort(actions);
        result += "_" + Joiner.on('_').join(actions);
    }
    return result;
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) Action(org.eclipse.xtext.Action) AbstractElement(org.eclipse.xtext.AbstractElement) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) Parameter(org.eclipse.xtext.Parameter) ISemState(org.eclipse.xtext.serializer.analysis.ISemanticSequencerNfaProvider.ISemState)

Example 25 with ISerializationContext

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

the class SerializationContext method compareTo.

@Override
public int compareTo(ISerializationContext o) {
    EObject o1 = getActionOrRule();
    EObject o2 = ((SerializationContext) o).getActionOrRule();
    if (o1 != o2) {
        GrammarElementDeclarationOrder order = GrammarElementDeclarationOrder.get(GrammarUtil.getGrammar(o1));
        int compare = order.compare(o1, o2);
        if (compare != 0)
            return compare;
    }
    EClass t1 = getType();
    EClass t2 = o.getType();
    if (t1 != t2) {
        if (t1 != null && t2 != null) {
            int compare = t1.getName().compareTo(t2.getName());
            if (compare != 0)
                return compare;
        }
        if (t1 != null)
            return -1;
        if (t2 != null)
            return 1;
    }
    List<Parameter> params = getDeclaredParameters();
    if (params.equals(((SerializationContext) o).getDeclaredParameters())) {
        Set<Parameter> v1 = getEnabledBooleanParameters();
        Set<Parameter> v2 = o.getEnabledBooleanParameters();
        if (v1 == null || v2 == null) {
            return v1 != null ? -1 : 1;
        } else {
            for (Parameter param : params) {
                boolean b1 = v1.contains(param);
                boolean b2 = v2.contains(param);
                if (b1 != b2) {
                    return b1 ? -1 : 1;
                }
            }
        }
    }
    ISerializationContext p1 = getParent();
    ISerializationContext p2 = ((SerializationContext) o).getParent();
    if (p1 != p2) {
        if (p1 != null && p2 != null)
            return p1.compareTo(p2);
        if (p1 != null)
            return -1;
        if (p2 != null)
            return 1;
    }
    return 0;
}
Also used : ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) EClass(org.eclipse.emf.ecore.EClass) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) EObject(org.eclipse.emf.ecore.EObject) Parameter(org.eclipse.xtext.Parameter)

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