This commit is contained in:
2023-12-15 14:27:11 +05:30
parent f8083b26d2
commit 36ed3328d1
5 changed files with 123 additions and 122 deletions

View File

@@ -0,0 +1,56 @@
import { CharStreams, CommonTokenStream, RecognitionException } from 'antlr4ts';
import { DiagnosticSeverity } from 'vscode-languageserver-types';
import { bfLexer } from './generated/bfLexer';
import { bfParser } from './generated/bfParser';
export interface TranslationError {
line: number;
charPositionInLine: number;
msg: string;
source?: any;
type: DiagnosticSeverity;
error?: RecognitionException;
}
export function getTree(str: string, fn: string) {
const charStreams = CharStreams.fromString(str, fn);
const lexer = new bfLexer(charStreams);
const issues: TranslationError[] = [];
// remove the error listener. We want to put our own
lexer.removeErrorListeners();
lexer.addErrorListener({
syntaxError(source, o, line, charPositionInLine, msg, error) {
issues.push({
line,
charPositionInLine,
msg,
type: DiagnosticSeverity.Error,
source,
error,
});
},
});
const tokenStreams = new CommonTokenStream(lexer);
const parser = new bfParser(tokenStreams);
// remove the error listener. We want to put our own
parser.removeErrorListeners();
parser.addErrorListener({
syntaxError(source, o, line, charPositionInLine, msg, error) {
issues.push({
line,
charPositionInLine,
msg,
type: DiagnosticSeverity.Error,
source,
error,
});
},
});
const tree = parser.program();
return { tree, issues, tokenStreams, charStreams };
}