Exemple #1
0
        private CfgGrammar.CfgSerializedHeader BuildHeader(List <State> sortedStates, int cBasePath, ushort iSemanticGlobals, out int cArcs, out float[] pWeights)
        {
            cArcs    = 1;
            pWeights = null;
            int num  = 0;
            int num2 = 0;

            foreach (State sortedState in sortedStates)
            {
                sortedState.SerializeId = cArcs;
                int numArcs = sortedState.NumArcs;
                cArcs += numArcs;
                if (num2 < numArcs)
                {
                    num2 = numArcs;
                }
                num += sortedState.NumSemanticTags;
            }
            CfgGrammar.CfgSerializedHeader cfgSerializedHeader = new CfgGrammar.CfgSerializedHeader();
            uint num3 = (uint)Marshal.SizeOf(typeof(CfgGrammar.CfgSerializedHeader));

            cfgSerializedHeader.FormatId = CfgGrammar._SPGDF_ContextFree;
            _guid = Guid.NewGuid();
            cfgSerializedHeader.GrammarGUID = _guid;
            cfgSerializedHeader.LangID      = (ushort)_langId;
            cfgSerializedHeader.pszSemanticInterpretationGlobals = iSemanticGlobals;
            cfgSerializedHeader.cArcsInLargestState = num2;
            cfgSerializedHeader.cchWords            = _words.StringSize();
            cfgSerializedHeader.cWords = _words.Count;
            if (cfgSerializedHeader.cWords > 0)
            {
                cfgSerializedHeader.cWords++;
            }
            cfgSerializedHeader.pszWords = num3;
            num3 = (uint)((int)num3 + _words.SerializeSize() * 2);
            cfgSerializedHeader.cchSymbols = _symbols.StringSize();
            cfgSerializedHeader.pszSymbols = num3;
            num3 = (uint)((int)num3 + _symbols.SerializeSize() * 2);
            cfgSerializedHeader.cRules = _rules.Count;
            cfgSerializedHeader.pRules = num3;
            num3 = (uint)((int)num3 + _rules.Count * Marshal.SizeOf(typeof(CfgRule)));
            cfgSerializedHeader.cBasePath = ((cBasePath > 0) ? num3 : 0u);
            num3 = (uint)((int)num3 + ((cBasePath * 2 + 3) & -4));
            cfgSerializedHeader.cArcs = cArcs;
            cfgSerializedHeader.pArcs = num3;
            num3 = (uint)((int)num3 + cArcs * Marshal.SizeOf(typeof(CfgArc)));
            if (_fNeedWeightTable)
            {
                cfgSerializedHeader.pWeights = num3;
                num3        = (uint)((int)num3 + cArcs * Marshal.SizeOf(typeof(float)));
                pWeights    = new float[cArcs];
                pWeights[0] = 0f;
            }
            else
            {
                cfgSerializedHeader.pWeights = 0u;
                num3 = num3;
            }
            if (_rootRule != null)
            {
                cfgSerializedHeader.ulRootRuleIndex = (uint)_rootRule._iSerialize;
            }
            else
            {
                cfgSerializedHeader.ulRootRuleIndex = uint.MaxValue;
            }
            cfgSerializedHeader.GrammarOptions  = (GrammarOptions)((int)_grammarOptions | ((_alphabet != 0) ? 4 : 0));
            cfgSerializedHeader.GrammarOptions |= (GrammarOptions)((_scriptRefs.Count > 0) ? 18 : 0);
            cfgSerializedHeader.GrammarMode     = (uint)_grammarMode;
            cfgSerializedHeader.cTags           = num;
            cfgSerializedHeader.tags            = num3;
            num3 = (uint)((int)num3 + num * Marshal.SizeOf(typeof(CfgSemanticTag)));
            cfgSerializedHeader.cScripts = _scriptRefs.Count;
            cfgSerializedHeader.pScripts = ((cfgSerializedHeader.cScripts > 0) ? num3 : 0u);
            num3 = (uint)((int)num3 + _scriptRefs.Count * Marshal.SizeOf(typeof(CfgScriptRef)));
            cfgSerializedHeader.cIL = ((_il != null) ? _il.Length : 0);
            cfgSerializedHeader.pIL = ((cfgSerializedHeader.cIL > 0) ? num3 : 0u);
            num3 = (uint)((int)num3 + cfgSerializedHeader.cIL * Marshal.SizeOf(typeof(byte)));
            cfgSerializedHeader.cPDB = ((_pdb != null) ? _pdb.Length : 0);
            cfgSerializedHeader.pPDB = ((cfgSerializedHeader.cPDB > 0) ? num3 : 0u);
            num3 = (cfgSerializedHeader.ulTotalSerializedSize = (uint)((int)num3 + cfgSerializedHeader.cPDB * Marshal.SizeOf(typeof(byte))));
            return(cfgSerializedHeader);
        }
