From e52f9d861ca0f37ebb8aab55592b31c4dc111c88 Mon Sep 17 00:00:00 2001 From: Avneet Toor Date: Fri, 29 Nov 2019 12:33:56 -0800 Subject: [PATCH 1/5] Initial commit. --- IonDotnet/ImportLocation.cs | 48 +++++++++++++++++++++++ IonDotnet/Internals/SharedSymbolTable.cs | 3 +- IonDotnet/Internals/Text/RawTextReader.cs | 2 +- IonDotnet/SymbolToken.cs | 36 +++++++++++++++-- IonDotnet/Tree/IonSymbol.cs | 7 +++- 5 files changed, 89 insertions(+), 7 deletions(-) create mode 100644 IonDotnet/ImportLocation.cs diff --git a/IonDotnet/ImportLocation.cs b/IonDotnet/ImportLocation.cs new file mode 100644 index 00000000..3cf1ec62 --- /dev/null +++ b/IonDotnet/ImportLocation.cs @@ -0,0 +1,48 @@ +namespace IonDotnet +{ + public readonly struct ImportLocation + { + + /// + /// The default ImportName, which is unknown + /// + public const string UnknownImportName = default; + + /// + /// The default value, corresponds to not_found/unknown + /// + public static readonly ImportLocation None = default; + + + /// + /// Create a new ImportLocation struct. + /// + /// ImportName + /// Sid + public ImportLocation(string importName, int sid) + { + ImportName = importName; + Sid = sid; + } + + /// + /// The import name of this import location. + /// + public readonly string ImportName; + + /// + /// The ID of this import location. + /// + public readonly int Sid; + + public override string ToString() => $"ImportLocation::{{importName:{ImportName}, id:{Sid}}}"; + + public static bool operator ==(ImportLocation x, ImportLocation y) => x.ImportName == y.ImportName && x.Sid == y.Sid; + + public static bool operator !=(ImportLocation x, ImportLocation y) => !(x == y); + + public override bool Equals(object that) => that is ImportLocation token && Equals(token); + + public override int GetHashCode() => ImportName?.GetHashCode() ?? Sid; + } +} diff --git a/IonDotnet/Internals/SharedSymbolTable.cs b/IonDotnet/Internals/SharedSymbolTable.cs index ef60a13d..b4aaba53 100644 --- a/IonDotnet/Internals/SharedSymbolTable.cs +++ b/IonDotnet/Internals/SharedSymbolTable.cs @@ -110,7 +110,8 @@ public SymbolToken Find(string text) return default; var internedText = _symbolNames[sid - 1]; - return new SymbolToken(internedText, sid); + // Normalize the SID and import reference to this table. + return new SymbolToken(internedText, sid, new ImportLocation(Name, sid)); } public int FindSymbolId(string name) diff --git a/IonDotnet/Internals/Text/RawTextReader.cs b/IonDotnet/Internals/Text/RawTextReader.cs index bfccac84..ea5db32d 100644 --- a/IonDotnet/Internals/Text/RawTextReader.cs +++ b/IonDotnet/Internals/Text/RawTextReader.cs @@ -705,7 +705,7 @@ public IEnumerable GetTypeAnnotations() } var text = symtab.FindKnownSymbol(a.Sid); - yield return new SymbolToken(text, a.Sid); + yield return new SymbolToken(text, a.Sid, a.ImportLocation); } else { diff --git a/IonDotnet/SymbolToken.cs b/IonDotnet/SymbolToken.cs index 473fb6f2..fc749ee2 100644 --- a/IonDotnet/SymbolToken.cs +++ b/IonDotnet/SymbolToken.cs @@ -17,6 +17,11 @@ namespace IonDotnet /// public const int UnknownSid = -1; + /// + /// The default import location, corresponds to not_found/unknown + /// + public static readonly ImportLocation UnknownImportLocation = new ImportLocation(ImportLocation.UnknownImportName, UnknownSid); + /// /// The default value, corresponds to not_found/unknown /// @@ -41,6 +46,26 @@ public SymbolToken(string text, int sid) Text = text; _sid = sid + 1; + ImportLocation = UnknownImportLocation; + } + + /// + /// Create a new symbol token. + /// + /// Text + /// Sid + /// ImportLocation + public SymbolToken(string text, int sid, ImportLocation importLocation) + { + /** + * Note: due to the fact that C# structs are initialized 'blank' (all fields 0), and we want the default + * Sid to be Unknown(-1), the actual field value is shifted by +1 compared to the publicly + * returned value + */ + + Text = text; + _sid = sid + 1; + ImportLocation = importLocation; } /// @@ -53,10 +78,15 @@ public SymbolToken(string text, int sid) /// public int Sid => _sid - 1; + /// + /// The import location of this symbol token. + /// + public readonly ImportLocation ImportLocation; + //Override everything to avoid boxing allocation - public override string ToString() => $"SymbolToken::{{text:{Text}, id:{Sid}}}"; + public override string ToString() => $"SymbolToken::{{text:{Text}, importLocation:{ImportLocation.ToString()}}}"; - public static bool operator ==(SymbolToken x, SymbolToken y) => x.Text == y.Text && x._sid == y._sid; + public static bool operator ==(SymbolToken x, SymbolToken y) => x.Text == y.Text && x.ImportLocation == y.ImportLocation; public static bool operator !=(SymbolToken x, SymbolToken y) => !(x == y); @@ -73,7 +103,7 @@ public bool IsEquivalentTo(SymbolToken other) if (other.Text != null) return false; - return other.Sid == Sid; + return other.ImportLocation == ImportLocation; } } } diff --git a/IonDotnet/Tree/IonSymbol.cs b/IonDotnet/Tree/IonSymbol.cs index 5dfbe6a2..dbeafd9e 100644 --- a/IonDotnet/Tree/IonSymbol.cs +++ b/IonDotnet/Tree/IonSymbol.cs @@ -5,6 +5,7 @@ namespace IonDotnet.Tree public sealed class IonSymbol : IonText { private int _sid; + private ImportLocation _importLocation; public IonSymbol(string text, int sid = SymbolToken.UnknownSid) : this(new SymbolToken(text, sid)) { @@ -13,6 +14,7 @@ public IonSymbol(string text, int sid = SymbolToken.UnknownSid) : this(new Symbo public IonSymbol(SymbolToken symbolToken) : base(symbolToken.Text, symbolToken == default) { _sid = symbolToken.Sid; + _importLocation = symbolToken.ImportLocation; } private IonSymbol(bool isNull) : base(null, isNull) @@ -28,7 +30,7 @@ public override bool IsEquivalentTo(IonValue other) { if (!base.IsEquivalentTo(other)) return false; - + if (!(other is IonSymbol oSymbol)) return false; @@ -63,11 +65,12 @@ public override string StringValue public SymbolToken SymbolValue { - get => new SymbolToken(StringVal, _sid); + get => new SymbolToken(StringVal, _sid, _importLocation); set { StringValue = value.Text; _sid = value.Sid; + _importLocation = value.ImportLocation; } } } From 499cf0d0738c477cd4c54ed4818c4a5beef63a02 Mon Sep 17 00:00:00 2001 From: Avneet Toor Date: Mon, 9 Dec 2019 13:53:20 -0800 Subject: [PATCH 2/5] Add a missing importLocation parameter per a PR comment. --- IonDotnet/Tree/IonSymbol.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/IonDotnet/Tree/IonSymbol.cs b/IonDotnet/Tree/IonSymbol.cs index dbeafd9e..bddd0f70 100644 --- a/IonDotnet/Tree/IonSymbol.cs +++ b/IonDotnet/Tree/IonSymbol.cs @@ -7,7 +7,7 @@ public sealed class IonSymbol : IonText private int _sid; private ImportLocation _importLocation; - public IonSymbol(string text, int sid = SymbolToken.UnknownSid) : this(new SymbolToken(text, sid)) + public IonSymbol(string text, int sid = SymbolToken.UnknownSid, ImportLocation importLocation = default) : this(new SymbolToken(text, sid, importLocation)) { } From b45f219fcb459316959b37ba00373352fb0496fd Mon Sep 17 00:00:00 2001 From: Avneet Toor Date: Tue, 10 Dec 2019 15:09:22 -0800 Subject: [PATCH 3/5] Address first round of PR comments from Ion team. --- IonDotnet.Tests/Common/SymTabUtils.cs | 4 +- .../Internals/ImportLocationTest.cs | 48 +++++++++++++++++ .../Internals/ReaderLocalTableTest.cs | 2 +- .../Internals/SharedSymbolTableTest.cs | 2 +- IonDotnet.Tests/Internals/SymbolTokenTest.cs | 41 +++++++++++++++ IonDotnet/ImportLocation.cs | 21 ++++---- IonDotnet/Internals/ReaderLocalTable.cs | 4 +- IonDotnet/Internals/SharedSymbolTable.cs | 2 +- IonDotnet/Internals/Text/RawTextReader.cs | 2 +- IonDotnet/SymbolToken.cs | 51 +++++-------------- 10 files changed, 122 insertions(+), 55 deletions(-) create mode 100644 IonDotnet.Tests/Internals/ImportLocationTest.cs diff --git a/IonDotnet.Tests/Common/SymTabUtils.cs b/IonDotnet.Tests/Common/SymTabUtils.cs index c9ab2994..2b206765 100644 --- a/IonDotnet.Tests/Common/SymTabUtils.cs +++ b/IonDotnet.Tests/Common/SymTabUtils.cs @@ -22,11 +22,11 @@ internal static void AssertSymbolInTable(string text, int sid, bool duplicate, I Assert.AreEqual(sid, symbolTable.FindSymbolId(text)); var token = symbolTable.Find(text); - Assert.AreEqual(sid, token.Sid); + Assert.AreEqual(sid, token.ImportLocation.Sid); Assert.AreEqual(text, token.Text); token = symbolTable.Intern(text); - Assert.AreEqual(sid, token.Sid); + Assert.AreEqual(sid, token.ImportLocation.Sid); Assert.AreEqual(text, token.Text); } } diff --git a/IonDotnet.Tests/Internals/ImportLocationTest.cs b/IonDotnet.Tests/Internals/ImportLocationTest.cs new file mode 100644 index 00000000..b8ed6315 --- /dev/null +++ b/IonDotnet.Tests/Internals/ImportLocationTest.cs @@ -0,0 +1,48 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace IonDotnet.Tests.Internals +{ + [TestClass] + public class ImportLocationTest + { + [TestMethod] + public void Init_SidAndTextUnknown() + { + var location = new ImportLocation(); + Assert.AreEqual(null, location.ImportName); + Assert.AreEqual(0, location.Sid); + } + + [TestMethod] + [DataRow("text1", 123, "text1", 123)] + [DataRow("text2", 456, "text2", 456)] + public void Bool_EqualsOperator(string text1, int sid1, string text2, int sid2) + { + var location1 = new ImportLocation(text1, sid1); + var location2 = new ImportLocation(text2, sid2); + Assert.IsTrue(location1 == location2); + } + + [TestMethod] + [DataRow("text1", 123, "text1", 456)] + [DataRow("text2", 456, "text3", 456)] + public void Bool_NotEqualsOperator(string text1, int sid1, string text2, int sid2) + { + var location1 = new ImportLocation(text1, sid1); + var location2 = new ImportLocation(text2, sid2); + Assert.IsTrue(location1 != location2); + } + + [TestMethod] + public void EqualsMethod() + { + var location = CreateSampleToken(); + var equalLocation = new ImportLocation("yo", 30); + var unEqualLocation = new ImportLocation("yo", 31); + Assert.IsTrue(location.Equals(equalLocation)); + Assert.IsFalse(location.Equals(unEqualLocation)); + } + + private static ImportLocation CreateSampleToken() => new ImportLocation("yo", 30); + } +} diff --git a/IonDotnet.Tests/Internals/ReaderLocalTableTest.cs b/IonDotnet.Tests/Internals/ReaderLocalTableTest.cs index a825b3f5..f4bcf8bf 100644 --- a/IonDotnet.Tests/Internals/ReaderLocalTableTest.cs +++ b/IonDotnet.Tests/Internals/ReaderLocalTableTest.cs @@ -15,7 +15,7 @@ void assertTable(ISymbolTable tab, params (string sym, int id)[] syms) { var symtok = tab.Find(sym.sym); Assert.AreEqual(sym, sym, symtok.Text); - Assert.AreEqual(sym.id, symtok.Sid); + Assert.AreEqual(sym.id, symtok.ImportLocation.Sid); var symText = tab.FindKnownSymbol(sym.id); Assert.AreEqual(sym.sym, symText); var sid = tab.FindSymbolId(sym.sym); diff --git a/IonDotnet.Tests/Internals/SharedSymbolTableTest.cs b/IonDotnet.Tests/Internals/SharedSymbolTableTest.cs index 00097bc9..96d5f57f 100644 --- a/IonDotnet.Tests/Internals/SharedSymbolTableTest.cs +++ b/IonDotnet.Tests/Internals/SharedSymbolTableTest.cs @@ -45,7 +45,7 @@ public void InternKnownText_KeepEntry(string text) //make sure that no extra allocation is made Assert.AreSame(text, symtok.Text); - Assert.AreEqual(1, symtok.Sid); + Assert.AreEqual(1, symtok.ImportLocation.Sid); } [TestMethod] diff --git a/IonDotnet.Tests/Internals/SymbolTokenTest.cs b/IonDotnet.Tests/Internals/SymbolTokenTest.cs index d08d6435..20683544 100644 --- a/IonDotnet.Tests/Internals/SymbolTokenTest.cs +++ b/IonDotnet.Tests/Internals/SymbolTokenTest.cs @@ -15,6 +15,46 @@ public void Init_SidAndTextUnknown() Assert.AreEqual(token, SymbolToken.None); } + [TestMethod] + [DataRow("text1", 123, "text1", 123)] + [DataRow("text2", 456, "text2", 456)] + public void Bool_EqualsOperator(string text1, int sid1, string text2, int sid2) + { + var token1 = new SymbolToken(text1, sid1); + var token2 = new SymbolToken(text2, sid2); + Assert.IsTrue(token1 == token2); + } + + [TestMethod] + [DataRow("text1", 123, "text1", 456)] + [DataRow("text2", 456, "text3", 456)] + public void Bool_NotEqualsOperator(string text1, int sid1, string text2, int sid2) + { + var token1 = new SymbolToken(text1, sid1); + var token2 = new SymbolToken(text2, sid2); + Assert.IsTrue(token1 != token2); + } + + [TestMethod] + public void EqualsMethod() + { + var token = CreateSampleToken(); + var equalToken = new SymbolToken("yo", 30); + var unEqualToken = new SymbolToken("yo", 31); + Assert.IsTrue(token.Equals(equalToken)); + Assert.IsFalse(token.Equals(unEqualToken)); + } + + [TestMethod] + public void IsEquivalentTo() + { + var token = CreateSampleTokenWihtImportLocation(); + var equalToken = new SymbolToken("yo", 30, new ImportLocation("hey", 40)); + var unEqualToken = new SymbolToken("oy", 30, new ImportLocation("hey", 41)); + Assert.IsTrue(token.IsEquivalentTo(equalToken)); + Assert.IsFalse(token.IsEquivalentTo(unEqualToken)); + } + [TestMethod] [DataRow(0)] [DataRow(1)] @@ -58,5 +98,6 @@ public void ArrayResize_RemainderSetToDefault(int oldLength, int newLength) } private static SymbolToken CreateSampleToken() => new SymbolToken("yo", 30); + private static SymbolToken CreateSampleTokenWihtImportLocation() => new SymbolToken("yo", 30, new ImportLocation("hey", 40)); } } diff --git a/IonDotnet/ImportLocation.cs b/IonDotnet/ImportLocation.cs index 3cf1ec62..ad25ca22 100644 --- a/IonDotnet/ImportLocation.cs +++ b/IonDotnet/ImportLocation.cs @@ -13,6 +13,15 @@ public readonly struct ImportLocation /// public static readonly ImportLocation None = default; + /// + /// The import name of this import location. + /// + public readonly string ImportName; + + /// + /// The ID of this import location. + /// + public readonly int Sid; /// /// Create a new ImportLocation struct. @@ -25,16 +34,6 @@ public ImportLocation(string importName, int sid) Sid = sid; } - /// - /// The import name of this import location. - /// - public readonly string ImportName; - - /// - /// The ID of this import location. - /// - public readonly int Sid; - public override string ToString() => $"ImportLocation::{{importName:{ImportName}, id:{Sid}}}"; public static bool operator ==(ImportLocation x, ImportLocation y) => x.ImportName == y.ImportName && x.Sid == y.Sid; @@ -43,6 +42,8 @@ public ImportLocation(string importName, int sid) public override bool Equals(object that) => that is ImportLocation token && Equals(token); + public bool Equals(ImportLocation other) => this == other; + public override int GetHashCode() => ImportName?.GetHashCode() ?? Sid; } } diff --git a/IonDotnet/Internals/ReaderLocalTable.cs b/IonDotnet/Internals/ReaderLocalTable.cs index 59634dfd..c1d97511 100644 --- a/IonDotnet/Internals/ReaderLocalTable.cs +++ b/IonDotnet/Internals/ReaderLocalTable.cs @@ -65,14 +65,14 @@ public SymbolToken Find(string text) { var t = import.Find(text); if (t != default) - return new SymbolToken(t.Text, offset + t.Sid); + return new SymbolToken(t.Text, offset + t.Sid, new ImportLocation(t.Text, offset + t.ImportLocation.Sid)); offset += import.MaxId; } for (var i = 0; i < _ownSymbols.Count; i++) { if (_ownSymbols[i] == text) - return new SymbolToken(text, i + 1 + _importedMaxId); + return new SymbolToken(text, i + 1 + _importedMaxId, new ImportLocation(text, i + 1 + _importedMaxId)); } return default; diff --git a/IonDotnet/Internals/SharedSymbolTable.cs b/IonDotnet/Internals/SharedSymbolTable.cs index b4aaba53..339c3534 100644 --- a/IonDotnet/Internals/SharedSymbolTable.cs +++ b/IonDotnet/Internals/SharedSymbolTable.cs @@ -111,7 +111,7 @@ public SymbolToken Find(string text) var internedText = _symbolNames[sid - 1]; // Normalize the SID and import reference to this table. - return new SymbolToken(internedText, sid, new ImportLocation(Name, sid)); + return new SymbolToken(internedText, SymbolToken.UnknownSid, new ImportLocation(Name, sid)); } public int FindSymbolId(string name) diff --git a/IonDotnet/Internals/Text/RawTextReader.cs b/IonDotnet/Internals/Text/RawTextReader.cs index ea5db32d..bb70f1fb 100644 --- a/IonDotnet/Internals/Text/RawTextReader.cs +++ b/IonDotnet/Internals/Text/RawTextReader.cs @@ -704,7 +704,7 @@ public IEnumerable GetTypeAnnotations() throw new UnknownSymbolException(a.Sid); } - var text = symtab.FindKnownSymbol(a.Sid); + var text = symtab.FindKnownSymbol(a.ImportLocation.Sid); yield return new SymbolToken(text, a.Sid, a.ImportLocation); } else diff --git a/IonDotnet/SymbolToken.cs b/IonDotnet/SymbolToken.cs index fc749ee2..1ce28529 100644 --- a/IonDotnet/SymbolToken.cs +++ b/IonDotnet/SymbolToken.cs @@ -17,11 +17,6 @@ namespace IonDotnet /// public const int UnknownSid = -1; - /// - /// The default import location, corresponds to not_found/unknown - /// - public static readonly ImportLocation UnknownImportLocation = new ImportLocation(ImportLocation.UnknownImportName, UnknownSid); - /// /// The default value, corresponds to not_found/unknown /// @@ -32,22 +27,19 @@ namespace IonDotnet private readonly int _sid; /// - /// Create a new symbol token. + /// The text of this symbol. /// - /// Text - /// Sid - public SymbolToken(string text, int sid) - { - /** - * Note: due to the fact that C# structs are initialized 'blank' (all fields 0), and we want the default - * Sid to be Unknown(-1), the actual field value is shifted by +1 compared to the publicly - * returned value - */ + public readonly string Text; - Text = text; - _sid = sid + 1; - ImportLocation = UnknownImportLocation; - } + /// + /// The ID of this symbol token. + /// + public int Sid => _sid - 1; + + /// + /// The import location of this symbol token. + /// + public readonly ImportLocation ImportLocation; /// /// Create a new symbol token. @@ -55,7 +47,7 @@ public SymbolToken(string text, int sid) /// Text /// Sid /// ImportLocation - public SymbolToken(string text, int sid, ImportLocation importLocation) + public SymbolToken(string text, int sid, ImportLocation importLocation = default) { /** * Note: due to the fact that C# structs are initialized 'blank' (all fields 0), and we want the default @@ -68,25 +60,10 @@ public SymbolToken(string text, int sid, ImportLocation importLocation) ImportLocation = importLocation; } - /// - /// The text of this symbol. - /// - public readonly string Text; - - /// - /// The ID of this symbol token. - /// - public int Sid => _sid - 1; - - /// - /// The import location of this symbol token. - /// - public readonly ImportLocation ImportLocation; - //Override everything to avoid boxing allocation - public override string ToString() => $"SymbolToken::{{text:{Text}, importLocation:{ImportLocation.ToString()}}}"; + public override string ToString() => $"SymbolToken::{{text:{Text}, id:{Sid}, importLocation:{ImportLocation.ToString()}}}"; - public static bool operator ==(SymbolToken x, SymbolToken y) => x.Text == y.Text && x.ImportLocation == y.ImportLocation; + public static bool operator ==(SymbolToken x, SymbolToken y) => x.Text == y.Text && x.Sid == y.Sid && x.ImportLocation == y.ImportLocation; public static bool operator !=(SymbolToken x, SymbolToken y) => !(x == y); From ab9c42611f1da404d8d057468e65241a7d8d488f Mon Sep 17 00:00:00 2001 From: Avneet Toor Date: Mon, 16 Dec 2019 12:59:41 -0800 Subject: [PATCH 4/5] Address second round of PR comments from Ion team. --- IonDotnet.Tests/Common/SymTabUtils.cs | 4 ++-- IonDotnet.Tests/Internals/ReaderLocalTableTest.cs | 2 +- IonDotnet.Tests/Internals/SharedSymbolTableTest.cs | 2 +- IonDotnet.Tests/Internals/SymbolTokenTest.cs | 4 ++-- IonDotnet/Internals/Binary/ManagedBinaryWriter.cs | 4 ++-- IonDotnet/Internals/ReaderLocalTable.cs | 6 ++---- IonDotnet/Internals/SharedSymbolTable.cs | 3 +-- 7 files changed, 11 insertions(+), 14 deletions(-) diff --git a/IonDotnet.Tests/Common/SymTabUtils.cs b/IonDotnet.Tests/Common/SymTabUtils.cs index 2b206765..9a990d7d 100644 --- a/IonDotnet.Tests/Common/SymTabUtils.cs +++ b/IonDotnet.Tests/Common/SymTabUtils.cs @@ -22,11 +22,11 @@ internal static void AssertSymbolInTable(string text, int sid, bool duplicate, I Assert.AreEqual(sid, symbolTable.FindSymbolId(text)); var token = symbolTable.Find(text); - Assert.AreEqual(sid, token.ImportLocation.Sid); + Assert.AreEqual(SymbolToken.UnknownSid, token.Sid); Assert.AreEqual(text, token.Text); token = symbolTable.Intern(text); - Assert.AreEqual(sid, token.ImportLocation.Sid); + Assert.AreEqual(SymbolToken.UnknownSid, token.Sid); Assert.AreEqual(text, token.Text); } } diff --git a/IonDotnet.Tests/Internals/ReaderLocalTableTest.cs b/IonDotnet.Tests/Internals/ReaderLocalTableTest.cs index f4bcf8bf..5a44eaac 100644 --- a/IonDotnet.Tests/Internals/ReaderLocalTableTest.cs +++ b/IonDotnet.Tests/Internals/ReaderLocalTableTest.cs @@ -15,7 +15,7 @@ void assertTable(ISymbolTable tab, params (string sym, int id)[] syms) { var symtok = tab.Find(sym.sym); Assert.AreEqual(sym, sym, symtok.Text); - Assert.AreEqual(sym.id, symtok.ImportLocation.Sid); + Assert.AreEqual(SymbolToken.UnknownSid, symtok.Sid); var symText = tab.FindKnownSymbol(sym.id); Assert.AreEqual(sym.sym, symText); var sid = tab.FindSymbolId(sym.sym); diff --git a/IonDotnet.Tests/Internals/SharedSymbolTableTest.cs b/IonDotnet.Tests/Internals/SharedSymbolTableTest.cs index 96d5f57f..1b293cb6 100644 --- a/IonDotnet.Tests/Internals/SharedSymbolTableTest.cs +++ b/IonDotnet.Tests/Internals/SharedSymbolTableTest.cs @@ -45,7 +45,7 @@ public void InternKnownText_KeepEntry(string text) //make sure that no extra allocation is made Assert.AreSame(text, symtok.Text); - Assert.AreEqual(1, symtok.ImportLocation.Sid); + Assert.AreEqual(SymbolToken.UnknownSid, symtok.Sid); } [TestMethod] diff --git a/IonDotnet.Tests/Internals/SymbolTokenTest.cs b/IonDotnet.Tests/Internals/SymbolTokenTest.cs index 20683544..b2666164 100644 --- a/IonDotnet.Tests/Internals/SymbolTokenTest.cs +++ b/IonDotnet.Tests/Internals/SymbolTokenTest.cs @@ -48,7 +48,7 @@ public void EqualsMethod() [TestMethod] public void IsEquivalentTo() { - var token = CreateSampleTokenWihtImportLocation(); + var token = CreateSampleTokenWithImportLocation(); var equalToken = new SymbolToken("yo", 30, new ImportLocation("hey", 40)); var unEqualToken = new SymbolToken("oy", 30, new ImportLocation("hey", 41)); Assert.IsTrue(token.IsEquivalentTo(equalToken)); @@ -98,6 +98,6 @@ public void ArrayResize_RemainderSetToDefault(int oldLength, int newLength) } private static SymbolToken CreateSampleToken() => new SymbolToken("yo", 30); - private static SymbolToken CreateSampleTokenWihtImportLocation() => new SymbolToken("yo", 30, new ImportLocation("hey", 40)); + private static SymbolToken CreateSampleTokenWithImportLocation() => new SymbolToken("yo", 30, new ImportLocation("hey", 40)); } } diff --git a/IonDotnet/Internals/Binary/ManagedBinaryWriter.cs b/IonDotnet/Internals/Binary/ManagedBinaryWriter.cs index 3b77dfb3..48ceea79 100644 --- a/IonDotnet/Internals/Binary/ManagedBinaryWriter.cs +++ b/IonDotnet/Internals/Binary/ManagedBinaryWriter.cs @@ -529,10 +529,10 @@ public override SymbolToken Find(string text) if (text == null) throw new ArgumentNullException(nameof(text)); var found = _writer._importContext.TryGetValue(text, out var sid); - if (found) return new SymbolToken(text, sid); + if (found) return new SymbolToken(text, SymbolToken.UnknownSid); found = _writer._locals.TryGetValue(text, out sid); - return found ? new SymbolToken(text, sid) : default; + return found ? new SymbolToken(text, SymbolToken.UnknownSid) : default; } public override string FindKnownSymbol(int sid) diff --git a/IonDotnet/Internals/ReaderLocalTable.cs b/IonDotnet/Internals/ReaderLocalTable.cs index c1d97511..568e7d19 100644 --- a/IonDotnet/Internals/ReaderLocalTable.cs +++ b/IonDotnet/Internals/ReaderLocalTable.cs @@ -60,19 +60,17 @@ internal void Refresh() public SymbolToken Find(string text) { - var offset = 0; foreach (var import in Imports) { var t = import.Find(text); if (t != default) - return new SymbolToken(t.Text, offset + t.Sid, new ImportLocation(t.Text, offset + t.ImportLocation.Sid)); - offset += import.MaxId; + return new SymbolToken(t.Text, SymbolToken.UnknownSid); } for (var i = 0; i < _ownSymbols.Count; i++) { if (_ownSymbols[i] == text) - return new SymbolToken(text, i + 1 + _importedMaxId, new ImportLocation(text, i + 1 + _importedMaxId)); + return new SymbolToken(text, SymbolToken.UnknownSid); } return default; diff --git a/IonDotnet/Internals/SharedSymbolTable.cs b/IonDotnet/Internals/SharedSymbolTable.cs index 339c3534..2b7e6a52 100644 --- a/IonDotnet/Internals/SharedSymbolTable.cs +++ b/IonDotnet/Internals/SharedSymbolTable.cs @@ -110,8 +110,7 @@ public SymbolToken Find(string text) return default; var internedText = _symbolNames[sid - 1]; - // Normalize the SID and import reference to this table. - return new SymbolToken(internedText, SymbolToken.UnknownSid, new ImportLocation(Name, sid)); + return new SymbolToken(internedText, SymbolToken.UnknownSid); } public int FindSymbolId(string name) From 25c686faad621368e8cfd3afa152fe177d2068a9 Mon Sep 17 00:00:00 2001 From: Avneet Toor Date: Mon, 16 Dec 2019 16:44:17 -0800 Subject: [PATCH 5/5] Address PR comment on changing reliance from local SID to ImportLocation. --- IonDotnet/Internals/Text/RawTextReader.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/IonDotnet/Internals/Text/RawTextReader.cs b/IonDotnet/Internals/Text/RawTextReader.cs index bb70f1fb..571232e8 100644 --- a/IonDotnet/Internals/Text/RawTextReader.cs +++ b/IonDotnet/Internals/Text/RawTextReader.cs @@ -487,7 +487,7 @@ private SymbolToken ParseSymbolToken(StringBuilder sb, int token) break; } - return new SymbolToken(text, sid); + return new SymbolToken(text, sid, new ImportLocation(GetSymbolTable().Name, sid)); } private void FinishValue() @@ -696,10 +696,10 @@ public IEnumerable GetTypeAnnotations() foreach (var a in _annotations) { - if (a.Text is null && a.Sid != 0) + if (a.Text is null && a.ImportLocation != default) { var symtab = GetSymbolTable(); - if (a.Sid < -1 || a.Sid > symtab.MaxId) + if (a.ImportLocation.Sid < -1 || a.ImportLocation.Sid > symtab.MaxId) { throw new UnknownSymbolException(a.Sid); }