public string[] ExportOptions( ) { DRegexOptions options = OptionsControl.GetSelectedOptions( ); var json = JsonSerializer.Serialize(options); return(new[] { $"json:{json}" }); }
Regex CreateHighlightingRegex(DRegexOptions options) { var pb = new PatternBuilder( ); pb.Add(@"\(\?\#.*?(\)|$)"); // inline comment pb.Add(@"(?'left_par'\()"); // '(' pb.Add(@"(?'right_par'\))"); // ')' pb.Add(@"\\[pP]\{.*?(\} | $)"); // property pb.Add(@"(?'left_brace'\{) (\d+(,\d*)? | ,\d+) ((?'right_brace'\})|$)"); // '{...}' string posix_bracket = ""; // Not supported: @"(\[:.*?(:\]|$))"; // [:...:] pb.Add($@" (?'left_bracket'\[) \]? (?> {posix_bracket}{( posix_bracket.Length == 0 ? "" : " |" )} (?'left_bracket'\[)(?<c>) | (\\. | [^\[\]])+ | (?'right_bracket'\])(?<-c>))* (?(c)(?!)) (?'right_bracket'\])? | (?'right_bracket'\]) " ); pb.Add(@"\\."); // '\...' return(pb.ToRegex( )); }
internal void SetSelectedOptions(DRegexOptions options) { try { ++ChangeCounter; Options = options.Clone( ); DataContext = Options; } finally { --ChangeCounter; } }
public void ImportOptions(string[] options) { var json = options.FirstOrDefault(o => o.StartsWith("json:"))?.Substring("json:".Length); DRegexOptions options_obj; if (string.IsNullOrWhiteSpace(json)) { options_obj = new DRegexOptions( ); } else { options_obj = JsonSerializer.Deserialize <DRegexOptions>(json); } OptionsControl.SetSelectedOptions(options_obj); }
Regex CreateColouringRegex(DRegexOptions options) { var pb_escape = new PatternBuilder( ); pb_escape.BeginGroup("escape"); pb_escape.Add(@"\\c[A-Za-z]"); // Matches the control character corresponding to letter C pb_escape.Add(@"\\x[0-9a-fA-F]{0,2}"); // Matches a character with hexadecimal value of XX. pb_escape.Add(@"\\u[0-9a-fA-F]{0,4}"); // Matches a character with hexadecimal value of XXXX. pb_escape.Add(@"\\U[0-9a-fA-F]{0,8}"); // Matches a character with hexadecimal value of YYYYYY. pb_escape.Add(@"\\[pP]\{.*?(\}|$)"); pb_escape.Add(@"\\[pP].?"); pb_escape.Add(@"\\."); pb_escape.EndGroup( ); var pb = new PatternBuilder( ); pb.BeginGroup("comment"); pb.Add(@"\(\?\#.*?(\)|$)"); // An inline comment that is ignored while matching. pb.EndGroup( ); pb.Add(@"\(\?P(?'name'<.*?(>|$))"); // Matches named subexpression regex labeling it with name 'name'. { // (nested groups: https://stackoverflow.com/questions/546433/regular-expression-to-match-balanced-parentheses) string posix_bracket = ""; // Not supported: @"(?'escape'\[:.*?(:\]|$))"; // [:...:], use escape colour pb.Add($@" \[ \]? (?> {posix_bracket}{( posix_bracket.Length == 0 ? "" : " |" )} \[(?<c>) | ({pb_escape.ToPattern( )} | [^\[\]])+ | \](?<-c>))* (?(c)(?!)) \] " ); } pb.Add(pb_escape.ToPattern( )); return(pb.ToRegex( )); }
Regex GetCachedHighlightingRegex( ) { DRegexOptions options = OptionsControl.GetSelectedOptions( ); string key = ""; lock ( CachedHighlightingRegexes ) { if (CachedHighlightingRegexes.TryGetValue(key, out Regex regex)) { return(regex); } regex = CreateHighlightingRegex(options); CachedHighlightingRegexes.Add(key, regex); return(regex); } }
public DMatcher(string pattern, DRegexOptions options) { Options = options; Pattern = pattern; }
public IMatcher ParsePattern(string pattern) { DRegexOptions options = OptionsControl.GetSelectedOptions( ); return(new DMatcher(pattern, options)); }