public MatchInfo Evaluate(string text) { EnqueueTokens(_Tokens); _text = text; IGlobToken token = null; using (_Reader = new GlobStringReader(text)) { while (_TokenQueue.Any()) { token = _TokenQueue.Dequeue(); token.Accept(this); if (!Success) { return(FailedResult(token)); } } // if all tokens matched but still more text then fail! if (_Reader.Peek() != -1) { return(FailedResult(null)); } // Success. return(SuccessfulResult()); } }
private MatchInfo FailedResult(IGlobToken token) { return(new MatchInfo() { Matches = MatchedTokens.ToArray(), Missed = token, Success = false, UnmatchedText = _Reader.ReadToEnd() }); }
public void Visit(WildcardDirectoryToken token) { // if no more tokens then just return as * matches the rest of the segment, and therefore no more matching. int remainingCount = _Tokens.Length - (_currentTokenIndex + 1); // Add a nested CompositeTokenEvaluator, passing all of our remaining tokens to it. IGlobToken[] remaining = new IGlobToken[remainingCount]; Array.Copy(_Tokens, _currentTokenIndex + 1, remaining, 0, remainingCount); AddEvaluator(_evaluatorFactory.CreateTokenEvaluator(token, new CompositeTokenEvaluator(remaining, _evaluatorFactory))); _finished = true; // signlas to stop visiting any further tokens as we have offloaded them all to the nested evaluator. }
void IGlobTokenVisitor.Visit(WildcardDirectoryToken token) { // if no more tokens then just return as * matches the rest of the segment, and therefore no more matching. int remainingCount = _tokens.Length - (_currentTokenIndex + 1); // Add a nested CompositeTokenEvaluator, passing all of our remaining tokens to it. IGlobToken[] remaining = new IGlobToken[remainingCount]; Array.Copy(_tokens, _currentTokenIndex + 1, remaining, 0, remainingCount); var subEvaluator = new WildcardDirectoryTokenMatchGenerator(token, _random, new CompositeTokenMatchGenerator(_random, remaining)); AddMatchGenerator(subEvaluator); _finished = true; // signlas to stop visiting any further tokens as we have offloaded them all to the nested evaluator. }
/// <summary> /// Parses a token for a range or list globbing expression. /// </summary> private IGlobToken ReadRangeOrListToken(GlobStringReader reader) { bool isNegated = false; bool isNumberRange = false; bool isLetterRange = false; bool isCharList = false; if (reader.PeekChar() == GlobStringReader.ExclamationMarkChar) { isNegated = true; reader.Read(); } var nextChar = reader.PeekChar(); if (Char.IsLetterOrDigit(nextChar)) { reader.Read(); nextChar = reader.PeekChar(); if (nextChar == GlobStringReader.DashChar) { if (Char.IsLetter(reader.CurrentChar)) { isLetterRange = true; } else { isNumberRange = true; } // throw new ArgumentOutOfRangeException("Range expressions must either be a letter range, i.e [a-z] or a number range i.e [0-9]"); } else { isCharList = true; } AcceptCurrentChar(reader); } else { isCharList = true; reader.Read(); AcceptCurrentChar(reader); } if (isLetterRange || isNumberRange) { // skip over the dash char reader.ReadChar(); } while (reader.ReadChar()) { // ReadCharacter(CharacterType.BracketedText, CurrentChar); if (reader.IsEndOfRangeOrList) { var peekChar = reader.PeekChar(); // Close brackets within brackets are escaped with another // Close bracket. e.g. [a]] matches a[ if (peekChar == GlobStringReader.CloseBracketChar) { AcceptCurrentChar(reader); // Read(); //ReadCharacter(CharacterType.BracketedText, CurrentChar); } else { break; } } else { AcceptCurrentChar(reader); } } // construct token IGlobToken result = null; var value = GetBufferAndReset(); if (isCharList) { result = new CharacterListToken(value.ToCharArray(), isNegated); } else if (isLetterRange) { var start = value[0]; var end = value[1]; result = new LetterRangeToken(start, end, isNegated); } else if (isNumberRange) { var start = value[0]; // int.Parse(value[0].ToString()); var end = value[1]; // int.Parse(value[1].ToString()); result = new NumberRangeToken(start, end, isNegated); } return(result); }