共用方式為


IScanner Interface

Definition

Scans individual source lines and provides coloring and trigger information about tokens.

public interface class IScanner
public interface class IScanner
__interface IScanner
public interface IScanner
type IScanner = interface
Public Interface IScanner

Examples

Here is an example of a simple implementation of this interface.

[C#]  
namespace MyLanguagePackage  
{  
    class CScanner : IScanner  
    {  
        /////////////////////////////////////////////////////  
        // Fields  

        private string m_line;  
        private int    m_offset;  
        private string m_source;  

        /////////////////////////////////////////////////////  
        // Enumerations  

        private enum ParseState  
        {  
            InText = 0,  
            InQuotes = 1,  
            InComment = 2  
        }  

        /////////////////////////////////////////////////////  
        // Private methods  

        private bool GetNextToken(int startIndex,  
                                 TokenInfo tokenInfo,  
                                 ref int state)  
        {  
            bool bFoundToken = false;  
            int endIndex = -1;  
            int index = startIndex;  
            if (index < m_source.Length)  
            {  
                if (state == (int) ParseState.InQuotes)  
                {  
                    // Find end quote. If found, set state to InText  
                    // and return the quoted string as a single token.  
                    // Otherwise, return the string to the end of the line  
                    // and keep the same state.  
                }  
                else if (state == (int) ParseState.InComment)  
                {  
                    // Find end of comment. If found, set state to InText  
                    // and return the comment as a single token.  
                    // Otherwise, return the comment to the end of the line  
                    // and keep the same state.  
                }  
                else  
                {  
                    // Parse the token starting at index, returning the  
                    // token's start and end index in tokenInfo, along with  
                    // the token's type and color to use.  
                    // If the token is a quoted string and the string continues  
                    // on the next line, set state to InQuotes.  
                    // If the token is a comment and the comment continues  
                    // on the next line, set state to InComment.  
                    bFoundToken = true;  
                }  
            }  
            return bFoundToken;  
        }  

        /////////////////////////////////////////////////////  
        // IScanner methods  

        public bool ScanTokenAndProvideInfoAboutIt(TokenInfo tokenInfo,  
                                                   ref int state)  
        {  
            bool bFound = false;  
            if (tokenInfo != null)  
            {  
                bFound = GetNextToken(m_offset, tokenInfo, ref state);  
                if (bFound)  
                {  
                    m_offset = tokenInfo.EndIndex + 1;  
                }  
            }  
            return bFound;  
        }  

        public void SetSource(string source, int offset)  
        {  
            m_offset = offset;  
            m_source = source;  
        }  
    }  
}  

Remarks

A language service must parse code in order to support any of the features offered by Visual Studio to developers. For example, syntax highlighting requires knowledge of the different elements of the language to provide the right color, while code completion requires knowledge of the current scope. Identifying the type of tokens is the role of the scanner, while identifying the functionality and scope of a token is the role of a parser.

Notes to Implementers

In order to parse code, you must implement the IScanner interface on a class and call the constructor of that class in your implementation of GetScanner(IVsTextLines).

Notes to Callers

The scanner is used by the Colorizer class to handle syntax highlighting and is typically supplied to the colorizer constructor. The parser can also be used for other language features, such as identifying token triggers that initiate more complex parsing through the ParseSource(ParseRequest) method.

Methods

ScanTokenAndProvideInfoAboutIt(TokenInfo, Int32)

Scan the next token and fill in syntax coloring details about it in tokenInfo.

SetSource(String, Int32)

Used to (re)initialize the scanner before scanning a small portion of text, such as single source line for syntax coloring purposes

Applies to