Walkthrough: Creating a Language Service Package

The Visual Studio 2008 Language Service Package solution helps you create a default Babel language service. This solution integrates your language into Visual Studio and provides language service support for your language, including syntax highlighting, syntax checking, block commenting, brace matching, statement completion, and quick info.

Before You Begin

You will need to have the following components defined before you create the solution.

  • A lexer tool and a parser tool, such as Flex and Bison. Native code lexer and parser tools are not shipped with the Visual Studio 2008 SDK. (The MPLex and MPPG tools generate C# code.) You can download a lexer and parser from a number of different sources on the Internet. You must use version 2.5.4a or later of Flex and version 1.24 or later of Bison. You should download the default bison.simple file as well. If you plan to distribute the tools to third parties, make sure that you are complying with the licensing agreement.

    The tools (for example, flex.exe, bison.exe, and bison.simple) should be copied locally on your machine. The default location is:

    VSSDKInstallDir\VisualStudioIntegration\Tools\Babel

  • A grammar of your language, that is the tokens and the appropriate token syntax, defined in lexer and parser files appropriate for the tools you are planning to use. You may use only one lexer file and one parser file. There are default .lex and .y files at: VSSDKInstallDir\VisualStudioIntegration\Tools\Wizards\Templates\LP

  • A definition of which sets of tokens should trigger brace matching (for example, ( and ), [ and ], or { and }).

  • A definition of which tokens should be colored as the following types:

    • Keyword: a language keyword

    • Identifier: a token that represents a language entity (as distinct from a keyword)

    • Comment: the text delimited with the comment characters

    • Text: a token that is not categorized, for example a variable name

    • String: a literal string

    • Number: a literal number

  • A definition of which tokens should trigger parameter start and parameter end actions.

To create a new Visual Studio Language Package

  1. Start Visual Studio (if you are on the Windows Vista operating system, run as an administrator) and create a new Visual Studio Language Package solution (Other Project TypesExtensibility, Visual Studio Language Package). 

  2. On the Language Information page, provide the name and the file extension (for example, .testext) for your language. The maximum length for the language name is one hundred characters. This information is registered in the Visual Studio environment only, and not in the larger Windows environment.

  3. On the Language Specification page, select the Use default lexer and parser check box to use the default .lex and .y files. The lexer and parser tools (and bison.simple) must be installed in the default location (VSSDKInstallDir\VisualStudioIntegration\Tools\Babel).

    If you wish to specify your own grammar files, clear the Use default lexer and parser check box. You must then fill out all the information on both the Lexer and the Parser tabs, specifying the source file (the .lex or .y file), the command line for running the tool, the names of the output header (by default, parser.cpp.h), and the names of the output source code file (by default, lexer.cpp and parser.cpp).

  4. On the Language Service Options page, select the features you want your language service to support. The options you can select are:

    • Enable syntax coloring

      To make this option fully functional, you must specify the color class (Keyword, Identifier, Comment, Text, String, Number) for each token on the Language Tokens page (see step 5) or update the tokenInfoTable in service.cpp that is generated by the solution. For more information, see How to: Enable Syntax Coloring, specifically the "Mapping tokens to color classes" subtopic.

    • Enable syntax checking (also known as squigglies)

      Select this check box to set the CodeSense registry entry to 1 (VSRegRoot\Languages\Language Services\Language Name) to enable syntax checking. (Squigglies are the wavy red lines under syntactically incorrect code.) For more information on the registry entries, see Babel Registry Information.

      To make this option fully functional, you must provide error productions in your parser source file. For more information on error productions, see How to: Add Syntax Checking, specifically the "Adding Error Productions" subtopic.

    • Allow block comment and uncomment

      For more information, see Enable Block Commenting.

    • Enable brace matching

      Select this check box to set the MatchBraces registry entry to 1 (VSRegRoot\Languages\Language Services\Language Name). To make this option fully functional, you need to add brace matching information into your parser source file. For more information on brace matching, see How to: Provide Automatic Brace Matching.

    • Match brace at cursor

      Select this check box to set the MatchBracesAtCaret registry entry to 1 (VSRegRoot\Languages\Language Services\Language Name) to enable brace matching as the caret moves through the code file.

    • Enable statement completion (complete word)

      Select this check box to set the ShowCompletion and SortMemberList registry entries to 1 (VSRegRoot\Languages\Language Services\Language Name) to enable statement completion. A default implementation of IScope Interface is also added to the language.

    • Enable quick info

      Select this check box to set the QuickInfo registry entry to 1 (VSRegRoot\Languages\Language Services\Language Name). This enables quick info tips to be displayed when the user hovers the mouse over an identifier.

  5. On the Language Tokens page, define the types, colors, and triggers for your tokens.

    The Load button loads a list of tokens from your parser source file (if that file follows the general yacc/Bison format). You can then select tokens from the list to specify how they should be treated. You can define the type of token by specifying its Char Class and how it should be colorized by specifying its Color Class. You can also specify triggers such as brace matching. After completing the definition of each token (or group of tokens), click Apply.

    For more information on triggers, see How to: Provide Automatic Brace Matching specifically the "Adding Triggers" subtopic, and TriggerClass Enumeration.

    You can add, rename, and delete tokens on this page. However, you must make sure that your lexer and parser source files define all the tokens you specify on this page.

  6. Click Finish to generate the code for your Language Service VSPackage.

Testing Your Language Service

To test your language service

  1. Build the solution and run it.

  2. In the new instance of Visual Studio, create a new text file and save it with the extension of your language.

  3. As you type text in this file, you should see that the features you defined for your language service are working. For example, you should see syntax highlighting with the colors you specified above, and brace matching for the braces you defined.

For a discussion of the code generated for the Visual Studio Language Service Package solution, see The Default Babel Implementation in the Language Service Package.

Change History

Date

History

Reason

July 2008

Rewrote and refactored project.

Content bug fix.