Exemplo n.º 1
0
        public QueryResult Run()
        {
            _expander.Expand();

            var patterns = QueryPattern.ParseFrom(this.Pattern);

            var results = new QueryResult()
            {
                Query = this
            };

            foreach (var item in _expander.Results)
            {
                foreach (var pattern in patterns)
                {
                    if (pattern.Matches(item))
                    {
                        results.Add(item);
                        break;
                    }
                }
            }

            return(results);
        }
Exemplo n.º 2
0
        public static IEnumerable <QueryPattern> ParseFrom(string text)
        {
            StringBuilder string_builder = new StringBuilder();

            foreach (var segment in text
                     .Split(new[] { "&&" }, StringSplitOptions.RemoveEmptyEntries)
                     .Select(x => x.Trim())
                     )
            {
                var pattern = new QueryPattern();
                var state   = State.ReadingFullName;

                var characters = segment.ToCharArray();
                for (var x = 0; x < characters.Length; x++)
                {
                    var character = characters[x];

                    switch (character)
                    {
                    case '[':
                        state = State.ReadingAssembly;
                        continue;

                    case ']':
                        state = State.ReadingFullName;
                        continue;

                    case '*':
                        pattern.StartsWith = x > 0;
                        pattern.EndsWith   = x == 0;
                        continue;

                    case '(':
                        state = State.ReadingParameters;
                        string_builder.Clear();
                        continue;

                    case ')':
                        if (state == State.ReadingParameters)
                        {
                            var parameter_type = string_builder.ToString();
                            string_builder.Clear();

                            pattern._parameters.Add(parameter_type);
                        }
                        state = State.NotSupported;
                        continue;

                    case ',':
                        if (state == State.ReadingParameters)
                        {
                            // complete
                            var parameter_type = string_builder.ToString();
                            string_builder.Clear();

                            pattern._parameters.Add(parameter_type);
                        }
                        continue;

                    default:
                        break;
                    }

                    switch (state)
                    {
                    case State.ReadingAssembly:

                        if (pattern.AssemblyName == null)
                        {
                            pattern.AssemblyName = character.ToString();
                        }
                        else
                        {
                            pattern.AssemblyName += character;
                        }

                        break;

                    case State.ReadingFullName:

                        if (pattern.FullName == null)
                        {
                            pattern.FullName = character.ToString();
                        }
                        else
                        {
                            pattern.FullName += character;
                        }

                        break;

                    case State.ReadingParameters:
                        string_builder.Append(character);
                        break;

                    case State.NotSupported:
                        throw new Exception("Raw query patterns do not (yet) support IL parsing");
                    }
                }

                if (pattern.Parameters.Any())
                {
                    pattern.FullName += $"({String.Join(",", pattern.Parameters)})";
                }

                yield return(pattern);
            }
        }