示例#1
0
        internal static void AssertSymbolInTable(string text, int sid, bool duplicate, ISymbolTable symbolTable)
        {
            if (text == null)
            {
                Assert.IsNull(symbolTable.FindKnownSymbol(sid));
                return;
            }

            if (sid != SymbolToken.UnknownSid)
            {
                Assert.AreEqual(text, symbolTable.FindKnownSymbol(sid));
            }

            if (duplicate)
            {
                return;
            }

            Assert.AreEqual(sid, symbolTable.FindSymbolId(text));
            var token = symbolTable.Find(text);

            Assert.AreEqual(SymbolToken.UnknownSid, token.Sid);
            Assert.AreEqual(text, token.Text);

            token = symbolTable.Intern(text);
            Assert.AreEqual(SymbolToken.UnknownSid, token.Sid);
            Assert.AreEqual(text, token.Text);
        }
示例#2
0
        public override string[] GetTypeAnnotations()
        {
            string[] annotations = new string[this.annotations.Count];
            for (int index = 0; index < this.annotations.Count; index++)
            {
                SymbolToken symbolToken = this.annotations[index];
                if (symbolToken.Text is null)
                {
                    if (symbolToken.ImportLocation == default)
                    {
                        throw new UnknownSymbolException(symbolToken.Sid);
                    }
                    else
                    {
                        ISymbolTable symtab = this.GetSymbolTable();

                        string text = symtab.FindKnownSymbol(symbolToken.ImportLocation.Sid);
                        if (text == null)
                        {
                            throw new UnknownSymbolException(symbolToken.ImportLocation.Sid);
                        }

                        annotations[index] = symtab.FindKnownSymbol(symbolToken.ImportLocation.Sid);
                    }
                }
                else
                {
                    annotations[index] = symbolToken.Text;
                }
            }

            return(annotations);
        }
示例#3
0
        public Entity(Stream stream, int id, int type, ISymbolTable symbolTable, IonLoader loader)
        {
            using var reader = new BinaryReader(stream, Encoding.UTF8, true);
            Signature        = Encoding.ASCII.GetString(reader.ReadBytes(4));
            if (Signature != EntitySignature)
            {
                throw new Exception("Invalid signature");
            }

            Version = reader.ReadUInt16();
            if (!_allowedVersions.Contains(Version))
            {
                throw new Exception($"Version not supported ({Version})");
            }

            Length = reader.ReadUInt32();
            if (Length < MinHeaderLength)
            {
                throw new Exception("Header too short");
            }

            // Duplicated in KfxContainer
            // 10 = number of bytes read so far
            var containerInfoData = new MemoryStream(stream.ReadBytes((int)Length - 10));
            var entityInfo        = loader.LoadSingle <IonStruct>(containerInfoData);

            if (entityInfo == null)
            {
                throw new Exception("Bad container or something");
            }

            var compressionType = entityInfo.GetField(KfxSymbols.BcComprType).IntValue;

            if (compressionType != KfxContainer.DefaultCompressionType)
            {
                throw new Exception($"Unexpected bcComprType ({compressionType})");
            }

            var drmScheme = entityInfo.GetField(KfxSymbols.BcDrmScheme).IntValue;

            if (drmScheme != KfxContainer.DefaultDrmScheme)
            {
                throw new Exception($"Unexpected bcDRMScheme ({drmScheme})");
            }

            FragmentId   = symbolTable.FindKnownSymbol(id);
            FragmentType = symbolTable.FindKnownSymbol(type);

            Value = RawFragmentTypes.Contains(FragmentType)
                ? new IonBlob(new ReadOnlySpan <byte>(stream.ReadToEnd()))
                : ((IonDatagram)loader.Load(stream.ReadToEnd())).Single();

            // Skipping annotation handling for now

            //if ftype == fid and ftype in ROOT_FRAGMENT_TYPES and not self.pure:

            //fid = "$348"

            //return YJFragment(fid = fid if fid != "$348" else None, ftype = ftype, value = self.value)
        }
示例#4
0
        /// <summary>
        /// Load the symbol string from the symbol table to _v
        /// </summary>
        /// <remarks>This assumes LoadOnce() has been called and _v already has the sid as Int</remarks>
        /// <exception cref="UnknownSymbolException">The Sid does not exist in the table</exception>
        private void LoadSymbolValue()
        {
            PrepareValue();
            Debug.Assert(_v.TypeSet.HasFlag(ScalarType.Int));
            Debug.Assert(_v.AuthoritativeType == ScalarType.Int, $"AuthType is ${_v.AuthoritativeType}");

            if (_v.TypeSet.HasFlag(ScalarType.String))
            {
                return;
            }

            var text = _symbolTable.FindKnownSymbol(_v.IntValue);

            _v.AddString(text);
        }
示例#5
0
        /// <summary>
        /// Try to re-make the token in the context of the <paramref name="table"/>.
        /// </summary>
        /// <param name="table">Symbol table</param>
        /// <param name="token">Un-localized token</param>
        /// <returns>Localized token</returns>
        public static SymbolToken Localize(ISymbolTable table, SymbolToken token)
        {
            var newToken = token;

            //try to localize
            if (token.Text == null)
            {
                var text = table.FindKnownSymbol(token.Sid);
                if (text != null)
                {
                    newToken = new SymbolToken(text, token.Sid);
                }
            }
            else
            {
                newToken = table.Find(token.Text);
                if (newToken == default)
                {
                    newToken = new SymbolToken(token.Text, SymbolToken.UnknownSid);
                }
            }

            return(newToken);
        }