1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package cz.zcu.mre.sparkle.tools.sparqlValidation;
21
22 import org.antlr.v4.runtime.RecognitionException;
23 import org.antlr.v4.runtime.Recognizer;
24 import org.antlr.v4.runtime.RuleContext;
25 import org.antlr.v4.runtime.Token;
26 import org.antlr.v4.runtime.atn.ATNState;
27 import org.antlr.v4.runtime.misc.IntervalSet;
28 import java.util.Objects;
29
30
31
32
33
34
35
36 public class SyntaxError {
37
38 private final Recognizer<?, ?> recognizer;
39
40 private final int line;
41 private final int charPositionInLine;
42 private String message;
43 private final String text;
44 private final int state;
45 private final ATNState atnState;
46
47 private RuleContext context;
48
49 private IntervalSet expectedTokens;
50
51 public SyntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line,
52 int charPositionInLine, String msg, RecognitionException e) {
53
54 this.recognizer = recognizer;
55 state = this.recognizer.getState();
56 atnState = recognizer.getATN().states.get(recognizer.getState());
57 context = e.getCtx();
58 expectedTokens = new IntervalSet(e.getExpectedTokens());
59
60 this.line = line;
61 this.charPositionInLine = charPositionInLine;
62 this.message = "ERROR on line " + line + "; column " + charPositionInLine + ": ";
63 this.message += msg;
64 this.message = this.message.replaceAll("\\\\n", "");
65
66
67
68
69
70
71
72 text = ((Token) offendingSymbol).getText();
73 }
74
75 public IntervalSet getExpectedTokens() {
76 return expectedTokens;
77 }
78
79 public int getLine() {
80 return line;
81 }
82
83 public int getCharPositionInLine() {
84 return charPositionInLine;
85 }
86
87 public String getMessage() {
88 return message;
89 }
90
91 public String getText() {
92 return text;
93 }
94
95 public Recognizer<?, ?> getRecognizer() {
96 return recognizer;
97 }
98
99 public int getState() {
100 return state;
101 }
102
103 public ATNState getAtnState() {
104 return atnState;
105 }
106
107 public RuleContext getContext() {
108 return context;
109 }
110
111 @Override
112 public String toString() {
113 return "SyntaxError{"
114 +
115 "line=" + line
116 +
117 ", charPositionInLine=" + charPositionInLine
118 +
119 ", text='" + text + '\''
120 +
121 '}';
122 }
123
124 @Override
125 public boolean equals(Object o) {
126 if (this == o) {
127 return true;
128 }
129 if (o == null || getClass() != o.getClass()) {
130 return false;
131 }
132 SyntaxError that = (SyntaxError) o;
133 return line == that.line
134 && charPositionInLine == that.charPositionInLine
135 && Objects.equals(message, that.message)
136 && Objects.equals(text, that.text);
137 }
138
139 @Override
140 public int hashCode() {
141
142 return Objects.hash(line, charPositionInLine, message, text);
143 }
144
145 }