private IExpression ParseConditionalAlternation() { ++index; // swallow '(' var expressionOrName = Parse(); if (index == regex.Length || regex[index] != ')') { // The Regex constructor should prevent this throw new InvalidOperationException(); } ++index; // swallow ')' var item = Parse(); IExpression yes = null; IExpression no = null; if (item is IAlternation alternation) { if (alternation.Alternatives.Count() > 2) { // The Regex constructor should prevent this throw new InvalidOperationException(); } yes = alternation.Alternatives.First(); no = alternation.Alternatives.Skip(1).FirstOrDefault(); } else { yes = item; } return(ConditionalAlternation.For(expressionOrName, yes, no)); }
/// <summary> /// Creates an alternation that tries to match the 'yes' option if the expression is matched. /// </summary> /// <param name="expression">The expression that must be matched for the 'yes' option to be matched.</param> /// <param name="yes">The alternative to match if the capture group is matched.</param> /// <param name="no">The alternative to match if the capture group is not matched.</param> /// <returns>The alternation.</returns> public static IConditionalAlternation For(IExpression expression, IExpression yes, IExpression no) { if (expression == null) { throw new ArgumentNullException(nameof(expression)); } if (yes == null) { throw new ArgumentNullException(nameof(yes)); } var alternation = new ConditionalAlternation() { Expression = expression, YesOption = yes, NoOption = no }; return(alternation); }