ReSharper Platform SDK
 
Because ReSharper Platform SDK undefined is still in development, this documentation may not be entirely accurate and is subject to change.

Implementing a lexer

Last modified: 04 July 2023

ReSharper requires a custom language to create a lexer that implements (at least) the ILexer interface:

The IBuffer is given to the lexer in the constructor, via ILexerFactory.

Clients of the lexer will follow these steps:

  • Call Start to get the lexer to recognise the first token.

  • Retrieve the current token type from the TokenType property, which will be a singleton instance of a language-specific class that derives from TokenNodeType (see the guide on Token Node Types for more details).

  • Use the TokenStart and TokenEnd properties to retrieve the offset of the token start and end in the text buffer. This is required because the token type is a singleton instance, and therefore cannot contain details about the location and length of the token itself. The start offset is inclusive, and the end offset is exclusive, just like TextRange (e.g. given "Hello world", the text range (0, 5) returns "Hello").

  • Call the Advance method repeatedly, to move to the next token, which will update the TokenType, TokenStart and TokenEnd properties with the information about the current token and location.

The CurrentPosition property is a lexer specific object that encapsulates the information required by the lexer to save and restore the current location. The LexerStateCookie class can be used by parsers to make it easy to rollback to a specific state in the lexer. It implements the IDisposable interface, so it can be used in using statement:

This can be used to implement lookahead, retrieving a number of tokens ahead, then rolling back to the current position (see Lexer Utility Methods for more details).