Search in sources :

Example 1 with ActionContext

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

the class ContextPDAProvider method getContextPDAs.

@Override
public SerializationContextMap<Pda<ISerState, RuleCall>> getContextPDAs(Grammar grammar) {
    Builder<Pda<ISerState, RuleCall>> result = SerializationContextMap.<Pda<ISerState, RuleCall>>builder();
    SerializationContextMap<Pda<ISerState, RuleCall>> grammarPDAs = grammarPdaProvider.getGrammarPDAs(grammar);
    Multimap<Action, SerializerPDA> actionPdas = ArrayListMultimap.create();
    Multimap<Action, ISerializationContext> actionContexts = LinkedHashMultimap.create();
    Map<ParserRule, Integer> indexedRules = indexRules(grammar);
    for (SerializationContextMap.Entry<Pda<ISerState, RuleCall>> e : grammarPDAs.values()) {
        List<ISerializationContext> contexts = e.getContexts();
        Pda<ISerState, RuleCall> pda = e.getValue();
        List<ISerState> actions = Lists.newArrayList();
        for (ISerState state : nfaUtil.collect(pda)) {
            if (GrammarUtil.isAssignedAction(state.getGrammarElement())) {
                actions.add(state);
            }
        }
        if (actions.isEmpty()) {
            Pda<ISerState, RuleCall> filtered = filterUnneededUnassignedRuleCalls(pda, indexedRules);
            result.put(contexts, filtered);
        } else {
            try {
                SerializerPDA rulePda = extract(pda.getStop());
                Pda<ISerState, RuleCall> filtered = filterUnneededUnassignedRuleCalls(rulePda, indexedRules);
                result.put(contexts, filtered);
                for (ISerState state : actions) {
                    Action action = (Action) state.getGrammarElement();
                    SerializerPDA actionPda = extract(state);
                    actionPdas.put(action, actionPda);
                    actionContexts.putAll(action, contexts);
                }
            } catch (Exception x) {
                LOG.error("Error extracting PDA for action in context '" + contexts + "': " + x.getMessage(), x);
            }
        }
    }
    for (Map.Entry<Action, Collection<SerializerPDA>> action : actionPdas.asMap().entrySet()) {
        SerializerPDA merged = merge(new ActionContext(null, action.getKey()), action.getValue());
        Set<Set<Parameter>> parameterPermutations = Sets.newLinkedHashSet();
        for (ISerializationContext container : actionContexts.get(action.getKey())) {
            parameterPermutations.add(container.getEnabledBooleanParameters());
        }
        // for (IContext container : actionContexts.get(action.getKey())) {
        for (Set<Parameter> parameters : parameterPermutations) {
            ISerializationContext context = new ActionContext(/* container */
            null, action.getKey());
            if (!parameters.isEmpty())
                context = new SerializationContext.ParameterValueContext(context, parameters);
            Pda<ISerState, RuleCall> filtered = filterUnneededUnassignedRuleCalls(merged, indexedRules);
            result.put(context, filtered);
        }
    // }
    }
    return result.create();
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) Action(org.eclipse.xtext.Action) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) Pda(org.eclipse.xtext.util.formallang.Pda) RuleCall(org.eclipse.xtext.RuleCall) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) ActionContext(org.eclipse.xtext.serializer.analysis.SerializationContext.ActionContext) Collection(java.util.Collection) Parameter(org.eclipse.xtext.Parameter) HashMap(java.util.HashMap) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap)

Aggregations

Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 IdentityHashMap (java.util.IdentityHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 Action (org.eclipse.xtext.Action)1 Parameter (org.eclipse.xtext.Parameter)1 ParserRule (org.eclipse.xtext.ParserRule)1 RuleCall (org.eclipse.xtext.RuleCall)1 ISerializationContext (org.eclipse.xtext.serializer.ISerializationContext)1 ActionContext (org.eclipse.xtext.serializer.analysis.SerializationContext.ActionContext)1 Pda (org.eclipse.xtext.util.formallang.Pda)1