共用方式為


IScanner Interface

Used as the interface for a language parser in a language service.

Namespace:  Microsoft.VisualStudio.Package
Assemblies:   Microsoft.VisualStudio.Package.LanguageService.9.0 (in Microsoft.VisualStudio.Package.LanguageService.9.0.dll)
  Microsoft.VisualStudio.Package.LanguageService (in Microsoft.VisualStudio.Package.LanguageService.dll)
  Microsoft.VisualStudio.Package.LanguageService.10.0 (in Microsoft.VisualStudio.Package.LanguageService.10.0.dll)

Syntax

'宣告
Public Interface IScanner
'用途
Dim instance As IScanner
public interface IScanner
public interface class IScanner
type IScanner =  interface end
public interface IScanner

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.

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 method.

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 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;
        }
    }
}

See Also

Reference

IScanner Members

Microsoft.VisualStudio.Package Namespace