-
Notifications
You must be signed in to change notification settings - Fork 0
/
ErrorHandlerImpl.java
96 lines (84 loc) · 2.93 KB
/
ErrorHandlerImpl.java
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
package org.example.error;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.example.interpreter.error.InterpreterException;
import org.example.lexer.CharactersUtility;
import org.example.lexer.LexerConfiguration;
import org.example.lexer.PositionalReader;
import org.example.lexer.PositionalReaderImpl;
import org.example.lexer.error.LexerException;
import org.example.parser.error.ParserException;
import org.example.token.Position;
@ToString
@Slf4j
public class ErrorHandlerImpl implements ErrorHandler {
private final Map<Position, Set<PositionalException>> exceptions = new HashMap<>();
@SuppressWarnings("StatementWithEmptyBody")
@Override
public void showExceptions(Reader reader) throws IOException {
var positionalReader = new PositionalReaderImpl(reader);
while (processLine(positionalReader)) ;
exceptions.clear();
}
private boolean processLine(PositionalReader reader) throws IOException {
var lineBuilder = formatLine(reader);
var exceptionsOnLine = new ArrayList<PositionalException>();
while (true) {
var read = reader.read();
if (read == CharactersUtility.END_OF_FILE) {
logLine(lineBuilder.toString(), exceptionsOnLine);
return false;
}
var character = Character.toString(read);
if (StringUtils.equals(character, CharactersUtility.NEW_LINE)) {
logLine(lineBuilder.toString(), exceptionsOnLine);
return true;
} else if (lineBuilder.length() < LexerConfiguration.MAX_STRING_LENGTH) {
lineBuilder.append(character);
}
var currentPosition = reader.getPosition();
var exceptionsAtPosition = exceptions.getOrDefault(currentPosition, Set.of());
exceptionsOnLine.addAll(exceptionsAtPosition);
}
}
private void logLine(String line, List<PositionalException> exceptionsOnLine) {
if (!exceptionsOnLine.isEmpty()) {
log.info(line);
for (var exception : exceptionsOnLine) {
log.error(exception.getMessage());
}
}
}
private StringBuilder formatLine(PositionalReader reader) {
var line = String.format("%4d: ", reader.getLine());
return new StringBuilder(line);
}
@Override
public void handleLexerException(LexerException exception) {
handleException(exception);
}
@Override
public void handleParserException(ParserException exception) {
handleException(exception);
}
@Override
public void handleInterpreterException(InterpreterException exception) {
handleException(exception);
}
private void handleException(PositionalException exception) {
var exceptionsAtPosition = exceptions.computeIfAbsent(exception.getPosition(), position -> new HashSet<>());
exceptionsAtPosition.add(exception);
if (exceptions.size() > ErrorHandlerConfiguration.MAX_ERRORS) {
throw new TooManyExceptionsException();
}
}
}