Udostępnij za pośrednictwem


Interfejs IScanner

Używany jako interfejs dla analizatora składni języka w służbie językowej.

Przestrzeń nazw:  Microsoft.VisualStudio.Package
Zestawy:   Microsoft.VisualStudio.Package.LanguageService.10.0 (w Microsoft.VisualStudio.Package.LanguageService.10.0.dll)
  Microsoft.VisualStudio.Package.LanguageService.11.0 (w Microsoft.VisualStudio.Package.LanguageService.11.0.dll)
  Microsoft.VisualStudio.Package.LanguageService.9.0 (w Microsoft.VisualStudio.Package.LanguageService.9.0.dll)
  Microsoft.VisualStudio.Package.LanguageService (w Microsoft.VisualStudio.Package.LanguageService.dll)

Składnia

'Deklaracja
Public Interface IScanner
public interface IScanner

Typ IScanner uwidacznia następujące elementy członkowskie.

Metody

  Nazwa Opis
Metoda publiczna ScanTokenAndProvideInfoAboutIt Analizuje dalej tokenu języka z bieżącego wiersza i zwraca informacje na jego temat.
Metoda publiczna SetSource Ustawia wiersz należy przeanalizować.

Początek

Uwagi

Usługa języka musi przeanalizować kodu do obsługi funkcji oferowanych przez Visual Studio dla deweloperów.Na przykład wyróżnianie składni wymaga znajomości różnych elementów języka zapewnienie odpowiedniego koloru, natomiast kod zakończenia wymaga znajomości bieżącego zakresu.Identyfikujące typ tokenów jest roli skanera, podczas gdy identyfikujący funkcje i zakres token jest roli analizator składni.

Uwagi dotyczące implementacji

Do analizy kodu, należy zaimplementować IScanner interfejsu klasy i wywołać konstruktora klasy w danej implementacji GetScanner.

Uwagi dotyczące wywoływania

Skaner jest używany przez Colorizer klasy obsłużyć wyróżnianie składni i są zazwyczaj dostarczane do konstruktora colorizer.Analizator składni można również inne funkcje języka, takie jak identyfikacji token wyzwalaczy, inicjujących, analizowania bardziej złożone przez ParseSource metody.

Przykłady

Poniżej przedstawiono przykład prostego realizacji tego interfejsu.

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

Zobacz też

Informacje

Przestrzeń nazw Microsoft.VisualStudio.Package