IScanner 接口

用作接口为语言分析器中的语言服务中。

命名空间:  Microsoft.VisualStudio.Package
程序集:   Microsoft.VisualStudio.Package.LanguageService.10.0(在 Microsoft.VisualStudio.Package.LanguageService.10.0.dll 中)
  Microsoft.VisualStudio.Package.LanguageService.11.0(在 Microsoft.VisualStudio.Package.LanguageService.11.0.dll 中)
  Microsoft.VisualStudio.Package.LanguageService.9.0(在 Microsoft.VisualStudio.Package.LanguageService.9.0.dll 中)
  Microsoft.VisualStudio.Package.LanguageService(在 Microsoft.VisualStudio.Package.LanguageService.dll 中)

语法

声明
Public Interface IScanner
public interface IScanner

IScanner 类型公开以下成员。

方法

  名称 说明
公共方法 ScanTokenAndProvideInfoAboutIt 分析从当前行的下一个语言标记并返回有关它的信息。
公共方法 SetSource 设置要分析的行。

页首

备注

语言服务必须分析代码以支持 Visual Studio 提供的任何功能为开发人员。 例如,在中,而代码执行当前范围内知识,语法显示需要该语言的不同组件的知识提供正确的颜色。 ,而标识标记的功能和大小是分析器,的角色标识标记的类型是扫描程序的角色。

对实现者的说明

为了分析代码,您必须在类中实现 IScanner 接口并调用该类构造函数在 GetScanner实现的。

对调用者的说明

扫描仪通过对显示处理的语法的 Colorizer 类使用和通常提供给 colorizer 构造函数。 该分析器可以为其他语言功能还使用,如标识通过 ParseSource 方法启动更复杂的分析标记触发器。

示例

这是此接口的简单实现的示例。

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

请参见

参考

Microsoft.VisualStudio.Package 命名空间