-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathStringSource.java
More file actions
38 lines (31 loc) · 1022 Bytes
/
StringSource.java
File metadata and controls
38 lines (31 loc) · 1022 Bytes
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
package expression.exceptions;
public class StringSource implements ExpressionSource {
private final int ERROR_PREVIEW_SIZE = 5;
private final String data;
private int pos;
private int savedPos;
public StringSource(final String data) {
this.data = data;
}
@Override
public boolean hasNext() {
return pos < data.length();
}
@Override
public char next() {
return data.charAt(pos++);
}
@Override
public ParserException error(final String message) {
int leftEdge = Math.max(pos - ERROR_PREVIEW_SIZE, 0);
int rightEdge = Math.min(pos + ERROR_PREVIEW_SIZE, data.length());
return new ParserException("Error at char #" + (pos - 1) + ": " + (leftEdge == 0 ? "" : "…")
+ data.substring(leftEdge, rightEdge) + (rightEdge == data.length() ? "" : "…") + " : " + message);
}
public void savePos() {
savedPos = pos;
}
public void restorePos() {
pos = savedPos - 1;
}
}