Using the Default Babel Implementation

The default implementation of IBabelService Interface allows you to easily integrate your language service in Visual Studio by providing a lexical and grammar specification for your language. If you use the default implementation, you get support for the following features:

  • Parameter Info ToolTips

  • Tip information

  • Statement completion

  • Brace matching

  • Syntax highlighting (including custom colors)

  • Error markers

  • Block comments

  • Autos window support in the Debugger

    注意

    To get Autos window support in the Debugger you need to provide an expression evaluator. For more information, see Providing Autos Window Support for Babel.

Default Babel Architecture

The architecture of the default Babel implementation is shown in the following diagram.

Overview of the default Babel implementation
Babel Declarative Overview

Implementation

The default Babel implementation of the IBabelService Interface is contained in the StdService and CommentService classes in the Visual C++ library common.lib. These classes assume the use of a standard lexer (lex or flex) and parser (yacc or bison) to parse the source text for your language: the lexer is accessed through the standard yylex method and the parser is accessed through the standard yyparse method. Just link your implementations of yylex and yyparse to the common.lib to give the default Babel implementation access to your language syntax.

In addition, override the getTokenInfo method in the StdService class to provide a list of tokens and their characteristics to support syntax highlighting and token triggers. This list is an array of TokenInfo objects that specify the token ID, colorable item class, token type, description, and triggers. The TokenInfo structure, the StdService and CommentService classes, and all other support classes are defined in stdservice.h, which is located in the Babel common folder (for example, <InstallPath>\VisualStudioIntegration\Babel\Common\stdservice.h).

You can override additional methods in the StdService class to alter support for various language features. For example, you may want to override the getMethodFormat method to return a different format for methods (this controls how method signatures are displayed in the IntelliSense Parameter Info ToolTip).

ヒント

Include the project file for the Babel common.lib in your project to gain easy access to the library. The project file is located in the VSIP Environment SDK folder, in Babel\Common; for example, <InstallPath>\VisualStudioIntegration\Babel\Common\common.vcproj).

Example

Here is a partial example of implementing the default Babel language service. This example is taken from code that was produced by the Visual Studio Language Service Wizard.

MyLanguageService.h
#include <common.h>
#include <languagedef.h>
#include <stdservice.h>

class Service : CommentService
{
protected:
    override const CommentFormat* getCommentFormat() const;
    override const TokenInfo*     getTokenInfo() const;
}

--------------------------------------------------------------------

MyLanguageService.cpp
#include "MyLanguageService.h"
#include "parser.cpp.h"  // Generated from .lex file.  Holds token IDs.

override const CommentFormat *MyLanguageService::getCommentFormat() const
{
    static CommentFormat commentFormat = { NULL, "/*", "*/", false );
    return commentFormat;
}


override const TokneInfo* MyLanguageService::getTokenInfo() const
{
    static TokenInfo tokenInfoTable[] =
    {
        { IDENTIFIER, ClassIdentifier, "", CharIdentifier, TriggerMatchBraces },
        { NUMBER    , ClassNumber, "", CharLiteral },
        { KWIF      , ClassKeyword, "if", CharKeyword },
        { KWELSE    , ClassKeyword, "else", CharKeyword },
        { '('       , ClassDefault, "", CharDelimiter, TriggerParamStart },
        { ','       , ClassDefault, "", CharDefault, TriggerParam },
        { ')'       , ClassDefault, "", CharDelimiter, TriggerParamEnd },
        { '{'       , ClassDefault, "", CharDelimiter, TriggerMatchBraces },
        { '}'       , ClassDefault, "", CharDelimiter, TriggerMatchBraces },
        // additional entries here.

        // Always end with the 'TokenEnd' token.
        { TokenEnd, ClassTExt, "<unknown>" }
    }

    return tokenInfoTable;
}

A Note About Lexers and Parsers

The default Babel implementation must link to a lexer and parser to scan and parse a source file. The lexer is assumed to be accessible through the yylex method and the parser is assumed to be accessible through the yyparse method. These are standard names for a lexer and parser as produced by the lex and yacc tools (alternatively, you can use the open source versions, flex and bison, to generate the lexer and parser, respectively).

In brief, a lexer transforms raw text into tokens that are used for colorization and token identification. The lex (or flex) tool creates a lexer from a lexical specification, which uses regular expressions to specify the kind of text fragments that constitute tokens.

A parser transforms a list of tokens into an abstract syntax tree of the language and is used for error messages, brace matching, and so on. The yacc (or bison) tool creates a parser from a grammar specification that specifies the grammar of the language using Backus-Naur Form (BNF) expressions. BNF expressions are also referred to as context-free grammars.

The Visual C++ library in the default implementation of IBabelService is tested with Flex version 2.5.4.a and Bison 1.24. When creating your language using the default Babel implementation, use version 1.24 or later of Bison (or version 1.9 or later of yacc), or version 2.5.4a or later of Flex. A sample language integration using the default IBabelService implementation is demonstrated in the My C Packages sample.

See Also

Concepts

Using the Babel Package

My C Packages