protected virtual bool ExpandOptionalQuantifiersForBlock(GrammarAST block, bool variant) { IList <GrammarAST> children = new List <GrammarAST>(); for (int i = 0; i < block.ChildCount; i++) { GrammarAST child = (GrammarAST)block.GetChild(i); if (child.Type != ANTLRParser.ALT) { children.Add(child); continue; } GrammarAST expandedAlt = ExpandOptionalQuantifiersForAlt(child); if (expandedAlt == null) { return(false); } children.Add(expandedAlt); } GrammarAST newChildren = (GrammarAST)adaptor.Nil(); newChildren.AddChildren(children); block.ReplaceChildren(0, block.ChildCount - 1, newChildren); block.FreshenParentAndChildIndexesDeeply(); if (!variant && block.Parent is RuleAST) { RuleAST ruleAST = (RuleAST)block.Parent; string ruleName = ruleAST.GetChild(0).Text; Rule r = _rules[ruleName]; IList <GrammarAST> blockAlts = block.GetAllChildrenWithType(ANTLRParser.ALT); r.numberOfAlts = blockAlts.Count; r.alt = new Alternative[blockAlts.Count + 1]; for (int i = 0; i < blockAlts.Count; i++) { r.alt[i + 1] = new Alternative(r, i + 1); r.alt[i + 1].ast = (AltAST)blockAlts[i]; } } return(true); }
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); }