-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpleErrorStrategy.java
More file actions
188 lines (177 loc) · 9 KB
/
Copy pathsimpleErrorStrategy.java
File metadata and controls
188 lines (177 loc) · 9 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
/*
* Copyright (c) 2025 - 2026 PCazzaniga (github.com)
*
* simpleErrorStrategy.java is part of SIMPLE.
*
* SIMPLE is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* SIMPLE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with SIMPLE. If not, see <http://www.gnu.org/licenses/>.
*/
import org.antlr.v4.runtime.DefaultErrorStrategy;
import org.antlr.v4.runtime.InputMismatchException;
import org.antlr.v4.runtime.NoViableAltException;
import org.antlr.v4.runtime.Parser;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.TokenStream;
import org.antlr.v4.runtime.Vocabulary;
import org.antlr.v4.runtime.misc.IntervalSet;
import java.util.regex.Pattern;
class simpleErrorStrategy extends DefaultErrorStrategy{
@Override
protected void reportMissingToken(Parser recognizer) {
if (!this.inErrorRecoveryMode(recognizer)) {
this.beginErrorCondition(recognizer);
Token t = recognizer.getCurrentToken();
IntervalSet expecting = this.getExpectedTokens(recognizer);
String msg = "Missing ";
if(expecting.size() > 1){
msg += "at " + this.getTokenErrorDisplay(t) + ":\n" + getTokenExpectations(expecting, recognizer.getVocabulary());
} else {
String descr = getDescription(expecting.get(0), recognizer.getVocabulary());
descr = Pattern.compile("^.").matcher(descr).replaceFirst(m -> m.group().toLowerCase());
msg += descr + " at " + this.getTokenErrorDisplay(t);
}
recognizer.notifyErrorListeners(t, msg, null);
}
}
@Override
protected void reportNoViableAlternative(Parser recognizer, NoViableAltException e) {
TokenStream tokens = recognizer.getInputStream();
String msg = "No viable parsing alternative ";
if (tokens != null) {
if (e.getStartToken().getType() == -1) {
msg = msg + "at <EOF>";
} else {
Token start = e.getStartToken();
String from = escapeWSAndQuote(start.getText()) + " (line " + start.getLine() + ":" + start.getCharPositionInLine() + ")";
String to= switch (e.getOffendingToken().getType()){
case simpleParser.INDENT -> "<indentation increase>";
case simpleParser.DEDENT -> "<indentation decrease>";
case simpleParser.NEWLINE -> "<newline>";
case simpleParser.TAB -> "<tabulation>";
default -> escapeWSAndQuote(e.getOffendingToken().getText());
};
msg = msg + "from " + from + " to " + to;
}
} else {
msg = msg + "at <unknown input>";
}
recognizer.notifyErrorListeners(e.getOffendingToken(), msg, e);
}
@Override
protected void reportInputMismatch(Parser recognizer, InputMismatchException e) {
Token offender = e.getOffendingToken();
String offendStr= switch (offender.getType()){
case simpleParser.INDENT -> "<indentation increase>";
case simpleParser.DEDENT -> "<indentation decrease>";
case simpleParser.NEWLINE -> "<newline>";
case simpleParser.TAB -> "<tabulation>";
default -> getTokenErrorDisplay(offender);
};
IntervalSet expecting = e.getExpectedTokens();
String msg = "Mismatched input " + offendStr + " expecting";
if(expecting.size() > 1){
msg += ":\n" + getTokenExpectations(expecting, recognizer.getVocabulary());
} else {
String descr = getDescription(expecting.get(0), recognizer.getVocabulary());
msg += " a " + Pattern.compile("^.").matcher(descr).replaceFirst(m -> m.group().toLowerCase());
}
recognizer.notifyErrorListeners(offender, msg, e);
}
@Override
protected void reportUnwantedToken(Parser recognizer) {
if (!this.inErrorRecoveryMode(recognizer)) {
this.beginErrorCondition(recognizer);
Token t = recognizer.getCurrentToken();
String tokenName = switch (t.getType()){
case simpleParser.INDENT -> "<indentation increase>";
case simpleParser.DEDENT -> "<indentation decrease>";
case simpleParser.NEWLINE -> "<newline>";
case simpleParser.TAB -> "<tabulation>";
default -> getTokenErrorDisplay(t);
};
IntervalSet expecting = this.getExpectedTokens(recognizer);
String msg = "Extraneous input " + tokenName + " expecting";
if(expecting.size() > 1){
msg += ":\n" + getTokenExpectations(expecting, recognizer.getVocabulary());
} else {
String descr = getDescription(expecting.get(0), recognizer.getVocabulary());
msg += " a " + Pattern.compile("^.").matcher(descr).replaceFirst(m -> m.group().toLowerCase());
}
recognizer.notifyErrorListeners(t, msg, null);
}
}
private String getTokenExpectations(IntervalSet tokens, Vocabulary vocabulary){
StringBuilder sb = new StringBuilder();
for (Integer i: tokens.toList()){
sb.append(" -> ").append(getDescription(i, vocabulary)).append("\n");
}
if (!sb.isEmpty()){
sb.setLength(sb.length() - 1);
}
return sb.toString();
}
private String getDescription(Integer type, Vocabulary vocabulary) {
return switch (type) {
case -1 -> "End of file";
case simpleParser.Text -> "Text literal";
case simpleParser.Boolean -> "Boolean literal";
case simpleParser.TYPENAME -> "Type alias";
case simpleParser.NAME -> "Name";
case simpleParser.NUM -> "Number literal";
case simpleParser.COMMENT -> "Comment";
case simpleParser.INDENT -> "Indentation increase";
case simpleParser.DEDENT -> "Indentation decrease";
case simpleParser.NEWLINE -> "Newline";
default -> {
String description = switch (type) {
case simpleParser.NEWTYPE -> "Type aliasing";
case simpleParser.DEFINE -> "Function declaration";
case simpleParser.RESULT -> "Function result";
case simpleParser.PARAMS,
simpleParser.PARAMS_ONLY -> "Function parameters";
case simpleParser.IF -> "Conditional statement";
case simpleParser.THEN -> "Statement entry";
case simpleParser.ELSEIF,
simpleParser.ELSE -> "Continued conditional statement";
case simpleParser.REPEAT -> "Loop statement";
case simpleParser.WHILE -> "Loop condition";
case simpleParser.TIMES -> "Loop quantification";
case simpleParser.RETURN -> "Return instruction";
case simpleParser.PREPARE -> "Declaration";
case simpleParser.NAMED -> "Naming";
case simpleParser.VALUED -> "Value initialization";
case simpleParser.SET -> "Assignment instruction";
case simpleParser.TO -> "Value assignment";
case simpleParser.INSERT -> "List insertion instruction";
case simpleParser.IN -> "List operation destination";
case simpleParser.REMOVE -> "List removal instruction";
case simpleParser.SPLIT -> "Text splitting instruction";
case simpleParser.MERGE -> "Text list merging instruction";
case simpleParser.NUMBER,
simpleParser.STRING,
simpleParser.BOOL,
simpleParser.SEQUENCE,
simpleParser.LIST,
simpleParser.KIT -> "Type";
case simpleParser.POSITION -> "Position";
case simpleParser.SIZEOF -> "Structure size";
case simpleParser.CALL -> "Function call";
case simpleParser.ARGUMENTS -> "Call arguments";
default -> "";
};
description += (!description.isEmpty() ? ", using " : "") + vocabulary.getLiteralName(type);
yield description;
}
};
}
}