Exemple #2
0
        internal void Commit(StreamMarshaler streamBuffer)
        {
            long         position = streamBuffer.Stream.Position;
            List <State> list     = new List <State>(_states);

            _states = null;
            list.Sort();
            ValidateAndTagRules();
            CheckLeftRecursion(list);
            int num    = (_basePath != null) ? (_basePath.Length + 1) : 0;
            int idWord = 0;

            if (_globalTags.Count > 0)
            {
                StringBuilder stringBuilder = new StringBuilder();
                foreach (string globalTag in _globalTags)
                {
                    stringBuilder.Append(globalTag);
                }
                _symbols.Add(stringBuilder.ToString(), out idWord);
                idWord = _symbols.OffsetFromId(idWord);
                if (idWord > 65535)
                {
                    throw new OverflowException(SR.Get(SRID.TooManyRulesWithSemanticsGlobals));
                }
            }
            foreach (ScriptRef scriptRef in _scriptRefs)
            {
                _symbols.Add(scriptRef._sMethod, out scriptRef._idSymbol);
            }
            int cArcs;

            float[] pWeights;
            CfgGrammar.CfgSerializedHeader o = BuildHeader(list, num, (ushort)idWord, out cArcs, out pWeights);
            streamBuffer.WriteStream(o);
            streamBuffer.WriteArrayChar(_words.SerializeData(), _words.SerializeSize());
            streamBuffer.WriteArrayChar(_symbols.SerializeData(), _symbols.SerializeSize());
            foreach (Rule rule in _rules)
            {
                rule.Serialize(streamBuffer);
            }
            if (num > 0)
            {
                streamBuffer.WriteArrayChar(_basePath.ToCharArray(), _basePath.Length);
                streamBuffer.WriteArrayChar(_achZero, 1);
                streamBuffer.WriteArray(_abZero3, (num * 2) & 3);
            }
            streamBuffer.WriteStream(default(CfgArc));
            int  iOffset    = 1;
            uint iArcOffset = 1u;
            bool flag       = (GrammarOptions & GrammarOptions.MssV1) == GrammarOptions.MssV1;

            foreach (State item in list)
            {
                item.SerializeStateEntries(streamBuffer, flag, pWeights, ref iArcOffset, ref iOffset);
            }
            if (_fNeedWeightTable)
            {
                streamBuffer.WriteArray(pWeights, cArcs);
            }
            if (!flag)
            {
                foreach (State item2 in list)
                {
                    item2.SetEndArcIndexForTags();
                }
            }
            for (int num2 = _tags.Count - 1; num2 >= 0; num2--)
            {
                if (_tags[num2]._cfgTag.ArcIndex == 0)
                {
                    _tags.RemoveAt(num2);
                }
            }
            _tags.Sort();
            foreach (Tag tag in _tags)
            {
                tag.Serialize(streamBuffer);
            }
            foreach (ScriptRef scriptRef2 in _scriptRefs)
            {
                scriptRef2.Serialize(_symbols, streamBuffer);
            }
            if (_il != null && _il.Length != 0)
            {
                streamBuffer.Stream.Write(_il, 0, _il.Length);
            }
            if (_pdb != null && _pdb.Length != 0)
            {
                streamBuffer.Stream.Write(_pdb, 0, _pdb.Length);
            }
        }