Antlr4.Analysis.LeftFactoringRuleTransformer.TranslateLeftFactoredAlternative C# (CSharp) Method

TranslateLeftFactoredAlternative() protected method

protected TranslateLeftFactoredAlternative ( GrammarAST alternative, string factoredRule, bool variant, DecisionFactorMode mode, bool includeFactoredElement ) : GrammarAST
alternative Antlr4.Tool.Ast.GrammarAST
factoredRule string
variant bool
mode DecisionFactorMode
includeFactoredElement bool
return Antlr4.Tool.Ast.GrammarAST
        protected virtual GrammarAST TranslateLeftFactoredAlternative(GrammarAST alternative, string factoredRule, bool variant, DecisionFactorMode mode, bool includeFactoredElement)
        {
            if (mode == DecisionFactorMode.PARTIAL_UNFACTORED && includeFactoredElement)
            {
                throw new ArgumentException("Cannot include the factored element in unfactored alternatives.");
            }
            else if (mode == DecisionFactorMode.COMBINED_FACTOR && !includeFactoredElement)
            {
                throw new ArgumentException("Cannot return a combined answer without the factored element.");
            }

            Debug.Assert(alternative.ChildCount > 0);

            if (alternative.GetChild(0).Type == 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.ChildCount == 0)
            {
                adaptor.AddChild(alternative, adaptor.Create(ANTLRParser.EPSILON, "EPSILON"));
            }

            Debug.Assert(alternative.ChildCount > 0);
            return alternative;
        }