From 61c141e994ad957b4a27774cabe47cdafd853e02 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Wed, 27 Feb 2013 12:40:45 -0600 Subject: [PATCH] Use IDictionary instead of IReadOnlyDictionary before .NET 4.5 --- .../Antlr4.Runtime/Dfa/AbstractEdgeMap`1.cs | 4 ++++ .../Antlr4.Runtime/Dfa/ArrayEdgeMap`1.cs | 8 ++++++++ runtime/CSharp/Antlr4.Runtime/Dfa/DFAState.cs | 14 ++++++++++++- .../CSharp/Antlr4.Runtime/Dfa/IEdgeMap`1.cs | 5 +++++ .../Antlr4.Runtime/Dfa/SingletonEdgeMap`1.cs | 4 ++++ .../Antlr4.Runtime/Dfa/SparseEdgeMap`1.cs | 8 ++++++++ .../Antlr4.Runtime/Sharpen/Collections.cs | 20 ++++++++++++++++++- 7 files changed, 61 insertions(+), 2 deletions(-) diff --git a/runtime/CSharp/Antlr4.Runtime/Dfa/AbstractEdgeMap`1.cs b/runtime/CSharp/Antlr4.Runtime/Dfa/AbstractEdgeMap`1.cs index 0b921e1af..77f4a8c5e 100644 --- a/runtime/CSharp/Antlr4.Runtime/Dfa/AbstractEdgeMap`1.cs +++ b/runtime/CSharp/Antlr4.Runtime/Dfa/AbstractEdgeMap`1.cs @@ -103,7 +103,11 @@ namespace Antlr4.Runtime.Dfa get; } +#if NET_4_5 public abstract IReadOnlyDictionary ToMap(); +#else + public abstract IDictionary ToMap(); +#endif public virtual IEnumerator> GetEnumerator() { diff --git a/runtime/CSharp/Antlr4.Runtime/Dfa/ArrayEdgeMap`1.cs b/runtime/CSharp/Antlr4.Runtime/Dfa/ArrayEdgeMap`1.cs index 38a58ba90..1fe602e86 100644 --- a/runtime/CSharp/Antlr4.Runtime/Dfa/ArrayEdgeMap`1.cs +++ b/runtime/CSharp/Antlr4.Runtime/Dfa/ArrayEdgeMap`1.cs @@ -168,7 +168,11 @@ namespace Antlr4.Runtime.Dfa return this; } +#if NET_4_5 public override IReadOnlyDictionary ToMap() +#else + public override IDictionary ToMap() +#endif { if (IsEmpty) { @@ -183,7 +187,11 @@ namespace Antlr4.Runtime.Dfa } result[i + minIndex] = arrayData[i]; } +#if NET_4_5 return new ReadOnlyDictionary(result); +#else + return result; +#endif } } } diff --git a/runtime/CSharp/Antlr4.Runtime/Dfa/DFAState.cs b/runtime/CSharp/Antlr4.Runtime/Dfa/DFAState.cs index 1683ba5ee..9b115061a 100644 --- a/runtime/CSharp/Antlr4.Runtime/Dfa/DFAState.cs +++ b/runtime/CSharp/Antlr4.Runtime/Dfa/DFAState.cs @@ -221,7 +221,11 @@ namespace Antlr4.Runtime.Dfa } } +#if NET_4_5 public virtual IReadOnlyDictionary EdgeMap +#else + public virtual IDictionary EdgeMap +#endif { get { @@ -265,7 +269,11 @@ namespace Antlr4.Runtime.Dfa } } +#if NET_4_5 public virtual IReadOnlyDictionary ContextEdgeMap +#else + public virtual IDictionary ContextEdgeMap +#endif { get { @@ -273,7 +281,7 @@ namespace Antlr4.Runtime.Dfa { return Sharpen.Collections.EmptyMap(); } - IReadOnlyDictionary map = contextEdges.ToMap(); + var map = contextEdges.ToMap(); if (map.ContainsKey(-1)) { if (map.Count == 1) @@ -285,7 +293,11 @@ namespace Antlr4.Runtime.Dfa Dictionary result = map.ToDictionary(i => i.Key, i => i.Value); result.Add(PredictionContext.EmptyFullStateKey, result[-1]); result.Remove(-1); +#if NET_4_5 map = new ReadOnlyDictionary(new SortedDictionary(result)); +#else + map = new SortedDictionary(result); +#endif } } return map; diff --git a/runtime/CSharp/Antlr4.Runtime/Dfa/IEdgeMap`1.cs b/runtime/CSharp/Antlr4.Runtime/Dfa/IEdgeMap`1.cs index f0f24dcec..a36041ff8 100644 --- a/runtime/CSharp/Antlr4.Runtime/Dfa/IEdgeMap`1.cs +++ b/runtime/CSharp/Antlr4.Runtime/Dfa/IEdgeMap`1.cs @@ -66,7 +66,12 @@ namespace Antlr4.Runtime.Dfa [return: NotNull] IEdgeMap Clear(); +#if NET_4_5 [return: NotNull] IReadOnlyDictionary ToMap(); +#else + [return: NotNull] + IDictionary ToMap(); +#endif } } diff --git a/runtime/CSharp/Antlr4.Runtime/Dfa/SingletonEdgeMap`1.cs b/runtime/CSharp/Antlr4.Runtime/Dfa/SingletonEdgeMap`1.cs index 56601a708..3c8c0136b 100644 --- a/runtime/CSharp/Antlr4.Runtime/Dfa/SingletonEdgeMap`1.cs +++ b/runtime/CSharp/Antlr4.Runtime/Dfa/SingletonEdgeMap`1.cs @@ -149,7 +149,11 @@ namespace Antlr4.Runtime.Dfa return this; } +#if NET_4_5 public override IReadOnlyDictionary ToMap() +#else + public override IDictionary ToMap() +#endif { if (IsEmpty) { diff --git a/runtime/CSharp/Antlr4.Runtime/Dfa/SparseEdgeMap`1.cs b/runtime/CSharp/Antlr4.Runtime/Dfa/SparseEdgeMap`1.cs index 5ae1d1430..c064c8b82 100644 --- a/runtime/CSharp/Antlr4.Runtime/Dfa/SparseEdgeMap`1.cs +++ b/runtime/CSharp/Antlr4.Runtime/Dfa/SparseEdgeMap`1.cs @@ -201,7 +201,11 @@ namespace Antlr4.Runtime.Dfa return result; } +#if NET_4_5 public override IReadOnlyDictionary ToMap() +#else + public override IDictionary ToMap() +#endif { if (IsEmpty) { @@ -212,7 +216,11 @@ namespace Antlr4.Runtime.Dfa { result[keys[i]] = values[i]; } +#if NET_4_5 return new ReadOnlyDictionary(result); +#else + return result; +#endif } } } diff --git a/runtime/CSharp/Antlr4.Runtime/Sharpen/Collections.cs b/runtime/CSharp/Antlr4.Runtime/Sharpen/Collections.cs index ee66b4999..1aa625af7 100644 --- a/runtime/CSharp/Antlr4.Runtime/Sharpen/Collections.cs +++ b/runtime/CSharp/Antlr4.Runtime/Sharpen/Collections.cs @@ -39,7 +39,11 @@ namespace Sharpen return EmptyListImpl.Instance; } +#if NET_4_5 public static ReadOnlyDictionary EmptyMap() +#else + public static IDictionary EmptyMap() +#endif { return EmptyMapImpl.Instance; } @@ -49,9 +53,13 @@ namespace Sharpen return new ReadOnlyCollection(new T[] { item }); } +#if NET_4_5 public static ReadOnlyDictionary SingletonMap(TKey key, TValue value) +#else + public static IDictionary SingletonMap(TKey key, TValue value) +#endif { - return new ReadOnlyDictionary(new Dictionary { { key, value } }); + return new Dictionary { { key, value } }; } private static class EmptyListImpl @@ -61,8 +69,18 @@ namespace Sharpen private static class EmptyMapImpl { +#if NET_4_5 public static readonly ReadOnlyDictionary Instance = new ReadOnlyDictionary(new Dictionary()); +#else + public static IDictionary Instance + { + get + { + return new Dictionary(); + } + } +#endif } } }