-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathparseErrorListener.ts
More file actions
188 lines (173 loc) · 5.99 KB
/
parseErrorListener.ts
File metadata and controls
188 lines (173 loc) · 5.99 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import {
Token,
Recognizer,
ANTLRErrorListener,
RecognitionException,
ATNSimulator,
LexerNoViableAltException,
Lexer,
Parser,
InputMismatchException,
NoViableAltException,
} from 'antlr4ng';
import { transform } from './transform';
import { BasicSQL } from './basicSQL';
/**
* Converted from {@link SyntaxError}.
*/
export interface ParseError {
/** start at 1 */
readonly startLine: number;
/** end at ..n */
readonly endLine: number;
/** start at 1 */
readonly startColumn: number;
/** end at ..n + 1 */
readonly endColumn: number;
readonly message: string;
}
/**
* The type of error resulting from lexical parsing and parsing.
*/
export interface SyntaxError {
readonly recognizer: Recognizer<ATNSimulator>;
readonly offendingSymbol: Token | null;
readonly line: number;
readonly charPositionInLine: number;
readonly msg: string;
readonly e: RecognitionException;
}
/**
* ErrorListener will be invoked when it encounters a parsing error.
* Includes lexical errors and parsing errors.
*/
export type ErrorListener = (parseError: ParseError, originalError: SyntaxError) => void;
export abstract class ParseErrorListener implements ANTLRErrorListener {
private _errorListener: ErrorListener;
protected preferredRules: Set<number>;
protected get locale() {
return this.parserContext.locale;
}
protected parserContext: BasicSQL;
constructor(
errorListener: ErrorListener,
parserContext: BasicSQL,
preferredRules: Set<number>
) {
this.parserContext = parserContext;
this.preferredRules = preferredRules;
this._errorListener = errorListener;
}
reportAmbiguity() {}
reportAttemptingFullContext() {}
reportContextSensitivity() {}
protected abstract getExpectedText(parser: Parser, token: Token): string;
syntaxError(
recognizer: Recognizer<ATNSimulator>,
offendingSymbol: Token | null,
line: number,
charPositionInLine: number,
msg: string,
e: RecognitionException
) {
let message = '';
// If not undefined then offendingSymbol is of type Token.
if (offendingSymbol) {
let token = offendingSymbol as Token;
const parser = recognizer as Parser;
// judge token is EOF
const isEof = token.type === Token.EOF;
if (isEof) {
token = parser.tokenStream.get(token.tokenIndex - 1);
}
const wrongText = token.text ?? '';
const isInComplete = isEof && wrongText !== ' ';
const expectedText = isInComplete ? '' : this.getExpectedText(parser, token);
if (!e) {
// handle missing or unwanted tokens.
message = msg;
if (msg.includes('extraneous')) {
message = `'${wrongText}' {noValidPosition}${
expectedText.length ? `{expecting}${expectedText}` : ''
}`;
}
if (msg.includes('missing')) {
const regex = /missing\s+'([^']+)'/;
const match = msg.match(regex);
message = `{missing}`;
if (match) {
const missKeyword = match[1];
message += `'${missKeyword}'`;
} else {
message += `{keyword}`;
}
message += `{at}'${wrongText}'`;
}
} else {
// handle mismatch exception or no viable alt exception
if (e instanceof InputMismatchException || e instanceof NoViableAltException) {
if (isEof) {
message = `{stmtInComplete}`;
} else {
message = `'${wrongText}' {noValidPosition}`;
}
if (expectedText.length > 0) {
message += `{expecting}${expectedText}`;
}
} else {
message = msg;
}
}
} else {
// No offending symbol, which indicates this is a lexer error.
if (e instanceof LexerNoViableAltException) {
const lexer = recognizer as Lexer;
const input = lexer.inputStream;
let text = lexer.getErrorDisplay(
input.getText(lexer._tokenStartCharIndex, input.index)
);
switch (text[0]) {
case '/':
message = '{unfinishedMultilineComment}';
break;
case '"':
message = '{unfinishedDoubleQuoted}';
break;
case "'":
message = '{unfinishedSingleQuoted}';
break;
case '`':
message = '{unfinishedTickQuoted}';
break;
default:
message = '"' + text + '" {noValidInput}';
break;
}
}
}
message = transform(message, this.locale);
let endCol = charPositionInLine + 1;
if (offendingSymbol && offendingSymbol.text !== null) {
endCol = charPositionInLine + offendingSymbol.text.length;
}
if (this._errorListener) {
this._errorListener(
{
startLine: line,
endLine: line,
startColumn: charPositionInLine + 1,
endColumn: endCol + 1,
message,
},
{
e,
line,
msg,
recognizer,
offendingSymbol,
charPositionInLine,
}
);
}
}
}