public SectionInfo(string name, bool inverted, bool expression) { this.name = name; this.inverted = inverted; this.parts = new Collection <ITemplatePart>(); this.properties = new Dictionary <string, object>(); this.jsonKeys = new Collection <string>(); if (expression) { Match match = ExpressionRegex.Match(name); if (match.Success) { this.name = match.Groups[1].Value.Trim() + ":" + match.Groups[2].Value.Trim(); string values = match.Groups[3].Value.Trim(); IEnumerable <KeyValuePair <string, string> > attributesMatches = ExpressionParamRegex1.Matches(values) .Cast <Match>() .Concat(ExpressionParamRegex2.Matches(values).Cast <Match>()) .Where( x => x.Success && !string.IsNullOrWhiteSpace(x.Groups[1].Value) && !string.IsNullOrWhiteSpace(x.Groups[2].Value)) .Select(x => new KeyValuePair <string, string>(x.Groups[1].Value, x.Groups[2].Value)); IEnumerable <KeyValuePair <string, string> > jsonMatches = ExpressionParamRegex3.Matches(values) .Cast <Match>() .Concat(ExpressionParamRegex4.Matches(values).Cast <Match>()) .Where( x => x.Success && !string.IsNullOrWhiteSpace(x.Groups[1].Value) && !string.IsNullOrWhiteSpace(x.Groups[2].Value)) .Select(x => new KeyValuePair <string, string>(x.Groups[1].Value, x.Groups[2].Value)); foreach (var attributesMatch in attributesMatches) { if (!this.properties.ContainsKey(attributesMatch.Key)) { this.properties.Add(attributesMatch.Key, attributesMatch.Value); } } foreach (var jsonMatch in jsonMatches) { if (!this.jsonKeys.Contains(jsonMatch.Key)) { this.jsonKeys.Add(jsonMatch.Key); this.properties.Add(jsonMatch.Key, jsonMatch.Value); } } } } }
public static IEnumerable <T> Parse <T>(string expression, Func <string, T> propertySelector, Func <object, T> indexSelector) { if (propertySelector == null) { throw new ArgumentNullException("propertySelector"); } if (indexSelector == null) { throw new ArgumentNullException("indexSelector"); } expression = (expression ?? string.Empty).Trim(); if (expression.Length == 0) { yield break; } var match = ExpressionRegex.Match(expression); if (!match.Success) { throw new FormatException(null); } var accessors = from Capture c in match.Groups["a"].Captures select c.Value.TrimStart('.') into text let isIndexer = text[0] == '[' || text[0] == '(' select new { Text = text, IsIndexer = isIndexer }; var indexes = from Capture c in match.Groups["i"].Captures select c.Value into text select text[0] == '\'' || text[0] == '\"' ? (object)text.Substring(1, text.Length - 2) : int.Parse(text, NumberStyles.Integer, CultureInfo.InvariantCulture); using (var i = indexes.GetEnumerator()) foreach (var a in accessors) { if (a.IsIndexer) { i.MoveNext(); } yield return(a.IsIndexer ? indexSelector(i.Current) : propertySelector(a.Text)); } }