protected ATNConfigSet ApplyPrecedenceFilter(ATNConfigSet configSet)
{
Dictionary<int, PredictionContext> statesFromAlt1 = new Dictionary<int, PredictionContext>();
ATNConfigSet result = new ATNConfigSet(configSet.fullCtx);
foreach (ATNConfig config in configSet.configs)
{
// handle alt 1 first
if (config.alt != 1)
{
continue;
}
SemanticContext updatedContext = config.semanticContext.EvalPrecedence(parser, context);
if (updatedContext == null)
{
// the configuration was eliminated
continue;
}
statesFromAlt1[config.state.stateNumber] = config.context;
if (updatedContext != config.semanticContext)
{
result.Add(new ATNConfig(config, updatedContext), mergeCache);
}
else {
result.Add(config, mergeCache);
}
}
foreach (ATNConfig config in configSet.configs)
{
if (config.alt == 1)
{
// already handled
continue;
}
if (!config.IsPrecedenceFilterSuppressed)
{
/* In the future, this elimination step could be updated to also
* filter the prediction context for alternatives predicting alt>1
* (basically a graph subtraction algorithm).
*/
PredictionContext ctx;
if (statesFromAlt1.TryGetValue(config.state.stateNumber, out ctx))
{
if (ctx != null && ctx.Equals(config.context))
{
// eliminated
continue;
}
}
}
result.Add(config, mergeCache);
}
return result;
}