Share via


IParseSink::MatchPair Method

Called when a matching pair is parsed.

HRESULT MatchPair ( 
   long startLine1,
   long startIdx1,
   long endLine1,
   long endIdx1,
   long startLine2,
   long startIdx2,
   long endLine2,
   long endIdx2
);

Parameters

  • startLine1
    [in] Specifies the starting line on which the first item in a matching pair occurs.

  • startIdx1
    [in] Specifies the starting index position on startLine1 for the first item in a matching pair.

  • endLine1
    [in] Specifies the ending line on which the first item in a matching pair occurs.

  • endIdx1
    [in] Specifies the ending index position on endLine1 for the first item in a matching pair.

  • startLine2
    [in] Specifies the starting line on which the second item in a matching pair occurs.

  • startIdx2
    [in] Specifies the starting index position on startLine1 for the second item in a matching pair.

  • endLine2
    [in] Specifies the ending line on which the second item in a matching pair occurs.

  • endIdx2
    [in] Specifies the ending index position on endLine1 for the second item in a matching pair.

Return Value

If the method succeeds, it returns S_OK. If it fails, it returns an error code.

Remarks

Examples of matching pairs include matching braces "{ }", and matching statements (for example, "If" and "Then"). Call this method only if your IBabelService::ParseSource Method implementation is called with a ParseReason Enumeration value of ReasonHighlightBraces or ReasonMatchBraces in the reason parameter. You should call IParseSink::MatchPair once for each matching pair you find in the span of text passed to you in the text parameter of IBabelService::ParseSource Method.

The information in the MatchPair method is used when auto-highlight matching braces is enabled or when a user presses CTRL+] in Visual Studio. Enable automatic highlighting of marching braces using the MatchBraces, ShowMatchingBrace and MatchBracesAtCaret registry entries. For more information, see Babel Registry Information.

The Default Babel Implementation in the Language Service Package, this method is called from the StdService::matchPair method that in turn can be called from within your grammar file. The Example in this topic shows how the matchPair method is called when parentheses and an if/else statement are parsed.

For a more detailed discussion of brace matching, see How to: Provide Automatic Brace Matching.

Example

This is an edited fragment from the default parser.y grammar file that is created using the Visual Studio Language Service Wizard. This fragment shows how an if/else statement is parsed and how, for both the parentheses and the keywords, the matchPair method is called on the language service (g_service). The matchPair method eventually calls the IParseSink::MatchPair method.

Statement
    : /* other statements types cut from this example */
    | KWIF ParenExprAlways Statement
    | KWIF ParenExprAlways Statement KWELSE Statement
                                { g_service->matchPair($1,$4); }
    ;

ParenExprAlways
    : ParenExpr
    | error ')'                 { g_service->syntaxError( "expression" ); }
    | error                     { g_service->syntaxError( "expression" ); }
    ;

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

See Also

Concepts

Babel Registry Information

How to: Provide Automatic Brace Matching

Reference

IParseSink Interface

IBabelService::ParseSource Method

ParseReason Enumeration