protected GrammarAST translateLeftFactoredAlternative(GrammarAST alternative, String factoredRule, boolean variant, DecisionFactorMode mode, boolean includeFactoredElement) {
if (mode == DecisionFactorMode.PARTIAL_UNFACTORED && includeFactoredElement) {
throw new IllegalArgumentException("Cannot include the factored element in unfactored alternatives.");
}
else if (mode == DecisionFactorMode.COMBINED_FACTOR && !includeFactoredElement) {
throw new IllegalArgumentException("Cannot return a combined answer without the factored element.");
}
assert alternative.getChildCount() > 0;
if (alternative.getChild(0).getType() == ANTLRParser.EPSILON) {
if (mode == DecisionFactorMode.PARTIAL_UNFACTORED) {
return alternative;
}
return null;
}
GrammarAST translatedElement = translateLeftFactoredElement((GrammarAST)alternative.getChild(0), factoredRule, variant, mode, includeFactoredElement);
if (translatedElement == null) {
return null;
}
alternative.replaceChildren(0, 0, translatedElement);
if (alternative.getChildCount() == 0) {
adaptor.addChild(alternative, adaptor.create(ANTLRParser.EPSILON, "EPSILON"));
}
assert alternative.getChildCount() > 0;
return alternative;
}