-
Notifications
You must be signed in to change notification settings - Fork 249
Expand file tree
/
Copy pathEoErrorRecoveryStrategy.java
More file actions
66 lines (62 loc) 路 2.16 KB
/
Copy pathEoErrorRecoveryStrategy.java
File metadata and controls
66 lines (62 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
/*
* SPDX-FileCopyrightText: Copyright (c) 2016-2025 Objectionary.com
* SPDX-License-Identifier: MIT
*/
package org.eolang.parser;
import org.antlr.v4.runtime.DefaultErrorStrategy;
import org.antlr.v4.runtime.Parser;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.TokenStream;
/**
* Custom error recovery strategy for EO parser.
* When encountering errors in object declarations, skip to the next
* object at the same indentation level to allow parsing to continue.
*
* @since 0.1
*/
final class EoErrorRecoveryStrategy extends DefaultErrorStrategy {
@Override
public void recover(
final Parser recognizer,
final RecognitionException exc
) {
final String rule = recognizer.getRuleInvocationStack().get(0);
final String[] names = recognizer.getRuleNames();
if (names[EoParser.RULE_bound].equals(rule)
|| names[EoParser.RULE_object].equals(rule)) {
skipToNextObjectAtSameLevel(recognizer);
} else {
super.recover(recognizer, exc);
}
}
/**
* Skip tokens until we find the start of the next object
* at the same indentation level.
* @param recognizer The parser
*/
private static void skipToNextObjectAtSameLevel(final Parser recognizer) {
final TokenStream tokens = recognizer.getInputStream();
int index = recognizer.getCurrentToken().getTokenIndex();
while (index < tokens.size()) {
final Token token = tokens.get(index);
if (token.getType() == Token.EOF) {
break;
}
if (token.getType() == EoParser.UNTAB) {
break;
}
if (token.getType() == EoParser.EOL && index + 1 < tokens.size()) {
final Token next = tokens.get(index + 1);
if (next.getType() == EoParser.LSQ) {
recognizer.getInputStream().seek(index);
return;
}
}
index = index + 1;
}
if (index < tokens.size()) {
recognizer.getInputStream().seek(index);
}
}
}