Search in sources :

Example 1 with TypeContext

use of org.eclipse.xtext.serializer.analysis.SerializationContext.TypeContext in project xtext-core by eclipse.

the class ContextTypePDAProvider method getContextTypePDAs.

@Override
public SerializationContextMap<Pda<ISerState, RuleCall>> getContextTypePDAs(Grammar grammar) {
    SerializationContextMap<Pda<ISerState, RuleCall>> cached = cache.get(grammar);
    if (cached != null)
        return cached;
    SerializationContextMap.Builder<Pda<ISerState, RuleCall>> builder = SerializationContextMap.builder();
    SerializationContextMap<Pda<ISerState, RuleCall>> contextPDAs = pdaProvider.getContextPDAs(grammar);
    for (SerializationContextMap.Entry<Pda<ISerState, RuleCall>> e : contextPDAs.values()) {
        List<ISerializationContext> parents = e.getContexts();
        Pda<ISerState, RuleCall> contextPDA = e.getValue();
        try {
            Map<ISerState, Integer> distances = nfaUtil.distanceToFinalStateMap(contextPDA);
            Set<EClass> types = collectTypes(contextPDA, distances);
            if (types.size() == 1) {
                for (ISerializationContext parent : parents) {
                    TypeContext ctx = new TypeContext(parent, types.iterator().next());
                    builder.put(ctx, contextPDA);
                }
            } else {
                for (EClass type : types) {
                    Pda<ISerState, RuleCall> filtered = filterByType(contextPDA, type, distances);
                    for (ISerializationContext parent : parents) {
                        TypeContext typeContext = new TypeContext(parent, type);
                        builder.put(typeContext, filtered);
                    }
                }
            }
        } catch (Exception x) {
            LOG.error("Error extracting PDAs for types for context '" + parents + "': " + x.getMessage(), x);
        }
    }
    SerializationContextMap<Pda<ISerState, RuleCall>> result = builder.create();
    cache.put(grammar, result);
    return result;
}
Also used : Pda(org.eclipse.xtext.util.formallang.Pda) TypeContext(org.eclipse.xtext.serializer.analysis.SerializationContext.TypeContext) RuleCall(org.eclipse.xtext.RuleCall) EClass(org.eclipse.emf.ecore.EClass) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext)

Aggregations

EClass (org.eclipse.emf.ecore.EClass)1 RuleCall (org.eclipse.xtext.RuleCall)1 ISerializationContext (org.eclipse.xtext.serializer.ISerializationContext)1 TypeContext (org.eclipse.xtext.serializer.analysis.SerializationContext.TypeContext)1 Pda (org.eclipse.xtext.util.formallang.Pda)1