Search in sources :

Example 6 with ISerializationContext

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

Example 7 with ISerializationContext

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

the class GrammarConstraintProvider method collectAllParameterValues.

protected Multimap<Parameter, Boolean> collectAllParameterValues(IConstraint constraint) {
    Set<Parameter> all = Sets.newLinkedHashSet();
    List<ISerializationContext> contexts = constraint.getContexts();
    for (ISerializationContext context : contexts) {
        all.addAll(((SerializationContext) context).getDeclaredParameters());
    }
    Multimap<Parameter, Boolean> values = LinkedHashMultimap.create();
    for (ISerializationContext ctx : contexts) {
        Set<Parameter> params = ctx.getEnabledBooleanParameters();
        for (Parameter param : all) values.put(param, params != null && params.contains(param));
    }
    return values;
}
Also used : ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) Parameter(org.eclipse.xtext.Parameter)

Example 8 with ISerializationContext

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

the class GrammarPDAProvider method getGrammarPDAs.

@Override
public SerializationContextMap<Pda<ISerState, RuleCall>> getGrammarPDAs(Grammar grammar) {
    RuleNames names = RuleNames.getRuleNames(grammar, true);
    RuleFilter filter = new RuleFilter();
    filter.setDiscardTerminalRules(true);
    filter.setDiscardUnreachableRules(false);
    filter.setDiscardRuleTypeRef(false);
    Grammar flattened = new FlattenedGrammarAccess(names, filter).getFlattenedGrammar();
    Builder<Pda<ISerState, RuleCall>> result = SerializationContextMap.<Pda<ISerState, RuleCall>>builder();
    for (ParserRule rule : GrammarUtil.allParserRules(flattened)) {
        RuleWithParameterValues withParams = RuleWithParameterValues.findInEmfObject(rule);
        AbstractRule original = withParams.getOriginal();
        if (original instanceof ParserRule && isValidRule((ParserRule) original)) {
            ISerializationContext context = createContext((ParserRule) original, withParams.getParamValues());
            try {
                Pda<ISerState, RuleCall> pda = createPDA(grammar, rule);
                result.put(context, pda);
            } catch (Exception e) {
                LOG.error("Error creating PDA for context '" + context + "': " + e.getMessage(), e);
            }
        }
    }
    return result.create();
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) RuleNames(org.eclipse.xtext.xtext.RuleNames) RuleWithParameterValues(org.eclipse.xtext.xtext.RuleWithParameterValues) Grammar(org.eclipse.xtext.Grammar) Pda(org.eclipse.xtext.util.formallang.Pda) FlattenedGrammarAccess(org.eclipse.xtext.xtext.FlattenedGrammarAccess) RuleCall(org.eclipse.xtext.RuleCall) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) RuleFilter(org.eclipse.xtext.xtext.RuleFilter) AbstractRule(org.eclipse.xtext.AbstractRule)

Example 9 with ISerializationContext

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

the class SemanticSequencerNfaProvider method getSemanticSequencerNFAs.

@Override
public SerializationContextMap<Nfa<ISemState>> getSemanticSequencerNFAs(Grammar grammar) {
    SerializationContextMap<Nfa<ISemState>> cached = cache.get(grammar);
    if (cached != null)
        return cached;
    SerializationContextMap.Builder<Nfa<ISemState>> builder = SerializationContextMap.builder();
    SerializationContextMap<ISynAbsorberState> PDAs = pdaProvider.getSyntacticSequencerPDAs(grammar);
    for (SerializationContextMap.Entry<ISynAbsorberState> e : PDAs.values()) {
        ISynAbsorberState synState = e.getValue();
        for (EClass type : e.getTypes()) {
            List<ISerializationContext> contexts = e.getContexts(type);
            try {
                SemNfa nfa = createNfa(grammar, synState, type);
                builder.put(contexts, nfa);
            } catch (Exception x) {
                LOG.error("Error during static analysis of context '" + contexts + "': " + x.getMessage(), x);
            }
        }
    }
    SerializationContextMap<Nfa<ISemState>> result = builder.create();
    cache.put(grammar, result);
    return result;
}
Also used : ISynAbsorberState(org.eclipse.xtext.serializer.analysis.ISyntacticSequencerPDAProvider.ISynAbsorberState) EClass(org.eclipse.emf.ecore.EClass) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) Nfa(org.eclipse.xtext.util.formallang.Nfa)

Example 10 with ISerializationContext

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

the class SerializationContext method forChild.

public static ISerializationContext forChild(ISerializationContext container, Action assignedAction, EObject sem) {
    EClass type = sem == null ? null : sem.eClass();
    // RuleContext ruleContext = new RuleContext(null, container.getParserRule());
    ISerializationContext context = new TypeContext(new ActionContext(/*ruleContext*/
    null, assignedAction), type);
    if (container != null) {
        Set<Parameter> params = container.getEnabledBooleanParameters();
        if (!params.isEmpty())
            context = new ParameterValueContext(context, params);
    }
    return context;
}
Also used : EClass(org.eclipse.emf.ecore.EClass) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) 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