Example #1
0
		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>());
			}
		}
Example #2
0
        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);
        }
Example #3
0
        public void Parse(string sourceCode,
                          ILanguage language,
                          Action <string, IList <Scope> > parseHandler)
        {
            if (string.IsNullOrEmpty(sourceCode))
            {
                return;
            }

            CompiledLanguage compiledLanguage = languageCompiler.Compile(language);

            Parse(sourceCode, compiledLanguage, parseHandler);
        }
Example #4
0
        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>());
                }
            }
        }
Example #5
0
        void AppendCapturedStylesForNestedLanguage(Capture regexCapture,
                                                   int offset,
                                                   string nestedLanguageId,
                                                   ICollection <Scope> capturedStyles)
        {
            ILanguage nestedLanguage = languageRepository.FindById(nestedLanguageId);

            if (nestedLanguage == null)
            {
                throw new InvalidOperationException("The nested language was not found in the language repository.");
            }
            else
            {
                CompiledLanguage nestedCompiledLanguage = languageCompiler.Compile(nestedLanguage);

                Match regexMatch = nestedCompiledLanguage.Regex.Match(regexCapture.Value, 0, regexCapture.Value.Length);

                if (!regexMatch.Success)
                {
                    return;
                }
                else
                {
                    while (regexMatch.Success)
                    {
                        List <Scope> capturedStylesForMatchedFragment = GetCapturedStyles(regexMatch, 0, nestedCompiledLanguage);
                        List <Scope> capturedStyleTree = CreateCapturedStyleTree(capturedStylesForMatchedFragment);

                        foreach (Scope nestedCapturedStyle in capturedStyleTree)
                        {
                            IncreaseCapturedStyleIndicies(capturedStyleTree, offset);
                            capturedStyles.Add(nestedCapturedStyle);
                        }

                        regexMatch = regexMatch.NextMatch();
                    }
                }
            }
        }
Example #6
0
		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;
		}