44 * terms of the MIT License, which is available in the project root.
55 ******************************************************************************/
66
7- import type { ILexingError , IMultiModeLexerDefinition , IToken , TokenType , TokenTypeDictionary , TokenVocabulary } from 'chevrotain' ;
7+ import type { ILexerErrorMessageProvider , ILexingError , IMultiModeLexerDefinition , IToken , TokenType , TokenTypeDictionary , TokenVocabulary } from 'chevrotain' ;
88import type { LangiumCoreServices } from '../services.js' ;
9- import { Lexer as ChevrotainLexer } from 'chevrotain' ;
9+ import { Lexer as ChevrotainLexer , defaultLexerErrorProvider } from 'chevrotain' ;
1010import type { LexingReport , TokenBuilder } from './token-builder.js' ;
1111
12+ export class DefaultLexerErrorMessageProvider implements ILexerErrorMessageProvider {
13+
14+ buildUnexpectedCharactersMessage ( fullText : string , startOffset : number , length : number , line ?: number , column ?: number ) : string {
15+ return defaultLexerErrorProvider . buildUnexpectedCharactersMessage ( fullText , startOffset , length , line , column ) ;
16+ }
17+
18+ buildUnableToPopLexerModeMessage ( token : IToken ) : string {
19+ return defaultLexerErrorProvider . buildUnableToPopLexerModeMessage ( token ) ;
20+ }
21+ }
22+
1223export interface LexerResult {
1324 /**
1425 * A list of all tokens that were lexed from the input.
@@ -40,11 +51,13 @@ export interface Lexer {
4051
4152export class DefaultLexer implements Lexer {
4253
43- protected chevrotainLexer : ChevrotainLexer ;
44- protected tokenBuilder : TokenBuilder ;
54+ protected readonly tokenBuilder : TokenBuilder ;
55+ protected readonly errorMessageProvider : ILexerErrorMessageProvider ;
4556 protected tokenTypes : TokenTypeDictionary ;
57+ protected chevrotainLexer : ChevrotainLexer ;
4658
4759 constructor ( services : LangiumCoreServices ) {
60+ this . errorMessageProvider = services . parser . LexerErrorMessageProvider ;
4861 this . tokenBuilder = services . parser . TokenBuilder ;
4962 const tokens = this . tokenBuilder . buildTokens ( services . Grammar , {
5063 caseInsensitive : services . LanguageMetaData . caseInsensitive
@@ -54,7 +67,8 @@ export class DefaultLexer implements Lexer {
5467 const production = services . LanguageMetaData . mode === 'production' ;
5568 this . chevrotainLexer = new ChevrotainLexer ( lexerTokens , {
5669 positionTracking : 'full' ,
57- skipValidations : production
70+ skipValidations : production ,
71+ errorMessageProvider : this . errorMessageProvider
5872 } ) ;
5973 }
6074
0 commit comments