Skip to content

Commit

Permalink
Add LexerErrorListener
Browse files Browse the repository at this point in the history
  • Loading branch information
chacha912 committed Oct 7, 2024
1 parent 2379af8 commit e48872a
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Recognizer,
Token,
CommonToken,
RecognitionException,
} from 'antlr4ts';
import { ParseTree } from 'antlr4ts/tree/ParseTree';
import { YorkieSchemaLexer } from '../antlr/YorkieSchemaLexer';
Expand Down Expand Up @@ -47,15 +48,40 @@ class Visitor implements YorkieSchemaVisitor<Node> {
}
}

class LexerErrorListener implements ANTLRErrorListener<number> {
constructor(private errorList: Diagnostic[]) {}

syntaxError<T extends number>(
_recognizer: Recognizer<T, any>,
_offendingSymbol: T | undefined,
line: number,
charPositionInLine: number,
msg: string,
_e: RecognitionException | undefined,
): void {
let error: Diagnostic = {
severity: 'error',
message: msg,
range: {
start: { column: charPositionInLine, line },
end: { column: charPositionInLine + 1, line },
},
};

this.errorList.push(error);
}
}

class ParserErrorListener implements ANTLRErrorListener<CommonToken> {
constructor(private errorList: Diagnostic[]) {}

syntaxError<T extends Token>(
_: Recognizer<T, any>,
_recognizer: Recognizer<T, any>,
offendingSymbol: T | undefined,
line: number,
charPositionInLine: number,
msg: string,
_e: RecognitionException | undefined,
): void {
const error: Diagnostic = {
severity: 'error',
Expand Down Expand Up @@ -94,9 +120,8 @@ export function getDiagnostics(data: string): Diagnostic[] {

const stream = CharStreams.fromString(data);
const lexer = new YorkieSchemaLexer(stream);
// TODO(hackerwins): Add error listener to lexer.
// lexer.removeErrorListeners();
// lexer.addErrorListener(new LexerErrorListener(diagnostics));
lexer.removeErrorListeners();
lexer.addErrorListener(new LexerErrorListener(diagnostics));

const tokens = new CommonTokenStream(lexer);
const parser = new YorkieSchemaParser(tokens);
Expand Down

0 comments on commit e48872a

Please sign in to comment.