private bool Match(MatchNode matchNode, IMatchable @object) { if ([email protected](matchNode, this)) { return(false); } int expr_index = 0; int stat_index = 0; foreach (MatchNode childNode in matchNode.GetChildren()) { bool isStatement = childNode.GetType() == MatchNode.Matchnode_Statement; IMatchable childObject = @object.FindObject(childNode, isStatement ? stat_index : expr_index); if (childObject == null || !Match(childNode, childObject)) { return(false); } if (isStatement) { stat_index++; } else { expr_index++; } } return(true); }
public MatchEngine(string description) { // each line is a separate statement/exprent string[] lines = description.Split("\n"); int depth = 0; Stack <MatchNode> stack = new Stack <MatchNode>(); foreach (string line in lines) { List <string> properties = new List <string>(Sharpen.Arrays.AsList(line.Split("\\s+" ))); // split on any number of whitespaces if ((properties[0].Length == 0)) { properties.RemoveAtReturningValue(0); } int node_type = "statement".Equals(properties[0]) ? MatchNode.Matchnode_Statement : MatchNode.Matchnode_Exprent; // create new node MatchNode matchNode = new MatchNode(node_type); for (int i = 1; i < properties.Count; ++i) { string[] values = properties[i].Split(":"); IMatchable.MatchProperties property = (node_type == MatchNode.Matchnode_Statement ? stat_properties : expr_properties).GetOrNull(values[0]); if (property == null) { // unknown property defined throw new Exception("Unknown matching property"); } else { object value; int parameter = 0; string strValue = values[1]; if (values.Length == 3) { parameter = System.Convert.ToInt32(values[1]); strValue = values[2]; } switch (property.ordinal()) { case 0: { value = stat_type.GetOrNullable(strValue); break; } case 2: case 3: { value = int.Parse(strValue); break; } case 4: case 8: case 13: case 14: case 15: case 16: case 17: case 12: case 1: case 7: { value = strValue; break; } case 5: { value = stat_if_type.GetOrNullable(strValue); break; } case 9: { value = expr_func_type.GetOrNullable(strValue); break; } case 10: { value = expr_exit_type.GetOrNullable(strValue); break; } case 11: { value = expr_const_type.GetOrNull(strValue); break; } case 6: { value = expr_type.GetOrNullable(strValue); break; } default: { throw new Exception("Unhandled matching property"); } } matchNode.AddRule(property, new MatchNode.RuleValue(parameter, value)); } } if ((stack.Count == 0)) { // first line, root node stack.Push(matchNode); } else { // return to the correct parent on the stack int new_depth = line.LastIndexOf(' ', depth) + 1; for (int i = new_depth; i <= depth; ++i) { stack.Pop(); } // insert new node stack.First().AddChild(matchNode); stack.Push(matchNode); depth = new_depth; } } this.rootNode = stack.Last(); }
public abstract bool Match(MatchNode matchNode, MatchEngine engine);
public virtual void AddChild(MatchNode child) { children.Add(child); }
public abstract IMatchable FindObject(MatchNode matchNode, int index);