Ejemplo n.º 1
0
        public override int GetHashCode()
        {
            var hashCode = 0;

            for (var i = 0; i < Length; i++)
            {
                hashCode = HashCode.ComputeIncrementalHash(this[i].GetHashCode(), hashCode, i == 0);
            }
            return(hashCode);
        }
Ejemplo n.º 2
0
            private static int ComputeHashCode(int[] states)
            {
                var hashCode = 0;

                for (int i = 0; i < states.Length; i++)
                {
                    hashCode = HashCode.ComputeIncrementalHash(states[i].GetHashCode(), hashCode, i == 0);
                }
                return(hashCode);
            }
Ejemplo n.º 3
0
        private int ComputeHashCode()
        {
            var hash = HashCode.ComputeIncrementalHash(LeftHandSide.GetHashCode(), 0, true);

            for (var s = 0; s < RightHandSide.Count; s++)
            {
                var symbol = RightHandSide[s];
                hash = HashCode.ComputeIncrementalHash(symbol.GetHashCode(), hash);
            }
            return(hash);
        }
Ejemplo n.º 4
0
        public IReadOnlyList <ILexerRule> GetExpectedLexerRules()
        {
            var frameSets     = _chart.Sets;
            var frameSetCount = frameSets.Count;

            if (frameSetCount == 0)
            {
                return(EmptyLexerRules);
            }

            var hashCode = 0;
            var count    = 0;

            if (_expectedLexerRuleIndicies == null)
            {
                _expectedLexerRuleIndicies = new BitArray(Grammar.LexerRules.Count);
            }
            else
            {
                _expectedLexerRuleIndicies.SetAll(false);
            }

            var frameSet = frameSets[frameSets.Count - 1];

            for (var i = 0; i < frameSet.States.Count; i++)
            {
                var stateFrame = frameSet.States[i];
                for (int j = 0; j < stateFrame.DottedRuleSet.ScanKeys.Count; j++)
                {
                    var lexerRule = stateFrame.DottedRuleSet.ScanKeys[j];
                    var index     = Grammar.GetLexerRuleIndex(lexerRule);
                    if (index < 0)
                    {
                        continue;
                    }
                    if (_expectedLexerRuleIndicies[index])
                    {
                        continue;
                    }

                    _expectedLexerRuleIndicies[index] = true;
                    hashCode = HashCode.ComputeIncrementalHash(lexerRule.GetHashCode(), hashCode, count == 0);
                    count++;
                }
            }

            if (_expectedLexerRuleCache == null)
            {
                _expectedLexerRuleCache = new Dictionary <int, ILexerRule[]>();
            }

            // if the hash is found in the cached lexer rule lists, return the cached array
            ILexerRule[] cachedLexerRules = null;
            if (_expectedLexerRuleCache.TryGetValue(hashCode, out cachedLexerRules))
            {
                return(cachedLexerRules);
            }

            // compute the new lexer rule array and add it to the cache
            var array           = new ILexerRule[count];
            var returnItemIndex = 0;

            for (var i = 0; i < Grammar.LexerRules.Count; i++)
            {
                if (_expectedLexerRuleIndicies[i])
                {
                    array[returnItemIndex] = Grammar.LexerRules[i];
                    returnItemIndex++;
                }
            }

            _expectedLexerRuleCache.Add(hashCode, array);

            return(array);
        }
Ejemplo n.º 5
0
        public IReadOnlyList <ILexerRule> GetExpectedLexerRules()
        {
            var earleySets       = _chart.EarleySets;
            var currentIndex     = earleySets.Count - 1;
            var currentEarleySet = earleySets[currentIndex];
            var scanStates       = currentEarleySet.Scans;

            if (scanStates.Count == 0)
            {
                return(EmptyLexerRules);
            }

            var hashCode = 0;
            var count    = 0;

            if (_expectedLexerRuleIndicies == null)
            {
                _expectedLexerRuleIndicies = new BitArray(Grammar.LexerRules.Count);
            }
            else
            {
                _expectedLexerRuleIndicies.SetAll(false);
            }

            // compute the lexer rule hash for caching the list of lexer rules
            // compute the unique lexer rule count
            // set bits in the rule index bit array corresponding to the position of the lexer rule in the list of rules
            for (int s = 0; s < scanStates.Count; s++)
            {
                var scanState     = scanStates[s];
                var postDotSymbol = scanState.DottedRule.PostDotSymbol;
                if (postDotSymbol == null || postDotSymbol.SymbolType != SymbolType.LexerRule)
                {
                    continue;
                }

                var lexerRule = postDotSymbol as ILexerRule;
                var index     = Grammar.GetLexerRuleIndex(lexerRule);

                if (index < 0)
                {
                    continue;
                }

                if (_expectedLexerRuleIndicies[index])
                {
                    continue;
                }

                count++;
                _expectedLexerRuleIndicies[index] = true;
                hashCode = HashCode.ComputeIncrementalHash(lexerRule.GetHashCode(), hashCode, hashCode == 0);
            }

            if (_expectedLexerRuleCache == null)
            {
                _expectedLexerRuleCache = new Dictionary <int, ILexerRule[]>();
            }

            // if the hash is found in the cached lexer rule lists, return the cached array
            ILexerRule[] cachedLexerRules = null;
            if (_expectedLexerRuleCache.TryGetValue(hashCode, out cachedLexerRules))
            {
                return(cachedLexerRules);
            }

            // compute the new lexer rule array and add it to the cache
            var array           = new ILexerRule[count];
            var returnItemIndex = 0;

            for (var i = 0; i < Grammar.LexerRules.Count; i++)
            {
                if (_expectedLexerRuleIndicies[i])
                {
                    array[returnItemIndex] = Grammar.LexerRules[i];
                    returnItemIndex++;
                }
            }

            _expectedLexerRuleCache.Add(hashCode, array);

            return(array);
        }