public static Action ParseActionSequence(string actionsSequence) { // ReSharper disable once RedundantAssignment Action action = null; action = ParseAtomicAction(actionsSequence); if (action == null) { int firstOpeningBracePosition = actionsSequence.IndexOf("("); int[] closingBracesPositions = BracketedConfigProcessor.AllIndexesOf(actionsSequence, ")"); if (firstOpeningBracePosition == -1 || closingBracesPositions.Length != 0) { if (closingBracesPositions.Length > 0) { for (int tryingClosingBraceIndex = 0; tryingClosingBraceIndex != closingBracesPositions.Length; tryingClosingBraceIndex++) { int possibleExpression1Start = firstOpeningBracePosition + 1; int possibleAction1End = closingBracesPositions[tryingClosingBraceIndex]; int possibleExpression1Length = possibleAction1End - possibleExpression1Start; Action action1 = null; if (possibleExpression1Length > 0) { string possibleAction1Substring = actionsSequence.Substring(possibleExpression1Start, possibleExpression1Length); action1 = ParseActionSequence(possibleAction1Substring); } if (action1 != null) { string restOfString = actionsSequence.Substring(possibleAction1End); if (!Regex.IsMatch(restOfString, @"^\s*\)\s*$")) { if (Regex.IsMatch(restOfString, @"^\s*\)\s*(and|AND).+$")) { string possibleAction2Substring = restOfString.Substring(restOfString.IndexOf("and") + "and".Length); Action action2 = ParseActionSequence(possibleAction2Substring); if (action2 != null) { action = new CombinedAction(new List <Action> { action1 }); if (action2 is OneOf) { ((CombinedAction)action).Actions.Add(action2); } else if (action2 is CombinedAction) { ((CombinedAction)action).Actions.AddRange(((CombinedAction)action2).Actions); } else { ((CombinedAction)action).Actions.Add(action2); } } } else if (Regex.IsMatch(restOfString, @"^\s*\)\s*(or|OR).+$")) { string possibleAction2Substring = restOfString.Substring(restOfString.IndexOf("or") + "or".Length); Action action2 = ParseActionSequence(possibleAction2Substring); if (action2 != null) { action = new OneOf(new List <Action> { action1 }); if (action2 is OneOf action2AsOneOf) { ((OneOf)action).Actions.AddRange(action2AsOneOf.Actions); } else { ((OneOf)action).Actions.Add(action2); } } } } else { action = action1; } if (action != null) { break; } } } } } } return(action); }
public static Action ParseActionSequence(string actionsSequence) { Action action = null; try { action = ParseAtomicAction(actionsSequence); return(action); } catch { int firstOpeningBracePosition = actionsSequence.IndexOf("("); int[] closingBracesPositions = BracketedConfigProcessor.AllIndexesOf(actionsSequence, ")"); if (firstOpeningBracePosition != -1 && closingBracesPositions.Length == 0) { throw new ActionParseException(); } if (closingBracesPositions.Length > 0) { for (int tryingClosingBraceIndex = 0; tryingClosingBraceIndex != closingBracesPositions.Length; tryingClosingBraceIndex++) { try { int possibleExpression1Start = firstOpeningBracePosition + 1; int possibleAction1End = closingBracesPositions[tryingClosingBraceIndex]; int possibleExpression1Length = possibleAction1End - possibleExpression1Start; string possibleAction1Substring = actionsSequence.Substring(possibleExpression1Start, possibleExpression1Length); Action action1 = ParseActionSequence(possibleAction1Substring); string restOfString = actionsSequence.Substring(possibleAction1End); if (!Regex.IsMatch(restOfString, @"^\s*\)\s*$")) { if (Regex.IsMatch(restOfString, @"^\s*\)\s*(and|AND).+$")) { string possibleAction2Substring = restOfString.Substring( restOfString.IndexOf("and") + "and".Length); Action action2 = ParseActionSequence(possibleAction2Substring); if (action1 == null || action2 == null) { throw new ActionParseException(); } action = new CombinedAction( new List <Action> { action1, action2 }); } else if (Regex.IsMatch(restOfString, @"^\s*\)\s*(or|OR).+$")) { string possibleAction2Substring = restOfString.Substring( restOfString.IndexOf("or") + "or".Length); Action action2 = ParseActionSequence(possibleAction2Substring); if (action1 == null || action2 == null) { throw new ActionParseException(); } action = new OneOf(new List <Action> { action1, action2 }); } else { throw new ActionParseException(); } break; } else { action = action1; } break; } catch { } } if (action == null) { throw new ActionParseException(); } } return(action); } }