forked from DTStack/dt-sql-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrorStrategy.ts
More file actions
73 lines (66 loc) · 2.13 KB
/
Copy patherrorStrategy.ts
File metadata and controls
73 lines (66 loc) · 2.13 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
import {
DefaultErrorStrategy,
Parser,
InputMismatchException,
IntervalSet,
ParserRuleContext,
RecognitionException,
Token,
} from 'antlr4ng';
/**
* Base on DefaultErrorStrategy.
* The difference is that it assigns exception to the context.exception when it encounters error.
*/
export class ErrorStrategy extends DefaultErrorStrategy {
public override recover(recognizer: Parser, e: RecognitionException): void {
// Mark the context as an anomaly
for (
let context: ParserRuleContext | null = recognizer.context;
context;
context = context.parent
) {
context.exception = e;
}
// Error recovery
if (
this.lastErrorIndex === recognizer.inputStream.index &&
this.lastErrorStates &&
this.lastErrorStates.contains(recognizer.state)
) {
recognizer.consume();
}
this.lastErrorIndex = recognizer.inputStream.index;
if (!this.lastErrorStates) {
this.lastErrorStates = new IntervalSet();
}
this.lastErrorStates.addOne(recognizer.state);
let followSet: IntervalSet = this.getErrorRecoverySet(recognizer);
this.consumeUntil(recognizer, followSet);
}
public override recoverInline(recognizer: Parser): Token {
let e: RecognitionException;
if (this.nextTokensContext === undefined) {
e = new InputMismatchException(recognizer);
} else {
e = new InputMismatchException(recognizer);
}
// Mark the context as an anomaly
for (
let context: ParserRuleContext | null = recognizer.context;
context;
context = context.parent
) {
context.exception = e;
}
// Error recovery
let matchedSymbol = this.singleTokenDeletion(recognizer);
if (matchedSymbol) {
recognizer.consume();
return matchedSymbol;
}
if (this.singleTokenInsertion(recognizer)) {
return this.getMissingSymbol(recognizer);
}
throw e;
}
}