public string[] ExportOptions( ) { RustRegexOptions options = OptionsControl.GetSelectedOptions( ); var json = JsonSerializer.Serialize(options); return(new[] { $"json:{json}" }); }
internal void SetSelectedOptions(RustRegexOptions options) { try { ++ChangeCounter; Options = options.Clone( ); DataContext = Options; UpdateControls( ); } finally { --ChangeCounter; } }
public void ImportOptions(string[] options) { var json = options.FirstOrDefault(o => o.StartsWith("json:"))?.Substring("json:".Length); RustRegexOptions options_obj; if (string.IsNullOrWhiteSpace(json)) { options_obj = new RustRegexOptions( ); } else { options_obj = JsonSerializer.Deserialize <RustRegexOptions>(json); } OptionsControl.SetSelectedOptions(options_obj); }
Regex GetCachedHighlightingRegex( ) { RustRegexOptions options = OptionsControl.GetSelectedOptions( ); string key = $"{options.@struct}\x1F{options.unicode}\x1F{options.ignore_whitespace}"; lock ( CachedHighlightingRegexes ) { if (CachedHighlightingRegexes.TryGetValue(key, out Regex regex)) { return(regex); } regex = CreateHighlightingRegex(options); CachedHighlightingRegexes.Add(key, regex); return(regex); } }
Regex CreateHighlightingRegex(RustRegexOptions options) { bool is_regex_builder = options.@struct == "RegexBuilder"; var pb = new PatternBuilder( ); if (is_regex_builder && options.ignore_whitespace) { pb.Add(@"\#.*?(\n|$)"); // line-comment } pb.Add(@"(?'left_par'\()"); // '(' pb.Add(@"(?'right_par'\))"); // ')' pb.Add(@"\\[xuU]\{.*?(\}|$)"); // \x{7HHHHHHH ...} etc. if (is_regex_builder && options.unicode) { pb.Add(@"\\[pP]\{.*?(\} | $)"); // property } pb.Add(@"(?'left_brace'\{) (\d+(,\d*)? | ,\d+) ((?'right_brace'\})|$)"); // '{...}' string posix_bracket = @"(\[:.*?(:\]|$))"; // [:...:] 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( )); }
public RustMatcher(string pattern, RustRegexOptions options) { Options = options; Pattern = pattern; }
public IMatcher ParsePattern(string pattern) { RustRegexOptions options = OptionsControl.GetSelectedOptions( ); return(new RustMatcher(pattern, options)); }
Regex CreateColouringRegex(RustRegexOptions options) { bool is_regex = options.@struct == "Regex"; bool is_regex_builder = options.@struct == "RegexBuilder"; var pb_escape = new PatternBuilder( ); pb_escape.BeginGroup("escape"); if (is_regex || (is_regex_builder && options.unicode)) { pb_escape.Add(@"\\[pP]\{.*?(\}|$)"); // Unicode character class (general category or script) pb_escape.Add(@"\\[pP].?"); // One-letter name Unicode character class } if (is_regex_builder && options.octal) { pb_escape.Add(@"\\[0-7]{1,3}"); // octal character code (up to three digits) (when enabled) } pb_escape.Add(@"\\x\{[0-9a-fA-F]*(\}|$)?"); // any hex character code corresponding to a Unicode code point pb_escape.Add(@"\\x[0-9a-fA-F]{0,2}"); // hex character code (exactly two digits) // (only 2 digits if no 'options.unicode' pb_escape.Add(@"\\u\{[0-9a-fA-F]*(\}|$)?"); // any hex character code corresponding to a Unicode code point pb_escape.Add(@"\\u[0-9a-fA-F]{0,4}"); // hex character code (exactly four digits) pb_escape.Add(@"\\U\{[0-9a-fA-F]*(\}|$)?"); // any hex character code corresponding to a Unicode code point pb_escape.Add(@"\\U[0-9a-fA-F]{0,8}"); // hex character code (exactly eight digits) string any_esc = ""; if (!(is_regex || (is_regex_builder && options.unicode))) { any_esc += @"(?!\\[pP])"; } if (!(is_regex_builder && options.octal)) { any_esc += @"(?!\\[0-7])"; } any_esc += @"\\."; pb_escape.Add(any_esc); pb_escape.EndGroup( ); var pb = new PatternBuilder( ); pb.BeginGroup("comment"); if (is_regex_builder && options.ignore_whitespace) { pb.Add(@"\#.*?(\n|$)"); // line-comment } pb.EndGroup( ); pb.Add(@"\(\?P(?'name'<.*?(>|$))"); { // (nested groups: https://stackoverflow.com/questions/546433/regular-expression-to-match-balanced-parentheses) string posix_bracket = @"(?'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( )); }