Skip to content

Commit

Permalink
Use Collections.EmptyList where applicable
Browse files Browse the repository at this point in the history
  • Loading branch information
Corniel committed Jan 24, 2025
1 parent 6722835 commit 84399df
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 11 deletions.
4 changes: 2 additions & 2 deletions runtime/CSharp/src/Atn/ATN.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ public class ATN
private readonly PredictionContextCache contextCache = new PredictionContextCache();

[NotNull]
public DFA[] decisionToDFA = new DFA[0];
public DFA[] decisionToDFA = Collections.EmptyList<DFA>();

[NotNull]
public DFA[] modeToDFA = new DFA[0];
public DFA[] modeToDFA = Collections.EmptyList<DFA>();

protected internal readonly ConcurrentDictionary<int, int> LL1Table = new ConcurrentDictionary<int, int>();

Expand Down
2 changes: 1 addition & 1 deletion runtime/CSharp/src/Dfa/DFA.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public DFA(DecisionState atnStartState, int decision)
{
this.precedenceDfa = true;
DFAState precedenceState = new DFAState(new ATNConfigSet());
precedenceState.edges = new DFAState[0];
precedenceState.edges = Collections.EmptyList<DFAState>();
precedenceState.isAcceptState = false;
precedenceState.requiresFullContext = false;
this.s0 = precedenceState;
Expand Down
2 changes: 1 addition & 1 deletion runtime/CSharp/src/LexerInterpreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class LexerInterpreter: Lexer

[Obsolete("Use constructor with channelNames argument")]
public LexerInterpreter(string grammarFileName, IVocabulary vocabulary, IEnumerable<string> ruleNames, IEnumerable<string> modeNames, ATN atn, ICharStream input)
: this(grammarFileName, vocabulary, ruleNames, new string[0], modeNames, atn, input)
: this(grammarFileName, vocabulary, ruleNames, Collections.EmptyList<string>(), modeNames, atn, input)
{
}

Expand Down
2 changes: 1 addition & 1 deletion runtime/CSharp/src/Recognizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ public virtual void RemoveErrorListener(IAntlrErrorListener<Symbol> listener)

public virtual void RemoveErrorListeners()
{
_listeners = new IAntlrErrorListener<Symbol>[0];
_listeners = Collections.EmptyList<IAntlrErrorListener<Symbol>>();

Check failure on line 289 in runtime/CSharp/src/Recognizer.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-20.04, csharp)

The name 'Collections' does not exist in the current context

Check failure on line 289 in runtime/CSharp/src/Recognizer.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-20.04, csharp)

The name 'Collections' does not exist in the current context

Check failure on line 289 in runtime/CSharp/src/Recognizer.cs

View workflow job for this annotation

GitHub Actions / build (windows-2022, csharp)

The name 'Collections' does not exist in the current context

Check failure on line 289 in runtime/CSharp/src/Recognizer.cs

View workflow job for this annotation

GitHub Actions / build (windows-2022, csharp)

The name 'Collections' does not exist in the current context

Check failure on line 289 in runtime/CSharp/src/Recognizer.cs

View workflow job for this annotation

GitHub Actions / build (windows-2022, csharp)

The name 'Collections' does not exist in the current context

Check failure on line 289 in runtime/CSharp/src/Recognizer.cs

View workflow job for this annotation

GitHub Actions / build (windows-2022, csharp)

The name 'Collections' does not exist in the current context
}

[NotNull]
Expand Down
2 changes: 1 addition & 1 deletion runtime/CSharp/src/Sharpen/BitSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Antlr4.Runtime.Sharpen

public class BitSet
{
private static readonly ulong[] EmptyBits = new ulong[0];
private static readonly ulong[] EmptyBits = Collections.EmptyList<ulong>();
private const int BitsPerElement = 8 * sizeof(ulong);

private ulong[] _data = EmptyBits;
Expand Down
15 changes: 11 additions & 4 deletions runtime/CSharp/src/Sharpen/Collections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ namespace Antlr4.Runtime.Sharpen

internal static class Collections
{
public static T[] EmptyList<T>()
/// <remarks>
/// Available in .NET as Array.Empty but not to the net45 target.
/// See: https://learn.microsoft.com/dotnet/api/system.array.empty.
/// </remarks>
public static T[] EmptyList<T>()
{
return EmptyListImpl<T>.Instance;
}
Expand All @@ -31,10 +35,13 @@ public static ReadOnlyDictionary<TKey, TValue> SingletonMap<TKey, TValue>(TKey k

private static class EmptyListImpl<T>
{
public static readonly T[] Instance = new T[0];
}
#pragma warning disable CA1825
// Provides a solution for CA1825.
public static readonly T[] Instance = new T[0];
#pragma warning restore CA1825
}

private static class EmptyMapImpl<TKey, TValue>
private static class EmptyMapImpl<TKey, TValue>
{
public static readonly ReadOnlyDictionary<TKey, TValue> Instance =
new ReadOnlyDictionary<TKey, TValue>(new Dictionary<TKey, TValue>());
Expand Down
2 changes: 1 addition & 1 deletion runtime/CSharp/src/Vocabulary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Antlr4.Runtime
/// <author>Sam Harwell</author>
public class Vocabulary : IVocabulary
{
private static readonly string[] EmptyNames = new string[0];
private static readonly string[] EmptyNames = System.Collections.EmptyList<string>();

Check failure on line 19 in runtime/CSharp/src/Vocabulary.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-20.04, csharp)

The type or namespace name 'EmptyList' does not exist in the namespace 'System.Collections' (are you missing an assembly reference?)

Check failure on line 19 in runtime/CSharp/src/Vocabulary.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-20.04, csharp)

The type or namespace name 'EmptyList' does not exist in the namespace 'System.Collections' (are you missing an assembly reference?)

Check failure on line 19 in runtime/CSharp/src/Vocabulary.cs

View workflow job for this annotation

GitHub Actions / build (windows-2022, csharp)

The type or namespace name 'EmptyList' does not exist in the namespace 'System.Collections' (are you missing an assembly reference?)

Check failure on line 19 in runtime/CSharp/src/Vocabulary.cs

View workflow job for this annotation

GitHub Actions / build (windows-2022, csharp)

The type or namespace name 'EmptyList' does not exist in the namespace 'System.Collections' (are you missing an assembly reference?)

Check failure on line 19 in runtime/CSharp/src/Vocabulary.cs

View workflow job for this annotation

GitHub Actions / build (windows-2022, csharp)

The type or namespace name 'EmptyList' does not exist in the namespace 'System.Collections' (are you missing an assembly reference?)

Check failure on line 19 in runtime/CSharp/src/Vocabulary.cs

View workflow job for this annotation

GitHub Actions / build (windows-2022, csharp)

The type or namespace name 'EmptyList' does not exist in the namespace 'System.Collections' (are you missing an assembly reference?)

/// <summary>
/// Gets an empty
Expand Down

0 comments on commit 84399df

Please sign in to comment.