Ejemplo n.º 1
0
        /* Function: FirstToken
         * Returns a <TokenIterator> at the beginning of the current line.  If the iterator is out of bounds it will be
         * set to one past the last token, regardless of which edge it has gone off.
         */
        public TokenIterator FirstToken(LineBoundsMode boundsMode)
        {
            if (!IsInBounds)
            {
                return(new TokenIterator(tokenizer, tokenizer.TokenCount, tokenizer.RawText.Length,
                                         tokenizer.StartingLineNumber + tokenizer.Lines.Count - 1));
            }
            else
            {
                int rawTextStart, rawTextEnd, tokenStart, tokenEnd;
                CalculateBounds(boundsMode, out rawTextStart, out rawTextEnd, out tokenStart, out tokenEnd);

                return(new TokenIterator(tokenizer, tokenStart, rawTextStart, tokenizer.StartingLineNumber + lineIndex));
            }
        }
Ejemplo n.º 2
0
        /* Function: FindToken
         * Attempts to find the passed string as a token in the line, and set a <TokenIterator> at its position if successful.
         * The string must match the entire token, so "some" will not match "something".
         */
        public bool FindToken(string text, bool ignoreCase, LineBoundsMode boundsMode, out TokenIterator result)
        {
            TokenIterator acrossTokensResult;

            if (FindAcrossTokens(text, ignoreCase, boundsMode, out acrossTokensResult) == false ||
                acrossTokensResult.RawTextLength != text.Length)
            {
                result = new TokenIterator();
                return(false);
            }
            else
            {
                result = acrossTokensResult;
                return(true);
            }
        }
Ejemplo n.º 3
0
        /* Function: GetBounds
         * Sets two <TokenIterators> to the beginning and end of the current line.  If the iterator is out of bounds they
         * will be equal.
         */
        public void GetBounds(LineBoundsMode boundsMode, out TokenIterator lineStart, out TokenIterator lineEnd)
        {
            if (!IsInBounds)
            {
                lineStart = FirstToken(boundsMode);
                lineEnd   = lineStart;
            }
            else
            {
                int rawTextStart, rawTextEnd, tokenStart, tokenEnd;
                CalculateBounds(boundsMode, out rawTextStart, out rawTextEnd, out tokenStart, out tokenEnd);

                lineStart = new TokenIterator(tokenizer, tokenStart, rawTextStart, tokenizer.StartingLineNumber + lineIndex);
                lineEnd   = new TokenIterator(tokenizer, tokenEnd, rawTextEnd, tokenizer.StartingLineNumber + lineIndex);
            }
        }
Ejemplo n.º 4
0
        /* Function: FindAcrossTokens
         * Attempts to find the passed string in the line, and sets a <TokenIterator> at its position if successful.  This function
         * can cross token boundaries, so you can search for "<<" even though that would normally be two tokens.  The result
         * must match complete tokens though, so "<< some" will not match "<< something".
         */
        public bool FindAcrossTokens(string text, bool ignoreCase, LineBoundsMode boundsMode, out TokenIterator result)
        {
            if (!IsInBounds)
            {
                result = new TokenIterator();
                return(false);
            }

            int rawTextStart, rawTextEnd, tokenStart, tokenEnd;

            CalculateBounds(boundsMode, out rawTextStart, out rawTextEnd, out tokenStart, out tokenEnd);

            int resultIndex = tokenizer.RawText.IndexOf(text, rawTextStart, rawTextEnd - rawTextStart,
                                                        (ignoreCase ? StringComparison.CurrentCultureIgnoreCase :
                                                         StringComparison.CurrentCulture));

            if (resultIndex == -1)
            {
                result = new TokenIterator();
                return(false);
            }

            result = new TokenIterator(tokenizer, tokenStart, rawTextStart, LineNumber);

            // Do this instead of NextByCharacters() so we don't cause an exception if it's not on a token boundary.
            while (result.RawTextIndex < resultIndex)
            {
                result.Next();
            }

            if (result.RawTextIndex != resultIndex)
            {
                result = new TokenIterator();
                return(false);
            }

            return(true);
        }
Ejemplo n.º 5
0
        /* Function: Indent
         * Returns the indent of the current line content according to the <LineBoundsMode>, expanding tabs.
         */
        public int Indent(LineBoundsMode boundsMode)
        {
            int rawTextBoundsStart, rawTextBoundsEnd, tokenBoundsStart, tokenBoundsEnd;

            CalculateBounds(boundsMode, out rawTextBoundsStart, out rawTextBoundsEnd, out tokenBoundsStart, out tokenBoundsEnd);

            int    indent  = 0;
            string rawText = tokenizer.RawText;

            for (int i = rawTextIndex; i < rawTextBoundsStart; i++)
            {
                if (rawText[i] == '\t')
                {
                    indent += tokenizer.TabWidth;
                    indent -= (indent % tokenizer.TabWidth);
                }
                else
                {
                    indent++;
                }
            }

            return(indent);
        }
Ejemplo n.º 6
0
        /* Function: Match
         * Applies a regular expression to the line and returns the Match object as if Regex.Match() was called.  If
         * the iterator is out of bounds it will be applied to an empty string.
         */
        public System.Text.RegularExpressions.Match Match(System.Text.RegularExpressions.Regex regex, LineBoundsMode boundsMode)
        {
            if (!IsInBounds)
            {
                return(regex.Match(""));
            }

            int rawTextStart, rawTextEnd, tokenStart, tokenEnd;

            CalculateBounds(boundsMode, out rawTextStart, out rawTextEnd, out tokenStart, out tokenEnd);

            return(regex.Match(tokenizer.RawText, rawTextStart, rawTextEnd - rawTextStart));
        }
Ejemplo n.º 7
0
        /* Function: GetRawTextBounds
         * Returns the location of the line in <Tokenizer.RawText>.
         */
        public void GetRawTextBounds(LineBoundsMode boundsMode, out int lineStartIndex, out int lineEndIndex)
        {
            int tokenStart, tokenEnd;

            CalculateBounds(boundsMode, out lineStartIndex, out lineEndIndex, out tokenStart, out tokenEnd);
        }