Skip to content
This repository was archived by the owner on Sep 11, 2023. It is now read-only.

Commit 6557a22

Browse files
committed
Update Regex to match properly function/method calls.
Removed atomic matching.
1 parent 8720f08 commit 6557a22

File tree

3 files changed

+46
-142
lines changed

3 files changed

+46
-142
lines changed

UI/Components/DASMElement/DASMElementHighlighter.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public HighlightingRuleSet MainRuleSet
4343
});
4444
rs.Rules.Add(new HighlightingRule() //opcodes
4545
{
46-
Regex = RegexKeywordsHelper.GetRegexFromKeywords(OpCodeStrings, true),
46+
Regex = RegexKeywordsHelper.GetRegexFromKeywords(OpCodeStrings),
4747
Color = new HighlightingColor() { Foreground = new SimpleHighlightingBrush(Program.OptionsObject.SH_CommentsMarker) }
4848
});
4949
rs.Rules.Add(new HighlightingRule() //hexnumbers
@@ -56,31 +56,31 @@ public HighlightingRuleSet MainRuleSet
5656
{
5757
rs.Rules.Add(new HighlightingRule() //Types
5858
{
59-
Regex = RegexKeywordsHelper.GetRegexFromKeywords(def.TypeStrings, true),
59+
Regex = RegexKeywordsHelper.GetRegexFromKeywords(def.TypeStrings),
6060
Color = new HighlightingColor() { Foreground = new SimpleHighlightingBrush(Colors.Gray) }
6161
});
6262
}
6363
if (def.Constants.Count > 0)
6464
{
6565
rs.Rules.Add(new HighlightingRule() //constants
6666
{
67-
Regex = RegexKeywordsHelper.GetRegexFromKeywords(def.Constants, true),
67+
Regex = RegexKeywordsHelper.GetRegexFromKeywords(def.Constants),
6868
Color = new HighlightingColor() { Foreground = new SimpleHighlightingBrush(Colors.Gray) }
6969
});
7070
}
7171
if (def.FunctionStrings.Length > 0)
7272
{
7373
rs.Rules.Add(new HighlightingRule() //Functions
7474
{
75-
Regex = RegexKeywordsHelper.GetRegexFromKeywords(def.FunctionStrings, true),
75+
Regex = RegexKeywordsHelper.GetFunctionRegex(def.FunctionStrings),
7676
Color = new HighlightingColor() { Foreground = new SimpleHighlightingBrush(Colors.Gray) }
7777
});
7878
}
7979
if (def.ObjectMethods.Count > 0)
8080
{
8181
rs.Rules.Add(new HighlightingRule() //Methods
8282
{
83-
Regex = RegexKeywordsHelper.GetRegexFromKeywords(def.ObjectMethods, true),
83+
Regex = RegexKeywordsHelper.GetRegexFromKeywords(def.ObjectMethods),
8484
Color = new HighlightingColor() { Foreground = new SimpleHighlightingBrush(Colors.Gray) }
8585
});
8686
}
@@ -89,7 +89,7 @@ public HighlightingRuleSet MainRuleSet
8989
{
9090
rs.Rules.Add(new HighlightingRule() //Methods
9191
{
92-
Regex = RegexKeywordsHelper.GetRegexFromKeywords(def.ObjectFields, true),
92+
Regex = RegexKeywordsHelper.GetRegexFromKeywords(def.ObjectFields),
9393
Color = new HighlightingColor() { Foreground = new SimpleHighlightingBrush(Colors.Gray) }
9494
});
9595
}

UI/Components/EditorElement/EditorElementHighlighter.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ public HighlightingRuleSet MainRuleSet
5151
{
5252
Name = "CommentMarkerSet"
5353
};
54-
5554
commentMarkerSet.Rules.Add(new HighlightingRule
5655
{
5756
Regex = RegexKeywordsHelper.GetRegexFromKeywords(new[]
@@ -63,6 +62,7 @@ public HighlightingRuleSet MainRuleSet
6362
}
6463
});
6564

65+
6666
// RULESET 2: Exclude inner single line comment (backslash to escape inside strings)
6767
var excludeInnerSingleLineComment = new HighlightingRuleSet();
6868

@@ -225,7 +225,7 @@ public HighlightingRuleSet MainRuleSet
225225
{
226226
rs.Rules.Add(new HighlightingRule // types
227227
{
228-
Regex = RegexKeywordsHelper.GetRegexFromKeywords(def.TypeStrings, true),
228+
Regex = RegexKeywordsHelper.GetRegexFromKeywords(def.TypeStrings),
229229
Color = new HighlightingColor { Foreground = typesBrush }
230230
});
231231
}
@@ -234,7 +234,7 @@ public HighlightingRuleSet MainRuleSet
234234
{
235235
rs.Rules.Add(new HighlightingRule // constants
236236
{
237-
Regex = RegexKeywordsHelper.GetRegexFromKeywords(def.Constants, true),
237+
Regex = RegexKeywordsHelper.GetRegexFromKeywords(def.Constants),
238238
Color = new HighlightingColor { Foreground = constantBrush }
239239
});
240240
}
@@ -243,7 +243,7 @@ public HighlightingRuleSet MainRuleSet
243243
{
244244
rs.Rules.Add(new HighlightingRule // Functions
245245
{
246-
Regex = RegexKeywordsHelper.GetRegexFromKeywords(def.FunctionStrings, true),
246+
Regex = RegexKeywordsHelper.GetFunctionRegex(def.FunctionStrings),
247247
Color = new HighlightingColor { Foreground = functionBrush }
248248
});
249249
}
@@ -252,7 +252,7 @@ public HighlightingRuleSet MainRuleSet
252252
{
253253
rs.Rules.Add(new HighlightingRule // Methods
254254
{
255-
Regex = RegexKeywordsHelper.GetRegexFromKeywords2(def.ObjectMethods),
255+
Regex = RegexKeywordsHelper.GetMethodRegex(def.ObjectMethods),
256256
Color = new HighlightingColor { Foreground = methodBrush }
257257
});
258258
}
@@ -261,7 +261,7 @@ public HighlightingRuleSet MainRuleSet
261261
{
262262
rs.Rules.Add(new HighlightingRule // Methods
263263
{
264-
Regex = RegexKeywordsHelper.GetRegexFromKeywords2(def.ObjectFields),
264+
Regex = RegexKeywordsHelper.GetMethodRegex(def.ObjectFields),
265265
Color = new HighlightingColor { Foreground = methodBrush }
266266
});
267267
}

Utils/RegexKeywordsHelper.cs

Lines changed: 34 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -11,156 +11,60 @@ public static class RegexKeywordsHelper
1111
/// Converts a string list a regexp matching all the given word.
1212
/// </summary>
1313
/// <param name="keywords">The words list</param>
14-
/// <param name="forceAtomicRegex">If true the word is enclosed in "\b"</param>
15-
/// <param name="noDot">If true the match requires a "." not to be present before the word</param>
1614
/// <returns></returns>
17-
public static Regex GetRegexFromKeywords(string[] keywords, bool forceAtomicRegex = false, bool noDot = false)
15+
public static Regex GetRegexFromKeywords(string[] keywords)
1816
{
19-
if (forceAtomicRegex)
20-
{
21-
keywords = ConvertToAtomicRegexAbleStringArray(keywords);
22-
}
23-
2417
if (keywords.Length == 0)
2518
{
26-
return new Regex("SPEdit_Error"); //hehe
27-
}
28-
29-
var useAtomicRegex = keywords.All(t => char.IsLetterOrDigit(t[0]) && char.IsLetterOrDigit(t[t.Length - 1]));
30-
31-
var regexBuilder = new StringBuilder();
32-
regexBuilder.Append(useAtomicRegex ? @"\b(?>" : @"(");
33-
34-
var orderedKeyWords = new List<string>(keywords);
35-
var i = 0;
36-
foreach (var keyword in orderedKeyWords.OrderByDescending(w => w.Length))
37-
{
38-
if (i++ > 0)
39-
{
40-
regexBuilder.Append('|');
41-
}
42-
43-
if (useAtomicRegex)
44-
{
45-
regexBuilder.Append(Regex.Escape(keyword));
46-
}
47-
else
48-
{
49-
if (char.IsLetterOrDigit(keyword[0]))
50-
{
51-
regexBuilder.Append(@"\b");
52-
}
53-
54-
regexBuilder.Append(Regex.Escape(keyword));
55-
if (char.IsLetterOrDigit(keyword[keyword.Length - 1]))
56-
{
57-
regexBuilder.Append(@"\b");
58-
}
59-
}
60-
}
61-
62-
if (useAtomicRegex)
63-
{
64-
regexBuilder.Append(@")\b");
65-
}
66-
else
67-
{
68-
regexBuilder.Append(@")");
19+
return new Regex("SPEdit_Error"); //We must not return regex that matches any string
6920
}
70-
71-
return new Regex(regexBuilder.ToString(), RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture);
21+
22+
return new Regex(
23+
@$"\b({string.Join("|", keywords)})\b",
24+
RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture);
7225
}
7326

74-
public static Regex GetRegexFromKeywords(List<string> keywords, bool ForceAtomicRegex = false)
27+
public static Regex GetRegexFromKeywords(List<string> keywords)
7528
{
76-
if (ForceAtomicRegex)
77-
{
78-
keywords = ConvertToAtomicRegexAbleStringArray(keywords);
79-
}
80-
8129
if (keywords.Count == 0)
8230
{
83-
return new Regex("SPEdit_Error"); //hehe
84-
}
85-
86-
var useAtomicRegex = keywords.All(t => char.IsLetterOrDigit(t[0]) && char.IsLetterOrDigit(t[t.Length - 1]));
87-
88-
var regexBuilder = new StringBuilder();
89-
regexBuilder.Append(useAtomicRegex ? @"\b(?>" : @"(");
90-
91-
var orderedKeyWords = new List<string>(keywords);
92-
var i = 0;
93-
foreach (var keyword in orderedKeyWords.OrderByDescending(w => w.Length))
94-
{
95-
if (i++ > 0)
96-
{
97-
regexBuilder.Append('|');
98-
}
99-
100-
if (useAtomicRegex)
101-
{
102-
regexBuilder.Append(Regex.Escape(keyword));
103-
}
104-
else
105-
{
106-
if (char.IsLetterOrDigit(keyword[0]))
107-
{
108-
regexBuilder.Append(@"\b");
109-
}
110-
111-
regexBuilder.Append(Regex.Escape(keyword));
112-
if (char.IsLetterOrDigit(keyword[keyword.Length - 1]))
113-
{
114-
regexBuilder.Append(@"\b");
115-
}
116-
}
31+
return new Regex("SPEdit_Error"); //We must not return regex that matches any string
11732
}
11833

119-
regexBuilder.Append(useAtomicRegex ? @")\b" : @")");
120-
121-
return new Regex(regexBuilder.ToString(), RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture);
34+
return new Regex(
35+
@$"\b({string.Join("|", keywords)})\b",
36+
RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture);
12237
}
12338

124-
public static string[] ConvertToAtomicRegexAbleStringArray(string[] keywords)
39+
40+
/// <summary>
41+
/// Used the match function class like "PrintToChat(...)"
42+
/// </summary>
43+
public static Regex GetFunctionRegex(string[] keywords)
12544
{
126-
var atomicRegexAbleList = new List<string>();
127-
for (var j = 0; j < keywords.Length; ++j)
45+
if (keywords.Length == 0)
12846
{
129-
if (keywords[j].Length > 0)
130-
{
131-
if (char.IsLetterOrDigit(keywords[j][0]) &&
132-
char.IsLetterOrDigit(keywords[j][keywords[j].Length - 1]))
133-
{
134-
atomicRegexAbleList.Add(keywords[j]);
135-
}
136-
}
47+
return new Regex("SPEdit_Error"); //We must not return regex that matches any string
13748
}
138-
139-
return atomicRegexAbleList.ToArray();
140-
}
141-
142-
private static List<string> ConvertToAtomicRegexAbleStringArray(IEnumerable<string> keywords)
143-
{
144-
return keywords.Where(t => t.Length > 0)
145-
.Where(t => char.IsLetterOrDigit(t[0]) && char.IsLetterOrDigit(t[t.Length - 1])).ToList();
146-
}
147-
148-
public static Regex GetRegexFromKeywords2(string[] keywords)
149-
{
150-
var regexBuilder = new StringBuilder(@"\b(?<=[^\s]+\.)(");
151-
regexBuilder.Append(string.Join("|", keywords));
152-
regexBuilder.Append(@")\b");
153-
154-
return new Regex(regexBuilder.ToString(), RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture);
49+
50+
return new Regex(
51+
@$"\b(?<!\.)({string.Join("|", keywords)})\b",
52+
RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture);
15553
}
15654

157-
public static Regex GetRegexFromKeywords2(List<string> keywords)
55+
/// <summary>
56+
/// Used the match method class like "myArr.Push"
57+
/// </summary>
58+
public static Regex GetMethodRegex(List<string> keywords)
15859
{
159-
var regexBuilder = new StringBuilder(@"\b(?<=[^\s]+\.)(");
160-
regexBuilder.Append(string.Join("|", keywords));
161-
regexBuilder.Append(@")\b");
162-
163-
return new Regex(regexBuilder.ToString(), RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture);
60+
if (keywords.Count == 0)
61+
{
62+
return new Regex("SPEdit_Error"); //We must not return regex that matches any string
63+
}
64+
65+
return new Regex(
66+
@$"\b(?<=[^\s]+\.)({string.Join("|", keywords)})\b",
67+
RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture);
16468
}
16569
}
16670
}

0 commit comments

Comments
 (0)