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();
}
Aggregations