private void Parse(string sourceCode, CompiledLanguage compiledLanguage, Action<string, IList<Scope>> parseHandler) { Match regexMatch = compiledLanguage.Regex.Match(sourceCode); if (!regexMatch.Success) parseHandler(sourceCode, new List<Scope>()); else { int currentIndex = 0; while (regexMatch.Success) { string sourceCodeBeforeMatch = sourceCode.Substring(currentIndex, regexMatch.Index - currentIndex); if (!string.IsNullOrEmpty(sourceCodeBeforeMatch)) parseHandler(sourceCodeBeforeMatch, new List<Scope>()); string matchedSourceCode = sourceCode.Substring(regexMatch.Index, regexMatch.Length); if (!string.IsNullOrEmpty(matchedSourceCode)) { List<Scope> capturedStylesForMatchedFragment = GetCapturedStyles(regexMatch, regexMatch.Index, compiledLanguage); List<Scope> capturedStyleTree = CreateCapturedStyleTree(capturedStylesForMatchedFragment); parseHandler(matchedSourceCode, capturedStyleTree); } currentIndex = regexMatch.Index + regexMatch.Length; regexMatch = regexMatch.NextMatch(); } string sourceCodeAfterAllMatches = sourceCode.Substring(currentIndex); if (!string.IsNullOrEmpty(sourceCodeAfterAllMatches)) parseHandler(sourceCodeAfterAllMatches, new List<Scope>()); } }
private List<Scope> GetCapturedStyles(Match regexMatch, int currentIndex, CompiledLanguage compiledLanguage) { var capturedStyles = new List<Scope>(); for (int i = 0; i < regexMatch.Groups.Count; i++) { Group regexGroup = regexMatch.Groups[i]; string styleName = compiledLanguage.Captures[i]; if (regexGroup.Length == 0 || String.IsNullOrEmpty(styleName)) continue; else foreach (Capture regexCapture in regexGroup.Captures) AppendCapturedStylesForRegexCapture(regexCapture, currentIndex, styleName, capturedStyles); } return capturedStyles; }