forked from DTStack/dt-sql-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparserErrorListener.ts
More file actions
84 lines (73 loc) · 2.16 KB
/
Copy pathparserErrorListener.ts
File metadata and controls
84 lines (73 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { Token, Recognizer } from 'antlr4';
import { ErrorListener } from 'antlr4/error';
export interface ParserError {
startLine: number;
endLine: number;
startCol: number;
endCol: number;
message: string;
}
export interface SyntaxError {
recognizer: Recognizer;
offendingSymbol: Token;
line: number;
charPositionInLine: number;
msg: string;
e: any;
}
export type ErrorHandler = (err: ParserError, errOption: SyntaxError) => void;
export class ParserErrorCollector extends ErrorListener {
private _errors: ParserError[];
constructor(error: ParserError[]) {
super();
this._errors = error;
}
syntaxError(
recognizer: Recognizer, offendingSymbol: Token, line: number,
charPositionInLine: number, msg: string, e: any,
) {
let endCol = charPositionInLine + 1;
if (offendingSymbol &&offendingSymbol.text !== null) {
endCol = charPositionInLine + offendingSymbol.text.length;
}
this._errors.push({
startLine: line,
endLine: line,
startCol: charPositionInLine,
endCol: endCol,
message: msg,
});
}
}
export default class ParserErrorListener extends ErrorListener {
private _errorHandler;
constructor(errorListener: ErrorHandler) {
super();
this._errorHandler = errorListener;
}
syntaxError(
recognizer: Recognizer, offendingSymbol: Token, line: number,
charPositionInLine: number, msg: string, e: any,
) {
let endCol = charPositionInLine + 1;
if (offendingSymbol &&offendingSymbol.text !== null) {
endCol = charPositionInLine + offendingSymbol.text.length;
}
if (this._errorHandler) {
this._errorHandler({
startLine: line,
endLine: line,
startCol: charPositionInLine,
endCol: endCol,
message: msg,
}, {
e,
line,
msg,
recognizer,
offendingSymbol,
charPositionInLine,
});
}
}
}