public void It_will_compile_a_single_language_rule_with_partial_captures() { Dictionary <string, CompiledLanguage> stubCompiledLanguagesCache = new Dictionary <string, CompiledLanguage>(); LanguageCompiler languageCompiler = new LanguageCompiler(stubCompiledLanguagesCache); StubLanguage stubLanguage = new StubLanguage(); stubLanguage.id__getValue = "fnord"; stubLanguage.name__getValue = "fnord"; stubLanguage.rules__getValue = new List <LanguageRule> { new LanguageRule("(a) (language) (rule)", new Dictionary <int, string> { { 1, "style for the a part" }, { 2, "style for the language part" }, { 3, "style for the rule part" } }) }; CompiledLanguage compiledLanguage = languageCompiler.Compile(stubLanguage); Assert.Equal(@"(?x) (?-xis)(?m)((a) (language) (rule))(?x)", compiledLanguage.Regex.ToString()); Assert.Null(compiledLanguage.Captures[0]); Assert.Null(compiledLanguage.Captures[1]); Assert.Equal("style for the a part", compiledLanguage.Captures[2]); Assert.Equal("style for the language part", compiledLanguage.Captures[3]); Assert.Equal("style for the rule part", compiledLanguage.Captures[4]); }
public void It_will_retrieve_a_previously_compiled_language_from_the_cache_when_one_is_there() { Dictionary <string, CompiledLanguage> stubCompiledLanguagesCache = new Dictionary <string, CompiledLanguage>(); CompiledLanguage stubCompiledLanguage = new CompiledLanguage("theLanguageId", "fnord", new Regex("fnord"), new List <string> { "fnord" }); stubCompiledLanguagesCache.Add(stubCompiledLanguage.Id, stubCompiledLanguage); LanguageCompiler languageCompiler = new LanguageCompiler(stubCompiledLanguagesCache); StubLanguage stubLanguage = new StubLanguage(); stubLanguage.id__getValue = "theLanguageId"; stubLanguage.name__getValue = "fnord"; stubLanguage.rules__getValue = new List <LanguageRule> { new LanguageRule("fnord", new Dictionary <int, string> { { 0, "fnord" } }) }; CompiledLanguage compiledLanguage = languageCompiler.Compile(stubLanguage); Assert.Equal("theLanguageId", compiledLanguage.Id); Assert.False(stubLanguage.Name__getInvoked); Assert.False(stubLanguage.Rules__getInvoked); }
public void It_will_set_the_compiled_language_identifier_and_name_and_regex_and_capture() { const string id = "theId"; const string name = "The ScopeName"; Regex regex = new Regex("theRegex"); List<string> captures = new List<string>{ "theFirstCapture" }; CompiledLanguage compiledLanguage = new CompiledLanguage(id, name, regex, captures); Assert.Equal("theId", compiledLanguage.Id); Assert.Equal("The ScopeName", compiledLanguage.Name); Assert.Equal("theRegex", compiledLanguage.Regex.ToString()); Assert.Equal("theFirstCapture", compiledLanguage.Captures[0]); }
public void It_will_return_the_name() { const string id = "fnord"; const string name = "The ScopeName"; Regex regex = new Regex("fnord"); List <string> captures = new List <string> { "fnord" }; CompiledLanguage compiledLanguage = new CompiledLanguage(id, name, regex, captures); string toString = compiledLanguage.ToString(); Assert.Equal("The ScopeName", toString); }
public void It_will_set_the_compiled_language_identifier_and_name_and_regex_and_capture() { const string id = "theId"; const string name = "The ScopeName"; Regex regex = new Regex("theRegex"); List <string> captures = new List <string> { "theFirstCapture" }; CompiledLanguage compiledLanguage = new CompiledLanguage(id, name, regex, captures); Assert.Equal("theId", compiledLanguage.Id); Assert.Equal("The ScopeName", compiledLanguage.Name); Assert.Equal("theRegex", compiledLanguage.Regex.ToString()); Assert.Equal("theFirstCapture", compiledLanguage.Captures[0]); }
public void It_will_use_the_language_name_for_the_compiled_language_name() { Dictionary <string, CompiledLanguage> stubCompiledLanguagesCache = new Dictionary <string, CompiledLanguage>(); LanguageCompiler languageCompiler = new LanguageCompiler(stubCompiledLanguagesCache); StubLanguage stubLanguage = new StubLanguage(); stubLanguage.id__getValue = "fnord"; stubLanguage.name__getValue = "The Language ScopeName"; stubLanguage.rules__getValue = new List <LanguageRule> { new LanguageRule("fnord", new Dictionary <int, string> { { 0, "fnord" } }) }; CompiledLanguage compiledLanguage = languageCompiler.Compile(stubLanguage); Assert.Equal("The Language ScopeName", compiledLanguage.Name); }
public void It_will_compile_a_single_language_rule_with_a_whole_capture() { Dictionary <string, CompiledLanguage> stubCompiledLanguagesCache = new Dictionary <string, CompiledLanguage>(); LanguageCompiler languageCompiler = new LanguageCompiler(stubCompiledLanguagesCache); StubLanguage stubLanguage = new StubLanguage(); stubLanguage.id__getValue = "fnord"; stubLanguage.name__getValue = "fnord"; stubLanguage.rules__getValue = new List <LanguageRule> { new LanguageRule("a language rule", new Dictionary <int, string> { { 0, "style for whole rule" } }) }; CompiledLanguage compiledLanguage = languageCompiler.Compile(stubLanguage); Assert.Equal(@"(?x) (?-xis)(?m)(a language rule)(?x)", compiledLanguage.Regex.ToString()); Assert.Null(compiledLanguage.Captures[0]); Assert.Equal("style for whole rule", compiledLanguage.Captures[1]); }
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; }
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]; if (regexGroup.Length > 0 && i < compiledLanguage.Captures.Count) { //note: i can be >= Captures.Count due to named groups; these do capture a group but always get added after all non-named groups (which is why we do not count them in numberOfCaptures) string styleName = compiledLanguage.Captures[i]; if (!String.IsNullOrEmpty(styleName)) { foreach (Capture regexCapture in regexGroup.Captures) AppendCapturedStylesForRegexCapture(regexCapture, currentIndex, styleName, capturedStyles); } } } return capturedStyles; }
public void It_will_return_the_name() { const string id = "fnord"; const string name = "The ScopeName"; Regex regex = new Regex("fnord"); List<string> captures = new List<string> { "fnord" }; CompiledLanguage compiledLanguage = new CompiledLanguage(id, name, regex, captures); string toString = compiledLanguage.ToString(); Assert.Equal("The ScopeName", toString); }