public virtual GrammarAST StripLeftRecursion(GrammarAST altAST)
{
GrammarAST lrlabel = null;
GrammarAST first = (GrammarAST)altAST.GetChild(0);
int leftRecurRuleIndex = 0;
if (first.Type == ELEMENT_OPTIONS)
{
first = (GrammarAST)altAST.GetChild(1);
leftRecurRuleIndex = 1;
}
ITree rref = first.GetChild(1); // if label=rule
if ((first.Type == RULE_REF && first.Text.Equals(ruleName)) ||
(rref != null && rref.Type == RULE_REF && rref.Text.Equals(ruleName)))
{
if (first.Type == ASSIGN || first.Type == PLUS_ASSIGN)
lrlabel = (GrammarAST)first.GetChild(0);
// remove rule ref (first child unless options present)
altAST.DeleteChild(leftRecurRuleIndex);
// reset index so it prints properly (sets token range of
// ALT to start to right of left recur rule we deleted)
GrammarAST newFirstChild = (GrammarAST)altAST.GetChild(leftRecurRuleIndex);
altAST.TokenStartIndex = newFirstChild.TokenStartIndex;
}
return lrlabel;
}