Skip to content

Commit bdf7fb6

Browse files
authored
Merge pull request #125 from udlose/fix/gethashcode-sealed-visibility-issues-part2
Part 2 of 3 for #123 - Fix sealed & visibility issues part2
2 parents c615bfd + eefd269 commit bdf7fb6

26 files changed

+253
-166
lines changed

src/TextMateSharp.Benchmarks/BigFileTokenizationBenchmark.cs

Lines changed: 70 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1+
using BenchmarkDotNet.Attributes;
2+
using BenchmarkDotNet.Columns;
3+
using BenchmarkDotNet.Configs;
4+
using BenchmarkDotNet.Reports;
5+
using BenchmarkDotNet.Running;
6+
using Perfolizer.Metrology;
17
using System;
28
using System.Collections.Generic;
9+
using System.Globalization;
310
using System.IO;
4-
5-
using BenchmarkDotNet.Attributes;
6-
711
using TextMateSharp.Grammars;
812

913
namespace TextMateSharp.Benchmarks
1014
{
15+
[Config(typeof(CustomBenchmarksConfig))]
1116
[MemoryDiagnoser]
1217
public class BigFileTokenizationBenchmark
1318
{
@@ -20,10 +25,10 @@ public void Setup()
2025
// Walk up directories to find the solution root
2126
string? dir = AppDomain.CurrentDomain.BaseDirectory;
2227
string bigFilePath = "";
23-
28+
2429
while (dir != null)
2530
{
26-
string candidate = Path.Combine(dir, "src", "TextMateSharp.Demo",
31+
string candidate = Path.Combine(dir, "src", "TextMateSharp.Demo",
2732
"testdata", "samplefiles", "bigfile.cs");
2833
if (File.Exists(candidate))
2934
{
@@ -42,13 +47,13 @@ public void Setup()
4247

4348
// Load the file into memory
4449
_content = File.ReadAllText(bigFilePath);
45-
Console.WriteLine($"Loaded bigfile.cs");
50+
Console.WriteLine("Loaded bigfile.cs");
4651

4752
// Load the C# grammar
4853
RegistryOptions options = new RegistryOptions(ThemeName.DarkPlus);
4954
Registry.Registry registry = new Registry.Registry(options);
5055
_grammar = registry.LoadGrammar("source.cs");
51-
56+
5257
if (_grammar == null)
5358
{
5459
throw new InvalidOperationException("Failed to load C# grammar");
@@ -94,5 +99,63 @@ public int TokenizeAllLines()
9499
yield return (lineStart, content.Length - lineStart);
95100
}
96101
}
102+
103+
#region helper classes for benchmarks
104+
105+
public sealed class CustomBenchmarksConfig : ManualConfig
106+
{
107+
public CustomBenchmarksConfig()
108+
{
109+
// Use the default summary style with size unit in kilobytes.
110+
// We have a separate column to measure in bytes so we can measure even small differences in memory usage.
111+
SummaryStyle = SummaryStyle.Default
112+
.WithSizeUnit(SizeUnit.KB)
113+
.WithCultureInfo(CultureInfo.CurrentCulture);
114+
115+
AddColumn(new AllocatedBytesColumn());
116+
}
117+
}
118+
119+
public sealed class AllocatedBytesColumn : IColumn
120+
{
121+
public string Id => nameof(AllocatedBytesColumn);
122+
123+
public string ColumnName => "Allocated B";
124+
125+
public bool AlwaysShow => true;
126+
127+
public ColumnCategory Category => ColumnCategory.Custom;
128+
129+
public int PriorityInCategory => 0;
130+
131+
public bool IsNumeric => true;
132+
133+
public UnitType UnitType => UnitType.Dimensionless;
134+
135+
public string Legend => "Bytes allocated per operation";
136+
137+
public bool IsAvailable(Summary summary) => true;
138+
139+
public bool IsDefault(Summary summary, BenchmarkCase benchmarkCase) => false;
140+
141+
public string GetValue(Summary summary, BenchmarkCase benchmarkCase, SummaryStyle style)
142+
{
143+
BenchmarkReport? report = summary[benchmarkCase];
144+
long? bytesAllocatedPerOperation = report?.GcStats.GetBytesAllocatedPerOperation(benchmarkCase);
145+
if (!bytesAllocatedPerOperation.HasValue)
146+
{
147+
return "NA";
148+
}
149+
150+
return bytesAllocatedPerOperation.Value.ToString("N0", style.CultureInfo);
151+
}
152+
153+
public string GetValue(Summary summary, BenchmarkCase benchmarkCase)
154+
=> GetValue(summary, benchmarkCase, summary.Style);
155+
156+
public override string ToString() => ColumnName;
157+
}
158+
159+
#endregion helper classes for benchmarks
97160
}
98161
}

src/TextMateSharp.Grammars/GrammarNames.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
namespace TextMateSharp.Grammars
22
{
3-
internal class GrammarNames
3+
internal static class GrammarNames
44
{
5-
internal static string[] SupportedGrammars = new string[] {
5+
internal static readonly string[] SupportedGrammars = new string[] {
66
"Asciidoc",
77
"Bat",
88
"Clojure",
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
using System.Runtime.CompilerServices;
22

3-
[assembly: InternalsVisibleTo("TextMateSharp.Tests, PublicKey=00240000048000009400000006020000002400005253413100040000010001000db16f8de24159e7ee94e32addce2a9b60f3ea5be200ae7b5abbf8676705064a1b5a5a44d570a884bd86bd2d3e83411fb88914e00028bc7d4b5be1ba8fd8db4335e3ad911d0ef7e694cf433f3314e991100c72c7473641a9e3437deeab402c8f4a03fdf9c174cbae00142a28ce43475ca61f0016ede73dc778b5ed5a0344cfc2")]
3+
[assembly: InternalsVisibleTo("TextMateSharp.Tests, PublicKey=00240000048000009400000006020000002400005253413100040000010001000db16f8de24159e7ee94e32addce2a9b60f3ea5be200ae7b5abbf8676705064a1b5a5a44d570a884bd86bd2d3e83411fb88914e00028bc7d4b5be1ba8fd8db4335e3ad911d0ef7e694cf433f3314e991100c72c7473641a9e3437deeab402c8f4a03fdf9c174cbae00142a28ce43475ca61f0016ede73dc778b5ed5a0344cfc2")]
4+
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")]

src/TextMateSharp/Internal/Grammars/AttributedScopeStack.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
using System;
22
using System.Collections.Generic;
3-
43
using TextMateSharp.Themes;
54

65
namespace TextMateSharp.Internal.Grammars
76
{
8-
public class AttributedScopeStack : IEquatable<AttributedScopeStack>
7+
internal sealed class AttributedScopeStack : IEquatable<AttributedScopeStack>
98
{
10-
public AttributedScopeStack Parent { get; private set; }
11-
public string ScopePath { get; private set; }
12-
public int TokenAttributes { get; private set; }
9+
internal AttributedScopeStack Parent { get; private set; }
10+
internal string ScopePath { get; private set; }
11+
internal int TokenAttributes { get; private set; }
1312
private List<string> _cachedScopeNames;
1413

1514
// Precomputed, per-node hash code (persistent structure => safe as long as instances are immutable)
1615
private readonly int _hashCode;
1716

18-
public AttributedScopeStack(AttributedScopeStack parent, string scopePath, int tokenAttributes)
17+
internal AttributedScopeStack(AttributedScopeStack parent, string scopePath, int tokenAttributes)
1918
{
2019
Parent = parent;
2120
ScopePath = scopePath;

src/TextMateSharp/Internal/Grammars/BalancedBracketSelectors.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
namespace TextMateSharp.Internal.Grammars
55
{
6-
public class BalancedBracketSelectors
6+
public sealed class BalancedBracketSelectors
77
{
8-
private Predicate<List<string>>[] _balancedBracketScopes;
9-
private Predicate<List<string>>[] _unbalancedBracketScopes;
8+
private readonly Predicate<List<string>>[] _balancedBracketScopes;
9+
private readonly Predicate<List<string>>[] _unbalancedBracketScopes;
1010

1111
private bool _allowAny = false;
1212

@@ -18,17 +18,17 @@ public BalancedBracketSelectors(
1818
_unbalancedBracketScopes = CreateUnbalancedBracketScopes(unbalancedBracketScopes);
1919
}
2020

21-
public bool MatchesAlways()
21+
internal bool MatchesAlways()
2222
{
2323
return _allowAny && _unbalancedBracketScopes.Length == 0;
2424
}
2525

26-
public bool MatchesNever()
26+
internal bool MatchesNever()
2727
{
2828
return !_allowAny && _balancedBracketScopes.Length == 0;
2929
}
3030

31-
public bool Match(List<string> scopes)
31+
internal bool Match(List<string> scopes)
3232
{
3333
foreach (var excluder in _unbalancedBracketScopes)
3434
{

src/TextMateSharp/Internal/Grammars/BasicScopeAttributes.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
namespace TextMateSharp.Internal.Grammars
66
{
7-
public class BasicScopeAttributes
7+
internal sealed class BasicScopeAttributes
88
{
9-
public int LanguageId { get; private set; }
10-
public int TokenType { get; private set; } /* OptionalStandardTokenType */
11-
public List<ThemeTrieElementRule> ThemeData { get; private set; }
9+
internal int LanguageId { get; private set; }
10+
internal int TokenType { get; private set; } /* OptionalStandardTokenType */
11+
internal List<ThemeTrieElementRule> ThemeData { get; private set; }
1212

13-
public BasicScopeAttributes(
13+
internal BasicScopeAttributes(
1414
int languageId,
1515
int tokenType,
1616
List<ThemeTrieElementRule> themeData)

src/TextMateSharp/Internal/Grammars/BasicScopeAttributesProvider.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@
77

88
namespace TextMateSharp.Internal.Grammars
99
{
10-
public class BasicScopeAttributesProvider
10+
internal sealed class BasicScopeAttributesProvider
1111
{
1212

1313
private static BasicScopeAttributes _NULL_SCOPE_METADATA = new BasicScopeAttributes(0, 0, null);
1414

1515
private static Regex STANDARD_TOKEN_TYPE_REGEXP = new Regex("\\b(comment|string|regex|meta\\.embedded)\\b");
1616

17-
private int _initialLanguage;
18-
private IThemeProvider _themeProvider;
19-
private Dictionary<string, BasicScopeAttributes> _cache = new Dictionary<string, BasicScopeAttributes>();
17+
private readonly int _initialLanguage;
18+
private readonly IThemeProvider _themeProvider;
19+
private readonly Dictionary<string, BasicScopeAttributes> _cache = new Dictionary<string, BasicScopeAttributes>();
2020
private BasicScopeAttributes _defaultAttributes;
21-
private Dictionary<string, int> _embeddedLanguages;
22-
private Regex _embeddedLanguagesRegex;
21+
private readonly Dictionary<string, int> _embeddedLanguages;
22+
private readonly Regex _embeddedLanguagesRegex;
2323

24-
public BasicScopeAttributesProvider(int initialLanguage, IThemeProvider themeProvider,
24+
internal BasicScopeAttributesProvider(int initialLanguage, IThemeProvider themeProvider,
2525
Dictionary<string, int> embeddedLanguages)
2626
{
2727
this._initialLanguage = initialLanguage;
@@ -58,7 +58,7 @@ public BasicScopeAttributesProvider(int initialLanguage, IThemeProvider themePro
5858
reversedScopes.Reverse();
5959
this._embeddedLanguagesRegex = new Regex(
6060
"^((" +
61-
string.Join(")|(", escapedScopes) +
61+
string.Join(")|(", reversedScopes) +
6262
"))($|\\.)");
6363
}
6464
}
@@ -72,12 +72,12 @@ public void OnDidChangeTheme()
7272
new List<ThemeTrieElementRule>() { this._themeProvider.GetDefaults() });
7373
}
7474

75-
public BasicScopeAttributes GetDefaultAttributes()
75+
internal BasicScopeAttributes GetDefaultAttributes()
7676
{
7777
return this._defaultAttributes;
7878
}
7979

80-
public BasicScopeAttributes GetBasicScopeAttributes(string scopeName)
80+
internal BasicScopeAttributes GetBasicScopeAttributes(string scopeName)
8181
{
8282
if (scopeName == null)
8383
{

src/TextMateSharp/Internal/Grammars/LineTokenizer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ private void ScanNext()
203203
if (beginWhileRule.WhileHasBackReferences)
204204
{
205205
_stack = _stack.WithEndRule(
206-
beginWhileRule.getWhileWithResolvedBackReferences(_lineText, captureIndices));
206+
beginWhileRule.GetWhileWithResolvedBackReferences(_lineText, captureIndices));
207207
}
208208

209209
if (!hasAdvanced && beforePush.HasSameRuleAs(_stack))

src/TextMateSharp/Internal/Matcher/IMatchesName.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public interface IMatchesName<T>
99
bool Match(ICollection<string> names, T scopes);
1010
}
1111

12-
public class NameMatcher : IMatchesName<List<string>>
12+
public sealed class NameMatcher : IMatchesName<List<string>>
1313
{
1414
public static IMatchesName<List<string>> Default = new NameMatcher();
1515

src/TextMateSharp/Internal/Parser/PList.cs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System;
21
using System.Collections.Generic;
32
using System.Text;
43

@@ -55,15 +54,15 @@ private PListObject Create(PListObject parent, bool valueAsArray)
5554
public void EndElement(string tagName)
5655
{
5756
object value = null;
58-
string text = this.text.ToString();
57+
string t = this.text.ToString();
5958
if ("key".Equals(tagName))
6059
{
6160
if (currObject == null || currObject.IsValueAsArray())
6261
{
6362
errors.Add("key can only be used inside an open dict element");
6463
return;
6564
}
66-
currObject.SetLastKey(text);
65+
currObject.SetLastKey(t);
6766
return;
6867
}
6968
else if ("dict".Equals(tagName) || "array".Equals(tagName))
@@ -78,35 +77,29 @@ public void EndElement(string tagName)
7877
}
7978
else if ("string".Equals(tagName) || "data".Equals(tagName))
8079
{
81-
value = text;
80+
value = t;
8281
}
8382
else if ("date".Equals(tagName))
8483
{
8584
// TODO : parse date
8685
}
8786
else if ("integer".Equals(tagName))
8887
{
89-
try
88+
if (!int.TryParse(t, out int i))
9089
{
91-
value = int.Parse(text);
92-
}
93-
catch (Exception)
94-
{
95-
errors.Add(text + " is not a integer");
90+
errors.Add(t + " is not an integer");
9691
return;
9792
}
93+
value = i;
9894
}
9995
else if ("real".Equals(tagName))
10096
{
101-
try
102-
{
103-
value = float.Parse(text);
104-
}
105-
catch (Exception)
97+
if (!float.TryParse(t, out float f))
10698
{
107-
errors.Add(text + " is not a float");
99+
errors.Add(t + " is not a float");
108100
return;
109101
}
102+
value = f;
110103
}
111104
else if ("true".Equals(tagName))
112105
{

0 commit comments

Comments
 (0)