public bool Match(IList<Token> tokens, HandlerRegistry registry) { var tokenIndex = 0; foreach (var pattern in _patterns) { var isOptional = pattern.IsOptional; var isRequired = !isOptional; var thereAreNoMoreTokens = tokenIndex == tokens.Count; if (pattern is TagPattern) { var match = tokenIndex < tokens.Count && tokens[tokenIndex].IsTaggedAs((pattern as TagPattern).TagType); if (match == false && isRequired) { return false; } if (match) { tokenIndex++; } } else if (pattern is RepeatPattern) { var repetition = pattern as RepeatPattern; int advancement; var match = repetition.Match(tokens.Skip(tokenIndex).ToList(), out advancement); if (match == false && isRequired) { return false; } if (match) { tokenIndex += advancement; } } else if (pattern is HandlerTypePattern) { if (isOptional && thereAreNoMoreTokens) { return true; } var subHandlers = registry.GetHandlers((pattern as HandlerTypePattern).Type); foreach (var handler in subHandlers) { if (handler.Match(tokens.Skip(tokenIndex).ToList(), registry)) { return true; } } } } if (tokenIndex != tokens.Count) { return false; } return true; }
public void MergeWith(HandlerRegistry registry) { foreach (var type in registry._handlers.Keys) { if (_handlers.ContainsKey(type) == false) { _handlers[type] = new List<ComplexHandler>(); } _handlers[type].AddRange(registry._handlers[type]); } }