private static void Tokenize(List <Token> tokens, Type dataType, Type defaultCriterionType) { if (!typeof(FilterCriterion).IsAssignableFrom(defaultCriterionType)) { throw new DataGridInternalException("The default criterion type should be derived from the FilterCriterion class."); } List <int> parserPriorities = new List <int>(); FilterParser.FillParserPriorityList(parserPriorities); FilterParser.Tokenize(parserPriorities, tokens); if ((tokens.Count == 1) && (tokens[0] is ValueToken)) { if (defaultCriterionType == typeof(ContainsFilterCriterion)) { // ContainsFilterCriterion has no keyword associated; it won't be handled when // building the criterion. Initialize it with the Value already in it. FilterCriterionToken containsToken = new FilterCriterionToken(( FilterCriterion )Activator.CreateInstance(defaultCriterionType, (( ValueToken )tokens[0]).Value)); tokens.Clear(); tokens.Add(containsToken); } else { // Insert the default token previous to the value. FilterCriterionToken defaultToken = new FilterCriterionToken(( FilterCriterion )Activator.CreateInstance(defaultCriterionType)); tokens.Insert(0, defaultToken); } } }
private static FilterCriterion BuildCriterion(List <Token> tokens, Type dataType, Type defaultCriterionType, CultureInfo culture) { FilterCriterion filterCriterion = null; ReferenceList <FilterCriterion> initializedCriteria = new ReferenceList <FilterCriterion>(); List <int> precedences = new List <int>(); FilterParser.FillPrecedenceList(precedences); for (int index = 0; index < precedences.Count; index++) { int curOperatorPrecedence = precedences[index]; foreach (Type type in FilterParser.RegisteredFilterCriterionTypes) { CriterionDescriptorAttribute attribute = ( CriterionDescriptorAttribute )type.GetCustomAttributes(typeof(CriterionDescriptorAttribute), true)[0]; if (( int )attribute.OperatorPrecedence == curOperatorPrecedence) { if (!string.IsNullOrEmpty(attribute.Pattern)) { bool tokenConsumed; do { tokenConsumed = false; for (int i = 0; i < tokens.Count; i++) { FilterCriterionToken filterToken = tokens[i] as FilterCriterionToken; if ((filterToken != null) && (filterToken.FilterCriterion.GetType() == type) && (!initializedCriteria.Contains(filterToken.FilterCriterion))) { List <object> parameters = new List <object>(); if (attribute.Pattern.StartsWith("@")) { if (i <= 0) { throw new DataGridException(string.Format(FilterParser.MissingLeftOperandErrorText, type.Name)); } if (tokens[i - 1] is ValueToken) { FilterParser.AddValueToParameters(parameters, ( ValueToken )tokens[i - 1], dataType, culture); } else { parameters.Add((( FilterCriterionToken )tokens[i - 1]).FilterCriterion); } tokens.RemoveAt(i - 1); i--; } if (attribute.Pattern.EndsWith("@")) { if (i + 1 >= tokens.Count) { throw new DataGridException(string.Format(FilterParser.MissingRightOperandErrorText, type.Name)); } if (tokens[i + 1] is ValueToken) { FilterParser.AddValueToParameters(parameters, ( ValueToken )tokens[i + 1], dataType, culture); } else { parameters.Add((( FilterCriterionToken )tokens[i + 1]).FilterCriterion); } tokens.RemoveAt(i + 1); i--; } filterToken.FilterCriterion.InitializeFrom(parameters.ToArray(), defaultCriterionType); initializedCriteria.Add(filterToken.FilterCriterion); tokenConsumed = true; break; } } }while(tokenConsumed); } else { if (type != typeof(ContainsFilterCriterion)) { throw new DataGridInternalException("Missing pattern in attribute: " + type.Name); } } } } } if ((tokens.Count == 1) && (tokens[0] is FilterCriterionToken)) { filterCriterion = (( FilterCriterionToken )tokens[0]).FilterCriterion; } else { throw new DataGridException(FilterParser.InvalidExpressionErrorText); } return(filterCriterion); }
//private static bool ExtractFirstCriterion( List<Token> rawTokens, int tokenIndex, int parserPriority, out FilterCriterionToken filterToken, out int startIndex, out int length ) private static bool ExtractFirstCriterion(Token precedentToken, RawToken rawToken, int parserPriority, out FilterCriterionToken filterToken, out int startIndex, out int length) { //RawToken rawToken = rawTokens[ tokenIndex ] as RawToken; Type foundCriterionType = null; filterToken = null; startIndex = int.MaxValue; length = 0; foreach (Type type in FilterParser.RegisteredFilterCriterionTypes) { CriterionDescriptorAttribute attribute = ( CriterionDescriptorAttribute )type.GetCustomAttributes(typeof(CriterionDescriptorAttribute), true)[0]; if (( int )attribute.ParserPriority == parserPriority) { if (!string.IsNullOrEmpty(attribute.Pattern)) { string separator = attribute.Pattern.Replace("@", ""); int foundIndex = -1; if (attribute.Pattern.StartsWith("@")) { // The keyword can be anywhere in the token (there can be characters // before the token). foundIndex = rawToken.Value.IndexOf(separator); } //else if( ( rawTokens[ tokenIndex + 1 ] as AtomicStringToken ) != null ) //{ //} else { // The keyword must be the first non whitespace character. if (rawToken.Value.TrimStart(null).StartsWith(separator)) { foundIndex = rawToken.Value.IndexOf(separator); } } if ((foundIndex >= 0) && ((foundIndex < startIndex) || ((foundIndex == startIndex) && (separator.Length > length)))) { if (type == typeof(EndsWithFilterCriterion) && (precedentToken as AtomicStringToken) != null) { foundCriterionType = typeof(StartsWithFilterCriterion); } else { foundCriterionType = type; } startIndex = foundIndex; length = separator.Length; } } else { if (type != typeof(ContainsFilterCriterion)) { throw new DataGridInternalException("Missing pattern in attribute: " + type.Name); } } } } if (foundCriterionType != null) { filterToken = new FilterCriterionToken(( FilterCriterion )Activator.CreateInstance(foundCriterionType)); } return(filterToken != null); }