private IRule WeighAndOr(IRule parentRule) { IRule currentRule = parentRule; Or previousRule = null; while (true) { IComparisonRule comparisonRule = currentRule as IComparisonRule; if (comparisonRule == null) { break; } if (comparisonRule is Or) { previousRule = currentRule as Or; currentRule = comparisonRule.RValue; continue; } And andRule = comparisonRule as And; while (andRule.RValue is And) { andRule = andRule.RValue as And; } Or bottomOr = andRule.RValue as Or; if (bottomOr != null) { IRule orLeft = bottomOr.LValue; bottomOr.LValue = andRule; andRule.RValue = orLeft; if (previousRule == null) { parentRule = previousRule = bottomOr; } else { previousRule.RValue = bottomOr; previousRule = bottomOr; } } else { break; } currentRule = bottomOr.RValue; } return(parentRule); }
public void SetNextRule(IComparisonRule rule) { next = rule; }
private IRule dumpStack(Stack <string> stack) { Stack <IRule> rules = new Stack <IRule>(); while (stack.Count > 0) { string result = stack.Pop(); switch (result) { case "!": { IRule lastRule = rules.Peek(); IComparisonRule comparisonRule = lastRule as IComparisonRule; if (comparisonRule != null) { comparisonRule.LValue = new Not { InnerRule = comparisonRule.LValue }; } else { rules.Pop(); rules.Push(new Not { InnerRule = lastRule }); } break; } case ")": { rules.Push(new GroupRule()); break; } case "&": { IRule rValue = rules.Pop(); rules.Push(new And { RValue = rValue }); break; } case "|": { IRule rValue = rules.Pop(); And andRule = rValue as And; if (rValue != null) { } rules.Push(new Or { RValue = rValue }); break; } default: { IRule currentRule = null; if (result == "(") { if (rules.Count == 0) { throw new ArgumentException("Missing an closing paren."); } IRule innerRule = WeighAndOr(rules.Pop()); if (rules.Count == 0) { throw new ArgumentException("Missing an closing paren."); } GroupRule groupRule = rules.Pop() as GroupRule; groupRule.InnerRule = innerRule; currentRule = groupRule; } else { currentRule = new Match { Role = result } }; if (rules.Count > 0) { IComparisonRule rule = rules.Peek() as IComparisonRule; if (rule != null) { rules.Pop(); rule.LValue = currentRule; currentRule = rule; } } rules.Push(currentRule); break; } } } if (rules.Count > 1) { throw new ArgumentException("Missing an opening paren."); } IRule finalRule = WeighAndOr(rules.Pop()); return(finalRule); }
private static void AddRule(IComparisonRule rule) { if (previous != null) previous.SetNextRule(rule); if (start == null) start = rule; previous = rule; }