Exemple #1
0
 private CfgGrammar.CfgHeader BuildRulesFromBinaryGrammar(CfgGrammar.CfgHeader header, State[] apStateTable, SortedDictionary <int, Rule> ruleFirstArcs, int previousCfgLastRules)
 {
     for (int i = 0; i < header.rules.Length; i++)
     {
         CfgRule cfgRule       = header.rules[i];
         int     firstArcIndex = (int)cfgRule.FirstArcIndex;
         cfgRule._nameOffset = _symbols.OffsetFromId(_symbols.Find(header.pszSymbols.FromOffset(cfgRule._nameOffset)));
         Rule rule = new Rule(this, _symbols.FromOffset(cfgRule._nameOffset), cfgRule, i + previousCfgLastRules, _grammarOptions & GrammarOptions.TagFormat, ref _cImportedRules);
         rule._firstState = _states.CreateNewState(rule);
         _rules.Add(rule);
         if (firstArcIndex > 0)
         {
             ruleFirstArcs.Add((int)cfgRule.FirstArcIndex, rule);
         }
         rule._fStaticRule       = ((!cfgRule.Dynamic) ? true : false);
         rule._cfgRule.DirtyRule = false;
         rule._fHasExitPath      = (rule._fStaticRule ? true : false);
         if (firstArcIndex != 0)
         {
             rule._firstState.SerializeId = (int)cfgRule.FirstArcIndex;
             apStateTable[firstArcIndex]  = rule._firstState;
         }
         if (rule._cfgRule.HasResources)
         {
             throw new NotImplementedException();
         }
         if (header.ulRootRuleIndex == i)
         {
             _rootRule = rule;
         }
         if (rule._cfgRule._nameOffset != 0)
         {
             _nameOffsetRules.Add(rule._cfgRule._nameOffset, rule);
         }
     }
     return(header);
 }
Exemple #2
0
        internal void InitFromBinaryGrammar(StreamMarshaler streamHelper)
        {
            CfgGrammar.CfgHeader header = CfgGrammar.ConvertCfgHeader(streamHelper);
            _words          = header.pszWords;
            _symbols        = header.pszSymbols;
            _grammarOptions = header.GrammarOptions;
            State[] array = new State[header.arcs.Length];
            SortedDictionary <int, Rule> sortedDictionary = new SortedDictionary <int, Rule>();
            int count = _rules.Count;

            BuildRulesFromBinaryGrammar(header, array, sortedDictionary, count);
            Arc[]  array2 = new Arc[header.arcs.Length];
            bool   flag   = true;
            CfgArc cfgArc = default(CfgArc);
            State  state  = null;
            IEnumerator <KeyValuePair <int, Rule> > enumerator = sortedDictionary.GetEnumerator();

            if (enumerator.MoveNext())
            {
                KeyValuePair <int, Rule> current = enumerator.Current;
                Rule value = current.Value;
                for (int i = 1; i < header.arcs.Length; i++)
                {
                    CfgArc cfgArc2 = header.arcs[i];
                    if (cfgArc2.RuleRef)
                    {
                        value._listRules.Add(_rules[(int)cfgArc2.TransitionIndex]);
                    }
                    if (current.Key == i)
                    {
                        value = current.Value;
                        if (enumerator.MoveNext())
                        {
                            current = enumerator.Current;
                        }
                    }
                    if (flag || cfgArc.LastArc)
                    {
                        if (array[i] == null)
                        {
                            uint nextHandle = CfgGrammar.NextHandle;
                            array[i] = new State(value, nextHandle, i);
                            AddState(array[i]);
                        }
                        state = array[i];
                    }
                    int   nextStartArcIndex = (int)cfgArc2.NextStartArcIndex;
                    State end = null;
                    if (state != null && nextStartArcIndex != 0)
                    {
                        if (array[nextStartArcIndex] == null)
                        {
                            uint nextHandle2 = CfgGrammar.NextHandle;
                            array[nextStartArcIndex] = new State(value, nextHandle2, nextStartArcIndex);
                            AddState(array[nextStartArcIndex]);
                        }
                        end = array[nextStartArcIndex];
                    }
                    float flWeight = (header.weights != null) ? header.weights[i] : 1f;
                    Arc   arc;
                    if (cfgArc2.RuleRef)
                    {
                        Rule ruleRef = _rules[(int)cfgArc2.TransitionIndex];
                        arc = new Arc(null, ruleRef, _words, flWeight, 0, null, MatchMode.AllWords, ref _fNeedWeightTable);
                    }
                    else
                    {
                        int transitionIndex = (int)cfgArc2.TransitionIndex;
                        int num             = (transitionIndex == 4194302 || transitionIndex == 4194301 || transitionIndex == 4194303) ? transitionIndex : 0;
                        arc = new Arc((int)((num == 0) ? cfgArc2.TransitionIndex : 0), flWeight, cfgArc2.LowConfRequired ? (-1) : (cfgArc2.HighConfRequired ? 1 : 0), num, MatchMode.AllWords, ref _fNeedWeightTable);
                    }
                    arc.Start = state;
                    arc.End   = end;
                    AddArc(arc);
                    array2[i] = arc;
                    flag      = false;
                    cfgArc    = cfgArc2;
                }
            }
            int j = 1;
            int k = 0;

            for (; j < header.arcs.Length; j++)
            {
                CfgArc cfgArc3 = header.arcs[j];
                if (!cfgArc3.HasSemanticTag)
                {
                    continue;
                }
                for (; k < header.tags.Length && header.tags[k].StartArcIndex == j; k++)
                {
                    CfgSemanticTag cfgTag = header.tags[k];
                    Tag            tag    = new Tag(this, cfgTag);
                    _tags.Add(tag);
                    array2[tag._cfgTag.StartArcIndex].AddStartTag(tag);
                    array2[tag._cfgTag.EndArcIndex].AddEndTag(tag);
                    if (cfgTag._nameOffset > 0)
                    {
                        tag._cfgTag._nameOffset = _symbols.OffsetFromId(_symbols.Find(_symbols.FromOffset(cfgTag._nameOffset)));
                    }
                    else
                    {
                        tag._cfgTag._valueOffset = _symbols.OffsetFromId(_symbols.Find(_symbols.FromOffset(cfgTag._valueOffset)));
                    }
                }
            }
            _fNeedWeightTable = true;
            if (header.BasePath != null)
            {
                SetBasePath(header.BasePath);
            }
            _guid              = header.GrammarGUID;
            _langId            = header.langId;
            _grammarMode       = header.GrammarMode;
            _fLoadedFromBinary = true;
        }