refactor: migrate antlr4 v4.12.0 to antlr4ts(4.9.0) (#106)
* build: ignore gen folder * refactor: remove useless code * fix: correct the Javascript usage in grammar * refactor: move to antlr4ts * fix: remove useless * fix: update grammars for javascript target * refactor: migrate to antlr4ts * refactor: migrate to antlr4ts * refactor: implements ParserErrorListener * fix: rename the start reserved word * refactor: remove unused import * refactor: migrate to antlr4ts * test: update the expects of test cases * refactor: migrate hive to antlr4ts * refactor: update the incompatible syntax for antlr4ts * refactor: migrate pgsql grammar to antlr4ts, increasing tests * refactor: migrate the plsql to antlr4ts * build: remove unused config * build: migrate to antlr4ts * build: migrate ts-jest to @swc/jest * refactor: migrate to anltr4ts * build: migrate ts-jest to @swc/jest
This commit is contained in:
@ -1,12 +1,13 @@
|
||||
import { ParseTreeWalker, CommonTokenStream } from 'antlr4';
|
||||
import type { Parser } from 'antlr4/src/antlr4';
|
||||
import { Parser } from 'antlr4ts';
|
||||
import { ParseTreeWalker } from 'antlr4ts/tree';
|
||||
|
||||
import ParserErrorListener, {
|
||||
ParserError,
|
||||
ErrorHandler,
|
||||
ParserErrorCollector,
|
||||
} from './parserErrorListener';
|
||||
|
||||
interface IParser {
|
||||
interface IParser extends Parser {
|
||||
// Lost in type definition
|
||||
ruleNames: string[];
|
||||
// Customized in our parser
|
||||
@ -17,7 +18,7 @@ interface IParser {
|
||||
* Custom Parser class, subclass needs extends it.
|
||||
*/
|
||||
export default abstract class BasicParser {
|
||||
private _parser: IParser & Parser;
|
||||
private _parser: IParser;
|
||||
|
||||
public parse(
|
||||
input: string,
|
||||
@ -66,16 +67,14 @@ export default abstract class BasicParser {
|
||||
*/
|
||||
public getAllTokens(input: string): string[] {
|
||||
const lexer = this.createLexer(input);
|
||||
const tokensStream = new CommonTokenStream(lexer);
|
||||
tokensStream.fill();
|
||||
return tokensStream.tokens;
|
||||
return lexer.getAllTokens().map(token => token.text);
|
||||
};
|
||||
|
||||
/**
|
||||
* Get Parser instance by input string
|
||||
* @param input
|
||||
*/
|
||||
public createParser(input: string): IParser & Parser {
|
||||
public createParser(input: string): IParser {
|
||||
const lexer = this.createLexer(input);
|
||||
const parser: any = this.createParserFromLexer(lexer);
|
||||
parser.buildParseTrees = true;
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { Token, Recognizer, ErrorListener, RecognitionException } from 'antlr4';
|
||||
import { Token, Recognizer, ParserErrorListener, RecognitionException } from 'antlr4ts';
|
||||
export interface ParserError {
|
||||
startLine: number;
|
||||
endLine: number;
|
||||
@ -8,7 +8,7 @@ export interface ParserError {
|
||||
}
|
||||
|
||||
export interface SyntaxError<T> {
|
||||
recognizer: Recognizer<T>;
|
||||
recognizer: Recognizer<T, any>;
|
||||
offendingSymbol: Token;
|
||||
line: number;
|
||||
charPositionInLine: number;
|
||||
@ -22,16 +22,15 @@ type ErrorOffendingSymbol = {
|
||||
|
||||
export type ErrorHandler<T> = (err: ParserError, errOption: SyntaxError<T>) => void;
|
||||
|
||||
export class ParserErrorCollector extends ErrorListener<ErrorOffendingSymbol> {
|
||||
export class ParserErrorCollector implements ParserErrorListener {
|
||||
private _errors: ParserError[];
|
||||
|
||||
constructor(error: ParserError[]) {
|
||||
super();
|
||||
this._errors = error;
|
||||
}
|
||||
|
||||
syntaxError(
|
||||
recognizer: Recognizer<ErrorOffendingSymbol>, offendingSymbol: ErrorOffendingSymbol, line: number,
|
||||
recognizer: Recognizer<ErrorOffendingSymbol, any>, offendingSymbol: ErrorOffendingSymbol, line: number,
|
||||
charPositionInLine: number, msg: string, e: RecognitionException,
|
||||
) {
|
||||
let endCol = charPositionInLine + 1;
|
||||
@ -49,16 +48,15 @@ export class ParserErrorCollector extends ErrorListener<ErrorOffendingSymbol> {
|
||||
}
|
||||
|
||||
|
||||
export default class ParserErrorListener extends ErrorListener<ErrorOffendingSymbol> {
|
||||
export default class CustomParserErrorListener implements ParserErrorListener {
|
||||
private _errorHandler;
|
||||
|
||||
constructor(errorListener: ErrorHandler<ErrorOffendingSymbol>) {
|
||||
super();
|
||||
this._errorHandler = errorListener;
|
||||
}
|
||||
|
||||
syntaxError(
|
||||
recognizer: Recognizer<ErrorOffendingSymbol>, offendingSymbol: ErrorOffendingSymbol, line: number,
|
||||
recognizer: Recognizer<ErrorOffendingSymbol, any>, offendingSymbol: ErrorOffendingSymbol, line: number,
|
||||
charPositionInLine: number, msg: string, e: any,
|
||||
) {
|
||||
let endCol = charPositionInLine + 1;
|
||||
|
Reference in New Issue
Block a user