refactor
This commit is contained in:
56
client/src/BranFlakesParseRunner.ts
Normal file
56
client/src/BranFlakesParseRunner.ts
Normal 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 };
|
||||
}
|
Reference in New Issue
Block a user