Beispiel #1
0
        private void AddKeywords(Match m)
        {
            if (!this.IsProcessingTokens) {
                return;
            }

            string keywordGroup = m.Groups["keywordGroup"].Value;

            foreach (Capture capture in m.Groups["keyword"].Captures) {
                Keyword k = new Keyword(this.SourceFile.SyntaxContext, keywordGroup, capture.Value, this.m_ignoreCase);
                this.ProcessOptions(k, m);
            }
        }
Beispiel #2
0
        /// <devdoc>
        /// SHOULD: This whole mess needs to be replaced with a mini-state machine for keywords, which is easy to build
        /// since keywords have no magic chars, only literal chars. Would be _much_ faster and would not instantiate
        /// any objects.
        /// </devdoc>
        private bool TryKeyword(ref int posAdvanceTo)
        {
            if (0 == this.TopScope.ActiveSyntaxItems.IdxFirstNonKeyword || !this.m_syntaxDefinition.IsKeywordChar(this[posAdvanceTo]))
            {
                return(false);
            }

            if (0 < this.Reader.PosCurrent && this.m_syntaxDefinition.IsKeywordChar(this[posAdvanceTo - 1]))
            {
                return(false);
            }

            int posAfterKeyword = posAdvanceTo;

            this.m_possibleKeyword.Length = 0;

            while (posAfterKeyword <= this.PosWindowEnd && this.m_syntaxDefinition.IsKeywordChar(this[posAfterKeyword]))
            {
                this.m_possibleKeyword.Append(this[posAfterKeyword]);
                posAfterKeyword++;
            }

            string possibleKeyword      = this.m_possibleKeyword.ToString();
            string upperPossibleKeyword = possibleKeyword.ToUpperInvariant();

            int idxFirstCandidate = this.m_syntaxDefinition.UpperSortedKeywordNames.BinarySearch(upperPossibleKeyword, StringComparer.Ordinal);

            if (idxFirstCandidate < 0)
            {
                if (!this.m_syntaxDefinition.HasPartialKeywordMatches)
                {
                    return(false);
                }

                idxFirstCandidate = ~idxFirstCandidate;
            }
            else
            {
                // we found a match, but not necessarily the first one. This is because keywords with the same name might exist, and
                // the BinarySearch might find one that is not the first. So we try going backwards in the list to make sure we got the first one.
                while ((idxFirstCandidate > 0) && this.m_syntaxDefinition.UpperSortedKeywordNames[idxFirstCandidate - 1].Equals(upperPossibleKeyword, StringComparison.Ordinal))
                {
                    idxFirstCandidate--;
                }
            }

            int     idxBestMatch = int.MaxValue;
            Keyword bestMatch    = null;

            for (int i = idxFirstCandidate; i < this.m_syntaxDefinition.KeywordsSortedByName.Count; i++)
            {
                if (!this.m_syntaxDefinition.UpperSortedKeywordNames[i].StartsWith(upperPossibleKeyword))
                {
                    break;
                }

                Keyword candidate = this.m_syntaxDefinition.KeywordsSortedByName[i];

                if (!candidate.IsMatch(possibleKeyword))
                {
                    continue;
                }

                int idxInScope = Array.BinarySearch <SyntaxItem>(this.TopScope.ActiveSyntaxItems.Items, candidate, SyntaxItem.Comparer);
                if ((idxInScope > -1) && (idxInScope < idxBestMatch))
                {
                    idxBestMatch = idxInScope;
                    bestMatch    = candidate;
                }
            }

            if (null == bestMatch)
            {
                return(false);
            }

            DoKeywordMatch(bestMatch, posAfterKeyword);
            posAdvanceTo = posAfterKeyword;
            this.ClearPendingMatch();
            return(true);
        }