private static async Task<ImmutableArray<INavigateToSearchResult>> FindNavigableDeclaredSymbolInfosAsync( Project project, Document searchDocument, string pattern, CancellationToken cancellationToken) { var containsDots = pattern.IndexOf('.') >= 0; using (var patternMatcher = new PatternMatcher(pattern, allowFuzzyMatching: true)) { var result = ArrayBuilder<INavigateToSearchResult>.GetInstance(); foreach (var document in project.Documents) { if (searchDocument != null && document != searchDocument) { continue; } cancellationToken.ThrowIfCancellationRequested(); var declarationInfo = await document.GetSyntaxTreeIndexAsync(cancellationToken).ConfigureAwait(false); foreach (var declaredSymbolInfo in declarationInfo.DeclaredSymbolInfos) { cancellationToken.ThrowIfCancellationRequested(); var patternMatches = patternMatcher.GetMatches( GetSearchName(declaredSymbolInfo), declaredSymbolInfo.FullyQualifiedContainerName, includeMatchSpans: true); if (!patternMatches.IsEmpty) { result.Add(ConvertResult(containsDots, declaredSymbolInfo, document, patternMatches)); } } } return result.ToImmutableAndFree(); } }
public virtual bool Match(PatternMatcher pm, ILExpression e) { if (e.Arguments.Count != this.Arguments.Length || e.Prefixes != null) return false; for (int i = 0; i < this.Arguments.Length; i++) if (!this.Arguments[i].Match(pm, e.Arguments[i])) return false; return true; }
public override void Given() { matcher = new PatternMatcher<string, int> { {One.unapply, x => x}, {NinetyNine.unapply, x => x} }; }
public override void Given() { matcher = new PatternMatcher<Option<string>, int> { {Some.unapply, str => str.Length}, //matches Some<string> {None.unapply, () => 1977} //matches None<string> }; }
public AliasBeGonePackage() { var configuration = new Configuration(); var resourceManager = new ResourceReader(SNIPPETS_NAMESPACE); _patternMatcher = new PatternMatcher(configuration); _snippetsManager = new SnippetsManager(configuration, resourceManager); _snippetsInstalled = _snippetsManager.IsInstalled; }
public void Search_TextToSearchParameterIsNull_ReturnsZeroPatternHits() { var configuration = new Configuration(); var patternMatcher = new PatternMatcher(configuration); var patternHits = patternMatcher.Search(null); Assert.AreEqual(0, patternHits.Count()); }
private static async Task <ImmutableArray <SearchResult> > ComputeSearchResultsAsync( Project project, ImmutableArray <Document> priorityDocuments, Document searchDocument, PatternMatcher nameMatcher, PatternMatcher containerMatcherOpt, DeclaredSymbolInfoKindSet kinds, ArrayBuilder <PatternMatch> nameMatches, ArrayBuilder <PatternMatch> containerMatches, CancellationToken cancellationToken) { var result = ArrayBuilder <SearchResult> .GetInstance(); // Prioritize the active documents if we have any. var highPriDocs = priorityDocuments.Where(d => project.ContainsDocument(d.Id)) .ToImmutableArray(); var highPriDocsSet = highPriDocs.ToSet(); var lowPriDocs = project.Documents.Where(d => !highPriDocsSet.Contains(d)); var orderedDocs = highPriDocs.AddRange(lowPriDocs); Debug.Assert(priorityDocuments.All(d => project.ContainsDocument(d.Id)), "Priority docs included doc not from project."); Debug.Assert(orderedDocs.Length == project.Documents.Count(), "Didn't have the same number of project after ordering them!"); Debug.Assert(orderedDocs.Distinct().Length == orderedDocs.Length, "Ordered list contained a duplicate!"); Debug.Assert(project.Documents.All(d => orderedDocs.Contains(d)), "At least one document from the project was missing from the ordered list!"); foreach (var document in orderedDocs) { if (searchDocument != null && document != searchDocument) { continue; } cancellationToken.ThrowIfCancellationRequested(); var declarationInfo = await document.GetSyntaxTreeIndexAsync(cancellationToken).ConfigureAwait(false); foreach (var declaredSymbolInfo in declarationInfo.DeclaredSymbolInfos) { AddResultIfMatch( document, declaredSymbolInfo, nameMatcher, containerMatcherOpt, kinds, nameMatches, containerMatches, result, cancellationToken); } } return(result.ToImmutableAndFree()); }
private static async Task <ImmutableArray <SearchResult> > TryFilterPreviousSearchResultsAsync( Project project, ImmutableArray <Document> priorityDocuments, Document searchDocument, string pattern, PatternMatcher nameMatcher, PatternMatcher containerMatcherOpt, DeclaredSymbolInfoKindSet kinds, ArrayBuilder <PatternMatch> nameMatches, ArrayBuilder <PatternMatch> containerMatches, CancellationToken cancellationToken) { // Searching an entire project. See if we already performed that same // search with a substring of the current pattern. if so, we can use // the previous result and just filter that down. This is useful for // the common case where a user types some pattern, then keeps adding // to it. ImmutableArray <SearchResult> searchResults; if (s_lastProjectSearchCache.TryGetValue(project, out var previousResult) && pattern.StartsWith(previousResult.Item1)) { // We can reuse the previous results and just filter them. searchResults = FilterPreviousResults( previousResult.Item2, nameMatcher, containerMatcherOpt, kinds, nameMatches, containerMatches, cancellationToken); } else { // Didn't have previous results. Or it was a very different pattern. // Can't reuse. searchResults = await ComputeSearchResultsAsync( project, priorityDocuments, searchDocument, nameMatcher, containerMatcherOpt, kinds, nameMatches, containerMatches, cancellationToken).ConfigureAwait(false); } // Would like to use CWT.AddOrUpdate. But that is not available on the // version of .NET that we're using. So we need to take lock as we're // making multiple mutations. lock (s_lastProjectSearchCache) { s_lastProjectSearchCache.Remove(project); s_lastProjectSearchCache.Add(project, Tuple.Create(pattern, searchResults)); } return(searchResults); }
internal static async Task <IEnumerable <ValueTuple <ISymbol, IEnumerable <PatternMatch> > > > FindNavigableSourceSymbolsAsync( Project project, string pattern, CancellationToken cancellationToken) { var results = new List <ValueTuple <ISymbol, IEnumerable <PatternMatch> > >(); var patternMatcher = new PatternMatcher(); var symbols = await SymbolFinder.FindSourceDeclarationsAsync( project, k => patternMatcher.MatchPattern(k, pattern) != null, SymbolFilter.TypeAndMember, cancellationToken).ConfigureAwait(false); symbols = symbols.Where(s => !s.IsConstructor() && !s.IsStaticConstructor() && // not constructors, they get matched on type name !(s is INamespaceSymbol) && // not namespaces s.Locations.Any(loc => loc.IsInSource)); // only source symbols foreach (var symbol in symbols) { cancellationToken.ThrowIfCancellationRequested(); var matches = patternMatcher.MatchPattern(GetSearchName(symbol), pattern); results.Add(ValueTuple.Create(symbol, matches)); // also report matching constructors (using same match result as type) var namedType = symbol as INamedTypeSymbol; if (namedType != null) { foreach (var constructor in namedType.Constructors) { // only constructors that were explicitly declared if (!constructor.IsImplicitlyDeclared) { results.Add(ValueTuple.Create((ISymbol)constructor, matches)); } } } // report both parts of partial methods var method = symbol as IMethodSymbol; if (method != null && method.PartialImplementationPart != null) { results.Add(ValueTuple.Create((ISymbol)method, matches)); } } return(results); }
private static async Task <ImmutableArray <INavigateToSearchResult> > FindSearchResultsAsync( Project project, Document searchDocument, string pattern, CancellationToken cancellationToken) { // If the user created a dotted pattern then we'll grab the last part of the name var(patternName, patternContainerOpt) = PatternMatcher.GetNameAndContainer(pattern); var nameMatcher = PatternMatcher.CreatePatternMatcher(patternName, includeMatchedSpans: true, allowFuzzyMatching: true); var containerMatcherOpt = patternContainerOpt != null ? PatternMatcher.CreateDotSeparatedContainerMatcher(patternContainerOpt) : null; using (nameMatcher) using (containerMatcherOpt) { var nameMatches = ArrayBuilder <PatternMatch> .GetInstance(); var containerMatches = ArrayBuilder <PatternMatch> .GetInstance(); try { // If we're searching a single document, then just do a full search of // that document (we're fast enough to not need to optimize that case). // // If, however, we are searching a project, then see if we could potentially // use the last computed results we have for that project. If so, it can // be much faster to reuse and filter that result than to compute it from // scratch. #if true var task = searchDocument != null ? ComputeSearchResultsAsync(project, searchDocument, nameMatcher, containerMatcherOpt, nameMatches, containerMatches, cancellationToken) : TryFilterPreviousSearchResultsAsync(project, searchDocument, pattern, nameMatcher, containerMatcherOpt, nameMatches, containerMatches, cancellationToken); #else var task = ComputeSearchResultsAsync(project, searchDocument, nameMatcher, containerMatcherOpt, nameMatches, containerMatches, cancellationToken); #endif var searchResults = await task.ConfigureAwait(false); return(ImmutableArray <INavigateToSearchResult> .CastUp(searchResults)); } finally { nameMatches.Free(); containerMatches.Free(); } } }
public static async Task SearchGeneratedDocumentsInCurrentProcessAsync( Project project, string pattern, IImmutableSet <string> kinds, Func <RoslynNavigateToItem, Task> onResultFound, CancellationToken cancellationToken) { // If the user created a dotted pattern then we'll grab the last part of the name var(patternName, patternContainerOpt) = PatternMatcher.GetNameAndContainer(pattern); var declaredSymbolInfoKindsSet = new DeclaredSymbolInfoKindSet(kinds); // First generate all the source-gen docs. Then handoff to the standard search routine to find matches in them. var generatedDocs = await project.GetSourceGeneratedDocumentsAsync(cancellationToken).ConfigureAwait(false); await ProcessDocumentsAsync(searchDocument : null, patternName, patternContainerOpt, declaredSymbolInfoKindsSet, onResultFound, generatedDocs.ToSet <Document>(), cancellationToken).ConfigureAwait(false); }
public Searcher( Solution solution, IAsynchronousOperationListener asyncListener, ItemDisplayFactory displayFactory, INavigateToCallback callback, string searchPattern, CancellationToken cancellationToken) { _solution = solution; _displayFactory = displayFactory; _callback = callback; _searchPattern = searchPattern; _patternMatcher = new PatternMatcher(); _cancellationToken = cancellationToken; _progress = new ProgressTracker(callback.ReportProgress); _asyncListener = asyncListener; }
private PatternMatcher GetPatternMatcher( string value, CultureInfo culture, Dictionary <string, PatternMatcher> map) { lock (_gate) { PatternMatcher patternMatcher; if (!map.TryGetValue(value, out patternMatcher)) { patternMatcher = new PatternMatcher(value, culture, verbatimIdentifierPrefixIsWordCharacter: true, allowFuzzyMatching: false); map.Add(value, patternMatcher); } return(patternMatcher); } }
private static PatternMatch?TryMatchSingleWordPattern(string candidate, string pattern) { MarkupTestFile.GetSpans(candidate, out candidate, out ImmutableArray <TextSpan> spans); var match = new PatternMatcher(pattern).MatchSingleWordPattern_ForTestingOnly(candidate); if (match == null) { Assert.True(spans.Length == 0); } else { Assert.Equal <TextSpan>(match.Value.MatchedSpans, spans); } return(match); }
private static void AddResultIfMatch( Document document, DeclaredSymbolInfo declaredSymbolInfo, PatternMatcher nameMatcher, PatternMatcher containerMatcherOpt, ArrayBuilder <PatternMatch> nameMatches, ArrayBuilder <PatternMatch> containerMatches, ArrayBuilder <SearchResult> result, CancellationToken cancellationToken) { nameMatches.Clear(); containerMatches.Clear(); cancellationToken.ThrowIfCancellationRequested(); if (nameMatcher.AddMatches(declaredSymbolInfo.Name, nameMatches) && containerMatcherOpt?.AddMatches(declaredSymbolInfo.FullyQualifiedContainerName, containerMatches) != false) { result.Add(ConvertResult( declaredSymbolInfo, document, nameMatches, containerMatches)); } }
public static LanguageMatcher GetJsonMatcher() { lock (_matcherMap) { if (!_matcherMap.TryGetValue(Language.Json.ToString(), out LanguageMatcher languageMatcher)) { PatternMatcher stringLiteralPattern = PatternReader.LazyParse(id: 1, name: "String", pattern: "\"((\"!.)|\\\\.)+\""); PatternMatcher nullPattern = PatternReader.LazyParse(id: 2, name: "Null", pattern: "`~null"); PatternMatcher booleanPattern = PatternReader.LazyParse(id: 3, name: "Boolean", pattern: "`~true|false"); PatternMatcher numberPattern = PatternReader.LazyParse(id: 4, name: "Number", pattern: "~-?(\\d*\\.\\d+(e(-|\\+)?\\d+)?|\\d+)"); PatternMatcher colonSeparatorPattern = PatternReader.LazyParse(id: 5, name: "ColonSeparator", pattern: "\\s*:\\s*"); PatternMatcher commaSeparatorPattern = PatternReader.LazyParse(id: 6, name: "CommaSeparator", pattern: "\\s*,\\s*"); PatternMatcher openBracePattern = PatternReader.LazyParse(id: 7, name: "OpenBrace", pattern: "\\{"); PatternMatcher closeBracePattern = PatternReader.LazyParse(id: 8, name: "CloseBrace", pattern: "\\}"); PatternMatcher openBracketPattern = PatternReader.LazyParse(id: 9, name: "OpenBracket", pattern: "\\["); PatternMatcher closeBracketPattern = PatternReader.LazyParse(id: 10, name: "CloseBracket", pattern: "\\]"); PatternMatcher whitespacePattern = PatternReader.LazyParse(id: 11, name: "Whitespace", pattern: "\\s+"); FragmentMatcher keyValueFragment = new FragmentMatcher(id: 5, name: "KeyValue", parts: new IMatcher[2] { stringLiteralPattern, null }, partsMatchMode: MatchMode.Ordered, partsDelimiter: colonSeparatorPattern); FragmentMatcher objectFragment = new FragmentMatcher(id: 4, name: "Object", parts: new IMatcher[] { keyValueFragment }, partsMatchMode: MatchMode.Multiple, partsDelimiter: commaSeparatorPattern, minMatchedParts: 3, partsPadding: whitespacePattern, start: openBracePattern, end: closeBracePattern); FragmentMatcher arrayFragment = new FragmentMatcher(id: 3, name: "Array", parts: new IMatcher[1], partsMatchMode: MatchMode.Multiple, partsDelimiter: commaSeparatorPattern, minMatchedParts: 0, partsPadding: whitespacePattern, start: openBracketPattern, end: closeBracketPattern); FragmentMatcher itemFragment = new FragmentMatcher(id: 2, name: "Item", parts: new IMatcher[] { objectFragment, arrayFragment, booleanPattern, nullPattern, stringLiteralPattern, numberPattern }, partsMatchMode: MatchMode.One, fallThrough: true); arrayFragment.Parts[0] = itemFragment; keyValueFragment.Parts[1] = itemFragment; FragmentMatcher jsonFragment = new FragmentMatcher(id: 1, name: "Json", parts: new IMatcher[] { objectFragment, arrayFragment }, partsMatchMode: MatchMode.One, partsPadding: whitespacePattern); languageMatcher = new LanguageMatcher { Name = Language.Json.ToString(), IndexingMode = IndexingMode.Lazy, StartingFragment = jsonFragment, Fragments = new List <FragmentMatcher> { keyValueFragment, objectFragment, arrayFragment, itemFragment, jsonFragment }, Patterns = new List <PatternMatcher> { stringLiteralPattern, nullPattern, booleanPattern, numberPattern, colonSeparatorPattern, commaSeparatorPattern, openBracePattern, closeBracePattern, openBracketPattern, closeBracketPattern, whitespacePattern } }; _matcherMap[Language.Json.ToString()] = languageMatcher; } return(languageMatcher); } }
public static void onMatcherFailure(ProseRuntime runtime, PatternMatcher match) { restoreConsoleColor(); StringBuilder str = new StringBuilder(); ProseObject[] partialPattern = match.AssociatedPattern; foreach (ProseObject po in partialPattern) { str.Append(po.getReadableString()); str.Append(" "); } if (str.Length != 0) { str.Remove(str.Length - 1, 1); } Console.WriteLine("match fail> " + str.ToString()); }
private static PatternMatch?TestNonFuzzyMatch(string candidate, string pattern) { MarkupTestFile.GetSpans(candidate, out candidate, out ImmutableArray <TextSpan> spans); var match = PatternMatcher.CreatePatternMatcher(pattern, includeMatchedSpans: true, allowFuzzyMatching: false) .GetFirstMatch(candidate); if (match == null) { Assert.True(spans.Length == 0); } else { Assert.Equal <TextSpan>(match.Value.MatchedSpans, spans); } return(match); }
private static IEnumerable <PatternMatch> TryMatchMultiWordPattern(string candidate, string pattern) { MarkupTestFile.GetSpans(candidate, out candidate, out ImmutableArray <TextSpan> expectedSpans); var matches = new PatternMatcher(pattern).GetMatches(candidate, includeMatchSpans: true); if (matches.IsDefaultOrEmpty) { Assert.True(expectedSpans.Length == 0); return(null); } else { var actualSpans = matches.SelectMany(m => m.MatchedSpans).OrderBy(s => s.Start).ToList(); Assert.Equal(expectedSpans, actualSpans); return(matches); } }
> FindSourceDeclarationsWithPatternInCurrentProcessAsync( string pattern, Func <SearchQuery, Task <ImmutableArray <ISymbol> > > searchAsync ) { // The compiler API only supports a predicate which is given a symbol's name. Because // we only have the name, and nothing else, we need to check it against the last segment // of the pattern. i.e. if the pattern is 'Console.WL' and we are given 'WriteLine', then // we don't want to check the whole pattern against it (as it will clearly fail), instead // we only want to check the 'WL' portion. Then, after we get all the candidate symbols // we'll check if the full name matches the full pattern. var(namePart, containerPart) = PatternMatcher.GetNameAndContainer(pattern); var dotIndex = pattern.LastIndexOf('.'); var isDottedPattern = dotIndex >= 0; // If we don't have a dot in the pattern, just make a pattern matcher for the entire // pattern they passed in. Otherwise, make a pattern matcher just for the part after // the dot. using var nameMatcher = PatternMatcher.CreatePatternMatcher( namePart, includeMatchedSpans: false ); using var query = SearchQuery.CreateCustom(nameMatcher.Matches); var symbolAndProjectIds = await searchAsync(query).ConfigureAwait(false); if (symbolAndProjectIds.Length == 0 || !isDottedPattern) { // If it wasn't a dotted pattern, or we didn't get anything back, then we're done. // We can just return whatever set of results we got so far. return(symbolAndProjectIds); } // Ok, we had a dotted pattern. Have to see if the symbol's container matches the // pattern as well. using var containerPatternMatcher = PatternMatcher.CreateDotSeparatedContainerMatcher( containerPart ); return(symbolAndProjectIds.WhereAsArray( t => containerPatternMatcher.Matches(GetContainer(t)) )); }
public FragmentMatcher( int id, string name, PatternMatcher start = default, PatternMatcher end = default, IList <IMatcher> parts = default, PatternMatcher partsDelimiter = default, bool partsDelimiterRequired = true, PatternMatcher partsPadding = default, int?minMatchedParts = default, MatchMode partsMatchMode = default, bool isNoise = default, bool fallThrough = default, bool cacheable = default, bool clearCache = default, ExpressionMode expressionMode = default, int?expressionOrder = default, bool boundsAsParts = default, bool discardBounds = default, bool negate = default ) { Id = id; Name = name; Start = start; End = end; Parts = parts ?? new List <IMatcher>(); PartsDelimiter = partsDelimiter; PartsDelimiterRequired = partsDelimiterRequired; PartsPadding = partsPadding; MinMatchedParts = minMatchedParts; PartsMatchMode = partsMatchMode; IsNoise = isNoise; FallThrough = fallThrough; Cacheable = cacheable; ClearCache = clearCache; ExpressionMode = expressionMode; ExpressionOrder = expressionOrder; BoundsAsParts = boundsAsParts; DiscardBounds = discardBounds; Negate = negate; _hashCode = name.GetHashCode(); }
private static PatternMatch?TryMatchSingleWordPattern(string candidate, string pattern) { IList <TextSpan> spans; MarkupTestFile.GetSpans(candidate, out candidate, out spans); var match = new PatternMatcher(pattern).MatchSingleWordPattern_ForTestingOnly(candidate); if (match == null) { Assert.True(spans == null || spans.Count == 0); } else { Assert.Equal(match.Value.MatchedSpans, spans); } return(match); }
internal static async Task <ImmutableArray <SymbolAndProjectId> > FindSourceDeclarationsWithPatternInCurrentProcessAsync( Project project, string pattern, SymbolFilter criteria, CancellationToken cancellationToken) { // The compiler API only supports a predicate which is given a symbol's name. Because // we only have the name, and nothing else, we need to check it against the last segment // of the pattern. i.e. if the pattern is 'Console.WL' and we are given 'WriteLine', then // we don't want to check the whole pattern against it (as it will clearly fail), instead // we only want to check the 'WL' portion. Then, after we get all the candidate symbols // we'll check if the full name matches the full pattern. var patternMatcher = new PatternMatcher(pattern); using (var query = SearchQuery.CreateCustom( k => !patternMatcher.GetMatchesForLastSegmentOfPattern(k).IsDefaultOrEmpty)) { var symbolAndProjectIds = await SymbolFinder.FindSourceDeclarationsWithCustomQueryAsync( project, query, criteria, cancellationToken).ConfigureAwait(false); var result = ArrayBuilder <SymbolAndProjectId> .GetInstance(); // Now see if the symbols the compiler returned actually match the full pattern. foreach (var symbolAndProjectId in symbolAndProjectIds) { var symbol = symbolAndProjectId.Symbol; // As an optimization, don't bother getting the container for this symbol if this // isn't a dotted pattern. Getting the container could cause lots of string // allocations that we don't if we're never going to check it. var matches = !patternMatcher.IsDottedPattern ? new PatternMatches(patternMatcher.GetMatches(GetSearchName(symbol))) : patternMatcher.GetMatches(GetSearchName(symbol), GetContainer(symbol)); if (matches.IsEmpty) { // Didn't actually match the full pattern, ignore it. continue; } result.Add(symbolAndProjectId); } return(result.ToImmutableAndFree()); } }
private static IEnumerable<PatternMatch> TryMatchMultiWordPattern(string candidate, string pattern) { MarkupTestFile.GetSpans(candidate, out candidate, out ImmutableArray<TextSpan> expectedSpans); var matches = ArrayBuilder<PatternMatch>.GetInstance(); PatternMatcher.CreatePatternMatcher(pattern, includeMatchedSpans: true).AddMatches(candidate, matches); if (matches.Count == 0) { Assert.True(expectedSpans.Length == 0); return null; } else { var actualSpans = matches.SelectMany(m => m.MatchedSpans).OrderBy(s => s.Start).ToList(); Assert.Equal(expectedSpans, actualSpans); return matches; } }
public void MatchOpenThreePattenThenFound() { // Arrange var positions = new[] { new Position(0, 0), new Position(0, 1), new Position(0, 2), new Position(0, 3), new Position(0, 4) }; var pieces = new[] { PieceType.Empty, PieceType.P1, PieceType.P1, PieceType.P1, PieceType.Empty }; var context = new BoardProperties(); IBoard board = new Board(context); IPositions line = new FreeLine(positions); foreach (var i in Enumerable.Range(0, positions.Count())) { board.Set(positions[i], pieces[i]); } IPattern pattern = new Pattern(PatternType.OpenThree, PatternPositionType.Any, player: PieceType.P1, pieces: pieces); var matcher = new PatternMatcher(new PatternFactory().Create()); // Act var result = matcher.MatchInternal(board, line, new HashSet <IPattern> { pattern }); // Assert Assert.AreEqual(1, result.Count()); CollectionAssert.AreEqual(positions, result.First().Positions.ToList()); }
public override ILExpression BuildNew(PatternMatcher pm) { var res = pm.Operator; res.Arguments.Clear(); if (pm.SimpleLeftOperand) { res.Arguments.Add(pm.SimpleOperand); } res.Arguments.Add(VariableA.BuildNew(pm)); if (pm.B != null) { res.Arguments.Add(VariableB.BuildNew(pm)); } else if (pm.SimpleOperand != null && !pm.SimpleLeftOperand) { res.Arguments.Add(pm.SimpleOperand); } return(res); }
private static ImmutableArray <SearchResult> FilterPreviousResults( ImmutableArray <SearchResult> previousResults, PatternMatcher nameMatcher, PatternMatcher containerMatcherOpt, ArrayBuilder <PatternMatch> nameMatches, ArrayBuilder <PatternMatch> containerMatches, CancellationToken cancellationToken) { var result = ArrayBuilder <SearchResult> .GetInstance(); foreach (var previousResult in previousResults) { var document = previousResult.Document; var info = previousResult.DeclaredSymbolInfo; AddResultIfMatch( document, info, nameMatcher, containerMatcherOpt, nameMatches, containerMatches, result, cancellationToken); } return(result.ToImmutableAndFree()); }
private static IEnumerable <PatternMatch> TryMatchMultiWordPattern(string candidate, string pattern) { IList <TextSpan> expectedSpans; MarkupTestFile.GetSpans(candidate, out candidate, out expectedSpans); var matches = new PatternMatcher(pattern).GetMatches(candidate, includeMatchSpans: true); if (matches == null) { Assert.True(expectedSpans == null || expectedSpans.Count == 0); } else { var actualSpans = matches.SelectMany(m => m.MatchedSpans).OrderBy(s => s.Start).ToList(); Assert.Equal(expectedSpans, actualSpans); } return(matches); }
public override void Given() { fooSir = new Foo { IsOrderOfBritishEmpire = true }; fooPoitier = new Foo { IsSidney = true }; matcher = new PatternMatcher <Foo, Mar> { { KnightedFoosToBar, (Bar bar) => new Mar { SomeNumber = 999 } }, { MrTibbs, (Bar bar) => new Mar { SomeNumber = bar.Name.Length } }, { () => new Mar { SomeNumber = 0 } } //alternate to GetOrElse for default }; }
public void Search_UsingIntAndStringWithGenericPatternConfigurationAndSearchTextContainsPatches_ReturnsTwoPatternHits() { //Arrange var configuration = A.Fake<IConfiguration>(); A.CallTo(() => configuration.GetPatterns()).Returns(new List<String> { "<{0}>" }); A.CallTo(() => configuration.GetMappings()).Returns(new List<Map> { new Map("int", "Int32"), new Map("string", "String") }); var patternMatcher = new PatternMatcher(configuration); //Act var patternHits = patternMatcher.Search("var stats = new List<int>(); var messages = new List<string>();"); //Assert Assert.AreEqual(2, patternHits.Count()); }
private static IEnumerable <PatternMatch> TryMatch( string pattern, string[] dotSeparatedPatternComponents, PatternMatcher patternMatcher, DeclaredSymbolInfo declaredSymbolInfo) { var matches1 = TryGetDotSeparatedPatternMatches(patternMatcher, dotSeparatedPatternComponents, declaredSymbolInfo); var matches2 = patternMatcher.MatchPattern(GetSearchName(declaredSymbolInfo), pattern); if (matches1 == null) { return(matches2); } if (matches2 == null) { return(matches1); } return(matches1.Concat(matches2)); }
private static PatternMatch?TestNonFuzzyMatch(string candidate, string pattern) { MarkupTestFile.GetSpans(candidate, out candidate, out ImmutableArray <TextSpan> spans); var match = new PatternMatcher(pattern).GetFirstMatch(candidate, includeMatchSpans: true); if (match?.Kind == PatternMatchKind.Fuzzy) { match = null; } if (match == null) { Assert.True(spans.Length == 0); } else { Assert.Equal <TextSpan>(match.Value.MatchedSpans, spans); } return(match); }
private PatternMatcher GetPatternMatcher( string pattern, CultureInfo culture, Dictionary <CultureInfo, Dictionary <string, PatternMatcher> > map) { lock (_gate) { if (!map.TryGetValue(culture, out var innerMap)) { innerMap = new Dictionary <string, PatternMatcher>(); map[culture] = innerMap; } if (!innerMap.TryGetValue(pattern, out var patternMatcher)) { patternMatcher = new PatternMatcher(pattern, culture, verbatimIdentifierPrefixIsWordCharacter: true, allowFuzzyMatching: false); innerMap.Add(pattern, patternMatcher); } return(patternMatcher); } }
private string GenerateMatchFragmentBounds(MatcherEngineGenerator generator, PatternMatcher matcher) { string methodName = $"MatchFragmentBounds{GetSafeMethodName(matcher.Name)}"; string method = $"{methodName}(ref state, {{0}}, out {{1}})"; if (!generator.TryGetMethod(methodName, ref method)) { generator.Add(methodName, method); string code = $@"private bool {methodName}(ref State state, bool readOnly, out StringMatchData matchData) {{ bool success = {string.Format(generator.LanguageMatcher.GenerateMatchPattern(generator, matcher), "matchData", "true", "readOnly")}; if ({(!Negate ? "!" : null)}success) {{ state.FailureIndex = Math.Max(state.FailureIndex ?? 0, state.CurrentIndex); }} {(generator.LanguageMatcher.LogMatches ? $@"state.MatchLogBuilder.AppendLine($""{{new String('\t', state.Id + 1)}} {{state.CurrentIndex}}. {{(success ? ""Passed"" : ""Failed"")}} Bounds: {{""{HttpUtility.JavaScriptStringEncode(matcher.ToString())}""}}"");" : null)} return success; }}"; method = generator.Add(method, methodName, code); } return(method); }
public void PatternJudgeWontTakeHalfThreeAsFive() { var boardString = new[] { // 2345678910 " ", // 0 " O ", // 1 " X ", // 2 " XX X ", // 3 " XOOO ", // 4 " XXO ", // 5 " OOOX ", // 6 " ", // 7 " ", // 8 " ", // 9 " ", // 10 }; var context = new BoardProperties(boardString.Length, boardString[0].Length); var positions = new PositionFactory().Create(context); var patterns = new PatternFactory().Create(); var matcher = new PatternMatcher(patterns); var boardFactory = new BoardFactory(context, positions); var patternBoardFactory = new PatternBoardFactory(context, positions, matcher); var centerScorer = new CenterScorer(context, positions); var patternScorer = new PatternScorer(positions, patterns, matcher); var aggregatedScorer = new AggregatedScorer(new[] { new Tuple <IScorer, double>(patternScorer, 1), new Tuple <IScorer, double>(centerScorer, 0.01) }); var judge = new PatternJudge(positions, patterns, matcher); var board = Utils.ParseBoard(boardString, context, positions); var winner = judge.GetWinner(board); Assert.AreEqual(PieceType.Empty, winner); }
private static bool MatchPattern(string value, params string[] patterns) { if (patterns == null) { return(true); } if (string.IsNullOrWhiteSpace(value)) { return(false); } foreach (string p in patterns) { bool isMatch = PatternMatcher.StrictMatchPattern( p.ToUpper(CultureInfo.InvariantCulture), value.ToUpper(CultureInfo.InvariantCulture) ); if (isMatch) { return(true); } } return(false); }
private IEnumerable <IPlayer> GetAiPlayers(BoardProperties context, PositionManager positions, PieceType player) { var patterns = new PatternFactory().Create(); var matcher = new PatternMatcher(patterns); var patternBoardFactory = new PatternBoardFactory(context, positions, matcher); var centerScorer = new CenterScorer(context, positions); var patternScorer = new PatternScorer(positions, patterns, matcher); var aggregatedScorer = new AggregatedScorer(new[] { new Tuple <IScorer, double>(patternScorer, 1), new Tuple <IScorer, double>(centerScorer, 0.01) }); var judge = new BasicJudge(context, positions); var emptyMoveEnumerator = new EmptyPositionMoveEnumerator(positions); var scoredMoveEnumerator = new ScoredMoveEnumerator(positions, aggregatedScorer); return(new[] { new AbPruningAi(player, 1, aggregatedScorer, scoredMoveEnumerator, patternBoardFactory, judge), new AbPruningAi(player, 2, aggregatedScorer, scoredMoveEnumerator, patternBoardFactory, judge), new AbPruningAi(player, 3, aggregatedScorer, scoredMoveEnumerator, patternBoardFactory, judge) }); }
public override ILExpression BuildNew(PatternMatcher pm) { // boolean constants are wrapped inside a container to disable simplyfication of equality comparisons return new ILExpression(ILCode.Wrap, null, new ILExpression(ILCode.Ldc_I4, value)); }
public override bool Match(PatternMatcher pm, ILExpression e) { return e.Code == ILCode.Ldc_I4 && TypeAnalysis.IsBoolean(e.InferredType) && object.Equals(e.Operand, value); }
public override ILExpression BuildNew(PatternMatcher pm) { return pm.SimpleOperand; }
public override bool Match(PatternMatcher pm, ILExpression e) { if (pm.SimpleOperand != null) throw new InvalidOperationException(); pm.SimpleOperand = e; return true; }
public override void Initialize (object o) { _matcher = (PatternMatcher) o; }
public void GetCount_WhenSubstringNotPresent_ReturnsZero() { var sut = new PatternMatcher("abra kadabra"); Assert.AreEqual(0, sut.GetCount("z")); }
public override bool Match(PatternMatcher pm, ILExpression e) { switch (e.Code) { case ILCode.Ceq: if (type != OperatorType.Equality) return false; break; case ILCode.Cne: if (type != OperatorType.InEquality) return false; break; case ILCode.Cgt: case ILCode.Cgt_Un: case ILCode.Cge: case ILCode.Cge_Un: case ILCode.Clt: case ILCode.Clt_Un: case ILCode.Cle: case ILCode.Cle_Un: if (type != OperatorType.Comparison) return false; break; case ILCode.Add: case ILCode.Add_Ovf: case ILCode.Add_Ovf_Un: case ILCode.Sub: case ILCode.Sub_Ovf: case ILCode.Sub_Ovf_Un: case ILCode.Mul: case ILCode.Mul_Ovf: case ILCode.Mul_Ovf_Un: case ILCode.Div: case ILCode.Div_Un: case ILCode.Rem: case ILCode.Rem_Un: case ILCode.And: case ILCode.Or: case ILCode.Xor: case ILCode.Shl: case ILCode.Shr: case ILCode.Shr_Un: case ILCode.Not: case ILCode.Neg: case ILCode.LogicNot: if (type != OperatorType.Other) return false; break; case ILCode.Call: var m = e.Operand as MethodReference; if (m == null || m.HasThis || !m.HasParameters || e.Arguments.Count > 2 || !IsCustomOperator(m.Name)) return false; break; default: return false; } if (pm.Operator != null) throw new InvalidOperationException(); pm.Operator = e; var a0 = e.Arguments[0]; if (!simple) return VariableAGetValueOrDefault.Match(pm, a0) && VariableBGetValueOrDefault.Match(pm, e.Arguments[1]); if (e.Arguments.Count == 1) return VariableAGetValueOrDefault.Match(pm, a0); if (VariableAGetValueOrDefault.Match(pm, a0)) { pm.SimpleOperand = e.Arguments[1]; pm.SimpleLeftOperand = false; return true; } if (VariableAGetValueOrDefault.Match(pm, e.Arguments[1])) { pm.SimpleOperand = a0; pm.SimpleLeftOperand = true; return true; } return false; }
internal async Task<ImmutableArray<ISymbol>> FindNavigableSourceSymbolsAsync( Project project, CancellationToken cancellationToken) { var results = ArrayBuilder<ISymbol>.GetInstance(); // The compiler API only supports a predicate which is given a symbol's name. Because // we only have the name, and nothing else, we need to check it against the last segment // of the pattern. i.e. if the pattern is 'Console.WL' and we are given 'WriteLine', then // we don't want to check the whole pattern against it (as it will clearly fail), instead // we only want to check the 'WL' portion. Then, after we get all the candidate symbols // we'll check if the full name matches the full pattern. var patternMatcher = new PatternMatcher(_searchPattern); var symbols = await SymbolFinder.FindSourceDeclarationsAsync( project, k => !patternMatcher.GetMatchesForLastSegmentOfPattern(k).IsDefaultOrEmpty, SymbolFilter.TypeAndMember, cancellationToken).ConfigureAwait(false); symbols = symbols.Where(s => !s.IsConstructor() && !s.IsStaticConstructor() // not constructors, they get matched on type name && !(s is INamespaceSymbol) // not namespaces && s.Locations.Any(loc => loc.IsInSource)); // only source symbols foreach (var symbol in symbols) { cancellationToken.ThrowIfCancellationRequested(); // As an optimization, don't bother getting the container for this symbol if this // isn't a dotted pattern. Getting the container could cause lots of string // allocations that we don't if we're never going to check it. var matches = !patternMatcher.IsDottedPattern ? new PatternMatches(patternMatcher.GetMatches(GetSearchName(symbol))) : patternMatcher.GetMatches(GetSearchName(symbol), GetContainer(symbol)); if (matches.IsEmpty) { continue; } results.Add(symbol); // also report matching constructors (using same match result as type) var namedType = symbol as INamedTypeSymbol; if (namedType != null) { foreach (var constructor in namedType.Constructors) { // only constructors that were explicitly declared if (!constructor.IsImplicitlyDeclared) { results.Add(constructor); } } } // report both parts of partial methods var method = symbol as IMethodSymbol; if (method != null && method.PartialImplementationPart != null) { results.Add(method); } } return results.ToImmutableAndFree(); }
public void Search_UsingIntWithGenericPatternConfigurationAndNotGenericIntsUsed_ReturnsZeroPatternHits() { //Arrange var configuration = A.Fake<IConfiguration>(); A.CallTo(() => configuration.GetPatterns()).Returns(new List<String> { "<{0}>" }); A.CallTo(() => configuration.GetMappings()).Returns(new List<Map> { new Map("int", "Int32") }); var patternMatcher = new PatternMatcher(configuration); //Act var patternHits = patternMatcher.Search("int x = 0;"); //Assert Assert.AreEqual(0, patternHits.Count()); }
public override bool Match(PatternMatcher pm, ILExpression e) { if (e.Code != this.code) return false; var m = (MethodReference)e.Operand; return m.Name == this.method && TypeAnalysis.IsNullableType(m.DeclaringType) && base.Match(pm, e); }
private PatternMatcher GetPatternMatcher( string value, CultureInfo culture, Dictionary<CultureInfo, Dictionary<string, PatternMatcher>> map) { lock (_gate) { if (!map.TryGetValue(culture, out var innerMap)) { innerMap = new Dictionary<string, PatternMatcher>(); map[culture] = innerMap; } if (!innerMap.TryGetValue(value, out var patternMatcher)) { patternMatcher = new PatternMatcher(value, culture, verbatimIdentifierPrefixIsWordCharacter: true, allowFuzzyMatching: false); innerMap.Add(value, patternMatcher); } return patternMatcher; } }
private static IEnumerable<PatternMatch> TryMatchMultiWordPattern(string candidate, string pattern) { MarkupTestFile.GetSpans(candidate, out candidate, out IList<TextSpan> expectedSpans); var matches = new PatternMatcher(pattern).GetMatches(candidate, includeMatchSpans: true); if (matches.IsDefaultOrEmpty) { Assert.True(expectedSpans == null || expectedSpans.Count == 0); return null; } else { var actualSpans = matches.SelectMany(m => m.MatchedSpans).OrderBy(s => s.Start).ToList(); Assert.Equal(expectedSpans, actualSpans); return matches; } }
private static PatternMatch? TryMatchSingleWordPattern(string candidate, string pattern) { MarkupTestFile.GetSpans(candidate, out candidate, out IList<TextSpan> spans); var match = new PatternMatcher(pattern).MatchSingleWordPattern_ForTestingOnly(candidate); if (match == null) { Assert.True(spans == null || spans.Count == 0); } else { Assert.Equal<TextSpan>(match.Value.MatchedSpans, spans); } return match; }
public override ILExpression BuildNew(PatternMatcher pm) { var args = new ILExpression[this.Arguments.Length]; for (int i = 0; i < args.Length; i++) args[i] = this.Arguments[i].BuildNew(pm); TypeReference t = null; switch (code) { case ILCode.Ceq: case ILCode.Cne: t = pm.typeSystem.Boolean; break; case ILCode.NullCoalescing: t = args[1].InferredType; break; } return new ILExpression(code, null, args) { InferredType = t }; }
public override ILExpression BuildNew(PatternMatcher pm) { var v = this.b ? pm.B : pm.A; var e = new ILExpression(ILCode.Ldloc, v, EmptyArguments); if (TypeAnalysis.IsNullableType(v.Type)) e = new ILExpression(ILCode.ValueOf, null, e); return e; }
public override bool Match(PatternMatcher pm, ILExpression e) { return e.Code == this.code && base.Match(pm, e); }
public override void Given() { fooSir = new Foo { IsOrderOfBritishEmpire = true }; fooPoitier = new Foo { IsSidney = true}; matcher = new PatternMatcher<Foo, Mar> { {KnightedFoosToBar, (Bar bar) => new Mar {SomeNumber = 999}}, {MrTibbs, (Bar bar) => new Mar {SomeNumber = bar.Name.Length}}, {()=>new Mar{SomeNumber = 0}} //alternate to GetOrElse for default }; }
public override ILExpression BuildNew(PatternMatcher pm) { var res = pm.Operator; res.Arguments.Clear(); if (pm.SimpleLeftOperand) res.Arguments.Add(pm.SimpleOperand); res.Arguments.Add(VariableA.BuildNew(pm)); if (pm.B != null) res.Arguments.Add(VariableB.BuildNew(pm)); else if (pm.SimpleOperand != null && !pm.SimpleLeftOperand) res.Arguments.Add(pm.SimpleOperand); return res; }
public void GetIndexes_WhenSubstringNotPresent_ReturnsEmptyCollection() { var sut = new PatternMatcher("abra kadabra"); Assert.AreEqual(0, sut.GetIndexes("z").Count); }
public virtual ILExpression BuildNew(PatternMatcher pm) { throw new NotSupportedException(); }
public void GetIndexes_WhenGivenNullSubstring_ThrowsArgumentNullException() { var sut = new PatternMatcher("abra kadabra"); Assert.Throws<ArgumentNullException>(() => sut.GetIndexes(null)); }
public override bool Match(PatternMatcher pm, ILExpression e) { if (e.Code != this.code) return false; var v = e.Operand as ILVariable; return v != null && (this.b ? Capture(ref pm.B, v) : Capture(ref pm.A, v)); }
public override bool Match(PatternMatcher pm, ILExpression e) { return e.Code == ILCode.Ldc_I4 && e.InferredType.GetElementType() == ElementType.Boolean && object.Equals(e.Operand, value); }