Exemple #1
0
        private Filter <T> MatchAndFilter(FilterLexer lexer)
        {
            var        filters     = new List <Filter <T> >();
            Filter <T> firstFilter = MatchNegationFilter(lexer);

            filters.Add(firstFilter);

            FilterToken nextToken = LookAhead(lexer, 1);

            while (nextToken != null && nextToken.Type == FilterTokenType.And)
            {
                GetNextToken(lexer);
                filters.Add(MatchNegationFilter(lexer));
                nextToken = LookAhead(lexer, 1);
            }

            if (filters.Count > 1)
            {
                return(new AndFilter <T>(filters));
            }
            else
            {
                return(firstFilter);
            }
        }
Exemple #2
0
        private static Filter <string> MatchValue(FilterLexer lexer)
        {
            FilterToken nextToken = LookAhead(lexer, 1);

            if (nextToken != null)
            {
                if (nextToken.Type == FilterTokenType.RegexWord)
                {
                    GetNextToken(lexer);
                    RegexOptions options = RegexOptions.CultureInvariant;
                    FilterToken  caseInsensitiveToken = LookAhead(lexer, 1);
                    if (caseInsensitiveToken != null && caseInsensitiveToken.Type == FilterTokenType.CaseInsensitiveModifier)
                    {
                        options |= RegexOptions.IgnoreCase;
                        GetNextToken(lexer);
                    }
                    return(new RegexFilter(new Regex(nextToken.Text, options)));
                }
                else if (IsWord(nextToken))
                {
                    GetNextToken(lexer);
                    return(new EqualityFilter <string>(nextToken.Text));
                }
            }

            throw new FilterParseException(Resources.FilterParser_ValueExpected);
        }
Exemple #3
0
        private static FilterToken LookAhead(FilterLexer lexer, int index)
        {
            FilterToken token = lexer.LookAhead(index);

            if (token != null && token.Type == FilterTokenType.Error)
            {
                throw new FilterParseException(token.Text);
            }
            return(token);
        }
Exemple #4
0
        private static void MatchRightBracket(FilterLexer lexer)
        {
            FilterToken nextToken = LookAhead(lexer, 1);

            if (nextToken == null || nextToken.Type != FilterTokenType.RightBracket)
            {
                throw new FilterParseException(Resources.FilterParser_RightBracketExpected);
            }
            GetNextToken(lexer);
        }
Exemple #5
0
        private static void MatchComma(FilterLexer lexer)
        {
            FilterToken nextToken = LookAhead(lexer, 1);

            if (nextToken == null || nextToken.Type != FilterTokenType.Comma)
            {
                // Should never happen because we call this method when we know a comma
                // token is next
                throw new FilterParseException(Resources.FilterParser_CommaExpected);
            }
            GetNextToken(lexer);
        }
Exemple #6
0
        private Filter <T> MatchNegationFilter(FilterLexer lexer)
        {
            FilterToken nextToken = LookAhead(lexer, 1);

            if (nextToken != null && nextToken.Type == FilterTokenType.Not)
            {
                GetNextToken(lexer);
                return(new NotFilter <T>(MatchNegationFilter(lexer)));
            }

            return(MatchParenthesizedFilter(lexer));
        }
Exemple #7
0
        private static string MatchKey(FilterLexer lexer)
        {
            FilterToken nextToken = LookAhead(lexer, 1);

            if (nextToken == null || IsNotWord(nextToken))
            {
                // Should never happen because we call this method when we know a word
                // token is next
                throw new FilterParseException(Resources.FilterParser_StringLiteralExpected);
            }
            GetNextToken(lexer);

            return(nextToken.Text);
        }
Exemple #8
0
        private Filter <T> MatchParenthesizedFilter(FilterLexer lexer)
        {
            Filter <T>  filter    = null;
            FilterToken nextToken = LookAhead(lexer, 1);

            if (nextToken != null)
            {
                if (nextToken.Type == FilterTokenType.LeftBracket)
                {
                    GetNextToken(lexer);
                    filter = MatchOrFilter(lexer);
                    MatchRightBracket(lexer);
                }
                else
                {
                    filter = MatchSimpleFilter(lexer);
                }
            }

            return(filter);
        }
Exemple #9
0
        private Filter <T> MatchSimpleFilter(FilterLexer lexer)
        {
            FilterToken nextToken = LookAhead(lexer, 1);

            if (nextToken != null)
            {
                if (nextToken.Type == FilterTokenType.Star)
                {
                    GetNextToken(lexer);
                    return(new AnyFilter <T>());
                }
                if (IsWord(nextToken))
                {
                    string key = MatchKey(lexer);
                    MatchColon(lexer);
                    Filter <string> valueFilter = MatchMatchSequence(lexer);
                    return(factory.CreateFilter(key, valueFilter));
                }
            }

            throw new FilterParseException(Resources.FilterParser_FilterExpressionExpected);
        }
Exemple #10
0
        private FilterSet <T> MatchFilterSet(FilterLexer lexer)
        {
            var filterRules = new List <FilterRule <T> >();

            FilterToken nextToken = LookAhead(lexer, 1);

            while (nextToken != null)
            {
                FilterRuleType filterRuleType;
                if (nextToken.Type == FilterTokenType.Include)
                {
                    filterRuleType = FilterRuleType.Inclusion;
                    GetNextToken(lexer);
                }
                else if (nextToken.Type == FilterTokenType.Exclude)
                {
                    filterRuleType = FilterRuleType.Exclusion;
                    GetNextToken(lexer);
                }
                else if (filterRules.Count == 0)
                {
                    // default to include for first filter
                    filterRuleType = FilterRuleType.Inclusion;
                }
                else
                {
                    throw new FilterParseException(Resources.FilterParser_RulesNotSeperated);
                }

                Filter <T> filter = MatchOrFilter(lexer);
                filterRules.Add(new FilterRule <T>(filterRuleType, filter));

                nextToken = LookAhead(lexer, 1);
            }

            return(new FilterSet <T>(filterRules));
        }
Exemple #11
0
        private static Filter <string> MatchMatchSequence(FilterLexer lexer)
        {
            var valueFilters = new List <Filter <string> >();

            valueFilters.Add(MatchValue(lexer));

            FilterToken nextToken = LookAhead(lexer, 1);

            while (nextToken != null && nextToken.Type == FilterTokenType.Comma)
            {
                MatchComma(lexer);
                valueFilters.Add(MatchValue(lexer));
                nextToken = LookAhead(lexer, 1);
            }

            if (valueFilters.Count == 1)
            {
                return(valueFilters[0]);
            }
            else
            {
                return(new OrFilter <string>(valueFilters));
            }
        }
Exemple #12
0
 private static bool IsNotWord(FilterToken token)
 {
     return(!IsWord(token));
 }
Exemple #13
0
 private static bool IsWord(FilterToken token)
 {
     return(token.Type == FilterTokenType.Word || token.Type == FilterTokenType.RegexWord);
 }