private static void Initialize() { _patternStyle = new PatternStyle { TokenMatchPattern = @"\{[a-zA-Z]\w*\}", TokenReplaceFilter = token => token.Replace("{", "").Replace("}", ""), OutputFilter = output => (output.StartsWith("{") && output.EndsWith("}") ? output : @"\{" + output + @"\}"), TokenFilter = tokens => "{(" + tokens + ")}" }; }
private static string Explode(this string source, Regex pattern, PatternStyle patternStyle, Func <string, string> expansionFactory, TreeNode <string> parent) { var output = source; while (output.HasChildren(pattern)) { foreach (Match match in pattern.Matches(source)) { var child = match.Value; var token = patternStyle.TokenReplaceFilter(match.Value); var thisNode = parent.Children.Add(token); // if we have already encountered this token in this call tree, we have a circular reference if (thisNode.CallTree.Contains(token)) { throw new CircularReferenceException(string.Format("Circular Reference Detected for token '{0}'. Call Tree: {1}->{2}", token, string.Join("->", thisNode.CallTree.ToArray().Reverse()), token)); } // expand this match var expandedValue = expansionFactory(token); // Replace the match with the expanded value child = Regex.Replace(child, patternStyle.OutputFilter(match.Value), expandedValue); // Recursively expand the child until we no longer encounter nested tokens (or hit a circular reference) child = child.Explode(pattern, patternStyle, expansionFactory, thisNode); // finally, replace the match in the output with the fully-expanded value output = Regex.Replace(output, patternStyle.OutputFilter(match.Value), child); } } return(output); }
private static string Explode(this string source, Regex pattern, PatternStyle patternStyle, Func<string, string> expansionFactory, TreeNode<string> parent) { var output = source; while (output.HasChildren(pattern)) { foreach (Match match in pattern.Matches(source)) { var child = match.Value; var token = patternStyle.TokenReplaceFilter(match.Value); var thisNode = parent.Children.Add(token); // if we have already encountered this token in this call tree, we have a circular reference if (thisNode.CallTree.Contains(token)) throw new CircularReferenceException(string.Format("Circular Reference Detected for token '{0}'. Call Tree: {1}->{2}", token, string.Join("->", thisNode.CallTree.ToArray().Reverse()), token)); // expand this match var expandedValue = expansionFactory(token); // Replace the match with the expanded value child = Regex.Replace(child, patternStyle.OutputFilter(match.Value), expandedValue); // Recursively expand the child until we no longer encounter nested tokens (or hit a circular reference) child = child.Explode(pattern, patternStyle, expansionFactory, thisNode); // finally, replace the match in the output with the fully-expanded value output = Regex.Replace(output, patternStyle.OutputFilter(match.Value), child); } } return output; }
private static void Initialize() { _patternStyle = new PatternStyle { TokenMatchPattern = @"\{[a-zA-Z]\w*\}", TokenReplaceFilter = token => token.Replace("{", "").Replace("}", ""), OutputFilter = output => (output.StartsWith("{") && output.EndsWith("}") ? output : @"\{" + output + @"\}"), TokenFilter = tokens => "{(" + tokens + ")}" }; }