Share via


How to: Add Syntax Checking

Adding syntax checking is done in the following three steps (see the procedures that follow for details):

  • Adding grammar rules

  • Setting language properties

  • Adding error productions

This topic is based on the Visual Studio Language Package solution, which provides a basic implementation of the Babel package. For more information about creating one of these solutions, see Walkthrough: Creating a Language Service Package.

Adding Grammar Rules

You need to add grammar rules in order to provide syntax checking. Grammar rules are defined in the parser source file. The default parser.y file in the Visual Studio Language Package solution has examples of these grammar rules.

Setting Language Properties

Language properties are set in the g_LanguageProperty global constant, found in the stdservice_.cpp source file. The following language properties must be set for your language service to provide syntax checking:

  • CodeSense: 1.

  • CodeSenseDelay: 1500. Sets the delay, in milliseconds, before errors are displayed. In practice, a value between 1,000 to 3,000 milliseconds works well.

  • CodeSenseFastOnChangeLine: 1. Causes the editor to display errors immediately when the user leaves a changed line. This property is enabled by default.

  • MaxErrorMessages: 5. Sets the maximum number of displayed error messages per source file). The default value is 5.

These properties

Adding Error Productions

The syntaxError, expectError, and errorMessage methods are used for handling errors. You can specify when to call these methods in the grammar rules of the parser source file to provide specific information about the error and allow parsing to recover.

Use the syntaxError method for general errors during parsing. For example, assuming that SourceFile is the start production, the following is a catch-all error production:

SourceFile: Program
          | error     { g_service->syntaxError( "program" ); }
          ;

Use the expectError method whenever a closing token is expected. The following error production catches absent closing parentheses:

ParenExpr
    : '(' Expr ')'    
    | '(' Expr error  
              { g_service->expectError( "unmatched parenthesis", ")" ); }
    ;

The current token may not always be the same as the location of the error. In the following example the error has occurred between the pair of parentheses, so the second parameter of the syntaxError method is specified as $2, that is the second member of the sequence. Token numbers start at 1.

ParenParams
    :  '(' ')'                   { $$ = $2; g_service->matchPair($1,$2); }
    |  '(' Params1 ')'           { $$ = $3; g_service->matchPair($1,$3); }
    |  '(' Params1 error         { $$ = $2; g_service->expectError( "unmatched parenthesis", ")" ); }
    |  '(' error ')'             { $$ = $3;
                                   g_service->matchPair($1,$3); 
                                   g_service->syntaxError( "parameters", &$2 ); }

Change History

Date

History

Reason

July 2008

Rewrote and refactored project.

Content bug fix.